[libgee] Refactor the Map implementations' tests
- From: Didier 'Ptitjes' Villevalois <dvillevalois src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [libgee] Refactor the Map implementations' tests
- Date: Fri, 11 Sep 2009 20:05:31 +0000 (UTC)
commit cefc2eeb1dd5d1ed6c3e49b8328247895fb98a40
Author: Julien Peeters <contact julienpeeters fr>
Date: Fri Sep 11 21:03:09 2009 +0200
Refactor the Map implementations' tests
Fixes part of bug 594241.
tests/Makefile.am | 22 +--
tests/testhashmap.vala | 537 ++----------------------------------------------
tests/testmain.vala | 2 +
tests/testmap.vala | 422 +++++++++++++++++++++++++++++++++++++
tests/testtreemap.vala | 348 ++++++-------------------------
5 files changed, 514 insertions(+), 817 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ddac44b..6b6ae94 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -19,17 +19,20 @@ tests_VALASOURCES = \
testcase.vala \
testcollection.vala \
testdeque.vala \
+ testhashmap.vala \
testhashmultimap.vala \
testhashmultiset.vala \
testlinkedlist.vala \
testlinkedlistasdeque.vala \
testlist.vala \
testmain.vala \
+ testmap.vala \
testmultimap.vala \
testmultiset.vala \
testqueue.vala \
testreadonlycollection.vala \
testreadonlylist.vala \
+ testtreemap.vala \
$(NULL)
tests_SOURCES = tests.vala.stamp $(tests_VALASOURCES:.vala=.c)
@@ -66,15 +69,6 @@ clean-local:
.PHONY: coverage-report
-TEST_PROGS += testhashmap
-testhashmap_VALASOURCES = testhashmap.vala
-testhashmap_SOURCES = testhashmap.c
-$(testhashmap_SOURCES): $(testhashmap_VALASOURCES)
- $(VALAC) -C --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^
- touch $@
-testhashmap_LDADD = $(progs_ldadd)
-EXTRA_DIST += $(testhashmap_VALASOURCES)
-
TEST_PROGS += testhashset
testhashset_VALASOURCES = testhashset.vala
testhashset_SOURCES = testhashset.c
@@ -92,13 +86,3 @@ $(testtreeset_SOURCES): $(testtreeset_VALASOURCES)
touch $@
testtreeset_LDADD = $(progs_ldadd)
EXTRA_DIST += $(testtreeset_VALASOURCES)
-
-TEST_PROGS += testtreemap
-testtreemap_VALASOURCES = testtreemap.vala
-testtreemap_SOURCES = testtreemap.c testtreemap.h
-$(testtreemap_SOURCES): $(testtreemap_VALASOURCES)
- $(VALAC) -C --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^
- touch $@
-testtreemap_LDADD = $(progs_ldadd)
-EXTRA_DIST += $(testtreemap_VALASOURCES)
-
diff --git a/tests/testhashmap.vala b/tests/testhashmap.vala
index b4967ae..599cba0 100644
--- a/tests/testhashmap.vala
+++ b/tests/testhashmap.vala
@@ -1,6 +1,7 @@
/* testhashmap.vala
*
* Copyright (C) 2008 Jürg Billeter
+ * 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
@@ -17,533 +18,35 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
- * Jürg Billeter <j bitron ch>
+ * Julien Peeters <contact julienpeeters fr>
*/
-using GLib;
using Gee;
-const string CODE_NOT_REACHABLE = "*code should not be reached*";
+public class HashMapTests : MapTests {
-void test_hashmap_get () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check get from empty map
- assert (hashmap.get ("foo") == null);
-
- // Check get from map with one items
- hashmap.set ("key", "value");
- assert (hashmap.get ("key") == "value");
-
- // Check get from non-existing key
- assert (hashmap.get ("foo") == null);
-
- // Check get from map with multiple items
- hashmap.set ("key2", "value2");
- hashmap.set ("key3", "value3");
- assert (hashmap.get ("key") == "value");
- assert (hashmap.get ("key2") == "value2");
- assert (hashmap.get ("key3") == "value3");
-
-}
-
-void test_hashmap_set () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // check map is empty
- assert (hashmap.size == 0);
-
- // check set an item to map
- hashmap.set ("abc", "one");
- assert (hashmap.contains ("abc"));
- assert (hashmap.get ("abc") == "one");
- assert (hashmap.size == 1);
-
- // check set an item to map with same value
- hashmap.set ("def", "one");
- assert (hashmap.contains ("def"));
- assert (hashmap.get ("abc") == "one");
- assert (hashmap.get ("def") == "one");
- assert (hashmap.size == 2);
-
- // check set with same key
- hashmap.set ("def", "two");
- assert (hashmap.contains ("def"));
- assert (hashmap.get ("abc") == "one");
- assert (hashmap.get ("def") == "two");
- assert (hashmap.size == 2);
-}
-
-void test_hashmap_remove () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
- string? value;
-
- // check removing when map is empty
- hashmap.remove ("foo");
- assert (hashmap.size == 0);
-
- // add items
- hashmap.set ("aaa", "111");
- hashmap.set ("bbb", "222");
- hashmap.set ("ccc", "333");
- hashmap.set ("ddd", "444");
- assert (hashmap.size == 4);
-
- // check remove on first place
- hashmap.remove ("aaa");
- assert (hashmap.size == 3);
-
- // check remove in between
- hashmap.remove ("ccc", out value);
- assert (hashmap.size == 2);
- assert (value == "333");
-
- // check remove in last place
- hashmap.remove ("ddd");
- assert (hashmap.size == 1);
-
- // check remove invalid key
- hashmap.remove ("bar", out value);
- assert (value == null);
-
- // check remove last in map
- hashmap.remove ("bbb");
- assert (hashmap.size == 0);
-}
-
-void test_hashmap_contains () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check on empty map
- assert (!hashmap.contains ("111"));
-
- // Check items
- hashmap.set ("10", "111");
- assert (hashmap.contains ("10"));
- assert (!hashmap.contains ("20"));
- assert (!hashmap.contains ("30"));
-
- assert (hashmap.get ("10") == "111");
-
- hashmap.set ("20", "222");
- assert (hashmap.contains ("10"));
- assert (hashmap.contains ("20"));
- assert (!hashmap.contains ("30"));
-
- assert (hashmap.get ("10") == "111");
- assert (hashmap.get ("20") == "222");
-
- hashmap.set ("30", "333");
- assert (hashmap.contains ("10"));
- assert (hashmap.contains ("20"));
- assert (hashmap.contains ("30"));
-
- assert (hashmap.get ("10") == "111");
- assert (hashmap.get ("20") == "222");
- assert (hashmap.get ("30") == "333");
-
- // Clear and recheck
- hashmap.clear ();
- assert (!hashmap.contains ("10"));
- assert (!hashmap.contains ("20"));
- assert (!hashmap.contains ("30"));
-
- var hashmapOfInt = new HashMap<int,int> ();
-
- // Check items
- hashmapOfInt.set (10, 111);
- assert (hashmapOfInt.contains (10));
- assert (!hashmapOfInt.contains (20));
- assert (!hashmapOfInt.contains (30));
-
- assert (hashmapOfInt.get (10) == 111);
-
- hashmapOfInt.set (20, 222);
- assert (hashmapOfInt.contains (10));
- assert (hashmapOfInt.contains (20));
- assert (!hashmapOfInt.contains (30));
-
- assert (hashmapOfInt.get (10) == 111);
- assert (hashmapOfInt.get (20) == 222);
-
- hashmapOfInt.set (30, 333);
- assert (hashmapOfInt.contains (10));
- assert (hashmapOfInt.contains (20));
- assert (hashmapOfInt.contains (30));
-
- assert (hashmapOfInt.get (10) == 111);
- assert (hashmapOfInt.get (20) == 222);
- assert (hashmapOfInt.get (30) == 333);
-}
-
-void test_hashmap_size () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check empty map
- assert (hashmap.size == 0);
-
- // Check when one item
- hashmap.set ("1", "1");
- assert (hashmap.size == 1);
-
- // Check when more items
- hashmap.set ("2", "2");
- assert (hashmap.size == 2);
-
- // Check when items cleared
- hashmap.clear ();
- assert (hashmap.size == 0);
-}
-
-void test_hashmap_get_keys () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check keys on empty map
- var keySet = hashmap.get_keys ();
- assert (keySet.size == 0);
-
- // Check keys on map with one item
- hashmap.set ("aaa", "111");
- assert (keySet.size == 1);
- assert (keySet.contains ("aaa"));
- keySet = hashmap.get_keys ();
- assert (keySet.size == 1);
- assert (keySet.contains ("aaa"));
-
- // Check modify key set directly
- if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
- keySet.add ("ccc");
- return;
+ public HashMapTests () {
+ base ("HashMap");
+ add_test ("[HashMap] selected functions", test_selected_functions);
}
- Test.trap_assert_failed ();
- Test.trap_assert_stderr (CODE_NOT_REACHABLE);
-
- // Check keys on map with multiple items
- hashmap.set ("bbb", "222");
- assert (keySet.size == 2);
- assert (keySet.contains ("aaa"));
- assert (keySet.contains ("bbb"));
- keySet = hashmap.get_keys ();
- assert (keySet.size == 2);
- assert (keySet.contains ("aaa"));
- assert (keySet.contains ("bbb"));
-
- // Check keys on map clear
- hashmap.clear ();
- assert (keySet.size == 0);
- keySet = hashmap.get_keys ();
- assert (keySet.size == 0);
-
-}
-
-void test_hashmap_get_values () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
- // Check keys on empty map
- var valueCollection = hashmap.get_values ();
- assert (valueCollection.size == 0);
-
- // Check keys on map with one item
- hashmap.set ("aaa", "111");
- assert (valueCollection.size == 1);
- assert (valueCollection.contains ("111"));
- valueCollection = hashmap.get_values ();
- assert (valueCollection.size == 1);
- assert (valueCollection.contains ("111"));
-
- // Check modify key set directly
- if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
- valueCollection.add ("ccc");
- return;
+ public override void set_up () {
+ test_map = new HashMap<string,string> ();
}
- Test.trap_assert_failed ();
- Test.trap_assert_stderr (CODE_NOT_REACHABLE);
-
- // Check keys on map with multiple items
- hashmap.set ("bbb", "222");
- assert (valueCollection.size == 2);
- assert (valueCollection.contains ("111"));
- assert (valueCollection.contains ("222"));
- valueCollection = hashmap.get_values ();
- assert (valueCollection.size == 2);
- assert (valueCollection.contains ("111"));
- assert (valueCollection.contains ("222"));
-
- // Check keys on map clear
- hashmap.clear ();
- assert (valueCollection.size == 0);
- valueCollection = hashmap.get_values ();
- assert (valueCollection.size == 0);
-
-}
-
-void test_hashmap_clear () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
- assert (hashmap.size == 0);
-
- // Check clear on empty map
- hashmap.clear ();
- assert (hashmap.size == 0);
-
- // Check clear one item
- hashmap.set ("1", "1");
- assert (hashmap.size == 1);
- hashmap.clear ();
- assert (hashmap.size == 0);
-
- // Check clear multiple items
- hashmap.set ("1", "1");
- hashmap.set ("2", "2");
- hashmap.set ("3", "3");
- assert (hashmap.size == 3);
- hashmap.clear ();
- assert (hashmap.size == 0);
-}
-
-void test_hashmap_empty () {
- var hashmap = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check empty map
- assert (hashmap.is_empty);
-
- // Check when one item
- hashmap.set ("1", "1");
- assert (!hashmap.is_empty);
-
- // Check when more items
- hashmap.set ("2", "2");
- assert (!hashmap.is_empty);
-
- // Check when items cleared
- hashmap.clear ();
- assert (hashmap.is_empty);
-}
-void test_hashmap_set_all () {
- var hashmap1 = new HashMap<string,string> (str_hash, str_equal, str_equal);
- var hashmap2 = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- hashmap1.set ("a", "1");
- hashmap1.set ("b", "2");
- hashmap1.set ("c", "3");
- hashmap2.set ("d", "4");
- hashmap2.set ("e", "5");
- hashmap2.set ("f", "6");
-
- hashmap1.set_all (hashmap2);
-
- assert (hashmap1.size == 6);
- assert (hashmap1.contains ("a"));
- assert (hashmap1.contains ("b"));
- assert (hashmap1.contains ("c"));
- assert (hashmap1.contains ("d"));
- assert (hashmap1.contains ("e"));
- assert (hashmap1.contains ("f"));
-
- assert (hashmap1.get ("a") == "1");
- assert (hashmap1.get ("b") == "2");
- assert (hashmap1.get ("c") == "3");
- assert (hashmap1.get ("d") == "4");
- assert (hashmap1.get ("e") == "5");
- assert (hashmap1.get ("f") == "6");
-}
-
-void test_hashmap_remove_all () {
- var map1 = new HashMap<string,string> (str_hash, str_equal, str_equal);
- var map2 = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check remove all on empty maps.
-
- assert (map1.is_empty);
- assert (map2.is_empty);
-
- map1.remove_all (map2);
-
- assert (map1.is_empty);
- assert (map2.is_empty);
-
- map1.clear ();
- map2.clear ();
-
- // Map1 is empty, map2 has entries. -> no change
-
- map2.set ("a", "1");
- map2.set ("b", "2");
-
- assert (map1.is_empty);
- assert (map2.size == 2);
-
- map1.remove_all (map2);
-
- assert (map1.is_empty);
- assert (map2.size == 2);
-
- map1.clear ();
- map2.clear ();
-
- // Map1 has entries, map2 is empty. -> no change
-
- map1.set ("a", "1");
- map1.set ("b", "2");
-
- assert (map1.size == 2);
- assert (map2.is_empty);
-
- map1.remove_all (map2);
-
- assert (map1.size == 2);
- assert (map2.is_empty);
-
- map1.clear ();
- map2.clear ();
-
- // Map1 and map2 have the same entries -> map1 is cleared
-
- map1.set ("a", "0");
- map1.set ("b", "1");
- map2.set ("a", "1");
- map2.set ("b", "0");
-
- assert (map1.size == 2);
- assert (map2.size == 2);
-
- map1.remove_all (map2);
-
- assert (map1.is_empty);
- assert (map2.size == 2);
-
- map1.clear ();
- map2.clear ();
-
- // Map1 has some common keys with map2 but both have also unique keys -> common key are cleared from map1
-
- map1.set ("x", "2");
- map1.set ("a", "1");
- map1.set ("b", "1");
- map1.set ("y", "2");
-
- map2.set ("i", "100");
- map2.set ("a", "200");
- map2.set ("j", "300");
- map2.set ("b", "400");
- map2.set ("k", "500");
-
- assert (map1.size == 4);
- assert (map2.size == 5);
-
- map1.remove_all (map2);
-
- assert (map1.size == 2);
- assert (map2.size == 5);
-
- assert (map1.contains ("x"));
- assert (map1.contains ("y"));
-
- map1.clear ();
- map2.clear ();
-
-}
-
-void test_hashmap_contains_all() {
- var map1 = new HashMap<string,string> (str_hash, str_equal, str_equal);
- var map2 = new HashMap<string,string> (str_hash, str_equal, str_equal);
-
- // Check empty.
- assert (map1.contains_all (map2));
-
- // Map1 has items, map2 is empty.
-
- map1.set ("1", "1");
-
- assert (map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-
- // Map1 is empty, map2 has items.
-
- map2.set ("1", "1");
-
- assert (!map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-
- // Map1 and map2 are the same.
-
- map1.set ("1", "a");
- map1.set ("2", "b");
-
- map2.set ("1", "c");
- map2.set ("2", "d");
-
- assert (map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-
- // Map1 and map2 are not the same
- map1.set ("1", "a");
- map2.set ("2", "b");
-
- assert (!map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-
- // Map1 has a subset of map2
- map1.set ("1", "a");
- map1.set ("2", "b");
- map1.set ("3", "c");
- map1.set ("4", "d");
- map1.set ("5", "e");
- map1.set ("6", "f");
-
- map2.set ("2", "g");
- map2.set ("4", "h");
- map2.set ("6", "i");
-
- assert (map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-
- // Map1 has a subset of map2 in all but one element map2
- map1.set ("1", "a");
- map1.set ("2", "b");
- map1.set ("3", "c");
- map1.set ("4", "d");
- map1.set ("5", "e");
- map1.set ("6", "f");
-
- map2.set ("2", "g");
- map2.set ("4", "h");
- map2.set ("6", "i");
- map2.set ("7", "j");
-
- assert (!map1.contains_all (map2));
-
- map1.clear ();
- map2.clear ();
-}
+ public override void tear_down () {
+ test_map = null;
+ }
-void main (string[] args) {
- Test.init (ref args);
+ public void test_selected_functions () {
+ var test_hash_map = test_map as HashMap<string,string>;
- // Methods of Map interface
- Test.add_func ("/HashMap/Map/get", test_hashmap_get);
- Test.add_func ("/HashMap/Map/set", test_hashmap_set);
- Test.add_func ("/HashMap/Map/remove", test_hashmap_remove);
- Test.add_func ("/HashMap/Map/contains", test_hashmap_contains);
- Test.add_func ("/HashMap/Map/size", test_hashmap_size);
- Test.add_func ("/HashMap/Map/get_keys", test_hashmap_get_keys);
- Test.add_func ("/HashMap/Map/get_values", test_hashmap_get_values);
- Test.add_func ("/HashMap/Map/clear", test_hashmap_clear);
- Test.add_func ("/HashMap/Map/empty", test_hashmap_empty);
- Test.add_func ("/HashMap/Map/set_all", test_hashmap_set_all);
- Test.add_func ("/HashMap/Map/remove_all", test_hashmap_remove_all);
- Test.add_func ("/HashMap/Map/contains_all", test_hashmap_contains_all);
+ // Check the map exists
+ assert (test_hash_map != null);
- Test.run ();
+ // Check the selected hash and equal functions
+ assert (test_hash_map.key_hash_func == str_hash);
+ assert (test_hash_map.key_equal_func == str_equal);
+ assert (test_hash_map.value_equal_func == str_equal);
+ }
}
-
diff --git a/tests/testmain.vala b/tests/testmain.vala
index 204a33d..6fb1c3a 100644
--- a/tests/testmain.vala
+++ b/tests/testmain.vala
@@ -25,11 +25,13 @@ void main (string[] args) {
Test.init (ref args);
TestSuite.get_root ().add_suite (new ArrayListTests ().get_suite ());
+ TestSuite.get_root ().add_suite (new HashMapTests ().get_suite ());
TestSuite.get_root ().add_suite (new HashMultiMapTests ().get_suite ());
TestSuite.get_root ().add_suite (new HashMultiSetTests ().get_suite ());
TestSuite.get_root ().add_suite (new LinkedListTests ().get_suite ());
TestSuite.get_root ().add_suite (new LinkedListAsDequeTests ().get_suite ());
TestSuite.get_root ().add_suite (new ReadOnlyListTests ().get_suite ());
+ TestSuite.get_root ().add_suite (new TreeMapTests ().get_suite ());
Test.run ();
}
diff --git a/tests/testmap.vala b/tests/testmap.vala
new file mode 100644
index 0000000..5447ce5
--- /dev/null
+++ b/tests/testmap.vala
@@ -0,0 +1,422 @@
+/* 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] contains, size and is_empty",
+ test_contains_size_is_empty);
+ add_test ("[Map] get keys", test_get_keys);
+ add_test ("[Map] get values", test_get_values);
+ add_test ("[Map] set all", test_set_all);
+ add_test ("[Map] remove all", test_remove_all);
+ add_test ("[Map] contains all", test_contains_all);
+ }
+
+ protected Map<string, string> test_map;
+
+ public void test_contains_size_is_empty () {
+ // Check the collection exists
+ assert (test_map != null);
+ string value;
+
+ // Check the collection is initially empty
+ assert (! test_map.contains ("one"));
+ assert (! test_map.contains ("two"));
+ assert (! test_map.contains ("three"));
+ assert (test_map.size == 0);
+ assert (test_map.is_empty);
+
+ // Add a binding
+ test_map.set ("one", "value_of_one");
+ assert (test_map.contains ("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (! test_map.contains ("two"));
+ assert (test_map.get ("two") == null);
+ assert (! test_map.contains ("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.remove ("one"));
+ assert (! test_map.contains ("one"));
+ assert (test_map.get ("one") == null);
+ assert (! test_map.contains ("two"));
+ assert (test_map.get ("two") == null);
+ assert (! test_map.contains ("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.contains ("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (! test_map.contains ("two"));
+ assert (test_map.get ("two") == null);
+ assert (! test_map.contains ("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.contains ("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (test_map.contains ("two"));
+ assert (test_map.get ("two") == "value_of_two");
+ assert (! test_map.contains ("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.contains ("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (test_map.contains ("two"));
+ assert (test_map.get ("two") == "value_of_two");
+ assert (test_map.contains ("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.contains ("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (test_map.contains ("two"));
+ assert (test_map.get ("two") == "value_of_two_new");
+ assert (test_map.contains ("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.remove ("two", out value));
+ assert (value == "value_of_two_new");
+ assert (test_map.contains("one"));
+ assert (test_map.get ("one") == "value_of_one");
+ assert (! test_map.contains("two"));
+ assert (test_map.get ("two") == null);
+ assert (test_map.contains("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.remove ("two", out value));
+ assert (value == null);
+ assert (test_map.contains("one"));
+ assert (! test_map.contains("two"));
+ assert (test_map.contains("three"));
+ assert (test_map.size == 2);
+ assert (! test_map.is_empty);
+
+ // Remove all elements
+ test_map.clear ();
+ assert (! test_map.contains("one"));
+ assert (test_map.get ("one") == null);
+ assert (! test_map.contains("two"));
+ assert (test_map.get ("two") == null);
+ assert (! test_map.contains("three"));
+ assert (test_map.get ("three") == null);
+ assert (test_map.size == 0);
+ assert (test_map.is_empty);
+ }
+
+ public void test_get_keys () {
+ // Check keys on empty map
+ var keySet = test_map.get_keys ();
+ assert (keySet.size == 0);
+
+ // Check keys on map with one item
+ test_map.set ("one", "value_of_one");
+ assert (keySet.size == 1);
+ assert (keySet.contains ("one"));
+ keySet = test_map.get_keys ();
+ assert (keySet.size == 1);
+ assert (keySet.contains ("one"));
+
+ // Check modify key set directly
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ assert (! keySet.add ("three"));
+ return;
+ }
+ 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 (keySet.size == 2);
+ assert (keySet.contains ("one"));
+ assert (keySet.contains ("two"));
+ keySet = test_map.get_keys ();
+ assert (keySet.size == 2);
+ assert (keySet.contains ("one"));
+ assert (keySet.contains ("two"));
+
+ // Check keys on map clear
+ test_map.clear ();
+ assert (keySet.size == 0);
+ keySet = test_map.get_keys ();
+ assert (keySet.size == 0);
+ }
+
+ public void test_get_values () {
+ // Check keys on empty map
+ var valueCollection = test_map.get_values ();
+ assert (valueCollection.size == 0);
+
+ // Check keys on map with one item
+ test_map.set ("one", "value_of_one");
+ assert (valueCollection.size == 1);
+ assert (valueCollection.contains ("value_of_one"));
+ valueCollection = test_map.get_values ();
+ assert (valueCollection.size == 1);
+ assert (valueCollection.contains ("value_of_one"));
+
+ // Check modify key set directly
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ assert (! valueCollection.add ("two"));
+ return;
+ }
+ 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 (valueCollection.size == 2);
+ assert (valueCollection.contains ("value_of_one"));
+ assert (valueCollection.contains ("value_of_two"));
+ valueCollection = test_map.get_values ();
+ assert (valueCollection.size == 2);
+ assert (valueCollection.contains ("value_of_one"));
+ assert (valueCollection.contains ("value_of_two"));
+
+ // Check keys on map clear
+ test_map.clear ();
+ assert (valueCollection.size == 0);
+ valueCollection = test_map.get_values ();
+ assert (valueCollection.size == 0);
+ }
+
+ 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.contains ("one"));
+ assert (test_map.contains ("two"));
+ assert (test_map.contains ("three"));
+ assert (test_map.contains ("four"));
+ assert (test_map.contains ("five"));
+ assert (test_map.contains ("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_remove_all () {
+ var another_map = new HashMap<string,string> (str_hash,
+ str_equal,
+ str_equal);
+
+ // Check remove all on empty maps.
+ assert (test_map.is_empty);
+ assert (another_map.is_empty);
+
+ assert (! test_map.remove_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.remove_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.remove_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.remove_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.remove_all (another_map));
+
+ assert (test_map.size == 1);
+ assert (another_map.size == 4);
+
+ assert (test_map.contains ("one"));
+ }
+
+ public void test_contains_all () {
+ var another_map = new HashMap<string,string> (str_hash,
+ str_equal,
+ str_equal);
+
+ // Check empty.
+ assert (test_map.contains_all (another_map));
+
+ // Test_Map has items, another_map is empty.
+ test_map.set ("one", "value_of_one");
+
+ assert (test_map.contains_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.contains_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", "another_value_of_one");
+ another_map.set ("two", "another_value_of_two");
+
+ assert (test_map.contains_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.contains_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.contains_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.contains_all (another_map));
+ }
+}
diff --git a/tests/testtreemap.vala b/tests/testtreemap.vala
index df32f72..92385f7 100644
--- a/tests/testtreemap.vala
+++ b/tests/testtreemap.vala
@@ -1,6 +1,7 @@
-/* testtreeset.vala
+/* testtreemap.vala
*
* Copyright (C) 2008 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
@@ -17,297 +18,82 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
- * Maciej Piechotka <uzytkownik2 gmail com>
+ * Julien Peeters <contact julienpeeters fr>
*/
-using GLib;
using Gee;
-const string CODE_NOT_REACHABLE = "*code should not be reached*";
+public class TreeMapTests : MapTests {
-void test_treemap_get () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // Check get from empty map
- assert (treemap.get ("foo") == null);
-
- // Check get from map with one items
- treemap.set ("key", "value");
- assert (treemap.get ("key") == "value");
-
- // Check get from non-existing key
- assert (treemap.get ("foo") == null);
-
- // Check get from map with multiple items
- treemap.set ("key2", "value2");
- treemap.set ("key3", "value3");
- assert (treemap.get ("key") == "value");
- assert (treemap.get ("key2") == "value2");
- assert (treemap.get ("key3") == "value3");
-}
-
-void test_treemap_set () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // check map is empty
- assert (treemap.size == 0);
-
- // check set an item to map
- treemap.set ("abc", "one");
- assert (treemap.contains ("abc"));
- assert (treemap.get ("abc") == "one");
- assert (treemap.size == 1);
-
- // check set an item to map with same value
- treemap.set ("def", "one");
- assert (treemap.contains ("def"));
- assert (treemap.get ("abc") == "one");
- assert (treemap.get ("def") == "one");
- assert (treemap.size == 2);
-
- // check set with same key
- treemap.set ("def", "two");
- assert (treemap.contains ("def"));
- assert (treemap.get ("abc") == "one");
- assert (treemap.get ("def") == "two");
- assert (treemap.size == 2);
-}
-
-void test_treemap_remove () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
- string? value;
-
- // check removing when map is empty
- treemap.remove ("foo");
- assert (treemap.size == 0);
-
- // add items
- treemap.set ("aaa", "111");
- treemap.set ("bbb", "222");
- treemap.set ("ccc", "333");
- treemap.set ("ddd", "444");
- assert (treemap.size == 4);
-
- // check remove on first place
- treemap.remove ("aaa");
- assert (treemap.size == 3);
-
- // check remove in between
- treemap.remove ("ccc", out value);
- assert (treemap.size == 2);
- assert (value == "333");
-
- // check remove in last place
- treemap.remove ("ddd");
- assert (treemap.size == 1);
-
- // check remove invalid key
- treemap.remove ("bar", out value);
- assert (value == null);
-
- // check remove last in map
- treemap.remove ("bbb");
- assert (treemap.size == 0);
-}
-
-void test_treemap_contains () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // Check on empty map
- assert (!treemap.contains ("111"));
-
- // Check items
- treemap.set ("10", "111");
- assert (treemap.contains ("10"));
- assert (!treemap.contains ("20"));
- assert (!treemap.contains ("30"));
-
- assert (treemap.get ("10") == "111");
-
- treemap.set ("20", "222");
- assert (treemap.contains ("10"));
- assert (treemap.contains ("20"));
- assert (!treemap.contains ("30"));
-
- assert (treemap.get ("10") == "111");
- assert (treemap.get ("20") == "222");
-
- treemap.set ("30", "333");
- assert (treemap.contains ("10"));
- assert (treemap.contains ("20"));
- assert (treemap.contains ("30"));
-
- assert (treemap.get ("10") == "111");
- assert (treemap.get ("20") == "222");
- assert (treemap.get ("30") == "333");
-
- // Clear and recheck
- treemap.clear ();
- assert (!treemap.contains ("10"));
- assert (!treemap.contains ("20"));
- assert (!treemap.contains ("30"));
-
- var treemapOfInt = new TreeMap<int,int> ();
-
- // Check items
- treemapOfInt.set (10, 111);
- assert (treemapOfInt.contains (10));
- assert (!treemapOfInt.contains (20));
- assert (!treemapOfInt.contains (30));
-
- assert (treemapOfInt.get (10) == 111);
-
- treemapOfInt.set (20, 222);
- assert (treemapOfInt.contains (10));
- assert (treemapOfInt.contains (20));
- assert (!treemapOfInt.contains (30));
-
- assert (treemapOfInt.get (10) == 111);
- assert (treemapOfInt.get (20) == 222);
-
- treemapOfInt.set (30, 333);
- assert (treemapOfInt.contains (10));
- assert (treemapOfInt.contains (20));
- assert (treemapOfInt.contains (30));
-
- assert (treemapOfInt.get (10) == 111);
- assert (treemapOfInt.get (20) == 222);
- assert (treemapOfInt.get (30) == 333);
-}
-
-void test_treemap_size () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // Check empty map
- assert (treemap.size == 0);
-
- // Check when one item
- treemap.set ("1", "1");
- assert (treemap.size == 1);
-
- // Check when more items
- treemap.set ("2", "2");
- assert (treemap.size == 2);
-
- // Check when items cleared
- treemap.clear ();
- assert (treemap.size == 0);
-}
-
-void test_treemap_get_keys () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // Check keys on empty map
- var keySet = treemap.get_keys ();
- assert (keySet.size == 0);
-
- // Check keys on map with one item
- treemap.set ("aaa", "111");
- assert (keySet.size == 1);
- assert (keySet.contains ("aaa"));
- keySet = treemap.get_keys ();
- assert (keySet.size == 1);
- assert (keySet.contains ("aaa"));
-
- // Check modify key set directly
- if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
- keySet.add ("ccc");
- return;
+ public TreeMapTests () {
+ base ("TreeMap");
+ add_test ("[TreeMap] selected functions", test_selected_functions);
+ add_test ("[TreeMap] key ordering", test_key_ordering);
}
- Test.trap_assert_failed ();
- Test.trap_assert_stderr (CODE_NOT_REACHABLE);
- // Check keys on map with multiple items
- treemap.set ("bbb", "222");
- assert (keySet.size == 2);
- assert (keySet.contains ("aaa"));
- assert (keySet.contains ("bbb"));
- keySet = treemap.get_keys ();
- assert (keySet.size == 2);
- assert (keySet.contains ("aaa"));
- assert (keySet.contains ("bbb"));
-
- // Check keys on map clear
- treemap.clear ();
- assert (keySet.size == 0);
- keySet = treemap.get_keys ();
- assert (keySet.size == 0);
-}
-
-void test_treemap_get_values () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
-
- // Check keys on empty map
- var valueCollection = treemap.get_values ();
- assert (valueCollection.size == 0);
-
- // Check keys on map with one item
- treemap.set ("aaa", "111");
- assert (valueCollection.size == 1);
- assert (valueCollection.contains ("111"));
- valueCollection = treemap.get_values ();
- assert (valueCollection.size == 1);
- assert (valueCollection.contains ("111"));
-
- // Check modify key set directly
- if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT | TestTrapFlags.SILENCE_STDERR)) {
- valueCollection.add ("ccc");
- return;
+ public override void set_up () {
+ test_map = new TreeMap<string,string> ();
}
- Test.trap_assert_failed ();
- Test.trap_assert_stderr (CODE_NOT_REACHABLE);
- // Check keys on map with multiple items
- treemap.set ("bbb", "222");
- assert (valueCollection.size == 2);
- assert (valueCollection.contains ("111"));
- assert (valueCollection.contains ("222"));
- valueCollection = treemap.get_values ();
- assert (valueCollection.size == 2);
- assert (valueCollection.contains ("111"));
- assert (valueCollection.contains ("222"));
-
- // Check keys on map clear
- treemap.clear ();
- assert (valueCollection.size == 0);
- valueCollection = treemap.get_values ();
- assert (valueCollection.size == 0);
-}
-
-void test_treemap_clear () {
- var treemap = new TreeMap<string,string> ((CompareFunc) strcmp, str_equal);
- assert (treemap.size == 0);
-
- // Check clear on empty map
- treemap.clear ();
- assert (treemap.size == 0);
+ public override void tear_down () {
+ test_map = null;
+ }
- // Check clear one item
- treemap.set ("1", "1");
- assert (treemap.size == 1);
- treemap.clear ();
- assert (treemap.size == 0);
+ public void test_selected_functions () {
+ var test_tree_map = test_map as TreeMap<string,string>;
- // Check clear multiple items
- treemap.set ("1", "1");
- treemap.set ("2", "2");
- treemap.set ("3", "3");
- assert (treemap.size == 3);
- treemap.clear ();
- assert (treemap.size == 0);
-}
+ // Check the map exists
+ assert (test_tree_map != null);
-void main (string[] args) {
- Test.init (ref args);
-
- Test.add_func ("/TreeMap/Map/get", test_treemap_get);
- Test.add_func ("/TreeMap/Map/set", test_treemap_set);
- Test.add_func ("/TreeMap/Map/remove", test_treemap_remove);
- Test.add_func ("/TreeMap/Map/contains", test_treemap_contains);
- Test.add_func ("/TreeMap/Map/size", test_treemap_size);
- Test.add_func ("/TreeMap/Map/get_keys", test_treemap_get_keys);
- Test.add_func ("/TreeMap/Map/get_values", test_treemap_get_values);
- Test.add_func ("/TreeMap/Map/clear", test_treemap_clear);
+ // Check the selected compare and equal functions
+ assert (test_tree_map.key_compare_func == strcmp);
+ assert (test_tree_map.value_equal_func == str_equal);
+ }
- Test.run ();
+ public void test_key_ordering () {
+ var test_tree_map = test_map as TreeMap<string,string>;
+
+ // Check the map exists
+ assert (test_tree_map != null);
+
+ test_tree_map.set ("one", "one");
+ test_tree_map.set ("two", "two");
+ test_tree_map.set ("three", "three");
+ test_tree_map.set ("four", "four");
+ test_tree_map.set ("five", "five");
+ test_tree_map.set ("six", "six");
+ test_tree_map.set ("seven", "seven");
+ test_tree_map.set ("eight", "eight");
+ test_tree_map.set ("nine", "nine");
+ test_tree_map.set ("ten", "ten");
+ test_tree_map.set ("eleven", "eleven");
+ test_tree_map.set ("twelve", "twelve");
+
+ Iterator<string> iterator = test_tree_map.get_keys ().iterator ();
+ assert (iterator.next () == true);
+ assert (iterator.get () == "eight");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "eleven");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "five");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "four");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "nine");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "one");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "seven");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "six");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "ten");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "three");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "twelve");
+ assert (iterator.next () == true);
+ assert (iterator.get () == "two");
+ assert (iterator.next () == false);
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]