|
| 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