2023-04-01 23:54:05 -07:00
|
|
|
from BoundedInt import BoundedInt
|
2023-04-01 23:30:05 -07:00
|
|
|
|
|
|
|
|
class BoundedIntTester:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
pass
|
2023-04-03 10:28:10 -07:00
|
|
|
|
2023-04-01 23:30:05 -07:00
|
|
|
def test1(self):
|
2023-04-03 10:28:10 -07:00
|
|
|
bi1 = BoundedInt(0xdeadbeef, min = (0xdeadbeef-1), max = (0xdeadbeef+1))
|
2023-04-01 23:30:05 -07:00
|
|
|
if int(bi1) == 0xdeadbeef:
|
|
|
|
|
print(f"✅ Test 1 passed!")
|
|
|
|
|
else:
|
|
|
|
|
print(f"❌ Test 1 failed, int(bi1) = {int(bi1)}, expected 0xdeadbeef")
|
|
|
|
|
|
|
|
|
|
def test2(self):
|
2023-04-01 23:54:05 -07:00
|
|
|
"""
|
|
|
|
|
Test that addition works for two BoundedInts, and throws an error if the bounds are exceeded.
|
|
|
|
|
"""
|
2023-04-01 23:30:05 -07:00
|
|
|
bi2 = BoundedInt(1, min = 0, max = 1)
|
|
|
|
|
bi3 = BoundedInt(1, min = 0, max = 1)
|
|
|
|
|
try:
|
|
|
|
|
bi4 = bi2 + bi3 # 1 + 1 = 2, which is more than max = 1
|
|
|
|
|
print(f"❌ Test 2 failed!")
|
|
|
|
|
except ValueError as err:
|
|
|
|
|
print(f"✅ Test 2 passed!")
|
|
|
|
|
|
|
|
|
|
def test3(self):
|
2023-04-01 23:54:05 -07:00
|
|
|
"""
|
|
|
|
|
Test that addition works for two BoundedInts, and throws an error if the bounds are exceeded.
|
|
|
|
|
"""
|
2023-04-01 23:30:05 -07:00
|
|
|
bi4 = BoundedInt(1, min = 0, max = 1)
|
2023-04-03 10:28:10 -07:00
|
|
|
bi5 = BoundedInt(2, min = 0, max = 2)
|
2023-04-01 23:30:05 -07:00
|
|
|
try:
|
|
|
|
|
bi6 = bi4 - bi5 # 1 - 2 = (-1), which is less than min = 0
|
|
|
|
|
print(f"❌ Test 3 failed!")
|
|
|
|
|
except ValueError as err:
|
|
|
|
|
print(f"✅ Test 3 passed!")
|
|
|
|
|
|
2023-04-01 23:54:05 -07:00
|
|
|
def test4(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that addition works for a BoundedInt and a regular int, and throws an error if the bounds are exceeded.
|
|
|
|
|
"""
|
|
|
|
|
bi7 = BoundedInt(1, min = 0, max = 1)
|
|
|
|
|
bi8 = 2
|
|
|
|
|
try:
|
|
|
|
|
bi9 = bi7 + bi8
|
|
|
|
|
print(f"❌ Test 4 failed!")
|
|
|
|
|
except ValueError as err:
|
|
|
|
|
print(f"✅ Test 4 passed!")
|
|
|
|
|
|
|
|
|
|
def test5(self):
|
|
|
|
|
"""
|
|
|
|
|
Test that subtraction works for a BoundedInt and a regular int, and throws an error if the bounds are exceeded.
|
|
|
|
|
"""
|
|
|
|
|
bi10 = BoundedInt(1, min = 0, max = 1)
|
|
|
|
|
bi11 = 2
|
|
|
|
|
try:
|
|
|
|
|
bi12 = bi10 - 11
|
|
|
|
|
print(f"❌ Test 5 failed!")
|
|
|
|
|
except ValueError as err:
|
|
|
|
|
print(f"✅ Test 5 passed!")
|
|
|
|
|
|
2023-04-01 23:30:05 -07:00
|
|
|
tester = BoundedIntTester()
|
|
|
|
|
tester.test1()
|
|
|
|
|
tester.test2()
|
2023-04-01 23:54:05 -07:00
|
|
|
tester.test3()
|
|
|
|
|
tester.test4()
|
|
|
|
|
tester.test5()
|