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 26import struct 27 28def parse_hex_8(s): 29 b = [int(x, base=16) for x in s.split(' ')] 30 return sum([x << (8 * i) for i, x in enumerate(b)]) 31 32def hex_8(u64): 33 as_bytes = struct.pack('<Q', u64) 34 as_strings = [('0' + hex(byte)[2:])[-2:] for byte in as_bytes] 35 return ' '.join(as_strings) 36 37# These should not throw exceptions 38def positive_test(machine, assembly): 39 try: 40 expected = parse_hex_8(machine) 41 val = parse_asm(assembly) 42 if val != expected: 43 return f"{hex_8(val)} Incorrect assembly" 44 except ParseError as exc: 45 return f"Unexpected exception: {exc}" 46 47# These should throw exceptions 48def negative_test(assembly): 49 try: 50 parse_asm(assembly) 51 return "Expected exception" 52 except Exception: 53 return None 54 55PASS = [] 56FAIL = [] 57 58def record_case(case, error): 59 if error is None: 60 PASS.append(case) 61 else: 62 FAIL.append((case, error)) 63 64if len(sys.argv) < 3: 65 print("Expected positive and negative case lists") 66 sys.exit(1) 67 68with open(sys.argv[1], "r") as f: 69 cases = f.read().split('\n') 70 cases = [x for x in cases if len(x) > 0 and x[0] != '#'] 71 72 for case in cases: 73 (machine, assembly) = case.split(' ') 74 record_case(case, positive_test(machine, assembly)) 75 76with open(sys.argv[2], "r") as f: 77 cases = f.read().split('\n') 78 cases = [x for x in cases if len(x) > 0] 79 80 for case in cases: 81 record_case(case, negative_test(case)) 82 83print("Passed {}/{} tests.".format(len(PASS), len(PASS) + len(FAIL))) 84 85if len(FAIL) > 0: 86 print("Failures:") 87 for (fail, err) in FAIL: 88 print("") 89 print(fail) 90 print(err) 91 sys.exit(1) 92