jimweirich / sudoku

A Simple Sudoku Solver

This URL has Read+Write access

jimweirich (author)
Wed Oct 07 12:31:33 -0700 2009
commit  2147dc66086d61bfd76afdb116db8164955cce41
tree    9c18a24cf52a64caf0d48957eb4f7a39c08e95bb
parent  face3e482fd15bc6ef874c4ed5b5917db12af1bb
sudoku / sudoku_test.rb
100644 265 lines (217 sloc) 5.608 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require 'rubygems'
require 'test/unit'
require 'shoulda'
 
require 'sudoku'
 
class CellTest < Test::Unit::TestCase
  def create_group_with(cell, *numbers)
    g = Group.new
    g << cell
    numbers.each do |n|
      c = Cell.new
      c.number = n
      g << c
    end
    g
  end
 
  context 'a cell' do
    setup do
      @cell = Cell.new("C25")
    end
 
    should 'know its name' do
      assert_equal "C25", @cell.to_s
    end
 
    should 'be inspectable' do
      assert_equal "C25", @cell.inspect
    end
 
    should 'initially have no number' do
      assert_nil @cell.number
    end
 
    should 'be able to set number' do
      @cell.number = 4
      assert_equal 4, @cell.number
    end
 
    should 'accept a zero as no number' do
      @cell.number = 0
      assert_nil @cell.number
    end
 
    should 'report all numbers are available' do
      assert_equal( Set[*(1..9)], @cell.available_numbers )
    end
 
    context 'within a group' do
      setup do
        @group = create_group_with(@cell, 3, 4, 5)
      end
 
      should 'report available numbers not in the group' do
        assert_equal Set[1, 2, 6, 7, 8, 9], @cell.available_numbers
      end
 
      should 'report no available numbers if a number has been assigned' do
        @cell.number = 6
        assert_equal Set[], @cell.available_numbers
      end
 
    end
  end
end
 
 
class GroupTest < Test::Unit::TestCase
  context 'a group of cells' do
    setup do
      @group = Group.new
    end
    
    should 'exist' do
      assert_not_nil @group
    end
 
    context 'with cells' do
      setup do
        @cells = (1..10).map { |i| Cell.new("C#{i}") }
        @cells.each do |c| @group << c end
      end
 
      should 'give a list of numbers' do
        assert_equal Set[], @group.numbers
      end
 
      context 'with some numbers' do
        setup do
          @cells[0].number = 3
          @cells[3].number = 6
        end
 
        should 'give a list of the remaining missing numbers' do
          assert_equal [3, 6], @group.numbers.sort
        end
      end
 
    end
 
  end
end
 
module Puzzles
  Wiki =
    "53 7 " +
    "6 195 " +
    " 98 6 " +
    "8 6 3" +
    "4 8 3 1" +
    "7 2 6" +
    " 6 28 " +
    " 419 5" +
    " 8 79"
 
  Medium =
    " 4 7 3 " +
    " 85 1 " +
    " 15 3 9 " +
    "5 7 21 " +
    " 6 8 " +
    " 81 6 9" +
    " 2 4 57 " +
    " 7 29 " +
    " 5 7 8 "
  
  Evil =
    " 53 694 " +
    " 3 1 6" +
    " 3 " +
    "7 9 " +
    " 1 3 2 " +
    " 2 7" +
    " 6 " +
    "8 7 5 " +
    " 436 81 "
end
 
class BoardTest < Test::Unit::TestCase
  include Puzzles
 
  context 'a board' do
    setup do
      @board = Board.new
    end
 
    should 'be inspectable' do
      assert_match %r(^<Board \.{81}>$), @board.inspect
    end
 
    should 'initially give all 9 numbers for all cells' do
      @board.each do |cell|
        assert_equal((1..9).to_a, cell.available_numbers.sort)
      end
    end
 
    should 'parse a string representation of the puzzle' do
      @board.parse(Wiki)
      assert_equal "5 3 . . 7 . . . . \n" +
        "6 . . 1 9 5 . . . \n" +
        ". 9 8 . . . . 6 . \n\n" +
        "8 . . . 6 . . . 3 \n" +
        "4 . . 8 . 3 . . 1 \n" +
        "7 . . . 2 . . . 6 \n\n" +
        ". 6 . . . . 2 8 . \n" +
        ". . . 4 1 9 . . 5 \n" +
        ". . . . 8 . . 7 9 \n\n",
        @board.to_s
    end
 
    should 'solve the Wikipedia Puzzle' do
      board = Board.new.parse(Wiki)
      board.solve
 
      assert board.solved?
      assert_equal "534678912672195348198342567" +
        "859761423426853791713924856" +
        "961537284287419635345286179",
        board.encoding
    end
 
    should 'solve the Wikipedia Puzzle with DOS line endings' do
      board = Board.new.parse(open("puzzles/wiki_dos.sud") { |f| f.read })
      board.solve
 
      assert board.solved?
      assert_equal "534678912672195348198342567" +
        "859761423426853791713924856" +
        "961537284287419635345286179",
        board.encoding
    end
 
    should 'solve the Medium Puzzle' do
      board = Board.new.parse(Medium)
      board.solve
 
      assert board.solved?
      assert_equal "942187635368594127715236498" +
        "593478216476921853281365749" +
        "829643571137852964654719382",
        board.encoding
    end
 
    should 'solve the Evil Puzzle' do
      board = Board.new.parse(Evil)
      board.solve
 
      assert board.solved?
      assert_equal "285376941439125786176849235" +
        "752981364618734529394562817" +
        "567213498821497653943658172",
        board.encoding
    end
 
  end
end
 
class SudokuSolverTest < Test::Unit::TestCase
  WikiPuzzleFile = 'puzzles/wiki.sud'
      SOLUTION = %{5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
 
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
 
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9}
      
 
  def redirect_output
    old_stdout = $stdout
    $stdout = StringIO.new
    yield
    $stdout.string
  ensure
    $stdout = old_stdout
  end
 
  context 'a solver' do
    setup do
      @solver = SudokuSolver.new
    end
 
    should 'solve a puzzle' do
      result = redirect_output do
        @solver.run([WikiPuzzleFile])
      end
      assert_match(/#{SOLUTION}/, result)
    end
 
    should 'complain if no file given' do
      result = redirect_output do
        assert_raise(SystemExit) do
          @solver.run([])
        end
      end
      assert_match(/Usage:/, result)
    end
  end
end