tests updated

This commit is contained in:
Nicolas Kruse 2025-10-18 23:20:40 +02:00
parent 79f1fc80a9
commit ef7ae1c0a1
9 changed files with 53 additions and 49 deletions

View File

@ -1,4 +1,4 @@
from copapy import Write, CPVariable
from copapy import Write, cpvalue
import copapy as rc
@ -20,8 +20,8 @@ def test_ast_generation():
#r2 = i1 + 9
#out = [Write(r1), Write(r2)]
c1 = CPVariable(4)
c2 = CPVariable(2)
c1 = cpvalue(4)
c2 = cpvalue(2)
#i1 = c1 * 2
#r1 = i1 + 7 + (c2 + 7 * 9)
#r2 = i1 + 9

View File

@ -1,4 +1,4 @@
from copapy import Write, CPVariable
from copapy import Write, cpvalue, NumLike
import copapy
import subprocess
import struct
@ -27,13 +27,8 @@ def test_example():
data = struct.pack(en + 'i', r2)
print("example r2 " + ' '.join(f'{b:02X}' for b in data))
# assert False
#example r1 42 A0 00 00
#example r2 41 88 00 00
def function(c1, c2):
def function(c1: NumLike, c2: NumLike) -> tuple[NumLike, ...]:
i1 = c1 // 3.3 + 5
i2 = c2 * 5 + c1
r1 = i1 + i2 * 55 / 4
@ -44,8 +39,8 @@ def function(c1, c2):
def test_compile():
c1 = CPVariable(4)
c2 = CPVariable(2)
c1 = cpvalue(4)
c2 = cpvalue(2)
ret = function(c1, c2)
#ret = [c1 // 3.3 + 5]
@ -58,6 +53,7 @@ def test_compile():
il.write_com(binwrite.Command.RUN_PROG)
for net in ret:
assert isinstance(net, copapy.Net)
copapy.add_read_command(il, variables, net)
il.write_com(binwrite.Command.END_COM)

View File

@ -1,7 +1,6 @@
from copapy import Write, CPVariable
from copapy import Write, cpvalue, NumLike
import copapy
import subprocess
import struct
from copapy import binwrite
@ -12,14 +11,15 @@ def run_command(command: list[str], encoding: str = 'utf8') -> str:
assert error is None, f"Error occurred: {error.decode(encoding)}"
return output.decode(encoding)
def function(c1):
r1 = c1 / 2
def function(c1: NumLike) -> list[NumLike]:
r1 = c1 / 2
return [r1]
def test_compile():
c1 = CPVariable(16)
c1 = cpvalue(16)
ret = function(c1)

View File

@ -1,8 +1,8 @@
from copapy import CPVariable, Target
from pytest import approx
from copapy import cpvalue, Target, NumLike
import pytest
def function(c1):
def function(c1: NumLike) -> list[NumLike]:
i1 = c1 / 4 + c1 / -4 - c1 // 4 * c1 // -4 + (c1 * -1) // 4
r1 = i1 / 32.4 + 54 * c1
@ -14,7 +14,7 @@ def function(c1):
def test_compile():
c1 = CPVariable(16)
c1 = cpvalue(16)
ret = function(c1)
@ -31,7 +31,7 @@ def test_compile():
for test, ref, name in zip(ret, ret_ref, ['i1', 'i2', 'r1', 'r2']):
val = tg.read_value(test)
print('+', name, val, ref)
assert val == approx(ref, 1e-5), name
assert val == pytest.approx(ref, 1e-5), name
if __name__ == "__main__":

View File

@ -1,13 +1,13 @@
from coparun_module import coparun
from copapy import Write, CPVariable
from copapy import Write, cpvalue
import copapy
from copapy import binwrite
def test_compile():
c1 = CPVariable(4)
c2 = CPVariable(2) * 4
c1 = cpvalue(4)
c2 = cpvalue(2) * 4
i1 = c2 * 2
r1 = i1 + 7 + (c1 + 7 * 9)
@ -19,7 +19,7 @@ def test_compile():
# run program command
il.write_com(binwrite.Command.RUN_PROG)
for net in [c1, c2, i1, r1, r2]:
for net in (c1, c2, i1, r1, r2):
copapy.add_read_command(il, variables, net)
# run program command

View File

