summaryrefslogtreecommitdiff
path: root/tests/testbidirsortedmap.vala
blob: 385d71566be7ae37b89c3231e257ad914ec07b62 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* testbidirsortedmap.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 BidirSortedMapTests : SortedMapTests {
	public BidirSortedMapTests(string name) {
		base (name);
		add_test ("[SortedMap] bi-directional iterators can go backward",
		          test_bidir_iterator_can_go_backward);
		add_test ("[SortedSet] bi-directional iterators can to end",
		          test_bidir_iterator_last);
		get_suite ().add_suite (new BidirSubMapTests (this, SortedMapTests.SubMapTests.Type.HEAD).get_suite ());
		get_suite ().add_suite (new BidirSubMapTests (this, SortedMapTests.SubMapTests.Type.TAIL).get_suite ());
		get_suite ().add_suite (new BidirSubMapTests (this, SortedMapTests.SubMapTests.Type.SUB).get_suite ());
		get_suite ().add_suite (new BidirSubMapTests (this, SortedMapTests.SubMapTests.Type.EMPTY).get_suite ());
	}

	public void test_bidir_iterator_can_go_backward () {
		var test_sorted_map = test_map as BidirSortedMap<string,string>;
		var keys = (test_sorted_map.ascending_keys) as BidirSortedSet<string>;
		var entries = (test_sorted_map.ascending_entries) as BidirSortedSet<Map.Entry<string,string>>;

		var keys_iterator = keys.bidir_iterator ();
		var entries_iterator = entries.bidir_iterator ();
		var map_iterator = test_sorted_map.bidir_map_iterator ();

		assert (!keys_iterator.has_previous ());
		assert (!entries_iterator.has_previous ());

		assert (!map_iterator.has_previous ());
		assert (!keys_iterator.previous ());
		assert (!entries_iterator.has_previous ());

		assert (!map_iterator.previous ());

		test_sorted_map.set ("one", "one");
		test_sorted_map.set ("two", "two");
		test_sorted_map.set ("three", "three");
		test_sorted_map.set ("four", "four");
		test_sorted_map.set ("five", "five");
		test_sorted_map.set ("six", "six");

		keys_iterator = keys.bidir_iterator ();
		entries_iterator = entries.bidir_iterator ();
		map_iterator = test_sorted_map.bidir_map_iterator ();

		assert (keys_iterator.next ());
		assert (entries_iterator.next ());

		assert (map_iterator.next ());
		assert (keys_iterator.get () == "five");
		assert_entry (entries_iterator.get (), "five", "five");

		assert (map_iterator.get_key () == "five");
		assert (map_iterator.get_value () == "five");

		assert (!keys_iterator.has_previous ());
		assert (!entries_iterator.has_previous ());

		assert (!map_iterator.has_previous ());
		assert (keys_iterator.next ());
		assert (entries_iterator.next ());

		assert (map_iterator.next ());
		assert (keys_iterator.get () == "four");
		assert_entry (entries_iterator.get (), "four", "four");

		assert (map_iterator.get_key () == "four");
		assert (map_iterator.get_value () == "four");

		assert (keys_iterator.has_previous ());
		assert (entries_iterator.has_previous ());

		assert (map_iterator.has_previous ());
		assert (keys_iterator.next ());
		assert (entries_iterator.next ());

		assert (map_iterator.next ());
		assert (keys_iterator.get () == "one");
		assert_entry (entries_iterator.get (), "one", "one");

		assert (map_iterator.get_key () == "one");
		assert (map_iterator.get_value () == "one");

		assert (keys_iterator.has_previous ());
		assert (entries_iterator.has_previous ());

		assert (map_iterator.has_previous ());
		assert (keys_iterator.next ());
		assert (entries_iterator.next ());

		assert (map_iterator.next ());
		assert (keys_iterator.get () == "six");
		assert_entry (entries_iterator.get (), "six", "six");

		assert (map_iterator.get_key () == "six");
		assert (map_iterator.get_value () == "six");
		assert (keys_iterator.has_previous ());

		assert (entries_iterator.has_previous ());
		assert (map_iterator.has_previous ());
		assert (keys_iterator.next ());

		assert (entries_iterator.next ());
		assert (map_iterator.next ());
		assert (keys_iterator.get () == "three");

		assert_entry (entries_iterator.get (), "three", "three");
		assert (map_iterator.get_key () == "three");
		assert (map_iterator.get_value () == "three");

		assert (keys_iterator.has_previous ());
		assert (entries_iterator.has_previous ());
		assert (map_iterator.has_previous ());

		assert (keys_iterator.next ());
		assert (entries_iterator.next ());
		assert (map_iterator.next ());

		assert (keys_iterator.get () == "two");
		assert_entry (entries_iterator.get (), "two", "two");
		assert (map_iterator.get_key () == "two");
		assert (map_iterator.get_value () == "two");

		assert (keys_iterator.has_previous ());
		assert (entries_iterator.has_previous ());
		assert (map_iterator.has_previous ());

		assert (!keys_iterator.next ());
		assert (!entries_iterator.next ());
		assert (!map_iterator.next ());

		assert (keys_iterator.previous ());
		assert (entries_iterator.previous ());
		assert (map_iterator.previous ());

		assert (keys_iterator.get () == "three");
		assert_entry (entries_iterator.get (), "three", "three");
		assert (map_iterator.get_key () == "three");
		assert (map_iterator.get_value () == "three");

		assert (keys_iterator.previous ());
		assert (entries_iterator.previous ());
		assert (map_iterator.previous ());

		assert (keys_iterator.get () == "six");
		assert_entry (entries_iterator.get (), "six", "six");
		assert (map_iterator.get_key () == "six");
		assert (map_iterator.get_value () == "six");

		assert (keys_iterator.previous ());
		assert (entries_iterator.previous ());
		assert (map_iterator.previous ());

		assert (keys_iterator.get () == "one");
		assert_entry (entries_iterator.get (), "one", "one");
		assert (map_iterator.get_key () == "one");
		assert (map_iterator.get_value () == "one");

		assert (keys_iterator.previous ());
		assert (entries_iterator.previous ());
		assert (map_iterator.previous ());

		assert (keys_iterator.get () == "four");
		assert_entry (entries_iterator.get (), "four", "four");
		assert (map_iterator.get_key () == "four");
		assert (map_iterator.get_value () == "four");

		assert (keys_iterator.previous ());
		assert (entries_iterator.previous ());
		assert (map_iterator.previous ());

		assert (keys_iterator.get () == "five");
		assert_entry (entries_iterator.get (), "five", "five");
		assert (map_iterator.get_key () == "five");
		assert (map_iterator.get_value () == "five");

		assert (!keys_iterator.previous ());
		assert (!entries_iterator.previous ());
		assert (!map_iterator.previous ());

		assert (keys_iterator.get () == "five");
		assert_entry (entries_iterator.get (), "five", "five");
		assert (map_iterator.get_key () == "five");
		assert (map_iterator.get_value () == "five");
	}

	public void test_bidir_iterator_last () {
		var test_sorted_map = test_map as BidirSortedMap<string,string>;
		var keys = (test_sorted_map.ascending_keys) as BidirSortedSet<string>;
		var entries = (test_sorted_map.ascending_entries) as BidirSortedSet<Map.Entry<string,string>>;

		var keys_iterator = keys.bidir_iterator ();
		var entries_iterator = entries.bidir_iterator ();

		assert (!keys_iterator.last ());
		assert (!entries_iterator.last ());

		test_sorted_map.set ("one", "one");
		test_sorted_map.set ("two", "two");
		test_sorted_map.set ("three", "three");
		test_sorted_map.set ("four", "four");
		test_sorted_map.set ("five", "five");
		test_sorted_map.set ("six", "six");

		keys_iterator = keys.bidir_iterator ();
		entries_iterator = entries.bidir_iterator ();

		assert (keys_iterator.last ());
		assert (entries_iterator.last ());

		assert (keys_iterator.get () == "two");
		assert_entry (entries_iterator.get (), "two", "two");
	}


	public class BidirSubMapTests : Gee.TestCase {
		private BidirSortedMap<string,string> master;
		private BidirSortedMap<string,string> submap;
		private BidirSortedMapTests test;
		private SortedMapTests.SubMapTests.Type type;

		public BidirSubMapTests (BidirSortedMapTests test, SortedMapTests.SubMapTests.Type type) {
			base ("%s Subset".printf (type.to_string ()));
			this.test = test;
			this.type = type;
			add_test ("[BidirSortedSet] bi-directional iterator", test_bidir_iterators);
		}

		public override void set_up () {
			test.set_up ();
			master = test.test_map as BidirSortedMap<string,string>;
			switch (type) {
			case SortedMapTests.SubMapTests.Type.HEAD:
				submap = master.head_map ("one") as BidirSortedMap<string,string>; break;
			case SortedMapTests.SubMapTests.Type.TAIL:
				submap = master.tail_map ("six") as BidirSortedMap<string,string>; break;
			case SortedMapTests.SubMapTests.Type.SUB:
				submap = master.sub_map ("four", "three") as BidirSortedMap<string,string>; break;
			case SortedMapTests.SubMapTests.Type.EMPTY:
				submap = master.sub_map ("three", "four") as BidirSortedMap<string,string>; break;
			default:
				assert_not_reached ();
			}
		}

		public override void tear_down () {
			test.tear_down ();
		}

		protected void set_default_values (out string[] contains = null, out string[] not_contains = null) {
			master.set ("one", "one");
			master.set ("two", "two");
			master.set ("three", "three");
			master.set ("four", "four");
			master.set ("five", "five");
			master.set ("six", "six");
			switch (type) {
			case SortedMapTests.SubMapTests.Type.HEAD:
				contains = {"five", "four"};
				not_contains = {"one", "two", "three", "six"};
				break;
			case SortedMapTests.SubMapTests.Type.TAIL:
				contains = {"six", "three", "two"};
				not_contains = {"one", "four", "five"};
				break;
			case SortedMapTests.SubMapTests.Type.SUB:
				contains = {"four", "one", "six"};
				not_contains = {"two", "three", "five"};
				break;
			case SortedMapTests.SubMapTests.Type.EMPTY:
				contains = {};
				not_contains = {"one", "two", "three", "four", "five", "six"};
				break;
			default:
				assert_not_reached ();
			}
		}

		public void test_bidir_iterators () {
			string[] contains, not_contains;

			var _map_iter = submap.bidir_map_iterator ();

			assert (!_map_iter.has_next ());
			assert (!_map_iter.next ());
			assert (!_map_iter.has_previous ());
			assert (!_map_iter.previous ());
			
			set_default_values (out contains, out not_contains);
			
			var i = 0;
			_map_iter = submap.bidir_map_iterator ();
			while (_map_iter.next ()) {
				assert (_map_iter.get_key () == contains[i]);
				assert (_map_iter.get_value () == contains[i]);
				i++;
			}
			assert (i == contains.length);
			
			i = 0;
			foreach (var k in submap.keys)
				assert (k == contains[i++]);
			assert (i == contains.length);
			
			i = 0;
			foreach (var e in submap.entries) {
				MapTests.assert_entry (e, contains[i], contains[i]);
				i++;
			}
			assert (i == contains.length);
			
			var keys_iter = ((submap.ascending_keys) as BidirSortedSet<string>).bidir_iterator ();
			var entries_iter = ((submap.ascending_entries) as BidirSortedSet<Map.Entry<string,string>>).bidir_iterator ();
			var map_iter = submap.bidir_map_iterator ();
			if (type != SortedMapTests.SubMapTests.Type.EMPTY) {
				assert (map_iter.last ());
				assert (map_iter.get_key () == contains[contains.length - 1]);
				assert (map_iter.get_value () == contains[contains.length - 1]);

				map_iter = submap.bidir_map_iterator ();
				assert (map_iter.next ());

				assert (map_iter.get_key () == contains[0]);
				assert (map_iter.get_value () == contains[0]);

				assert (map_iter.has_next ());
				assert (map_iter.next ());
				assert (map_iter.get_key () == contains[1]);
				assert (map_iter.get_value () == contains[1]);

				assert (map_iter.has_previous ());
				map_iter.unset ();
				assert (map_iter.has_previous ());
				if (type != SortedMapTests.SubMapTests.Type.HEAD)
					assert (map_iter.has_next ());
				else
					assert (!map_iter.has_next ());
				assert (map_iter.previous ());
				assert (map_iter.get_key () == contains[0]);
				assert (map_iter.get_value () == contains[0]);
				
				// Repeat for keys
				master.clear ();
				set_default_values (out contains, out not_contains);
				
				assert (keys_iter.last ());
				assert (keys_iter.get () == contains[contains.length - 1]);
				assert (keys_iter.first ());

				assert (keys_iter.get () == contains[0]);
				assert (keys_iter.has_next ());
				assert (keys_iter.next ());
				assert (keys_iter.get () == contains[1]);
				assert (keys_iter.has_previous ());
				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
				                       TestTrapFlags.SILENCE_STDERR)) {
					keys_iter.remove ();
					Posix.exit (0);
				}
				assert (keys_iter.has_previous ());
				if (type != SortedMapTests.SubMapTests.Type.HEAD)
					assert (keys_iter.has_next ());
				else
					assert (!keys_iter.has_next ());
				assert (keys_iter.previous ());
				assert (keys_iter.get () == contains[0]);
				
				// Repeat for entries
				master.clear ();
				set_default_values (out contains, out not_contains);

				assert (entries_iter.last ());
				MapTests.assert_entry (entries_iter.get (), contains[contains.length - 1], contains[contains.length - 1]);
				assert (entries_iter.first ());

				MapTests.assert_entry (entries_iter.get (), contains[0], contains[0]);
				assert (entries_iter.has_next ());
				assert (entries_iter.next ());
				MapTests.assert_entry (entries_iter.get (), contains[1], contains[1]);
				assert (entries_iter.has_previous ());
				entries_iter.remove ();
				assert (entries_iter.has_previous ());
				if (type != SortedMapTests.SubMapTests.Type.HEAD)
					assert (entries_iter.has_next ());
				else
					assert (!entries_iter.has_next ());
				assert (entries_iter.previous ());
				MapTests.assert_entry (entries_iter.get (), contains[0], contains[0]);
			} else {
				assert (!keys_iter.first ());
				assert (!keys_iter.last ());
				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
				                       TestTrapFlags.SILENCE_STDERR)) {
					keys_iter.remove ();
					Posix.exit (0);
				}
				Test.trap_assert_failed ();
				assert (!entries_iter.first ());
				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
				                       TestTrapFlags.SILENCE_STDERR)) {
					entries_iter.remove ();
					Posix.exit (0);
				}
				Test.trap_assert_failed ();
			}
		}
	}
}