2025-10-09 20:50:57 +00:00
|
|
|
from copapy import Write, CPVariable
|
2025-10-01 21:09:49 +00:00
|
|
|
import copapy
|
2025-09-30 21:18:58 +00:00
|
|
|
import subprocess
|
2025-10-01 21:09:49 +00:00
|
|
|
import struct
|
2025-10-03 21:09:25 +00:00
|
|
|
from copapy import binwrite
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-09-30 21:18:58 +00:00
|
|
|
def run_command(command: list[str], encoding: str = 'utf8') -> str:
|
2025-10-14 20:59:51 +00:00
|
|
|
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2025-09-30 21:18:58 +00:00
|
|
|
output, error = process.communicate()
|
|
|
|
|
|
2025-10-14 20:59:51 +00:00
|
|
|
assert not error, f"\n -Error occurred: {error.decode(encoding)}\n -Output: {output.decode(encoding)}"
|
2025-09-30 21:18:58 +00:00
|
|
|
return output.decode(encoding)
|
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))
|
|
|
|
|
|
|
|
|
|
# assert False
|
2025-10-02 21:23:07 +00:00
|
|
|
#example r1 42 A0 00 00
|
|
|
|
|
#example r2 41 88 00 00
|
2025-10-01 21:09:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-08 20:47:49 +00:00
|
|
|
def function(c1, c2):
|
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
|
|
|
|
|
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-10-08 20:47:49 +00:00
|
|
|
def test_compile():
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-09 20:50:57 +00:00
|
|
|
c1 = CPVariable(4)
|
|
|
|
|
c2 = CPVariable(2)
|
2025-10-08 20:47:49 +00:00
|
|
|
|
|
|
|
|
ret = function(c1, c2)
|
|
|
|
|
|
|
|
|
|
out = [Write(r) for r in ret]
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-04 20:57:45 +00:00
|
|
|
il, _ = copapy.compile_to_instruction_list(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-04 20:57:45 +00:00
|
|
|
il.write_com(binwrite.Command.RUN_PROG)
|
2025-10-01 21:09:49 +00:00
|
|
|
|
2025-10-03 21:09:25 +00:00
|
|
|
il.write_com(binwrite.Command.READ_DATA)
|
2025-10-01 21:09:49 +00:00
|
|
|
il.write_int(0)
|
|
|
|
|
il.write_int(36)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-12 21:21:34 +00:00
|
|
|
il.write_com(binwrite.Command.END_COM)
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-02 21:23:07 +00:00
|
|
|
print('* Data to runner:')
|
|
|
|
|
il.print()
|
2025-08-29 20:58:50 +00:00
|
|
|
|
2025-10-04 21:01:44 +00:00
|
|
|
il.to_file('bin/test.copapy')
|
2025-09-30 21:18:58 +00:00
|
|
|
|
2025-10-04 21:01:44 +00:00
|
|
|
result = run_command(['bin/coparun', 'bin/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-01 21:09:49 +00:00
|
|
|
assert 'Return value: 1' 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()
|