| 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 ArrayIteratorTest extends PlTestCase |
| 25 | { |
| 26 | public function testSimple() |
| 27 | { |
| 28 | $it = PlIteratorUtils::fromArray(array(1, 2, 3, 4)); |
| 29 | $this->assertSame(4, $it->total()); |
| 30 | |
| 31 | $this->assertSame(array('keys' => array(0), 'value' => 1), $it->next()); |
| 32 | $this->assertTrue($it->first()); |
| 33 | $this->assertFalse($it->last()); |
| 34 | |
| 35 | $this->assertSame(array('keys' => array(1), 'value' => 2), $it->next()); |
| 36 | $this->assertFalse($it->first()); |
| 37 | $this->assertFalse($it->last()); |
| 38 | |
| 39 | $this->assertSame(array('keys' => array(2), 'value' => 3), $it->next()); |
| 40 | $this->assertFalse($it->first()); |
| 41 | $this->assertFalse($it->last()); |
| 42 | |
| 43 | $this->assertSame(array('keys' => array(3), 'value' => 4), $it->next()); |
| 44 | $this->assertFalse($it->first()); |
| 45 | $this->assertTrue($it->last()); |
| 46 | |
| 47 | $this->assertNull($it->next()); |
| 48 | } |
| 49 | |
| 50 | public function testSimpleFlat() |
| 51 | { |
| 52 | $it = PlIteratorUtils::fromArray(array(1, 2, 3, 4), 1, true); |
| 53 | $this->assertSame(4, $it->total()); |
| 54 | |
| 55 | $this->assertSame(1, $it->next()); |
| 56 | $this->assertTrue($it->first()); |
| 57 | $this->assertFalse($it->last()); |
| 58 | |
| 59 | $this->assertSame(2, $it->next()); |
| 60 | $this->assertFalse($it->first()); |
| 61 | $this->assertFalse($it->last()); |
| 62 | |
| 63 | $this->assertSame(3, $it->next()); |
| 64 | $this->assertFalse($it->first()); |
| 65 | $this->assertFalse($it->last()); |
| 66 | |
| 67 | $this->assertSame(4, $it->next()); |
| 68 | $this->assertFalse($it->first()); |
| 69 | $this->assertTrue($it->last()); |
| 70 | |
| 71 | $this->assertNull($it->next()); |
| 72 | } |
| 73 | |
| 74 | public function testAssoc() |
| 75 | { |
| 76 | $it = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4)); |
| 77 | $this->assertSame(4, $it->total()); |
| 78 | |
| 79 | $this->assertSame(array('keys' => array('a'), 'value' => 1), $it->next()); |
| 80 | $this->assertTrue($it->first()); |
| 81 | $this->assertFalse($it->last()); |
| 82 | |
| 83 | $this->assertSame(array('keys' => array('b'), 'value' => 2), $it->next()); |
| 84 | $this->assertFalse($it->first()); |
| 85 | $this->assertFalse($it->last()); |
| 86 | |
| 87 | $this->assertSame(array('keys' => array('c'), 'value' => 3), $it->next()); |
| 88 | $this->assertFalse($it->first()); |
| 89 | $this->assertFalse($it->last()); |
| 90 | |
| 91 | $this->assertSame(array('keys' => array('d'), 'value' => 4), $it->next()); |
| 92 | $this->assertFalse($it->first()); |
| 93 | $this->assertTrue($it->last()); |
| 94 | |
| 95 | $this->assertNull($it->next()); |
| 96 | } |
| 97 | |
| 98 | public function testAssocFlat() |
| 99 | { |
| 100 | $it = PlIteratorUtils::fromArray(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4), 1, true); |
| 101 | $this->assertSame(4, $it->total()); |
| 102 | |
| 103 | $this->assertSame(1, $it->next()); |
| 104 | $this->assertTrue($it->first()); |
| 105 | $this->assertFalse($it->last()); |
| 106 | |
| 107 | $this->assertSame(2, $it->next()); |
| 108 | $this->assertFalse($it->first()); |
| 109 | $this->assertFalse($it->last()); |
| 110 | |
| 111 | $this->assertSame(3, $it->next()); |
| 112 | $this->assertFalse($it->first()); |
| 113 | $this->assertFalse($it->last()); |
| 114 | |
| 115 | $this->assertSame(4, $it->next()); |
| 116 | $this->assertFalse($it->first()); |
| 117 | $this->assertTrue($it->last()); |
| 118 | |
| 119 | $this->assertNull($it->next()); |
| 120 | } |
| 121 | |
| 122 | public function testDepth() |
| 123 | { |
| 124 | $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 1); |
| 125 | $this->assertSame(2, $it->total()); |
| 126 | |
| 127 | $this->assertSame(array('keys' => array(0), 'value' => array(1, 2)), $it->next()); |
| 128 | $this->assertTrue($it->first()); |
| 129 | $this->assertFalse($it->last()); |
| 130 | |
| 131 | $this->assertSame(array('keys' => array(1), 'value' => array(3, 4)), $it->next()); |
| 132 | $this->assertFalse($it->first()); |
| 133 | $this->assertTrue($it->last()); |
| 134 | |
| 135 | $this->assertNull($it->next()); |
| 136 | |
| 137 | |
| 138 | $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 2); |
| 139 | $this->assertSame(4, $it->total()); |
| 140 | |
| 141 | $this->assertSame(array('keys' => array(0, 0), 'value' => 1), $it->next()); |
| 142 | $this->assertTrue($it->first()); |
| 143 | $this->assertFalse($it->last()); |
| 144 | |
| 145 | $this->assertSame(array('keys' => array(0, 1), 'value' => 2), $it->next()); |
| 146 | $this->assertFalse($it->first()); |
| 147 | $this->assertFalse($it->last()); |
| 148 | |
| 149 | $this->assertSame(array('keys' => array(1, 0), 'value' => 3), $it->next()); |
| 150 | $this->assertFalse($it->first()); |
| 151 | $this->assertFalse($it->last()); |
| 152 | |
| 153 | $this->assertSame(array('keys' => array(1, 1), 'value' => 4), $it->next()); |
| 154 | $this->assertFalse($it->first()); |
| 155 | $this->assertTrue($it->last()); |
| 156 | |
| 157 | $this->assertNull($it->next()); |
| 158 | } |
| 159 | |
| 160 | public function testDepthFlat() |
| 161 | { |
| 162 | $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 1, true); |
| 163 | $this->assertSame(2, $it->total()); |
| 164 | |
| 165 | $this->assertSame(array(1, 2), $it->next()); |
| 166 | $this->assertTrue($it->first()); |
| 167 | $this->assertFalse($it->last()); |
| 168 | |
| 169 | $this->assertSame(array(3, 4), $it->next()); |
| 170 | $this->assertFalse($it->first()); |
| 171 | $this->assertTrue($it->last()); |
| 172 | |
| 173 | $this->assertNull($it->next()); |
| 174 | |
| 175 | |
| 176 | $it = PlIteratorUtils::fromArray(array(array(1, 2), array(3, 4)), 2, true); |
| 177 | $this->assertSame(4, $it->total()); |
| 178 | |
| 179 | $this->assertSame(1, $it->next()); |
| 180 | $this->assertTrue($it->first()); |
| 181 | $this->assertFalse($it->last()); |
| 182 | |
| 183 | $this->assertSame(2, $it->next()); |
| 184 | $this->assertFalse($it->first()); |
| 185 | $this->assertFalse($it->last()); |
| 186 | |
| 187 | $this->assertSame(3, $it->next()); |
| 188 | $this->assertFalse($it->first()); |
| 189 | $this->assertFalse($it->last()); |
| 190 | |
| 191 | $this->assertSame(4, $it->next()); |
| 192 | $this->assertFalse($it->first()); |
| 193 | $this->assertTrue($it->last()); |
| 194 | |
| 195 | $this->assertNull($it->next()); |
| 196 | } |
| 197 | |
| 198 | public function testDepthAssoc() |
| 199 | { |
| 200 | $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 1); |
| 201 | $this->assertSame(2, $it->total()); |
| 202 | |
| 203 | $this->assertSame(array('keys' => array('a'), 'value' => array('b' => 1, 'c' => 2)), $it->next()); |
| 204 | $this->assertTrue($it->first()); |
| 205 | $this->assertFalse($it->last()); |
| 206 | |
| 207 | $this->assertSame(array('keys' => array('d'), 'value' => array('e' => 3, 'f' => 4)), $it->next()); |
| 208 | $this->assertFalse($it->first()); |
| 209 | $this->assertTrue($it->last()); |
| 210 | |
| 211 | $this->assertNull($it->next()); |
| 212 | |
| 213 | |
| 214 | $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 2); |
| 215 | $this->assertSame(4, $it->total()); |
| 216 | |
| 217 | $this->assertSame(array('keys' => array('a', 'b'), 'value' => 1), $it->next()); |
| 218 | $this->assertTrue($it->first()); |
| 219 | $this->assertFalse($it->last()); |
| 220 | |
| 221 | $this->assertSame(array('keys' => array('a', 'c'), 'value' => 2), $it->next()); |
| 222 | $this->assertFalse($it->first()); |
| 223 | $this->assertFalse($it->last()); |
| 224 | |
| 225 | $this->assertSame(array('keys' => array('d', 'e'), 'value' => 3), $it->next()); |
| 226 | $this->assertFalse($it->first()); |
| 227 | $this->assertFalse($it->last()); |
| 228 | |
| 229 | $this->assertSame(array('keys' => array('d', 'f'), 'value' => 4), $it->next()); |
| 230 | $this->assertFalse($it->first()); |
| 231 | $this->assertTrue($it->last()); |
| 232 | |
| 233 | $this->assertNull($it->next()); |
| 234 | } |
| 235 | |
| 236 | public function testDepthAssocFlat() |
| 237 | { |
| 238 | $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 1, true); |
| 239 | $this->assertSame(2, $it->total()); |
| 240 | |
| 241 | $this->assertSame(array('b' => 1, 'c' => 2), $it->next()); |
| 242 | $this->assertTrue($it->first()); |
| 243 | $this->assertFalse($it->last()); |
| 244 | |
| 245 | $this->assertSame(array('e' => 3, 'f' => 4), $it->next()); |
| 246 | $this->assertFalse($it->first()); |
| 247 | $this->assertTrue($it->last()); |
| 248 | |
| 249 | $this->assertNull($it->next()); |
| 250 | |
| 251 | |
| 252 | $it = PlIteratorUtils::fromArray(array('a' => array('b' => 1, 'c' => 2), 'd' => array('e' => 3, 'f' => 4)), 2, true); |
| 253 | $this->assertSame(4, $it->total()); |
| 254 | |
| 255 | $this->assertSame(1, $it->next()); |
| 256 | $this->assertTrue($it->first()); |
| 257 | $this->assertFalse($it->last()); |
| 258 | |
| 259 | $this->assertSame(2, $it->next()); |
| 260 | $this->assertFalse($it->first()); |
| 261 | $this->assertFalse($it->last()); |
| 262 | |
| 263 | $this->assertSame(3, $it->next()); |
| 264 | $this->assertFalse($it->first()); |
| 265 | $this->assertFalse($it->last()); |
| 266 | |
| 267 | $this->assertSame(4, $it->next()); |
| 268 | $this->assertFalse($it->first()); |
| 269 | $this->assertTrue($it->last()); |
| 270 | |
| 271 | $this->assertNull($it->next()); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 276 | ?> |