From 679f32fac58a45d3660eb83871be2cdfd9657d3f Mon Sep 17 00:00:00 2001 From: Maciej Piechotka Date: Tue, 27 Jul 2010 11:05:53 +0200 Subject: [PATCH] Fix lack of unsetting _first and _last in TreeSet on Set.clear() --- gee/treemap.vala | 2 +- gee/treeset.vala | 2 +- tests/testcollection.vala | 35 +++++++++++++++++++++++++++++++++++ tests/testmap.vala | 31 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/gee/treemap.vala b/gee/treemap.vala index f84de0f..90ddeca 100644 --- a/gee/treemap.vala +++ b/gee/treemap.vala @@ -357,7 +357,7 @@ public class Gee.TreeMap : Gee.AbstractMap { * {@inheritDoc} */ public override void clear () { - root = null; + first = last = root = null; _size = 0; stamp++; } diff --git a/gee/treeset.vala b/gee/treeset.vala index 1d7ee76..bb5d868 100644 --- a/gee/treeset.vala +++ b/gee/treeset.vala @@ -315,7 +315,7 @@ public class Gee.TreeSet : AbstractSet, SortedSet { * {@inheritDoc} */ public override void clear () { - root = null; + _first = _last = root = null; _size = 0; stamp++; } diff --git a/tests/testcollection.vala b/tests/testcollection.vala index 682302d..6155f9e 100644 --- a/tests/testcollection.vala +++ b/tests/testcollection.vala @@ -36,6 +36,7 @@ public abstract class CollectionTests : Gee.TestCase { add_test ("[Collection] contains, size and is_empty", test_contains_size_and_is_empty); add_test ("[Collection] add and remove", test_add_remove); + add_test ("[Collection] clear", test_clear); add_test ("[Collection] add_all", test_add_all); add_test ("[Collection] contains_all", test_contains_all); add_test ("[Collection] remove_all", test_remove_all); @@ -368,6 +369,40 @@ public abstract class CollectionTests : Gee.TestCase { assert (test_collection.size == 0); } + public void test_clear () { + // 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" + }; + var expected_size = 0; + + foreach (var a in to_add) { + assert (!test_collection.contains (a)); + assert (test_collection.size == expected_size++); + assert (test_collection.add (a)); + assert (test_collection.contains (a)); + } + assert (test_collection.size == to_add.length); + + test_collection.clear (); + + assert (test_collection.size == 0); + + Iterator iter = test_collection.iterator (); + assert (iter != null); + assert (!iter.has_next ()); + + } + public void test_add_all () { // Check the collection exists assert (test_collection != null); diff --git a/tests/testmap.vala b/tests/testmap.vala index 65ac245..19fc091 100644 --- a/tests/testmap.vala +++ b/tests/testmap.vala @@ -281,6 +281,37 @@ public abstract class MapTests : Gee.TestCase { 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 keys = test_map.keys; + assert (keys != null); + Iterator ikeys = keys.iterator (); + assert (ikeys != null); + assert (!ikeys.has_next ()); + + Collection vals = test_map.values; + assert (vals != null); + Iterator ivals = vals.iterator (); + assert (ivals != null); + assert (!ivals.has_next ()); + + Set> ents = test_map.entries; + assert (ents != null); + Iterator> ients = ents.iterator (); + assert (ients != null); + assert (!ients.has_next ()); + + MapIterator iter = test_map.map_iterator (); + assert (iter != null); + assert (!iter.has_next ()); + } public void test_set_all () { var another_map = new HashMap (str_hash, -- 2.7.4