From 891520128045e71e798cd4754449b00fae6c9338 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Fri, 14 Nov 2025 08:56:43 +0100 Subject: [PATCH] tests updated --- tests/test_math.py | 31 +++++++++++++++++++++++++++---- tests/test_ops_aarch64.py | 20 +++++++++++++------- tests/test_ops_x86.py | 31 +++++++++++++++++++++++++------ 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/tests/test_math.py b/tests/test_math.py index 76d21ee..974889b 100644 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -70,12 +70,12 @@ def test_trig_precision(): tg.compile(ret_test) tg.run() - for i, (test, ref) in enumerate(zip(ret_test, ret_refe)): + for i, (v, test, ref) in enumerate(zip(test_vals, ret_test, ret_refe)): func_name = ['sin', 'cos', 'tan'][i % 3] assert isinstance(test, cp.variable) val = tg.read_value(test) print(f"+ Result of {func_name}: {val}; reference: {ref}") - assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] + assert val == pytest.approx(ref, abs=1e-3), f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref} (value: {v})" # pyright: ignore[reportUnknownMemberType] #if not val == pytest.approx(ref, abs=1e-5): # pyright: ignore[reportUnknownMemberType] # warnings.warn(f"Result of {func_name} for input {test_vals[i // 3]} does not match: {val} and reference: {ref}", UserWarning) @@ -88,11 +88,34 @@ def test_arcus_trig_precision(): ret_test = [r for v in test_vals for r in (cp.asin(variable(v)), cp.acos(variable(v)), cp.atan(variable(v)), - cp.atan2(variable(v), variable(3)),)] + cp.atan2(variable(v), variable(3)), + cp.atan2(variable(v), variable(-3)),)] ret_refe = [r for v in test_vals for r in (ma.asin(v), ma.acos(v), ma.atan(v), - ma.atan2(v, 3),)] + ma.atan2(v, 3), + ma.atan2(v, -3),)] + + tg = Target() + tg.compile(ret_test) + tg.run() + + for i, (test, ref) in enumerate(zip(ret_test, ret_refe)): + func_name = ['asin', 'acos', 'atan', 'atan2[1]', 'atan2[2]'][i % 5] + assert isinstance(test, cp.variable) + val = tg.read_value(test) + print(f"+ Result of {func_name}: {val}; reference: {ref}") + #assert val == pytest.approx(ref, abs=1e-5), f"Result of {func_name} for input {test_vals[i // 5]} does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] + if not val == pytest.approx(ref, abs=1e-5): # pyright: ignore[reportUnknownMemberType] + warnings.warn(f"Result of {func_name} for input {test_vals[i // 5]} does not match: {val} and reference: {ref}", UserWarning) + + +def test_arcus_trig_crash(): + + v = 0.0 + + ret_test = [cp.asin(variable(v))] + ret_refe = [ma.asin(v)] tg = Target() tg.compile(ret_test) diff --git a/tests/test_ops_aarch64.py b/tests/test_ops_aarch64.py index 62b125a..306454f 100644 --- a/tests/test_ops_aarch64.py +++ b/tests/test_ops_aarch64.py @@ -8,6 +8,7 @@ import warnings import re import struct import pytest +import copapy as cp if os.name == 'nt': # On Windows wsl and qemu-user is required: @@ -87,8 +88,8 @@ def test_compile(): c_f = variable(1.111) c_b = variable(True) - ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [variable(9) % 2] + iiftests(c_i) + iiftests(c_f) - ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [variable(9) % 2] + iiftests(c_i) + iiftests(c_f) + [cp.asin(c_i/10)] + ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2] + iiftests(9) + iiftests(1.111) + [cp.asin(9/10)] out = [Write(r) for r in ret_test] @@ -128,8 +129,13 @@ def test_compile(): warnings.warn("aarch64 runner not found, aarch64 test skipped!", UserWarning) return - command = ['build/runner/coparun-aarch64', 'build/runner/test-arm64.copapy'] + ['build/runner/test-arm64.copapy.bin'] - result = run_command(qemu_command + command) + command = qemu_command + ['build/runner/coparun-aarch64', 'build/runner/test-arm64.copapy'] + ['build/runner/test-arm64.copapy.bin'] + #try: + result = run_command(command) + #except FileNotFoundError: + # warnings.warn(f"Test skipped, executable not found.", UserWarning) + # return + print('* Output from runner:\n--') print(result) print('--') @@ -153,9 +159,9 @@ def test_compile(): else: raise Exception(f"Unknown type: {test.dtype}") print('+', val, ref, test.dtype, f" addr={address}") - #for t in (int, float, bool): - # assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}" - #assert val == pytest.approx(ref, 1e-5), f"Result does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] + for t in (int, float, bool): + assert isinstance(val, t) == isinstance(ref, t), f"Result type does not match for {val} and {ref}" + assert val == pytest.approx(ref, 1e-5), f"Result does not match: {val} and reference: {ref}" # pyright: ignore[reportUnknownMemberType] if __name__ == "__main__": diff --git a/tests/test_ops_x86.py b/tests/test_ops_x86.py index 286d316..29f2c96 100644 --- a/tests/test_ops_x86.py +++ b/tests/test_ops_x86.py @@ -77,15 +77,34 @@ def test_compile(): t4 = ((t3 * t1) * 2).sum() t5 = ((t3 * t1) * 2).magnitude() - c_i = variable(9) c_f = variable(1.111) c_b = variable(True) - ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [c_i % 2, sin(c_f)] + iiftests(c_i) + iiftests(c_f) - ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2, sin(1.111)] + iiftests(9) + iiftests(1.111) + #ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [c_i % 2, sin(c_f)] + iiftests(c_i) + iiftests(c_f) + #ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2, sin(1.111)] + iiftests(9) + iiftests(1.111) - out = [Write(r) for r in ret_test + [t2, t4, t5]] + #ret_test = [cp.sin(c_i), cp.asin(variable(0.0))] + #ret_ref = [cp.sin(9), cp.asin(0.0)] + + ret_test: list[variable[float]] = [] + ret_ref: list[float] = [] + #sval = variable(8.0) + #tval = 8.0 + #for i in range(20): + # tval = (10-i) / 12 + # sval = cp.asin(variable(tval)) + # tval = cp.asin(tval) + # ret_test.append(sval) + # ret_ref.append(tval) + + #ret_test = [cp.sin(c_i)] + #ret_ref = [cp.sin(9)] + + ret_test = [cp.get_42(c_i)] + ret_ref = [cp.get_42(9)] + + out = [Write(r) for r in ret_test] #ret_test += [c_i, v2] #ret_ref += [9, 4.44, -4.44] @@ -111,8 +130,8 @@ def test_compile(): dw.write_com(_binwrite.Command.END_COM) - print('* Data to runner:') - dw.print() + #print('* Data to runner:') + #dw.print() dw.to_file('build/runner/test-x86.copapy')