-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_execute_programs.py
More file actions
87 lines (63 loc) · 2.64 KB
/
Copy pathtest_execute_programs.py
File metadata and controls
87 lines (63 loc) · 2.64 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""
Program to test the TMA-16 Assembler with pytest.
Written by Romain Pesche.
"""
import re
import subprocess
import tempfile
from pathlib import Path
import pytest
import os
from tma_16_assembler import assemble
@pytest.fixture(autouse=True, scope="function")
def check_vm_executable():
vm_location = Path('tma-16-rs/target/debug/tma-16-rs')
if not vm_location.exists():
pytest.skip("No rust VM available, please run : cd tma-16-rs && cargo build")
def launch_asm(source_file):
output_file = tempfile.NamedTemporaryFile(suffix=".tmx")
os.system("cd examples")
assemble(source_file, output_file.name)
res = subprocess.run([
'tma-16-rs/target/debug/tma-16-rs',
output_file.name,
'--no-display',
'--report'], capture_output=True)
assert res
stdout = res.stdout.decode('utf-8')
stderr = res.stderr.decode('utf-8')
os.system("cd ..")
return stdout, stderr
@pytest.mark.parametrize("source_file, result", [
('examples/helloworld.asm', ('Hello, world!', 'FD', '10C', '0', '0', '0', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
('examples/bitshift.asm', ('', '1C', '1111', '40', '40', '0', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
('examples/divide.asm', ('0x1776 divided by 0x0004 equals 0x05DD, remainder 0x0002',
'1F0', 'A', '2', '0', 'EC', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
('examples/e.asm', ('E', '06', '45', '0', '0', '0', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
('examples/mult.asm', ('DONE', '41', '15', '1', '3', 'A', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
('examples/str_print.asm', ('This program was written in TMA-16 Assembly by Dante Falzone',
'FD', '13B', '0', '0', '0', '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0')),
])
def test_program_execution(source_file, result):
report_string, error_string = launch_asm(source_file)
assert not error_string
regex = re.compile(r"""
(.*)\n?
Program\ terminated\ at\ address\ ([\dA-F]+)\ with\ no\ errors\n
RA:\ (.*)\n
RB:\ (.*)\n
RC:\ (.*)\n
RD:\ (.*)\n
Stack:\ (.*)
""", re.VERBOSE | re.MULTILINE)
match = re.match(regex, report_string)
assert match
assert match.groups() == result
@pytest.mark.parametrize("source_file, thread, error", [
("examples/except.asm", 'main', 'attempt to add with overflow'),
])
def test_program_with_exception(source_file, thread, error):
report_string, error_string = launch_asm(source_file)
assert not report_string.replace('\n', '')
match = re.match(r"thread '(.*)' panicked at '(.*)',", error_string)
assert match.groups() == (thread, error)