2025-12-06 17:09:25 +00:00
|
|
|
from copapy import value, NumLike
|
2026-01-01 14:34:56 +00:00
|
|
|
from copapy.backend import Store, compile_to_dag, add_read_command
|
2025-10-08 20:47:49 +00:00
|
|
|
import copapy
|
|
|
|
|
import subprocess
|
2025-10-19 21:24:14 +00:00
|
|
|
from copapy import _binwrite
|
2025-11-09 21:53:07 +00:00
|
|
|
import pytest
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command: list[str], encoding: str = 'utf8') -> str:
|
|
|
|
|
process = subprocess.Popen(command, stdout=subprocess.PIPE)
|
|
|
|
|
output, error = process.communicate()
|
|
|
|
|
|
|
|
|
|
assert error is None, f"Error occurred: {error.decode(encoding)}"
|
|
|
|
|
return output.decode(encoding)
|
|
|
|
|
|
|
|
|
|
|
2025-10-18 21:20:40 +00:00
|
|
|
def function(c1: NumLike) -> list[NumLike]:
|
|
|
|
|
r1 = c1 / 2
|
2025-10-08 20:47:49 +00:00
|
|
|
return [r1]
|
|
|
|
|
|
2025-10-18 21:20:40 +00:00
|
|
|
|
2025-11-09 21:53:07 +00:00
|
|
|
@pytest.mark.runner
|
2025-10-08 20:47:49 +00:00
|
|
|
def test_compile():
|
|
|
|
|
|
2025-12-06 17:09:25 +00:00
|
|
|
c1 = value(16)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
ret = function(c1)
|
|
|
|
|
|
2026-01-01 14:34:56 +00:00
|
|
|
out = [Store(r) for r in ret]
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-12-21 12:22:53 +00:00
|
|
|
il, vars = compile_to_dag(out, copapy.generic_sdb)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
# run program command
|
2025-10-19 21:24:14 +00:00
|
|
|
il.write_com(_binwrite.Command.RUN_PROG)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-12-23 16:54:57 +00:00
|
|
|
for v in ret:
|
|
|
|
|
assert isinstance(v, value)
|
|
|
|
|
add_read_command(il, vars, v.net)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-10-19 21:24:14 +00:00
|
|
|
il.write_com(_binwrite.Command.END_COM)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-12-08 13:09:14 +00:00
|
|
|
#print('* Data to runner:')
|
|
|
|
|
#il.print()
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
il.to_file('build/runner/test.copapy')
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
result = run_command(['build/runner/coparun', 'build/runner/test.copapy'])
|
2025-10-08 20:47:49 +00:00
|
|
|
print('* Output from runner:')
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
assert 'Return value: 1' in result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
#test_example()
|
|
|
|
|
test_compile()
|