2025-12-06 17:09:25 +00:00
|
|
|
from copapy import value
|
2025-11-11 14:57:17 +00:00
|
|
|
from copapy.backend import Write, compile_to_dag, add_read_command
|
|
|
|
|
import copapy as cp
|
|
|
|
|
import subprocess
|
|
|
|
|
from copapy import _binwrite
|
|
|
|
|
import copapy.backend
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command: list[str]) -> str:
|
|
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False)
|
|
|
|
|
assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}\n -Return code: {result.returncode}"
|
|
|
|
|
return result.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.runner
|
|
|
|
|
def test_compile():
|
|
|
|
|
|
|
|
|
|
test_vals = [0.0, -1.5, -2.0, -2.5, -3.0]
|
|
|
|
|
|
|
|
|
|
# Function with no passing-on-jump as last instruction:
|
2025-12-06 17:09:25 +00:00
|
|
|
ret_test = [r for v in test_vals for r in (cp.tan(value(v)),)]
|
2025-11-11 14:57:17 +00:00
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret_test]
|
|
|
|
|
|
|
|
|
|
il, variables = compile_to_dag(out, copapy.generic_sdb)
|
|
|
|
|
|
|
|
|
|
# run program command
|
|
|
|
|
il.write_com(_binwrite.Command.RUN_PROG)
|
|
|
|
|
#il.write_com(_binwrite.Command.DUMP_CODE)
|
|
|
|
|
|
2025-12-23 16:54:57 +00:00
|
|
|
for v in ret_test:
|
|
|
|
|
assert isinstance(v, value)
|
|
|
|
|
add_read_command(il, variables, v.net)
|
2025-11-11 14:57:17 +00:00
|
|
|
|
|
|
|
|
il.write_com(_binwrite.Command.END_COM)
|
|
|
|
|
|
|
|
|
|
print('* Data to runner:')
|
|
|
|
|
#il.print()
|
|
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
il.to_file('build/runner/test.copapy')
|
2025-11-11 14:57:17 +00:00
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
result = run_command(['build/runner/coparun', 'build/runner/test.copapy', 'build/runner/test.copapy.bin'])
|
2025-11-11 14:57:17 +00:00
|
|
|
print('* Output from runner:\n--')
|
|
|
|
|
print(result)
|
|
|
|
|
print('--')
|
|
|
|
|
|
|
|
|
|
assert 'Return value: 1' in result, 'No Return value: 1'
|
|
|
|
|
#assert 'END_COM' in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
#test_example()
|
|
|
|
|
test_compile()
|