aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/src/SpecTestsuites/CoreTest.php
blob: 75d4c2f7571d494572c838afd16655633debf2a6 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
<?php

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\Tests\SpecTestsuites;

use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\Execution\Runtime;
use Nsfisis\Waddiwasi\Execution\Store;
use PHPUnit\Framework\TestCase;
use function count;

final class CoreTest extends TestCase
{
    public function testAddress(): void
    {
        $this->runSpecTestsuite('address');
    }

    public function testAlign(): void
    {
        $this->runSpecTestsuite('align');
    }

    public function testBinaryLeb128(): void
    {
        $this->runSpecTestsuite('binary-leb128');
    }

    public function testBinary(): void
    {
        $this->runSpecTestsuite('binary');
    }

    public function testBlock(): void
    {
        $this->runSpecTestsuite('block');
    }

    public function testBr(): void
    {
        $this->runSpecTestsuite('br');
    }

    public function testBrIf(): void
    {
        $this->runSpecTestsuite('br_if');
    }

    public function testBrTable(): void
    {
        $this->runSpecTestsuite('br_table');
    }

    public function testBulk(): void
    {
        $this->runSpecTestsuite('bulk');
    }

    public function testCall(): void
    {
        $this->runSpecTestsuite('call');
    }

    public function testCallIndirect(): void
    {
        $this->runSpecTestsuite('call_indirect');
    }

    public function testConst(): void
    {
        $this->runSpecTestsuite('const');
    }

    public function testConversions(): void
    {
        $this->runSpecTestsuite('conversions');
    }

    public function testCustom(): void
    {
        $this->runSpecTestsuite('custom');
    }

    public function testData(): void
    {
        $this->runSpecTestsuite('data');
    }

    public function testElem(): void
    {
        $this->runSpecTestsuite('elem');
    }

    public function testEndianness(): void
    {
        $this->runSpecTestsuite('endianness');
    }

    public function testExports(): void
    {
        $this->runSpecTestsuite('exports');
    }

    public function testF32(): void
    {
        $this->runSpecTestsuite('f32');
    }

    public function testF32Bitwise(): void
    {
        $this->runSpecTestsuite('f32_bitwise');
    }

    public function testF32Cmp(): void
    {
        $this->runSpecTestsuite('f32_cmp');
    }

    public function testF64(): void
    {
        $this->runSpecTestsuite('f64');
    }

    public function testF64Bitwise(): void
    {
        $this->runSpecTestsuite('f64_bitwise');
    }

    public function testF64Cmp(): void
    {
        $this->runSpecTestsuite('f64_cmp');
    }

    public function testFac(): void
    {
        $this->runSpecTestsuite('fac');
    }

    public function testFloatExprs(): void
    {
        $this->runSpecTestsuite('float_exprs');
    }

    public function testFloatLiterals(): void
    {
        $this->runSpecTestsuite('float_literals');
    }

    public function testFloatMemory(): void
    {
        $this->runSpecTestsuite('float_memory');
    }

    public function testFloatMisc(): void
    {
        $this->runSpecTestsuite('float_misc');
    }

    public function testForward(): void
    {
        $this->runSpecTestsuite('forward');
    }

    public function testFunc(): void
    {
        $this->runSpecTestsuite('func');
    }

    public function testFuncPtrs(): void
    {
        $this->runSpecTestsuite('func_ptrs');
    }

    public function testGlobal(): void
    {
        $this->runSpecTestsuite('global');
    }

    public function testI32(): void
    {
        $this->runSpecTestsuite('i32');
    }

    public function testI64(): void
    {
        $this->runSpecTestsuite('i64');
    }

    public function testIf(): void
    {
        $this->runSpecTestsuite('if');
    }

    public function testImports(): void
    {
        $this->runSpecTestsuite('imports');
    }

    public function testInlineModule(): void
    {
        $this->runSpecTestsuite('inline-module');
    }

