All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fathzer.chess.utils.test.PerftTest Maven / Gradle / Ivy

There is a newer version: 0.0.2
Show newest version
package com.fathzer.chess.utils.test;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;

import com.fathzer.chess.utils.model.IBoard;
import com.fathzer.chess.utils.model.Variant;
import com.fathzer.chess.utils.test.helper.perft.PerfT;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

/** 
 * A test class based on [Perft](https://www.chessprogramming.org/Perft).
 * 
This class uses the data set available in [jchess-perft-dataset](https://github.com/fathzer-games/jchess-perft-dataset). * @param the type of the board * @param the type of the move */ public class PerftTest, M> extends AbstractAdaptableTest { @Test @Tag("PerftTest.standardSuite") @DisabledIfSystemProperty(named="perftDepth", matches = "0") void standardSuite() throws IOException { doTestSuite("perftDepth", 2, readTests("/com/fathzer/jchess/perft/Perft.epd"), Variant.STANDARD); } @Test @Tag("PerftTest.chess960Suite") @IfVariantSupported(Variant.CHESS960) @DisabledIfSystemProperty(named="perftChess960Depth", matches = "0") void chess960Suite() throws IOException { doTestSuite("perftChess960Depth", 2, readTests("/com/fathzer/jchess/perft/Perft960.epd"), Variant.CHESS960); } private List readTests(String resource) throws IOException { try (BufferedReader stream = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(resource), StandardCharsets.UTF_8))) { return stream.lines().toList(); } } private void doTestSuite(String depthProperty, int defaultDepth, List tests, Variant variant) { final int depth = Integer.getInteger(depthProperty, defaultDepth); if (depth!=defaultDepth) { System.err.println(depthProperty+": "+depth+", "+tests.size()+" lines"); } final PerfT perfT = new PerfT<>(); tests.stream().parallel().forEach(line -> { String[] parts = line.split(";"); String fen = parts[0]; if (parts.length<=depth) { return; } final IBoard board = adapter.fenToBoard(fen, variant); final PerfT.Result result = perfT.divide(board, depth); long expectedTotalMoves = Long.parseLong(parts[depth].split(" ")[1].trim()); assertEquals(expectedTotalMoves, result.getNbLeaves(), String.format("Fen: %s, Depth: %s, Expected: %s, Actual: %s", fen, depth, expectedTotalMoves, result.getNbLeaves())); }); } }