more complex but easier to write tests

This commit is contained in:
S Groesz 2020-10-14 07:55:47 +00:00
parent 2dca413258
commit f5f6813326
2 changed files with 77 additions and 23 deletions

View File

@ -289,11 +289,13 @@ class Bits:
raise ValueError("Integer must be between 0 and 255")
self.__value = var
elif isinstance(var, bytes):
for _byte in var:
self.__value += _byte
if ret > 255 or ret < 0:
raise ValueError("Sum value of bytes must be between 0"
" and 255")
if len(var) == 1:
self.__value = ord(var)
elif len(var) > 1:
raise ValueError("bytes must be single byte with integer"
" value between 0 and 255")
else:
self.__value = 0
elif (len(var) > 0) and (len(var) <=8):
for bit in range(0, len(var)):
self.__value += (int(bool(int(var[bit]))) *
@ -302,6 +304,7 @@ class Bits:
raise TypeError("Expected object with len <= 8")
def bin(self, pad=True, reverse=False):
from math import ceil
bitcount = self.__value.bit_length()
ret = ""
if pad and ((bitcount % 8) > 0):

View File

@ -2,31 +2,80 @@ from unittest import TestCase
from .context import Bytes
from .context import Bits, Bit
class TestBytes(TestCase):
def setUp(self):
pass
from random import randint
from math import ceil
test_ints = [0]
test_str = ["0"]
test_bytes = [b'\x00']
test_bytearray = [bytearray(b'\x00')]
test_list_of_Bits = [[Bits(0)]]
test_list_of_Bit = [[Bit(0)] * 8]
test_list_of_bool = [[False] * 8]
test_list_of_bytes = [[b'\x00']]
test_list_of_ints = [[0]]
test_list_of_str = [["0"] * 8]
for i in range(0, 100):
max_value = int("9" * randint(0, 25))
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)
test_str.append(pad_bits + bin(i)[2:])
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))
list_of_bytes.append(bite)
list_of_int.append(ord(bite))
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)
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}
def test_init(self):
"""
Test creation of Bytes object
"""
self.assertIsInstance(Bytes(b'abc'), Bytes,
"bytes: Bytes(b'abc') is Type(Bytes)")
self.assertIsInstance(Bytes(1234), Bytes,
"int: Bytes(1234) is Type(Bytes)")
self.assertIsInstance(Bytes("01010101110101"), Bytes,
"str: Bytes(\"01010101110101\") is Type(Bytes)")
self.assertIsInstance(Bytes(Bytes(1234)), Bytes,
"Bytes: Bytes(Bytes(1234)) is Type(Bytes)")
self.assertIsInstance(Bytes([True] * 30), Bytes,
"list (of bool): "
"Bytes([True] * 30) is Type(Bytes)")
self.assertIsInstance(Bytes([Bits(127)] * 30), Bytes,
"list (of Bits): "
"Bytes([Bits(127)] * 30) is Type(Bytes)")
self.assertIsInstance(Bytes([Bit(True)] * 30), Bytes,
"list (of bit): "
"Bytes([Bit(True)] * 30) is Type(Bytes)")
for title, tests in self.testcases.items():
with self.subTest(f"Create from {title}"):
for testcase in tests:
test = f"Bytes({testcase})"
compare = f"Bytes"
self.assertIsInstance(eval(test), eval(compare),
f"{test} is instance of {compare}")
with self.subTest(f"Create from bits.Bytes"):
for testcase in self.testcases["int"]:
test = f"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\")"):
Bytes("1234")
with self.assertRaises(ValueError, "Bytes(-1234)"):
@ -36,6 +85,8 @@ class TestBytes(TestCase):
"""
Test the comparison operators with Bytes objects
"""
with self.subTest("Bytes Type"):
self.assertEqual(Bytes(
with self.subTest("int Type"):
self.assertEqual(Bytes(1234), 1234,
"Bytes(1234) == 1234")