2014-06-29 00:41:13 +02:00
|
|
|
"""Tests for the binary exercise
|
|
|
|
|
|
|
|
|
|
Implementation note:
|
|
|
|
|
If the argument to parse_binary isn't a valid binary number the
|
|
|
|
|
function should raise a ValueError with a meaningful error message.
|
|
|
|
|
"""
|
2013-08-08 16:44:16 -05:00
|
|
|
import unittest
|
|
|
|
|
|
2014-06-29 00:41:13 +02:00
|
|
|
from binary import parse_binary
|
2014-06-11 15:06:22 +02:00
|
|
|
|
2014-02-22 10:55:33 +08:00
|
|
|
|
2013-08-08 16:44:16 -05:00
|
|
|
class BinaryTests(unittest.TestCase):
|
|
|
|
|
def test_binary_1_is_decimal_1(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("1"), 1)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_10_is_decimal_2(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("10"), 2)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_11_is_decimal_3(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("11"), 3)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_100_is_decimal_4(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("100"), 4)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_1001_is_decimal_9(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("1001"), 9)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_11010_is_decimal_26(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("11010"), 26)
|
2013-08-08 16:44:16 -05:00
|
|
|
|
|
|
|
|
def test_binary_10001101000_is_decimal_1128(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(parse_binary("10001101000"), 1128)
|
2014-06-29 00:41:13 +02:00
|
|
|
|
2015-12-02 18:03:08 +01:00
|
|
|
def test_invalid_binary_text_only(self):
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
parse_binary("carrot")
|
2013-08-08 16:44:16 -05:00
|
|
|
|
2015-12-02 18:03:08 +01:00
|
|
|
def test_invalid_binary_number_not_base2(self):
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
parse_binary("102011")
|
2013-08-08 16:44:16 -05:00
|
|
|
|
2015-12-02 18:03:08 +01:00
|
|
|
def test_invalid_binary_numbers_with_text(self):
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
parse_binary("10nope")
|
2015-12-02 18:03:08 +01:00
|
|
|
|
|
|
|
|
def test_invalid_binary_text_with_numbers(self):
|
2017-10-21 03:17:15 -05:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
|
parse_binary("nope10")
|
2015-12-02 18:03:08 +01:00
|
|
|
|
2016-11-29 09:44:47 +01:00
|
|
|
|
2013-08-08 16:44:16 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|