phasm/tests/integration/test_lang/test_fractional.py
Johan B.W. de Vries 46dbc90475 Implements ceil, floor, trunc, nearest
To round of the f32 / f64 wasm supported opcodes.

This also means we can remove the now outdated
WEBASSEMBLY_BUILTIN_FLOAT_OPS.
2025-04-06 16:38:57 +02:00

74 lines
1.9 KiB
Python

import pytest
from ..helpers import Suite
TYPE_LIST = ['f32', 'f64']
TEST_LIST = [
('10.0 / 8.0', 1.25, ),
# WebAssembly dictates that float division follows the IEEE rules
# https://www.w3.org/TR/wasm-core-1/#-hrefop-fdivmathrmfdiv_n-z_1-z_2
('10.0 / 0.0', float('+inf') , ),
('-10.0 / 0.0', float('-inf') , ),
( 'ceil(4.5)', 5.0, ),
( 'ceil(4.75)', 5.0, ),
( 'ceil(5.0)', 5.0, ),
( 'ceil(5.25)', 6.0, ),
( 'ceil(5.5)', 6.0, ),
('ceil(-4.5)', -4.0, ),
('ceil(-4.75)', -4.0, ),
('ceil(-5.0)' , -5.0, ),
('ceil(-5.25)', -5.0, ),
('ceil(-5.5)', -5.0, ),
( 'floor(4.5)', 4.0, ),
( 'floor(4.75)', 4.0, ),
( 'floor(5.0)', 5.0, ),
( 'floor(5.25)', 5.0, ),
( 'floor(5.5)', 5.0, ),
('floor(-4.5)', -5.0, ),
('floor(-4.75)', -5.0, ),
('floor(-5.0)' , -5.0, ),
('floor(-5.25)', -6.0, ),
('floor(-5.5)', -6.0, ),
( 'trunc(4.5)', 4.0, ),
( 'trunc(4.75)', 4.0, ),
( 'trunc(5.0)', 5.0, ),
( 'trunc(5.25)', 5.0, ),
( 'trunc(5.5)', 5.0, ),
('trunc(-4.5)', -4.0, ),
('trunc(-4.75)', -4.0, ),
('trunc(-5.0)' , -5.0, ),
('trunc(-5.25)', -5.0, ),
('trunc(-5.5)', -5.0, ),
( 'nearest(4.5)', 4.0, ),
( 'nearest(4.75)', 5.0, ),
( 'nearest(5.0)', 5.0, ),
( 'nearest(5.25)', 5.0, ),
( 'nearest(5.5)', 6.0, ),
('nearest(-4.5)', -4.0, ),
('nearest(-4.75)', -5.0, ),
('nearest(-5.0)', -5.0, ),
('nearest(-5.25)', -5.0, ),
('nearest(-5.5)', -6.0, ),
]
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
@pytest.mark.parametrize('test_in,test_out', TEST_LIST)
def test_fractional(type_, test_in, test_out):
code_py = f"""
@exported
def testEntry() -> {type_}:
return {test_in}
"""
result = Suite(code_py).run_code()
assert test_out == result.returned_value
assert isinstance(result.returned_value, float)