[libgee] Refactor the Set implementations' tests



commit 13dfc6c8897aaa4388b29d805aa9d84d80f0f618
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Wed Sep 16 01:46:58 2009 +0200

    Refactor the Set implementations' tests
    
    Fixes bug 594241.

 tests/Makefile.am         |   21 +--
 tests/testcollection.vala |   47 +---
 tests/testhashset.vala    |  667 +++------------------------------------------
 tests/testmain.vala       |    2 +
 tests/testset.vala        |   58 ++++
 tests/testtreeset.vala    |  353 ++++++------------------
 6 files changed, 191 insertions(+), 957 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 48e98c6..8450a19 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -22,6 +22,7 @@ tests_VALASOURCES = \
        testhashmap.vala \
        testhashmultimap.vala \
        testhashmultiset.vala \
+       testhashset.vala \
        testlinkedlist.vala \
        testlinkedlistasdeque.vala \
        testlist.vala \
@@ -33,7 +34,9 @@ tests_VALASOURCES = \
        testqueue.vala \
        testreadonlycollection.vala \
        testreadonlylist.vala \
+       testset.vala \
        testtreemap.vala \
+       testtreeset.vala \
        $(NULL)
 
 tests_SOURCES = tests.vala.stamp $(tests_VALASOURCES:.vala=.c)
@@ -69,21 +72,3 @@ clean-local:
 	-rm -rf coverage
 
 .PHONY: coverage-report
