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.
This commit is contained in:
Johan B.W. de Vries 2025-04-05 16:41:46 +02:00
parent 3e916a242e
commit 521171540b
3 changed files with 14 additions and 27 deletions

View File

@ -471,10 +471,12 @@ class OurVisitor:
if 1 != len(node.args): if 1 != len(node.args):
_raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given')
return UnaryOp( unary_op = UnaryOp(
'cast', 'cast',
self.visit_Module_FunctionDef_expr(module, function, our_locals, node.args[0]), 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': elif node.func.id == 'len':
if 1 != len(node.args): if 1 != len(node.args):
_raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given')

View File

@ -26,7 +26,7 @@ class Suite:
def __init__(self, code_py: str) -> None: def __init__(self, code_py: str) -> None:
self.code_py = code_py 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 Compiles the given python code into wasm and
then runs it then runs it
@ -51,7 +51,8 @@ class Suite:
runner.interpreter_load(imports) runner.interpreter_load(imports)
# Check if code formatting works # 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] func_args = [x.type3 for x in runner.phasm_ast.functions[func_name].posonlyargs]
if len(func_args) != len(args): if len(func_args) != len(args):

View File

@ -1,5 +1,3 @@
import binascii
import pytest import pytest
from ..helpers import Suite from ..helpers import Suite
@ -7,29 +5,15 @@ from ..helpers import Suite
@pytest.mark.slow_integration_test @pytest.mark.slow_integration_test
def test_crc32(): def test_crc32():
# FIXME: Stub with open('examples/crc32.py', 'r', encoding='ASCII') as fil:
# crc = 0xFFFFFFFF code_py = "\n" + fil.read()
# 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
code_py = """ # https://reveng.sourceforge.io/crc-catalogue/legend.htm#crc.legend.params
def _crc32_f(crc: u32, byt: u8) -> u32: in_put = b'123456789'
return 16777215 ^ 397917763
@exported # https://reveng.sourceforge.io/crc-catalogue/17plus.htm#crc.cat.crc-32-iso-hdlc
def testEntry(data: bytes) -> u32: check = 0xcbf43926
return 4294967295 ^ _crc32_f(4294967295, data[0])
"""
exp_result = binascii.crc32(b'a')
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