| 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 ParallelIteratorTest extends PlTestCase |
| 25 | { |
| 26 | public function testSameCallback() |
| 27 | { |
| 28 | $m = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5)); |
| 29 | $s1 = PlIteratorUtils::fromArray(array('X' => 1, 'Y' => 2, 'Z' => 4)); |
| 30 | $s2 = PlIteratorUtils::fromArray(array('aaa' => 2, 'bbb' => 3, 'ccc' => 6)); |
| 31 | |
| 32 | $cb = PlIteratorUtils::arrayValueCallback('value'); |
| 33 | |
| 34 | $its = array(0 => $m, 1 => $s1, 2 => $s2); |
| 35 | $cbs = array(0 => $cb, 1 => $cb, 2 => $cb); |
| 36 | |
| 37 | $it = PlIteratorUtils::parallelIterator($its, $cbs, 0); |
| 38 | |
| 39 | $this->assertSame(5, $it->total()); |
| 40 | |
| 41 | $this->assertSame(array(0 => array('keys' => array('a'), 'value' => 1), |
| 42 | 1 => array('keys' => array('X'), 'value' => 1), |
| 43 | 2 => null), $it->next()); |
| 44 | $this->assertTrue($it->first()); |
| 45 | $this->assertFalse($it->last()); |
| 46 | |
| 47 | $this->assertSame(array(0 => array('keys' => array('b'), 'value' => 2), |
| 48 | 1 => array('keys' => array('Y'), 'value' => 2), |
| 49 | 2 => array('keys' => array('aaa'), 'value' => 2) |
| 50 | ), $it->next()); |
| 51 | |
| 52 | $this->assertFalse($it->first()); |
| 53 | $this->assertFalse($it->last()); |
| 54 | |
| 55 | $this->assertSame(array(0 => array('keys' => array('c'), 'value' => 3), |
| 56 | 1 => null, |
| 57 | 2 => array('keys' => array('bbb'), 'value' => 3), |
| 58 | ), $it->next()); |
| 59 | |
| 60 | $this->assertFalse($it->first()); |
| 61 | $this->assertFalse($it->last()); |
| 62 | |
| 63 | $this->assertSame(array(0 => array('keys' => array('d'), 'value' => 4), |
| 64 | 1 => array('keys' => array('Z'), 'value' => 4), |
| 65 | 2 => null, |
| 66 | ), $it->next()); |
| 67 | |
| 68 | $this->assertFalse($it->first()); |
| 69 | $this->assertFalse($it->last()); |
| 70 | |
| 71 | $this->assertSame(array(0 => array('keys' => array('e'), 'value' => 5), |
| 72 | 1 => null, |
| 73 | 2 => null, |
| 74 | ), $it->next()); |
| 75 | |
| 76 | $this->assertFalse($it->first()); |
| 77 | $this->assertTrue($it->last()); |
| 78 | |
| 79 | $this->assertNull($it->next()); |
| 80 | } |
| 81 | |
| 82 | public function testOneCallback() |
| 83 | { |
| 84 | $m = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5)); |
| 85 | $s1 = PlIteratorUtils::fromArray(array('X' => 1, 'Y' => 2, 'Z' => 4)); |
| 86 | $s2 = PlIteratorUtils::fromArray(array('aaa' => 2, 'bbb' => 3, 'ccc' => 6)); |
| 87 | |
| 88 | $cb = PlIteratorUtils::arrayValueCallback('value'); |
| 89 | |
| 90 | $its = array(0 => $m, 1 => $s1, 2 => $s2); |
| 91 | |
| 92 | $it = PlIteratorUtils::parallelIterator($its, $cb, 0); |
| 93 | |
| 94 | $this->assertSame(5, $it->total()); |
| 95 | |
| 96 | $this->assertSame(array(0 => array('keys' => array('a'), 'value' => 1), |
| 97 | 1 => array('keys' => array('X'), 'value' => 1), |
| 98 | 2 => null), $it->next()); |
| 99 | $this->assertTrue($it->first()); |
| 100 | $this->assertFalse($it->last()); |
| 101 | |
| 102 | $this->assertSame(array(0 => array('keys' => array('b'), 'value' => 2), |
| 103 | 1 => array('keys' => array('Y'), 'value' => 2), |
| 104 | 2 => array('keys' => array('aaa'), 'value' => 2) |
| 105 | ), $it->next()); |
| 106 | |
| 107 | $this->assertFalse($it->first()); |
| 108 | $this->assertFalse($it->last()); |
| 109 | |
| 110 | $this->assertSame(array(0 => array('keys' => array('c'), 'value' => 3), |
| 111 | 1 => null, |
| 112 | 2 => array('keys' => array('bbb'), 'value' => 3), |
| 113 | ), $it->next()); |
| 114 | |
| 115 | $this->assertFalse($it->first()); |
| 116 | $this->assertFalse($it->last()); |
| 117 | |
| 118 | $this->assertSame(array(0 => array('keys' => array('d'), 'value' => 4), |
| 119 | 1 => array('keys' => array('Z'), 'value' => 4), |
| 120 | 2 => null, |
| 121 | ), $it->next()); |
| 122 | |
| 123 | $this->assertFalse($it->first()); |
| 124 | $this->assertFalse($it->last()); |
| 125 | |
| 126 | $this->assertSame(array(0 => array('keys' => array('e'), 'value' => 5), |
| 127 | 1 => null, |
| 128 | 2 => null, |
| 129 | ), $it->next()); |
| 130 | |
| 131 | $this->assertFalse($it->first()); |
| 132 | $this->assertTrue($it->last()); |
| 133 | |
| 134 | $this->assertNull($it->next()); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 140 | ?> |