From 6e937879c269d2d91c19e0ef6065fd9f5ca53a9d Mon Sep 17 00:00:00 2001 From: S Groesz Date: Wed, 21 Oct 2020 05:34:06 +0000 Subject: [PATCH] tests passing --- Makefile | 1 + bits/main.py | 13 ++++++++++++- tests/test_bytes.py | 33 +++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 26a6c9f..5340086 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ init: test: python -m unittest -v tests/test_bit.py python -m unittest -v tests/test_bits.py + python -m unittest -v tests/test_bytes.py .PHONY: init test diff --git a/bits/main.py b/bits/main.py index 8664e85..f81cb76 100644 --- a/bits/main.py +++ b/bits/main.py @@ -463,6 +463,13 @@ class Bits: """ return self.__r_to_l + @property + def Bit(self): + """ + Returns list(of Bit, self) + """ + return list(self) + @rtl.setter def rtl(self, msb_last): """ @@ -636,6 +643,10 @@ class Bytes: elif isinstance(var, bytes): retvalue = bytearray(var) elif isinstance(var, str): + import re + if not re.match('[01]*$', var): + raise TypeError("Bytes(str): str must contain only binary " + "characters [0,1]") if len(var) % 8 > 0: var = ("0" * (8 - (len(var) % 8))) + var bitelist = [] @@ -652,7 +663,7 @@ class Bytes: bitstring = "" for bit in var: bitstring = bitstring + str(Bit(bit)) - retvalue += bytearray(Bytes(bitstring)) + retvalue += bytearray(bytes(Bytes(bitstring))) else: for item in var: if isinstance(item, (int, Bits, Bytes)): diff --git a/tests/test_bytes.py b/tests/test_bytes.py index f29e3ee..f809314 100644 --- a/tests/test_bytes.py +++ b/tests/test_bytes.py @@ -10,7 +10,7 @@ class TestBytes(TestCase): from math import ceil test_ints = [0] test_str = ["0"] - test_bytes = [b'0'] + test_bytes = [b''] test_bytearray = [] test_list_of_Bits = [] test_list_of_Bit = [] @@ -64,30 +64,43 @@ class TestBytes(TestCase): """ for title, tests in self.testcases.items(): with self.subTest(f"Create from {title}"): + # print("\tCreate Bytes from " + title) for testcase in tests: - test = f"Bytes({testcase})" + test = "Bytes(testcase)" compare = f"Bytes" - self.assertIsInstance(eval(test), eval(compare), - f"{test} is instance of {compare}") + #if not isinstance(testcase, int) and len(testcase) > 8: + # print(f"\t\t{testcase[0:9]}...") + #else: + # print(f"\t\t{testcase}") + try: + self.assertIsInstance(eval(test), eval(compare), + f"{test} is instance of {compare}") + except: + import pdb + pdb.set_trace() with self.subTest(f"Create from bits.Bytes"): + #print("\tCreate from Bytes object") for testcase in self.testcases["int"]: - test = f"Bytes(Bytes({testcase}))" + #print(f"\t\tBytes(Bytes({testcase}))") + test = "Bytes(Bytes(testcase))" compare = f"Bytes" self.assertIsInstance(eval(test), eval(compare), f"{test} is instance of {compare}") def test_errors(self): - with self.assertRaises(TypeError, "Bytes(\"1234\")"): + with self.assertRaises(TypeError, msg="Bytes(\"1234\")"): Bytes("1234") - with self.assertRaises(ValueError, "Bytes(-1234)"): - Bytes(-1234) def test_comparison_operators(self): """ Test the comparison operators with Bytes objects """ with self.subTest("Bytes Type"): - pass + for i in range(0, len(self.testcases["int"])): + self.assertEqual(Bytes(self.testcases["int"][i]), + Bytes(self.testcases["bytes"][i]), + (f"Bytes({self.testcases['int'][i]}) == " + f"Bytes({self.testcases['bytes'][i]})")) with self.subTest("int Type"): self.assertEqual(Bytes(1234), 1234, "Bytes(1234) == 1234") @@ -111,7 +124,7 @@ class TestBytes(TestCase): "Bytes(512) >= 512") self.assertLess(123, Bytes(125), "123 < Bytes(125)") - self.assertlessEqual(1024, Bytes(2048), + self.assertLessEqual(1024, Bytes(2048), "1024 <= Bytes(2048)") self.assertLessEqual(4092, Bytes(4092), "4092 <= Bytes(4092)")