-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
20 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/com/fathzer/chess/utils/test/BoardExplorerBuilderTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.fathzer.chess.utils.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import static com.fathzer.chess.utils.Pieces.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fathzer.chess.utils.adapters.BoardExplorer; | ||
import com.fathzer.chess.utils.adapters.BoardExplorerBuilder; | ||
|
||
/** A generic test of {@link BoardExplorerBuilder} | ||
* @param <B> The type of chess board | ||
*/ | ||
public abstract class BoardExplorerBuilderTest<B> { | ||
|
||
@Test | ||
void test() { | ||
final BoardExplorerBuilder<B> builder = getBuilder(); | ||
B board = toBoard("3k1q2/b1n5/5p2/3P2rp/P7/8/2B3N1/2Q1K2R w - - 0 1"); | ||
Map<Integer, Integer> expected = Map.ofEntries(Map.entry(3, -KING), Map.entry(5, -QUEEN), | ||
Map.entry(8, -BISHOP), Map.entry(10, -KNIGHT), Map.entry(21, -PAWN), | ||
Map.entry(27, PAWN), Map.entry(30,-ROOK), Map.entry(31, -PAWN), | ||
Map.entry(32, PAWN), Map.entry(50, BISHOP), | ||
Map.entry(54, KNIGHT), Map.entry(58, QUEEN), Map.entry(60, KING), Map.entry(63, ROOK)); | ||
Map<Integer, Integer> map = toMap(builder.getExplorer(board)); | ||
assertEquals(expected, map); | ||
testStream(expected, builder.getPieces(board)); | ||
|
||
board = toBoard("b2k1q2/2n5/5p2/3P2r1/8/8/2B3N1/2Q1KR2 w - - 0 1"); | ||
expected = Map.ofEntries(Map.entry(0, -BISHOP), Map.entry(3, -KING), Map.entry(5, -QUEEN), | ||
Map.entry(10, -KNIGHT), Map.entry(21, -PAWN), Map.entry(27, PAWN), | ||
Map.entry(30,-ROOK), Map.entry(50, BISHOP), Map.entry(54, KNIGHT), | ||
Map.entry(58, QUEEN), Map.entry(60, KING), Map.entry(61, ROOK)); | ||
map = toMap(builder.getExplorer(board)); | ||
assertEquals(expected, map); | ||
testStream(expected, builder.getPieces(board)); | ||
} | ||
|
||
/** Creates the builder to test. | ||
* @return The builder to test | ||
*/ | ||
protected abstract BoardExplorerBuilder<B> getBuilder(); | ||
|
||
/** Converts a <a href="https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation">fen representation</a> to a board. | ||
* @param fen The FEN to convert. | ||
* @return The board that corresponds to the <i>fen</i> argument. | ||
*/ | ||
protected abstract B toBoard(String fen); | ||
|
||
private Map<Integer, Integer> toMap(BoardExplorer exp) { | ||
final Map<Integer, Integer> result = new HashMap<Integer, Integer>(); | ||
do { | ||
result.put(exp.getIndex(), exp.getPiece()); | ||
} while (exp.next()); | ||
return result; | ||
} | ||
|
||
private void testStream(Map<Integer, Integer> expected, IntStream pieces) { | ||
final List<Integer> expectedValues = new ArrayList<>(expected.values()); | ||
Collections.sort(expectedValues); | ||
final List<Integer> actual = pieces.boxed().collect(Collectors.toList()); | ||
Collections.sort(actual); | ||
assertEquals(expectedValues, actual); | ||
} | ||
} |
35 changes: 15 additions & 20 deletions
35
src/test/java/com/fathzer/chess/utils/adapters/chesslib/ExplorerBuilderTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
package com.fathzer.chess.utils.adapters.chesslib; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fathzer.chess.test.utils.FENUtils; | ||
import com.fathzer.chess.utils.adapters.BoardExplorer; | ||
import com.fathzer.chess.utils.adapters.BoardExplorerBuilder; | ||
import com.fathzer.chess.utils.test.BoardExplorerBuilderTest; | ||
|
||
class ExplorerBuilderTest extends BoardExplorerBuilderTest<ChessLibMoveGenerator> implements BoardExplorerBuilder<ChessLibMoveGenerator>{ | ||
|
||
class ExplorerBuilderTest { | ||
@Override | ||
protected BoardExplorerBuilder<ChessLibMoveGenerator> getBuilder() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public ChessLibMoveGenerator toBoard(String fen) { | ||
return FENUtils.from(fen); | ||
} | ||
|
||
@Test | ||
void test() { | ||
final BoardExplorerBuilder<ChessLibMoveGenerator> builder = new BoardExplorerBuilder<ChessLibMoveGenerator>() { | ||
@Override | ||
public BoardExplorer getExplorer(ChessLibMoveGenerator board) { | ||
return new ChessLibBoardExplorer(board.getBoard()); | ||
} | ||
}; | ||
|
||
ChessLibMoveGenerator mg = FENUtils.from("4k3/6p1/3q4/8/8/8/8/1N2K3 w HAha - 0 1"); | ||
Set<Integer> result = builder.getPieces(mg).boxed().collect(Collectors.toSet()); | ||
assertEquals(Set.of(-6,-5,-1,6,2), result); | ||
@Override | ||
public BoardExplorer getExplorer(ChessLibMoveGenerator board) { | ||
return new ChessLibBoardExplorer(board.getBoard()); | ||
} | ||
} |