Commit | Line | Data |
---|---|---|
f06e1619 FB |
1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
f06e1619 FB |
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 | ||
536fc7f7 | 22 | require_once dirname(__FILE__) . '/../include/test.inc.php'; |
f06e1619 FB |
23 | |
24 | class ArrayIteratorTest extends PlTestCase | |
25 | { | |
26 | public function testSimple() | |
27 | { | |
28 | $it = PlIteratorUtils::fromArray(array(1, 2, 3, 4)); | |
748543a7 | 29 | $this->assertSame(4, $it->total()); |
f06e1619 | 30 | |
748543a7 | 31 | $this->assertSame(array('keys' => array(0), 'value' => 1), $it->next()); |
f06e1619 FB |
32 | $this->assertTrue($it->first()); |
33 | $this->assertFalse($it->last()); | |
34 | ||
748543a7 | 35 | $this->assertSame(array('keys' => array(1), 'value' => 2), $it->next()); |
f06e1619 FB |
36 | $this->assertFalse($it->first()); |
37 | $this->assertFalse($it->last()); | |
38 | ||
748543a7 | 39 | $this->assertSame(array('keys' => array(2), 'value' => 3), $it->next()); |
f06e1619 FB |
40 | $this->assertFalse($it->first()); |
41 | $this->assertFalse($it->last()); | |
42 | ||
748543a7 | 43 | $this->assertSame(array('keys' => array(3), 'value' => 4), $it->next()); |
f06e1619 FB |
44 | $this->assertFalse($it->first()); |
45 | $this->assertTrue($it->last()); | |
6cbf939b FB |
46 | |
47 | $this->assertNull($it->next()); | |
f06e1619 FB |
48 | } |
49 | ||
50 | public function testSimpleFlat() | |
51 | { | |
52 | $it = PlIteratorUtils::fromArray(array(1, 2, 3, 4), 1, true); | |
748543a7 | 53 | $this->assertSame(4, $it->total()); |
f06e1619 | 54 | |
748543a7 | 55 | $this->assertSame(1, $it->next()); |
f06e1619 FB |
56 | $this->assertTrue($it->first()); |
57 | $this->assertFalse($it->last()); | |
58 | ||
748543a7 | 59 | $this->assertSame(2, $it->next()); |
f06e1619 FB |
60 | $this->assertFalse($it->first()); |
61 | $this->assertFalse($it->last()); | |
62 | ||
748543a7 | 63 | $this->assertSame(3, $it->next()); |
f06e1619 FB |
64 | $this->assertFalse($it->first()); |
65 | $this->assertFalse($it->last()); | |
66 | ||
748543a7 | 67 | $this->assertSame(4, $it->next()); |
f06e1619 FB |
68 | $this->assertFalse($it->first()); |
69 | $this->assertTrue($it->last()); | |
6cbf939b FB |
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)); | |
748543a7 | 77 | $this->assertSame(4, $it->total()); |
6cbf939b | 78 | |
748543a7 | 79 | $this->assertSame(array('keys' => array('a'), 'value' => 1), $it->next()); |
6cbf939b FB |
80 | $this->assertTrue($it->first()); |
81 | $this->assertFalse($it->last()); | |
82 | ||
748543a7 | 83 | $this->assertSame(array('keys' => array('b'), 'value' => 2), $it->next()); |
6cbf939b FB |
84 | $this->assertFalse($it->first()); |
85 | $this->assertFalse($it->last()); | |
86 | ||
748543a7 | 87 | $this->assertSame(array('keys' => array('c'), 'value' => 3), $it->next()); |
6cbf939b FB |
88 | $this->assertFalse($it->first()); |
89 | $this->assertFalse($it->last()); | |
90 | ||
748543a7 | 91 | $this->assertSame(array('keys' => array('d'), 'value' => 4), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 101 | $this->assertSame(4, $it->total()); |
6cbf939b | 102 | |
748543a7 | 103 | $this->assertSame(1, $it->next()); |
6cbf939b FB |
104 | $this->assertTrue($it->first()); |
105 | $this->assertFalse($it->last()); | |
106 | ||
748543a7 | 107 | $this->assertSame(2, $it->next()); |
6cbf939b FB |
108 | $this->assertFalse($it->first()); |
109 | $this->assertFalse($it->last()); | |
110 | ||
748543a7 | 111 | $this->assertSame(3, $it->next()); |
6cbf939b FB |
112 | $this->assertFalse($it->first()); |
113 | $this->assertFalse($it->last()); | |
114 | ||
748543a7 | 115 | $this->assertSame(4, $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 125 | $this->assertSame(2, $it->total()); |
6cbf939b | 126 | |
748543a7 | 127 | $this->assertSame(array('keys' => array(0), 'value' => array(1, 2)), $it->next()); |
6cbf939b FB |
128 | $this->assertTrue($it->first()); |
129 | $this->assertFalse($it->last()); | |
130 | ||
748543a7 | 131 | $this->assertSame(array('keys' => array(1), 'value' => array(3, 4)), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 139 | $this->assertSame(4, $it->total()); |
6cbf939b | 140 | |
748543a7 | 141 | $this->assertSame(array('keys' => array(0, 0), 'value' => 1), $it->next()); |
6cbf939b FB |
142 | $this->assertTrue($it->first()); |
143 | $this->assertFalse($it->last()); | |
144 | ||
748543a7 | 145 | $this->assertSame(array('keys' => array(0, 1), 'value' => 2), $it->next()); |
6cbf939b FB |
146 | $this->assertFalse($it->first()); |
147 | $this->assertFalse($it->last()); | |
148 | ||
748543a7 | 149 | $this->assertSame(array('keys' => array(1, 0), 'value' => 3), $it->next()); |
6cbf939b FB |
150 | $this->assertFalse($it->first()); |
151 | $this->assertFalse($it->last()); | |
152 | ||
748543a7 | 153 | $this->assertSame(array('keys' => array(1, 1), 'value' => 4), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 163 | $this->assertSame(2, $it->total()); |
6cbf939b | 164 | |
748543a7 | 165 | $this->assertSame(array(1, 2), $it->next()); |
6cbf939b FB |
166 | $this->assertTrue($it->first()); |
167 | $this->assertFalse($it->last()); | |
168 | ||
748543a7 | 169 | $this->assertSame(array(3, 4), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 177 | $this->assertSame(4, $it->total()); |
6cbf939b | 178 | |
748543a7 | 179 | $this->assertSame(1, $it->next()); |
6cbf939b FB |
180 | $this->assertTrue($it->first()); |
181 | $this->assertFalse($it->last()); | |
182 | ||
748543a7 | 183 | $this->assertSame(2, $it->next()); |
6cbf939b FB |
184 | $this->assertFalse($it->first()); |
185 | $this->assertFalse($it->last()); | |
186 | ||
748543a7 | 187 | $this->assertSame(3, $it->next()); |
6cbf939b FB |
188 | $this->assertFalse($it->first()); |
189 | $this->assertFalse($it->last()); | |
190 | ||
748543a7 | 191 | $this->assertSame(4, $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 201 | $this->assertSame(2, $it->total()); |
6cbf939b | 202 | |
748543a7 | 203 | $this->assertSame(array('keys' => array('a'), 'value' => array('b' => 1, 'c' => 2)), $it->next()); |
6cbf939b FB |
204 | $this->assertTrue($it->first()); |
205 | $this->assertFalse($it->last()); | |
206 | ||
748543a7 | 207 | $this->assertSame(array('keys' => array('d'), 'value' => array('e' => 3, 'f' => 4)), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 215 | $this->assertSame(4, $it->total()); |
6cbf939b | 216 | |
748543a7 | 217 | $this->assertSame(array('keys' => array('a', 'b'), 'value' => 1), $it->next()); |
6cbf939b FB |
218 | $this->assertTrue($it->first()); |
219 | $this->assertFalse($it->last()); | |
220 | ||
748543a7 | 221 | $this->assertSame(array('keys' => array('a', 'c'), 'value' => 2), $it->next()); |
6cbf939b FB |
222 | $this->assertFalse($it->first()); |
223 | $this->assertFalse($it->last()); | |
224 | ||
748543a7 | 225 | $this->assertSame(array('keys' => array('d', 'e'), 'value' => 3), $it->next()); |
6cbf939b FB |
226 | $this->assertFalse($it->first()); |
227 | $this->assertFalse($it->last()); | |
228 | ||
748543a7 | 229 | $this->assertSame(array('keys' => array('d', 'f'), 'value' => 4), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 239 | $this->assertSame(2, $it->total()); |
6cbf939b | 240 | |
748543a7 | 241 | $this->assertSame(array('b' => 1, 'c' => 2), $it->next()); |
6cbf939b FB |
242 | $this->assertTrue($it->first()); |
243 | $this->assertFalse($it->last()); | |
244 | ||
748543a7 | 245 | $this->assertSame(array('e' => 3, 'f' => 4), $it->next()); |
6cbf939b FB |
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); | |
748543a7 | 253 | $this->assertSame(4, $it->total()); |
6cbf939b | 254 | |
748543a7 | 255 | $this->assertSame(1, $it->next()); |
6cbf939b FB |
256 | $this->assertTrue($it->first()); |
257 | $this->assertFalse($it->last()); | |
258 | ||
748543a7 | 259 | $this->assertSame(2, $it->next()); |
6cbf939b FB |
260 | $this->assertFalse($it->first()); |
261 | $this->assertFalse($it->last()); | |
262 | ||
748543a7 | 263 | $this->assertSame(3, $it->next()); |
6cbf939b FB |
264 | $this->assertFalse($it->first()); |
265 | $this->assertFalse($it->last()); | |
266 | ||
748543a7 | 267 | $this->assertSame(4, $it->next()); |
6cbf939b FB |
268 | $this->assertFalse($it->first()); |
269 | $this->assertTrue($it->last()); | |
270 | ||
271 | $this->assertNull($it->next()); | |
f06e1619 FB |
272 | } |
273 | } | |
274 | ||
275 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
276 | ?> |