From 521171540b825e214080a3e5e0e243ad1e2a3502 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 5 Apr 2025 16:41:46 +0200 Subject: [PATCH] Properly implemented test_crc32 Also extended the hack around u8 to u32 cast used in crc32 - its type was lost, so it would just cast to whatever the environment was expecting. Had to do_format_check=False since the file has some comments and such. --- phasm/parser.py | 4 ++- tests/integration/helpers.py | 5 +-- tests/integration/test_examples/test_crc32.py | 32 +++++-------------- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/phasm/parser.py b/phasm/parser.py index 15f4992..426f606 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -471,10 +471,12 @@ class OurVisitor: if 1 != len(node.args): _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') - return UnaryOp( + unary_op = UnaryOp( 'cast', self.visit_Module_FunctionDef_expr(module, function, our_locals, node.args[0]), ) + unary_op.type3 = type3types.u32 + return unary_op elif node.func.id == 'len': if 1 != len(node.args): _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 3ea3aef..d999200 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -26,7 +26,7 @@ class Suite: def __init__(self, code_py: str) -> None: self.code_py = code_py - def run_code(self, *args: Any, runtime: str = 'wasmtime', func_name: str = 'testEntry', imports: runners.Imports = None) -> Any: + def run_code(self, *args: Any, runtime: str = 'wasmtime', func_name: str = 'testEntry', imports: runners.Imports = None, do_format_check: bool = True) -> Any: """ Compiles the given python code into wasm and then runs it @@ -51,7 +51,8 @@ class Suite: runner.interpreter_load(imports) # Check if code formatting works - assert self.code_py == '\n' + phasm_render(runner.phasm_ast) # \n for formatting in tests + if do_format_check: + assert self.code_py == '\n' + phasm_render(runner.phasm_ast) # \n for formatting in tests func_args = [x.type3 for x in runner.phasm_ast.functions[func_name].posonlyargs] if len(func_args) != len(args): diff --git a/tests/integration/test_examples/test_crc32.py b/tests/integration/test_examples/test_crc32.py index 976e7d4..4c09c1c 100644 --- a/tests/integration/test_examples/test_crc32.py +++ b/tests/integration/test_examples/test_crc32.py @@ -1,5 +1,3 @@ -import binascii - import pytest from ..helpers import Suite @@ -7,29 +5,15 @@ from ..helpers import Suite @pytest.mark.slow_integration_test def test_crc32(): - # FIXME: Stub - # crc = 0xFFFFFFFF - # byt = 0x61 - # => (crc >> 8) ^ _CRC32_Table[(crc & 0xFF) ^ byt] - # (crc >> 8) = 0x00FFFFFF - # => 0x00FFFFFF ^ _CRC32_Table[(crc & 0xFF) ^ byt] - # (crc & 0xFF) = 0xFF - # => 0x00FFFFFF ^ _CRC32_Table[0xFF ^ byt] - # 0xFF ^ 0x61 = 0x9E - # => 0x00FFFFFF ^ _CRC32_Table[0x9E] - # _CRC32_Table[0x9E] = 0x17b7be43 - # => 0x00FFFFFF ^ 0x17b7be43 + with open('examples/crc32.py', 'r', encoding='ASCII') as fil: + code_py = "\n" + fil.read() - code_py = """ -def _crc32_f(crc: u32, byt: u8) -> u32: - return 16777215 ^ 397917763 + # https://reveng.sourceforge.io/crc-catalogue/legend.htm#crc.legend.params + in_put = b'123456789' -@exported -def testEntry(data: bytes) -> u32: - return 4294967295 ^ _crc32_f(4294967295, data[0]) -""" - exp_result = binascii.crc32(b'a') + # https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-iso-hdlc + check = 0xcbf43926 - result = Suite(code_py).run_code(b'a') + result = Suite(code_py).run_code(in_put, func_name='crc32', do_format_check=False) - assert exp_result == result.returned_value + assert check == result.returned_value