From c483020d140746564a1251820d580051a7907df4 Mon Sep 17 00:00:00 2001 From: Murad Karammaev Date: Fri, 12 Aug 2022 23:08:30 +0300 Subject: [PATCH] add sketched acceptance tests framework --- Makefile | 17 +++++++++++ acceptance-tests/add_four_ints.asm | 15 ++++++++++ acceptance-tests/add_four_ints.pgm | 13 +++++++++ acceptance-tests/main.py | 59 ++++++++++++++++++++++++++++++++++++++ acceptance-tests/requirements.txt | 1 + 5 files changed, 105 insertions(+) create mode 100644 Makefile create mode 100644 acceptance-tests/add_four_ints.asm create mode 100644 acceptance-tests/add_four_ints.pgm create mode 100755 acceptance-tests/main.py create mode 100644 acceptance-tests/requirements.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b5a0e80 --- /dev/null +++ b/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 diff --git a/acceptance-tests/add_four_ints.asm b/acceptance-tests/add_four_ints.asm new file mode 100644 index 0000000..1ea913e --- /dev/null +++ b/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 diff --git a/acceptance-tests/add_four_ints.pgm b/acceptance-tests/add_four_ints.pgm new file mode 100644 index 0000000..8a8a712 --- /dev/null +++ b/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 diff --git a/acceptance-tests/main.py b/acceptance-tests/main.py new file mode 100755 index 0000000..d193e9f --- /dev/null +++ b/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() diff --git a/acceptance-tests/requirements.txt b/acceptance-tests/requirements.txt new file mode 100644 index 0000000..0dd006b --- /dev/null +++ b/acceptance-tests/requirements.txt @@ -0,0 +1 @@ +opencv-python