1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Set; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Set\Lazy, |
8
|
|
|
Set\Implementation, |
9
|
|
|
Set, |
10
|
|
|
Map, |
11
|
|
|
Str, |
12
|
|
|
Sequence, |
13
|
|
|
SideEffect, |
14
|
|
|
}; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class LazyTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testInterface() |
20
|
|
|
{ |
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
Implementation::class, |
23
|
|
|
Lazy::of(static function() { |
24
|
|
|
yield; |
25
|
|
|
}), |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testSize() |
30
|
|
|
{ |
31
|
|
|
$set = Lazy::of(static function() { |
32
|
|
|
yield 1; |
33
|
|
|
yield 2; |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$this->assertSame(2, $set->size()); |
37
|
|
|
$this->assertSame(2, $set->count()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testIterator() |
41
|
|
|
{ |
42
|
|
|
$set = Lazy::of(static function() { |
43
|
|
|
yield 1; |
44
|
|
|
yield 2; |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
$this->assertSame([1, 2], \iterator_to_array($set->iterator())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testIntersect() |
51
|
|
|
{ |
52
|
|
|
$aLoaded = false; |
53
|
|
|
$bLoaded = false; |
54
|
|
|
$a = Lazy::of(static function() use (&$aLoaded) { |
55
|
|
|
yield 1; |
56
|
|
|
yield 2; |
57
|
|
|
$aLoaded = true; |
58
|
|
|
}); |
59
|
|
|
$b = Lazy::of(static function() use (&$bLoaded) { |
60
|
|
|
yield 2; |
61
|
|
|
yield 3; |
62
|
|
|
$bLoaded = true; |
63
|
|
|
}); |
64
|
|
|
$c = $a->intersect($b); |
65
|
|
|
|
66
|
|
|
$this->assertFalse($aLoaded); |
67
|
|
|
$this->assertFalse($bLoaded); |
68
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
69
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
70
|
|
|
$this->assertInstanceOf(Lazy::class, $c); |
71
|
|
|
$this->assertSame([2], \iterator_to_array($c->iterator())); |
72
|
|
|
$this->assertTrue($aLoaded); |
73
|
|
|
$this->assertTrue($bLoaded); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testAdd() |
77
|
|
|
{ |
78
|
|
|
$loaded = false; |
79
|
|
|
$a = Lazy::of(static function() use (&$loaded) { |
80
|
|
|
yield 1; |
81
|
|
|
$loaded = true; |
82
|
|
|
}); |
83
|
|
|
$b = ($a)(2); |
84
|
|
|
|
85
|
|
|
$this->assertFalse($loaded); |
86
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
87
|
|
|
$this->assertInstanceOf(Lazy::class, $b); |
88
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
89
|
|
|
$this->assertSame([1, 2], \iterator_to_array(($b)(2)->iterator())); |
90
|
|
|
$this->assertTrue($loaded); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testContains() |
94
|
|
|
{ |
95
|
|
|
$set = Lazy::of(static function() { |
96
|
|
|
yield 1; |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$this->assertTrue($set->contains(1)); |
|
|
|
|
100
|
|
|
$this->assertFalse($set->contains(2)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testRemove() |
104
|
|
|
{ |
105
|
|
|
$a = Lazy::of(static function() { |
106
|
|
|
yield 1; |
107
|
|
|
yield 2; |
108
|
|
|
yield 3; |
109
|
|
|
yield 4; |
110
|
|
|
}); |
111
|
|
|
$b = $a->remove(3); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
114
|
|
|
$this->assertInstanceOf(Lazy::class, $b); |
115
|
|
|
$this->assertSame([1, 2, 4], \iterator_to_array($b->iterator())); |
116
|
|
|
$this->assertSame($a, $a->remove(5)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testDiff() |
120
|
|
|
{ |
121
|
|
|
$aLoaded = false; |
122
|
|
|
$a = Lazy::of(static function() use (&$aLoaded) { |
123
|
|
|
yield 1; |
124
|
|
|
yield 2; |
125
|
|
|
yield 3; |
126
|
|
|
$aLoaded = true; |
127
|
|
|
}); |
128
|
|
|
$bLoaded = false; |
129
|
|
|
$b = Lazy::of(static function() use (&$bLoaded) { |
130
|
|
|
yield 2; |
131
|
|
|
yield 4; |
132
|
|
|
$bLoaded = true; |
133
|
|
|
}); |
134
|
|
|
$c = $a->diff($b); |
135
|
|
|
|
136
|
|
|
$this->assertFalse($aLoaded); |
137
|
|
|
$this->assertFalse($bLoaded); |
138
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
139
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
140
|
|
|
$this->assertInstanceOf(Lazy::class, $c); |
141
|
|
|
$this->assertSame([1, 3], \iterator_to_array($c->iterator())); |
142
|
|
|
$this->assertTrue($aLoaded); |
143
|
|
|
$this->assertTrue($bLoaded); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testEquals() |
147
|
|
|
{ |
148
|
|
|
$a = Lazy::of(static function() { |
149
|
|
|
yield 1; |
150
|
|
|
yield 2; |
151
|
|
|
}); |
152
|
|
|
$aBis = Lazy::of(static function() { |
153
|
|
|
yield 1; |
154
|
|
|
yield 2; |
155
|
|
|
}); |
156
|
|
|
$b = Lazy::of(static function() { |
157
|
|
|
yield 1; |
158
|
|
|
}); |
159
|
|
|
$c = Lazy::of(static function() { |
160
|
|
|
yield 1; |
161
|
|
|
yield 2; |
162
|
|
|
yield 3; |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
$this->assertTrue($a->equals($a)); |
166
|
|
|
$this->assertTrue($a->equals($aBis)); |
167
|
|
|
$this->assertFalse($a->equals($b)); |
168
|
|
|
$this->assertFalse($a->equals($c)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testFilter() |
172
|
|
|
{ |
173
|
|
|
$loaded = false; |
174
|
|
|
$a = Lazy::of(static function() use (&$loaded) { |
175
|
|
|
yield 1; |
176
|
|
|
yield 2; |
177
|
|
|
yield 3; |
178
|
|
|
yield 4; |
179
|
|
|
$loaded = true; |
180
|
|
|
}); |
181
|
|
|
$b = $a->filter(static fn($i) => $i % 2 === 0); |
182
|
|
|
|
183
|
|
|
$this->assertFalse($loaded); |
184
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
185
|
|
|
$this->assertInstanceOf(Lazy::class, $b); |
186
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
187
|
|
|
$this->assertTrue($loaded); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testForeach() |
191
|
|
|
{ |
192
|
|
|
$set = Lazy::of(static function() { |
193
|
|
|
yield 1; |
194
|
|
|
yield 2; |
195
|
|
|
yield 3; |
196
|
|
|
yield 4; |
197
|
|
|
}); |
198
|
|
|
$calls = 0; |
199
|
|
|
$sum = 0; |
200
|
|
|
|
201
|
|
|
$this->assertInstanceOf( |
202
|
|
|
SideEffect::class, |
203
|
|
|
$set->foreach(static function($i) use (&$calls, &$sum) { |
204
|
|
|
++$calls; |
205
|
|
|
$sum += $i; |
206
|
|
|
}), |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
$this->assertSame(4, $calls); |
210
|
|
|
$this->assertSame(10, $sum); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testGroupBy() |
214
|
|
|
{ |
215
|
|
|
$set = Lazy::of(static function() { |
216
|
|
|
yield 1; |
217
|
|
|
yield 2; |
218
|
|
|
yield 3; |
219
|
|
|
yield 4; |
220
|
|
|
}); |
221
|
|
|
$groups = $set->groupBy(static fn($i) => $i % 2); |
222
|
|
|
|
223
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
224
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
225
|
|
|
$this->assertCount(2, $groups); |
226
|
|
|
$this->assertSame([2, 4], $this->get($groups, 0)->toList()); |
227
|
|
|
$this->assertSame([1, 3], $this->get($groups, 1)->toList()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testMap() |
231
|
|
|
{ |
232
|
|
|
$loaded = false; |
233
|
|
|
$a = Lazy::of(static function() use (&$loaded) { |
234
|
|
|
yield 1; |
235
|
|
|
yield 2; |
236
|
|
|
yield 3; |
237
|
|
|
$loaded = true; |
238
|
|
|
}); |
239
|
|
|
$b = $a->map(static fn($i) => $i * 2); |
240
|
|
|
|
241
|
|
|
$this->assertFalse($loaded); |
242
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
243
|
|
|
$this->assertInstanceOf(Lazy::class, $b); |
244
|
|
|
$this->assertSame([2, 4, 6], \iterator_to_array($b->iterator())); |
245
|
|
|
$this->assertTrue($loaded); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testMapDoesntIntroduceDuplicates() |
249
|
|
|
{ |
250
|
|
|
$set = Lazy::of(static function() { |
251
|
|
|
yield 1; |
252
|
|
|
yield 2; |
253
|
|
|
yield 3; |
254
|
|
|
}); |
255
|
|
|
|
256
|
|
|
$this->assertSame( |
257
|
|
|
[1], |
258
|
|
|
\iterator_to_array($set->map(static fn() => 1)->iterator()), |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testPartition() |
263
|
|
|
{ |
264
|
|
|
$set = Lazy::of(static function() { |
265
|
|
|
yield 1; |
266
|
|
|
yield 2; |
267
|
|
|
yield 3; |
268
|
|
|
yield 4; |
269
|
|
|
}); |
270
|
|
|
$groups = $set->partition(static fn($i) => $i % 2 === 0); |
271
|
|
|
|
272
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
273
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
274
|
|
|
$this->assertCount(2, $groups); |
275
|
|
|
$this->assertSame([2, 4], $this->get($groups, true)->toList()); |
276
|
|
|
$this->assertSame([1, 3], $this->get($groups, false)->toList()); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testSort() |
280
|
|
|
{ |
281
|
|
|
$loaded = false; |
282
|
|
|
$set = Lazy::of(static function() use (&$loaded) { |
283
|
|
|
yield 1; |
284
|
|
|
yield 4; |
285
|
|
|
yield 3; |
286
|
|
|
yield 2; |
287
|
|
|
$loaded = true; |
288
|
|
|
}); |
289
|
|
|
$sorted = $set->sort(static fn($a, $b) => $a > $b ? 1 : -1); |
290
|
|
|
|
291
|
|
|
$this->assertFalse($loaded); |
292
|
|
|
$this->assertSame([1, 4, 3, 2], \iterator_to_array($set->iterator())); |
293
|
|
|
$this->assertInstanceOf(Sequence::class, $sorted); |
294
|
|
|
$this->assertSame([1, 2, 3, 4], $sorted->toList()); |
295
|
|
|
$this->assertTrue($loaded); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testMerge() |
299
|
|
|
{ |
300
|
|
|
$aLoaded = false; |
301
|
|
|
$a = Lazy::of(static function() use (&$aLoaded) { |
302
|
|
|
yield 1; |
303
|
|
|
yield 2; |
304
|
|
|
$aLoaded = true; |
305
|
|
|
}); |
306
|
|
|
$bLoaded = false; |
307
|
|
|
$b = Lazy::of(static function() use (&$bLoaded) { |
308
|
|
|
yield 2; |
309
|
|
|
yield 3; |
310
|
|
|
$bLoaded = true; |
311
|
|
|
}); |
312
|
|
|
$c = $a->merge($b); |
313
|
|
|
|
314
|
|
|
$this->assertFalse($aLoaded); |
315
|
|
|
$this->assertFalse($bLoaded); |
316
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
317
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
318
|
|
|
$this->assertInstanceOf(Lazy::class, $c); |
319
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($c->iterator())); |
320
|
|
|
$this->assertTrue($aLoaded); |
321
|
|
|
$this->assertTrue($bLoaded); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testReduce() |
325
|
|
|
{ |
326
|
|
|
$set = Lazy::of(static function() { |
327
|
|
|
yield 1; |
328
|
|
|
yield 2; |
329
|
|
|
yield 3; |
330
|
|
|
yield 4; |
331
|
|
|
}); |
332
|
|
|
|
333
|
|
|
$this->assertSame(10, $set->reduce(0, static fn($sum, $i) => $sum + $i)); |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function testClear() |
337
|
|
|
{ |
338
|
|
|
$a = Lazy::of(static function() { |
339
|
|
|
yield 1; |
340
|
|
|
}); |
341
|
|
|
$b = $a->clear(); |
342
|
|
|
|
343
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
344
|
|
|
$this->assertInstanceOf(Implementation::class, $b); |
345
|
|
|
$this->assertSame([], \iterator_to_array($b->iterator())); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function testEmpty() |
349
|
|
|
{ |
350
|
|
|
$a = Lazy::of(static function() { |
351
|
|
|
yield 1; |
352
|
|
|
}); |
353
|
|
|
$b = Lazy::of(static function() { |
354
|
|
|
if (false) { |
355
|
|
|
yield 1; |
356
|
|
|
} |
357
|
|
|
}); |
358
|
|
|
|
359
|
|
|
$this->assertTrue($b->empty()); |
360
|
|
|
$this->assertFalse($a->empty()); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function testFind() |
364
|
|
|
{ |
365
|
|
|
$count = 0; |
366
|
|
|
$sequence = Lazy::of(static function() use (&$count) { |
367
|
|
|
++$count; |
368
|
|
|
yield 1; |
369
|
|
|
++$count; |
370
|
|
|
yield 2; |
371
|
|
|
++$count; |
372
|
|
|
yield 3; |
373
|
|
|
}); |
374
|
|
|
|
375
|
|
|
$this->assertSame( |
376
|
|
|
1, |
377
|
|
|
$sequence->find(static fn($i) => $i === 1)->match( |
378
|
|
|
static fn($i) => $i, |
379
|
|
|
static fn() => null, |
380
|
|
|
), |
381
|
|
|
); |
382
|
|
|
$this->assertSame(1, $count); |
383
|
|
|
$this->assertSame( |
384
|
|
|
2, |
385
|
|
|
$sequence->find(static fn($i) => $i === 2)->match( |
386
|
|
|
static fn($i) => $i, |
387
|
|
|
static fn() => null, |
388
|
|
|
), |
389
|
|
|
); |
390
|
|
|
$this->assertSame(3, $count); |
391
|
|
|
$this->assertSame( |
392
|
|
|
3, |
393
|
|
|
$sequence->find(static fn($i) => $i === 3)->match( |
394
|
|
|
static fn($i) => $i, |
395
|
|
|
static fn() => null, |
396
|
|
|
), |
397
|
|
|
); |
398
|
|
|
$this->assertSame(6, $count); |
399
|
|
|
|
400
|
|
|
$this->assertNull( |
401
|
|
|
$sequence->find(static fn($i) => $i === 0)->match( |
402
|
|
|
static fn($i) => $i, |
403
|
|
|
static fn() => null, |
404
|
|
|
), |
405
|
|
|
); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function testCallCleanupWhenElementBeingLookedForIsFoundBeforeTheEndOfGenerator() |
409
|
|
|
{ |
410
|
|
|
$cleanupCalled = false; |
411
|
|
|
$sequence = Lazy::of(static function($registerCleanup) use (&$cleanupCalled) { |
412
|
|
|
$registerCleanup(static function() use (&$cleanupCalled) { |
413
|
|
|
$cleanupCalled = true; |
414
|
|
|
}); |
415
|
|
|
yield 2; |
416
|
|
|
yield 3; |
417
|
|
|
yield 4; |
418
|
|
|
yield 5; |
419
|
|
|
}); |
420
|
|
|
|
421
|
|
|
$this->assertFalse($cleanupCalled); |
422
|
|
|
$this->assertTrue($sequence->contains(3)); |
|
|
|
|
423
|
|
|
$this->assertTrue($cleanupCalled); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
public function testCallCleanupWhenFindingElementBeforeTheEndOfGenerator() |
427
|
|
|
{ |
428
|
|
|
$cleanupCalled = false; |
429
|
|
|
$sequence = Lazy::of(static function($registerCleanup) use (&$cleanupCalled) { |
430
|
|
|
$registerCleanup(static function() use (&$cleanupCalled) { |
431
|
|
|
$cleanupCalled = true; |
432
|
|
|
}); |
433
|
|
|
yield 2; |
434
|
|
|
yield 3; |
435
|
|
|
yield 4; |
436
|
|
|
yield 5; |
437
|
|
|
}); |
438
|
|
|
$this->assertFalse($cleanupCalled); |
439
|
|
|
$value = $sequence->find(static fn($value) => $value === 3)->match( |
440
|
|
|
static fn($value) => $value, |
441
|
|
|
static fn() => null, |
442
|
|
|
); |
443
|
|
|
|
444
|
|
|
$this->assertSame(3, $value); |
445
|
|
|
$this->assertTrue($cleanupCalled); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function get($map, $index) |
449
|
|
|
{ |
450
|
|
|
return $map->get($index)->match( |
451
|
|
|
static fn($value) => $value, |
452
|
|
|
static fn() => null, |
453
|
|
|
); |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
|