~macaptain/lkdo-chess

ref: 21e5e0dc3de477df4b250673eb89c4e94aa00c2d lkdo-chess/test/attack.zig -rw-r--r-- 3.1 KiB
21e5e0dcMichael Captain Add knight attacks with tests 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
usingnamespace @import("attack.zig");

const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "white pawn attacks two squares in front" {
    const side = Side.White;
    const pawn = Square.e2;
    const expected_attacked_squares = [2]Square{ Square.d3, Square.f3 };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn attacks two squares in front" {
    const side = Side.Black;
    const pawn = Square.c7;
    const expected_attacked_squares = [2]Square{ Square.b6, Square.d6 };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on a file attacks only one square" {
    const side = Side.White;
    const pawn = Square.a4;
    const expected = bitboardFromSquare(Square.b5);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on h file attacks only one square" {
    const side = Side.White;
    const pawn = Square.h7;
    const expected = bitboardFromSquare(Square.g8);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn on a file attacks only one square" {
    const side = Side.Black;
    const pawn = Square.a8;
    const expected = bitboardFromSquare(Square.b7);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "white pawn on 8th rank attacks nothing" {
    const side = Side.White;
    const pawn = Square.f8;
    const expected = @as(u64, 0);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "black pawn on 1st rank attacks nothing" {
    const side = Side.Black;
    const pawn = Square.g1;
    const expected = @as(u64, 0);
    const actual = pawnAttacks(side, pawn);
    expectEqual(expected, actual);
}

test "knight in center attacks all 8 squares" {
    const knight = Square.e4;
    const expected_attacked_squares = [8]Square{
        Square.f6,
        Square.g5,
        Square.g3,
        Square.f2,
        Square.d2,
        Square.c3,
        Square.c5,
        Square.d6,
    };
    const expected = bitboardFromSquares(expected_attacked_squares[0..]);
    const actual = knightAttacks(knight);
    expectEqual(expected, actual);
}

test "knights in corners attack only 2 squares" {
    const knight_a8 = Square.a8;
    const knight_h8 = Square.h8;
    const knight_h1 = Square.h1;
    const knight_a1 = Square.a1;

    const expected_a8 = [2]Square{ Square.c7, Square.b6 };
    const expected_h8 = [2]Square{ Square.g6, Square.f7 };
    const expected_h1 = [2]Square{ Square.f2, Square.g3 };
    const expected_a1 = [2]Square{ Square.b3, Square.c2 };

    expectEqual(bitboardFromSquares(expected_a8[0..]), knightAttacks(knight_a8));
    expectEqual(bitboardFromSquares(expected_h8[0..]), knightAttacks(knight_h8));
    expectEqual(bitboardFromSquares(expected_h1[0..]), knightAttacks(knight_h1));
    expectEqual(bitboardFromSquares(expected_a1[0..]), knightAttacks(knight_a1));
}