Created
December 7, 2019 03:54
-
-
Save azakharov3/0a136fb6b837f3d50b637e81afb99df2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ***************************************************************************** | |
* Name: | |
* Date: | |
* Description: | |
**************************************************************************** */ | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static junit.framework.TestCase.assertEquals; | |
import static junit.framework.TestCase.assertNotNull; | |
import static junit.framework.TestCase.assertTrue; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNull; | |
public class SolverTest { | |
@Test | |
public void ctor_whenBoardIsNull_throws(){ | |
// Arrange | |
IllegalArgumentException exception = null; | |
// Act | |
try { | |
new Solver(null); | |
} catch (IllegalArgumentException e) { | |
exception = e; | |
} | |
// Assert | |
assertNotNull(exception); | |
} | |
/** | |
* An odd-size board is solvable if and only if | |
* the number of inversions is even. | |
*/ | |
@Test | |
public void isSolvable_whenSolvableOddSize_true() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{0, 1, 3}, | |
{4, 2, 5}, | |
{7, 8, 6} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertTrue(isSolvable); | |
} | |
@Test | |
public void isSolvable_puzzle01_true() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 0}, | |
{3, 2} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertEquals(true, isSolvable); | |
} | |
@Test | |
public void isSolvable_puzzle03_true() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{2, 0}, | |
{1, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertEquals(true, isSolvable); | |
} | |
@Test | |
public void isSolvable_puzzle04_true() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{0, 1, 3}, | |
{4, 2, 5}, | |
{7, 8, 6} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertEquals(true, isSolvable); | |
} | |
@Test | |
public void isSolvable_puzzle2x2unsolvable1_false() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 0}, | |
{2, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertEquals(false, isSolvable); | |
} | |
/** | |
* An odd-size board is solvable if and only if | |
* the number of inversions is even. | |
*/ | |
@Test | |
public void isSolvable_whenNonSolvableOddSizeBoard_false() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{8, 7, 0} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
boolean isSolvable = solver.isSolvable(); | |
// Assert | |
assertFalse(isSolvable); | |
} | |
@Test | |
public void moves_puzzle01_is1() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 0}, | |
{3, 2} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
int moves = solver.moves(); | |
// Assert | |
assertEquals(1, moves); | |
} | |
@Test | |
public void moves_puzzle03_is3() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{2, 0}, | |
{1, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
int moves = solver.moves(); | |
// Assert | |
assertEquals(3, moves); | |
} | |
@Test | |
public void moves_puzzle2x2Unsolvable1_isMinus1() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 0}, | |
{2, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
int moves = solver.moves(); | |
// Assert | |
assertEquals(-1, moves); | |
} | |
@Test | |
public void moves_whenCalledTwice_returnsTheSameResult() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{2, 0}, | |
{1, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
int movesFirstCall = solver.moves(); | |
int movesSecondCall = solver.moves(); | |
// Assert | |
assertEquals(3, movesFirstCall); | |
assertEquals(3, movesSecondCall); | |
} | |
@Test | |
public void solution_specificationExample() { | |
// Arrange | |
Board[] boards = new Board[]{ | |
new Board(new int[][] { | |
{0, 1, 3}, | |
{4, 2, 5}, | |
{7, 8, 6}}), | |
new Board(new int[][] { | |
{1, 0, 3}, | |
{4, 2, 5}, | |
{7, 8, 6}}), | |
new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 0, 5}, | |
{7, 8, 6}}), | |
new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 5, 0}, | |
{7, 8, 6}}), | |
new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 0}}), | |
}; | |
Solver solver = new Solver(boards[0]); | |
// Act | |
Iterable<Board> solution = solver.solution(); | |
// Assert | |
int index = 0; | |
for (Board board: | |
solution) { | |
assertEquals(boards[index], board); | |
index++; | |
} | |
} | |
@Test | |
public void solution_puzzle07() { | |
// Arrange | |
Board inputBoard = new Board(new int[][] { | |
{1, 2, 3}, | |
{0, 7, 6}, | |
{5, 4, 8}}); | |
Board expectedResultBoard = new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 0}}); | |
Solver solver = new Solver(inputBoard); | |
// Act | |
Iterable<Board> solution = solver.solution(); | |
// Assert | |
List<Board> solutionList = new ArrayList<>(); | |
solution.iterator().forEachRemaining(solutionList::add); | |
assertEquals(8, solutionList.size()); | |
assertEquals(expectedResultBoard, solutionList.get(solutionList.size() - 1)); | |
} | |
@Test | |
public void solution_whenCalledTwice_returnsTheSameObject() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{2, 0}, | |
{1, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
Iterable<Board> solutionOnFirstCall = solver.solution(); | |
Iterable<Board> solutionOnSecondCall = solver.solution(); | |
// Assert | |
assertEquals(solutionOnFirstCall, solutionOnSecondCall); | |
} | |
@Test | |
public void solution_puzzle26() { | |
Board inputBoard = new Board(new int[][] { | |
{5, 7, 0}, | |
{3, 2, 8}, | |
{1, 6, 4} | |
}); | |
Board expectedResultBoard = new Board(new int[][] { | |
{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 0}}); | |
Solver solver = new Solver(inputBoard); | |
// Act | |
Iterable<Board> solution = solver.solution(); | |
// Assert | |
List<Board> solutionList = new ArrayList<>(); | |
solution.iterator().forEachRemaining(solutionList::add); | |
assertEquals(27, solutionList.size()); | |
assertEquals(expectedResultBoard, solutionList.get(solutionList.size() - 1)); | |
} | |
@Test | |
public void solution_puzzle2x2Unsolvable1_isNull() { | |
// Arrange | |
Board board = new Board(new int[][] { | |
{1, 0}, | |
{2, 3} | |
}); | |
Solver solver = new Solver(board); | |
// Act | |
Iterable<Board> solution = solver.solution(); | |
// Assert | |
assertNull(solution); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment