-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.php
More file actions
245 lines (224 loc) · 6.15 KB
/
test.php
File metadata and controls
245 lines (224 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?hh // strict
namespace HackUtils;
final class _Tests {
public static function getTests(): array<classname<Test>> {
return [
TestTests::class_(),
TestToHex::class_(),
TestFromHex::class_(),
TestStringShuffle::class_(),
TestReverseString::class_(),
TestToLower::class_(),
TestToUpper::class_(),
TestStringSplit::class_(),
TestStringChunk::class_(),
TestStringJoin::class_(),
TestStringReplace::class_(),
TestStringSplice::class_(),
TestStringSlice::class_(),
TestStringPad::class_(),
TestStringRepeat::class_(),
TestStringCharCode::class_(),
TestStringCompare::class_(),
TestStringSearch::class_(),
TestStringEndsWith::class_(),
TestStringStartsWith::class_(),
TestFloatRounding::class_(),
TestStringSetLength::class_(),
TestStringSplitAt::class_(),
TestLeapYear::class_(),
TestDaysInMonth::class_(),
TestOverflowDate::class_(),
TestValidDate::class_(),
TestQuotRemDivMod::class_(),
TestConcatMap::class_(),
TestFrac::class_(),
TestTypeof::class_(),
TestFileSystem::class_(),
TestArrayIterator::class_(),
TestDateTime::class_(),
TestException::class_(),
PCRE\Test::class_(),
TestCtype::class_(),
TestOverflowDateTime::class_(),
TestJSONEncode::class_(),
TestJSONDecode::class_(),
TestJSONError::class_(),
TestConcat::class_(),
TestConcatAll::class_(),
TestFilter::class_(),
TestFilterAssoc::class_(),
TestFst::class_(),
TestSnd::class_(),
TestIfNull::class_(),
TestIsAssoc::class_(),
TestMap::class_(),
TestMapAssoc::class_(),
TestMapKeys::class_(),
TestNewNull::class_(),
TestNullThrows::class_(),
TestPop::class_(),
TestPush::class_(),
TestShift::class_(),
TestUnshift::class_(),
TestRange::class_(),
TestReduce::class_(),
TestReduceRight::class_(),
TestRef::class_(),
TestThrow::class_(),
TestUnreachable::class_(),
TestAnyAll::class_(),
TestFromPairs::class_(),
TestToPairs::class_(),
TestGroupBy::class_(),
TestKeysToLower::class_(),
TestKeysToUpper::class_(),
TestGet::class_(),
TestGetPair::class_(),
TestSet::class_(),
TestGetOrNull::class_(),
TestGetOrDefault::class_(),
TestGetIssetDefault::class_(),
TestKeyExists::class_(),
TestKeyIsset::class_(),
TestGetOffset::class_(),
TestSetOffset::class_(),
];
}
public static function main(): void {
$errors = StrictErrors::start();
foreach (self::getTests() as $test) {
print ' '.$test::name()."\n";
$test::runStatic();
}
$errors->finish();
print "done\n";
}
}
<<__ConsistentConstruct>>
abstract class Test {
public final static function runStatic(): void {
$self = new static();
$self->run();
}
public final static function class_(): classname<this> {
return \get_called_class();
}
public final static function assertEqual(
mixed $actual,
mixed $expected,
): void {
if (!self::isEqual($actual, $expected)) {
throw new \Exception(
"Expected ".dump($expected).", got ".dump($actual),
);
}
}
public final static function isEqual(mixed $a, mixed $b): bool {
if (\is_float($a) && \is_float($b)) {
// Consider NAN as equal to itself (PHP doesn't)
if (\is_nan($a) && \is_nan($b))
return true;
// Don't consider -0.0 and +0.0 as equal (PHP does)
if (signbit($a) != signbit($b))
return false;
}
if (\is_array($a) && \is_array($b)) {
if (\count($a) !== \count($b))
return false;
// Iterate over both arrays in parallel
$iterA = new ArrayIterator($a);
$iterB = new ArrayIterator($b);
for (
$iterA->reset(), $iterB->reset();
$iterA->valid() || $iterB->valid();
$iterA->next(), $iterB->next()
) {
if (!self::isEqual($iterA->key(), $iterB->key()) ||
!self::isEqual($iterA->current(), $iterB->current())) {
return false;
}
}
return true;
}
return $a === $b;
}
public final static function assertException(
fn0<void> $f,
string $message = '',
int $code = 0,
): void {
$e = self::getException($f);
self::assertEqual($e->getMessage(), $message);
self::assertEqual($e->getCode(), $code);
}
public final static function getException(fn0<void> $f): \Exception {
try {
$f();
} catch (\Exception $e) {
return $e;
}
throw new \Exception('Code was supposed to throw but didnt');
}
public static function name(): string {
return self::class_();
}
public static function description(): string {
return '';
}
public abstract function run(): void;
}
abstract class SampleTest<Tin, Tout> extends Test {
public final function run(): void {
foreach ($this->getData() as $v) {
list($in, $out) = $v;
self::assertEqual($this->evaluate($in), $out);
}
}
public abstract function getData(): array<(Tin, Tout)>;
public abstract function evaluate(Tin $in): Tout;
}
class TestTests extends Test {
public function run(): void {
self::assertEqual(
self::getException(
function() {
Test::assertEqual([], [1]);
},
)->getMessage(),
"Expected [1], got []",
);
self::assertEqual(
self::getException(
function() {
Test::assertEqual([2], [1]);
},
)->getMessage(),
"Expected [1], got [2]",
);
self::assertEqual(
self::getException(
function() {
Test::assertEqual([0.0], [0.0 * -1.0]);
},
)->getMessage(),
"Expected [-0.0], got [0.0]",
);
self::assertEqual(
self::getException(
function() {
Test::assertEqual(0.0, 0.0 * -1.0);
},
)->getMessage(),
'Expected -0.0, got 0.0',
);
self::assertEqual(
self::getException(
function() {
Test::getException(function() {});
},
)->getMessage(),
'Code was supposed to throw but didnt',
);
}
}