tests passing

This commit is contained in:
S Groesz 2020-10-21 05:34:06 +00:00
parent de72cfef04
commit 6e937879c2
3 changed files with 36 additions and 11 deletions

View File

@ -4,6 +4,7 @@ init:
test: test:
python -m unittest -v tests/test_bit.py python -m unittest -v tests/test_bit.py
python -m unittest -v tests/test_bits.py python -m unittest -v tests/test_bits.py
python -m unittest -v tests/test_bytes.py
.PHONY: init test .PHONY: init test

View File

@ -463,6 +463,13 @@ class Bits:
""" """
return self.__r_to_l return self.__r_to_l
@property
def Bit(self):
"""
Returns list(of Bit, self)
"""
return list(self)
@rtl.setter @rtl.setter
def rtl(self, msb_last): def rtl(self, msb_last):
""" """
@ -636,6 +643,10 @@ class Bytes:
elif isinstance(var, bytes): elif isinstance(var, bytes):
retvalue = bytearray(var) retvalue = bytearray(var)
elif isinstance(var, str): 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: if len(var) % 8 > 0:
var = ("0" * (8 - (len(var) % 8))) + var var = ("0" * (8 - (len(var) % 8))) + var
bitelist = [] bitelist = []
@ -652,7 +663,7 @@ class Bytes:
bitstring = "" bitstring = ""
for bit in var: for bit in var:
bitstring = bitstring + str(Bit(bit)) bitstring = bitstring + str(Bit(bit))
retvalue += bytearray(Bytes(bitstring)) retvalue += bytearray(bytes(Bytes(bitstring)))
else: else:
for item in var: for item in var:
if isinstance(item, (int, Bits, Bytes)): if isinstance(item, (int, Bits, Bytes)):

View File

@ -10,7 +10,7 @@ class TestBytes(TestCase):
from math import ceil from math import ceil
test_ints = [0] test_ints = [0]
test_str = ["0"] test_str = ["0"]
test_bytes = [b'0'] test_bytes = [b'']
test_bytearray = [] test_bytearray = []
test_list_of_Bits = [] test_list_of_Bits = []
test_list_of_Bit = [] test_list_of_Bit = []
@ -64,30 +64,43 @@ class TestBytes(TestCase):
""" """
for title, tests in self.testcases.items(): for title, tests in self.testcases.items():
with self.subTest(f"Create from {title}"): with self.subTest(f"Create from {title}"):
# print("\tCreate Bytes from " + title)
for testcase in tests: for testcase in tests:
test = f"Bytes({testcase})" test = "Bytes(testcase)"
compare = f"Bytes" compare = f"Bytes"
#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), self.assertIsInstance(eval(test), eval(compare),
f"{test} is instance of {compare}") f"{test} is instance of {compare}")
except:
import pdb
pdb.set_trace()
with self.subTest(f"Create from bits.Bytes"): with self.subTest(f"Create from bits.Bytes"):
#print("\tCreate from Bytes object")
for testcase in self.testcases["int"]: for testcase in self.testcases["int"]:
test = f"Bytes(Bytes({testcase}))" #print(f"\t\tBytes(Bytes({testcase}))")
test = "Bytes(Bytes(testcase))"
compare = f"Bytes" compare = f"Bytes"
self.assertIsInstance(eval(test), eval(compare), self.assertIsInstance(eval(test), eval(compare),
f"{test} is instance of {compare}") f"{test} is instance of {compare}")
def test_errors(self): def test_errors(self):
with self.assertRaises(TypeError, "Bytes(\"1234\")"): with self.assertRaises(TypeError, msg="Bytes(\"1234\")"):
Bytes("1234") Bytes("1234")
with self.assertRaises(ValueError, "Bytes(-1234)"):
Bytes(-1234)
def test_comparison_operators(self): def test_comparison_operators(self):
""" """
Test the comparison operators with Bytes objects Test the comparison operators with Bytes objects
""" """
with self.subTest("Bytes Type"): 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"): with self.subTest("int Type"):
self.assertEqual(Bytes(1234), 1234, self.assertEqual(Bytes(1234), 1234,
"Bytes(1234) == 1234") "Bytes(1234) == 1234")
@ -111,7 +124,7 @@ class TestBytes(TestCase):
"Bytes(512) >= 512") "Bytes(512) >= 512")
self.assertLess(123, Bytes(125), self.assertLess(123, Bytes(125),
"123 < Bytes(125)") "123 < Bytes(125)")
self.assertlessEqual(1024, Bytes(2048), self.assertLessEqual(1024, Bytes(2048),
"1024 <= Bytes(2048)") "1024 <= Bytes(2048)")
self.assertLessEqual(4092, Bytes(4092), self.assertLessEqual(4092, Bytes(4092),
"4092 <= Bytes(4092)") "4092 <= Bytes(4092)")