Browse Source

add sketched acceptance tests framework

pull/7/head
Murad Karammaev 2 years ago
parent
commit
c483020d14
Signed by: foxpy GPG Key ID: 78BE32418B0C8450
  1. 17
      Makefile
  2. 15
      acceptance-tests/add_four_ints.asm
  3. 13
      acceptance-tests/add_four_ints.pgm
  4. 59
      acceptance-tests/main.py
  5. 1
      acceptance-tests/requirements.txt

17
Makefile

@ -0,0 +1,17 @@
.PHONY: build clean test unit-tests acceptance-tests all
all: build test
build:
@cargo build --release
clean:
@cargo clean
test: unit-tests acceptance-tests
unit-tests:
@cargo test
acceptance-tests: build
@acceptance-tests/main.py

15
acceptance-tests/add_four_ints.asm

@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT
# Copyright Murad Karammaev, Nikita Kuzmin
# This program computes 3 + 7 + 8 + 80
# and stores result in [15]
MOV R0.l, 3
MOV R1.l, 7
ADD
MOV R1.l, 8
ADD
MOV R1.l, 0
MOV R1.h, 5
ADD
MOV [0xf], R0

13
acceptance-tests/add_four_ints.pgm

@ -0,0 +1,13 @@
P2
9 1
255
67 # MOV R0.l, 3
87 # MOV R1.l, 7
228 # ADD
88 # MOV R1.l, 8
228 # ADD
80 # MOV R1.l, 0
117 # MOV R1.h, 5
228 # ADD
47 # MOV [0xf], R0

59
acceptance-tests/main.py

@ -0,0 +1,59 @@
#!/usr/bin/env python3
import os
import subprocess
from tempfile import NamedTemporaryFile
import cv2
class AssemblerAcceptanceTest:
def __init__(self, source, binary):
self.source_code = os.path.join(os.getcwd(), "acceptance-tests", source)
self.expected_binary = os.path.join(os.getcwd(), "acceptance-tests", binary)
def __str__(self):
return f'acceptance_test(asm("{self.source_code}") → {self.expected_binary})'
def run(self, toyasm):
tmp_file = NamedTemporaryFile(delete=False)
expected_binary = cv2.imread(self.expected_binary)
result = subprocess.run([toyasm, self.source_code, tmp_file.name])
assert result.returncode == 0
compiled_binary = tmp_file.read()
for i in range(len(expected_binary[0])):
assert compiled_binary[i] == expected_binary[0][i][0]
for i in range(len(expected_binary[0]), 256):
assert compiled_binary[i] == 0
os.unlink(tmp_file.name)
print(f'OK: asm("{self.source_code}") == "{self.expected_binary}"')
class AcceptanceTestsRunner:
def __init__(self):
self.toyasm = self.prepare_tool("toyasm")
self.toyvm = self.prepare_tool("toyvm")
self.tests = [
AssemblerAcceptanceTest("add_four_ints.asm", "add_four_ints.pgm")
]
def prepare_tool(self, name):
tool_path = os.path.join(os.getcwd(), "target", "release", name)
if not os.path.exists(tool_path):
raise FileNotFoundError(tool_path)
return tool_path
def run(self):
for test in self.tests:
test.run(self.toyasm)
def main():
runner = AcceptanceTestsRunner()
runner.run()
if __name__ == '__main__':
main()

1
acceptance-tests/requirements.txt

@ -0,0 +1 @@
opencv-python
Loading…
Cancel
Save