Ton

Форк
0
/
run_tests.js 
77 строк · 2.7 Кб
1
const fs = require('fs/promises');
2
const os = require('os');
3
const path = require('path');
4
const { compileWasm, compileFile } = require('./wasm_tests_common');
5
const { execSync } = require('child_process');
6

7
async function main() {
8
    const compiledPath = path.join(os.tmpdir(), 'compiled.fif');
9
    const runnerPath = path.join(os.tmpdir(), 'runner.fif');
10

11
    const tests = (await fs.readdir('.')).filter(f => f.endsWith('.fc')).sort();
12

13
    const mathChars = '0x123456789()+-*/<>'.split('')
14

15
    for (const testFile of tests) {
16
        const mod = await compileWasm()
17

18
        const result = await compileFile(mod, testFile)
19

20
        if (result.status !== 'ok') {
21
            console.error(result);
22
            throw new Error('Could not compile ' + filename);
23
        }
24

25
        const fileLines = (await fs.readFile(testFile)).toString('utf-8').split('\n');
26

27
        const testCases = [];
28

29
        for (const line of fileLines) {
30
            const parts = line.split('|').map(c => c.trim());
31

32
            if (parts.length !== 4 || parts[0] !== 'TESTCASE') continue;
33

34
            const processedInputs = [];
35

36
            for (const input of parts[2].split(' ')) {
37
                if (input.includes('x{')) {
38
                    processedInputs.push(input);
39
                    continue;
40
                }
41

42
                if (input.length === 0) {
43
                    continue
44
                }
45

46
                const replacedInput = input.split('').filter(c => mathChars.includes(c)).join('').replace('//', '/').replace(/([0-9a-f])($|[^0-9a-fx])/gmi, '$1n$2')
47

48
                processedInputs.push(eval(replacedInput).toString());
49
            }
50

51
            testCases.push([parts[1], processedInputs.join(' '), parts[3]]);
52
        }
53

54
        await fs.writeFile(compiledPath, '"Asm.fif" include\n' + JSON.parse('"' + result.fiftCode + '"'));
55
        await 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

57
        const fiftResult = execSync(`${process.env.FIFT_EXECUTABLE || 'fift'} -I ${process.env.FIFT_LIBS} /tmp/runner.fif`, {
58
            stdio: ['pipe', 'pipe', 'ignore']
59
        }).toString('utf-8')
60

61
        const testResults = fiftResult.split('\n').map(s => s.trim()).filter(s => s.length > 0)
62

63
        if (testResults.length !== testCases.length) {
64
            throw new Error(`Got ${testResults.length} results but there are ${testCases.length} cases`)
65
        }
66

67
        for (let i = 0; i < testResults.length; i++) {
68
            if (testResults[i] !== testCases[i][2]) {
69
                throw new Error(`Unequal result ${testResults[i]} and case ${testCases[i][2]}`)
70
            }
71
        }
72

73
        console.log(testFile, 'ok')
74
    }
75
}
76

77
main()

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.