~dricottone/numberplay

numberplay/test.py -rw-r--r-- 4.4 KiB
44371135Dominic Ricottone Solution 2 years ago
                                                                                
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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()