More unit tests.
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 13 Mar 2010 21:27:20 +0000 (22:27 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Sat, 13 Mar 2010 21:27:20 +0000 (22:27 +0100)
New tests fully cover:
 * array iterator
 * filter/map iterator

Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
ut/arrayiteratortest.php
ut/filtermapiteratortest.php [new file with mode: 0644]

index 96c4932..06e0455 100644 (file)
@@ -43,6 +43,8 @@ class ArrayIteratorTest extends PlTestCase
         $this->assertEquals(array('keys' => array(3), 'value' => 4), $it->next());
         $this->assertFalse($it->first());
         $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
     }
 
     public function testSimpleFlat()
@@ -65,6 +67,208 @@ class ArrayIteratorTest extends PlTestCase
         $this->assertEquals(4, $it->next());
         $this->assertFalse($it->first());
         $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testAssoc()
+    {
+        $it = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4));
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(array('keys' => array('a'), 'value' => 1), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('b'), 'value' => 2), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('c'), 'value' => 3), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('d'), 'value' => 4), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testAssocFlat()
+    {
+        $it = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4), 1, true);
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(1, $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(2, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(3, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(4, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testDepth()
+    {
+        $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 1);
+        $this->assertEquals(2, $it->total());
+
+        $this->assertEquals(array('keys' => array(0), 'value' => array(1, 2)), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array(1), 'value' => array(3, 4)), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+
+
+        $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 2);
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(array('keys' => array(0, 0), 'value' => 1), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array(0, 1), 'value' => 2), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array(1, 0), 'value' => 3), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array(1, 1), 'value' => 4), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testDepthFlat()
+    {
+        $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 1, true);
+        $this->assertEquals(2, $it->total());
+
+        $this->assertEquals(array(1, 2), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array(3, 4), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+
+
+        $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 2, true);
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(1, $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(2, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(3, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(4, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testDepthAssoc()
+    {
+        $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 1);
+        $this->assertEquals(2, $it->total());
+
+        $this->assertEquals(array('keys' => array('a'), 'value' => array('b' => 1, 'c' => 2)), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('d'), 'value' => array('e' => 3, 'f' => 4)), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+
+
+        $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 2);
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(array('keys' => array('a', 'b'), 'value' => 1), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('a', 'c'), 'value' => 2), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('d', 'e'), 'value' => 3), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('keys' => array('d', 'f'), 'value' => 4), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public function testDepthAssocFlat()
+    {
+        $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 1, true);
+        $this->assertEquals(2, $it->total());
+
+        $this->assertEquals(array('b' => 1, 'c' => 2), $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(array('e' => 3, 'f' => 4), $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+
+
+        $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 2, true);
+        $this->assertEquals(4, $it->total());
+
+        $this->assertEquals(1, $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(2, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(3, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(4, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
     }
 }
 
diff --git a/ut/filtermapiteratortest.php b/ut/filtermapiteratortest.php
new file mode 100644 (file)
index 0000000..4d320df
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program 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 General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once dirname(__FILE__) . '/../include/test.inc.php';
+
+class FilterMapIteratorTest extends PlTestCase
+{
+    public static function filter($e)
+    {
+        return $e > 8;
+    }
+
+    public function testFilter()
+    {
+        $it = PlIteratorUtils::fromArray(array(1, 10, 3, 9, 42, -23, 9, 8), 1, true);
+        $it = PlIteratorUtils::filter($it, array('FilterMapIteratorTest', 'filter'));
+        $this->assertLessThanOrEqual(8, $it->total());
+        $this->assertGreaterThanOrEqual(4, $it->total());
+
+        $this->assertEquals(10, $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(9, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(42, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(9, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+
+    public static function map($e)
+    {
+        return $e * 2;
+    }
+
+    public function testMap()
+    {
+        $it = PlIteratorUtils::fromArray(array(1, 2, 3, 4, 7, 512), 1, true);
+        $it = PlIteratorUtils::map($it, array('FilterMapIteratorTest', 'map'));
+        $this->assertEquals(6, $it->total());
+
+        $this->assertEquals(2, $it->next());
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(4, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(6, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(8, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(14, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertEquals(1024, $it->next());
+        $this->assertFalse($it->first());
+        $this->assertTrue($it->last());
+
+        $this->assertNull($it->next());
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>