3 * Copyright (C) 2008 Jürg Billeter
4 * Copyright (C) 2009 Didier Villevalois, Julien Peeters, Maciej Piechotka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Jürg Billeter <j@bitron.ch>
22 * Maciej Piechotka <uzytkownik2@gmail.com>
23 * Julien Peeters <contact@julienpeeters.fr>
28 public abstract class MapTests : Gee.TestCase {
30 public MapTests (string name) {
32 add_test ("[Map] type correctness", test_type_correctness);
33 add_test ("[Map] has_key, size and is_empty",
34 test_has_key_size_is_empty);
35 add_test ("[Map] keys", test_keys);
36 add_test ("[Map] values", test_values);
37 add_test ("[Map] entries", test_entries);
38 add_test ("[Map] set all", test_set_all);
39 add_test ("[Map] unset all", test_unset_all);
40 add_test ("[Map] has all", test_has_all);
41 add_test ("[Map] GObject properties", test_gobject_properties);
42 add_test ("[Map] fold", test_fold);
43 add_test ("[Map] foreach", test_foreach);
46 protected Map<string, string> test_map;
48 public void test_type_correctness () {
49 // Check the map exists
50 assert (test_map != null);
52 // Check the advertised key and value types
53 assert (test_map.key_type == typeof (string));
54 assert (test_map.value_type == typeof (string));
57 public void test_has_key_size_is_empty () {
58 // Check the collection exists
59 assert (test_map != null);
62 // Check the collection is initially empty
63 assert (! test_map.has_key ("one"));
64 assert (! test_map.has_key ("two"));
65 assert (! test_map.has_key ("three"));
66 assert (test_map.size == 0);
67 assert (test_map.is_empty);
70 test_map.set ("one", "value_of_one");
71 assert (test_map.has_key ("one"));
72 assert (test_map.has ("one", "value_of_one"));
73 assert (! test_map.has ("one", "another_value_for_one"));
74 assert (test_map.get ("one") == "value_of_one");
75 assert (! test_map.has_key ("two"));
76 assert (test_map.get ("two") == null);
77 assert (! test_map.has_key ("three"));
78 assert (test_map.get ("three") == null);
79 assert (test_map.size == 1);
80 assert (! test_map.is_empty);
82 // Remove the last added binding
83 assert (test_map.unset ("one"));
84 assert (! test_map.has_key ("one"));
85 assert (! test_map.has ("one", "value_of_one"));
86 assert (! test_map.has ("one", "another_value_for_one"));
87 assert (test_map.get ("one") == null);
88 assert (! test_map.has_key ("two"));
89 assert (test_map.get ("two") == null);
90 assert (! test_map.has_key ("three"));
91 assert (test_map.get ("three") == null);
92 assert (test_map.size == 0);
93 assert (test_map.is_empty);
96 test_map.set ("one", "value_of_one");
97 assert (test_map.has_key ("one"));
98 assert (test_map.get ("one") == "value_of_one");
99 assert (! test_map.has_key ("two"));
100 assert (test_map.get ("two") == null);
101 assert (! test_map.has_key ("three"));
102 assert (test_map.get ("three") == null);
103 assert (test_map.size == 1);
104 assert (! test_map.is_empty);
106 test_map.set ("two", "value_of_two");
107 assert (test_map.has_key ("one"));
108 assert (test_map.get ("one") == "value_of_one");
109 assert (test_map.has_key ("two"));
110 assert (test_map.get ("two") == "value_of_two");
111 assert (! test_map.has_key ("three"));
112 assert (test_map.get ("three") == null);
113 assert (test_map.size == 2);
114 assert (! test_map.is_empty);
116 test_map.set ("three", "value_of_three");
117 assert (test_map.has_key ("one"));
118 assert (test_map.get ("one") == "value_of_one");
119 assert (test_map.has_key ("two"));
120 assert (test_map.get ("two") == "value_of_two");
121 assert (test_map.has_key ("three"));
122 assert (test_map.get ("three") == "value_of_three");
123 assert (test_map.size == 3);
124 assert (! test_map.is_empty);
126 // Update an existent binding
127 test_map.set ("two", "value_of_two_new");
128 assert (test_map.has_key ("one"));
129 assert (test_map.get ("one") == "value_of_one");
130 assert (test_map.has_key ("two"));
131 assert (test_map.get ("two") == "value_of_two_new");
132 assert (test_map.has_key ("three"));
133 assert (test_map.get ("three") == "value_of_three");
134 assert (test_map.size == 3);
135 assert (! test_map.is_empty);
137 // Remove one element
138 assert (test_map.unset ("two", out value));
139 assert (value == "value_of_two_new");
140 assert (test_map.has_key ("one"));
141 assert (test_map.get ("one") == "value_of_one");
142 assert (! test_map.has_key ("two"));
143 assert (test_map.get ("two") == null);
144 assert (test_map.has_key ("three"));
145 assert (test_map.get ("three") == "value_of_three");
146 assert (test_map.size == 2);
147 assert (! test_map.is_empty);
149 // Remove the same element again
150 assert (! test_map.unset ("two", out value));
151 assert (value == null);
152 assert (test_map.has_key ("one"));
153 assert (! test_map.has_key ("two"));
154 assert (test_map.has_key ("three"));
155 assert (test_map.size == 2);
156 assert (! test_map.is_empty);
158 // Remove all elements
160 assert (! test_map.has_key ("one"));
161 assert (test_map.get ("one") == null);
162 assert (! test_map.has_key ("two"));
163 assert (test_map.get ("two") == null);
164 assert (! test_map.has_key ("three"));
165 assert (test_map.get ("three") == null);
166 assert (test_map.size == 0);
167 assert (test_map.is_empty);
170 public void test_keys () {
171 // Check keys on empty map
172 var keys = test_map.keys;
173 assert (keys.size == 0);
175 // Check keys on map with one item
176 test_map.set ("one", "value_of_one");
177 assert (keys.size == 1);
178 assert (keys.contains ("one"));
179 keys = test_map.keys;
180 assert (keys.size == 1);
181 assert (keys.contains ("one"));
183 // Check modify key set directly
184 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
185 TestTrapFlags.SILENCE_STDERR)) {
186 assert (! keys.add ("three"));
189 Test.trap_assert_failed ();
190 Test.trap_assert_stderr ("*code should not be reached*");
192 // Check keys on map with multiple items
193 test_map.set ("two", "value_of_two");
194 assert (keys.size == 2);
195 assert (keys.contains ("one"));
196 assert (keys.contains ("two"));
197 keys = test_map.keys;
198 assert (keys.size == 2);
199 assert (keys.contains ("one"));
200 assert (keys.contains ("two"));
202 // Check keys on map clear
204 assert (keys.size == 0);
205 keys = test_map.keys;
206 assert (keys.size == 0);
209 public void test_values () {
210 // Check keys on empty map
211 var values = test_map.values;
212 assert (values.size == 0);
214 // Check keys on map with one item
215 test_map.set ("one", "value_of_one");
216 assert (values.size == 1);
217 assert (values.contains ("value_of_one"));
218 values = test_map.values;
219 assert (values.size == 1);
220 assert (values.contains ("value_of_one"));
222 // Check modify key set directly
223 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
224 TestTrapFlags.SILENCE_STDERR)) {
225 assert (! values.add ("two"));
228 Test.trap_assert_failed ();
229 Test.trap_assert_stderr ("*code should not be reached*");
231 // Check keys on map with multiple items
232 test_map.set ("two", "value_of_two");
233 assert (values.size == 2);
234 assert (values.contains ("value_of_one"));
235 assert (values.contains ("value_of_two"));
236 values = test_map.values;
237 assert (values.size == 2);
238 assert (values.contains ("value_of_one"));
239 assert (values.contains ("value_of_two"));
241 // Check keys on map clear
243 assert (values.size == 0);
244 values = test_map.values;
245 assert (values.size == 0);
248 public void test_entries () {
249 // Check entries on empty map
250 var entries = test_map.entries;
251 assert (entries.size == 0);
253 // Check entries on map with one item
254 test_map.set ("one", "value_of_one");
255 assert (entries.size == 1);
256 assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
257 entries = test_map.entries;
258 assert (entries.size == 1);
259 assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
261 // Check modify entry set directly
262 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
263 TestTrapFlags.SILENCE_STDERR)) {
264 assert (! entries.add (new TestEntry<string,string> ("two", "value_of_two")));
267 Test.trap_assert_failed ();
268 Test.trap_assert_stderr ("*code should not be reached*");
270 // Check entries on map with multiple items
271 test_map.set ("two", "value_of_two");
272 assert (entries.size == 2);
273 assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
274 assert (entries.contains (new TestEntry<string,string> ("two", "value_of_two")));
275 entries = test_map.entries;
276 assert (entries.size == 2);
277 assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
278 assert (entries.contains (new TestEntry<string,string> ("two", "value_of_two")));
280 // Check keys on map clear
282 assert (entries.size == 0);
283 entries = test_map.entries;
284 assert (entries.size == 0);
287 public void test_clear () {
288 test_map.set ("one", "value_of_one");
289 test_map.set ("two", "value_of_two");
290 test_map.set ("three", "value_of_three");
293 assert (test_map.size == 0);
295 Set<string> keys = test_map.keys;
296 assert (keys != null);
297 Iterator<string> ikeys = keys.iterator ();
298 assert (ikeys != null);
299 assert (!ikeys.has_next ());
301 Collection<string> vals = test_map.values;
302 assert (vals != null);
303 Iterator<string> ivals = vals.iterator ();
304 assert (ivals != null);
305 assert (!ivals.has_next ());
307 Set<Map.Entry<string, string>> ents = test_map.entries;
308 assert (ents != null);
309 Iterator<Map.Entry<string, string>> ients = ents.iterator ();
310 assert (ients != null);
311 assert (!ients.has_next ());
313 MapIterator<string, string> iter = test_map.map_iterator ();
314 assert (iter != null);
315 assert (!iter.has_next ());
318 public void test_set_all () {
319 var another_map = new HashMap<string,string> ();
321 test_map.set ("one", "value_of_one");
322 test_map.set ("two", "value_of_two");
323 test_map.set ("three", "value_of_three");
324 another_map.set ("four", "value_of_four");
325 another_map.set ("five", "value_of_five");
326 another_map.set ("six", "value_of_six");
328 test_map.set_all (another_map);
330 assert (test_map.size == 6);
331 assert (test_map.has_key ("one"));
332 assert (test_map.has_key ("two"));
333 assert (test_map.has_key ("three"));
334 assert (test_map.has_key ("four"));
335 assert (test_map.has_key ("five"));
336 assert (test_map.has_key ("six"));
338 assert (test_map.get ("one") == "value_of_one");
339 assert (test_map.get ("two") == "value_of_two");
340 assert (test_map.get ("three") == "value_of_three");
341 assert (test_map.get ("four") == "value_of_four");
342 assert (test_map.get ("five") == "value_of_five");
343 assert (test_map.get ("six") == "value_of_six");
346 public void test_unset_all () {
347 var another_map = new HashMap<string,string> ();
349 // Check unset all on empty maps.
350 assert (test_map.is_empty);
351 assert (another_map.is_empty);
353 assert (! test_map.unset_all (another_map));
355 assert (test_map.is_empty);
356 assert (another_map.is_empty);
359 another_map.clear ();
361 // Test_Map is empty, another_map has entries. -> no change
362 another_map.set ("one", "value_of_one");
363 another_map.set ("two", "value_of_two");
365 assert (test_map.is_empty);
366 assert (another_map.size == 2);
368 assert (! test_map.unset_all (another_map));
370 assert (test_map.is_empty);
371 assert (another_map.size == 2);
374 another_map.clear ();
376 // Test_Map has entries, another_map is empty. -> no change
377 test_map.set ("one", "value_of_one");
378 test_map.set ("two", "value_of_two");
380 assert (test_map.size == 2);
381 assert (another_map.is_empty);
383 assert (! test_map.unset_all (another_map));
385 assert (test_map.size == 2);
386 assert (another_map.is_empty);
389 another_map.clear ();
391 // Test_Map and another_map have the same
392 // entries -> test_map is cleared
393 test_map.set ("one", "value_of_one");
394 test_map.set ("two", "value_of_two");
395 another_map.set ("one", "value_of_one");
396 another_map.set ("two", "value_of_two");
398 assert (test_map.size == 2);
399 assert (another_map.size == 2);
401 assert (test_map.unset_all (another_map));
403 assert (test_map.is_empty);
404 assert (another_map.size == 2);
407 another_map.clear ();
409 // Test_Map has some common keys with another_map
410 // but both have also unique keys -> common key are
411 // cleared from test_map
412 test_map.set ("one", "value_of_one");
413 test_map.set ("two", "value_of_two");
414 test_map.set ("three", "value_of_three");
415 another_map.set ("two", "value_of_two");
416 another_map.set ("three", "value_of_three");
417 another_map.set ("four", "value_of_four");
418 another_map.set ("five", "value_of_five");
420 assert (test_map.size == 3);
421 assert (another_map.size == 4);
423 assert (test_map.unset_all (another_map));
425 assert (test_map.size == 1);
426 assert (another_map.size == 4);
428 assert (test_map.has_key ("one"));
431 public void test_has_all () {
432 var another_map = new HashMap<string,string> ();
435 assert (test_map.has_all (another_map));
437 // Test_Map has items, another_map is empty.
438 test_map.set ("one", "value_of_one");
440 assert (test_map.has_all (another_map));
443 another_map.clear ();
445 // Test_Map is empty, another_map has items.
446 another_map.set ("one", "value_of_one");
448 assert (! test_map.has_all (another_map));
451 another_map.clear ();
453 // Test_Map and another_map are the same.
454 test_map.set ("one", "value_of_one");
455 test_map.set ("two", "value_of_two");
457 another_map.set ("one", "value_of_one");
458 another_map.set ("two", "value_of_two");
460 assert (test_map.has_all (another_map));
463 another_map.clear ();
465 // Test_Map and another_map are not the same.
466 test_map.set ("one", "value_of_one");
467 test_map.set ("two", "value_of_two");
469 another_map.set ("one", "another_value_of_one");
470 another_map.set ("two", "another_value_of_two");
472 assert (! test_map.has_all (another_map));
475 another_map.clear ();
477 // Test_Map and another_map are not the same
478 test_map.set ("one", "value_of_one");
479 another_map.set ("two", "value_of_two");
481 assert (! test_map.has_all (another_map));
484 another_map.clear ();
486 // Test_Map has a subset of another_map
487 test_map.set ("one", "value_of_one");
488 test_map.set ("two", "value_of_two");
489 test_map.set ("three", "value_of_three");
490 test_map.set ("four", "value_of_four");
491 test_map.set ("five", "value_of_five");
492 test_map.set ("six", "value_of_six");
493 another_map.set ("two", "value_of_two");
494 another_map.set ("three", "value_of_three");
495 another_map.set ("four", "value_of_four");
497 assert (test_map.has_all (another_map));
500 another_map.clear ();
502 // Test_Map has a subset of another_map in all but one element another_map
503 test_map.set ("one", "value_of_one");
504 test_map.set ("two", "value_of_two");
505 test_map.set ("three", "value_of_three");
506 test_map.set ("four", "value_of_four");
507 test_map.set ("five", "value_of_five");
508 test_map.set ("six", "value_of_six");
509 another_map.set ("two", "value_of_two");
510 another_map.set ("three", "value_of_three");
511 another_map.set ("four", "value_of_four");
512 another_map.set ("height", "value_of_height");
514 assert (! test_map.has_all (another_map));
517 public void test_gobject_properties () {
518 // Check the map exists
519 assert (test_map != null);
522 value = Value (typeof (int));
523 test_map.get_property ("size", ref value);
524 assert (value.get_int () == test_map.size);
528 public void test_fold () {
529 test_map.set ("one", "one");
530 test_map.set ("two", "two");
531 test_map.set ("three", "three");
535 count = test_map.map_iterator ().fold<int> ((x, y, z) => {return z + 1;}, 0);
538 var iter = test_map.map_iterator ();
539 assert (iter.next ());
540 count = iter.fold<int> ((x, y, z) => {return z + 1;}, 0);
544 public void test_foreach () {
545 test_map.set ("one", "one");
546 test_map.set ("two", "two");
547 test_map.set ("three", "three");
551 test_map.map_iterator ().foreach ((x, y) => {count++; return true;});
554 var iter = test_map.map_iterator ();
555 assert (iter.next ());
556 iter.foreach ((x, y) => {count++; return true;});
560 public static Map.Entry<string,string> entry_for (string key, string value) {
561 return new TestEntry<string,string> (key, value);
564 public static bool check_entry (owned Map.Entry<string,string> e, string key, string value) {
565 return e.key == key && e.value == value;
568 public static void assert_entry (Map.Entry<string,string> e, string key, string value) {
569 assert (e.key == key);
570 assert (e.value == value);
573 private class TestEntry<K,V> : Map.Entry<K,V> {
574 public TestEntry (K key, V value) {
579 public override K key { get {return _key; } }
581 public override V value { get; set; }
582 public override bool read_only { get { return false; } }