Add Gee.Iterator<G>.foreach method
authorMaciej Piechotka <uzytkownik2@gmail.com>
Sun, 7 Nov 2010 22:15:20 +0000 (22:15 +0000)
committerMaciej Piechotka <uzytkownik2@gmail.com>
Sun, 7 Nov 2010 22:15:20 +0000 (22:15 +0000)
gee/iterator.vala
tests/testcollection.vala

index 92a7d73..0183c79 100644 (file)
@@ -25,6 +25,7 @@
 
 namespace Gee {
        public delegate A FoldFunc<A, G> (G g, owned A a);
+       public delegate void ForallFunc<G> (G g);
 }
 
 /**
@@ -97,7 +98,7 @@ public interface Gee.Iterator<G> : Object {
         * Operation moves the iterator to last element in iteration. If iterator
         * points at some element it will be included in iteration.
         */
-       public virtual void forall (ForallFunc<G> f) {
+       public new virtual void foreach (ForallFunc<G> f) {
                if (valid)
                        f (get ());
                while (next ())
index 93171cf..f8613c2 100644 (file)
@@ -44,6 +44,7 @@ public abstract class CollectionTests : Gee.TestCase {
                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);
        }
 
        protected Collection<string> test_collection;
@@ -762,4 +763,20 @@ public abstract class CollectionTests : Gee.TestCase {
                count = iter.fold<int> ((x, y) => {return y + 1;}, 0);
                assert (count == 3);
        }
+       
+       public void test_foreach () {
+               assert (test_collection.add ("one"));
+               assert (test_collection.add ("two"));
+               assert (test_collection.add ("three"));
+               
+               int count = 0;
+               
+               test_collection.iterator ().foreach ((x) => {count++;});
+               assert (count == 3);
+               
+               Iterator<string> iter = test_collection.iterator ();
+               assert (iter.next ());
+               iter.foreach ((x) => {count++;});
+               assert (count == 6);
+       }
 }