-
-TEST_PROGS += testhashset
-testhashset_VALASOURCES = testhashset.vala
-testhashset_SOURCES = testhashset.c
-$(testhashset_SOURCES): $(testhashset_VALASOURCES)
-	$(VALAC) -C --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^
-	touch $@
-testhashset_LDADD = $(progs_ldadd)
-EXTRA_DIST += $(testhashset_VALASOURCES)
-
-TEST_PROGS += testtreeset
-testtreeset_VALASOURCES = testtreeset.vala
-testtreeset_SOURCES = testtreeset.c testtreeset.h
-$(testtreeset_SOURCES): $(testtreeset_VALASOURCES)
-	$(VALAC) -C --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^
-	touch $@
-testtreeset_LDADD = $(progs_ldadd)
-EXTRA_DIST += $(testtreeset_VALASOURCES)
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index 7f26259..ebb88bc 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -407,7 +407,9 @@ public abstract class CollectionTests : Gee.TestCase {
 
 		assert (test_collection.add_all (dummy));
 
-		assert (test_collection.size == 4);
+		// We can only assert the result is greater or equal than 3
+		// as we do not assume duplicates
+		assert (test_collection.size >= 3);
 		assert (test_collection.contains ("hello1"));
 		assert (test_collection.contains ("hello2"));
 		assert (test_collection.contains ("hello3"));
@@ -508,20 +510,6 @@ public abstract class CollectionTests : Gee.TestCase {
 
 		test_collection.clear ();
 		dummy.clear ();
-
-		// Check removing the same element
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-
-		assert (dummy.add ("hello1"));
-		assert (dummy.add ("hello1"));
-
-		assert (test_collection.remove_all (dummy));
-
-		assert (test_collection.size == 2);
-		assert (dummy.size == 2);
 	}
 
 	public void test_retain_all () {
@@ -630,35 +618,6 @@ public abstract class CollectionTests : Gee.TestCase {
 
 		test_collection.clear ();
 		dummy.clear ();
-
-		// Check when the test collection contains the same elements several
-		// times and the dummy collection contains the element too
-		assert (dummy.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-
-		assert (! test_collection.retain_all (dummy));
-
-		assert (test_collection.size == 4);
-		assert (dummy.size == 1);
-
-		test_collection.clear ();
-		dummy.clear ();
-
-		// Check when the test collection contains the same elements several
-		// times and the dummy collection contains the element too
-		assert (dummy.add ("hello2"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-		assert (test_collection.add ("hello1"));
-
-		assert (test_collection.retain_all (dummy));
-
-		assert (test_collection.is_empty);
-		assert (dummy.size == 1);
 	}
 
 	public void test_to_array() {
diff --git a/tests/testhashset.vala b/tests/testhashset.vala
index 352b17b..c724c58 100644
--- a/tests/testhashset.vala
+++ b/tests/testhashset.vala
@@ -1,6 +1,6 @@
 /* testhashset.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,649 +17,54 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  *
  * Author:
- * 	Jürg Billeter <j bitron ch>
+ * 	Didier 'Ptitjes' Villevalois <ptitjes free fr>
+ * 	Julien Peeters <contact julienpeeters fr>
  */
 
-using GLib;
 using Gee;
 
-void test_hashset_add () {
-	// Check adding of strings
-	var hashset = new HashSet<string> (str_hash, str_equal);
+public class HashSetTests : SetTests {
 
-	hashset.add ("42");
-	assert (hashset.contains ("42"));
-	assert (hashset.size == 1);
-
-	hashset.add ("43");
-	assert (hashset.contains ("42"));
-	assert (hashset.contains ("43"));
-	assert (hashset.size == 2);
-
-	// Check add same element
-	assert (hashset.size == 2);
-	hashset.add ("43");
-	assert (hashset.contains ("42"));
-	assert (hashset.contains ("43"));
-	assert (hashset.size == 2);
-
-	// Check adding of ints
-	var hashsetInt = new HashSet<int> ();
-
-	hashsetInt.add (42);
-	assert (hashsetInt.contains (42));
-	assert (hashsetInt.size == 1);
-
-	hashsetInt.add (43);
-	assert (hashsetInt.contains (42));
-	assert (hashsetInt.contains (43));
-	assert (hashsetInt.size == 2);
-
-	// Check add same element
-	assert (hashsetInt.size == 2);
-	hashsetInt.add (43);
-	assert (hashsetInt.contains (42));
-	assert (hashsetInt.contains (43));
-	assert (hashsetInt.size == 2);
-
-	// Check adding of objects
-	var hashsetObject = new HashSet<Object> ();
-
-	var fooObject = new Object();
-	hashsetObject.add (fooObject);
-	assert (hashsetObject.contains (fooObject));
-	assert (hashsetObject.size == 1);
-
-	var fooObject2 = new Object();
-	hashsetObject.add (fooObject2);
-	assert (hashsetObject.contains (fooObject));
-	assert (hashsetObject.contains (fooObject2));
-	assert (hashsetObject.size == 2);
-
-	// Check add same element
-	assert (hashsetObject.size == 2);
-	hashsetObject.add (fooObject2);
-	assert (hashsetObject.contains (fooObject));
-	assert (hashsetObject.contains (fooObject2));
-	assert (hashsetObject.size == 2);
-}
-
-void test_hashset_clear () {
-	var hashset = new HashSet<string> (str_hash, str_equal);
-	assert (hashset.size == 0);
-
-	// Check clear on empty set
-	hashset.clear ();
-	assert (hashset.size == 0);
-
-	// Check clear one item
-	hashset.add ("1");
-	assert (hashset.size == 1);
-	hashset.clear ();
-	assert (hashset.size == 0);
-
-	// Check clear multiple items
-	hashset.add ("1");
-	hashset.add ("2");
-	hashset.add ("3");
-	assert (hashset.size == 3);
-	hashset.clear ();
-	assert (hashset.size == 0);
-}
-
-void test_hashset_contains () {
-	var hashsetString = new HashSet<string> (str_hash, str_equal);
-
-	// Check on empty set
-	assert (!hashsetString.contains ("1"));
-
-	// Check items
-	hashsetString.add ("10");
-	assert (hashsetString.contains ("10"));
-	assert (!hashsetString.contains ("20"));
-	assert (!hashsetString.contains ("30"));
-
-	hashsetString.add ("20");
-	assert (hashsetString.contains ("10"));
-	assert (hashsetString.contains ("20"));
-	assert (!hashsetString.contains ("30"));
-
-	hashsetString.add ("30");
-	assert (hashsetString.contains ("10"));
-	assert (hashsetString.contains ("20"));
-	assert (hashsetString.contains ("30"));
-
-	// Clear and recheck
-	hashsetString.clear ();
-	assert (!hashsetString.contains ("10"));
-	assert (!hashsetString.contains ("20"));
-	assert (!hashsetString.contains ("30"));
-
-	var hashsetInt = new HashSet<int> ();
-
-	// Check items
-	hashsetInt.add (10);
-	assert (hashsetInt.contains (10));
-	assert (!hashsetInt.contains (20));
-	assert (!hashsetInt.contains (30));
-
-	hashsetInt.add (20);
-	assert (hashsetInt.contains (10));
-	assert (hashsetInt.contains (20));
-	assert (!hashsetInt.contains (30));
-
-	hashsetInt.add (30);
-	assert (hashsetInt.contains (10));
-	assert (hashsetInt.contains (20));
-	assert (hashsetInt.contains (30));
-
-	// Clear and recheck
-	hashsetInt.clear ();
-	assert (!hashsetInt.contains (10));
-	assert (!hashsetInt.contains (20));
-	assert (!hashsetInt.contains (30));
-}
-
-void test_hashset_remove () {
-	var hashsetString = new HashSet<string> (str_hash, str_equal);
-
-	// Check remove if list is empty
-	hashsetString.remove ("42");
-
-	// Add 5 different elements
-	hashsetString.add ("42");
-	hashsetString.add ("43");
-	hashsetString.add ("44");
-	hashsetString.add ("45");
-	hashsetString.add ("46");
-	assert (hashsetString.size == 5);
-
-	// Check remove first
-	hashsetString.remove ("42");
-	assert (hashsetString.size == 4);
-	assert (hashsetString.contains ("43"));
-	assert (hashsetString.contains ("44"));
-	assert (hashsetString.contains ("45"));
-	assert (hashsetString.contains ("46"));
-
-	// Check remove last
-	hashsetString.remove ("46");
-	assert (hashsetString.size == 3);
-	assert (hashsetString.contains ("43"));
-	assert (hashsetString.contains ("44"));
-	assert (hashsetString.contains ("45"));
-
-	// Check remove in between
-	hashsetString.remove ("44");
-	assert (hashsetString.size == 2);
-	assert (hashsetString.contains ("43"));
-	assert (hashsetString.contains ("45"));
-
-	// Check removing of int element
-	var hashsetInt = new HashSet<int> ();
-
-	// Add 5 different elements
-	hashsetInt.add (42);
-	hashsetInt.add (43);
-	hashsetInt.add (44);
-	hashsetInt.add (45);
-	hashsetInt.add (46);
-	assert (hashsetInt.size == 5);
-
-	// Remove first
-	hashsetInt.remove (42);
-	assert (hashsetInt.size == 4);
-	assert (hashsetInt.contains (43));
-	assert (hashsetInt.contains (44));
-	assert (hashsetInt.contains (45));
-	assert (hashsetInt.contains (46));
-
-	// Remove last
-	hashsetInt.remove (46);
-	assert (hashsetInt.size == 3);
-	assert (hashsetInt.contains (43));
-	assert (hashsetInt.contains (44));
-	assert (hashsetInt.contains (45));
-
-	// Remove in between
-	hashsetInt.remove (44);
-	assert (hashsetInt.size == 2);
-	assert (hashsetInt.contains (43));
-	assert (hashsetInt.contains (45));
-}
-
-void test_hashset_size () {
-	var hashset = new HashSet<string> (str_hash, str_equal);
-
-	// Check empty list
-	assert (hashset.size == 0);
-
-	// Check when one item
-	hashset.add ("1");
-	assert (hashset.size == 1);
-
-	// Check when more items
-	hashset.add ("2");
-	assert (hashset.size == 2);
-
-	// Check when items cleared
-	hashset.clear ();
-	assert (hashset.size == 0);
-}
-
-void test_hashset_iterator () {
-	var hashset = new HashSet<string> (str_hash, str_equal);
-
-	// Check iterate empty list
-	var iterator = hashset.iterator ();
-	assert (!iterator.next());
-
-	// Check iterate list
-	hashset.add ("42");
-	hashset.add ("43");
-
-	iterator = hashset.iterator ();
-
-	// A set is usually not ordered, so this is not a requirement 
-	assert (iterator.next());
-	string firstString = iterator.get();
-	assert (hashset.contains (firstString)); 
-
-	assert (iterator.next());
-	string secondString = iterator.get();
-	assert (hashset.contains (secondString));
-	assert (!str_equal (firstString, secondString)); // they can not be identical neither equal
-
-	assert (!iterator.next());
-}
-
-void test_hashset_empty () {
-	var hashset = new HashSet<string> (str_hash, str_equal);
-
-	// Case 1: Check empty set
-	assert (hashset.is_empty);
-
-	// Case 2: Check when one item
-	hashset.add ("1");
-	assert (!hashset.is_empty);
-
-	// Case 3: Check when more items
-	hashset.add ("2");
-	assert (!hashset.is_empty);
-
-	// Case 4: Check when items cleared
-	hashset.clear ();
-	assert (hashset.is_empty);
-}
-
-void test_hashset_add_all () {
-	var hashset1 = new HashSet<string> (str_hash, str_equal);
-	var hashset2 = new HashSet<string> (str_hash, str_equal);
-
-	// Case 1: Check set empty
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.is_empty);
-	assert (hashset2.is_empty);
-
-	// Case 2: Check hashset1 not empty, hashset2 is empty
-	hashset1.add ("1");
-
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.size == 1);
-	assert (hashset1.contains ("1"));
-	assert (hashset2.is_empty);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 3: Check hashset1 empty, hashset2 contains 1 element
-	hashset2.add ("1");
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.size == 1);
-	assert (hashset1.contains ("1"));
-	assert (hashset2.size == 1);
-	assert (hashset2.contains ("1"));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 4: Check more elements
-	hashset1.add ("0");
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset2.add ("3");
-	hashset2.add ("4");
-	hashset2.add ("5");
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.size == 6);
-	assert (hashset1.contains ("0"));
-	assert (hashset1.contains ("1"));
-	assert (hashset1.contains ("2"));
-	assert (hashset1.contains ("3"));
-	assert (hashset1.contains ("4"));
-	assert (hashset1.contains ("5"));
-
-	assert (hashset2.size == 3);
-	assert (hashset2.contains ("3"));
-	assert (hashset2.contains ("4"));
-	assert (hashset2.contains ("5"));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 5: Check intersecting elements
-	hashset1.add ("0");
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset1.add ("3");
-	hashset2.add ("2");
-	hashset2.add ("3");
-	hashset2.add ("4");
-	hashset2.add ("5");
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.size == 6);
-	assert (hashset1.contains ("0"));
-	assert (hashset1.contains ("1"));
-	assert (hashset1.contains ("2"));
-	assert (hashset1.contains ("3"));
-	assert (hashset1.contains ("4"));
-	assert (hashset1.contains ("5"));
-
-	assert (hashset2.size == 4);
-	assert (hashset2.contains( "2"));
-	assert (hashset2.contains ("3"));
-	assert (hashset2.contains ("4"));
-	assert (hashset2.contains ("5"));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 6: Add large collections
-	hashset1.add ("0");
-	hashset1.add ("1");
-	hashset1.add ("2");
-
-	for(int i = 3; i < 103; i++) {
-		hashset2.add ("%d".printf (i));
+	public HashSetTests () {
+		base ("HashSet");
+		add_test ("[HashSet] selected functions", test_selected_functions);
+		add_test ("[HashSet] GObject properties", test_gobject_properties);
 	}
 
-	hashset1.add_all (hashset2);
-
-	assert (hashset1.size == 103);
-	assert (hashset1.contains ("0"));
-	assert (hashset1.contains ("1"));
-	assert (hashset1.contains ("2"));
-	assert (hashset1.contains ("3"));
-	assert (hashset1.contains ("4"));
-	assert (hashset1.contains ("5"));
-	assert (hashset1.contains ("99"));
-	assert (hashset1.contains ("100"));
-	assert (hashset1.contains ("101"));
-	assert (hashset1.contains ("102"));
-
-	assert (hashset2.size == 100);
-
-	hashset1.clear ();
-	hashset2.clear ();
-}
-
-void test_hashset_contains_all () {
-	var hashset1 = new HashSet<string> (str_hash, str_equal);
-	var hashset2 = new HashSet<string> (str_hash, str_equal);
-
-	// Case 1: Check empty
-	assert (hashset1.contains_all (hashset2));
-
-	// Case 2: Hashset1 has elements, hashset2 is empty
-	hashset1.add ("1");
-
-	assert (hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 3: Hashset1 is empty, hashset2 has elements
-	hashset2.add ("1");
-
-	assert (!hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 4: Hashset1 and hashset2 are the same
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset2.add ("1");
-	hashset1.add ("2");
-
-	assert (hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 5: Hashset1 and hashset2 are not the same
-	hashset1.add ("1");
-	hashset2.add ("2");
-
-	assert (!hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 6: Hashset1 has a subset of hashset2
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset1.add ("3");
-	hashset1.add ("4");
-	hashset1.add ("5");
-	hashset1.add ("6");
-
-	hashset2.add ("2");
-	hashset2.add ("4");
-	hashset2.add ("6");
-
-	assert (hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 7: Hashset1 has a subset of hashset2 in all but one element hashset2
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset1.add ("3");
-	hashset1.add ("4");
-	hashset1.add ("5");
-	hashset1.add ("6");
-
-	hashset2.add ("2");
-	hashset2.add ("4");
-	hashset2.add ("6");
-	hashset2.add ("7");
-
-	assert (!hashset1.contains_all (hashset2));
-
-	hashset1.clear ();
-	hashset2.clear ();
-}
-
-void test_hashset_remove_all () {
-	var hashset1 = new HashSet<string> (str_hash, str_equal);
-	var hashset2 = new HashSet<string> (str_hash, str_equal);
-
-	// Case 1: Check empty
-	hashset1.remove_all (hashset2);
-	assert (hashset1.is_empty);
-	assert (hashset2.is_empty);
-
-	// Case 2: Hashset1 and hashset2 have no common elements -> nothing is removed in hashset1
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset1.add ("3");
-	hashset2.add ("4");
-	hashset2.add ("5");
-	hashset2.add ("6");
-
-	hashset1.remove_all (hashset2);
-
-	assert (hashset1.size == 3);
-	assert (hashset1.size == 3);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 3: Hashset1 and hashset2 have all elements the same -> everything is removed in hashset1 but not hashset2
-	hashset1.add ("1");
-	hashset1.add ("2");
-	hashset1.add ("3");
-	hashset2.add ("1");
-	hashset2.add ("2");
-	hashset2.add ("3");
-
-	hashset1.remove_all (hashset2);
-
-	assert (hashset1.is_empty);
-	assert (hashset2.size == 3);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-}
-
-void test_hashset_retain_all () {
-	var hashset1 = new HashSet<int> ();
-	var hashset2 = new HashSet<int> ();
-
-	// Case 1: Check empty
-
-	assert (hashset1.is_empty);
-	assert (hashset2.is_empty);
-
-	hashset1.retain_all (hashset2);
-
-	assert (hashset1.is_empty);
-	assert (hashset2.is_empty);
-
-
-	// Case 2: Hashset1 has elements, hashset2 is empty -> everything in hashset1 is removed
-	hashset1.add (1);
-	hashset1.add (2);
-	hashset1.add (3);
-
-	assert (hashset1.size == 3);
-	assert (hashset2.size == 0);
-
-	hashset1.retain_all (hashset2);
-
-	assert (hashset1.size == 0);
-	assert (hashset2.size == 0);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 3: Hashset1 is empty and hashset2 has elements -> nothing changes
-	hashset2.add (4);
-	hashset2.add (5);
-	hashset2.add (6);
-
-	assert (hashset1.size == 0);
-	assert (hashset2.size == 3);
-
-	hashset1.retain_all (hashset2);
-
-	assert (hashset1.size == 0);
-	assert (hashset2.size == 3);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 4: Hashset1 and hashset2 have no common elements -> everything is removed in hashset1
-	hashset1.add (1);
-	hashset1.add (2);
-	hashset1.add (3);
-	hashset2.add (4);
-	hashset2.add (5);
-	hashset2.add (6);
-
-	assert (hashset1.size == 3);
-	assert (hashset2.size == 3);
-
-	hashset1.retain_all (hashset2);
-
-	assert (hashset1.size == 0);
-	assert (hashset2.size == 3);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 5: Hashset1 and hashset2 have all elements the same -> nothing is removed in hashset1
-	hashset1.add (1);
-	hashset1.add (2);
-	hashset1.add (3);
-	hashset2.add (1);
-	hashset2.add (2);
-	hashset2.add (3);
-
-	assert (hashset1.size == 3);
-	assert (hashset2.size == 3);
-
-	hashset1.retain_all (hashset2);
-
-	assert (hashset1.size == 3);
-	assert (hashset2.size == 3);
-
-	hashset1.clear ();
-	hashset2.clear ();
-
-	// Case 6: Hashset1 and hashset2 have 2 common elements but each also has his own elements -> hashset1 only retains what is in hashset2
-
-	hashset1.add (1);
-	hashset1.add (2);
-	hashset1.add (3);
-	hashset1.add (4);
-	hashset1.add (5);
-
-	hashset2.add (0);
-	hashset2.add (2);
-	hashset2.add (3);
-	hashset2.add (7);
-
-	assert (hashset1.size == 5);
-	assert (hashset2.size == 4);
+	public override void set_up () {
+		test_collection = new HashSet<string> ();
+	}
 
-	hashset1.retain_all (hashset2);
+	public override void tear_down () {
+		test_collection = null;
+	}
 
-	assert (hashset1.size == 2);
-	assert (hashset2.size == 4);
+	public void test_selected_functions () {
+		var test_set = test_collection as HashSet<string>;
 
-	assert (hashset1.contains (2));
-	assert (hashset2.contains (3));
+		// Check the map exists
+		assert (test_set != null);
 
-	hashset1.clear ();
-	hashset2.clear ();
-}
+		// Check the selected hash and equal functions
+		assert (test_set.hash_func == str_hash);
+		assert (test_set.equal_func == str_equal);
+	}
 
-void main (string[] args) {
-	Test.init (ref args);
+	public new void test_gobject_properties() {
+		var test_set = test_collection as HashSet<string>;
 
-	// Methods of Collection interface
-	Test.add_func ("/HashSet/Collection/add", test_hashset_add);
-	Test.add_func ("/HashSet/Collection/clear", test_hashset_clear);
-	Test.add_func ("/HashSet/Collection/contains", test_hashset_contains);
-	Test.add_func ("/HashSet/Collection/remove", test_hashset_remove);
-	Test.add_func ("/HashSet/Collection/size", test_hashset_size);
-	Test.add_func ("/HashSet/Collection/empty", test_hashset_empty);
-	Test.add_func ("/HashSet/Collection/add_all", test_hashset_add_all);
-	Test.add_func ("/HashSet/Collection/contains_all", test_hashset_contains_all);
-	Test.add_func ("/HashSet/Collection/remove_all", test_hashset_remove_all);
-	Test.add_func ("/HashSet/Collection/retain_all", test_hashset_retain_all);
+		// Check the list exists
+		assert (test_set != null);
+		Value value;
 
-	Test.add_func ("/HashSet/Iterable/iterator", test_hashset_iterator);
+		value = Value (typeof (HashFunc));
+		test_set.get_property ("hash-func", ref value);
+		assert (value.get_pointer () == (void*) test_set.hash_func);
+		value.unset ();
 
-	Test.run ();
+		value = Value (typeof (EqualFunc));
+		test_set.get_property ("equal-func", ref value);
+		assert (value.get_pointer () == (void*) test_set.equal_func);
+		value.unset ();
+	}
 }
-
diff --git a/tests/testmain.vala b/tests/testmain.vala
index ddb6a7f..525d3cf 100644
--- a/tests/testmain.vala
+++ b/tests/testmain.vala
@@ -28,11 +28,13 @@ void main (string[] args) {
 	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 HashSetTests ().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 PriorityQueueTests ().get_suite ());
 	TestSuite.get_root ().add_suite (new ReadOnlyListTests ().get_suite ());
 	TestSuite.get_root ().add_suite (new TreeMapTests ().get_suite ());
+	TestSuite.get_root ().add_suite (new TreeSetTests ().get_suite ());
 
 	Test.run ();
 }
diff --git a/tests/testset.vala b/tests/testset.vala
new file mode 100644
index 0000000..e2fc83f
--- /dev/null
+++ b/tests/testset.vala
@@ -0,0 +1,58 @@
+/* testset.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
+ * 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>
+ * 	Didier 'Ptitjes' Villevalois <ptitjes free fr>
+ * 	Julien Peeters <contact julienpeeters fr>
+ */
+
+using GLib;
+using Gee;
+
+public abstract class SetTests : CollectionTests {
+
+	public SetTests (string name) {
+		base (name);
+		add_test ("[Set] duplicates are ignored", test_duplicates_are_ignored);
+	}
+
+	public virtual void test_duplicates_are_ignored () {
+		var test_set = test_collection as Set<string>;
+
+		// Check the test list is not null
+		assert (test_set != null);
+
+		assert (test_set.add ("one"));
+		assert (test_set.contains ("one"));
+		assert (test_set.size == 1);
+
+		assert (! test_set.add ("one"));
+		assert (test_set.contains ("one"));
+		assert (test_set.size == 1);
+
+		assert (test_set.remove ("one"));
+		assert (! test_set.contains ("one"));
+		assert (test_set.size == 0);
+
+		assert (! test_set.remove ("one"));
+		assert (! test_set.contains ("one"));
+		assert (test_set.size == 0);
+	}
+}
diff --git a/tests/testtreeset.vala b/tests/testtreeset.vala
index a3d6385..04bebc4 100644
--- a/tests/testtreeset.vala
+++ b/tests/testtreeset.vala
@@ -1,6 +1,6 @@
 /* testtreeset.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,271 +17,96 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  *
  * Author:
- * 	Maciej Piechotka <uzytkownik2 gmail com>
+ * 	Didier 'Ptitjes' Villevalois <ptitjes free fr>
+ * 	Julien Peeters <contact julienpeeters fr>
  */
 
-using GLib;
 using Gee;
 
-void test_treeset_add () {
-	// Check adding of strings
-	var treeset = new TreeSet<string> ((CompareFunc) strcmp);
-
-	treeset.add ("42");
-	assert (treeset.contains ("42"));
-	assert (treeset.size == 1);
-
-	treeset.add ("43");
-	assert (treeset.contains ("42"));
-	assert (treeset.contains ("43"));
-	assert (treeset.size == 2);
-
-	// Check add same element
-	assert (treeset.size == 2);
-	treeset.add ("43");
-	assert (treeset.contains ("42"));
-	assert (treeset.contains ("43"));
-	assert (treeset.size == 2);
-
-	// Check adding of ints
-	var treesetInt = new TreeSet<int> ();
-
-	treesetInt.add (42);
-	assert (treesetInt.contains (42));
-	assert (treesetInt.size == 1);
-
-	treesetInt.add (43);
-	assert (treesetInt.contains (42));
-	assert (treesetInt.contains (43));
-	assert (treesetInt.size == 2);
-
-	// Check add same element
-	assert (treesetInt.size == 2);
-	treesetInt.add (43);
-	assert (treesetInt.contains (42));
-	assert (treesetInt.contains (43));
-	assert (treesetInt.size == 2);
-
-	// Check adding of objects
-	var treesetObject = new TreeSet<Object> ();
-
-	var fooObject = new Object();
-	treesetObject.add (fooObject);
-	assert (treesetObject.contains (fooObject));
-	assert (treesetObject.size == 1);
-
-	var fooObject2 = new Object();
-	treesetObject.add (fooObject2);
-	assert (treesetObject.contains (fooObject));
-	assert (treesetObject.contains (fooObject2));
-	assert (treesetObject.size == 2);
-
-	// Check add same element
-	assert (treesetObject.size == 2);
-	treesetObject.add (fooObject2);
-	assert (treesetObject.contains (fooObject));
-	assert (treesetObject.contains (fooObject2));
-	assert (treesetObject.size == 2);
-}
-
-void test_treeset_clear () {
-	var treeset = new TreeSet<string> ((CompareFunc) strcmp);
-	assert (treeset.size == 0);
-
-	// Check clear on empty set
-	treeset.clear ();
-	assert (treeset.size == 0);
-
-	// Check clear one item
-	treeset.add ("1");
-	assert (treeset.size == 1);
-	treeset.clear ();
-	assert (treeset.size == 0);
-
-	// Check clear multiple items
-	treeset.add ("1");
-	treeset.add ("2");
-	treeset.add ("3");
-	assert (treeset.size == 3);
-	treeset.clear ();
-	assert (treeset.size == 0);
-}
-
-void test_treeset_contains () {
-	var treesetString = new TreeSet<string> ((CompareFunc) strcmp);
-
-	// Check on empty set
-	assert (!treesetString.contains ("1"));
-
-	// Check items
-	treesetString.add ("10");
-	assert (treesetString.contains ("10"));
-	assert (!treesetString.contains ("20"));
-	assert (!treesetString.contains ("30"));
-
-	treesetString.add ("20");
-	assert (treesetString.contains ("10"));
-	assert (treesetString.contains ("20"));
-	assert (!treesetString.contains ("30"));
-
-	treesetString.add ("30");
-	assert (treesetString.contains ("10"));
-	assert (treesetString.contains ("20"));
-	assert (treesetString.contains ("30"));
-
-	// Clear and recheck
-	treesetString.clear ();
-	assert (!treesetString.contains ("10"));
-	assert (!treesetString.contains ("20"));
-	assert (!treesetString.contains ("30"));
-
-	var treesetInt = new TreeSet<int> ();
-
-	// Check items
-	treesetInt.add (10);
-	assert (treesetInt.contains (10));
-	assert (!treesetInt.contains (20));
-	assert (!treesetInt.contains (30));
-
-	treesetInt.add (20);
-	assert (treesetInt.contains (10));
-	assert (treesetInt.contains (20));
-	assert (!treesetInt.contains (30));
-
-	treesetInt.add (30);
-	assert (treesetInt.contains (10));
-	assert (treesetInt.contains (20));
-	assert (treesetInt.contains (30));
-
-	// Clear and recheck
-	treesetInt.clear ();
-	assert (!treesetInt.contains (10));
-	assert (!treesetInt.contains (20));
-	assert (!treesetInt.contains (30));
-}
-
-void test_treeset_remove () {
-	var treesetString = new TreeSet<string> ((CompareFunc) strcmp);
-
-	// Check remove if list is empty
-	treesetString.remove ("42");
-
-	// Add 5 different elements
-	treesetString.add ("42");
-	treesetString.add ("43");
-	treesetString.add ("44");
-	treesetString.add ("45");
-	treesetString.add ("46");
-	assert (treesetString.size == 5);
-
-	// Check remove first
-	treesetString.remove ("42");
-	assert (treesetString.size == 4);
-	assert (treesetString.contains ("43"));
-	assert (treesetString.contains ("44"));
-	assert (treesetString.contains ("45"));
-	assert (treesetString.contains ("46"));
-
-	// Check remove last
-	treesetString.remove ("46");
-	assert (treesetString.size == 3);
-	assert (treesetString.contains ("43"));
-	assert (treesetString.contains ("44"));
-	assert (treesetString.contains ("45"));
-
-	// Check remove in between
-	treesetString.remove ("44");
-	assert (treesetString.size == 2);
-	assert (treesetString.contains ("43"));
-	assert (treesetString.contains ("45"));
-
-	// Check removing of int element
-	var treesetInt = new TreeSet<int> ();
-
-	// Add 5 different elements
-	treesetInt.add (42);
-	treesetInt.add (43);
-	treesetInt.add (44);
-	treesetInt.add (45);
-	treesetInt.add (46);
-	assert (treesetInt.size == 5);
-
-	// Remove first
-	treesetInt.remove (42);
-	assert (treesetInt.size == 4);
-	assert (treesetInt.contains (43));
-	assert (treesetInt.contains (44));
-	assert (treesetInt.contains (45));
-	assert (treesetInt.contains (46));
-
-	// Remove last
-	treesetInt.remove (46);
-	assert (treesetInt.size == 3);
-	assert (treesetInt.contains (43));
-	assert (treesetInt.contains (44));
-	assert (treesetInt.contains (45));
-
-	// Remove in between
-	treesetInt.remove (44);
-	assert (treesetInt.size == 2);
-	assert (treesetInt.contains (43));
-	assert (treesetInt.contains (45));
-}
-
-void test_treeset_size () {
-	var treeset = new TreeSet<string> ((CompareFunc) strcmp);
-
-	// Check empty list
-	assert (treeset.size == 0);
-
-	// Check when one item
-	treeset.add ("1");
-	assert (treeset.size == 1);
-
-	// Check when more items
-	treeset.add ("2");
-	assert (treeset.size == 2);
-
-	// Check when items cleared
-	treeset.clear ();
-	assert (treeset.size == 0);
-}
-
-void test_treeset_iterator () {
-	var treeset = new TreeSet<string> ((CompareFunc) strcmp);
-
-	// Check iterate empty list
-	var iterator = treeset.iterator ();
-	assert (!iterator.next());
-
-	// Check iterate list
-	treeset.add ("42");
-	treeset.add ("43");
-
-	iterator = treeset.iterator ();
-
-	assert (iterator.next());
-	string firstString = iterator.get();
-	assert (treeset.contains (firstString)); 
-	assert (firstString == "42");
-
-	assert (iterator.next());
-	string secondString = iterator.get();
-	assert (treeset.contains (secondString));
-	assert (secondString == "43");
-
-	assert (!iterator.next());
-}
-
-void main (string[] args) {
-	Test.init (ref args);
-
-	Test.add_func ("/TreeSet/Collection/add", test_treeset_add);
-	Test.add_func ("/TreeSet/Collection/clear", test_treeset_clear);
-	Test.add_func ("/TreeSet/Collection/contains", test_treeset_contains);
-	Test.add_func ("/TreeSet/Collection/remove", test_treeset_remove);
-	Test.add_func ("/TreeSet/Collection/size", test_treeset_size);
-	Test.add_func ("/TreeSet/Iterable/iterator", test_treeset_iterator);
-
-	Test.run ();
+public class TreeSetTests : SetTests {
+
+	public TreeSetTests () {
+		base ("TreeSet");
+		add_test ("[TreeSet] selected functions", test_selected_functions);
+		add_test ("[TreeSet] GObject properties", test_gobject_properties);
+		add_test ("[TreeSet] ordering", test_ordering);
+	}
+
+	public override void set_up () {
+		test_collection = new TreeSet<string> ();
+	}
+
+	public override void tear_down () {
+		test_collection = null;
+	}
+
+	public void test_selected_functions () {
+		var test_set = test_collection as TreeSet<string>;
+
+		// Check the set exists
+		assert (test_set != null);
+
+		// Check the selected compare function
+		assert (test_set.compare_func == strcmp);
+	}
+
+	public new void test_gobject_properties() {
+		var test_set = test_collection as TreeSet<string>;
+
+		// Check the set exists
+		assert (test_set != null);
+		Value value;
+
+		value = Value (typeof (CompareFunc));
+		test_set.get_property ("compare-func", ref value);
+		assert (value.get_pointer () == (void*) test_set.compare_func);
+		value.unset ();
+	}
+
+	public void test_ordering () {
+		var test_set = test_collection as Set<string>;
+
+		// Check the set exists
+		assert (test_set != null);
+
+		test_set.add ("one");
+		test_set.add ("two");
+		test_set.add ("three");
+		test_set.add ("four");
+		test_set.add ("five");
+		test_set.add ("six");
+		test_set.add ("seven");
+		test_set.add ("eight");
+		test_set.add ("nine");
+		test_set.add ("ten");
+		test_set.add ("eleven");
+		test_set.add ("twelve");
+
+		Iterator<string> iterator = test_set.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]