more complex but easier to write tests
This commit is contained in:
parent
2dca413258
commit
f5f6813326
13
bits/main.py
13
bits/main.py
|
@ -289,11 +289,13 @@ class Bits:
|
||||||
raise ValueError("Integer must be between 0 and 255")
|
raise ValueError("Integer must be between 0 and 255")
|
||||||
self.__value = var
|
self.__value = var
|
||||||
elif isinstance(var, bytes):
|
elif isinstance(var, bytes):
|
||||||
for _byte in var:
|
if len(var) == 1:
|
||||||
self.__value += _byte
|
self.__value = ord(var)
|
||||||
if ret > 255 or ret < 0:
|
elif len(var) > 1:
|
||||||
raise ValueError("Sum value of bytes must be between 0"
|
raise ValueError("bytes must be single byte with integer"
|
||||||
" and 255")
|
" value between 0 and 255")
|
||||||
|
else:
|
||||||
|
self.__value = 0
|
||||||
elif (len(var) > 0) and (len(var) <=8):
|
elif (len(var) > 0) and (len(var) <=8):
|
||||||
for bit in range(0, len(var)):
|
for bit in range(0, len(var)):
|
||||||
self.__value += (int(bool(int(var[bit]))) *
|
self.__value += (int(bool(int(var[bit]))) *
|
||||||
|
@ -302,6 +304,7 @@ class Bits:
|
||||||
raise TypeError("Expected object with len <= 8")
|
raise TypeError("Expected object with len <= 8")
|
||||||
|
|
||||||
def bin(self, pad=True, reverse=False):
|
def bin(self, pad=True, reverse=False):
|
||||||
|
from math import ceil
|
||||||
bitcount = self.__value.bit_length()
|
bitcount = self.__value.bit_length()
|
||||||
ret = ""
|
ret = ""
|
||||||
if pad and ((bitcount % 8) > 0):
|
if pad and ((bitcount % 8) > 0):
|
||||||
|
|
|
@ -2,31 +2,80 @@ from unittest import TestCase
|
||||||
|
|
||||||
from .context import Bytes
|
from .context import Bytes
|
||||||
|
|
||||||
|
from .context import Bits, Bit
|
||||||
|
|
||||||
class TestBytes(TestCase):
|
class TestBytes(TestCase):
|
||||||
def setUp(self):
|
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):
|
def test_init(self):
|
||||||
"""
|
"""
|
||||||
Test creation of Bytes object
|
Test creation of Bytes object
|
||||||
"""
|
"""
|
||||||
self.assertIsInstance(Bytes(b'abc'), Bytes,
|
for title, tests in self.testcases.items():
|
||||||
"bytes: Bytes(b'abc') is Type(Bytes)")
|
with self.subTest(f"Create from {title}"):
|
||||||
self.assertIsInstance(Bytes(1234), Bytes,
|
for testcase in tests:
|
||||||
"int: Bytes(1234) is Type(Bytes)")
|
test = f"Bytes({testcase})"
|
||||||
self.assertIsInstance(Bytes("01010101110101"), Bytes,
|
compare = f"Bytes"
|
||||||
"str: Bytes(\"01010101110101\") is Type(Bytes)")
|
self.assertIsInstance(eval(test), eval(compare),
|
||||||
self.assertIsInstance(Bytes(Bytes(1234)), Bytes,
|
f"{test} is instance of {compare}")
|
||||||
"Bytes: Bytes(Bytes(1234)) is Type(Bytes)")
|
with self.subTest(f"Create from bits.Bytes"):
|
||||||
self.assertIsInstance(Bytes([True] * 30), Bytes,
|
for testcase in self.testcases["int"]:
|
||||||
"list (of bool): "
|
test = f"Bytes(Bytes({testcase}))"
|
||||||
"Bytes([True] * 30) is Type(Bytes)")
|
compare = f"Bytes"
|
||||||
self.assertIsInstance(Bytes([Bits(127)] * 30), Bytes,
|
self.assertIsInstance(eval(test), eval(compare),
|
||||||
"list (of Bits): "
|
f"{test} is instance of {compare}")
|
||||||
"Bytes([Bits(127)] * 30) is Type(Bytes)")
|
|
||||||
self.assertIsInstance(Bytes([Bit(True)] * 30), Bytes,
|
def test_errors(self):
|
||||||
"list (of bit): "
|
|
||||||
"Bytes([Bit(True)] * 30) is Type(Bytes)")
|
|
||||||
with self.assertRaises(TypeError, "Bytes(\"1234\")"):
|
with self.assertRaises(TypeError, "Bytes(\"1234\")"):
|
||||||
Bytes("1234")
|
Bytes("1234")
|
||||||
with self.assertRaises(ValueError, "Bytes(-1234)"):
|
with self.assertRaises(ValueError, "Bytes(-1234)"):
|
||||||
|
@ -36,6 +85,8 @@ class TestBytes(TestCase):
|
||||||
"""
|
"""
|
||||||
Test the comparison operators with Bytes objects
|
Test the comparison operators with Bytes objects
|
||||||
"""
|
"""
|
||||||
|
with self.subTest("Bytes Type"):
|
||||||
|
self.assertEqual(Bytes(
|
||||||
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")
|
||||||
|
|
Loading…
Reference in New Issue