2025-11-03 21:49:31 +00:00
|
|
|
from copapy import NumLike
|
2026-01-01 14:34:56 +00:00
|
|
|
from copapy.backend import Store, compile_to_dag, add_read_command
|
2025-11-03 14:00:56 +00:00
|
|
|
import copapy as cp
|
2025-09-30 21:18:58 +00:00
|
|
|
import subprocess
|
2025-10-01 21:09:49 +00:00
|
|
|
import struct
|
2025-10-19 21:24:14 +00:00
|
|
|
from copapy import _binwrite
|
|
|
|
|
import copapy.backend
|
2025-11-09 21:53:07 +00:00
|
|
|
import pytest
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-14 21:02:30 +00:00
|
|
|
def run_command(command: list[str]) -> str:
|
|
|
|
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf8', check=False)
|
2025-10-14 21:04:20 +00:00
|
|
|
assert result.returncode != 11, f"SIGSEGV (segmentation fault)\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
2025-10-14 21:02:30 +00:00
|
|
|
assert result.returncode == 0, f"\n -Error occurred: {result.stderr}\n -Output: {result.stdout}"
|
|
|
|
|
return result.stdout
|
2025-09-26 21:25:51 +00:00
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
def test_example():
|
2025-10-02 21:23:07 +00:00
|
|
|
c1 = 4
|
|
|
|
|
c2 = 2
|
2025-08-29 20:58:50 +00:00
|
|
|
|
|
|
|
|
i1 = c1 * 2
|
2025-10-02 21:23:07 +00:00
|
|
|
r1 = i1 + 7 + (c2 + 7 * 9)
|
|
|
|
|
r2 = i1 + 9
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-01 21:09:49 +00:00
|
|
|
en = {'little': '<', 'big': '>'}['little']
|
2025-10-02 21:23:07 +00:00
|
|
|
data = struct.pack(en + 'i', r1)
|
2025-10-01 21:09:49 +00:00
|
|
|
print("example r1 " + ' '.join(f'{b:02X}' for b in data))
|
|
|
|
|
|
2025-10-02 21:23:07 +00:00
|
|
|
data = struct.pack(en + 'i', r2)
|
2025-10-01 21:09:49 +00:00
|
|
|
print("example r2 " + ' '.join(f'{b:02X}' for b in data))
|
|
|
|
|
|
|
|
|
|
|
2025-10-18 21:20:40 +00:00
|
|
|
def function(c1: NumLike, c2: NumLike) -> tuple[NumLike, ...]:
|
2025-10-14 20:59:51 +00:00
|
|
|
i1 = c1 // 3.3 + 5
|
2025-10-08 20:47:49 +00:00
|
|
|
i2 = c2 * 5 + c1
|
|
|
|
|
r1 = i1 + i2 * 55 / 4
|
|
|
|
|
r2 = 4 * i2 + 5
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-08 20:47:49 +00:00
|
|
|
return i1, i2, r1, r2
|
2025-10-01 21:09:49 +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-10-01 21:09:49 +00:00
|
|
|
|
2025-11-03 14:00:56 +00:00
|
|
|
#c1 = variable(4)
|
|
|
|
|
#c2 = variable(2)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-11-03 14:00:56 +00:00
|
|
|
#ret = function(c1, c2)
|
2025-10-14 21:21:23 +00:00
|
|
|
#ret = [c1 // 3.3 + 5]
|
2025-10-08 20:47:49 +00:00
|
|
|
|
2025-12-06 17:09:25 +00:00
|
|
|
t1 = cp.vector([10, 11, 12]) + cp.vector(cp.value(v) for v in range(3))
|
2025-11-03 14:00:56 +00:00
|
|
|
t2 = t1.sum()
|
|
|
|
|
|
2025-12-06 17:09:25 +00:00
|
|
|
t3 = cp.vector(cp.value(1 / (v + 1)) for v in range(3))
|
2025-11-03 14:00:56 +00:00
|
|
|
t4 = ((t3 * t1) * 2).sum()
|
|
|
|
|
t5 = ((t3 * t1) * 2).magnitude()
|
|
|
|
|
|
|
|
|
|
ret = (t2, t4, t5)
|
|
|
|
|
|
2026-01-01 14:34:56 +00:00
|
|
|
out = [Store(r) for r in ret]
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-26 21:30:38 +00:00
|
|
|
il, variables = compile_to_dag(out, copapy.generic_sdb)
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-03 21:26:51 +00:00
|
|
|
# run program command
|
2025-10-19 21:24:14 +00:00
|
|
|
il.write_com(_binwrite.Command.RUN_PROG)
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-12-23 16:54:57 +00:00
|
|
|
for v in ret:
|
|
|
|
|
assert isinstance(v, cp.value)
|
|
|
|
|
add_read_command(il, variables, v.net)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-19 21:24:14 +00:00
|
|
|
il.write_com(_binwrite.Command.END_COM)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-12-08 13:09:14 +00:00
|
|
|
#print('* Data to runner:')
|
|
|
|
|
#il.print()
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
il.to_file('build/runner/test.copapy')
|
2025-09-30 21:18:58 +00:00
|
|
|
|
2025-11-12 23:29:48 +00:00
|
|
|
result = run_command(['build/runner/coparun', 'build/runner/test.copapy'])
|
2025-10-14 20:59:51 +00:00
|
|
|
print('* Output from runner:\n--')
|
2025-09-30 21:18:58 +00:00
|
|
|
print(result)
|
2025-10-14 20:59:51 +00:00
|
|
|
print('--')
|
2025-09-30 21:18:58 +00:00
|
|
|
|
2025-10-14 21:06:52 +00:00
|
|
|
assert 'Return value: 1' in result
|
|
|
|
|
#assert 'END_COM' in result
|
2025-09-26 21:25:51 +00:00
|
|
|
|
2025-08-29 20:58:50 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-10-02 21:23:07 +00:00
|
|
|
#test_example()
|
2025-10-01 21:09:49 +00:00
|
|
|
test_compile()
|