[libgee] Improve the tests for Collection.foreach and Collection.fold



commit 769ac73404025ea78c2c7465e64e2e542610f15b
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Mon Jul 15 19:37:50 2013 +0200

    Improve the tests for Collection.foreach and Collection.fold

 tests/testcollection.vala |  227 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 183 insertions(+), 44 deletions(-)
---
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index 090724a..e80d9dd 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -2,7 +2,7 @@
  *
  * Copyright (C) 2008  Jürg Billeter
  * Copyright (C) 2009  Didier Villevalois, Julien Peeters
- * Copyright (C) 2011-2012  Maciej Piechotka
+ * Copyright (C) 2011-2013  Maciej Piechotka
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -45,8 +45,8 @@ 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] fold", test_fold);
                add_test ("[Collection] map", test_map);
                add_test ("[Collection] scan", test_scan);
                add_test ("[Collection] filter", test_filter);
@@ -310,15 +310,14 @@ public abstract class CollectionTests : Gee.TestCase {
                        assert (test_collection.contains (a));
                }
                assert (test_collection.size == to_add.length);
-               
+
                test_collection.clear ();
-               
+
                assert (test_collection.size == 0);
-               
+
                Iterator<string> iter = test_collection.iterator ();
                assert (iter != null);
-               assert (!iter.has_next ());             
-               
+               assert (!iter.has_next ());
        }
 
        public void test_add_all () {
@@ -641,51 +640,191 @@ public abstract class CollectionTests : Gee.TestCase {
                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;
                bool res;
-               
-               res = test_collection.foreach ((x) => {count++; return true;});
-               assert (count == 3);
+               unowned string[] data = TestData.get_data ();
+
+               // Check for empty collection
+               res = test_collection.foreach ((x) => {
+                       assert_not_reached ();
+               });
                assert (res == true);
-               
-               res = test_collection.iterator ().foreach ((x) => {count++; return true;});
-               assert (count == 6);
+               res = test_collection.iterator ().foreach ((x) => {
+                       assert_not_reached ();
+               });
                assert (res == true);
-               
+
+               // Check for full run
+               foreach (unowned string el in data) {
+                       assert (test_collection.add (el));
+               }
+
+               int count;
+
+               count = 0;
+               var test_iterator = test_collection.iterator ();
+               res = test_collection.foreach ((x) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       count++;
+                       return true;
+               });
+               assert (count == data.length);
+               assert (res == true);
+
+               count = 0;
+               test_iterator = test_collection.iterator ();
+               res = test_collection.iterator ().foreach ((x) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       count++;
+                       return true;
+               });
+               assert (count == data.length);
+               assert (res == true);
+
+               count = 1;
+               test_iterator = test_collection.iterator ();
                Iterator<string> iter = test_collection.iterator ();
                assert (iter.next ());
-               res = iter.foreach ((x) => {count++; return true;});
-               assert (count == 9);
+               assert (iter.next ());
+               assert (test_iterator.next ());
+               res = iter.foreach ((x) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       count++;
+                       return true;
+               });
+               assert (count == data.length);
                assert (res == true);
 
-               res = test_collection.foreach ((x) => {count++; return false;});
-               assert (count == 10);
-               assert (res == false);
+               count = 1;
+               test_iterator = test_collection.iterator ();
+               iter = test_collection.iterator ();
+               assert (iter.next ());
+               assert (iter.next ());
+               assert (iter.has_next ());
+               assert (test_iterator.next ());
+               res = iter.foreach ((x) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       count++;
+                       return true;
+               });
+               assert (count == data.length);
+               assert (res == true);
+
+               // Check for break after 1-3 elements
+               for (int i = 0; i < 3; i++) {
+                       count = 0;
+                       test_iterator = test_collection.iterator ();
+                       res = test_collection.foreach ((x) => {
+                               assert (test_iterator.next ());
+                               assert (x == test_iterator.get ());
+                               return count++ != i;
+                       });
+                       assert (count == i + 1);
+                       assert (res == false);
+
+                       count = 0;
+                       test_iterator = test_collection.iterator ();
+                       res = test_collection.iterator ().foreach ((x) => {
+                               assert (test_iterator.next ());
+                               assert (x == test_iterator.get ());
+                               return count++ != i;
+                       });
+                       assert (count == i + 1);
+                       assert (res == false);
+
+                       count = 1;
+                       test_iterator = test_collection.iterator ();
+                       iter = test_collection.iterator ();
+                       assert (iter.next ());
+                       assert (iter.next ());
+                       assert (test_iterator.next ());
+                       res = iter.foreach ((x) => {
+                               assert (test_iterator.next ());
+                               assert (x == test_iterator.get ());
+                               return count++ != i + 1;
+                       });
+                       assert (count == i + 2);
+                       assert (res == false);
+
+                       count = 1;
+                       test_iterator = test_collection.iterator ();
+                       iter = test_collection.iterator ();
+                       assert (iter.next ());
+                       assert (iter.next ());
+                       assert (iter.has_next ());
+                       assert (test_iterator.next ());
+                       res = iter.foreach ((x) => {
+                               assert (test_iterator.next ());
+                               assert (x == test_iterator.get ());
+                               return count++ != i + 1;
+                       });
+                       assert (count == i + 2);
+                       assert (res == false);
+               }
+       }
+
+       public void test_fold () {
+               int count;
+               unowned string[] data = TestData.get_data ();
+
+               // Check for empty collection
+               count = test_collection.fold<int> ((x, y) => {
+                       assert_not_reached ();
+               }, 0);
+               count = test_collection.iterator ().fold<int> ((x, y) => {
+                       assert_not_reached ();
+               }, 0);
+
+               // Check for some elements in the collection
+               foreach (unowned string el in data) {
+                       assert (test_collection.add (el));
+               }
+
+               var test_iterator = test_collection.iterator ();
+               count = test_collection.fold<int> ((x, y) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       return y + 1;
+               }, 0);
+               assert (count == data.length);
+
+               test_iterator = test_collection.iterator ();
+               count = test_collection.iterator ().fold<int> ((x, y) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       return y + 1;
+               }, 0);
+               assert (count == data.length);
+
+               test_iterator = test_collection.iterator ();
+               var iter = test_collection.iterator ();
+               assert (iter.next ());
+               assert (iter.next ());
+               assert (test_iterator.next ());
+               count = iter.fold<int> ((x, y) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       return y + 1;
+               }, 1);
+               assert (count == data.length);
+
+               test_iterator = test_collection.iterator ();
+               iter = test_collection.iterator ();
+               assert (iter.next ());
+               assert (iter.next ());
+               assert (iter.has_next ());
+               assert (test_iterator.next ());
+               count = iter.fold<int> ((x, y) => {
+                       assert (test_iterator.next ());
+                       assert (x == test_iterator.get ());
+                       return y + 1;
+               }, 1);
+               assert (count == data.length);
        }
 
        public void test_map () {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]