1 | <?php |
||
2 | declare(strict_types = 1); |
||
3 | |||
4 | namespace Tests\Innmind\Immutable\Map; |
||
5 | |||
6 | use Innmind\Immutable\{ |
||
7 | Map\DoubleIndex, |
||
8 | Map\Implementation, |
||
9 | Map, |
||
10 | Pair, |
||
11 | Str, |
||
12 | Set, |
||
13 | Sequence, |
||
14 | }; |
||
15 | use PHPUnit\Framework\TestCase; |
||
16 | |||
17 | class DoubleIndexTest extends TestCase |
||
18 | { |
||
19 | public function testInterface() |
||
20 | { |
||
21 | $m = new DoubleIndex; |
||
22 | |||
23 | $this->assertInstanceOf(Map\Implementation::class, $m); |
||
24 | $this->assertInstanceOf(\Countable::class, $m); |
||
25 | } |
||
26 | |||
27 | public function testPut() |
||
28 | { |
||
29 | $m = new DoubleIndex; |
||
30 | |||
31 | $this->assertSame(0, $m->size()); |
||
32 | $m2 = ($m)(42, 42); |
||
33 | $this->assertNotSame($m, $m2); |
||
34 | $this->assertSame(0, $m->size()); |
||
35 | $this->assertSame(1, $m2->size()); |
||
36 | |||
37 | $m = new DoubleIndex; |
||
38 | $m = $m |
||
39 | (23, 24) |
||
40 | (41, 42) |
||
41 | (65, 66) |
||
42 | (89, 90) |
||
43 | (65, 1); |
||
44 | |||
45 | $this->assertSame(24, $this->get($m, 23)); |
||
46 | $this->assertSame(42, $this->get($m, 41)); |
||
47 | $this->assertSame(1, $this->get($m, 65)); |
||
48 | $this->assertSame(90, $this->get($m, 89)); |
||
49 | $this->assertSame(4, $m->size()); |
||
50 | } |
||
51 | |||
52 | public function testGet() |
||
53 | { |
||
54 | $m = new DoubleIndex; |
||
55 | $m = ($m)(23, 24); |
||
56 | |||
57 | $this->assertSame( |
||
58 | 24, |
||
59 | $m->get(23)->match( |
||
60 | static fn($value) => $value, |
||
61 | static fn() => null, |
||
62 | ), |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | public function testReturnNothingWhenGettingUnknownKey() |
||
67 | { |
||
68 | $this->assertNull( |
||
69 | (new DoubleIndex)->get(24)->match( |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
70 | static fn($value) => $value, |
||
71 | static fn() => null, |
||
72 | ), |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | public function testContains() |
||
77 | { |
||
78 | $m = new DoubleIndex; |
||
79 | $m = ($m)(23, 24); |
||
80 | |||
81 | $this->assertFalse($m->contains(24)); |
||
82 | $this->assertTrue($m->contains(23)); |
||
83 | } |
||
84 | |||
85 | public function testClear() |
||
86 | { |
||
87 | $m = new DoubleIndex; |
||
88 | $m = ($m)(24, 42.0); |
||
89 | |||
90 | $m2 = $m->clear(); |
||
91 | $this->assertNotSame($m, $m2); |
||
92 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
93 | $this->assertSame(1, $m->size()); |
||
94 | $this->assertSame(0, $m2->size()); |
||
95 | } |
||
96 | |||
97 | public function testEquals() |
||
98 | { |
||
99 | $m = (new DoubleIndex)(24, 42); |
||
100 | $m2 = (new DoubleIndex)(24, 42); |
||
101 | |||
102 | $this->assertTrue($m->equals($m2)); |
||
103 | $this->assertFalse($m->equals(($m2)(65, 66))); |
||
104 | $this->assertFalse($m->equals(($m2)(24, 24))); |
||
105 | $this->assertFalse( |
||
106 | (new DoubleIndex)('foo_res', 'res')('foo_bar_res', 'res')->equals( |
||
107 | (new DoubleIndex) |
||
108 | ('foo_res', 'res') |
||
109 | ('bar_res', 'res'), |
||
110 | ), |
||
111 | ); |
||
112 | |||
113 | $m = (new DoubleIndex) |
||
114 | (24, 42) |
||
115 | (42, 24); |
||
116 | $m2 = (new DoubleIndex) |
||
117 | (42, 24) |
||
118 | (24, 42); |
||
119 | |||
120 | $this->assertTrue($m->equals($m2)); |
||
121 | |||
122 | $this->assertTrue((new DoubleIndex)->equals(new DoubleIndex)); |
||
123 | } |
||
124 | |||
125 | public function testFilter() |
||
126 | { |
||
127 | $m = (new DoubleIndex) |
||
128 | (0, 1) |
||
129 | (1, 2) |
||
130 | (2, 3) |
||
131 | (4, 5); |
||
132 | |||
133 | $m2 = $m->filter(static function(int $key, int $value) { |
||
134 | return ($key + $value) % 3 === 0; |
||
135 | }); |
||
136 | |||
137 | $this->assertNotSame($m, $m2); |
||
138 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
139 | $this->assertSame(4, $m->size()); |
||
140 | $this->assertSame(2, $m2->size()); |
||
141 | $this->assertTrue($m2->contains(1)); |
||
142 | $this->assertTrue($m2->contains(4)); |
||
143 | $this->assertFalse($m2->contains(0)); |
||
144 | $this->assertFalse($m2->contains(2)); |
||
145 | } |
||
146 | |||
147 | public function testForeach() |
||
148 | { |
||
149 | $m = (new DoubleIndex) |
||
150 | (0, 1) |
||
151 | (1, 2) |
||
152 | (2, 3) |
||
153 | (3, 4); |
||
154 | $count = 0; |
||
155 | |||
156 | $m->foreach(function(int $key, int $value) use (&$count) { |
||
157 | $this->assertSame($count, $key); |
||
158 | $this->assertSame($value, $key + 1); |
||
159 | ++$count; |
||
160 | }); |
||
161 | $this->assertSame(4, $count); |
||
162 | } |
||
163 | |||
164 | public function testGroupEmptyMap() |
||
165 | { |
||
166 | $this->assertTrue( |
||
167 | (new DoubleIndex) |
||
168 | ->groupBy(static function() {}) |
||
169 | ->equals(Map::of()), |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | public function testGroupBy() |
||
174 | { |
||
175 | $m = (new DoubleIndex) |
||
176 | (0, 1) |
||
177 | (1, 2) |
||
178 | (2, 3) |
||
179 | (4, 5); |
||
180 | |||
181 | $m2 = $m->groupBy(static function(int $key, int $value) { |
||
182 | return ($key + $value) % 3; |
||
183 | }); |
||
184 | $this->assertNotSame($m, $m2); |
||
185 | $this->assertInstanceOf(Map::class, $m2); |
||
186 | $this->assertTrue($m2->contains(0)); |
||
187 | $this->assertTrue($m2->contains(1)); |
||
188 | $this->assertTrue($m2->contains(2)); |
||
189 | $this->assertSame(2, $this->get($m2, 0)->size()); |
||
190 | $this->assertSame(1, $this->get($m2, 1)->size()); |
||
191 | $this->assertSame(1, $this->get($m2, 2)->size()); |
||
192 | $this->assertSame(1, $this->get($this->get($m2, 1), 0)); |
||
193 | $this->assertSame(2, $this->get($this->get($m2, 0), 1)); |
||
194 | $this->assertSame(3, $this->get($this->get($m2, 2), 2)); |
||
195 | $this->assertSame(5, $this->get($this->get($m2, 0), 4)); |
||
196 | } |
||
197 | public function testKeys() |
||
198 | { |
||
199 | $m = (new DoubleIndex) |
||
200 | (0, 1) |
||
201 | (1, 2) |
||
202 | (2, 3) |
||
203 | (4, 5); |
||
204 | |||
205 | $k = $m->keys(); |
||
206 | $this->assertInstanceOf(Set::class, $k); |
||
207 | $this->assertSame([0, 1, 2, 4], $k->toList()); |
||
208 | $this->assertTrue($k->equals($m->keys())); |
||
209 | } |
||
210 | |||
211 | public function testValues() |
||
212 | { |
||
213 | $m = (new DoubleIndex) |
||
214 | (0, 1) |
||
215 | (1, 2) |
||
216 | (2, 3) |
||
217 | (4, 5) |
||
218 | (5, 5); |
||
219 | |||
220 | $v = $m->values(); |
||
221 | $this->assertInstanceOf(Sequence::class, $v); |
||
222 | $this->assertSame([1, 2, 3, 5, 5], $v->toList()); |
||
223 | $this->assertTrue($v->equals($m->values())); |
||
224 | } |
||
225 | |||
226 | public function testMap() |
||
227 | { |
||
228 | $m = (new DoubleIndex) |
||
229 | (0, 1) |
||
230 | (1, 2) |
||
231 | (2, 3) |
||
232 | (4, 5); |
||
233 | |||
234 | $m2 = $m->map(static function(int $key, int $value) { |
||
235 | return $value**2; |
||
236 | }); |
||
237 | $this->assertNotSame($m, $m2); |
||
238 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
239 | $this->assertSame([0, 1, 2, 4], $m->keys()->toList()); |
||
240 | $this->assertSame([1, 2, 3, 5], $m->values()->toList()); |
||
241 | $this->assertSame([0, 1, 2, 4], $m2->keys()->toList()); |
||
242 | $this->assertSame([1, 4, 9, 25], $m2->values()->toList()); |
||
243 | } |
||
244 | |||
245 | public function testRemove() |
||
246 | { |
||
247 | $m = (new DoubleIndex) |
||
248 | (0, 1) |
||
249 | (1, 2) |
||
250 | (2, 3) |
||
251 | (3, 4) |
||
252 | (4, 5); |
||
253 | |||
254 | $m2 = $m->remove(12); |
||
0 ignored issues
–
show
|
|||
255 | $this->assertSame([0, 1, 2, 3, 4], $m->keys()->toList()); |
||
256 | $this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
||
257 | |||
258 | $m2 = $m->remove(3); |
||
259 | $this->assertNotSame($m, $m2); |
||
260 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
261 | $this->assertSame([0, 1, 2, 3, 4], $m->keys()->toList()); |
||
262 | $this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
||
263 | $this->assertSame([0, 1, 2, 4], $m2->keys()->toList()); |
||
264 | $this->assertSame([1, 2, 3, 5], $m2->values()->toList()); |
||
265 | |||
266 | $m2 = $m->remove(4); |
||
267 | $this->assertNotSame($m, $m2); |
||
268 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
269 | $this->assertSame([0, 1, 2, 3, 4], $m->keys()->toList()); |
||
270 | $this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
||
271 | $this->assertSame([0, 1, 2, 3], $m2->keys()->toList()); |
||
272 | $this->assertSame([1, 2, 3, 4], $m2->values()->toList()); |
||
273 | |||
274 | $m2 = $m->remove(0); |
||
275 | $this->assertNotSame($m, $m2); |
||
276 | $this->assertInstanceOf(DoubleIndex::class, $m2); |
||
277 | $this->assertSame([0, 1, 2, 3, 4], $m->keys()->toList()); |
||
278 | $this->assertSame([1, 2, 3, 4, 5], $m->values()->toList()); |
||
279 | $this->assertSame([1, 2, 3, 4], $m2->keys()->toList()); |
||
280 | $this->assertSame([2, 3, 4, 5], $m2->values()->toList()); |
||
281 | } |
||
282 | |||
283 | public function testMerge() |
||
284 | { |
||
285 | $m = (new DoubleIndex) |
||
286 | ($s = new \stdClass, 24) |
||
287 | ($s2 = new \stdClass, 42); |
||
288 | $m2 = (new DoubleIndex) |
||
289 | ($s3 = new \stdClass, 24) |
||
290 | ($s2, 66) |
||
291 | ($s4 = new \stdClass, 42); |
||
292 | |||
293 | $m3 = $m->merge($m2); |
||
294 | $this->assertNotSame($m, $m3); |
||
295 | $this->assertNotSame($m2, $m3); |
||
296 | $this->assertInstanceOf(DoubleIndex::class, $m3); |
||
297 | $this->assertSame(4, $m3->size()); |
||
298 | $this->assertSame([$s, $s2], $m->keys()->toList()); |
||
299 | $this->assertSame([24, 42], $m->values()->toList()); |
||
300 | $this->assertSame([$s3, $s2, $s4], $m2->keys()->toList()); |
||
301 | $this->assertSame([24, 66, 42], $m2->values()->toList()); |
||
302 | $this->assertSame([$s, $s3, $s2, $s4], $m3->keys()->toList()); |
||
303 | $this->assertSame([24, 24, 66, 42], $m3->values()->toList()); |
||
304 | $this->assertFalse($m3->equals($m2->merge($m))); |
||
305 | } |
||
306 | |||
307 | public function testPartition() |
||
308 | { |
||
309 | $m = (new DoubleIndex) |
||
310 | (0, 1) |
||
311 | (1, 2) |
||
312 | (2, 3) |
||
313 | (3, 4) |
||
314 | (4, 5); |
||
315 | |||
316 | $p = $m->partition(static function(int $i, int $v) { |
||
317 | return ($i + $v) % 3 === 0; |
||
318 | }); |
||
319 | |||
320 | $this->assertInstanceOf(Map::class, $p); |
||
321 | $this->assertNotSame($p, $m); |
||
322 | $this->assertSame( |
||
323 | [true, false], |
||
324 | $p->keys()->toList(), |
||
325 | ); |
||
326 | $this->assertSame( |
||
327 | [1, 4], |
||
328 | $this->get($p, true)->keys()->toList(), |
||
329 | ); |
||
330 | $this->assertSame( |
||
331 | [2, 5], |
||
332 | $this->get($p, true)->values()->toList(), |
||
333 | ); |
||
334 | $this->assertSame( |
||
335 | [0, 2, 3], |
||
336 | $this->get($p, false)->keys()->toList(), |
||
337 | ); |
||
338 | $this->assertSame( |
||
339 | [1, 3, 4], |
||
340 | $this->get($p, false)->values()->toList(), |
||
341 | ); |
||
342 | } |
||
343 | |||
344 | public function testReduce() |
||
345 | { |
||
346 | $m = (new DoubleIndex)(4, 4); |
||
347 | |||
348 | $v = $m->reduce( |
||
349 | 42, |
||
350 | static function(float $carry, int $key, int $value): float { |
||
351 | return $carry / ($key * $value); |
||
352 | }, |
||
353 | ); |
||
354 | |||
355 | $this->assertSame(2.625, $v); |
||
356 | $this->assertSame([4], $m->keys()->toList()); |
||
357 | $this->assertSame([4], $m->values()->toList()); |
||
358 | } |
||
359 | |||
360 | public function testEmpty() |
||
361 | { |
||
362 | $this->assertTrue((new DoubleIndex)->empty()); |
||
363 | $this->assertFalse((new DoubleIndex)(1, 2)->empty()); |
||
364 | } |
||
365 | |||
366 | public function testFind() |
||
367 | { |
||
368 | $map = (new DoubleIndex)(1, 2)(3, 4)(5, 6); |
||
369 | |||
370 | $this->assertSame( |
||
371 | 4, |
||
372 | $map |
||
373 | ->find(static fn($k) => $k === 3) |
||
374 | ->map(static fn($pair) => $pair->value()) |
||
375 | ->match( |
||
376 | static fn($value) => $value, |
||
377 | static fn() => null, |
||
378 | ), |
||
379 | ); |
||
380 | } |
||
381 | |||
382 | private function get($map, $index) |
||
383 | { |
||
384 | return $map->get($index)->match( |
||
385 | static fn($value) => $value, |
||
386 | static fn() => null, |
||
387 | ); |
||
388 | } |
||
389 | } |
||
390 |