| 1 |
#encoding=utf-8 |
| 2 |
|
| 3 |
# Copyright (C) 2020 Collabora, Ltd. |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
from asm import parse_asm, ParseError |
| 7 |
import sys |
| 8 |
import struct |
| 9 |
|
| 10 |
def parse_hex_8(s): |
| 11 |
b = [int(x, base=16) for x in s.split(' ')] |
| 12 |
return sum([x << (8 * i) for i, x in enumerate(b)]) |
| 13 |
|
| 14 |
def hex_8(u64): |
| 15 |
as_bytes = struct.pack('<Q', u64) |
| 16 |
as_strings = [('0' + hex(byte)[2:])[-2:] for byte in as_bytes] |
| 17 |
return ' '.join(as_strings) |
| 18 |
|
| 19 |
# These should not throw exceptions |
| 20 |
def positive_test(machine, assembly): |
| 21 |
try: |
| 22 |
expected = parse_hex_8(machine) |
| 23 |
val = parse_asm(assembly) |
| 24 |
if val != expected: |
| 25 |
return f"{hex_8(val)} Incorrect assembly" |
| 26 |
except ParseError as exc: |
| 27 |
return f"Unexpected exception: {exc}" |
| 28 |
|
| 29 |
# These should throw exceptions |
| 30 |
def negative_test(assembly): |
| 31 |
try: |
| 32 |
parse_asm(assembly) |
| 33 |
return "Expected exception" |
| 34 |
except Exception: |
| 35 |
return None |
| 36 |
|
| 37 |
PASS = [] |
| 38 |
FAIL = [] |
| 39 |
|
| 40 |
def record_case(case, error): |
| 41 |
if error is None: |
| 42 |
PASS.append(case) |
| 43 |
else: |
| 44 |
FAIL.append((case, error)) |
| 45 |
|
| 46 |
if len(sys.argv) < 3: |
| 47 |
print("Expected positive and negative case lists") |
| 48 |
sys.exit(1) |
| 49 |
|
| 50 |
with open(sys.argv[1], "r") as f: |
| 51 |
cases = f.read().split('\n') |
| 52 |
cases = [x for x in cases if len(x) > 0 and x[0] != '#'] |
| 53 |
|
| 54 |
for case in cases: |
| 55 |
(machine, assembly) = case.split(' ') |
| 56 |
record_case(case, positive_test(machine, assembly)) |
| 57 |
|
| 58 |
with open(sys.argv[2], "r") as f: |
| 59 |
cases = f.read().split('\n') |
| 60 |
cases = [x for x in cases if len(x) > 0] |
| 61 |
|
| 62 |
for case in cases: |
| 63 |
record_case(case, negative_test(case)) |
| 64 |
|
| 65 |
print("Passed {}/{} tests.".format(len(PASS), len(PASS) + len(FAIL))) |
| 66 |
|
| 67 |
if len(FAIL) > 0: |
| 68 |
print("Failures:") |
| 69 |
for (fail, err) in FAIL: |
| 70 |
print("") |
| 71 |
print(fail) |
| 72 |
print(err) |
| 73 |
sys.exit(1) |