From ab724721c04be6bd708ffa227dc33d68964c59fb Mon Sep 17 00:00:00 2001 From: Johannes Maier Date: Sat, 13 Jan 2024 13:46:09 +0100 Subject: [PATCH] Add more tests for add and addi --- tests/common.py | 5 +++-- tests/test_add.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/common.py b/tests/common.py index 0a00f7f..2588e05 100644 --- a/tests/common.py +++ b/tests/common.py @@ -37,8 +37,9 @@ def instr_r(opcode, reg1, reg2): return bytes([opcode, reg1, 0, 0, reg2, 0, 0, 0]) -def exec_program(program: bytes) -> int | None: - context.log_level = 'debug' +def exec_program(program: bytes, debug: bool = False) -> int | None: + if debug: + context.log_level = 'debug' with remote("localhost", 1337, fam="ipv4") as p: p.recvuntil(b"Password: ", timeout=1) p.sendline(b"1234") diff --git a/tests/test_add.py b/tests/test_add.py index aeb4583..a47d1f8 100644 --- a/tests/test_add.py +++ b/tests/test_add.py @@ -10,6 +10,25 @@ class BasicInstructionTest(unittest.TestCase): self.assertIsNotNone(exit_code, "Connection timeout!") self.assertEqual(exit_code, val & 0xFF, "Computed the wrong result!") + def test_multiple_addi(self): + for vals in [[0] * 16, [1] * 8, [0x50] * 256, [0x543210] * 7, [0x6543210] * 5, + [0x1337, 0x69, 0x42, 0x420, 0xbeef]]: + program = b'' + val_sum = sum(vals) + for val in vals: + program += instr_i(Opcode.ADDI, Register.A, val) + exit_code = exec_program(program) + self.assertIsNotNone(exit_code, "Connection timeout!") + self.assertEqual(exit_code, val_sum & 0xFF, "Computed the wrong result!") + + def test_add(self): + for val1, val2 in [(0, 0), (5, 8), (0xFFFFFFFF, 0xFFFFFFFF), (0xFFFFFFFF, 1)]: + program = instr_i(Opcode.ADDI, Register.A, val1) + instr_i(Opcode.ADDI, Register.B, val2) + instr_r( + Opcode.ADD, Register.A, Register.B) + exit_code = exec_program(program) + self.assertIsNotNone(exit_code, "Connection timeout!") + self.assertEqual(exit_code, (val1 + val2) & 0xFF, "Computed the wrong result!") + if __name__ == '__main__': unittest.main()