1/* sortedset.vala
2 *
3 * Copyright (C) 2009-2011  Maciej Piechotka
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18 *
19 * Author:
20 * 	Maciej Piechotka <uzytkownik2@gmail.com>
21 */
22
23public abstract class Gee.SortedMapTests : MapTests {
24	protected SortedMapTests (string name) {
25		base (name);
26		add_test ("[SortedMap] key ordering", test_key_ordering);
27		add_test ("[SortedMap] first", test_first);
28		add_test ("[SortedMap] last", test_last);
29		add_test ("[SortedMap] iterator_at", test_iterator_at);
30		add_test ("[SortedMap] lower", test_lower);
31		add_test ("[SortedMap] higher", test_higher);
32		add_test ("[SortedMap] floor", test_floor);
33		add_test ("[SortedMap] ceil", test_ceil);
34		get_suite ().add_suite (new SubMapTests (this, SubMapTests.Type.HEAD).get_suite ());
35		get_suite ().add_suite (new SubMapTests (this, SubMapTests.Type.TAIL).get_suite ());
36		get_suite ().add_suite (new SubMapTests (this, SubMapTests.Type.SUB).get_suite ());
37		get_suite ().add_suite (new SubMapTests (this, SubMapTests.Type.EMPTY).get_suite ());
38	}
39
40	public void test_key_ordering () {
41		var test_sorted_map = test_map as SortedMap<string,string>;
42
43		// Check the map exists
44		assert (test_sorted_map != null);
45
46		test_sorted_map.set ("one", "one");
47		test_sorted_map.set ("two", "two");
48		test_sorted_map.set ("three", "three");
49		test_sorted_map.set ("four", "four");
50		test_sorted_map.set ("five", "five");
51		test_sorted_map.set ("six", "six");
52		test_sorted_map.set ("seven", "seven");
53		test_sorted_map.set ("eight", "eight");
54		test_sorted_map.set ("nine", "nine");
55		test_sorted_map.set ("ten", "ten");
56		test_sorted_map.set ("eleven", "eleven");
57		test_sorted_map.set ("twelve", "twelve");
58
59		Iterator<string> iterator = test_sorted_map.keys.iterator ();
60		assert (iterator.next ());
61		assert (iterator.get () == "eight");
62		assert (iterator.next ());
63		assert (iterator.get () == "eleven");
64		assert (iterator.next ());
65		assert (iterator.get () == "five");
66		assert (iterator.next ());
67		assert (iterator.get () == "four");
68		assert (iterator.next ());
69		assert (iterator.get () == "nine");
70		assert (iterator.next ());
71		assert (iterator.get () == "one");
72		assert (iterator.next ());
73		assert (iterator.get () == "seven");
74		assert (iterator.next ());
75		assert (iterator.get () == "six");
76		assert (iterator.next ());
77		assert (iterator.get () == "ten");
78		assert (iterator.next ());
79		assert (iterator.get () == "three");
80		assert (iterator.next ());
81		assert (iterator.get () == "twelve");
82		assert (iterator.next ());
83		assert (iterator.get () == "two");
84		assert (iterator.next () == false);
85	}
86
87	public void test_first () {
88		var test_sorted_map = test_map as SortedMap<string,string>;
89		var keys = test_sorted_map.ascending_keys;
90		var entries = test_sorted_map.ascending_entries;
91
92		// Check the map exists
93		assert (test_sorted_map != null);
94
95		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
96		                       TestTrapFlags.SILENCE_STDERR)) {
97			keys.first ();
98			Posix.exit (0);
99		}
100		Test.trap_assert_failed ();
101
102		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
103		                       TestTrapFlags.SILENCE_STDERR)) {
104			entries.first ();
105			Posix.exit (0);
106		}
107		Test.trap_assert_failed ();
108
109		test_sorted_map.set ("one", "one");
110		test_sorted_map.set ("two", "two");
111		test_sorted_map.set ("three", "three");
112		test_sorted_map.set ("four", "four");
113		test_sorted_map.set ("five", "five");
114		test_sorted_map.set ("six", "six");
115		test_sorted_map.set ("seven", "seven");
116		test_sorted_map.set ("eight", "eight");
117		test_sorted_map.set ("nine", "nine");
118		test_sorted_map.set ("ten", "ten");
119		test_sorted_map.set ("eleven", "eleven");
120		test_sorted_map.set ("twelve", "twelve");
121
122		assert (keys.first () == "eight");
123		/*assert_entry (entries.first (), "eight", "eight");*/
124	}
125
126	public void test_last () {
127		var test_sorted_map = test_map as SortedMap<string,string>;
128		var keys = test_sorted_map.ascending_keys;
129		var entries = test_sorted_map.ascending_entries;
130		// Check the map exists
131		assert (test_sorted_map != null);
132
133		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
134		                       TestTrapFlags.SILENCE_STDERR)) {
135			keys.last ();
136			Posix.exit (0);
137		}
138		Test.trap_assert_failed ();
139
140		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
141		                       TestTrapFlags.SILENCE_STDERR)) {
142			entries.last ();
143			Posix.exit (0);
144		}
145		Test.trap_assert_failed ();
146
147		test_sorted_map.set ("one", "one");
148		test_sorted_map.set ("two", "two");
149		test_sorted_map.set ("three", "three");
150		test_sorted_map.set ("four", "four");
151		test_sorted_map.set ("five", "five");
152		test_sorted_map.set ("six", "six");
153		test_sorted_map.set ("seven", "seven");
154		test_sorted_map.set ("eight", "eight");
155		test_sorted_map.set ("nine", "nine");
156		test_sorted_map.set ("ten", "ten");
157		test_sorted_map.set ("eleven", "eleven");
158		test_sorted_map.set ("twelve", "twelve");
159
160		assert (keys.last () == "two");
161		assert_entry (entries.last (), "two", "two");
162	}
163
164	public void test_iterator_at () {
165		var test_sorted_map = test_map as SortedMap<string,string>;
166		var keys = test_sorted_map.ascending_keys;
167		var entries = test_sorted_map.ascending_entries;
168
169		test_map.set ("one", "one");
170		test_map.set ("two", "two");
171		test_map.set ("three", "three");
172
173		var keys_iter = keys.iterator_at ("one");
174		assert (keys_iter != null);
175		assert (keys_iter.get () == "one");
176
177		var entries_iter = entries.iterator_at (entry_for ("one", "one"));
178		assert (entries_iter != null);
179		assert_entry (entries_iter.get (), "one", "one");
180
181		keys_iter = keys.iterator_at ("two");
182		assert (keys_iter != null);
183		assert (keys_iter.get () == "two");
184
185		entries_iter = entries.iterator_at (entry_for ("two", "two"));
186		assert (entries_iter != null);
187		assert_entry (entries_iter.get (), "two", "two");
188
189		keys_iter = keys.iterator_at ("three");
190		assert (keys_iter != null);
191		assert (keys_iter.get () == "three");
192
193		entries_iter = entries.iterator_at (entry_for ("three", "three"));
194		assert (entries_iter != null);
195		assert_entry (entries_iter.get (), "three", "three");
196
197		keys_iter = keys.iterator_at ("zero");
198		assert (keys_iter == null);
199
200		entries_iter = entries.iterator_at (entry_for ("zero", "zero"));
201		assert (entries_iter == null);
202	}
203
204	public void test_lower () {
205		var test_sorted_map = test_map as SortedMap<string,string>;
206		var keys = test_sorted_map.ascending_keys;
207		var entries = test_sorted_map.ascending_entries;
208
209		assert (keys.lower ("one") == null);
210
211		test_sorted_map.set ("one", "one");
212		test_sorted_map.set ("two", "two");
213		test_sorted_map.set ("three", "three");
214		test_sorted_map.set ("four", "four");
215		test_sorted_map.set ("five", "five");
216		test_sorted_map.set ("six", "six");
217
218		assert (keys.lower ("one") == "four");
219		assert_entry (entries.lower (entry_for ("one", "one")), "four", "four");
220
221		assert (keys.lower ("o") == "four");
222		assert_entry (entries.lower (entry_for ("o", "one")), "four", "four");
223
224		assert (keys.lower ("two") == "three");
225		assert_entry (entries.lower (entry_for ("two", "two")), "three", "three");
226
227		assert (keys.lower ("t") == "six");
228		assert_entry (entries.lower (entry_for ("t", "two")), "six", "six");
229
230		assert (keys.lower ("three") == "six");
231		assert_entry (entries.lower (entry_for ("three", "three")), "six", "six");
232
233		assert (keys.lower ("four") == "five");
234		assert_entry (entries.lower (entry_for ("four", "four")), "five", "five");
235
236		assert (keys.lower ("f") == null);
237		assert (entries.lower (entry_for ("f", "four")) == null);
238
239		assert (keys.lower ("five") == null);
240		assert (entries.lower (entry_for ("five", "five")) == null);
241
242		assert (keys.lower ("six") == "one");
243		assert_entry (entries.lower (entry_for ("six", "six")), "one", "one");
244
245		assert (keys.lower ("s") == "one");
246		assert_entry (entries.lower (entry_for ("s", "six")), "one", "one");
247
248	}
249
250	public void test_higher () {
251		var test_sorted_map = test_map as SortedMap<string,string>;
252		var keys = test_sorted_map.ascending_keys;
253		var entries = test_sorted_map.ascending_entries;
254
255		assert (keys.higher ("one") == null);
256
257		test_sorted_map.set ("one", "one");
258		test_sorted_map.set ("two", "two");
259		test_sorted_map.set ("three", "three");
260		test_sorted_map.set ("four", "four");
261		test_sorted_map.set ("five", "five");
262		test_sorted_map.set ("six", "six");
263
264		assert (keys.higher ("one") == "six");
265		assert_entry (entries.higher (entry_for ("one", "one")), "six", "six");
266
267		assert (keys.higher ("o") == "one");
268		assert_entry (entries.higher (entry_for ("o", "one")), "one", "one");
269
270		assert (keys.higher ("two") == null);
271		assert (entries.higher (entry_for ("two", "two")) == null);
272
273		assert (keys.higher ("t") == "three");
274		assert_entry (entries.higher (entry_for ("t", "two")), "three", "three");
275
276		assert (keys.higher ("three") == "two");
277		assert_entry (entries.higher (entry_for ("three", "three")), "two", "two");
278
279		assert (keys.higher ("four") == "one");
280		assert_entry (entries.higher (entry_for ("four", "four")), "one", "one");
281
282		assert (keys.higher ("f") == "five");
283		assert_entry (entries.higher (entry_for ("f", "four")), "five", "five");
284
285		assert (keys.higher ("five") == "four");
286		assert_entry (entries.higher (entry_for ("five", "five")), "four", "four");
287
288		assert (keys.higher ("six") == "three");
289		assert_entry (entries.higher (entry_for ("six", "six")), "three", "three");
290
291		assert (keys.higher ("s") == "six");
292		assert_entry (entries.higher (entry_for ("s", "six")), "six", "six");
293	}
294
295	public void test_floor () {
296		var test_sorted_map = test_map as SortedMap<string,string>;
297		var keys = test_sorted_map.ascending_keys;
298		var entries = test_sorted_map.ascending_entries;
299
300		assert (keys.floor ("one") == null);
301
302		test_sorted_map.set ("one", "one");
303		test_sorted_map.set ("two", "two");
304		test_sorted_map.set ("three", "three");
305		test_sorted_map.set ("four", "four");
306		test_sorted_map.set ("five", "five");
307		test_sorted_map.set ("six", "six");
308
309		assert (keys.floor ("one") == "one");
310		assert_entry (entries.floor (entry_for ("one", "one")), "one", "one");
311
312		assert (keys.floor ("o") == "four");
313		assert_entry (entries.floor (entry_for ("o", "one")), "four", "four");
314
315		assert (keys.floor ("two") == "two");
316		assert_entry (entries.floor (entry_for ("two", "two")), "two", "two");
317
318		assert (keys.floor ("t") == "six");
319		assert_entry (entries.floor (entry_for ("t", "two")), "six", "six");
320
321		assert (keys.floor ("three") == "three");
322		assert_entry (entries.floor (entry_for ("three", "three")), "three", "three");
323
324		assert (keys.floor ("four") == "four");
325		assert_entry (entries.floor (entry_for ("four", "four")), "four", "four");
326
327		assert (keys.floor ("f") == null);
328		assert (entries.floor (entry_for ("f", "four")) == null);
329
330		assert (keys.floor ("five") == "five");
331		assert_entry (entries.floor (entry_for ("five", "five")), "five", "five");
332
333		assert (keys.floor ("six") == "six");
334		assert_entry (entries.floor (entry_for ("six", "six")), "six", "six");
335
336		assert (keys.floor ("s") == "one");
337		assert_entry (entries.floor (entry_for ("s", "six")), "one", "one");
338	}
339
340	public void test_ceil () {
341		var test_sorted_map = test_map as SortedMap<string,string>;
342		var keys = test_sorted_map.ascending_keys;
343		var entries = test_sorted_map.ascending_entries;
344
345		assert (keys.ceil ("one") == null);
346
347		test_sorted_map.set ("one", "one");
348		test_sorted_map.set ("two", "two");
349		test_sorted_map.set ("three", "three");
350		test_sorted_map.set ("four", "four");
351		test_sorted_map.set ("five", "five");
352		test_sorted_map.set ("six", "six");
353
354		assert (keys.ceil ("one") == "one");
355		assert_entry (entries.ceil (entry_for ("one", "one")), "one", "one");
356
357		assert (keys.ceil ("o") == "one");
358		assert_entry (entries.ceil (entry_for ("o", "one")), "one", "one");
359
360		assert (keys.ceil ("two") == "two");
361		assert_entry (entries.ceil (entry_for ("two", "two")), "two", "two");
362
363		assert (keys.ceil ("t") == "three");
364		assert_entry (entries.ceil (entry_for ("t", "two")), "three", "three");
365
366		assert (keys.ceil ("three") == "three");
367		assert_entry (entries.ceil (entry_for ("three", "three")), "three", "three");
368
369		assert (keys.ceil ("four") == "four");
370		assert_entry (entries.ceil (entry_for ("four", "four")), "four", "four");
371
372		assert (keys.ceil ("f") == "five");
373		assert_entry (entries.ceil (entry_for ("f", "four")), "five", "five");
374
375		assert (keys.ceil ("five") == "five");
376		assert_entry (entries.ceil (entry_for ("five", "five")), "five", "five");
377
378		assert (keys.ceil ("six") == "six");
379		assert_entry (entries.ceil (entry_for ("six", "six")), "six", "six");
380
381		assert (keys.ceil ("s") == "six");
382		assert_entry (entries.ceil (entry_for ("s", "six")), "six", "six");
383	}
384
385	public class SubMapTests : Gee.TestCase {
386		private SortedMap<string,string> master;
387		private SortedMap<string,string> submap;
388		private SortedMapTests test;
389		public enum Type {
390			HEAD,
391			TAIL,
392			SUB,
393			EMPTY;
394			public unowned string to_string () {
395				switch (this) {
396				case Type.HEAD: return "Head";
397				case Type.TAIL: return "Tail";
398				case Type.SUB: return "Range";
399				case Type.EMPTY: return "Empty";
400				default: assert_not_reached ();
401				}
402			}
403		}
404		private Type type;
405
406		public SubMapTests (SortedMapTests test, Type type) {
407			base ("%s Submap".printf (type.to_string ()));
408			this.test = test;
409			this.type = type;
410			add_test ("[Map] has_key, size and is_empty",
411			          test_has_key_size_is_empty);
412			add_test ("[Map] keys", test_keys);
413			add_test ("[Map] values", test_values);
414			add_test ("[Map] entries", test_entries);
415			add_test ("[Map] get", test_get);
416			add_test ("[Map] set", test_set);
417			add_test ("[Map] unset", test_unset);
418			add_test ("[Map] clear", test_clear);
419			add_test ("[Map] iterators", test_iterators);
420			add_test ("[SortedMap] boundaries", test_boundaries);
421			add_test ("[SortedMap] lower", test_lower);
422			add_test ("[SortedMap] higher", test_higher);
423			add_test ("[SortedMap] floor", test_floor);
424			add_test ("[SortedMap] ceil", test_ceil);
425			add_test ("[SortedMap] iterator_at", test_iterator_at);
426			add_test ("[SortedMap] submap and subsets", test_submap_and_subsets);
427		}
428
429		public override void set_up () {
430			test.set_up ();
431			master = test.test_map as SortedMap<string,string>;
432			switch (type) {
433			case Type.HEAD:
434				submap = master.head_map ("one"); break;
435			case Type.TAIL:
436				submap = master.tail_map ("six"); break;
437			case Type.SUB:
438				submap = master.sub_map ("four", "three"); break;
439			case Type.EMPTY:
440				submap = master.sub_map ("three", "four"); break;
441			default:
442				assert_not_reached ();
443			}
444		}
445
446		public override void tear_down () {
447			test.tear_down ();
448		}
449
450		protected void set_default_values (out string[] contains = null, out string[] not_contains = null) {
451			master.set ("one", "one");
452			master.set ("two", "two");
453			master.set ("three", "three");
454			master.set ("four", "four");
455			master.set ("five", "five");
456			master.set ("six", "six");
457			switch (type) {
458			case Type.HEAD:
459				contains = {"five", "four"};
460				not_contains = {"one", "two", "three", "six"};
461				break;
462			case Type.TAIL:
463				contains = {"six", "three", "two"};
464				not_contains = {"one", "four", "five"};
465				break;
466			case Type.SUB:
467				contains = {"four", "one", "six"};
468				not_contains = {"two", "three", "five"};
469				break;
470			case Type.EMPTY:
471				contains = {};
472				not_contains = {"one", "two", "three", "four", "five", "six"};
473				break;
474			default:
475				assert_not_reached ();
476			}
477		}
478
479		public void test_has_key_size_is_empty () {
480			string[] contains, not_contains;
481
482			set_default_values (out contains, out not_contains);
483
484			assert (submap.size == contains.length);
485			assert (submap.is_empty == (contains.length == 0));
486
487			foreach (var s in contains) {
488				assert (submap.has_key (s));
489				assert (submap.has (s, s));
490			}
491			foreach (var s in not_contains) {
492				assert (!submap.has_key (s));
493				assert (!submap.has (s, s));
494			}
495		}
496
497		public void test_get () {
498			string[] contains, not_contains;
499
500			set_default_values (out contains, out not_contains);
501
502			foreach (var s in contains) {
503				assert (submap.get (s) == s);
504			}
505		}
506
507		public void test_keys () {
508			var keys = submap.keys;
509
510			assert (keys.size == 0);
511			assert (keys.is_empty);
512
513			string[] contains, not_contains;
514
515			set_default_values (out contains, out not_contains);
516
517			assert (keys.size == contains.length);
518			foreach (var s in contains)
519				assert (keys.contains (s));
520			foreach (var s in not_contains)
521				assert (!keys.contains (s));
522
523			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
524			                   TestTrapFlags.SILENCE_STDERR)) {
525				keys.add ("three");
526				Posix.exit (0);
527			}
528			Test.trap_assert_failed ();
529
530			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
531			                   TestTrapFlags.SILENCE_STDERR)) {
532				keys.remove ("three");
533				Posix.exit (0);
534			}
535			Test.trap_assert_failed ();
536		}
537
538		public void test_values () {
539			var values = submap.values;
540
541			assert (values.size == 0);
542			assert (values.is_empty);
543
544			string[] contains, not_contains;
545
546			set_default_values (out contains, out not_contains);
547
548			assert (values.size == contains.length);
549			foreach (var s in contains)
550				assert (values.contains (s));
551			foreach (var s in not_contains)
552				assert (!values.contains (s));
553
554			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
555			                   TestTrapFlags.SILENCE_STDERR)) {
556				values.add ("three");
557				Posix.exit (0);
558			}
559			Test.trap_assert_failed ();
560
561			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
562			                   TestTrapFlags.SILENCE_STDERR)) {
563				values.remove ("three");
564				Posix.exit (0);
565			}
566			Test.trap_assert_failed ();
567		}
568
569		public void test_entries () {
570			var entries = submap.entries;
571
572			assert (entries.size == 0);
573			assert (entries.is_empty);
574
575			string[] contains, not_contains;
576
577			set_default_values (out contains, out not_contains);
578
579			assert (entries.size == contains.length);
580			foreach (var s in contains)
581				assert (entries.contains (MapTests.entry_for (s, s)));
582			foreach (var s in not_contains)
583				assert (!entries.contains (MapTests.entry_for (s, s)));
584
585			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
586			                   TestTrapFlags.SILENCE_STDERR)) {
587				entries.add (MapTests.entry_for ("three", "three"));
588				Posix.exit (0);
589			}
590			Test.trap_assert_failed ();
591
592			if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
593			                   TestTrapFlags.SILENCE_STDERR)) {
594				entries.remove (MapTests.entry_for ("three", "three"));
595				Posix.exit (0);
596			}
597			Test.trap_assert_failed ();
598		}
599
600		public void test_set () {
601			string[] contains, not_contains;
602
603			set_default_values (out contains, out not_contains);
604
605			string[] success, fail;
606			switch (type) {
607			case Type.HEAD:
608				success = {"a", "o"};
609				fail = {"oz", "z"};
610				break;
611			case Type.TAIL:
612				success = {"siz", "z"};
613				fail = {"sia", "a"};
614				break;
615			case Type.SUB:
616				success = {"o", "th"};
617				fail = {"f", "u"};
618				break;
619			case Type.EMPTY:
620				success = {};
621				fail = {"o", "th", "f", "u"};
622				break;
623			default:
624				assert_not_reached ();
625			}
626
627			foreach (var s in success) {
628				submap.set (s, s);
629				assert (submap.has (s, s));
630				assert (master.has (s, s));
631			}
632
633			foreach (var s in fail) {
634				submap.set (s, s);
635				assert (!submap.has_key (s));
636				assert (!master.has_key (s));
637			}
638
639			assert (master.size == 6 + success.length);
640		}
641
642		public void test_unset () {
643			string[] contains, not_contains;
644
645			set_default_values (out contains, out not_contains);
646
647			foreach (var s in contains) {
648				string? value;
649				assert (submap.unset (s, out value));
650				assert (value == s);
651				assert (!master.has_key (s));
652			}
653			foreach (var s in not_contains) {
654				assert (!submap.unset (s));
655				assert (master.has (s, s));
656			}
657
658			assert (master.size == 6 - contains.length);
659		}
660
661		public void test_clear () {
662			string[] contains, not_contains;
663
664			set_default_values (out contains, out not_contains);
665
666			submap.clear ();
667
668			foreach (var s in contains) {
669				assert (!master.has_key (s));
670			}
671			foreach (var s in not_contains) {
672				assert (!submap.unset (s));
673				assert (master.has (s, s));
674			}
675
676			assert (master.size == 6 - contains.length);
677		}
678
679		public void test_iterators () {
680			string[] contains, not_contains;
681
682			var _map_iter = submap.map_iterator ();
683
684			assert (!_map_iter.has_next ());
685			assert (!_map_iter.next ());
686
687			set_default_values (out contains, out not_contains);
688
689			var i = 0;
690			_map_iter = submap.map_iterator ();
691			while (_map_iter.next ()) {
692				assert (_map_iter.get_key () == contains[i]);
693				assert (_map_iter.get_value () == contains[i]);
694				i++;
695			}
696			assert (i == contains.length);
697
698			i = 0;
699			foreach (var k in submap.keys)
700				assert (k == contains[i++]);
701			assert (i == contains.length);
702
703			i = 0;
704			foreach (var e in submap.entries) {
705				MapTests.assert_entry (e, contains[i], contains[i]);
706				i++;
707			}
708			assert (i == contains.length);
709
710			if (type != Type.EMPTY) {
711				var map_iter = submap.map_iterator ();
712				assert (map_iter.next ());
713
714				assert (map_iter.get_key () == contains[0]);
715				assert (map_iter.get_value () == contains[0]);
716
717				assert (map_iter.has_next ());
718				assert (map_iter.next ());
719				assert (map_iter.get_key () == contains[1]);
720				assert (map_iter.get_value () == contains[1]);
721
722				// Repeat for keys
723				master.clear ();
724				set_default_values (out contains, out not_contains);
725				var keys_iter = submap.ascending_keys.iterator ();
726
727				assert (keys_iter.has_next ());
728				assert (keys_iter.next ());
729
730				assert (keys_iter.get () == contains[0]);
731				assert (keys_iter.has_next ());
732				assert (keys_iter.next ());
733				assert (keys_iter.get () == contains[1]);
734
735				// Repeat for entries
736				master.clear ();
737				set_default_values (out contains, out not_contains);
738				var entries_iter = submap.ascending_entries.iterator ();
739
740				assert (entries_iter.has_next ());
741				assert (entries_iter.next ());
742
743				MapTests.assert_entry (entries_iter.get (), contains[0], contains[0]);
744				assert (entries_iter.has_next ());
745				assert (entries_iter.next ());
746				MapTests.assert_entry (entries_iter.get (), contains[1], contains[1]);
747			} else {
748				var keys_iter = submap.ascending_keys.iterator ();
749				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
750				                       TestTrapFlags.SILENCE_STDERR)) {
751					keys_iter.remove ();
752					Posix.exit (0);
753				}
754				Test.trap_assert_failed ();
755			}
756		}
757
758		public void test_boundaries () {
759			var keys = submap.ascending_keys;
760			var entries = submap.ascending_entries;
761
762			string[] contains, not_contains;
763
764			set_default_values (out contains, out not_contains);
765
766			switch (type) {
767			case Type.HEAD:
768				assert (keys.first () == "five");
769				assert (keys.last () == "four");
770				assert_entry (entries.first (), "five", "five");
771				assert_entry (entries.last (), "four", "four");
772				break;
773			case Type.TAIL:
774				assert (keys.first () == "six");
775				assert (keys.last () == "two");
776				assert_entry (entries.first (), "six", "six");
777				assert_entry (entries.last (), "two", "two");
778				break;
779			case Type.SUB:
780				assert (keys.first () == "four");
781				assert (keys.last () == "six");
782				assert_entry (entries.first (), "four", "four");
783				assert_entry (entries.last (), "six", "six");
784				break;
785			case Type.EMPTY:
786				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
787				                       TestTrapFlags.SILENCE_STDERR)) {
788					keys.first ();
789					Posix.exit (0);
790				}
791				Test.trap_assert_failed ();
792				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
793				                       TestTrapFlags.SILENCE_STDERR)) {
794					keys.last ();
795					Posix.exit (0);
796				}
797				Test.trap_assert_failed ();
798				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
799				                       TestTrapFlags.SILENCE_STDERR)) {
800					entries.first ();
801					Posix.exit (0);
802				}
803				Test.trap_assert_failed ();
804				if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
805				                       TestTrapFlags.SILENCE_STDERR)) {
806					entries.last ();
807					Posix.exit (0);
808				}
809				Test.trap_assert_failed ();
810				break;
811			default:
812				assert_not_reached ();
813			}
814		}
815
816		public void test_lower () {
817			string[] contains, not_contains;
818			var keys = submap.ascending_keys;
819			var entries = submap.ascending_entries;
820
821			set_default_values (out contains, out not_contains);
822
823			switch (type) {
824			case Type.HEAD:
825				assert (keys.lower ("a") == null);
826				assert (entries.lower (MapTests.entry_for ("a", "a")) == null);
827				assert (keys.lower ("five") == null);
828				assert (entries.lower (MapTests.entry_for ("five", "five")) == null);
829				assert (keys.lower ("four") == "five");
830				MapTests.assert_entry (entries.lower (MapTests.entry_for ("four", "four")), "five", "five");
831				assert (keys.lower ("six") == "four");
832				MapTests.assert_entry (entries.lower (MapTests.entry_for ("six", "six")), "four", "four");
833				break;
834			case Type.TAIL:
835				assert (keys.lower ("one") == null);
836				assert (entries.lower (MapTests.entry_for ("one", "one")) == null);
837				assert (keys.lower ("six") == null);
838				assert (entries.lower (MapTests.entry_for ("six", "six")) == null);
839				assert (keys.lower ("three") == "six");
840				MapTests.assert_entry (entries.lower (MapTests.entry_for ("three", "three")), "six", "six");
841				assert (keys.lower ("two") == "three");
842				MapTests.assert_entry (entries.lower (MapTests.entry_for ("two", "two")), "three", "three");
843				assert (keys.lower ("z") == "two");
844				MapTests.assert_entry (entries.lower (MapTests.entry_for ("z", "z")), "two", "two");
845				break;
846			case Type.SUB:
847				assert (keys.lower ("five") == null);
848				assert (entries.lower (MapTests.entry_for ("five", "five")) == null);
849				assert (keys.lower ("four") == null);
850				assert (entries.lower (MapTests.entry_for ("four", "four")) == null);
851				assert (keys.lower ("one") == "four");
852				MapTests.assert_entry (entries.lower (MapTests.entry_for ("one", "one")), "four", "four");
853				assert (keys.lower ("six") == "one");
854				MapTests.assert_entry (entries.lower (MapTests.entry_for ("six", "six")), "one", "one");
855				assert (keys.lower ("three") == "six");
856				MapTests.assert_entry (entries.lower (MapTests.entry_for ("three", "three")), "six", "six");
857				break;
858			case Type.EMPTY:
859				assert (keys.lower ("six") == null);
860				assert (entries.lower (MapTests.entry_for ("six", "six")) == null);
861				break;
862			default:
863				assert_not_reached ();
864			}
865		}
866
867		public void test_higher () {
868			string[] contains, not_contains;
869			var keys = submap.ascending_keys;
870			var entries = submap.ascending_entries;
871
872			set_default_values (out contains, out not_contains);
873
874			switch (type) {
875			case Type.HEAD:
876				assert (keys.higher ("a") == "five");
877				MapTests.assert_entry (entries.higher (MapTests.entry_for ("a", "a")), "five", "five");
878				assert (keys.higher ("five") == "four");
879				MapTests.assert_entry (entries.higher (MapTests.entry_for ("five", "five")), "four", "four");
880				assert (keys.higher ("four") == null);
881				assert (entries.higher (MapTests.entry_for ("four", "four")) == null);
882				assert (keys.higher ("six") == null);
883				assert (entries.higher (MapTests.entry_for ("six", "six")) == null);
884				break;
885			case Type.TAIL:
886				assert (keys.higher ("one") == "six");
887				MapTests.assert_entry (entries.higher (MapTests.entry_for ("one", "one")), "six", "six");
888				assert (keys.higher ("six") == "three");
889				MapTests.assert_entry (entries.higher (MapTests.entry_for ("six", "six")), "three", "three");
890				assert (keys.higher ("three") == "two");
891				MapTests.assert_entry (entries.higher (MapTests.entry_for ("three", "three")), "two", "two");
892				assert (keys.higher ("two") == null);
893				assert (entries.higher (MapTests.entry_for ("two", "two")) == null);
894				assert (keys.higher ("z") == null);
895				assert (entries.higher (MapTests.entry_for ("z", "z")) == null);
896				break;
897			case Type.SUB:
898				assert (keys.higher ("five") == "four");
899				MapTests.assert_entry (entries.higher (MapTests.entry_for ("five", "five")), "four", "four");
900				assert (keys.higher ("four") == "one");
901				MapTests.assert_entry (entries.higher (MapTests.entry_for ("four", "four")), "one", "one");
902				assert (keys.higher ("one") == "six");
903				MapTests.assert_entry (entries.higher (MapTests.entry_for ("one", "one")), "six", "six");
904				assert (keys.higher ("six") == null);
905				assert (entries.higher (MapTests.entry_for ("six", "six")) == null);
906				assert (keys.higher ("three") == null);
907				assert (entries.higher (MapTests.entry_for ("three", "three")) == null);
908				break;
909			case Type.EMPTY:
910				assert (keys.higher ("six") == null);
911				assert (entries.higher (MapTests.entry_for ("six", "six")) == null);
912				break;
913			default:
914				assert_not_reached ();
915			}
916		}
917
918		public void test_floor () {
919			string[] contains, not_contains;
920			var keys = submap.ascending_keys;
921			var entries = submap.ascending_entries;
922
923			set_default_values (out contains, out not_contains);
924
925			switch (type) {
926			case Type.HEAD:
927				assert (keys.floor ("a") == null);
928				assert (entries.floor (MapTests.entry_for ("a", "a")) == null);
929				assert (keys.floor ("five") == "five");
930				MapTests.assert_entry (entries.floor (MapTests.entry_for ("five", "fiv")), "five", "five");
931				assert (keys.floor ("four") == "four");
932				MapTests.assert_entry (entries.floor (MapTests.entry_for ("four", "four")), "four", "four");
933				assert (keys.floor ("six") == "four");
934				MapTests.assert_entry (entries.floor (MapTests.entry_for ("six", "six")), "four", "four");
935				break;
936			case Type.TAIL:
937				assert (keys.floor ("one") == null);
938				assert (entries.floor (MapTests.entry_for ("one", "one")) == null);
939				assert (keys.floor ("six") == "six");
940				MapTests.assert_entry (entries.floor (MapTests.entry_for ("six", "six")), "six", "six");
941				assert (keys.floor ("three") == "three");
942				MapTests.assert_entry (entries.floor (MapTests.entry_for ("three", "three")), "three", "three");
943				assert (keys.floor ("two") == "two");
944				MapTests.assert_entry (entries.floor (MapTests.entry_for ("two", "two")), "two", "two");
945				assert (keys.floor ("z") == "two");
946				MapTests.assert_entry (entries.floor (MapTests.entry_for ("z", "z")), "two", "two");
947				break;
948			case Type.SUB:
949				assert (keys.floor ("five") == null);
950				assert (entries.floor (MapTests.entry_for ("five", "five")) == null);
951				assert (keys.floor ("four") == "four");
952				MapTests.assert_entry (entries.floor (MapTests.entry_for ("four", "four")), "four", "four");
953				assert (keys.floor ("one") == "one");
954				MapTests.assert_entry (entries.floor (MapTests.entry_for ("one", "one")), "one", "one");
955				assert (keys.floor ("six") == "six");
956				MapTests.assert_entry (entries.floor (MapTests.entry_for ("six", "six")), "six", "six");
957				assert (keys.floor ("three") == "six");
958				MapTests.assert_entry (entries.floor (MapTests.entry_for ("three", "three")), "six", "six");
959				break;
960			case Type.EMPTY:
961				assert (keys.floor ("six") == null);
962				assert (entries.floor (MapTests.entry_for ("six", "six")) == null);
963				break;
964			default:
965				assert_not_reached ();
966			}
967		}
968
969		public void test_ceil () {
970			string[] contains, not_contains;
971			var keys = submap.ascending_keys;
972			var entries = submap.ascending_entries;
973
974			set_default_values (out contains, out not_contains);
975
976			switch (type) {
977			case Type.HEAD:
978				assert (keys.ceil ("a") == "five");
979				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("a", "a")), "five", "five");
980				assert (keys.ceil ("five") == "five");
981				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("five", "five")), "five", "five");
982				assert (keys.ceil ("four") == "four");
983				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("four", "four")), "four", "four");
984				assert (keys.ceil ("six") == null);
985				assert (entries.ceil (MapTests.entry_for ("six", "six")) == null);
986				break;
987			case Type.TAIL:
988				assert (keys.ceil ("one") == "six");
989				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("one", "one")), "six", "six");
990				assert (keys.ceil ("six") == "six");
991				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("six", "six")), "six", "six");
992				assert (keys.ceil ("three") == "three");
993				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("three", "three")), "three", "three");
994				assert (keys.ceil ("two") == "two");
995				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("two", "two")), "two", "two");
996				assert (keys.ceil ("z") == null);
997				assert (entries.ceil (MapTests.entry_for ("z", "z")) == null);
998				break;
999			case Type.SUB:
1000				assert (keys.ceil ("five") == "four");
1001				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("five", "five")), "four", "four");
1002				assert (keys.ceil ("four") == "four");
1003				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("four", "four")), "four", "four");
1004				assert (keys.ceil ("one") == "one");
1005				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("one", "one")), "one", "one");
1006				assert (keys.ceil ("six") == "six");
1007				MapTests.assert_entry (entries.ceil (MapTests.entry_for ("six", "six")), "six", "six");
1008				assert (keys.ceil ("three") == null);
1009				assert (entries.ceil (MapTests.entry_for ("three", "three")) == null);
1010				break;
1011			case Type.EMPTY:
1012				assert (keys.ceil ("six") == null);
1013				assert (entries.ceil (MapTests.entry_for ("six", "six")) == null);
1014				break;
1015			default:
1016				assert_not_reached ();
1017			}
1018		}
1019
1020		public void test_iterator_at () {
1021			string[] contains, not_contains;
1022			var keys = submap.ascending_keys;
1023			var entries = submap.ascending_entries;
1024
1025			set_default_values (out contains, out not_contains);
1026
1027			foreach (var s in contains) {
1028				var key_iter = keys.iterator_at (s);
1029				var entry_iter = entries.iterator_at (MapTests.entry_for (s, s));
1030				assert (key_iter != null);
1031				assert (key_iter.get () == s);
1032				MapTests.assert_entry (entry_iter.get (), s, s);
1033			}
1034			foreach (var s in not_contains) {
1035				var key_iter = keys.iterator_at (s);
1036				var entry_iter = entries.iterator_at (MapTests.entry_for (s, s));
1037				assert (key_iter == null);
1038				assert (entry_iter == null);
1039			}
1040			assert (keys.iterator_at ("seven") == null);
1041			assert (entries.iterator_at (MapTests.entry_for ("seven", "seven")) == null);
1042		}
1043
1044		public void test_submap_and_subsets () {
1045			string[] contains, not_contains;
1046			var keys = submap.ascending_keys;
1047			var entries = submap.ascending_entries;
1048
1049			set_default_values (out contains, out not_contains);
1050
1051			switch (type) {
1052			case Type.HEAD:
1053				var subsubmap = submap.head_map ("four");
1054				var keyssubset = keys.head_set ("four");
1055				var entriessubset = entries.head_set (MapTests.entry_for ("four", "four"));
1056
1057				assert (subsubmap.size == 1);
1058				assert (keyssubset.size == 1);
1059				assert (entriessubset.size == 1);
1060
1061				subsubmap = submap.tail_map ("four");
1062				keyssubset = keys.tail_set ("four");
1063				entriessubset = entries.tail_set (MapTests.entry_for ("four", "four"));
1064
1065				assert (subsubmap.size == 1);
1066				assert (keyssubset.size == 1);
1067				assert (entriessubset.size == 1);
1068
1069				subsubmap = submap.sub_map ("four", "one");
1070				keyssubset = keys.sub_set ("four", "one");
1071				entriessubset = entries.sub_set (MapTests.entry_for ("four", "four"), MapTests.entry_for ("one", "one"));
1072
1073				assert (subsubmap.size == 1);
1074				assert (keyssubset.size == 1);
1075				assert (entriessubset.size == 1);
1076
1077				subsubmap = submap.sub_map ("four", "four");
1078				keyssubset = keys.sub_set ("four", "four");
1079				entriessubset = entries.sub_set (MapTests.entry_for ("four", "four"), MapTests.entry_for ("four", "four"));
1080
1081				assert (subsubmap.size == 0);
1082				assert (keyssubset.size == 0);
1083				assert (entriessubset.size == 0);
1084				break;
1085			case Type.TAIL:
1086				var subsubmap = submap.head_map ("two");
1087				var keyssubset = keys.head_set ("two");
1088				var entriessubset = entries.head_set (MapTests.entry_for ("two", "two"));
1089
1090				assert (subsubmap.size == 2);
1091				assert (keyssubset.size == 2);
1092				assert (entriessubset.size == 2);
1093
1094				subsubmap = submap.tail_map ("three");
1095				keyssubset = keys.tail_set ("three");
1096				entriessubset = entries.tail_set (MapTests.entry_for ("three", "three"));
1097
1098				assert (subsubmap.size == 2);
1099				assert (keyssubset.size == 2);
1100				assert (entriessubset.size == 2);
1101
1102				subsubmap = submap.sub_map ("three", "two");
1103				keyssubset = keys.sub_set ("three", "two");
1104				entriessubset = entries.sub_set (MapTests.entry_for ("three", "three"), MapTests.entry_for ("two", "two"));
1105
1106				assert (subsubmap.size == 1);
1107				assert (keyssubset.size == 1);
1108				assert (entriessubset.size == 1);
1109				break;
1110			case Type.SUB:
1111				var subsubmap = submap.head_map ("six");
1112				var keyssubset = keys.head_set ("six");
1113				var entriessubset = entries.head_set (MapTests.entry_for ("six", "six"));
1114
1115				assert (subsubmap.size == 2);
1116				assert (keyssubset.size == 2);
1117				assert (entriessubset.size == 2);
1118
1119				subsubmap = submap.tail_map ("one");
1120				keyssubset = keys.tail_set ("one");
1121				entriessubset = entries.tail_set (MapTests.entry_for ("one", "one"));
1122
1123				assert (subsubmap.size == 2);
1124				assert (keyssubset.size == 2);
1125				assert (entriessubset.size == 2);
1126
1127				subsubmap = submap.sub_map ("one", "six");
1128				keyssubset = keys.sub_set ("one", "six");
1129				entriessubset = entries.sub_set (MapTests.entry_for ("one", "one"), MapTests.entry_for ("six", "six"));
1130
1131				assert (subsubmap.size == 1);
1132				assert (keyssubset.size == 1);
1133				assert (entriessubset.size == 1);
1134
1135				subsubmap = submap.sub_map ("five", "two");
1136				keyssubset = keys.sub_set ("five", "two");
1137				entriessubset = entries.sub_set (MapTests.entry_for ("five", "five"), MapTests.entry_for ("two", "two"));
1138
1139				assert (subsubmap.size == 3);
1140				assert (keyssubset.size == 3);
1141				assert (entriessubset.size == 3);
1142				break;
1143			case Type.EMPTY:
1144				var subsubmap = submap.head_map ("six");
1145				var keyssubset = keys.head_set ("six");
1146				var entriessubset = entries.head_set (MapTests.entry_for ("six", "six"));
1147
1148				assert (subsubmap.size == 0);
1149				assert (keyssubset.size == 0);
1150				assert (entriessubset.size == 0);
1151
1152				subsubmap = submap.tail_map ("three");
1153				keyssubset = keys.tail_set ("three");
1154				entriessubset = entries.tail_set (MapTests.entry_for ("three", "three"));
1155
1156				assert (subsubmap.size == 0);
1157				assert (keyssubset.size == 0);
1158				assert (entriessubset.size == 0);
1159
1160				subsubmap = submap.sub_map ("one", "six");
1161				keyssubset = keys.sub_set ("one", "six");
1162				entriessubset = entries.sub_set (MapTests.entry_for ("one", "one"), MapTests.entry_for ("six", "six"));
1163
1164				assert (subsubmap.size == 0);
1165				assert (keyssubset.size == 0);
1166				assert (entriessubset.size == 0);
1167				break;
1168			default:
1169				assert_not_reached ();
1170			}
1171		}
1172	}
1173}
1174
1175