From 8cf381ec61ba79a31ef6150c02ab3ad8a44f3002 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Sun, 18 Apr 2021 00:57:53 +0200 Subject: [PATCH] added basic tests --- test/__init__.py | 0 test/test_helpers.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/__init__.py create mode 100644 test/test_helpers.py diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_helpers.py b/test/test_helpers.py new file mode 100644 index 0000000..1166e50 --- /dev/null +++ b/test/test_helpers.py @@ -0,0 +1,30 @@ +from unittest import TestCase + +from riscemu.helpers import * + + +class Test(TestCase): + def test_int_to_bytes(self): + self.assertEqual(int_to_bytes(-1), bytearray([0xff] * 4), "-1") + self.assertEqual(int_to_bytes(1), bytearray([0, 0, 0, 1]), "1") + self.assertEqual(int_to_bytes(1231132), bytearray(b'\x00\x12\xc9\x1c'), "random number") + self.assertEqual(int_to_bytes(-1231132), bytearray(b'\xff\xed6\xe4'), "random negative number") + + def test_int_from_bytes(self): + self.assertEqual(bytearray([0xff] * 4), int_to_bytes(-1), "-1") + self.assertEqual(bytearray([0, 0, 0, 1]), int_to_bytes(1), "1") + self.assertEqual(bytearray(b'\x00\x12\xc9\x1c'), int_to_bytes(1231132), "random number") + self.assertEqual(bytearray(b'\xff\xed6\xe4'), int_to_bytes(-1231132), "random negative number") + + def test_to_unsigned(self): + self.assertEqual(to_unsigned(-1), 0xFFFFFFFF) + self.assertEqual(to_unsigned(-100), 0xffffff9c) + self.assertEqual(to_unsigned(1), 1) + self.assertEqual(to_unsigned(0xffffffff), 0xffffffff) + self.assertEqual(to_unsigned(0xffed36e4), 0xffed36e4) + + def test_to_signed(self): + self.assertEqual(to_signed(0xFFFFFFFF), -1) + self.assertEqual(to_signed(0xffed36e4), -1231132) + self.assertEqual(to_signed(0x0FFFFFFF), 0x0FFFFFFF) +