aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/tools_contract_test.py
blob: 9323a478b091284e0ce336d6b799616eece76536 (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
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
#!/usr/bin/env python3
"""bounded accept/reject tests for standalone build tools and generated images."""
import argparse
import struct
import subprocess
import tempfile
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument('--group', choices=['image', 'map', 'runners'], default='image')
args = parser.parse_args()

def command(argv, data=None):
    r = subprocess.run(argv, input=data, capture_output=True, timeout=15)
    assert r.returncode >= 0, (argv, r.returncode, r.stderr)
    return r

if args.group == 'image':
    image = Path('build/core/genome.bin').read_bytes()
    words = list(struct.unpack('<%dq' % (len(image) // 8), image))
    pack = lambda xs: b''.join(struct.pack('<q', x) for x in xs)
    good = command(['build/bin/mkgenome'], image)
    assert good.returncode == 0 and good.stdout == image
    for data in [b'', b'x', pack(words[:6]), image + b'x']:
        r = command(['build/bin/mkgenome'], data)
        assert r.returncode != 0 and b'incomplete' in r.stderr and not r.stdout
    for index in range(3):
        changed = words[:]; changed[index] ^= 2
        r = command(['build/bin/mkgenome'], pack(changed))
        assert r.returncode != 0 and b'not a genome' in r.stderr and not r.stdout
        # the header words are named, with both values, and a layout mismatch points at rt.zc and the map
        assert [b'FTOP', b'ZFP', b'third word'][index] in r.stderr, r.stderr
        if index < 2: assert b'the map has 0x' in r.stderr and b'src/runtime/rt.zc' in r.stderr and b'make clean' in r.stderr, r.stderr
    r = command(['build/bin/mkgenome'], b'zc: line 1: something is wrong\nzc: 1 error\n')
    assert r.returncode != 0 and b'something is wrong' in r.stderr and not r.stdout
    print('ok   mkgenome roundtrip, truncation, alignment, map-header mutation (naming FTOP, ZFP and both values) and diagnostic contracts')
elif args.group == 'map':
    for preset in ['default', 'small', 'run', 'lsp', 'fixed']:
        with tempfile.TemporaryDirectory(prefix='tinyvm preset ') as directory:
            r = command(['build/bin/genmap', '--preset', preset, '-o', directory])
            assert r.returncode == 0, (preset, r.stderr)
            assert (Path(directory) / 'memmap.h').is_file()
    assert command(['build/bin/genmap', '--preset', 'default', '--check']).returncode == 0
    for options in [['--words', '-1'], ['--heap', '0'], ['--data', '0'], ['--symbols', '-1'], ['--total', '999999999999999999999'], ['--preset', 'imaginary']]:
        r = command(['build/bin/genmap', *options, '--check'])
        assert r.returncode != 0, options
    with tempfile.TemporaryDirectory(prefix='tinyvm map ') as directory:
        r = command(['build/bin/genmap', '--preset', 'small', '--locals', '32', '--symbols', '64', '-o', directory])
        assert r.returncode == 0, r.stderr
        for name in ['memmap.h', 'rt.zc', 'compiler.zc', 'map.txt']:
            assert (Path(directory) / name).is_file(), name
    print('ok   genmap presets, invalid capacities and generated paths containing spaces')
else:
    r = command(['bash', '-n', 'tinyvm.sh'])
    assert r.returncode == 0, r.stderr
    for script in ['src/tools/automap.sh', 'src/tools/mklsp.sh', 'src/tools/mklspwasm.sh', 'src/tools/mkvsix.sh', 'src/tools/findcc.sh', 'src/tools/bootstrap-check.sh']:
        assert command(['bash', '-n', script]).returncode == 0
    assert command(['bash', 'tinyvm.sh', 'imaginary-command']).returncode != 0
    assert Path('tinyvm.bat').is_file()
    assert not list(Path('.').glob('run*.bat')), 'windows run files must be consolidated'
    print('ok   runner and build script syntax; unsupported commands fail explicitly')