aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/wasm_test.js
blob: ddab3be8007e895f162394e7ca128e0393278df4 (plain) (blame)
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
// runs programs on the webassembly build (src/web/zc.wasm) and compares with the expected output.
// usage: node tests/drivers/wasm_test.js [path/to/zc.wasm]
const fs = require('fs');
const ZC = require('../../src/web/zc.js');
(async () => {
	const zc = await ZC.load(fs.readFileSync(process.argv[2] || __dirname + '/../../src/web/zc.wasm'));
	const cases = [
		['examples/hello.zc', 'hello, world!'],
		['examples/fact.zc', '1101110101111100000000\n3628800\n'],
		['examples/selfpatch.zc', 'ABC'],
		['examples/lists.zc', '[0 1 4 9 16 25]\nlive: 7\nafter the block: 0\n'],
		['examples/wiring.zc', '16 1234 25 4/9'],
		['examples/struct_closure_opt.zc', '299\n'],
		['examples/opt_test.zc', '1303\n'],
		['tests/programs/basics.zc', '285 610 42 512\nhey 65 -3 -2 1024 -8\n12456\n757\n9 100-101\n5\nok\n'],
	];
	let ok = 0, bad = 0;
	for (const [file, want] of cases) {
		const r = await zc.run(fs.readFileSync(__dirname + '/../../' + file, 'utf8'));
		if (r.output.replace(/\n+$/, '') === want.replace(/\n+$/, '')) { ok++; console.log('ok   wasm ' + file); }
		else { bad++; console.log('FAIL wasm ' + file + ': got ' + JSON.stringify(r.output) + ' ' + r.messages); }
	}
	// an error is reported the way the interpreter reports it
	const r = await zc.run('fn main() { return undefined_name; }');
	if (/undefined_name/.test(r.output)) { ok++; console.log('ok   wasm compiler errors'); } else { bad++; console.log('FAIL wasm compiler errors: ' + JSON.stringify(r.output)); }
	console.log(ok + ' wasm checks passed, ' + bad + ' failed');
	process.exit(bad ? 1 : 0);
})();