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