/** * Test scenario implementation. * @param testId Test id. * @param threadId Thread id. * @return True is test successful, otherwise false. */ static bool runTest(int testId, int threadId) { SudokuSolver s; int sol; int solNum; int solUnq; int solvingState; int boardState; int[,] solution; int[,] puzzle; bool solCorr; bool testResult = true; switch (testId) { case 0: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 1) { testResult = false; } SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); } break; case 1: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s."); } } break; case 2: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { s = new SudokuSolver(example); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s."); } } break; case 3: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example)); s = new SudokuSolver(puzzle); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 1) { testResult = false; } SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: " + example + ", solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); } break; case 4: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example)); s = new SudokuSolver(puzzle); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: " + example + ", is solution unique: YES, time: " + s.getComputingTime() + " s."); } } break; case 5: for (int example = 0; example < SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES; example++) { puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuStore.getPuzzleExample(example)); s = new SudokuSolver(puzzle); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: " + example + ", is solution correct: YES, time: " + s.getComputingTime() + " s."); } } break; case 6: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum <= 1) { testResult = false; } SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 7: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s."); } break; case 8: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s."); } break; case 9: puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); s = new SudokuSolver(puzzle); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum <= 1) { testResult = false; } SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + findAllSolutions, example: non unique, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 10: puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); s = new SudokuSolver(puzzle); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq != SudokuSolver.SOLUTION_NON_UNIQUE) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: YES, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + checkIfUniqueSolution, example: non unique, is solution unique: NO, time: " + s.getComputingTime() + " s."); } break; case 11: puzzle = SudokuStore.seqOfRandomBoardTransf(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); s = new SudokuSolver(puzzle); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); solution = s.getSolvedBoard(); solCorr = SudokuStore.checkSolvedBoard(solution); if (solCorr != true) { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: NO, time: " + s.getComputingTime() + " s."); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", seqOfRandomBoardTransf + solve, example: non unique, is solution correct: YES, time: " + s.getComputingTime() + " s."); } break; case 12: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); solNum = s.findAllSolutions(); ErrorCodes.consolePrintlnIfError(solNum); if (solNum != 0) { testResult = false; } SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", findAllSolutions, example: no solution, solutions: " + solNum + ", time: " + s.getComputingTime() + " s."); break; case 13: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(solUnq); if (solUnq == SudokuSolver.SOLUTION_NOT_EXISTS) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", checkIfUniqueSolution, example: no solution, no solutions found: NO, time: " + s.getComputingTime() + " s."); } break; case 14: s = new SudokuSolver(SudokuPuzzles.PUZZLE_NO_SOLUTION); sol = s.solve(); solution = s.getSolvedBoard(); if (s.getSolvingState() == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_FAILED) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: no solution, solving failed: NO, time: " + s.getComputingTime() + " s."); } break; case 15: s = new SudokuSolver(SudokuPuzzles.PUZZLE_ERROR); solvingState = s.solve(); boardState = s.getBoardState(); if ((boardState == SudokuBoard.BOARD_STATE_ERROR) && (solvingState == ErrorCodes.SUDOKUSOLVER_SOLVE_SOLVING_NOT_STARTED)) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: board with error, board state error and solving not started: NO, time: " + s.getComputingTime() + " s."); } break; case 16: s = new SudokuSolver(SudokuPuzzles.PUZZLE_REGTESTS); sol = s.solve(); ErrorCodes.consolePrintlnIfError(sol); if (SudokuStore.boardsAreEqual(s.getSolvedBoard(), SudokuPuzzles.PUZZLE_REGTESTS_SOLUTION)) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: with solution, solutions are equal: NO, time: " + s.getComputingTime() + " s."); } break; case 17: s = new SudokuSolver(SudokuPuzzles.PUZZLE_EMPTY); sol = s.solve(); solUnq = s.checkIfUniqueSolution(); ErrorCodes.consolePrintlnIfError(sol); ErrorCodes.consolePrintlnIfError(solUnq); if ((SudokuStore.checkSolvedBoard(s.getSolvedBoard()) == true) & (solUnq == SudokuSolver.SOLUTION_NON_UNIQUE)) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: YES, time: " + s.getComputingTime() + " s."); } else { testResult = false; SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + ", solve, example: empty puzzle, found solution and solution non unique: NO, time: " + s.getComputingTime() + " s."); } break; } if (testResult == true) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: OK"); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " >>>>>>>>>>>>>>>>>>>>>> SudokuSolver, result: ERROR"); } return(testResult); }
/** * Test scenario implementation. * @param testId Test id. * @param threadId Thread id. * @return True is test successful, otherwise false. */ static bool runTest(int testId, int threadId) { bool testResult = true; String testDesc = "", resultDesc = ""; int[,] a = { { 0, 0, 0, 8, 0, 0, 0, 0, 0 }, { 4, 0, 0, 0, 1, 5, 0, 3, 0 }, { 0, 2, 9, 0, 4, 0, 5, 1, 8 }, { 0, 4, 0, 0, 0, 0, 1, 2, 0 }, { 0, 0, 0, 6, 0, 2, 0, 0, 0 }, { 0, 3, 2, 0, 0, 0, 0, 9, 0 }, { 6, 9, 3, 0, 5, 0, 8, 7, 0 }, { 0, 5, 0, 4, 8, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 3, 0, 0, 0 } }; switch (testId) { case 0: testDesc = "SudokuSolver.setCell(int, int, int)"; { SudokuSolver s = new SudokuSolver(); s.setCell(0, 0, 0); s.setCell(1, 0, 4); s.setCell(2, 0, 0); s.setCell(3, 0, 0); s.setCell(4, 0, 0); s.setCell(5, 0, 0); s.setCell(6, 0, 6); s.setCell(7, 0, 0); s.setCell(8, 0, 0); s.setCell(0, 1, 0); s.setCell(1, 1, 0); s.setCell(2, 1, 2); s.setCell(3, 1, 4); s.setCell(4, 1, 0); s.setCell(5, 1, 3); s.setCell(6, 1, 9); s.setCell(7, 1, 5); s.setCell(8, 1, 0); s.setCell(0, 2, 0); s.setCell(1, 2, 0); s.setCell(2, 2, 9); s.setCell(3, 2, 0); s.setCell(4, 2, 0); s.setCell(5, 2, 2); s.setCell(6, 2, 3); s.setCell(7, 2, 0); s.setCell(8, 2, 0); s.setCell(0, 3, 8); s.setCell(1, 3, 0); s.setCell(2, 3, 0); s.setCell(3, 3, 0); s.setCell(4, 3, 6); s.setCell(5, 3, 0); s.setCell(6, 3, 0); s.setCell(7, 3, 4); s.setCell(8, 3, 0); s.setCell(0, 4, 0); s.setCell(1, 4, 1); s.setCell(2, 4, 4); s.setCell(3, 4, 0); s.setCell(4, 4, 0); s.setCell(5, 4, 0); s.setCell(6, 4, 5); s.setCell(7, 4, 8); s.setCell(8, 4, 0); s.setCell(0, 5, 0); s.setCell(1, 5, 5); s.setCell(2, 5, 0); s.setCell(3, 5, 0); s.setCell(4, 5, 2); s.setCell(5, 5, 0); s.setCell(6, 5, 0); s.setCell(7, 5, 0); s.setCell(8, 5, 3); s.setCell(0, 6, 0); s.setCell(1, 6, 0); s.setCell(2, 6, 5); s.setCell(3, 6, 1); s.setCell(4, 6, 0); s.setCell(5, 6, 0); s.setCell(6, 6, 8); s.setCell(7, 6, 0); s.setCell(8, 6, 0); s.setCell(0, 7, 0); s.setCell(1, 7, 3); s.setCell(2, 7, 1); s.setCell(3, 7, 2); s.setCell(4, 7, 0); s.setCell(5, 7, 9); s.setCell(6, 7, 7); s.setCell(7, 7, 0); s.setCell(8, 7, 0); s.setCell(0, 8, 0); s.setCell(1, 8, 0); s.setCell(2, 8, 8); s.setCell(3, 8, 0); s.setCell(4, 8, 0); s.setCell(5, 8, 0); s.setCell(6, 8, 0); s.setCell(7, 8, 1); s.setCell(8, 8, 0); int[,] b = s.getBoard(); if ((SudokuStore.boardsAreEqual(a, b) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; SudokuStore.consolePrintln(s.getMessages()); } } break; case 1: testDesc = "SudokuSolver.getCellDigit(int, int)"; { SudokuSolver s1 = new SudokuSolver(a); SudokuSolver s2 = new SudokuSolver(); for (int i = 0; i < SudokuBoard.BOARD_SIZE; i++) { for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) { int d = s1.getCellDigit(i, j); s2.setCell(i, j, d); } } int[,] b = s2.getBoard(); if ((SudokuStore.boardsAreEqual(a, b) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 2: testDesc = "SudokuSolver.getBoardCopy()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoard(); int[,] c = s.getBoardCopy(); if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 3: testDesc = "SudokuSolver.getSolutionBoardCells()"; { SudokuSolver s = new SudokuSolver(a); s.solve(); int[,] b = s.getSolvedBoard(); int[,] c = SudokuStore.boardCopy(a); BoardCell[] sol = s.getSolutionBoardCells(); foreach (BoardCell bc in sol) { c[bc.rowIndex, bc.colIndex] = bc.digit; } if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 4: testDesc = "SudokuSolver.getAllBoardCells()"; { SudokuSolver s = new SudokuSolver(a); int[,] b = s.getBoardCopy(); int[,] c = new int[SudokuBoard.BOARD_SIZE, SudokuBoard.BOARD_SIZE]; BoardCell[] bc = s.getAllBoardCells(); foreach (BoardCell cell in bc) { c[cell.rowIndex, cell.colIndex] = cell.digit; } if ((SudokuStore.boardsAreEqual(b, c) == true)) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; testResult = false; } } break; case 5: testDesc = "SudokuSolver.getAllSolutionsList()"; { SudokuSolver s = new SudokuSolver(SudokuPuzzles.PUZZLE_NON_UNIQUE_SOLUTION); s.findAllSolutions(); List <SudokuBoard> solList; solList = s.getAllSolutionsList(); foreach (SudokuBoard sb in solList) { if (SudokuStore.checkSolvedBoard(sb.board) == false) { testResult = false; break; } } if (testResult == true) { resultDesc = "Expecting each solution valid - each is valid."; } else { resultDesc = "Expecting each solution valid - found not valid."; } } break; case 6: testDesc = "SudokuGenerator -> generate -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuGenerator g = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD); int[,] generated = g.generate(); g.saveBoard(filePath, "generated", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(generated, loaded) == false) { testResult = false; } if (testResult == true) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; case 7: testDesc = "SudokuSolver -> solve -> save -> load -> compare"; { String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt"); SudokuSolver s = new SudokuSolver(SudokuStore.getPuzzleExample()); s.solve(); int[,] solved = s.getSolvedBoard(); s.saveSolvedBoard(filePath, "solved", "saved"); int[,] loaded = SudokuStore.loadBoard(filePath); FileX.removeFile(filePath); if (SudokuStore.boardsAreEqual(solved, loaded) == false) { testResult = false; } if (testResult == true) { resultDesc = "Expecting equal - are equal."; } else { resultDesc = "Expecting equal - are not equal."; } } break; } if (testResult == true) { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: OK"); } else { SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: ERROR"); } return(testResult); }