tests passing; improve doc

This commit is contained in:
2020-11-23 07:51:45 +00:00
parent 97f7d58dd7
commit 66aa7ee915
2 changed files with 317 additions and 57 deletions

View File

@@ -1,46 +1,81 @@
from unittest import TestCase
from .context import Bits, Bytes
from .context import Bit, Bits, Bytes
class TestBits(TestCase):
def setUp(self):
self.testObjects = [
{"bytes": b'\x7f',
"bits": "01111111",
"str": "01111111",
"int": 127,
"reverse": 254,
"bitsObject": Bits(127)
"bitsObject": Bits(127),
"list": [Bit(0),
Bit(1),
Bit(1),
Bit(1),
Bit(1),
Bit(1),
Bit(1),
Bit(1)
]
},
{"bytes": b'\xcf',
"bits": "11001111",
"str": "11001111",
"int": 207,
"reverse": 243,
"bitsObject": Bits(207)
"bitsObject": Bits(207),
"list": [1, 1, 0, 0, 1, 1, 1, 1]
},
{"bytes": b'{',
"bits": "01111011",
"str": "01111011",
"int": 123,
"reverse": 222,
"bitsObject": Bits(123)
"bitsObject": Bits(123),
"list": ["0", "1", "1", "1", "1", "0", "1", "1"]
},
{"bytes": b'<',
"bits": "00111100",
"str": "00111100",
"int": 60,
"reverse": 60,
"bitsObject": Bits(60)
"bitsObject": Bits(60),
"list": [False,
False,
True,
True,
True,
True,
False,
False
]
},
{"bytes": b'>',
"bits": "00111110",
"str": "00111110",
"int": 62,
"reverse": 124,
"bitsObject": Bits(62)
"bitsObject": Bits(62),
"list": [0,
False,
"1",
1,
Bit("1"),
Bit(True),
Bit(1),
Bit(False)
]
},
{"bytes": b'=',
"str": "00111101",
"int": 61,
"reverse": 188,
"bitsObject": Bits(61),
"list": [True, 1, 1, True, Bit(0), Bit(1)]
}
]
def test_class(self):
"""
Test various class features
"""
"""Test various class features"""
with self.subTest("Reject values > 255"):
self.assertRaises(ValueError, Bits, 256)
with self.subTest("Reject values < 0"):
@@ -54,58 +89,53 @@ class TestBits(TestCase):
self.assertEqual(len(Bits(0)), 8)
def test_bytes(self):
"""
Test conversion to bytes object
"""
"""Test conversion to bytes objects"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"])):
self.assertEqual(bytes(testcase["bitsObject"]),
testcase["bytes"])
def test_int(self):
"""
Test integer conversion
"""
"""Test integer conversion"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"])):
self.assertEqual(int(testcase["bitsObject"]), testcase["int"])
def test_str(self):
"""
Test string representation
"""
"""Test string representation"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"])):
s = str(testcase["bitsObject"])
self.assertEqual(s, testcase["bits"])
self.assertEqual(s, testcase["str"])
def test_list(self):
"""Test list conversion"""
for testcase in self.testObjects:
with self.subTest("testcase[\"list\"]: " + str(testcase["list"])):
self.assertEqual(testcase["bytes"],
bytes(Bits(testcase["list"])))
def test_bits(self):
"""
Test bit representation
"""
"""Test bit representation"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"])):
self.assertEqual(testcase["bitsObject"].bin(),
testcase["bits"])
testcase["str"])
with self.subTest("testcase[\"int\"]: " + str(testcase["int"]) \
+ " [without leading zeros]"):
self.assertEqual(testcase["bitsObject"].bin(pad=False),
testcase["bits"].lstrip("0"))
testcase["str"].lstrip("0"))
def test_reverse(self):
"""
Test the reverse function changes the object bitorder and value
"""
"""Test the reverse function changes the object bitorder and value"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"])):
testcase["bitsObject"].reverse()
self.assertEqual(testcase["bitsObject"].bin(),
testcase["bits"][::-1])
testcase["str"][::-1])
def test_membership_operators(self):
"""
Test the membership operator (x in y)
"""
"""Test the membership operator (x in y)"""
with self.subTest("should all be True"):
for i in range(1, 256):
self.assertTrue(i in Bits(255), f"Bits({i}) in Bits(255) fail")
@@ -145,9 +175,7 @@ class TestBits(TestCase):
"Bits(80) and Bits(64) == Bits(64)")
def test_comparisons(self):
"""
Test the comparison operators
"""
"""Test the comparison operators"""
for testcase in self.testObjects:
with self.subTest("testcase[\"int\"]: " + str(testcase["int"]) \
+ " [==]"):