Ton
77 строк · 2.7 Кб
1const fs = require('fs/promises');2const os = require('os');3const path = require('path');4const { compileWasm, compileFile } = require('./wasm_tests_common');5const { execSync } = require('child_process');6
7async function main() {8const compiledPath = path.join(os.tmpdir(), 'compiled.fif');9const runnerPath = path.join(os.tmpdir(), 'runner.fif');10
11const tests = (await fs.readdir('.')).filter(f => f.endsWith('.fc')).sort();12
13const mathChars = '0x123456789()+-*/<>'.split('')14
15for (const testFile of tests) {16const mod = await compileWasm()17
18const result = await compileFile(mod, testFile)19
20if (result.status !== 'ok') {21console.error(result);22throw new Error('Could not compile ' + filename);23}24
25const fileLines = (await fs.readFile(testFile)).toString('utf-8').split('\n');26
27const testCases = [];28
29for (const line of fileLines) {30const parts = line.split('|').map(c => c.trim());31
32if (parts.length !== 4 || parts[0] !== 'TESTCASE') continue;33
34const processedInputs = [];35
36for (const input of parts[2].split(' ')) {37if (input.includes('x{')) {38processedInputs.push(input);39continue;40}41
42if (input.length === 0) {43continue44}45
46const replacedInput = input.split('').filter(c => mathChars.includes(c)).join('').replace('//', '/').replace(/([0-9a-f])($|[^0-9a-fx])/gmi, '$1n$2')47
48processedInputs.push(eval(replacedInput).toString());49}50
51testCases.push([parts[1], processedInputs.join(' '), parts[3]]);52}53
54await fs.writeFile(compiledPath, '"Asm.fif" include\n' + JSON.parse('"' + result.fiftCode + '"'));55await fs.writeFile(runnerPath, `"${compiledPath}" include <s constant code\n${testCases.map(t => `${t[1]} ${t[0]} code 1 runvmx abort"exitcode is not 0" .s cr { drop } depth 1- times`).join('\n')}`)56
57const fiftResult = execSync(`${process.env.FIFT_EXECUTABLE || 'fift'} -I ${process.env.FIFT_LIBS} /tmp/runner.fif`, {58stdio: ['pipe', 'pipe', 'ignore']59}).toString('utf-8')60
61const testResults = fiftResult.split('\n').map(s => s.trim()).filter(s => s.length > 0)62
63if (testResults.length !== testCases.length) {64throw new Error(`Got ${testResults.length} results but there are ${testCases.length} cases`)65}66
67for (let i = 0; i < testResults.length; i++) {68if (testResults[i] !== testCases[i][2]) {69throw new Error(`Unequal result ${testResults[i]} and case ${testCases[i][2]}`)70}71}72
73console.log(testFile, 'ok')74}75}
76
77main()