@ -1,4 +1,4 @@
from copapy import Write, CPVariable
from copapy import NumLike, Write, cpvalue, Net
import copapy
import subprocess
from copapy import binwrite
@ -12,7 +12,7 @@ def run_command(command: list[str], encoding: str = 'utf8') -> str:
return output.decode(encoding)
def function(c1, c2):
def function(c1: NumLike, c2: NumLike) -> tuple[NumLike, ...]:
i1 = c1 * 3.3 + 5
i2 = c2 * 5 + c1
r1 = i1 + i2 * 55 / 4
@ -23,8 +23,8 @@ def function(c1, c2):
def test_compile():
c1 = CPVariable(4)
c2 = CPVariable(2)
c1 = cpvalue(4)
c2 = cpvalue(2)
ret = function(c1, c2)
@ -39,6 +39,7 @@ def test_compile():
for net, name in zip(ret, ['i1', 'i2', 'r1', 'r2']):
print('+', name)
assert isinstance(net, Net)
copapy.add_read_command(dw, variable_list, net)
dw.write_com(binwrite.Command.END_COM)

View File

@ -1,39 +1,46 @@
from copapy import CPVariable, Target
from copapy import cpvalue, Target, NumLike, Net, cpint
from pytest import approx
def function1(c1):
def function1(c1: NumLike) -> list[NumLike]:
return [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4,
c1 * 4, c1 * -4,
c1 + 4, c1 - 4,
c1 > 2, c1 > 100, c1 < 4, c1 < 100]
def function2(c1):
def function2(c1: NumLike) -> list[NumLike]:
return [c1 / 4.44, c1 / -4.44, c1 // 4.44, c1 // -4.44, (c1 * -1) // 4.44,
c1 * 4.44, c1 * -4.44,
c1 + 4.44, c1 - 4.44,
c1 > 2, c1 > 100.11, c1 < 4.44, c1 < 100.11]
def function3(c1):
def function3(c1: NumLike) -> list[NumLike]:
return [c1 / 4]
def function4(c1):
def function4(c1: NumLike) -> list[NumLike]:
return [c1 == 9, c1 == 4, c1 != 9, c1 != 4]
def function5(c1):
return [c1 == True, c1 == False, c1 != True, c1 != False]
def function5(c1: NumLike) -> list[NumLike]:
return [c1 == True, c1 == False, c1 != True, c1 != False, c1 / 2, c1 + 2]
def function6(c1: NumLike) -> list[NumLike]:
return [c1 == True]
def test_compile():
c_i = CPVariable(9)
c_f = CPVariable(1.111)
c_b = CPVariable(True)
c_i = cpvalue(9)
c_f = cpvalue(1.111)
c_b = cpvalue(True)
ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [CPVariable(9) % 2]
ret_test = function1(c_i) + function1(c_f) + function2(c_i) + function2(c_f) + function3(c_i) + function4(c_i) + function5(c_b) + [cpint(9) % 2]
ret_ref = function1(9) + function1(1.111) + function2(9) + function2(1.111) + function3(9) + function4(9) + function5(True) + [9 % 2]
print(ret_test)
tg = Target()
print('* compile and copy ...')
tg.compile(ret_test)
@ -42,8 +49,9 @@ def test_compile():
print('* finished')
for test, ref in zip(ret_test, ret_ref):
assert isinstance(test, Net)
val = tg.read_value(test)
#print('+', val, ref, test.dtype)
print('+', val, ref, test.dtype)
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 == approx(ref, 1e-5), f"Result does not match: {val} and reference: {ref}"

View File

@ -4,6 +4,7 @@ import platform
arch = platform.machine()
sdb = stencil_database(f'src/copapy/obj/stencils_{arch}_O3.o')
def test_list_symbols():
print('----')
#print(sdb.function_definitions)
@ -17,7 +18,7 @@ def test_start_end_function():
symbol = sdb.elf.symbols[sym_name]
if symbol.relocations and symbol.relocations[-1].symbol.info == 'STT_NOTYPE':
print('-', sym_name, stencil_db.get_stencil_position(symbol), len(symbol.data))
start, end = stencil_db.get_stencil_position(symbol)
@ -33,6 +34,5 @@ def test_aux_functions():
print(reloc.symbol.name, reloc.symbol.info)
if __name__ == "__main__":
test_aux_functions()

View File

@ -1,11 +1,10 @@
from copapy import CPVariable, Target, Write, binwrite
from copapy import cpvalue, Write, binwrite
import copapy
from pytest import approx
def test_compile() -> None:
c1 = CPVariable(9)
c1 = cpvalue(9)
#ret = [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4]
ret = [c1 // 3.3 + 5]