summaryrefslogtreecommitdiff
path: root/tests/testmap.vala
blob: 19fc091cf80479c21d84cd0c7d554735a1c054c1 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* testmap.vala
 *
 * Copyright (C) 2008  Jürg Billeter, Maciej Piechotka
 * Copyright (C) 2009  Didier Villevalois, Julien Peeters
 *
 * 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:
 * 	Jürg Billeter <j@bitron.ch>
 * 	Maciej Piechotka <uzytkownik2@gmail.com>
 * 	Julien Peeters <contact@julienpeeters.fr>
 */

using Gee;

public abstract class MapTests : Gee.TestCase {

	public MapTests (string name) {
		base (name);
		add_test ("[Map] type correctness", test_type_correctness);
		add_test ("[Map] has_key, size and is_empty",
		          test_has_key_size_is_empty);
		add_test ("[Map] keys", test_keys);
		add_test ("[Map] values", test_values);
		add_test ("[Map] entries", test_entries);
		add_test ("[Map] set all", test_set_all);
		add_test ("[Map] unset all", test_unset_all);
		add_test ("[Map] has all", test_has_all);
		add_test ("[Map] GObject properties", test_gobject_properties);
	}

	protected Map<string, string> test_map;

	public void test_type_correctness () {
		// Check the map exists
		assert (test_map != null);

		// Check the advertised key and value types
		assert (test_map.key_type == typeof (string));
		assert (test_map.value_type == typeof (string));
	}

