bits/tests/test_bytes.py

138 lines
5.9 KiB
Python
Raw Normal View History

2020-10-13 02:14:40 -05:00
from unittest import TestCase
from .context import Bytes
2020-10-14 02:55:47 -05:00
from .context import Bits, Bit
2020-10-13 02:14:40 -05:00
class TestBytes(TestCase):
def setUp(self):
2020-10-14 02:55:47 -05:00
from random import randint
from math import ceil
test_ints = [0]
test_str = ["0"]
2020-10-21 00:34:06 -05:00
test_bytes = [b'']
2020-10-16 01:28:51 -05:00
test_bytearray = []
test_list_of_Bits = []
test_list_of_Bit = []
test_list_of_bool = []
test_list_of_bytes = []
test_list_of_ints = []
test_list_of_str = []
2020-10-14 02:55:47 -05:00
for i in range(0, 100):
2020-10-14 23:07:34 -05:00
max_value = int("9" * randint(1, 25))
2020-10-14 02:55:47 -05:00
test_value = randint(1, max_value)
bitesize = ceil(test_value.bit_length() / 8)
pad_bits = "0" * ((bitesize * 8) - test_value.bit_length())
test_ints.append(test_value)
2020-10-16 01:28:51 -05:00
test_str.append(pad_bits + bin(test_value)[2:])
2020-10-14 02:55:47 -05:00
test_bytes.append(test_value.to_bytes(bitesize, "big"))
for bites in test_bytes:
list_of_Bits = []
list_of_Bit = []
list_of_bool = []
list_of_bytes = []
list_of_ints = []
list_of_str = []
for bite in bites:
list_of_Bits.append(Bits(bite))
2020-10-14 23:07:34 -05:00
list_of_bytes.append(bytes([bite]))
list_of_ints.append(bite)
2020-10-14 02:55:47 -05:00
list_of_str.append(Bits(bite).bin())
for bit in Bits(bite).bin():
list_of_Bit.append(Bit(bit))
list_of_bool.append(bool(Bit(bit)))
test_bytearray.append(bytearray(bites))
test_list_of_bytes.append(list_of_bytes)
test_list_of_Bits.append(list_of_Bits)
test_list_of_Bit.append(list_of_Bit)
2020-10-14 23:07:34 -05:00
test_list_of_ints.append(list_of_ints)
2020-10-14 02:55:47 -05:00
test_list_of_str.append(list_of_str)
self.testcases = {"int": test_ints,
"str": test_str,
"bytes": test_bytes,
"bytearray": test_bytearray,
"list of Bits": test_list_of_Bits,
"list of Bit": test_list_of_Bit,
"list of bool": test_list_of_bool,
"list of bytes": test_list_of_bytes,
"list of ints": test_list_of_ints,
"list of str": test_list_of_str}
2020-10-13 02:14:40 -05:00
def test_init(self):
"""
Test creation of Bytes object
"""
2020-10-14 02:55:47 -05:00
for title, tests in self.testcases.items():
with self.subTest(f"Create from {title}"):
2020-10-21 00:34:06 -05:00
# print("\tCreate Bytes from " + title)
2020-10-14 02:55:47 -05:00
for testcase in tests:
2020-10-21 00:34:06 -05:00
test = "Bytes(testcase)"
2020-10-14 02:55:47 -05:00
compare = f"Bytes"
2020-10-21 00:34:06 -05:00
#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()
2020-10-14 02:55:47 -05:00
with self.subTest(f"Create from bits.Bytes"):
2020-10-21 00:34:06 -05:00
#print("\tCreate from Bytes object")
2020-10-14 02:55:47 -05:00
for testcase in self.testcases["int"]:
2020-10-21 00:34:06 -05:00
#print(f"\t\tBytes(Bytes({testcase}))")
test = "Bytes(Bytes(testcase))"
2020-10-14 02:55:47 -05:00
compare = f"Bytes"
self.assertIsInstance(eval(test), eval(compare),
f"{test} is instance of {compare}")
def test_errors(self):
2020-10-21 00:34:06 -05:00
with self.assertRaises(TypeError, msg="Bytes(\"1234\")"):
2020-10-13 02:14:40 -05:00
Bytes("1234")
def test_comparison_operators(self):
"""
Test the comparison operators with Bytes objects
"""
2020-10-14 02:55:47 -05:00
with self.subTest("Bytes Type"):
2020-10-21 00:34:06 -05:00
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]})"))
2020-10-13 02:14:40 -05:00
with self.subTest("int Type"):
self.assertEqual(Bytes(1234), 1234,
"Bytes(1234) == 1234")
self.assertNotEqual(Bytes(1234), 4321,
"Bytes(1234) != 4321")
self.assertEqual(5678, Bytes(5678),
"5678 == Bytes(5678)")
self.assertNotEqual(8765, Bytes(5678),
"8765 != Bytes(5678)")
self.assertLess(Bytes(123), 128,
"Bytes(123) < 128")
self.assertLessEqual(Bytes(124), 125,
"Bytes(124) <= 125")
self.assertLessEqual(Bytes(125), 125,
"Bytes(125) <= 125")
self.assertGreater(Bytes(255), 128,
"Bytes(255) > 128")
self.assertGreaterEqual(Bytes(256), 255,
"Bytes(256) >= 255")
self.assertGreaterEqual(Bytes(512), 512,
"Bytes(512) >= 512")
self.assertLess(123, Bytes(125),
"123 < Bytes(125)")
2020-10-21 00:34:06 -05:00
self.assertLessEqual(1024, Bytes(2048),
2020-10-13 02:14:40 -05:00
"1024 <= Bytes(2048)")
self.assertLessEqual(4092, Bytes(4092),
"4092 <= Bytes(4092)")
self.assertGreater(499, Bytes(200),
"499 > Bytes(200)")
self.assertGreaterEqual(322, Bytes(122),
"322 >= Bytes(122)")
self.assertGreaterEqual(510, Bytes(510),
"510 >= Bytes(510)")