Johan B.W. de Vries 20c507a9ec Adds the Bits type class
Also adds the remaining unexposed WebAssembly opcodes as
comments (eqz, clz, ctz, popcnt, copysign).

This also means that BinaryOp.operator is now always
a type class method.
2025-04-09 15:40:20 +02:00

82 lines
2.4 KiB
Python

import pytest
from ..helpers import Suite
class ExpResult:
def __init__(self, default, **kwargs):
self.default = default
self.kwargs = kwargs
def get(self, type_):
return self.kwargs.get(type_, self.default)
def __repr__(self):
return 'ExpResult(' + repr(self.default) + ', ' + ', '.join(
f'{k}={repr(v)}'
for k, v in self.kwargs.items()
) + ')'
TYPE_LIST = [
'u8', 'u32', 'u64',
]
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
@pytest.mark.parametrize('test_lft,test_opr,test_rgt,test_out', [
(9, '&', 1, 1),
(9, '&', 2, 0),
(9, '&', 8, 8),
(9, '|', 2, 11),
(9, '|', 3, 11),
(9, '^', 2, 11),
(9, '^', 3, 10),
])
def test_bits_operators(type_, test_lft, test_opr, test_rgt, test_out):
code_py = f"""
@exported
def testEntry(lft: {type_}, rgt: {type_}) -> {type_}:
return lft {test_opr} rgt
"""
result = Suite(code_py).run_code(test_lft, test_rgt)
assert test_out == result.returned_value
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
@pytest.mark.parametrize('test_mtd,test_lft,test_rgt,test_out', [
# 195 = 1100 0011
# shl(195, 3) = 1560 = 0001 1000
# shl(195, 3) = 1560 = 0000 0110 0001 1000
# shr(195, 3) = 24 = 1 1000
# rotl(195, 3) = 30 = 0001 1110
# rotl(195, 3) = 1560 = 0000 0110 0001 1000
# rotr(195, 3) = 120 = 0111 1000
# rotr(195, 3) = = 0110 0000 0001 1000
# rotr(195, 3) = = 0110 0000 0000 0000 0000 0000 0001 1000
# rotr(195, 3) = = 0110 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 1000
('shl', 6, 1, ExpResult(12)),
('shl', 195, 3, ExpResult(1560, u8=24)),
('shr', 6, 1, ExpResult(3)),
('shr', 195, 3, ExpResult(24)),
('rotl', 6, 1, ExpResult(12)),
('rotl', 195, 3, ExpResult(1560, u8=30)),
('rotr', 6, 1, ExpResult(3)),
('rotr', 195, 3, ExpResult(None, u8=120, u16=24600, u32=1610612760, u64=6917529027641081880)),
])
def test_bits_methods(type_, test_mtd, test_lft, test_rgt, test_out):
code_py = f"""
@exported
def testEntry(lft: {type_}, rgt: u32) -> {type_}:
return {test_mtd}(lft, rgt)
"""
result = Suite(code_py).run_code(test_lft, test_rgt)
assert test_out.get(type_) == result.returned_value