	public void test_has_key_size_is_empty () {
		// Check the collection exists
		assert (test_map != null);
		string value;

		// Check the collection is initially empty
		assert (! test_map.has_key ("one"));
		assert (! test_map.has_key ("two"));
		assert (! test_map.has_key ("three"));
		assert (test_map.size == 0);
		assert (test_map.is_empty);

		// Add a binding
		test_map.set ("one", "value_of_one");
		assert (test_map.has_key ("one"));
		assert (test_map.has ("one", "value_of_one"));
		assert (! test_map.has ("one", "another_value_for_one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (! test_map.has_key ("two"));
		assert (test_map.get ("two") == null);
		assert (! test_map.has_key ("three"));
		assert (test_map.get ("three") == null);
		assert (test_map.size == 1);
		assert (! test_map.is_empty);

		// Remove the last added binding
		assert (test_map.unset ("one"));
		assert (! test_map.has_key ("one"));
		assert (! test_map.has ("one", "value_of_one"));
		assert (! test_map.has ("one", "another_value_for_one"));
		assert (test_map.get ("one") == null);
		assert (! test_map.has_key ("two"));
		assert (test_map.get ("two") ==  null);
		assert (! test_map.has_key ("three"));
		assert (test_map.get ("three") == null);
		assert (test_map.size == 0);
		assert (test_map.is_empty);

		// Add more bindings
		test_map.set ("one", "value_of_one");
		assert (test_map.has_key ("one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (! test_map.has_key ("two"));
		assert (test_map.get ("two") == null);
		assert (! test_map.has_key ("three"));
		assert (test_map.get ("three") == null);
		assert (test_map.size == 1);
		assert (! test_map.is_empty);

		test_map.set ("two", "value_of_two");
		assert (test_map.has_key ("one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (test_map.has_key ("two"));
		assert (test_map.get ("two") == "value_of_two");
		assert (! test_map.has_key ("three"));
		assert (test_map.get ("three") == null);
		assert (test_map.size == 2);
		assert (! test_map.is_empty);

		test_map.set ("three", "value_of_three");
		assert (test_map.has_key ("one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (test_map.has_key ("two"));
		assert (test_map.get ("two") == "value_of_two");
		assert (test_map.has_key ("three"));
		assert (test_map.get ("three") == "value_of_three");
		assert (test_map.size == 3);
		assert (! test_map.is_empty);

		// Update an existent binding
		test_map.set ("two", "value_of_two_new");
		assert (test_map.has_key ("one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (test_map.has_key ("two"));
		assert (test_map.get ("two") == "value_of_two_new");
		assert (test_map.has_key ("three"));
		assert (test_map.get ("three") == "value_of_three");
		assert (test_map.size == 3);
		assert (! test_map.is_empty);

		// Remove one element
		assert (test_map.unset ("two", out value));
		assert (value == "value_of_two_new");
		assert (test_map.has_key ("one"));
		assert (test_map.get ("one") == "value_of_one");
		assert (! test_map.has_key ("two"));
		assert (test_map.get ("two") == null);
		assert (test_map.has_key ("three"));
		assert (test_map.get ("three") == "value_of_three");
		assert (test_map.size == 2);
		assert (! test_map.is_empty);

		// Remove the same element again
		assert (! test_map.unset ("two", out value));
		assert (value == null);
		assert (test_map.has_key ("one"));
		assert (! test_map.has_key ("two"));
		assert (test_map.has_key ("three"));
		assert (test_map.size == 2);
		assert (! test_map.is_empty);

		// Remove all elements
		test_map.clear ();
		assert (! test_map.has_key ("one"));
		assert (test_map.get ("one") == null);
		assert (! test_map.has_key ("two"));
		assert (test_map.get ("two") == null);
		assert (! test_map.has_key ("three"));
		assert (test_map.get ("three") == null);
		assert (test_map.size == 0);
		assert (test_map.is_empty);
	}

	public void test_keys () {
		// Check keys on empty map
		var keys = test_map.keys;
		assert (keys.size == 0);

		// Check keys on map with one item
		test_map.set ("one", "value_of_one");
		assert (keys.size == 1);
		assert (keys.contains ("one"));
		keys = test_map.keys;
		assert (keys.size == 1);
		assert (keys.contains ("one"));

		// Check modify key set directly
		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
		                       TestTrapFlags.SILENCE_STDERR)) {
			assert (! keys.add ("three"));
			Posix.exit (0);
		}
		Test.trap_assert_failed ();
		Test.trap_assert_stderr ("*code should not be reached*");

		// Check keys on map with multiple items
		test_map.set ("two", "value_of_two");
		assert (keys.size == 2);
		assert (keys.contains ("one"));
		assert (keys.contains ("two"));
		keys = test_map.keys;
		assert (keys.size == 2);
		assert (keys.contains ("one"));
		assert (keys.contains ("two"));

		// Check keys on map clear
		test_map.clear ();
		assert (keys.size == 0);
		keys = test_map.keys;
		assert (keys.size == 0);
	}

	public void test_values () {
		// Check keys on empty map
		var values = test_map.values;
		assert (values.size == 0);

		// Check keys on map with one item
		test_map.set ("one", "value_of_one");
		assert (values.size == 1);
		assert (values.contains ("value_of_one"));
		values = test_map.values;
		assert (values.size == 1);
		assert (values.contains ("value_of_one"));

		// Check modify key set directly
		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
		                       TestTrapFlags.SILENCE_STDERR)) {
			assert (! values.add ("two"));
			Posix.exit (0);
		}
		Test.trap_assert_failed ();
		Test.trap_assert_stderr ("*code should not be reached*");

		// Check keys on map with multiple items
		test_map.set ("two", "value_of_two");
		assert (values.size == 2);
		assert (values.contains ("value_of_one"));
		assert (values.contains ("value_of_two"));
		values = test_map.values;
		assert (values.size == 2);
		assert (values.contains ("value_of_one"));
		assert (values.contains ("value_of_two"));

		// Check keys on map clear
		test_map.clear ();
		assert (values.size == 0);
		values = test_map.values;
		assert (values.size == 0);
	}

	public void test_entries () {
		// Check entries on empty map
		var entries = test_map.entries;
		assert (entries.size == 0);

		// Check entries on map with one item
		test_map.set ("one", "value_of_one");
		assert (entries.size == 1);
		assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
		entries = test_map.entries;
		assert (entries.size == 1);
		assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));

		// Check modify entry set directly
		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
		                       TestTrapFlags.SILENCE_STDERR)) {
			assert (! entries.add (new TestEntry<string,string> ("two", "value_of_two")));
			Posix.exit (0);
		}
		Test.trap_assert_failed ();
		Test.trap_assert_stderr ("*code should not be reached*");

		// Check entries on map with multiple items
		test_map.set ("two", "value_of_two");
		assert (entries.size == 2);
		assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
		assert (entries.contains (new TestEntry<string,string> ("two", "value_of_two")));
		entries = test_map.entries;
		assert (entries.size == 2);
		assert (entries.contains (new TestEntry<string,string> ("one", "value_of_one")));
		assert (entries.contains (new TestEntry<string,string> ("two", "value_of_two")));

		// Check keys on map clear
		test_map.clear ();
		assert (entries.size == 0);
		entries = test_map.entries;
		assert (entries.size == 0);
	}
	
	public void test_clear () {
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		test_map.set ("three", "value_of_three");
		
		test_map.clear ();
		assert (test_map.size == 0);
		
		Set<string> keys = test_map.keys;
		assert (keys != null);
		Iterator<string> ikeys = keys.iterator ();
		assert (ikeys != null);
		assert (!ikeys.has_next ());
		
		Collection<string> vals = test_map.values;
		assert (vals != null);
		Iterator<string> ivals = vals.iterator ();
		assert (ivals != null);
		assert (!ivals.has_next ());
		
		Set<Map.Entry<string, string>> ents = test_map.entries;
		assert (ents != null);
		Iterator<Map.Entry<string, string>> ients = ents.iterator ();
		assert (ients != null);
		assert (!ients.has_next ());
		
		MapIterator<string, string> iter = test_map.map_iterator ();
		assert (iter != null);
		assert (!iter.has_next ());
	}

	public void test_set_all () {
		var another_map = new HashMap<string,string> (str_hash,
		                                              str_equal,
		                                              str_equal);

		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		test_map.set ("three", "value_of_three");
		another_map.set ("four", "value_of_four");
		another_map.set ("five", "value_of_five");
		another_map.set ("six", "value_of_six");

		test_map.set_all (another_map);

		assert (test_map.size == 6);
		assert (test_map.has_key ("one"));
		assert (test_map.has_key ("two"));
		assert (test_map.has_key ("three"));
		assert (test_map.has_key ("four"));
		assert (test_map.has_key ("five"));
		assert (test_map.has_key ("six"));

		assert (test_map.get ("one") == "value_of_one");
		assert (test_map.get ("two") == "value_of_two");
		assert (test_map.get ("three") == "value_of_three");
		assert (test_map.get ("four") == "value_of_four");
		assert (test_map.get ("five") == "value_of_five");
		assert (test_map.get ("six") == "value_of_six");
	}

	public void test_unset_all () {
		var another_map = new HashMap<string,string> (str_hash,
		                                              str_equal,
		                                              str_equal);

		// Check unset all on empty maps.
		assert (test_map.is_empty);
		assert (another_map.is_empty);

		assert (! test_map.unset_all (another_map));

		assert (test_map.is_empty);
		assert (another_map.is_empty);

		test_map.clear ();
		another_map.clear ();

		// Test_Map is empty, another_map has entries. -> no change
		another_map.set ("one", "value_of_one");
		another_map.set ("two", "value_of_two");

		assert (test_map.is_empty);
		assert (another_map.size == 2);

		assert (! test_map.unset_all (another_map));

		assert (test_map.is_empty);
		assert (another_map.size == 2);

		test_map.clear ();
		another_map.clear ();

		// Test_Map has entries, another_map is empty. -> no change
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");

		assert (test_map.size == 2);
		assert (another_map.is_empty);

		assert (! test_map.unset_all (another_map));

		assert (test_map.size == 2);
		assert (another_map.is_empty);

		test_map.clear ();
		another_map.clear ();

		// Test_Map and another_map have the same
		// entries -> test_map is cleared
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		another_map.set ("one", "value_of_one");
		another_map.set ("two", "value_of_two");

		assert (test_map.size == 2);
		assert (another_map.size == 2);

		assert (test_map.unset_all (another_map));

		assert (test_map.is_empty);
		assert (another_map.size == 2);

		test_map.clear ();
		another_map.clear ();

		// Test_Map has some common keys with another_map
		// but both have also unique keys -> common key are
		// cleared from test_map
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		test_map.set ("three", "value_of_three");
		another_map.set ("two", "value_of_two");
		another_map.set ("three", "value_of_three");
		another_map.set ("four", "value_of_four");
		another_map.set ("five", "value_of_five");

		assert (test_map.size == 3);
		assert (another_map.size == 4);

		assert (test_map.unset_all (another_map));

		assert (test_map.size == 1);
		assert (another_map.size == 4);

		assert (test_map.has_key ("one"));
	}

	public void test_has_all () {
		var another_map = new HashMap<string,string> (str_hash,
		                                              str_equal,
		                                              str_equal);

		// Check empty.
		assert (test_map.has_all (another_map));

		// Test_Map has items, another_map is empty.
		test_map.set ("one", "value_of_one");

		assert (test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map is empty, another_map has items.
		another_map.set ("one", "value_of_one");

		assert (! test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map and another_map are the same.
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");

		another_map.set ("one", "value_of_one");
		another_map.set ("two", "value_of_two");

		assert (test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map and another_map are not the same.
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");

		another_map.set ("one", "another_value_of_one");
		another_map.set ("two", "another_value_of_two");

		assert (! test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map and another_map are not the same
		test_map.set ("one", "value_of_one");
		another_map.set ("two", "value_of_two");

		assert (! test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map has a subset of another_map
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		test_map.set ("three", "value_of_three");
		test_map.set ("four", "value_of_four");
		test_map.set ("five", "value_of_five");
		test_map.set ("six", "value_of_six");
		another_map.set ("two", "value_of_two");
		another_map.set ("three", "value_of_three");
		another_map.set ("four", "value_of_four");

		assert (test_map.has_all (another_map));

		test_map.clear ();
		another_map.clear ();

		// Test_Map has a subset of another_map in all but one element another_map
		test_map.set ("one", "value_of_one");
		test_map.set ("two", "value_of_two");
		test_map.set ("three", "value_of_three");
		test_map.set ("four", "value_of_four");
		test_map.set ("five", "value_of_five");
		test_map.set ("six", "value_of_six");
		another_map.set ("two", "value_of_two");
		another_map.set ("three", "value_of_three");
		another_map.set ("four", "value_of_four");
		another_map.set ("height", "value_of_height");

		assert (! test_map.has_all (another_map));
	}

	public void test_gobject_properties () {
		// Check the map exists
		assert (test_map != null);
		Value value;

		value = Value (typeof (bool));
		test_map.get_property ("is-empty", ref value);
		assert (value.get_boolean () == test_map.is_empty);
		value.unset ();

		value = Value (typeof (int));
		test_map.get_property ("size", ref value);
		assert (value.get_int () == test_map.size);
		value.unset ();
	}



	public class TestEntry<K,V> : Map.Entry<K,V> {
		public TestEntry (K key, V value) {
			this._key = key;
			this.value = value;
		}

		public override K key { get {return _key; } }
		private K _key;
		public override V value { get; set; }
	}
}