1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Set, |
8
|
|
|
Map, |
9
|
|
|
Str, |
10
|
|
|
Sequence, |
11
|
|
|
}; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class SetTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testInterface() |
17
|
|
|
{ |
18
|
|
|
$this->assertInstanceOf(\Countable::class, Set::of()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testOf() |
22
|
|
|
{ |
23
|
|
|
$this->assertTrue( |
24
|
|
|
Set::of(1, 1, 2, 3)->equals( |
|
|
|
|
25
|
|
|
Set::of() |
26
|
|
|
->add(1) |
27
|
|
|
->add(2) |
28
|
|
|
->add(3), |
29
|
|
|
), |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testDefer() |
34
|
|
|
{ |
35
|
|
|
$loaded = false; |
36
|
|
|
$set = Set::defer((static function() use (&$loaded) { |
37
|
|
|
yield 1; |
38
|
|
|
yield 2; |
39
|
|
|
yield 3; |
40
|
|
|
$loaded = true; |
41
|
|
|
})()); |
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf(Set::class, $set); |
44
|
|
|
$this->assertFalse($loaded); |
45
|
|
|
$this->assertSame([1, 2, 3], $set->toList()); |
46
|
|
|
$this->assertTrue($loaded); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testLazy() |
50
|
|
|
{ |
51
|
|
|
$loaded = false; |
52
|
|
|
$set = Set::lazy(static function() use (&$loaded) { |
53
|
|
|
yield 1; |
54
|
|
|
yield 2; |
55
|
|
|
yield 3; |
56
|
|
|
$loaded = true; |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->assertInstanceOf(Set::class, $set); |
60
|
|
|
$this->assertFalse($loaded); |
61
|
|
|
$this->assertSame([1, 2, 3], $set->toList()); |
62
|
|
|
$this->assertTrue($loaded); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testMixed() |
66
|
|
|
{ |
67
|
|
|
$set = Set::mixed(1, '2', 3, 1); |
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf(Set::class, $set); |
70
|
|
|
$this->assertSame([1, '2', 3], $set->toList()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInts() |
74
|
|
|
{ |
75
|
|
|
$set = Set::ints(1, 2, 3, 1); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf(Set::class, $set); |
78
|
|
|
$this->assertSame([1, 2, 3], $set->toList()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testFloats() |
82
|
|
|
{ |
83
|
|
|
$set = Set::floats(1, 2, 3.2, 1); |
84
|
|
|
|
85
|
|
|
$this->assertInstanceOf(Set::class, $set); |
86
|
|
|
$this->assertSame([1.0, 2.0, 3.2], $set->toList()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testStrings() |
90
|
|
|
{ |
91
|
|
|
$set = Set::strings('1', '2', '3', '1'); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(Set::class, $set); |
94
|
|
|
$this->assertSame(['1', '2', '3'], $set->toList()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testObjects() |
98
|
|
|
{ |
99
|
|
|
$a = new \stdClass; |
100
|
|
|
$b = new \stdClass; |
101
|
|
|
$c = new \stdClass; |
102
|
|
|
$set = Set::objects($a, $b, $c, $a); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf(Set::class, $set); |
105
|
|
|
$this->assertSame([$a, $b, $c], $set->toList()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testAdd() |
109
|
|
|
{ |
110
|
|
|
$this->assertSame(0, Set::of()->size()); |
111
|
|
|
|
112
|
|
|
$s = Set::of()->add(42); |
113
|
|
|
|
114
|
|
|
$this->assertSame(1, $s->size()); |
115
|
|
|
$this->assertSame(1, $s->count()); |
116
|
|
|
$s->add(24); |
117
|
|
|
$this->assertSame(1, $s->size()); |
118
|
|
|
$s = $s->add(24); |
119
|
|
|
$this->assertInstanceOf(Set::class, $s); |
120
|
|
|
$this->assertSame(2, $s->size()); |
121
|
|
|
$s = $s->add(24); |
122
|
|
|
$this->assertSame(2, $s->size()); |
123
|
|
|
$this->assertSame([42, 24], $s->toList()); |
124
|
|
|
|
125
|
|
|
$this->assertSame( |
126
|
|
|
[1, 2, 3], |
127
|
|
|
Set::ints(1)(2)(3)->toList(), |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testIntersect() |
132
|
|
|
{ |
133
|
|
|
$s = Set::of() |
134
|
|
|
->add(24) |
135
|
|
|
->add(42) |
136
|
|
|
->add(66); |
137
|
|
|
|
138
|
|
|
$s2 = $s->intersect(Set::of()->add(42)); |
139
|
|
|
$this->assertNotSame($s, $s2); |
140
|
|
|
$this->assertInstanceOf(Set::class, $s2); |
141
|
|
|
$this->assertSame([24, 42, 66], $s->toList()); |
142
|
|
|
$this->assertSame([42], $s2->toList()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testContains() |
146
|
|
|
{ |
147
|
|
|
$s = Set::of(); |
148
|
|
|
|
149
|
|
|
$this->assertFalse($s->contains(42)); |
150
|
|
|
$s = $s->add(42); |
151
|
|
|
$this->assertTrue($s->contains(42)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testRemove() |
155
|
|
|
{ |
156
|
|
|
$s = Set::of() |
157
|
|
|
->add(24) |
158
|
|
|
->add(42) |
159
|
|
|
->add(66) |
160
|
|
|
->add(90) |
161
|
|
|
->add(114); |
162
|
|
|
|
163
|
|
|
$s2 = $s->remove(42); |
164
|
|
|
$this->assertNotSame($s, $s2); |
165
|
|
|
$this->assertInstanceOf(Set::class, $s2); |
166
|
|
|
$this->assertSame([24, 42, 66, 90, 114], $s->toList()); |
167
|
|
|
$this->assertSame([24, 66, 90, 114], $s2->toList()); |
168
|
|
|
$this->assertSame([42, 66, 90, 114], $s->remove(24)->toList()); |
169
|
|
|
$this->assertSame([24, 42, 90, 114], $s->remove(66)->toList()); |
170
|
|
|
$this->assertSame([24, 42, 66, 114], $s->remove(90)->toList()); |
171
|
|
|
$this->assertSame([24, 42, 66, 90], $s->remove(114)->toList()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testDiff() |
175
|
|
|
{ |
176
|
|
|
$s = Set::of() |
177
|
|
|
->add(24) |
178
|
|
|
->add(42) |
179
|
|
|
->add(66); |
180
|
|
|
|
181
|
|
|
$s2 = $s->diff(Set::of()->add(42)); |
182
|
|
|
$this->assertNotSame($s, $s2); |
183
|
|
|
$this->assertInstanceOf(Set::class, $s2); |
184
|
|
|
$this->assertSame([24, 42, 66], $s->toList()); |
185
|
|
|
$this->assertSame([24, 66], $s2->toList()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testEquals() |
189
|
|
|
{ |
190
|
|
|
$s = Set::of() |
191
|
|
|
->add(24) |
192
|
|
|
->add(42) |
193
|
|
|
->add(66); |
194
|
|
|
|
195
|
|
|
$this->assertTrue( |
196
|
|
|
$s->equals( |
197
|
|
|
Set::of() |
198
|
|
|
->add(24) |
199
|
|
|
->add(66) |
200
|
|
|
->add(42), |
201
|
|
|
), |
202
|
|
|
); |
203
|
|
|
$this->assertTrue(Set::of()->equals(Set::of())); |
204
|
|
|
$this->assertFalse( |
205
|
|
|
$s->equals( |
206
|
|
|
Set::of() |
207
|
|
|
->add(24) |
208
|
|
|
->add(66), |
209
|
|
|
), |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testFilter() |
214
|
|
|
{ |
215
|
|
|
$s = Set::of() |
216
|
|
|
->add(1) |
217
|
|
|
->add(2) |
218
|
|
|
->add(3) |
219
|
|
|
->add(4); |
220
|
|
|
|
221
|
|
|
$s2 = $s->filter(static function(int $v) { |
222
|
|
|
return $v % 2 === 0; |
223
|
|
|
}); |
224
|
|
|
$this->assertNotSame($s, $s2); |
225
|
|
|
$this->assertInstanceOf(Set::class, $s2); |
226
|
|
|
$this->assertSame([1, 2, 3, 4], $s->toList()); |
227
|
|
|
$this->assertSame([2, 4], $s2->toList()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testForeach() |
231
|
|
|
{ |
232
|
|
|
$s = Set::of() |
233
|
|
|
->add(1) |
234
|
|
|
->add(2) |
235
|
|
|
->add(3) |
236
|
|
|
->add(4); |
237
|
|
|
$count = 0; |
238
|
|
|
|
239
|
|
|
$s->foreach(function(int $v) use (&$count) { |
240
|
|
|
$this->assertSame(++$count, $v); |
241
|
|
|
}); |
242
|
|
|
$this->assertSame(4, $count); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function testGroupBy() |
246
|
|
|
{ |
247
|
|
|
$s = Set::of() |
248
|
|
|
->add(1) |
249
|
|
|
->add(2) |
250
|
|
|
->add(3) |
251
|
|
|
->add(4); |
252
|
|
|
|
253
|
|
|
$m = $s->groupBy(static function(int $v) { |
254
|
|
|
return $v % 2; |
255
|
|
|
}); |
256
|
|
|
$this->assertInstanceOf(Map::class, $m); |
257
|
|
|
$this->assertSame([1, 0], $m->keys()->toList()); |
258
|
|
|
$this->assertSame([1, 3], $this->get($m, 1)->toList()); |
259
|
|
|
$this->assertSame([2, 4], $this->get($m, 0)->toList()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testMap() |
263
|
|
|
{ |
264
|
|
|
$s = Set::of() |
265
|
|
|
->add(1) |
266
|
|
|
->add(2) |
267
|
|
|
->add(3) |
268
|
|
|
->add(4); |
269
|
|
|
|
270
|
|
|
$s2 = $s->map(static function(int $v) { |
271
|
|
|
return $v**2; |
272
|
|
|
}); |
273
|
|
|
$this->assertNotSame($s, $s2); |
274
|
|
|
$this->assertInstanceOf(Set::class, $s2); |
275
|
|
|
$this->assertSame([1, 2, 3, 4], $s->toList()); |
276
|
|
|
$this->assertSame([1, 4, 9, 16], $s2->toList()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testFlatMap() |
280
|
|
|
{ |
281
|
|
|
$set = Set::of(1, 2, 3, 4); |
|
|
|
|
282
|
|
|
$set2 = $set->flatMap(static fn($i) => Set::of($i, $i + 2)); |
|
|
|
|
283
|
|
|
|
284
|
|
|
$this->assertNotSame($set, $set2); |
285
|
|
|
$this->assertSame([1, 2, 3, 4], $set->toList()); |
286
|
|
|
$this->assertSame([1, 3, 2, 4, 5, 6], $set2->toList()); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function testPartition() |
290
|
|
|
{ |
291
|
|
|
$s = Set::of() |
292
|
|
|
->add(1) |
293
|
|
|
->add(2) |
294
|
|
|
->add(3) |
295
|
|
|
->add(4); |
296
|
|
|
|
297
|
|
|
$s2 = $s->partition(static function(int $v) { |
298
|
|
|
return $v % 2 === 0; |
299
|
|
|
}); |
300
|
|
|
$this->assertNotSame($s, $s2); |
301
|
|
|
$this->assertInstanceOf(Map::class, $s2); |
302
|
|
|
$this->assertSame([1, 2, 3, 4], $s->toList()); |
303
|
|
|
$this->assertInstanceOf(Set::class, $this->get($s2, true)); |
304
|
|
|
$this->assertInstanceOf(Set::class, $this->get($s2, false)); |
305
|
|
|
$this->assertSame([2, 4], $this->get($s2, true)->toList()); |
306
|
|
|
$this->assertSame([1, 3], $this->get($s2, false)->toList()); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function testSort() |
310
|
|
|
{ |
311
|
|
|
$s = Set::of() |
312
|
|
|
->add(1) |
313
|
|
|
->add(2) |
314
|
|
|
->add(3) |
315
|
|
|
->add(4); |
316
|
|
|
|
317
|
|
|
$s2 = $s->sort(static function(int $a, int $b) { |
318
|
|
|
return ($a < $b) ? 1 : -1; |
319
|
|
|
}); |
320
|
|
|
$this->assertInstanceOf(Sequence::class, $s2); |
321
|
|
|
$this->assertSame([1, 2, 3, 4], $s->toList()); |
322
|
|
|
$this->assertSame([4, 3, 2, 1], $s2->toList()); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function testMerge() |
326
|
|
|
{ |
327
|
|
|
$s = Set::of() |
328
|
|
|
->add(24) |
329
|
|
|
->add(42) |
330
|
|
|
->add(66); |
331
|
|
|
|
332
|
|
|
$this->assertTrue( |
333
|
|
|
$s |
334
|
|
|
->merge( |
335
|
|
|
Set::of() |
336
|
|
|
->add(24) |
337
|
|
|
->add(42) |
338
|
|
|
->add(66), |
339
|
|
|
) |
340
|
|
|
->equals($s), |
341
|
|
|
); |
342
|
|
|
$this->assertSame( |
343
|
|
|
[24, 42, 66, 90, 114], |
344
|
|
|
$s->merge( |
345
|
|
|
Set::of() |
346
|
|
|
->add(90) |
347
|
|
|
->add(114), |
348
|
|
|
)->toList(), |
349
|
|
|
); |
350
|
|
|
$this->assertSame([24, 42, 66], $s->toList()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function testReduce() |
354
|
|
|
{ |
355
|
|
|
$s = Set::of() |
356
|
|
|
->add(4) |
357
|
|
|
->add(3) |
358
|
|
|
->add(2); |
359
|
|
|
|
360
|
|
|
$v = $s->reduce( |
361
|
|
|
42, |
|
|
|
|
362
|
|
|
static function(float $carry, int $value): float { |
363
|
|
|
return $carry / $value; |
364
|
|
|
}, |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
$this->assertSame(1.75, $v); |
368
|
|
|
$this->assertSame([4, 3, 2], $s->toList()); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testVariableSet() |
372
|
|
|
{ |
373
|
|
|
$this->assertSame( |
374
|
|
|
['foo', 42, 42.1, true, []], |
375
|
|
|
Set::of() |
376
|
|
|
->add('foo') |
377
|
|
|
->add(42) |
378
|
|
|
->add(42.1) |
379
|
|
|
->add(true) |
380
|
|
|
->add([]) |
381
|
|
|
->toList(), |
382
|
|
|
); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function testEmpty() |
386
|
|
|
{ |
387
|
|
|
$this->assertTrue(Set::of()->empty()); |
388
|
|
|
$this->assertFalse(Set::of(1)->empty()); |
|
|
|
|
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function testFind() |
392
|
|
|
{ |
393
|
|
|
$sequence = Set::ints(1, 2, 3); |
394
|
|
|
|
395
|
|
|
$this->assertSame( |
396
|
|
|
1, |
397
|
|
|
$sequence->find(static fn($i) => $i === 1)->match( |
398
|
|
|
static fn($i) => $i, |
399
|
|
|
static fn() => null, |
400
|
|
|
), |
401
|
|
|
); |
402
|
|
|
$this->assertSame( |
403
|
|
|
2, |
404
|
|
|
$sequence->find(static fn($i) => $i === 2)->match( |
405
|
|
|
static fn($i) => $i, |
406
|
|
|
static fn() => null, |
407
|
|
|
), |
408
|
|
|
); |
409
|
|
|
$this->assertSame( |
410
|
|
|
3, |
411
|
|
|
$sequence->find(static fn($i) => $i === 3)->match( |
412
|
|
|
static fn($i) => $i, |
413
|
|
|
static fn() => null, |
414
|
|
|
), |
415
|
|
|
); |
416
|
|
|
|
417
|
|
|
$this->assertNull( |
418
|
|
|
$sequence->find(static fn($i) => $i === 0)->match( |
419
|
|
|
static fn($i) => $i, |
420
|
|
|
static fn() => null, |
421
|
|
|
), |
422
|
|
|
); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function testMatches() |
426
|
|
|
{ |
427
|
|
|
$set = Set::ints(1, 2, 3); |
428
|
|
|
|
429
|
|
|
$this->assertTrue($set->matches(static fn($i) => $i % 1 === 0)); |
430
|
|
|
$this->assertFalse($set->matches(static fn($i) => $i % 2 === 0)); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
public function testAny() |
434
|
|
|
{ |
435
|
|
|
$set = Set::ints(1, 2, 3); |
436
|
|
|
|
437
|
|
|
$this->assertTrue($set->any(static fn($i) => $i === 2)); |
438
|
|
|
$this->assertFalse($set->any(static fn($i) => $i === 0)); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
public function testToList() |
442
|
|
|
{ |
443
|
|
|
$this->assertSame( |
444
|
|
|
[1, 2, 3], |
445
|
|
|
Set::ints(1, 2, 3)->toList(), |
446
|
|
|
); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
public function testPossibilityToCleanupResourcesWhenGeneratorStoppedBeforeEnd() |
450
|
|
|
{ |
451
|
|
|
$cleanupCalled = false; |
452
|
|
|
$endReached = false; |
453
|
|
|
$started = 0; |
454
|
|
|
$set = Set::lazy(static function($registerCleanup) use (&$cleanupCalled, &$endReached, &$started) { |
455
|
|
|
++$started; |
456
|
|
|
$file = \fopen(__FILE__, 'r'); |
457
|
|
|
$registerCleanup(static function() use ($file, &$cleanupCalled) { |
458
|
|
|
\fclose($file); |
459
|
|
|
$cleanupCalled = true; |
460
|
|
|
}); |
461
|
|
|
|
462
|
|
|
while (!\feof($file)) { |
463
|
|
|
$line = \fgets($file); |
464
|
|
|
|
465
|
|
|
yield $line; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
$endReached = true; |
469
|
|
|
\fclose($file); |
470
|
|
|
}); |
471
|
|
|
|
472
|
|
|
$line = $set |
473
|
|
|
->map(static fn($line) => \trim($line)) |
474
|
|
|
->filter(static fn($line) => $line !== '') |
475
|
|
|
->find(static fn($line) => \substr($line, -2) === '()') |
476
|
|
|
->match( |
477
|
|
|
static fn($line) => $line, |
478
|
|
|
static fn() => null, |
479
|
|
|
); |
480
|
|
|
|
481
|
|
|
$this->assertSame('public function testInterface()', $line); |
482
|
|
|
$this->assertSame(1, $started); |
483
|
|
|
$this->assertTrue($cleanupCalled); |
484
|
|
|
$this->assertFalse($endReached); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public function testCleanupIsNotCalledWhenReachingTheEndOfTheGenerator() |
488
|
|
|
{ |
489
|
|
|
$cleanupCalled = false; |
490
|
|
|
$endReached = false; |
491
|
|
|
$started = 0; |
492
|
|
|
$set = Set::lazy(static function($registerCleanup) use (&$cleanupCalled, &$endReached, &$started) { |
493
|
|
|
++$started; |
494
|
|
|
$file = \fopen(__FILE__, 'r'); |
495
|
|
|
$registerCleanup(static function() use ($file, &$cleanupCalled) { |
496
|
|
|
\fclose($file); |
497
|
|
|
$cleanupCalled = true; |
498
|
|
|
}); |
499
|
|
|
|
500
|
|
|
while (!\feof($file)) { |
501
|
|
|
$line = \fgets($file); |
502
|
|
|
|
503
|
|
|
yield $line; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$endReached = true; |
507
|
|
|
\fclose($file); |
508
|
|
|
}); |
509
|
|
|
|
510
|
|
|
$line = $set |
511
|
|
|
->filter(static fn($line) => \is_string($line)) |
512
|
|
|
->map(static fn($line) => \trim($line)) |
513
|
|
|
->filter(static fn($line) => $line !== '') |
514
|
|
|
->find(static fn($line) => $line === 'unknown') |
515
|
|
|
->match( |
516
|
|
|
static fn($line) => $line, |
517
|
|
|
static fn() => null, |
518
|
|
|
); |
519
|
|
|
|
520
|
|
|
$this->assertNull($line); |
521
|
|
|
$this->assertSame(1, $started); |
522
|
|
|
$this->assertFalse($cleanupCalled); |
523
|
|
|
$this->assertTrue($endReached); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
public function get($map, $index) |
527
|
|
|
{ |
528
|
|
|
return $map->get($index)->match( |
529
|
|
|
static fn($value) => $value, |
530
|
|
|
static fn() => null, |
531
|
|
|
); |
532
|
|
|
} |
533
|
|
|
} |
534
|
|
|
|