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:
parent
3e916a242e
commit
521171540b
@ -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')
|
||||
|
||||
@ -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):
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user