|
| 1 | +package com.dodecaedro; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import org.junit.BeforeClass; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +import com.dodecaedro.backtrack.sudoku.SudokuNode; |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit test for simple App. |
| 12 | + */ |
| 13 | +public class SudokuTest { |
| 14 | + private static SudokuNode goalNode; |
| 15 | + private static SudokuNode nonCompleteNode; |
| 16 | + private static SudokuNode boardWithRepeatedNumbersInRow; |
| 17 | + private static SudokuNode boardWithRepeatedNumbersInColumn; |
| 18 | + private static SudokuNode boardWithRepeatedNumbersInBlock; |
| 19 | + |
| 20 | + @BeforeClass |
| 21 | + public static void setUp() { |
| 22 | + goalNode = new SudokuNode(); |
| 23 | + |
| 24 | + goalNode.setValuePositionXY(6, 0, 0); |
| 25 | + goalNode.setValuePositionXY(4, 1, 0); |
| 26 | + goalNode.setValuePositionXY(5, 2, 0); |
| 27 | + goalNode.setValuePositionXY(7, 3, 0); |
| 28 | + goalNode.setValuePositionXY(8, 4, 0); |
| 29 | + goalNode.setValuePositionXY(2, 5, 0); |
| 30 | + goalNode.setValuePositionXY(3, 6, 0); |
| 31 | + goalNode.setValuePositionXY(9, 7, 0); |
| 32 | + goalNode.setValuePositionXY(1, 8, 0); |
| 33 | + |
| 34 | + goalNode.setValuePositionXY(2, 0, 1); |
| 35 | + goalNode.setValuePositionXY(1, 1, 1); |
| 36 | + goalNode.setValuePositionXY(3, 2, 1); |
| 37 | + goalNode.setValuePositionXY(6, 3, 1); |
| 38 | + goalNode.setValuePositionXY(9, 4, 1); |
| 39 | + goalNode.setValuePositionXY(4, 5, 1); |
| 40 | + goalNode.setValuePositionXY(5, 6, 1); |
| 41 | + goalNode.setValuePositionXY(7, 7, 1); |
| 42 | + goalNode.setValuePositionXY(8, 8, 1); |
| 43 | + |
| 44 | + goalNode.setValuePositionXY(7, 0, 2); |
| 45 | + goalNode.setValuePositionXY(9, 1, 2); |
| 46 | + goalNode.setValuePositionXY(8, 2, 2); |
| 47 | + goalNode.setValuePositionXY(1, 3, 2); |
| 48 | + goalNode.setValuePositionXY(3, 4, 2); |
| 49 | + goalNode.setValuePositionXY(5, 5, 2); |
| 50 | + goalNode.setValuePositionXY(4, 6, 2); |
| 51 | + goalNode.setValuePositionXY(2, 7, 2); |
| 52 | + goalNode.setValuePositionXY(6, 8, 2); |
| 53 | + |
| 54 | + goalNode.setValuePositionXY(9, 0, 3); |
| 55 | + goalNode.setValuePositionXY(3, 1, 3); |
| 56 | + goalNode.setValuePositionXY(2, 2, 3); |
| 57 | + goalNode.setValuePositionXY(4, 3, 3); |
| 58 | + goalNode.setValuePositionXY(7, 4, 3); |
| 59 | + goalNode.setValuePositionXY(6, 5, 3); |
| 60 | + goalNode.setValuePositionXY(1, 6, 3); |
| 61 | + goalNode.setValuePositionXY(8, 7, 3); |
| 62 | + goalNode.setValuePositionXY(5, 8, 3); |
| 63 | + |
| 64 | + goalNode.setValuePositionXY(1, 0, 4); |
| 65 | + goalNode.setValuePositionXY(8, 1, 4); |
| 66 | + goalNode.setValuePositionXY(4, 2, 4); |
| 67 | + goalNode.setValuePositionXY(2, 3, 4); |
| 68 | + goalNode.setValuePositionXY(5, 4, 4); |
| 69 | + goalNode.setValuePositionXY(3, 5, 4); |
| 70 | + goalNode.setValuePositionXY(9, 6, 4); |
| 71 | + goalNode.setValuePositionXY(6, 7, 4); |
| 72 | + goalNode.setValuePositionXY(7, 8, 4); |
| 73 | + |
| 74 | + goalNode.setValuePositionXY(5, 0, 5); |
| 75 | + goalNode.setValuePositionXY(7, 1, 5); |
| 76 | + goalNode.setValuePositionXY(6, 2, 5); |
| 77 | + goalNode.setValuePositionXY(8, 3, 5); |
| 78 | + goalNode.setValuePositionXY(1, 4, 5); |
| 79 | + goalNode.setValuePositionXY(9, 5, 5); |
| 80 | + goalNode.setValuePositionXY(2, 6, 5); |
| 81 | + goalNode.setValuePositionXY(4, 7, 5); |
| 82 | + goalNode.setValuePositionXY(3, 8, 5); |
| 83 | + |
| 84 | + goalNode.setValuePositionXY(8, 0, 6); |
| 85 | + goalNode.setValuePositionXY(6, 1, 6); |
| 86 | + goalNode.setValuePositionXY(9, 2, 6); |
| 87 | + goalNode.setValuePositionXY(3, 3, 6); |
| 88 | + goalNode.setValuePositionXY(2, 4, 6); |
| 89 | + goalNode.setValuePositionXY(1, 5, 6); |
| 90 | + goalNode.setValuePositionXY(7, 6, 6); |
| 91 | + goalNode.setValuePositionXY(5, 7, 6); |
| 92 | + goalNode.setValuePositionXY(4, 8, 6); |
| 93 | + |
| 94 | + goalNode.setValuePositionXY(4, 0, 7); |
| 95 | + goalNode.setValuePositionXY(2, 1, 7); |
| 96 | + goalNode.setValuePositionXY(1, 2, 7); |
| 97 | + goalNode.setValuePositionXY(5, 3, 7); |
| 98 | + goalNode.setValuePositionXY(6, 4, 7); |
| 99 | + goalNode.setValuePositionXY(7, 5, 7); |
| 100 | + goalNode.setValuePositionXY(8, 6, 7); |
| 101 | + goalNode.setValuePositionXY(3, 7, 7); |
| 102 | + goalNode.setValuePositionXY(9, 8, 7); |
| 103 | + |
| 104 | + goalNode.setValuePositionXY(3, 0, 8); |
| 105 | + goalNode.setValuePositionXY(5, 1, 8); |
| 106 | + goalNode.setValuePositionXY(7, 2, 8); |
| 107 | + goalNode.setValuePositionXY(9, 3, 8); |
| 108 | + goalNode.setValuePositionXY(4, 4, 8); |
| 109 | + goalNode.setValuePositionXY(8, 5, 8); |
| 110 | + goalNode.setValuePositionXY(6, 6, 8); |
| 111 | + goalNode.setValuePositionXY(1, 7, 8); |
| 112 | + goalNode.setValuePositionXY(2, 8, 8); |
| 113 | + |
| 114 | + int[][] original = goalNode.getBoard(); |
| 115 | + int[][] nonCompleteArray = copyUsingForLoop(original); |
| 116 | + nonCompleteArray[1][0] = 0; |
| 117 | + |
| 118 | + int[][] repeatedNumberArray = copyUsingForLoop(original); |
| 119 | + repeatedNumberArray[0][2] = 6; |
| 120 | + |
| 121 | + nonCompleteNode = new SudokuNode(); |
| 122 | + nonCompleteNode.setBoard(nonCompleteArray); |
| 123 | + |
| 124 | + boardWithRepeatedNumbersInRow = new SudokuNode(); |
| 125 | + boardWithRepeatedNumbersInRow.setBoard(repeatedNumberArray); |
| 126 | + |
| 127 | + boardWithRepeatedNumbersInColumn = new SudokuNode(); |
| 128 | + |
| 129 | + int[][] repeatedNumberColumnArray = copyUsingForLoop(original); |
| 130 | + repeatedNumberColumnArray[2][0] = 6; |
| 131 | + boardWithRepeatedNumbersInColumn.setBoard(repeatedNumberColumnArray); |
| 132 | + |
| 133 | + boardWithRepeatedNumbersInBlock = new SudokuNode(); |
| 134 | + int[][] repeatedNumberBlockArray = copyUsingForLoop(original); |
| 135 | + repeatedNumberBlockArray[1][4] = 7; |
| 136 | + boardWithRepeatedNumbersInBlock.setBoard(repeatedNumberBlockArray); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testIsFull() { |
| 141 | + assertTrue(goalNode.isAllBoardFull()); |
| 142 | + assertFalse(nonCompleteNode.isAllBoardFull()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testNumberRepeatedRow() { |
| 147 | + assertFalse(goalNode.isAnyNumberRepeatedRow()); |
| 148 | + assertTrue(boardWithRepeatedNumbersInRow.isAnyNumberRepeatedRow()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testNumberRepeatedColumn() { |
| 153 | + assertFalse(goalNode.isAnyNumberRepeatedColumn()); |
| 154 | + assertTrue(boardWithRepeatedNumbersInColumn.isAnyNumberRepeatedColumn()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testNumberRepeatedBlock() { |
| 159 | + //assertFalse(goalNode.isAnyNumberRepeatedBlock()); |
| 160 | + assertTrue(boardWithRepeatedNumbersInBlock.isAnyNumberRepeatedBlock()); |
| 161 | + } |
| 162 | + |
| 163 | + private static int[][] copyUsingForLoop(int[][] aArray) { |
| 164 | + int[][] copy = new int[aArray.length][aArray.length]; |
| 165 | + for (int idy = 0; idy < aArray.length; ++idy) { |
| 166 | + for (int idx = 0; idx < aArray.length; ++idx) { |
| 167 | + copy[idx][idy] = aArray[idx][idy]; |
| 168 | + } |
| 169 | + } |
| 170 | + return copy; |
| 171 | + } |
| 172 | +} |
0 commit comments