1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Set; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Set\Primitive, |
8
|
|
|
Set\Implementation, |
9
|
|
|
Set, |
10
|
|
|
Map, |
11
|
|
|
Str, |
12
|
|
|
Sequence, |
13
|
|
|
SideEffect, |
14
|
|
|
}; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class PrimitiveTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testInterface() |
20
|
|
|
{ |
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
Implementation::class, |
23
|
|
|
Primitive::of(), |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testSize() |
28
|
|
|
{ |
29
|
|
|
$this->assertSame(2, (Primitive::of(1, 2))->size()); |
|
|
|
|
30
|
|
|
$this->assertSame(2, (Primitive::of(1, 2))->count()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testIterator() |
34
|
|
|
{ |
35
|
|
|
$this->assertSame([1, 2], \iterator_to_array((Primitive::of(1, 2))->iterator())); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testIntersect() |
39
|
|
|
{ |
40
|
|
|
$a = Primitive::of(1, 2); |
|
|
|
|
41
|
|
|
$b = Primitive::of(2, 3); |
42
|
|
|
$c = $a->intersect($b); |
43
|
|
|
|
44
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
45
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
46
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
47
|
|
|
$this->assertSame([2], \iterator_to_array($c->iterator())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAdd() |
51
|
|
|
{ |
52
|
|
|
$a = Primitive::of(1); |
|
|
|
|
53
|
|
|
$b = ($a)(2); |
54
|
|
|
|
55
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
56
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
57
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
58
|
|
|
$this->assertSame($b, ($b)(2)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testContains() |
62
|
|
|
{ |
63
|
|
|
$set = Primitive::of(1); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->assertTrue($set->contains(1)); |
|
|
|
|
66
|
|
|
$this->assertFalse($set->contains(2)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testRemove() |
70
|
|
|
{ |
71
|
|
|
$a = Primitive::of(1, 2, 3, 4); |
|
|
|
|
72
|
|
|
$b = $a->remove(3); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
75
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
76
|
|
|
$this->assertSame([1, 2, 4], \iterator_to_array($b->iterator())); |
77
|
|
|
$this->assertSame($a, $a->remove(5)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testDiff() |
81
|
|
|
{ |
82
|
|
|
$a = Primitive::of(1, 2, 3); |
|
|
|
|
83
|
|
|
$b = Primitive::of(2, 4); |
84
|
|
|
$c = $a->diff($b); |
85
|
|
|
|
86
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
87
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
88
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
89
|
|
|
$this->assertSame([1, 3], \iterator_to_array($c->iterator())); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testEquals() |
93
|
|
|
{ |
94
|
|
|
$this->assertTrue((Primitive::of(1, 2))->equals(Primitive::of(1, 2))); |
|
|
|
|
95
|
|
|
$this->assertFalse((Primitive::of(1, 2))->equals(Primitive::of(1))); |
96
|
|
|
$this->assertFalse((Primitive::of(1, 2))->equals(Primitive::of(1, 2, 3))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testFilter() |
100
|
|
|
{ |
101
|
|
|
$a = Primitive::of(1, 2, 3, 4); |
|
|
|
|
102
|
|
|
$b = $a->filter(static fn($i) => $i % 2 === 0); |
103
|
|
|
|
104
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
105
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
106
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testForeach() |
110
|
|
|
{ |
111
|
|
|
$set = Primitive::of(1, 2, 3, 4); |
|
|
|
|
112
|
|
|
$calls = 0; |
113
|
|
|
$sum = 0; |
114
|
|
|
|
115
|
|
|
$this->assertInstanceOf( |
116
|
|
|
SideEffect::class, |
117
|
|
|
$set->foreach(static function($i) use (&$calls, &$sum) { |
118
|
|
|
++$calls; |
119
|
|
|
$sum += $i; |
120
|
|
|
}), |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->assertSame(4, $calls); |
124
|
|
|
$this->assertSame(10, $sum); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGroupBy() |
128
|
|
|
{ |
129
|
|
|
$set = Primitive::of(1, 2, 3, 4); |
|
|
|
|
130
|
|
|
$groups = $set->groupBy(static fn($i) => $i % 2); |
131
|
|
|
|
132
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
133
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
134
|
|
|
$this->assertCount(2, $groups); |
135
|
|
|
$this->assertSame([2, 4], $this->get($groups, 0)->toList()); |
136
|
|
|
$this->assertSame([1, 3], $this->get($groups, 1)->toList()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testMap() |
140
|
|
|
{ |
141
|
|
|
$a = Primitive::of(1, 2, 3); |
|
|
|
|
142
|
|
|
$b = $a->map(static fn($i) => $i * 2); |
143
|
|
|
|
144
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
145
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
146
|
|
|
$this->assertSame([2, 4, 6], \iterator_to_array($b->iterator())); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testPartition() |
150
|
|
|
{ |
151
|
|
|
$set = Primitive::of(1, 2, 3, 4); |
|
|
|
|
152
|
|
|
$groups = $set->partition(static fn($i) => $i % 2 === 0); |
153
|
|
|
|
154
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
155
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
156
|
|
|
$this->assertCount(2, $groups); |
157
|
|
|
$this->assertSame([2, 4], $this->get($groups, true)->toList()); |
158
|
|
|
$this->assertSame([1, 3], $this->get($groups, false)->toList()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testSort() |
162
|
|
|
{ |
163
|
|
|
$set = Primitive::of(1, 4, 3, 2); |
|
|
|
|
164
|
|
|
$sorted = $set->sort(static fn($a, $b) => $a > $b ? 1 : -1); |
165
|
|
|
|
166
|
|
|
$this->assertSame([1, 4, 3, 2], \iterator_to_array($set->iterator())); |
167
|
|
|
$this->assertInstanceOf(Sequence::class, $sorted); |
168
|
|
|
$this->assertSame([1, 2, 3, 4], $sorted->toList()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testMerge() |
172
|
|
|
{ |
173
|
|
|
$a = Primitive::of(1, 2); |
|
|
|
|
174
|
|
|
$b = Primitive::of(2, 3); |
175
|
|
|
$c = $a->merge($b); |
176
|
|
|
|
177
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
178
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
179
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
180
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($c->iterator())); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testReduce() |
184
|
|
|
{ |
185
|
|
|
$set = Primitive::of(1, 2, 3, 4); |
|
|
|
|
186
|
|
|
|
187
|
|
|
$this->assertSame(10, $set->reduce(0, static fn($sum, $i) => $sum + $i)); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testClear() |
191
|
|
|
{ |
192
|
|
|
$a = Primitive::of(1); |
|
|
|
|
193
|
|
|
$b = $a->clear(); |
194
|
|
|
|
195
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
196
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
197
|
|
|
$this->assertSame([], \iterator_to_array($b->iterator())); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testEmpty() |
201
|
|
|
{ |
202
|
|
|
$this->assertTrue((Primitive::of())->empty()); |
203
|
|
|
$this->assertFalse((Primitive::of(1))->empty()); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testFind() |
207
|
|
|
{ |
208
|
|
|
$sequence = Primitive::of(1, 2, 3); |
|
|
|
|
209
|
|
|
|
210
|
|
|
$this->assertSame( |
211
|
|
|
1, |
212
|
|
|
$sequence->find(static fn($i) => $i === 1)->match( |
213
|
|
|
static fn($i) => $i, |
214
|
|
|
static fn() => null, |
215
|
|
|
), |
216
|
|
|
); |
217
|
|
|
$this->assertSame( |
218
|
|
|
2, |
219
|
|
|
$sequence->find(static fn($i) => $i === 2)->match( |
220
|
|
|
static fn($i) => $i, |
221
|
|
|
static fn() => null, |
222
|
|
|
), |
223
|
|
|
); |
224
|
|
|
$this->assertSame( |
225
|
|
|
3, |
226
|
|
|
$sequence->find(static fn($i) => $i === 3)->match( |
227
|
|
|
static fn($i) => $i, |
228
|
|
|
static fn() => null, |
229
|
|
|
), |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$this->assertNull( |
233
|
|
|
$sequence->find(static fn($i) => $i === 0)->match( |
234
|
|
|
static fn($i) => $i, |
235
|
|
|
static fn() => null, |
236
|
|
|
), |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function get($map, $index) |
241
|
|
|
{ |
242
|
|
|
return $map->get($index)->match( |
243
|
|
|
static fn($value) => $value, |
244
|
|
|
static fn() => null, |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|