forked from claraj/hello_python_unittest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bin_to_dec.py
More file actions
57 lines (35 loc) · 1.72 KB
/
Copy pathtest_bin_to_dec.py
File metadata and controls
57 lines (35 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
import bin_to_dec
class TestBinaryToDecimal(unittest.TestCase):
def test_binary_decimal_conversion_with_binary_numbers(self):
# Python's bin method does the conversion from binary to decimal
# Loops are helpful - test a range of numbers
for d in range(100):
binary = bin(d) # in the format '0b10101'
binary = binary[2:] # Remove the inital '0b'
dec_output = bin_to_dec.decimal(binary)
self.assertEqual(d, dec_output)
# Lets test some larger numbers too
# Some 'round numbers' and some other large numbers
test_vals = [4000, 4001, 4002, 1024, 1099511627776, 1099511627777, 1099511627775]
for d in test_vals:
binary = bin(d) # in the format '0b10101'
binary = binary[2:] # Remove the inital '0b'
dec_output = bin_to_dec.decimal(binary)
self.assertEqual(d, dec_output)
# And test with some actual strings
test_bin_str = [ '101010', '1111', '000111', '0', '1']
expected_dec = [ 42, 15, 7, 0, 1]
for binary_input, expected_dec_output in zip( test_bin_str, expected_dec) :
dec = bin_to_dec.decimal(binary_input)
self.assertEqual(dec, expected_dec_output)
def test_binary_decimal_conversion_with_invalid_input(self):
# Verify a value error is raised with strings that are not made entirely of 0 and 1.
valid = '010101'
valid2 = '1111111'
invalid = [ '123456', '101010012', 'abc', '@#$%$%^%^&']
for invalid_input in invalid:
with self.assertRaises(ValueError):
bin_to_dec.decimal(invalid_input)
if __name__ == '__main__':
unittest.main()