|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Fixtures; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\Set as Structure; |
|
7
|
|
|
use Innmind\BlackBox\{ |
|
8
|
|
|
Set as DataSet, |
|
9
|
|
|
Random\RandomInt, |
|
10
|
|
|
}; |
|
11
|
|
|
use Fixtures\Innmind\Immutable\Set; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
class SetTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
public function testOf() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->assertInstanceOf( |
|
19
|
|
|
DataSet::class, |
|
20
|
|
|
Set::of(new DataSet\Chars), |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testGeneratesAtMost100ValuesByDefault() |
|
25
|
|
|
{ |
|
26
|
|
|
$sets = Set::of(new DataSet\Chars); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertInstanceOf(\Generator::class, $sets->values(new RandomInt)); |
|
29
|
|
|
$count = \count(\iterator_to_array($sets->values(new RandomInt))); |
|
30
|
|
|
$this->assertLessThanOrEqual(100, $count); |
|
31
|
|
|
$this->assertGreaterThan(10, $count); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($sets->values(new RandomInt) as $set) { |
|
34
|
|
|
$this->assertInstanceOf(DataSet\Value::class, $set); |
|
35
|
|
|
$this->assertInstanceOf(Structure::class, $set->unwrap()); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testGeneratesSequencesOfDifferentSizes() |
|
40
|
|
|
{ |
|
41
|
|
|
$sets = Set::of( |
|
42
|
|
|
new DataSet\Chars, |
|
43
|
|
|
DataSet\Integers::between(0, 50), |
|
44
|
|
|
); |
|
45
|
|
|
$sizes = []; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($sets->values(new RandomInt) as $set) { |
|
48
|
|
|
$sizes[] = $set->unwrap()->size(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->assertTrue(\count(\array_unique($sizes)) > 1); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testTake() |
|
55
|
|
|
{ |
|
56
|
|
|
$sets1 = Set::of(new DataSet\Chars); |
|
57
|
|
|
$sets2 = $sets1->take(50); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertNotSame($sets1, $sets2); |
|
60
|
|
|
$this->assertInstanceOf(DataSet::class, $sets2); |
|
61
|
|
|
$count1 = \count(\iterator_to_array($sets1->values(new RandomInt))); |
|
62
|
|
|
$count2 = \count(\iterator_to_array($sets2->values(new RandomInt))); |
|
63
|
|
|
$this->assertLessThanOrEqual(100, $count1); |
|
64
|
|
|
$this->assertLessThanOrEqual(50, $count2); |
|
65
|
|
|
$this->assertGreaterThan($count2, $count1); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testFilter() |
|
69
|
|
|
{ |
|
70
|
|
|
$sets = Set::of(DataSet\Chars::any()); |
|
71
|
|
|
$sets2 = $sets->filter(static fn($set) => $set->size() % 2 === 0); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertInstanceOf(DataSet::class, $sets2); |
|
74
|
|
|
$this->assertNotSame($sets, $sets2); |
|
75
|
|
|
|
|
76
|
|
|
$hasOddSet = static fn(bool $hasOddSet, $set) => $hasOddSet || $set->unwrap()->size() % 2 === 1; |
|
77
|
|
|
|
|
78
|
|
|
$this->assertTrue( |
|
79
|
|
|
\array_reduce( |
|
80
|
|
|
\iterator_to_array($sets->values(new RandomInt)), |
|
81
|
|
|
$hasOddSet, |
|
82
|
|
|
false, |
|
83
|
|
|
), |
|
84
|
|
|
); |
|
85
|
|
|
$this->assertFalse( |
|
86
|
|
|
\array_reduce( |
|
87
|
|
|
\iterator_to_array($sets2->values(new RandomInt)), |
|
88
|
|
|
$hasOddSet, |
|
89
|
|
|
false, |
|
90
|
|
|
), |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testFlagStructureAsMutableWhenUnderlyingSetValuesAreMutable() |
|
95
|
|
|
{ |
|
96
|
|
|
$sets = Set::of( |
|
97
|
|
|
DataSet\Decorate::mutable( |
|
98
|
|
|
static fn() => new \stdClass, |
|
99
|
|
|
new DataSet\Chars, |
|
100
|
|
|
), |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($sets->values(new RandomInt) as $set) { |
|
104
|
|
|
$this->assertFalse($set->isImmutable()); |
|
105
|
|
|
$this->assertNotSame($set->unwrap(), $set->unwrap()); |
|
106
|
|
|
$this->assertSame($set->unwrap()->size(), $set->unwrap()->size()); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testNonEmptySetCanBeShrunk() |
|
111
|
|
|
{ |
|
112
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::between(1, 100)); |
|
113
|
|
|
|
|
114
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
115
|
|
|
$this->assertTrue($value->shrinkable()); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testEmptySetCanNotBeShrunk() |
|
120
|
|
|
{ |
|
121
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::below(1)); |
|
122
|
|
|
|
|
123
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
124
|
|
|
if (!$value->unwrap()->empty()) { |
|
125
|
|
|
// as it can generate sets of 1 element |
|
126
|
|
|
continue; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->assertFalse($value->shrinkable()); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testNonEmptySetAreShrunkWithDifferentStrategies() |
|
134
|
|
|
{ |
|
135
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::between(3, 100)); |
|
136
|
|
|
|
|
137
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
138
|
|
|
if ($value->unwrap()->size() < 6) { |
|
139
|
|
|
// when generating the lower bound it will shrink identity values |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$dichotomy = $value->shrink(); |
|
144
|
|
|
$this->assertFalse( |
|
145
|
|
|
$dichotomy->a()->unwrap()->equals($dichotomy->b()->unwrap()), |
|
146
|
|
|
"Initial set size: {$value->unwrap()->size()}", |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testShrunkSetsDoContainsLessThanTheInitialValue() |
|
152
|
|
|
{ |
|
153
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::between(2, 100)); |
|
154
|
|
|
|
|
155
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
156
|
|
|
if ($value->unwrap()->size() < 4) { |
|
157
|
|
|
// otherwise strategy A will return it's identity since 3/2 won't |
|
158
|
|
|
// match the predicate of minimum size 2, so strategy will return |
|
159
|
|
|
// an identity value |
|
160
|
|
|
continue; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$dichotomy = $value->shrink(); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertLessThan($value->unwrap()->size(), $dichotomy->a()->unwrap()->size()); |
|
166
|
|
|
$this->assertLessThan($value->unwrap()->size(), $dichotomy->b()->unwrap()->size()); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testShrinkingStrategyAReduceTheSetFasterThanStrategyB() |
|
171
|
|
|
{ |
|
172
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::between(3, 100)); |
|
173
|
|
|
|
|
174
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
175
|
|
|
if ($value->unwrap()->size() < 6) { |
|
176
|
|
|
// otherwise strategy A will return it's identity since 5/2 won't |
|
177
|
|
|
// match the predicate of minimum size 3, so strategy will return |
|
178
|
|
|
// an identity value so it will always be greater than stragey B |
|
179
|
|
|
continue; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$dichotomy = $value->shrink(); |
|
183
|
|
|
|
|
184
|
|
|
$this->assertLessThan($dichotomy->b()->unwrap()->size(), $dichotomy->a()->unwrap()->size()); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testShrunkValuesConserveMutabilityProperty() |
|
189
|
|
|
{ |
|
190
|
|
|
$sets = Set::of(DataSet\Chars::any(), DataSet\Integers::between(1, 100)); |
|
191
|
|
|
|
|
192
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
193
|
|
|
$dichotomy = $value->shrink(); |
|
194
|
|
|
|
|
195
|
|
|
$this->assertTrue($dichotomy->a()->isImmutable()); |
|
196
|
|
|
$this->assertTrue($dichotomy->b()->isImmutable()); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$sets = Set::of( |
|
200
|
|
|
DataSet\Decorate::mutable( |
|
201
|
|
|
static fn() => new \stdClass, |
|
202
|
|
|
new DataSet\Chars, |
|
203
|
|
|
), |
|
204
|
|
|
DataSet\Integers::between(1, 100), |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($sets->values(new RandomInt) as $value) { |
|
208
|
|
|
$dichotomy = $value->shrink(); |
|
209
|
|
|
|
|
210
|
|
|
$this->assertFalse($dichotomy->a()->isImmutable()); |
|
211
|
|
|
$this->assertFalse($dichotomy->b()->isImmutable()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|