| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | require_once dirname(__FILE__) . '/../include/test.inc.php'; |
| 23 | |
| 24 | class SubIteratorTest extends PlTestCase |
| 25 | { |
| 26 | public function testSimple() |
| 27 | { |
| 28 | $iter = PlIteratorUtils::fromArray(array('a' => 1, |
| 29 | 'b' => 1, |
| 30 | 'c' => 2, |
| 31 | 'd' => 3)); |
| 32 | $cb = PlIteratorUtils::arrayValueCallback('value'); |
| 33 | |
| 34 | $it = PlIteratorUtils::subIterator($iter, $cb); |
| 35 | |
| 36 | $subit = $it->next(); |
| 37 | $this->assertTrue($it->first()); |
| 38 | $this->assertFalse($it->last()); |
| 39 | |
| 40 | $v = $subit->next(); |
| 41 | $this->assertSame(1, $subit->value()); |
| 42 | $this->assertSame(array('keys' => array('a'), 'value' => 1), $v); |
| 43 | |
| 44 | $this->assertTrue($subit->first()); |
| 45 | $this->assertFalse($subit->last()); |
| 46 | |
| 47 | $this->assertSame(array('keys' => array('b'), 'value' => 1), $subit->next()); |
| 48 | $this->assertFalse($subit->first()); |
| 49 | $this->assertTrue($subit->last()); |
| 50 | |
| 51 | $this->assertNull($subit->next()); |
| 52 | |
| 53 | $this->assertTrue($it->first()); |
| 54 | |
| 55 | $subit2 = $it->next(); |
| 56 | $this->assertFalse($it->first()); |
| 57 | $this->assertFalse($it->last()); |
| 58 | |
| 59 | $this->assertSame(2, $subit2->value()); |
| 60 | $this->assertSame(array('keys' => array('c'), 'value' => 2), $subit2->next()); |
| 61 | |
| 62 | $this->assertTrue($subit2->first()); |
| 63 | $this->assertTrue($subit2->last()); |
| 64 | |
| 65 | $this->assertNull($subit2->next()); |
| 66 | |
| 67 | $subit3 = $it->next(); |
| 68 | $this->assertFalse($it->first()); |
| 69 | $this->assertFalse($it->last()); |
| 70 | |
| 71 | $this->assertSame(3, $subit3->value()); |
| 72 | $this->assertSame(array('keys' => array('d'), 'value' => 3), $subit3->next()); |
| 73 | |
| 74 | $this->assertTrue($subit3->first()); |
| 75 | $this->assertTrue($subit3->last()); |
| 76 | |
| 77 | $this->assertNull($subit3->next()); |
| 78 | |
| 79 | $this->assertTrue($it->last()); |
| 80 | $this->assertNull($it->next()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 85 | ?> |