83 lines
4.3 KiB
Python
83 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Unary depth, exact lowering/fuel and rejected-value positions (Q-061)."""
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
AJI = REPO / 'zig-out/bin/aji'
|
|
checks = 0
|
|
|
|
|
|
def cli(command, path, *options, code=0):
|
|
global checks
|
|
result = subprocess.run([str(AJI), command, *map(str, options), str(path)], capture_output=True)
|
|
assert (result.returncode, result.stderr) == (code, b''), (command, path, result.returncode, result.stdout, result.stderr)
|
|
checks += 1
|
|
return result.stdout
|
|
|
|
|
|
def source(kind, depth):
|
|
if kind == 'not':
|
|
ty, setup, expression = 'bool', '', 'not ' * depth + 'false'
|
|
instructions, expected = 2 * depth + 2, bool(depth % 2)
|
|
elif kind == 'neg':
|
|
ty, setup, expression = 'i64', 'let n: i64 = 1i64; ', '- ' * depth + 'n'
|
|
instructions, expected = 2 * depth + 2, 1 if depth % 2 == 0 else -1
|
|
else:
|
|
ty, setup = 'i64', ''
|
|
expression = '1i64' + ''.join(' ' + ('as i64', 'as! i64', 'as% i64')[i % 3] for i in range(depth))
|
|
instructions, expected = depth + 2, 1
|
|
return f'module depth\nfn main() -> {ty} effects [] {{ {setup}return {expression}; }}\n', instructions, expected
|
|
|
|
|
|
with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
|
root = Path(directory)
|
|
for depth in (0, 1, 16, 1024, 16384, 65536):
|
|
for kind in ('not', 'neg', 'cast'):
|
|
path = root / f'{kind}-{depth}.aji'
|
|
text, instructions, expected = source(kind, depth)
|
|
path.write_text(text)
|
|
assert cli('check', path) == b''
|
|
lowered = cli('lower', path)
|
|
actual = sum(b' = ' in line or line.strip().startswith(b'ret ') for line in lowered.splitlines())
|
|
assert actual == instructions, (kind, depth, actual, instructions)
|
|
if depth <= 1024:
|
|
# Independently count every emitted instruction and ret.
|
|
# No wall-clock timeout changes execution or ends a test.
|
|
assert json.loads(cli('run', path, '--fuel', instructions)) == {'status': 'returned', 'value': expected}
|
|
assert json.loads(cli('run', path, '--fuel', instructions - 1, code=5)) == {
|
|
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': instructions - 1, 'steps': instructions - 1}
|
|
air = path.with_suffix('.air')
|
|
air.write_bytes(lowered)
|
|
assert cli('run', air, '--fuel', instructions) == cli('run', path, '--fuel', instructions)
|
|
record = root / 'hosted.json'
|
|
assert cli('run', path, '--fuel', instructions, '--profile', 'hosted', '--result-file', record) == b''
|
|
assert json.loads(record.read_text()) == {'status': 'returned', 'value': expected}
|
|
print(f'unary depth: {kind} {depth}', flush=True)
|
|
|
|
for depth in (1024, 16384, 65536):
|
|
header = 'module depth\nfn main() -> i64 effects [] { return '
|
|
path = root / f'deref-{depth}.aji'
|
|
path.write_text(header + '* ' * depth + '1i64; }\n')
|
|
error = json.loads(cli('check', path, code=1))
|
|
col = len(header) - len('module depth\n') + 1 + 2 * (depth - 1)
|
|
assert (error['code'], error['line'], error['col'], error['file']) == ('E7005', 2, col, str(path)), error
|
|
for command in ('lower', 'run', 'report', 'explore'):
|
|
assert json.loads(cli(command, path, code=1)) == error
|
|
|
|
# Signed boundary literal remains one negative constant; the outer
|
|
# checked negation traps rather than changing literal acceptance.
|
|
minimum = root / 'minimum.aji'
|
|
minimum.write_text('module depth\nfn main() -> i8 effects [] { return -128i8; }\n')
|
|
assert json.loads(cli('run', minimum, '--fuel', 2)) == {'status': 'returned', 'value': -128}
|
|
minimum.write_text('module depth\nfn main() -> i8 effects [] { return - -128i8; }\n')
|
|
assert json.loads(cli('run', minimum, '--fuel', 2, code=5)) == {
|
|
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': 2, 'steps': 2}
|
|
trapped = json.loads(cli('run', minimum, '--fuel', 3, code=4))
|
|
assert (trapped['status'], trapped['code'], trapped['kind'], trapped['inst']) == (
|
|
'trapped', 'T0003', 'trap.overflow', 2), trapped
|
|
|
|
print(f'source expression depth: {checks} checks passed')
|