    public function testIntExprs(): void
    {
        $this->runSpecTestsuite('int_exprs');
    }

    public function testIntLiterals(): void
    {
        $this->runSpecTestsuite('int_literals');
    }

    public function testLabels(): void
    {
        $this->runSpecTestsuite('labels');
    }

    public function testLeftToRight(): void
    {
        $this->runSpecTestsuite('left-to-right');
    }

    public function testLinking(): void
    {
        $this->runSpecTestsuite('linking');
    }

    public function testLoad(): void
    {
        $this->runSpecTestsuite('load');
    }

    public function testLocalGet(): void
    {
        $this->runSpecTestsuite('local_get');
    }

    public function testLocalSet(): void
    {
        $this->runSpecTestsuite('local_set');
    }

    public function testLocalTee(): void
    {
        $this->runSpecTestsuite('local_tee');
    }

    public function testLoop(): void
    {
        $this->runSpecTestsuite('loop');
    }

    public function testMemory(): void
    {
        $this->runSpecTestsuite('memory');
    }

    public function testMemoryCopy(): void
    {
        $this->runSpecTestsuite('memory_copy');
    }

    public function testMemoryFill(): void
    {
        $this->runSpecTestsuite('memory_fill');
    }

    public function testMemoryGrow(): void
    {
        $this->runSpecTestsuite('memory_grow');
    }

    public function testMemoryInit(): void
    {
        $this->runSpecTestsuite('memory_init');
    }

    public function testMemoryRedundancy(): void
    {
        $this->runSpecTestsuite('memory_redundancy');
    }

    public function testMemorySize(): void
    {
        $this->runSpecTestsuite('memory_size');
    }

    public function testMemoryTrap(): void
    {
        $this->runSpecTestsuite('memory_trap');
    }

    public function testNames(): void
    {
        $this->runSpecTestsuite('names');
    }

    public function testNop(): void
    {
        $this->runSpecTestsuite('nop');
    }

    public function testObsoleteKeywords(): void
    {
        $this->runSpecTestsuite('obsolete-keywords');
    }

    public function testRefFunc(): void
    {
        $this->runSpecTestsuite('ref_func');
    }

    public function testRefIsNull(): void
    {
        $this->runSpecTestsuite('ref_is_null');
    }

    public function testRefNull(): void
    {
        $this->runSpecTestsuite('ref_null');
    }

    public function testReturn(): void
    {
        $this->runSpecTestsuite('return');
    }

    public function testSelect(): void
    {
        $this->runSpecTestsuite('select');
    }

    public function testSkipStackGuardPage(): void
    {
        $this->runSpecTestsuite('skip-stack-guard-page');
    }

    public function testStack(): void
    {
        $this->runSpecTestsuite('stack');
    }

    public function testStart(): void
    {
        $this->runSpecTestsuite('start');
    }

    public function testStore(): void
    {
        $this->runSpecTestsuite('store');
    }

    public function testSwitch(): void
    {
        $this->runSpecTestsuite('switch');
    }

    public function testTableSub(): void
    {
        $this->runSpecTestsuite('table-sub');
    }

    public function testTable(): void
    {
        $this->runSpecTestsuite('table');
    }

    public function testTableCopy(): void
    {
        $this->runSpecTestsuite('table_copy');
    }

    public function testTableFill(): void
    {
        $this->runSpecTestsuite('table_fill');
    }

    public function testTableGet(): void
    {
        $this->runSpecTestsuite('table_get');
    }

    public function testTableGrow(): void
    {
        $this->runSpecTestsuite('table_grow');
    }

    public function testTableInit(): void
    {
        $this->runSpecTestsuite('table_init');
    }

    public function testTableSet(): void
    {
        $this->runSpecTestsuite('table_set');
    }

    public function testTableSize(): void
    {
        $this->runSpecTestsuite('table_size');
    }

    public function testToken(): void
    {
        $this->runSpecTestsuite('token');
    }

