diff options
Diffstat (limited to 'tests/testcollection.vala')
-rw-r--r-- | tests/testcollection.vala | 660 |
1 files changed, 422 insertions, 238 deletions
diff --git a/tests/testcollection.vala b/tests/testcollection.vala index 6155f9e..ddb9308 100644 --- a/tests/testcollection.vala +++ b/tests/testcollection.vala @@ -2,6 +2,7 @@ * * Copyright (C) 2008 Jürg Billeter * Copyright (C) 2009 Didier Villevalois, Julien Peeters + * Copyright (C) 2011-2012 Maciej Piechotka * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -21,6 +22,7 @@ * Jürg Billeter <j@bitron.ch> * Didier 'Ptitjes' Villevalois <ptitjes@free.fr> * Julien Peeters <contact@julienpeeters.fr> + * Maciej Piechotka <uzytkownik2@gmail.com> */ using Gee; @@ -43,6 +45,12 @@ public abstract class CollectionTests : Gee.TestCase { add_test ("[Collection] retain_all", test_retain_all); add_test ("[Collection] to_array", test_to_array); add_test ("[Collection] GObject properties", test_gobject_properties); + add_test ("[Collection] fold", test_fold); + add_test ("[Collection] foreach", test_foreach); + add_test ("[Collection] map", test_map); + add_test ("[Collection] scan", test_scan); + add_test ("[Collection] filter", test_filter); + add_test ("[Collection] chop", test_chop); } protected Collection<string> test_collection; @@ -64,98 +72,50 @@ public abstract class CollectionTests : Gee.TestCase { Iterator<string> iterator = test_collection.iterator (); assert (! iterator.has_next ()); assert (! iterator.next ()); - assert (! iterator.first ()); - // Check for some elements in the collection - assert (test_collection.add ("one")); - assert (test_collection.add ("two")); - assert (test_collection.add ("three")); + unowned string[] data = TestData.get_data (); - bool one_found = false; - bool two_found = false; - bool three_found = false; - bool one_found_once = true; - bool two_found_once = true; - bool three_found_once = true; - iterator = test_collection.iterator (); - while (true) { - has_next = iterator.has_next (); - assert (has_next == iterator.next ()); - if (! has_next) { - break; - } + // Check for some elements in the collection + foreach (unowned string el in data) { + assert (test_collection.add (el)); + } - string element = iterator.get (); - if (element == "one") { - if (one_found) { - one_found_once = false; - } - one_found = true; - } else if (element == "two") { - if (two_found) { - two_found_once = false; - } - two_found = true; - } else if (element == "three") { - if (three_found) { - three_found_once = false; - } - three_found = true; + uint[] found_times = new uint[data.length]; + for (uint i = 0; i < 2; i++) { + for (uint j = 0; j < found_times.length; j++) { + found_times[j] = 0; } - } - has_next = iterator.has_next (); - assert (! has_next); - assert (has_next == iterator.next ()); - assert (one_found); - assert (one_found_once); - assert (two_found); - assert (two_found_once); - assert (three_found); - assert (three_found_once); - - // Do it twice to check first () - assert (iterator.first ()); - - one_found = false; - two_found = false; - three_found = false; - one_found_once = true; - two_found_once = true; - three_found_once = true; - while (true) { - string element = iterator.get (); - if (element == "one") { - if (one_found) { - one_found_once = false; - } - one_found = true; - } else if (element == "two") { - if (two_found) { - two_found_once = false; + iterator = test_collection.iterator (); + bool valid = iterator.valid; + assert (! valid); + while (true) { + has_next = iterator.has_next (); + assert (valid == iterator.valid); + assert (has_next == iterator.next ()); + assert (valid = iterator.valid); + if (! has_next) { + break; } - two_found = true; - } else if (element == "three") { - if (three_found) { - three_found_once = false; + + string element = iterator.get (); + assert (iterator.valid); + for (uint element_idx = 0;; element_idx++) { + assert (element_idx < data.length); + if (data[element_idx] == element) { + found_times[element_idx]++; + break; + } } - three_found = true; } - has_next = iterator.has_next (); + assert (! has_next); + assert (iterator.valid); assert (has_next == iterator.next ()); - if (! has_next) { - break; + assert (iterator.valid); + foreach (var ft in found_times) { + assert (ft == 1); } } - has_next = iterator.has_next (); - assert (! has_next); - assert (has_next == iterator.next ()); - assert (one_found); - assert (one_found_once); - assert (two_found); - assert (two_found_once); - assert (three_found); - assert (three_found_once); } public void test_mutable_iterator () { @@ -167,167 +127,143 @@ public abstract class CollectionTests : Gee.TestCase { Iterator<string> iterator = test_collection.iterator (); // ... - // Check for some elements in the collection and remove one - assert (test_collection.add ("one")); - assert (test_collection.add ("two")); - assert (test_collection.add ("three")); + unowned string[] data = TestData.get_data (); + unowned uint[] idx = TestData.get_drawn_numbers (); + + // Check for some elements in the collection and remove few + foreach (unowned string el in data) { + assert (test_collection.add (el)); + } - bool one_found = false; - bool two_found = false; - bool three_found = false; - bool one_found_once = true; - bool two_found_once = true; - bool three_found_once = true; iterator = test_collection.iterator (); - while (true) { - has_next = iterator.has_next (); - assert (has_next == iterator.next ()); - if (! has_next) { - break; + uint[] found_times = new uint[data.length]; + for (uint i = 0; i <= idx.length; i++) { + for (uint j = 0; j < found_times.length; j++) { + found_times[j] = 0; } - - string element = iterator.get (); - if (element == "one") { - if (one_found) { - one_found_once = false; + iterator = test_collection.iterator (); + assert (! iterator.valid); + bool last_removed = false; + while (true) { + has_next = iterator.has_next (); + assert (has_next == iterator.next ()); + if (! has_next) { + break; } - one_found = true; - } else if (element == "two") { - if (two_found) { - two_found_once = false; - } - two_found = true; - // Remove this element - iterator.remove (); - } else if (element == "three") { - if (three_found) { - three_found_once = false; - } - three_found = true; - } - } - has_next = iterator.has_next (); - assert (! has_next); - assert (has_next == iterator.next ()); - assert (one_found); - assert (one_found_once); - assert (two_found); - assert (two_found_once); - assert (three_found); - assert (three_found_once); - - // Check after removal - assert (iterator.first ()); - - one_found = false; - two_found = false; - three_found = false; - one_found_once = true; - two_found_once = true; - three_found_once = true; - while (true) { - string element = iterator.get (); - if (element == "one") { - if (one_found) { - one_found_once = false; + string element = iterator.get (); + assert (iterator.valid); + for (uint element_idx = 0;; element_idx++) { + assert (element_idx < data.length); + if (data[element_idx] == element) { + if (i != idx.length && data[element_idx] == data[idx[i]]) { + iterator.remove (); + assert (! iterator.valid); + last_removed = true; + } else { + last_removed = false; + } + found_times[element_idx]++; + break; + } } - one_found = true; - } else if (element == "two") { - two_found = true; - } else if (element == "three") { - if (three_found) { - three_found_once = false; - } - three_found = true; } - has_next = iterator.has_next (); + assert (! has_next); + assert (iterator.valid == !last_removed); assert (has_next == iterator.next ()); - if (! has_next) { - break; + assert (iterator.valid == !last_removed); + for (uint j = 0; j < found_times.length; j++) { + bool removed = false; + for (int k = 0; k < i; k++) { + if (idx[k] == j) { + removed = true; + break; + } + } + assert (found_times[j] == (removed ? 0 : 1)); } } - has_next = iterator.has_next (); - assert (! has_next); - assert (has_next == iterator.next ()); - assert (one_found); - assert (one_found_once); - assert (!two_found); - assert (three_found); - assert (three_found_once); } public void test_contains_size_and_is_empty () { // Check the collection exists assert (test_collection != null); + unowned string[] data = TestData.get_data (); + unowned uint[] idx = TestData.get_drawn_numbers (); + // Check the collection is initially empty - assert (! test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (! test_collection.contains ("three")); + foreach (unowned string s in data) { + assert (! test_collection.contains (s)); + } assert (test_collection.size == 0); assert (test_collection.is_empty); // Add an element - assert (test_collection.add ("one")); - assert (test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (! test_collection.contains ("three")); + assert (test_collection.add (data[0])); + assert (test_collection.contains (data[0])); + for (uint i = 1; i < data.length; i++) { + assert (! test_collection.contains (data[i])); + } assert (test_collection.size == 1); assert (! test_collection.is_empty); // Remove the added element assert (test_collection.remove ("one")); - assert (! test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (! test_collection.contains ("three")); + foreach (unowned string s in data) { + assert (! test_collection.contains (s)); + } assert (test_collection.size == 0); assert (test_collection.is_empty); // Add more elements - assert (test_collection.add ("one")); - assert (test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (! test_collection.contains ("three")); - assert (test_collection.size == 1); - assert (! test_collection.is_empty); - - assert (test_collection.add ("two")); - assert (test_collection.contains ("one")); - assert (test_collection.contains ("two")); - assert (! test_collection.contains ("three")); - assert (test_collection.size == 2); - assert (! test_collection.is_empty); - - assert (test_collection.add ("three")); - assert (test_collection.contains ("one")); - assert (test_collection.contains ("two")); - assert (test_collection.contains ("three")); - assert (test_collection.size == 3); - assert (! test_collection.is_empty); - - // Remove one element - assert (test_collection.remove ("two")); - assert (test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (test_collection.contains ("three")); - assert (test_collection.size == 2); - assert (! test_collection.is_empty); - - // Remove the same element again - assert (! test_collection.remove ("two")); - assert (test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (test_collection.contains ("three")); - assert (test_collection.size == 2); - assert (! test_collection.is_empty); + for (uint i = 0; i < data.length; i++) { + assert (test_collection.add (data[i])); + for (uint j = 0; j <= i; j++) { + assert (test_collection.contains (data[j])); + } + for (uint j = i + 1; j < data.length; j++) { + assert (! test_collection.contains (data[j])); + } + assert (test_collection.size == i + 1); + assert (! test_collection.is_empty); + } + for (uint i = 0; i < idx.length; i++) { + // Remove one element + assert (test_collection.remove (data[idx[i]])); + for (uint j = 0; j < data.length; j++) { + bool removed = false; + for (uint k = 0; k <= i; k++) { + if (idx[k] == j) { + removed = true; + break; + } + } + assert (test_collection.contains (data[j]) == !removed); + } + assert (test_collection.size == data.length - (i + 1)); + + // Remove the same element again + assert (! test_collection.remove (data[idx[i]])); + for (uint j = 0; j < data.length; j++) { + bool removed = false; + for (uint k = 0; k <= i; k++) { + if (idx[k] == j) { + removed = true; + break; + } + } + assert (test_collection.contains (data[j]) == !removed); + } + assert (test_collection.size == data.length - (i + 1)); + } // Remove all elements test_collection.clear (); - assert (! test_collection.contains ("one")); - assert (! test_collection.contains ("two")); - assert (! test_collection.contains ("three")); + foreach (unowned string el in data) { + assert (! test_collection.contains (el)); + } assert (test_collection.size == 0); assert (test_collection.is_empty); } @@ -336,16 +272,7 @@ public abstract class CollectionTests : Gee.TestCase { // Check the collection exists assert (test_collection != null); - string[] to_add = { - "one", "two", "three", "four", "five", "six", "seven", "eight", - "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", - "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", - "twenty one", "twenty two", "twenty three", "twenty four", - "twenty five", "twenty six", "twenty seven", "twenty eight", - "twenty nine", "thirty", "thirty one", "thirty two", "thirty four", - "thirty five", "thirty six", "thirty seven", "thirty eight", - "thirty nine", "fourty" - }; + unowned string[] to_add = TestData.get_data (); var expected_size = 0; foreach (var a in to_add) { @@ -373,16 +300,7 @@ public abstract class CollectionTests : Gee.TestCase { // Check the collection exists assert (test_collection != null); - string[] to_add = { - "one", "two", "three", "four", "five", "six", "seven", "eight", - "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", - "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", - "twenty one", "twenty two", "twenty three", "twenty four", - "twenty five", "twenty six", "twenty seven", "twenty eight", - "twenty nine", "thirty", "thirty one", "thirty two", "thirty four", - "thirty five", "thirty six", "thirty seven", "thirty eight", - "thirty nine", "fourty" - }; + unowned string[] to_add = TestData.get_data (); var expected_size = 0; foreach (var a in to_add) { @@ -718,19 +636,285 @@ public abstract class CollectionTests : Gee.TestCase { assert (test_collection != null); Value value; - value = Value (typeof (Type)); - test_collection.get_property ("element-type", ref value); - assert (value.get_gtype () == test_collection.element_type); - value.unset (); - - value = Value (typeof (bool)); - test_collection.get_property ("is-empty", ref value); - assert (value.get_boolean () == test_collection.is_empty); - value.unset (); - value = Value (typeof (int)); test_collection.get_property ("size", ref value); assert (value.get_int () == test_collection.size); value.unset (); } + + public void test_fold () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + int count; + + count = test_collection.fold<int> ((x, y) => {return y + 1;}, 0); + assert (count == 3); + + count = test_collection.iterator ().fold<int> ((x, y) => {return y + 1;}, 0); + assert (count == 3); + + Iterator<string> iter = test_collection.iterator (); + assert (iter.next ()); + count = iter.fold<int> ((x, y) => {return y + 1;}, 0); + assert (count == 3); + } + + public void test_foreach () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + int count = 0; + + test_collection.foreach ((x) => {count++; return true;}); + assert (count == 3); + + test_collection.iterator ().foreach ((x) => {count++; return true;}); + assert (count == 6); + + Iterator<string> iter = test_collection.iterator (); + assert (iter.next ()); + iter.foreach ((x) => {count++; return true;}); + assert (count == 9); + } + + public void test_map () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + bool one = false; + bool two = false; + bool three = false; + + int i = 0; + var iter = test_collection.iterator().map<int> ((str) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return i++; + }); + int j = 0; + while (iter.next ()) { + assert (i == j); + assert (j == iter.get ()); + assert (j == iter.get ()); + j++; + assert (i == j); + } + + assert (i == j); + assert (i == test_collection.size); + assert (one); + assert (two); + assert (three); + + one = two = three = false; + i = j = 0; + + iter = test_collection.map<int> ((str) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return i++; + }); + while (iter.next ()) { + assert (i == j); + assert (j == iter.get ()); + assert (j == iter.get ()); + j++; + assert (i == j); + } + + assert (i == j); + assert (i == test_collection.size); + assert (one); + assert (two); + assert (three); + } + + public void test_scan () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + bool one = false; + bool two = false; + bool three = false; + + var iter = test_collection.iterator().scan<int> ((str, cur) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return cur + 1; + }, 0); + + int j = 0; + do { + assert (j == iter.get ()); + assert (j == iter.get ()); + j++; + } while (iter.next ()); + + assert (j == test_collection.size + 1); + assert (one); + assert (two); + assert (three); + + one = two = three = false; + j = 0; + + iter = test_collection.scan<int> ((str, cur) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return cur + 1; + }, 0); + + do { + assert (j == iter.get ()); + assert (j == iter.get ()); + j++; + } while (iter.next ()); + + assert (j == test_collection.size + 1); + assert (one); + assert (two); + assert (three); + } + + public void test_filter () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + bool one = false; + bool two = false; + bool three = false; + + var iter = test_collection.iterator().filter ((str) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return str != "two"; + }); + + assert (!iter.valid); + + int j = 0; + while (iter.next ()) { + assert(iter.get () != "two"); + j++; + } + assert (j == 2); + assert (one); + assert (two); + assert (three); + + one = two = three = false; + j = 0; + + iter = test_collection.filter ((str) => { + if (str == "one") { + assert (!one); + one = true; + } else if (str == "two") { + assert (!two); + two = true; + } else if (str == "three") { + assert (!three); + three = true; + } else { + assert_not_reached (); + } + return str != "two"; + }); + + assert (!iter.valid); + + while (iter.next ()) { + assert(iter.get () != "two"); + j++; + } + assert (j == 2); + assert (one); + assert (two); + assert (three); + } + + public void test_chop () { + assert (test_collection.add ("one")); + assert (test_collection.add ("two")); + assert (test_collection.add ("three")); + + var iter = test_collection.iterator().chop (1, 1); + assert (!iter.valid); + var iter2 = test_collection.iterator(); + + assert (iter2.next ()); + assert (iter2.next ()); + assert (iter.next ()); + assert (iter2.get () == iter.get ()); + assert (!iter.next ()); + assert (iter2.next ()); + + iter = test_collection.chop (1, 1); + assert (!iter.valid); + iter2 = test_collection.iterator(); + + assert (iter2.next ()); + assert (iter2.next ()); + assert (iter.next ()); + assert (iter2.get () == iter.get ()); + assert (!iter.next ()); + assert (iter2.next ()); + } } + |