Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94ff1e3

Browse files
Merge pull request TheAlgorithms#713 from defaude/test/Backtracking/Sudoku
Add proper test to Backtracking/Sudoku
2 parents 177c476 + 2b3db6c commit 94ff1e3

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

‎Backtracking/Sudoku.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,4 @@ class Sudoku {
7272
}
7373
}
7474

75-
function main () {
76-
// main function with an example
77-
const sudokuBoard = new Sudoku([
78-
[3, 0, 6, 5, 0, 8, 4, 0, 0],
79-
[5, 2, 0, 0, 0, 0, 0, 0, 0],
80-
[0, 8, 7, 0, 0, 0, 0, 3, 1],
81-
[0, 0, 3, 0, 1, 0, 0, 8, 0],
82-
[9, 0, 0, 8, 6, 3, 0, 0, 5],
83-
[0, 5, 0, 0, 9, 0, 6, 0, 0],
84-
[1, 3, 0, 0, 0, 0, 2, 5, 0],
85-
[0, 0, 0, 0, 0, 0, 0, 7, 4],
86-
[0, 0, 5, 2, 0, 6, 3, 0, 0]
87-
])
88-
89-
sudokuBoard.printBoard()
90-
91-
console.log('\n')
92-
sudokuBoard.solve()
93-
94-
sudokuBoard.printBoard()
95-
}
96-
97-
main()
75+
export { Sudoku }

‎Backtracking/tests/Sudoku.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Sudoku } from '../Sudoku'
2+
3+
const data = [
4+
[3, 0, 6, 5, 0, 8, 4, 0, 0],
5+
[5, 2, 0, 0, 0, 0, 0, 0, 0],
6+
[0, 8, 7, 0, 0, 0, 0, 3, 1],
7+
[0, 0, 3, 0, 1, 0, 0, 8, 0],
8+
[9, 0, 0, 8, 6, 3, 0, 0, 5],
9+
[0, 5, 0, 0, 9, 0, 6, 0, 0],
10+
[1, 3, 0, 0, 0, 0, 2, 5, 0],
11+
[0, 0, 0, 0, 0, 0, 0, 7, 4],
12+
[0, 0, 5, 2, 0, 6, 3, 0, 0]
13+
]
14+
15+
const solved = [
16+
[3, 1, 6, 5, 7, 8, 4, 9, 2],
17+
[5, 2, 9, 1, 3, 4, 7, 6, 8],
18+
[4, 8, 7, 6, 2, 9, 5, 3, 1],
19+
[2, 6, 3, 4, 1, 5, 9, 8, 7],
20+
[9, 7, 4, 8, 6, 3, 1, 2, 5],
21+
[8, 5, 1, 7, 9, 2, 6, 4, 3],
22+
[1, 3, 8, 9, 4, 7, 2, 5, 6],
23+
[6, 9, 2, 3, 5, 1, 8, 7, 4],
24+
[7, 4, 5, 2, 8, 6, 3, 1, 9]
25+
]
26+
27+
describe('Sudoku', () => {
28+
it('should create a valid board successfully', () => {
29+
// we deliberately want to check whether this constructor call fails or not
30+
// eslint-disable-next-line no-new
31+
expect(() => { new Sudoku(data) }).not.toThrow()
32+
})
33+
34+
it('should find an empty cell', () => {
35+
const board = new Sudoku(data)
36+
const emptyCell = board.findEmptyCell()
37+
expect(emptyCell).not.toEqual([-1, -1])
38+
})
39+
40+
it('should solve the board successfully', () => {
41+
const board = new Sudoku(data)
42+
board.solve()
43+
44+
// should not have empty cells anymore
45+
const emptyCell = board.findEmptyCell()
46+
expect(emptyCell).toEqual([-1, -1])
47+
48+
// solved board should match our expectation
49+
for (let i = 0; i < 9; i++) {
50+
const section = board.getSection(i, [0, 9])
51+
expect(section).toEqual(solved[i])
52+
}
53+
})
54+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /