1#encoding=utf-8 2 3# Copyright (C) 2020 Collabora, Ltd. 4# 5# Permission is hereby granted, free of charge, to any person obtaining a 6# copy of this software and associated documentation files (the "Software"), 7# to deal in the Software without restriction, including without limitation 8# the rights to use, copy, modify, merge, publish, distribute, sublicense, 9# and/or sell copies of the Software, and to permit persons to whom the 10# Software is furnished to do so, subject to the following conditions: 11# 12# The above copyright notice and this permission notice (including the next 13# paragraph) shall be included in all copies or substantial portions of the 14# Software. 15# 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22# IN THE SOFTWARE. 23 24from asm import parse_asm, ParseError 25import sys 26 27def parse_hex_8(s): 28 b = [int(x, base=16) for x in s.split(' ')] 29 return sum([x << (8 * i) for i, x in enumerate(b)]) 30 31# These should not throw exceptions 32def positive_test(machine, assembly): 33 try: 34 expected = parse_hex_8(machine) 35 val = parse_asm(assembly) 36 if val != expected: 37 return f"Expected {hex(expected)} but got {hex(val)}" 38 except ParseError as exc: 39 return f"Unexpected exception: {exc}" 40 41# These should throw exceptions 42def negative_test(assembly): 43 try: 44 parse_asm(assembly) 45 return "Expected exception" 46 except Exception: 47 return None 48 49PASS = [] 50FAIL = [] 51 52def record_case(case, error): 53 if error is None: 54 PASS.append(case) 55 else: 56 FAIL.append((case, error)) 57 58if len(sys.argv) < 3: 59 print("Expected positive and negative case lists") 60 sys.exit(1) 61 62with open(sys.argv[1], "r") as f: 63 cases = f.read().split('\n') 64 cases = [x for x in cases if len(x) > 0 and x[0] != '#'] 65 66 for case in cases: 67 (machine, assembly) = case.split(' ') 68 record_case(case, positive_test(machine, assembly)) 69 70with open(sys.argv[2], "r") as f: 71 cases = f.read().split('\n') 72 cases = [x for x in cases if len(x) > 0] 73 74 for case in cases: 75 record_case(case, negative_test(case)) 76 77print("Passed {}/{} tests.".format(len(PASS), len(PASS) + len(FAIL))) 78 79if len(FAIL) > 0: 80 print("Failures:") 81 for (fail, err) in FAIL: 82 print("") 83 print(fail) 84 print(err) 85 sys.exit(1) 86