1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* testbidirsortedset.vala
*
* Copyright (C) 2012 Maciej Piechotka
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Maciej Piechotka <uzytkownik2@gmail.com>
*/
using GLib;
using Gee;
public abstract class BidirSortedSetTests : SortedSetTests {
public BidirSortedSetTests(string name) {
base (name);
add_test ("[SortedSet] bi-directional iterators can go backward",
test_bidir_iterator_can_go_backward);
add_test ("[SortedSet] bi-directional iterators are mutable",
test_mutable_bidir_iterator);
add_test ("[SortedSet] bi-directional iterators can to beginning",
test_bidir_iterator_first);
add_test ("[SortedSet] bi-directional iterators can to end",
test_bidir_iterator_last);
get_suite ().add_suite (new BidirSubSetTests (this, SortedSetTests.SubSetTests.Type.HEAD).get_suite ());
get_suite ().add_suite (new BidirSubSetTests (this, SortedSetTests.SubSetTests.Type.TAIL).get_suite ());
get_suite ().add_suite (new BidirSubSetTests (this, SortedSetTests.SubSetTests.Type.SUB).get_suite ());
get_suite ().add_suite (new BidirSubSetTests (this, SortedSetTests.SubSetTests.Type.EMPTY).get_suite ());
}
public void test_bidir_iterator_can_go_backward () {
var test_set = test_collection as BidirSortedSet<string>;
var iterator = test_set.bidir_iterator ();
assert (!iterator.has_previous ());
assert (test_set.add ("one"));
assert (test_set.add ("two"));
assert (test_set.add ("three"));
assert (test_set.add ("four"));
assert (test_set.add ("five"));
assert (test_set.add ("six"));
iterator = test_set.bidir_iterator ();
assert (iterator.next ());
assert (iterator.get () == "five");
assert (!iterator.has_previous ());
assert (iterator.next ());
assert (iterator.get () == "four");
assert (iterator.has_previous ());
assert (iterator.next ());
assert (iterator.get () == "one");
assert (iterator.has_previous ());
assert (iterator.next ());
assert (iterator.get () == "six");
assert (iterator.has_previous ());
assert (iterator.next ());
assert (iterator.get () == "three");
assert (iterator.has_previous ());
assert (iterator.next ());
assert (iterator.get () == "two");
assert (iterator.has_previous ());
assert (!iterator.next ());
assert (iterator.previous ());
assert (iterator.get () == "three");
assert (iterator.previous ());
assert (iterator.get () == "six");
assert (iterator.previous ());
assert (iterator.get () == "one");
assert (iterator.previous ());
assert (iterator.get () == "four");
assert (iterator.previous ());
assert (iterator.get () == "five");
assert (!iterator.previous ());
assert (iterator.get () == "five");
}
public void test_bidir_iterator_first () {
var test_set = test_collection as BidirSortedSet<string>;
var iterator = test_set.bidir_iterator ();
assert (!iterator.first ());
assert (test_set.add ("one"));
assert (test_set.add ("two"));
assert (test_set.add ("three"));
assert (test_set.add ("four"));
assert (test_set.add ("five"));
assert (test_set.add ("six"));
iterator = test_set.bidir_iterator ();
assert (iterator.last ());
assert (iterator.get () == "two");
assert (iterator.first ());
assert (iterator.get () == "five");
}
public void test_bidir_iterator_last () {
var test_set = test_collection as BidirSortedSet<string>;
var iterator = test_set.bidir_iterator ();
assert (!iterator.last ());
assert (test_set.add ("one"));
assert (test_set.add ("two"));
assert (test_set.add ("three"));
assert (test_set.add ("four"));
assert (test_set.add ("five"));
assert (test_set.add ("six"));
iterator = test_set.bidir_iterator ();
assert (iterator.last ());
assert (iterator.get () == "two");
}
public void test_mutable_bidir_iterator () {
var test_set = test_collection as BidirSortedSet<string>;
var iterator = test_set.bidir_iterator ();
assert (!iterator.has_previous ());
assert (test_set.add ("one"));
assert (test_set.add ("two"));
assert (test_set.add ("three"));
assert (test_set.add ("four"));
assert (test_set.add ("five"));
assert (test_set.add ("six"));
iterator = test_set.bidir_iterator ();
if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
TestTrapFlags.SILENCE_STDERR)) {
iterator.remove ();
Posix.exit (0);
}
Test.trap_assert_failed ();
assert (iterator.next ());
assert (iterator.get () == "five");
iterator.remove ();
assert (!test_set.contains ("five"));
assert (iterator.has_next ());
assert (!iterator.has_previous ());
if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
TestTrapFlags.SILENCE_STDERR)) {
iterator.get ();
Posix.exit (0);
}
assert (!iterator.previous ());
assert (iterator.next ());
assert (iterator.get () == "four");
assert (iterator.next ());
assert (iterator.get () == "one");
iterator.remove ();
assert (!test_set.contains ("one"));
assert (iterator.has_next ());
assert (iterator.has_previous ());
assert (iterator.previous ());
assert (iterator.get () == "four");
}
public class BidirSubSetTests : Gee.TestCase {
private BidirSortedSet<string> master;
private BidirSortedSet<string> subset;
private BidirSortedSetTests test;
private SortedSetTests.SubSetTests.Type type;
public BidirSubSetTests(BidirSortedSetTests test, SortedSetTests.SubSetTests.Type type) {
base ("%s Subset".printf (type.to_string ()));
this.test = test;
this.type = type;
add_test ("[BidirSortedSet] bi-directional iterator", test_bidir_iterator);
}
public override void set_up () {
test.set_up ();
master = test.test_collection as BidirSortedSet<string>;
switch (type) {
case SortedSetTests.SubSetTests.Type.HEAD:
subset = master.head_set ("one") as BidirSortedSet<string>; break;
case SortedSetTests.SubSetTests.Type.TAIL:
subset = master.tail_set ("six") as BidirSortedSet<string>; break;
case SortedSetTests.SubSetTests.Type.SUB:
subset = master.sub_set ("four", "three") as BidirSortedSet<string>; break;
case SortedSetTests.SubSetTests.Type.EMPTY:
subset = master.sub_set ("three", "four") as BidirSortedSet<string>; break;
default:
assert_not_reached ();
}
}
public override void tear_down () {
test.tear_down ();
}
public void test_bidir_iterator () {
assert (master.add ("one"));
assert (master.add ("two"));
assert (master.add ("three"));
assert (master.add ("four"));
assert (master.add ("five"));
assert (master.add ("six"));
assert (master.size == 6);
string[] contains;
switch (type) {
case SortedSetTests.SubSetTests.Type.HEAD:
contains = {"five", "four"};
break;
case SortedSetTests.SubSetTests.Type.TAIL:
contains = {"six", "three", "two"};
break;
case SortedSetTests.SubSetTests.Type.SUB:
contains = {"four", "one", "six"};
break;
case SortedSetTests.SubSetTests.Type.EMPTY:
contains = {};
break;
default:
assert_not_reached ();
}
uint i = 0;
foreach (var e in subset) {
assert (e == contains[i++]);
}
assert (i == contains.length);
var iter = subset.bidir_iterator ();
if (type != SortedSetTests.SubSetTests.Type.EMPTY) {
assert (iter.last ());
assert (iter.get () == contains[contains.length - 1]);
assert (iter.first ());
assert (iter.get () == contains[0]);
assert (iter.has_next ());
assert (iter.next ());
assert (iter.get () == contains[1]);
assert (iter.has_previous ());
iter.remove ();
assert (iter.has_previous ());
if (type != SortedSetTests.SubSetTests.Type.HEAD)
assert (iter.has_next ());
else
assert (!iter.has_next ());
assert (iter.previous ());
assert (iter.get () == contains[0]);
} else {
assert (!iter.first ());
if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
TestTrapFlags.SILENCE_STDERR)) {
iter.remove ();
Posix.exit (0);
}
Test.trap_assert_failed ();
}
}
}
}
|