#include "unittest.h" #include "board.h" #include "bitboard.h" int main() { testSection("Bitboards"); { unitTest(popCount(0b01110) == 3, "Pop count of 01110 is 3"); Bitboard bb = 0b1100; unitTest(bitscanAndReset(&bb) == 2, "Bitscan of 0b1100 is 2"); unitTest(bb == 0b1000, "After bitscan with reset the LS1B is flipped"); } testSection("Default FEN string"); { Board board = parseFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); unitTest(popCount(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "There are 16 pawns total"); unitTest(popCount(board.pieces[ROOK]) == 2, "There are 2 white rooks"); // TODO unitTest(board.side == WHITE, "Side to move is white"); unitTest(board.castlingRights == 0, "Both sides can castle"); unitTest(board.enPassantSquare == 0, "No en passant move is avaialble"); } testSection("Test knight attacks"); { Bitboard attacks[64]; precomputeKnightAttackTable(attacks); int max_attacks = 0; for (int i = 0; i < 64; i++) { int attack_count = popCount(attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; } unitTest(max_attacks == 8, "Max amount of knight attacks should be 8"); unitTest( attacks[b7] == ((BIT << d8) | (BIT << d6) | (BIT << c5) | (BIT << a5)), "Knight on b7 attacks only d8, d6, c5, a5" ); } testSection("Test king attacks"); { Bitboard attacks[64]; precomputeKingAttackTable(attacks); int max_attacks = 0; for (int i = 0; i < 64; i++) { int attack_count = popCount(attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; } unitTest(max_attacks == 8, "Max amount of king attacks should be 8"); unitTest( attacks[h2] == ((BIT << h1) | (BIT << g1) | (BIT << g2) | (BIT << g3) | (BIT << h3)), "King on h2 attacks only h1, g1, g2, g3, h3" ); } testSection("Test pawn attacks"); { Bitboard white_attacks[64]; Bitboard black_attacks[64]; precomputePawnAttackTable(white_attacks, WHITE); precomputePawnAttackTable(black_attacks, BLACK); int is_same = 0; int max_attacks = 0; for (int i = 0; i < 64; i++) { int attack_count = popCount(white_attacks[i]); if (attack_count > max_attacks) max_attacks = attack_count; if (white_attacks[i] == black_attacks[i]) is_same = 1; } unitTest(max_attacks == 2, "Max amount of pawn attacks should be 2"); unitTest(is_same == 0, "Black and white pawns always move differently"); unitTest(white_attacks[g2] == (BIT << h3 | BIT << f3), "White pawn on g2 attacks only h3 and f3"); } report(); return 0; }