-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_sudoku_utils.py
160 lines (117 loc) · 6.65 KB
/
test_sudoku_utils.py
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
import unittest
from sudoku_utils import *
class SudokuUtilsTests(unittest.TestCase):
def test_cell_could_contain__cell_is_locked_with_value__returns_value(self):
cell = 0 + 0 + 0 + 0 + 0 + 0 + 4 + 0 + 0
value = 4
self.assertEqual(value, cell_could_contain(cell, value))
def test_cell_could_contain__cell_is_locked_with_different_value__returns_zero(self):
cell = 0 + 0 + 0 + 0 + 0 + 8 + 0 + 0 + 0
value = 4
self.assertEqual(0, cell_could_contain(cell, value))
def test_cell_could_contain__cell_does_not_contain_value__returns_zero(self):
cell = 256 + 128 + 64 + 32 + 0 + 8 + 4 + 2 + 1
value = 16
self.assertEqual(0, cell_could_contain(cell, value))
def test_cell_could_contain__cell_contains_value__returns_value(self):
cell = 256 + 128 + 64 + 32 + 0 + 8 + 4 + 2 + 1
value = 8
self.assertEqual(value, cell_could_contain(cell, value))
def test_get_row_ix__first_cell__on_first_row(self):
self.assertEqual(0, get_row_ix(0))
def test_get_row_ix__last_cell__on_last_row(self):
self.assertEqual(8, get_row_ix(80))
def test_get_row_ix__from_middle__on_middle_row(self):
self.assertEqual(4, get_row_ix(40))
def test_get_row_ix__from_right_top_corner__on_first_row(self):
self.assertEqual(0, get_row_ix(8))
def test_get_box_ix__first_cell__in_first_box(self):
self.assertEqual(0, get_box_ix(0))
def test_get_box_ix__last_cell__in_last_box(self):
self.assertEqual(8, get_box_ix(80))
def test_get_box_ix__from_middle__on_middle_box(self):
self.assertEqual(4, get_box_ix(40))
def test_get_box_ix__from_right_top_corner__on_second_box(self):
self.assertEqual(2, get_box_ix(8))
def test_get_col_ix__first_cell__in_first_col(self):
self.assertEqual(0, get_col_ix(0))
def test_get_col_ix__last_cell__in_last_col(self):
self.assertEqual(8, get_col_ix(80))
def test_get_col_ix__from_middle__in_middle_col(self):
self.assertEqual(4, get_col_ix(4))
def test_get_col_ix__from_right_top_corner__in_last_col(self):
self.assertEqual(8, get_col_ix(8))
def test_initialize__valid_input__returns_valid_table(self):
all = 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
expected_table = [all, all, 8, 64, all, 2, all, all, all,
all, all, 2, 1, 256, all, 4, 16, 32,
32, all, all, all, 128, 4, all, all, all,
1, all, 128, all, 16, 64, all, all, all,
all, 256, all, all, 8, all, all, 4, all,
all, all, all, 4, 2, all, 16, all, 128,
all, all, all, 2, 64, all, all, all, 4,
256, 128, 1, all, 4, 32, 64, all, all,
all, all, all, 128, all, 16, 8, all, all]
input = "004702000002190356600083000108057000090040030000320508000270003981036700000805400"
table = initialize(input)
self.assertEqual(expected_table, table)
def test_count_solved_numbers__easy_table__returns_solved_numbers(self):
all = 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
table = [all, all, 8, 64, all, 2, all, all, all,
all, all, 2, 1, 256, all, 4, 16, 32,
32, all, all, all, 128, 4, all, all, all,
1, all, 128, all, 16, 64, all, all, all,
all, 256, all, all, 8, all, all, 4, all,
all, all, all, 4, 2, all, 16, all, 128,
all, all, all, 2, 64, all, all, all, 4,
256, 128, 1, all, 4, 32, 64, all, all,
all, all, all, 128, all, 16, 8, all, all]
self.assertEqual(35, count_solved_numbers(table))
def test_count_set_bits__no_bits__returns_zero(self):
self.assertEqual(0, count_set_bits(0))
def test_count_set_bits__all_bits__returns_all(self):
all = 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
self.assertEqual(9, count_set_bits(all))
def test_count_set_bits__locked_value__returns_one(self):
locked_value = 128
self.assertEqual(1, count_set_bits(locked_value))
def test_convert_human_table_to_bit_table(self):
expected_table = [ 4, 16, 8, 64, 32, 2, 256, 128, 1,
128, 64, 2, 1, 256, 8, 4, 16, 32,
32, 1, 256, 16, 128, 4, 2, 64, 8,
1, 4, 128, 256, 16, 64, 32, 8, 2,
2, 256, 16, 32, 8, 128, 1, 4, 64,
8, 32, 64, 4, 2, 1, 16, 256, 128,
16, 8, 32, 2, 64, 256, 128, 1, 4,
256, 128, 1, 8, 4, 32, 64, 2, 16,
64, 2, 4, 128, 1, 16, 8, 32, 256]
table = [3, 5, 4, 7, 6, 2, 9, 8, 1,
8, 7, 2, 1, 9, 4, 3, 5, 6,
6, 1, 9, 5, 8, 3, 2, 7, 4,
1, 3, 8, 9, 5, 7, 6, 4, 2,
2, 9, 5, 6, 4, 8, 1, 3, 7,
4, 6, 7, 3, 2, 1, 5, 9, 8,
5, 4, 6, 2, 7, 9, 8, 1, 3,
9, 8, 1, 4, 3, 6, 7, 2, 5,
7, 2, 3, 8, 1, 5, 4, 6, 9]
self.assertEqual(expected_table, convert_human_table_to_bit_table(table))
def test_convert_bit_table_to_human_table(self):
expected_table = [3, 5, 4, 7, 6, 2, 9, 8, 1,
8, 7, 2, 1, 9, 4, 3, 5, 6,
6, 1, 9, 5, 8, 3, 2, 7, 4,
1, 3, 8, 9, 5, 7, 6, 4, 2,
2, 9, 5, 6, 4, 8, 1, 3, 7,
4, 6, 7, 3, 2, 1, 5, 9, 8,
5, 4, 6, 2, 7, 9, 8, 1, 3,
9, 8, 1, 4, 3, 6, 7, 2, 5,
7, 2, 3, 8, 1, 5, 4, 6, 9]
table = [ 4, 16, 8, 64, 32, 2, 256, 128, 1,
128, 64, 2, 1, 256, 8, 4, 16, 32,
32, 1, 256, 16, 128, 4, 2, 64, 8,
1, 4, 128, 256, 16, 64, 32, 8, 2,
2, 256, 16, 32, 8, 128, 1, 4, 64,
8, 32, 64, 4, 2, 1, 16, 256, 128,
16, 8, 32, 2, 64, 256, 128, 1, 4,
256, 128, 1, 8, 4, 32, 64, 2, 16,
64, 2, 4, 128, 1, 16, 8, 32, 256]
self.assertEqual(expected_table, convert_bit_table_to_human_table(table))