3 * Copyright (C) 2008 Jürg Billeter
4 * Copyright (C) 2009 Didier Villevalois, Julien Peeters
5 * Copyright (C) 2011-2012 Maciej Piechotka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Jürg Billeter <j@bitron.ch>
23 * Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
24 * Julien Peeters <contact@julienpeeters.fr>
25 * Maciej Piechotka <uzytkownik2@gmail.com>
30 public abstract class CollectionTests : Gee.TestCase {
32 public CollectionTests (string name) {
34 add_test ("[Collection] type correctness", test_type_correctness);
35 add_test ("[Collection] iterator returns all elements once",
36 test_iterator_returns_all_elements_once);
37 add_test ("[Collection] mutable iterator", test_mutable_iterator);
38 add_test ("[Collection] contains, size and is_empty",
39 test_contains_size_and_is_empty);
40 add_test ("[Collection] add and remove", test_add_remove);
41 add_test ("[Collection] clear", test_clear);
42 add_test ("[Collection] add_all", test_add_all);
43 add_test ("[Collection] contains_all", test_contains_all);
44 add_test ("[Collection] remove_all", test_remove_all);
45 add_test ("[Collection] retain_all", test_retain_all);
46 add_test ("[Collection] to_array", test_to_array);
47 add_test ("[Collection] GObject properties", test_gobject_properties);
48 add_test ("[Collection] fold", test_fold);
49 add_test ("[Collection] foreach", test_foreach);
50 add_test ("[Collection] map", test_map);
51 add_test ("[Collection] scan", test_scan);
52 add_test ("[Collection] filter", test_filter);
53 add_test ("[Collection] chop", test_chop);
56 protected Collection<string> test_collection;
58 public void test_type_correctness () {
59 // Check the collection exists
60 assert (test_collection != null);
62 // Check the advertised element type
63 assert (test_collection.element_type == typeof (string));
66 public void test_iterator_returns_all_elements_once () {
67 // Check the collection exists
68 assert (test_collection != null);
71 // Check with an empty collection
72 Iterator<string> iterator = test_collection.iterator ();
73 assert (! iterator.has_next ());
74 assert (! iterator.next ());
76 unowned string[] data = TestData.get_data ();
78 // Check for some elements in the collection
79 foreach (unowned string el in data) {
80 assert (test_collection.add (el));
83 uint[] found_times = new uint[data.length];
84 for (uint i = 0; i < 2; i++) {
85 for (uint j = 0; j < found_times.length; j++) {
88 iterator = test_collection.iterator ();
89 bool valid = iterator.valid;
92 has_next = iterator.has_next ();
93 assert (valid == iterator.valid);
94 assert (has_next == iterator.next ());
95 assert (valid = iterator.valid);
100 string element = iterator.get ();
101 assert (iterator.valid);
102 for (uint element_idx = 0;; element_idx++) {
103 assert (element_idx < data.length);
104 if (data[element_idx] == element) {
105 found_times[element_idx]++;
110 has_next = iterator.has_next ();
112 assert (iterator.valid);
113 assert (has_next == iterator.next ());
114 assert (iterator.valid);
115 foreach (var ft in found_times) {
121 public void test_mutable_iterator () {
122 // Check the collection exists
123 assert (test_collection != null);
126 // Check with an empty collection
127 Iterator<string> iterator = test_collection.iterator ();
130 unowned string[] data = TestData.get_data ();
131 unowned uint[] idx = TestData.get_drawn_numbers ();
133 // Check for some elements in the collection and remove few
134 foreach (unowned string el in data) {
135 assert (test_collection.add (el));
138 iterator = test_collection.iterator ();
139 uint[] found_times = new uint[data.length];
140 for (uint i = 0; i <= idx.length; i++) {
141 for (uint j = 0; j < found_times.length; j++) {
144 iterator = test_collection.iterator ();
145 assert (! iterator.valid);
146 bool last_removed = false;
148 has_next = iterator.has_next ();
149 assert (has_next == iterator.next ());
154 string element = iterator.get ();
155 assert (iterator.valid);
156 for (uint element_idx = 0;; element_idx++) {
157 assert (element_idx < data.length);
158 if (data[element_idx] == element) {
159 if (i != idx.length && data[element_idx] == data[idx[i]]) {
161 assert (! iterator.valid);
164 last_removed = false;
166 found_times[element_idx]++;
171 has_next = iterator.has_next ();
173 assert (iterator.valid == !last_removed);
174 assert (has_next == iterator.next ());
175 assert (iterator.valid == !last_removed);
176 for (uint j = 0; j < found_times.length; j++) {
177 bool removed = false;
178 for (int k = 0; k < i; k++) {
184 assert (found_times[j] == (removed ? 0 : 1));
189 public void test_contains_size_and_is_empty () {
190 // Check the collection exists
191 assert (test_collection != null);
193 unowned string[] data = TestData.get_data ();
194 unowned uint[] idx = TestData.get_drawn_numbers ();
196 // Check the collection is initially empty
197 foreach (unowned string s in data) {
198 assert (! test_collection.contains (s));
200 assert (test_collection.size == 0);
201 assert (test_collection.is_empty);
204 assert (test_collection.add (data[0]));
205 assert (test_collection.contains (data[0]));
206 for (uint i = 1; i < data.length; i++) {
207 assert (! test_collection.contains (data[i]));
209 assert (test_collection.size == 1);
210 assert (! test_collection.is_empty);
212 // Remove the added element
213 assert (test_collection.remove ("one"));
214 foreach (unowned string s in data) {
215 assert (! test_collection.contains (s));
217 assert (test_collection.size == 0);
218 assert (test_collection.is_empty);
221 for (uint i = 0; i < data.length; i++) {
222 assert (test_collection.add (data[i]));
223 for (uint j = 0; j <= i; j++) {
224 assert (test_collection.contains (data[j]));
226 for (uint j = i + 1; j < data.length; j++) {
227 assert (! test_collection.contains (data[j]));
229 assert (test_collection.size == i + 1);
230 assert (! test_collection.is_empty);
232 for (uint i = 0; i < idx.length; i++) {
233 // Remove one element
234 assert (test_collection.remove (data[idx[i]]));
235 for (uint j = 0; j < data.length; j++) {
236 bool removed = false;
237 for (uint k = 0; k <= i; k++) {
243 assert (test_collection.contains (data[j]) == !removed);
245 assert (test_collection.size == data.length - (i + 1));
247 // Remove the same element again
248 assert (! test_collection.remove (data[idx[i]]));
249 for (uint j = 0; j < data.length; j++) {
250 bool removed = false;
251 for (uint k = 0; k <= i; k++) {
257 assert (test_collection.contains (data[j]) == !removed);
259 assert (test_collection.size == data.length - (i + 1));
262 // Remove all elements
263 test_collection.clear ();
264 foreach (unowned string el in data) {
265 assert (! test_collection.contains (el));
267 assert (test_collection.size == 0);
268 assert (test_collection.is_empty);
271 public void test_add_remove () {
272 // Check the collection exists
273 assert (test_collection != null);
275 unowned string[] to_add = TestData.get_data ();
276 var expected_size = 0;
278 foreach (var a in to_add) {
279 assert (!test_collection.contains (a));
280 assert (test_collection.size == expected_size++);
281 assert (test_collection.add (a));
282 assert (test_collection.contains (a));
284 assert (test_collection.size == to_add.length);
286 foreach (var a in to_add) {
287 assert (test_collection.contains (a));
290 foreach (var a in to_add) {
291 assert (test_collection.contains (a));
292 assert (test_collection.size == expected_size--);
293 assert (test_collection.remove (a));
294 assert (!test_collection.contains (a));
296 assert (test_collection.size == 0);
299 public void test_clear () {
300 // Check the collection exists
301 assert (test_collection != null);
303 unowned string[] to_add = TestData.get_data ();
304 var expected_size = 0;
306 foreach (var a in to_add) {
307 assert (!test_collection.contains (a));
308 assert (test_collection.size == expected_size++);
309 assert (test_collection.add (a));
310 assert (test_collection.contains (a));
312 assert (test_collection.size == to_add.length);
314 test_collection.clear ();
316 assert (test_collection.size == 0);
318 Iterator<string> iter = test_collection.iterator ();
319 assert (iter != null);
320 assert (!iter.has_next ());
324 public void test_add_all () {
325 // Check the collection exists
326 assert (test_collection != null);
328 // Creating a dummy collection
329 var dummy = new ArrayList<string> ();
331 // Check when the test collection is initially empty and
332 // dummy collection is empty
333 assert (! test_collection.add_all (dummy));
335 assert (test_collection.is_empty);
336 assert (dummy.is_empty);
338 // Check when test collection is not empty but dummy is
339 assert (test_collection.add ("hello"));
340 assert (! test_collection.add_all (dummy));
342 assert (test_collection.size == 1);
343 assert (test_collection.contains ("hello"));
345 test_collection.clear ();
348 // Check when the test collection is initially empty and
349 // dummy collection is not empty
350 assert (dummy.add ("hello1"));
351 assert (dummy.add ("hello2"));
352 assert (dummy.add ("hello3"));
354 assert (test_collection.add_all (dummy));
356 assert (test_collection.size == 3);
357 assert (test_collection.contains ("hello1"));
358 assert (test_collection.contains ("hello2"));
359 assert (test_collection.contains ("hello3"));
360 assert (dummy.size == 3);
361 assert (dummy.contains ("hello1"));
362 assert (dummy.contains ("hello2"));
363 assert (dummy.contains ("hello3"));
365 test_collection.clear ();
368 // Check when the test collection is not empty and both
369 // collections does not intersect
370 assert (dummy.add ("hello1"));
371 assert (dummy.add ("hello2"));
372 assert (dummy.add ("hello3"));
374 assert (test_collection.add ("hello"));
376 assert (test_collection.add_all (dummy));
378 assert (test_collection.size == 4);
379 assert (test_collection.contains ("hello"));
380 assert (test_collection.contains ("hello1"));
381 assert (test_collection.contains ("hello2"));
382 assert (test_collection.contains ("hello3"));
383 assert (dummy.size == 3);
384 assert (dummy.contains ("hello1"));
385 assert (dummy.contains ("hello2"));
386 assert (dummy.contains ("hello3"));
388 test_collection.clear ();
391 // Check when the test collection is not empty and both
392 // collection intersect
393 assert (dummy.add ("hello1"));
394 assert (dummy.add ("hello2"));
395 assert (dummy.add ("hello3"));
397 assert (test_collection.add ("hello1"));
399 assert (test_collection.add_all (dummy));
401 // We can only assert the result is greater or equal than 3
402 // as we do not assume duplicates
403 assert (test_collection.size >= 3);
404 assert (test_collection.contains ("hello1"));
405 assert (test_collection.contains ("hello2"));
406 assert (test_collection.contains ("hello3"));
407 assert (dummy.size == 3);
408 assert (dummy.contains ("hello1"));
409 assert (dummy.contains ("hello2"));
410 assert (dummy.contains ("hello3"));
413 public void test_contains_all () {
414 // Check the collection exists
415 assert (test_collection != null);
417 // Creating a dummy collection
418 var dummy = new ArrayList<string> ();
419 assert (dummy.add ("hello1"));
420 assert (dummy.add ("hello2"));
421 assert (dummy.add ("hello3"));
423 // Check when the test collection is initially empty
424 assert (test_collection.is_empty);
425 assert (! test_collection.contains_all (dummy));
427 // Check when the test collection is not empty and both
428 // collections does not intersect
429 assert (test_collection.add ("hello4"));
430 assert (test_collection.add ("hello5"));
431 assert (! test_collection.contains_all (dummy));
433 // Check when the test collection is not empty and both
434 // collections intersect but are not equal
435 assert (test_collection.add ("hello1"));
436 assert (test_collection.add ("hello2"));
437 assert (! test_collection.contains_all (dummy));
439 // Check when the test collection is not empty and the
440 // dummy collection is contained in the test one
441 assert (test_collection.add ("hello3"));
442 assert (test_collection.contains_all (dummy));
443 assert (! dummy.contains_all (test_collection));
446 public void test_remove_all () {
447 // Check the collection exists
448 assert (test_collection != null);
450 // Creating a dummy collection
451 var dummy = new ArrayList<string> ();
453 // Check when both collection are intially empty
454 assert (! test_collection.remove_all (dummy));
456 assert (test_collection.is_empty);
457 assert (dummy.is_empty);
459 // Check when the test collection is initially empty and
460 // dummy collection is not empty
461 assert (dummy.add ("hello1"));
462 assert (dummy.add ("hello2"));
463 assert (dummy.add ("hello3"));
465 assert (! test_collection.remove_all (dummy));
467 assert (test_collection.is_empty);
469 test_collection.clear ();
472 // Check when the test collection is not empty and both
473 // collections does not intersect
474 assert (dummy.add ("hello1"));
475 assert (dummy.add ("hello2"));
476 assert (dummy.add ("hello3"));
477 assert (test_collection.add ("hello4"));
478 assert (test_collection.add ("hello5"));
480 assert (! test_collection.remove_all (dummy));
482 assert (test_collection.size == 2);
483 assert (dummy.size == 3);
485 test_collection.clear ();
488 // Check when the test collection is not empty and both
489 // collections intersect
490 assert (dummy.add ("hello1"));
491 assert (dummy.add ("hello2"));
492 assert (dummy.add ("hello3"));
493 assert (test_collection.add ("hello1"));
494 assert (test_collection.add ("hello2"));
495 assert (test_collection.add ("hello3"));
497 assert (test_collection.remove_all (dummy));
499 assert (test_collection.is_empty);
500 assert (dummy.size == 3);
502 test_collection.clear ();
506 public void test_retain_all () {
507 // Check the collection exists
508 assert (test_collection != null);
510 // Creating a dummy collection
511 var dummy = new ArrayList<string> ();
513 // Check when the test collection is initially empty and
514 // dummy collection is empty
515 assert (! test_collection.retain_all (dummy));
517 assert (test_collection.is_empty);
518 assert (dummy.is_empty);
520 // Check when the test collection is not empty and
522 assert (test_collection.add ("hello1"));
523 assert (test_collection.add ("hello2"));
525 assert (test_collection.retain_all (dummy));
527 assert (test_collection.is_empty);
528 assert (dummy.is_empty);
530 test_collection.clear ();
533 // Check when the test collection is initially empty and
534 // dummy collection is not empty
535 assert (dummy.add ("hello1"));
536 assert (dummy.add ("hello2"));
537 assert (dummy.add ("hello3"));
539 assert (! test_collection.retain_all (dummy));
541 assert (test_collection.is_empty);
542 assert (dummy.size == 3);
543 assert (dummy.contains ("hello1"));
544 assert (dummy.contains ("hello2"));
545 assert (dummy.contains ("hello3"));
547 test_collection.clear ();
550 // Check when the test collection is not empty and both
551 // collection does not intersect
552 assert (dummy.add ("hello1"));
553 assert (dummy.add ("hello2"));
554 assert (dummy.add ("hello3"));
555 assert (test_collection.add ("hello4"));
556 assert (test_collection.add ("hello5"));
558 assert (test_collection.retain_all (dummy));
560 assert (test_collection.is_empty);
561 assert (dummy.size == 3);
562 assert (dummy.contains ("hello1"));
563 assert (dummy.contains ("hello2"));
564 assert (dummy.contains ("hello3"));
566 test_collection.clear ();
569 // Check when both collections have the same elements
570 assert (dummy.add ("hello1"));
571 assert (dummy.add ("hello2"));
572 assert (dummy.add ("hello3"));
573 assert (test_collection.add ("hello1"));
574 assert (test_collection.add ("hello2"));
575 assert (test_collection.add ("hello3"));
577 assert (! test_collection.retain_all (dummy));
579 assert (test_collection.size == 3);
580 assert (test_collection.contains ("hello1"));
581 assert (test_collection.contains ("hello2"));
582 assert (test_collection.contains ("hello3"));
583 assert (dummy.size == 3);
584 assert (dummy.contains ("hello1"));
585 assert (dummy.contains ("hello2"));
586 assert (dummy.contains ("hello3"));
588 test_collection.clear ();
591 // Check when the test collection is not empty and both
592 // collections intersect but are not equal
593 assert (dummy.add ("hello1"));
594 assert (dummy.add ("hello2"));
595 assert (dummy.add ("hello3"));
596 assert (test_collection.add ("hello2"));
597 assert (test_collection.add ("hello3"));
598 assert (test_collection.add ("hello4"));
600 assert (test_collection.retain_all (dummy));
602 assert (test_collection.size == 2);
603 assert (test_collection.contains ("hello2"));
604 assert (test_collection.contains ("hello3"));
605 assert (dummy.size == 3);
606 assert (dummy.contains ("hello1"));
607 assert (dummy.contains ("hello2"));
608 assert (dummy.contains ("hello3"));
610 test_collection.clear ();
614 public void test_to_array () {
615 // Check the collection exists
616 assert (test_collection != null);
618 // Check the collection is empty
619 assert (test_collection.is_empty);
622 assert (test_collection.add ("hello1"));
623 assert (test_collection.add ("hello2"));
624 assert (test_collection.add ("hello3"));
626 // Check the conversion to array
627 string[] array = (string[]) test_collection.to_array ();
629 foreach (string element in test_collection) {
630 assert (element == array[index++]);
634 public void test_gobject_properties () {
635 // Check the collection exists
636 assert (test_collection != null);
639 value = Value (typeof (int));
640 test_collection.get_property ("size", ref value);
641 assert (value.get_int () == test_collection.size);
645 public void test_fold () {
646 assert (test_collection.add ("one"));
647 assert (test_collection.add ("two"));
648 assert (test_collection.add ("three"));
652 count = test_collection.fold<int> ((x, y) => {return y + 1;}, 0);
655 count = test_collection.iterator ().fold<int> ((x, y) => {return y + 1;}, 0);
658 Iterator<string> iter = test_collection.iterator ();
659 assert (iter.next ());
660 count = iter.fold<int> ((x, y) => {return y + 1;}, 0);
664 public void test_foreach () {
665 assert (test_collection.add ("one"));
666 assert (test_collection.add ("two"));
667 assert (test_collection.add ("three"));
671 test_collection.foreach ((x) => {count++; return true;});
674 test_collection.iterator ().foreach ((x) => {count++; return true;});
677 Iterator<string> iter = test_collection.iterator ();
678 assert (iter.next ());
679 iter.foreach ((x) => {count++; return true;});
683 public void test_map () {
684 assert (test_collection.add ("one"));
685 assert (test_collection.add ("two"));
686 assert (test_collection.add ("three"));
693 var iter = test_collection.iterator().map<int> ((str) => {
697 } else if (str == "two") {
700 } else if (str == "three") {
704 assert_not_reached ();
709 while (iter.next ()) {
711 assert (j == iter.get ());
712 assert (j == iter.get ());
718 assert (i == test_collection.size);
723 one = two = three = false;
726 iter = test_collection.map<int> ((str) => {
730 } else if (str == "two") {
733 } else if (str == "three") {
737 assert_not_reached ();
741 while (iter.next ()) {
743 assert (j == iter.get ());
744 assert (j == iter.get ());
750 assert (i == test_collection.size);
756 public void test_scan () {
757 assert (test_collection.add ("one"));
758 assert (test_collection.add ("two"));
759 assert (test_collection.add ("three"));
765 var iter = test_collection.iterator().scan<int> ((str, cur) => {
769 } else if (str == "two") {
772 } else if (str == "three") {
776 assert_not_reached ();
783 assert (j == iter.get ());
784 assert (j == iter.get ());
786 } while (iter.next ());
788 assert (j == test_collection.size + 1);
793 one = two = three = false;
796 iter = test_collection.scan<int> ((str, cur) => {
800 } else if (str == "two") {
803 } else if (str == "three") {
807 assert_not_reached ();
813 assert (j == iter.get ());
814 assert (j == iter.get ());
816 } while (iter.next ());
818 assert (j == test_collection.size + 1);
824 public void test_filter () {
825 assert (test_collection.add ("one"));
826 assert (test_collection.add ("two"));
827 assert (test_collection.add ("three"));
833 var iter = test_collection.iterator().filter ((str) => {
837 } else if (str == "two") {
840 } else if (str == "three") {
844 assert_not_reached ();
849 assert (!iter.valid);
852 while (iter.next ()) {
853 assert(iter.get () != "two");
861 one = two = three = false;
864 iter = test_collection.filter ((str) => {
868 } else if (str == "two") {
871 } else if (str == "three") {
875 assert_not_reached ();
880 assert (!iter.valid);
882 while (iter.next ()) {
883 assert(iter.get () != "two");
892 public void test_chop () {
893 assert (test_collection.add ("one"));
894 assert (test_collection.add ("two"));
895 assert (test_collection.add ("three"));
897 var iter = test_collection.iterator().chop (1, 1);
898 assert (!iter.valid);
899 var iter2 = test_collection.iterator();
901 assert (iter2.next ());
902 assert (iter2.next ());
903 assert (iter.next ());
904 assert (iter2.get () == iter.get ());
905 assert (!iter.next ());
906 assert (iter2.next ());
908 iter = test_collection.chop (1, 1);
909 assert (!iter.valid);
910 iter2 = test_collection.iterator();
912 assert (iter2.next ());
913 assert (iter2.next ());
914 assert (iter.next ());
915 assert (iter2.get () == iter.get ());
916 assert (!iter.next ());
917 assert (iter2.next ());