import unittest
import main
class TestAddDigits(unittest.TestCase):
"""add_digits(a, b, carry)"""
def test_simple_add(self):
# only test 0-8, since adding 1 to 9 would carry
for a in range(0, 9):
with self.subTest(a=a):
self.assertEqual(main.add_digits(a, 1, 0), (0, a+1, ))
def test_carry(self):
# only test 1-9, since adding 0 to 9 would not carry
for a in range(1,10):
with self.subTest(a=a):
self.assertEqual(main.add_digits(a, 9, 0)[0], 1)
def test_large_add(self):
# only test 1-9, since this test uses a-1
for a in range(1,10):
with self.subTest(a=a):
self.assertEqual(main.add_digits(a, 9, 0)[1], a-1)
def test_large_add_alt(self):
for a in range(0,10):
with self.subTest(a=a):
self.assertEqual(main.add_digits(a, 9, 1)[1], a)
class TestSolution(unittest.TestCase):
"""Solution()"""
def test_str(self):
s = main.Solution(n=1, u=2, m=3, b=4, e=5, r=6, p=7, l=8, a=9, y=0)
self.assertEqual(str(s), "123 + 456 = 7890")
def test_copy(self):
s = main.Solution(n=1)
c = s.copy()
c['u'] = 2
self.assertEqual(s.u, None)
self.assertEqual(c.u, 2)
def test_get_wrong_name(self):
with self.assertRaises(AttributeError):
main.Solution()['x']
def test_set_wrong_name(self):
with self.assertRaises(AttributeError):
main.Solution()['x'] = 1
def test_set_wrong_name_wrong_value(self):
with self.assertRaises(AttributeError):
main.Solution()['x'] = None
def test_raise_leading_0(self):
with self.subTest(a="n"):
with self.assertRaises(ValueError):
main.Solution(n=0)
with self.subTest(a="b"):
with self.assertRaises(ValueError):
main.Solution(b=0)
with self.subTest(a="p"):
with self.assertRaises(ValueError):
main.Solution(p=0)
with self.assertRaises(ValueError):
main.Solution(m=10)
def test_raise_10(self):
with self.assertRaises(ValueError):
main.Solution(m=10)
def test_raise_neg1(self):
with self.assertRaises(ValueError):
main.Solution(m=-1)
def test_raise_set_leading_0(self):
for a in ["n", "b", "p"]:
with self.subTest(a=a):
with self.assertRaises(ValueError):
main.Solution()[a] = 0
def test_raise_set_10(self):
with self.assertRaises(ValueError):
main.Solution()["m"] = 10
def test_raise_set_neg1(self):
with self.assertRaises(ValueError):
main.Solution()["m"] = -1
def test_raise_set_none(self):
with self.assertRaises(ValueError):
main.Solution()["m"] = None
class TestIsUsed(unittest.TestCase):
"""Solution().is_used(n)"""
def test_raise_10(self):
with self.assertRaises(ValueError):
main.Solution().is_using(10)
def test_raise_neg1(self):
with self.assertRaises(ValueError):
main.Solution().is_using(-1)
def test_empty_solution(self):
for a in range(0, 10):
with self.subTest(a=a):
self.assertFalse(main.Solution().is_using(a))
def test_unused(self):
# only test 0-8, since this test uses a+1
for a in range(0, 9):
with self.subTest(a=a):
self.assertFalse(main.Solution(m=a+1).is_using(a))
def test_unused_alt(self):
# only test 1-9, since this test uses a-1
for a in range(1, 10):
with self.subTest(a=a):
self.assertFalse(main.Solution(m=a-1).is_using(a))
def test_copy_unused(self):
# only test 0-8, since this test uses a+1
for a in range(0, 9):
with self.subTest(a=a):
s = main.Solution(m=a+1)
self.assertFalse(s.copy().is_using(a))
def test_updated_copy_unused(self):
# only test 0-7, since this test uses a+1 and a+2
for a in range(0, 8):
with self.subTest(a=a):
s = main.Solution(m=a+1)
c = s.copy()
c['r'] = a+2
self.assertFalse(c.is_using(a))
def test_used(self):
for a in range(0, 10):
with self.subTest(a=a):
self.assertTrue(main.Solution(m=a).is_using(a))
def test_copy_used(self):
for a in range(0, 10):
with self.subTest(a=a):
s = main.Solution(m=a)
self.assertTrue(s.copy().is_using(a))
def test_updated_copy_used(self):
# only test 0-8, since this test uses a+1
for a in range(0, 9):
with self.subTest(a=a):
s = main.Solution(m=a+1)
c = s.copy()
c['r'] = a
self.assertTrue(c.is_using(a))
class TestCase1(unittest.TestCase):
def test_case(self):
"""N32 + B57 = 1L89"""
s = main.Solution(u=3, m=2, e=5, r=7, p=1, a=8, y=9)
self.assertEqual(str(s), "N32 + B57 = 1L89")
result = main.solve_column(s, 'n', 'b', 'l')
should_be = ["432 + 657 = 1089","632 + 457 = 1089"]
self.assertEqual([str(r) for r in result], should_be)
if __name__ == '__main__':
unittest.main()