    public function testTraps(): void
    {
        $this->runSpecTestsuite('traps');
    }

    public function testType(): void
    {
        $this->runSpecTestsuite('type');
    }

    public function testUnreachable(): void
    {
        $this->runSpecTestsuite('unreachable');
    }

    public function testUnreachedInvalid(): void
    {
        $this->runSpecTestsuite('unreached-invalid');
    }

    public function testUnreachedValid(): void
    {
        $this->runSpecTestsuite('unreached-valid');
    }

    public function testUnwind(): void
    {
        $this->runSpecTestsuite('unwind');
    }

    public function testUtf8CustomSectionId(): void
    {
        $this->runSpecTestsuite('utf8-custom-section-id');
    }

    public function testUtf8ImportField(): void
    {
        $this->runSpecTestsuite('utf8-import-field');
    }

    public function testUtf8ImportModule(): void
    {
        $this->runSpecTestsuite('utf8-import-module');
    }

    public function testUtf8InvalidEncoding(): void
    {
        $this->runSpecTestsuite('utf8-invalid-encoding');
    }

    private function runSpecTestsuite(string $testsuiteName): void
    {
        $baseDir = __DIR__ . '/../../fixtures/spec_testsuites/core';
        $testsuiteDefinitionFileContent = file_get_contents("$baseDir/$testsuiteName.json");
        $testsuiteDefinition = json_decode($testsuiteDefinitionFileContent, true);

        $commands = $testsuiteDefinition['commands'];
        $modules = [];
        $runtimes = [];
        foreach ($commands as $command) {
            switch ($command['type']) {
                case 'module':
                    $filename = $command['filename'];
                    $moduleName = $command['name'] ?? '_';
                    $filePath = "$baseDir/$filename";
                    $wasmBinary = file_get_contents($filePath);
                    $module = (new Decoder($wasmBinary))->decode();
                    $modules[$moduleName] = $module;
                    $runtime = Runtime::instantiate(Store::empty(), $module, []);
                    $runtimes[$moduleName] = $runtime;
                    break;
                case 'action':
                    break;
                case 'assert_exhaustion':
                    break;
                case 'assert_invalid':
                    break;
                case 'assert_malformed':
                    break;
                case 'assert_return':
                    $targetModuleName = $command['module'] ?? '_';
                    $targetModule = $modules[$targetModuleName];
                    $expectedResults = $command['expected'];
                    $action = $command['action'];
                    $actionType = $action['type'];
                    $actionField = $action['field'];
                    if ($actionType === 'invoke') {
                        $actionArgs = $action['args'];
                        $runtime = $runtimes[$targetModuleName];
                        if (count($actionArgs) === 0) {
                            $actualResults = $runtime->invoke($actionField, []);
                            $this->assertCount(count($expectedResults), $actualResults, "at $command[line]");
                            for ($i = 0; $i < count($expectedResults); $i++) {
                                $expectedResult = $expectedResults[$i];
                                $actualResult = $actualResults[$i];
                                if ($expectedResult['type'] === 'f32') {
                                    $expectedValue = unpack('g', pack('l', (int)$expectedResult['value']))[1];
                                    $this->assertSame($expectedValue, $actualResult, "at $command[line]");
                                } elseif ($expectedResult['type'] === 'f64') {
                                    $expectedValue = unpack('e', pack('q', (int)$expectedResult['value']))[1];
                                    $this->assertSame($expectedValue, $actualResult, "at $command[line]");
                                } else {
                                    $expectedValue = (int)$expectedResult['value'];
                                    $this->assertSame($expectedValue, $actualResult, "at $command[line]");
                                }
                            }
                        }
                    } else {
                    }
                    break;
                case 'assert_trap':
                    break;
                case 'assert_uninstantiable':
                    break;
                case 'assert_unlinkable':
                    break;
                case 'register':
                    break;
            }
        }
    }
}