464ec0f11f8263a456c7b258d0e83f1a6f754d60
[platform/upstream/libgee.git] / tests / testmap.vala
1 /* testmap.vala
2  *
3  * Copyright (C) 2008  Jürg Billeter
4  * Copyright (C) 2009  Didier Villevalois, Julien Peeters, Maciej Piechotka
5  *
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.
10
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.
15
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
19  *
20  * Author:
21  *      Jürg Billeter <j@bitron.ch>
22  *      Maciej Piechotka <uzytkownik2@gmail.com>
23  *      Julien Peeters <contact@julienpeeters.fr>
24  */
25
26 using Gee;
27
28 public abstract class MapTests : Gee.TestCase {
29
30         public MapTests (string name) {
31                 base (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);
44         }
45
46         protected Map<string, string> test_map;
47
48         public void test_type_correctness () {
49                 // Check the map exists
50                 assert (test_map != null);
51
52                 // Check the advertised key and value types
53                 assert (test_map.key_type == typeof (string));
54                 assert (test_map.value_type == typeof (string));
55         }
56
57         public void test_has_key_size_is_empty () {
58                 // Check the collection exists
59                 assert (test_map != null);
60                 string value;
61
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);
68
69                 // Add a binding
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);
81
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);
94
95                 // Add more bindings
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);
105
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);
115
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);
125
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);
136
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);
148
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);
157
158                 // Remove all elements
159                 test_map.clear ();
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);
168         }
169
170         public void test_keys () {
171                 // Check keys on empty map
172                 var keys = test_map.keys;
173                 assert (keys.size == 0);
174
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"));
182
183                 // Check modify key set directly
184                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
185                                        TestTrapFlags.SILENCE_STDERR)) {
186                         assert (! keys.add ("three"));
187                         Posix.exit (0);
188                 }
189                 Test.trap_assert_failed ();
190                 Test.trap_assert_stderr ("*code should not be reached*");
191
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"));
201
202                 // Check keys on map clear
203                 test_map.clear ();
204                 assert (keys.size == 0);
205                 keys = test_map.keys;
206                 assert (keys.size == 0);
207         }
208
209         public void test_values () {
210                 // Check keys on empty map
211                 var values = test_map.values;
212                 assert (values.size == 0);
213
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"));
221
222                 // Check modify key set directly
223                 if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
224                                        TestTrapFlags.SILENCE_STDERR)) {
225                         assert (! values.add ("two"));
226                         Posix.exit (0);
227                 }
228                 Test.trap_assert_failed ();
229                 Test.trap_assert_stderr ("*code should not be reached*");
230
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"));
240
241                 // Check keys on map clear
242                 test_map.clear ();
243                 assert (values.size == 0);
244                 values = test_map.values;
245                 assert (values.size == 0);
246         }
247
248         public void test_entries () {
249                 // Check entries on empty map
250                 var entries = test_map.entries;
251                 assert (entries.size == 0);
252
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")));
260
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")));
265                         Posix.exit (0);
266                 }
267                 Test.trap_assert_failed ();
268                 Test.trap_assert_stderr ("*code should not be reached*");
269
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")));
279
280                 // Check keys on map clear
281                 test_map.clear ();
282                 assert (entries.size == 0);
283                 entries = test_map.entries;
284                 assert (entries.size == 0);
285         }
286         
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");
291                 
292                 test_map.clear ();
293                 assert (test_map.size == 0);
294                 
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 ());
300                 
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 ());
306                 
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 ());
312                 
313                 MapIterator<string, string> iter = test_map.map_iterator ();
314                 assert (iter != null);
315                 assert (!iter.has_next ());
316         }
317
318         public void test_set_all () {
319                 var another_map = new HashMap<string,string> ();
320
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");
327
328                 test_map.set_all (another_map);
329
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"));
337
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");
344         }
345
346         public void test_unset_all () {
347                 var another_map = new HashMap<string,string> ();
348
349                 // Check unset all on empty maps.
350                 assert (test_map.is_empty);
351                 assert (another_map.is_empty);
352
353                 assert (! test_map.unset_all (another_map));
354
355                 assert (test_map.is_empty);
356                 assert (another_map.is_empty);
357
358                 test_map.clear ();
359                 another_map.clear ();
360
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");
364
365                 assert (test_map.is_empty);
366                 assert (another_map.size == 2);
367
368                 assert (! test_map.unset_all (another_map));
369
370                 assert (test_map.is_empty);
371                 assert (another_map.size == 2);
372
373                 test_map.clear ();
374                 another_map.clear ();
375
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");
379
380                 assert (test_map.size == 2);
381                 assert (another_map.is_empty);
382
383                 assert (! test_map.unset_all (another_map));
384
385                 assert (test_map.size == 2);
386                 assert (another_map.is_empty);
387
388                 test_map.clear ();
389                 another_map.clear ();
390
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");
397
398                 assert (test_map.size == 2);
399                 assert (another_map.size == 2);
400
401                 assert (test_map.unset_all (another_map));
402
403                 assert (test_map.is_empty);
404                 assert (another_map.size == 2);
405
406                 test_map.clear ();
407                 another_map.clear ();
408
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");
419
420                 assert (test_map.size == 3);
421                 assert (another_map.size == 4);
422
423                 assert (test_map.unset_all (another_map));
424
425                 assert (test_map.size == 1);
426                 assert (another_map.size == 4);
427
428                 assert (test_map.has_key ("one"));
429         }
430
431         public void test_has_all () {
432                 var another_map = new HashMap<string,string> ();
433
434                 // Check empty.
435                 assert (test_map.has_all (another_map));
436
437                 // Test_Map has items, another_map is empty.
438                 test_map.set ("one", "value_of_one");
439
440                 assert (test_map.has_all (another_map));
441
442                 test_map.clear ();
443                 another_map.clear ();
444
445                 // Test_Map is empty, another_map has items.
446                 another_map.set ("one", "value_of_one");
447
448                 assert (! test_map.has_all (another_map));
449
450                 test_map.clear ();
451                 another_map.clear ();
452
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");
456
457                 another_map.set ("one", "value_of_one");
458                 another_map.set ("two", "value_of_two");
459
460                 assert (test_map.has_all (another_map));
461
462                 test_map.clear ();
463                 another_map.clear ();
464
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");
468
469                 another_map.set ("one", "another_value_of_one");
470                 another_map.set ("two", "another_value_of_two");
471
472                 assert (! test_map.has_all (another_map));
473
474                 test_map.clear ();
475                 another_map.clear ();
476
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");
480
481                 assert (! test_map.has_all (another_map));
482
483                 test_map.clear ();
484                 another_map.clear ();
485
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");
496
497                 assert (test_map.has_all (another_map));
498
499                 test_map.clear ();
500                 another_map.clear ();
501
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");
513
514                 assert (! test_map.has_all (another_map));
515         }
516
517         public void test_gobject_properties () {
518                 // Check the map exists
519                 assert (test_map != null);
520                 Value value;
521
522                 value = Value (typeof (int));
523                 test_map.get_property ("size", ref value);
524                 assert (value.get_int () == test_map.size);
525                 value.unset ();
526         }
527
528         public void test_fold () {
529                 test_map.set ("one", "one");
530                 test_map.set ("two", "two");
531                 test_map.set ("three", "three");
532                 
533                 int count;
534                 
535                 count = test_map.map_iterator ().fold<int> ((x, y, z) => {return z + 1;}, 0);
536                 assert (count == 3);
537                 
538                 var iter = test_map.map_iterator ();
539                 assert (iter.next ());
540                 count = iter.fold<int> ((x, y, z) => {return z + 1;}, 0);
541                 assert (count == 3);
542         }
543         
544         public void test_foreach () {
545                 test_map.set ("one", "one");
546                 test_map.set ("two", "two");
547                 test_map.set ("three", "three");
548                 
549                 int count = 0;
550                 
551                 test_map.map_iterator ().foreach ((x, y) => {count++;});
552                 assert (count == 3);
553                 
554                 var iter = test_map.map_iterator ();
555                 assert (iter.next ());
556                 iter.foreach ((x, y) => {count++;});
557                 assert (count == 6);
558         }
559
560         public static Map.Entry<string,string> entry_for (string key, string value) {
561                 return new TestEntry<string,string> (key, value);
562         }
563         
564         public static bool check_entry (owned Map.Entry<string,string> e, string key, string value) {
565                 return e.key == key && e.value == value;
566         }
567         
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);
571         }
572         
573         private class TestEntry<K,V> : Map.Entry<K,V> {
574                 public TestEntry (K key, V value) {
575                         this._key = key;
576                         this.value = value;
577                 }
578                 
579                 public override K key { get {return _key; } }
580                 private K _key;
581                 public override V value { get; set; }
582                 public override bool read_only { get { return false; } }
583         }
584 }
585