Repository navigation
Expand file tree
/
Copy pathattacks_test.go
More file actions
165 lines (137 loc) · 3.31 KB
/
Copy pathattacks_test.go
File metadata and controls
165 lines (137 loc) · 3.31 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package chess
import "testing"
func assertSameSquares(t *testing.T, got []Square, want ...Square) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %v, want %v", got, want)
}
gotSet := make(map[Square]bool, len(got))
for _, sq := range got {
gotSet[sq] = true
}
for _, sq := range want {
if !gotSet[sq] {
t.Errorf("missing expected square %s; got %v", sq, got)
}
}
}
func TestBoardAttacksFromWhitePawn(t *testing.T) {
pos := mustPosition(
"4k3/8/8/8/4P3/8/8/4K3 w - - 0 1",
)
got := pos.Board().AttacksFrom(E4)
assertSameSquares(t, got, D5, F5)
}
func TestBoardAttacksFromBlackPawn(t *testing.T) {
pos := mustPosition(
"4k3/8/8/4p3/8/8/8/4K3 b - - 0 1",
)
got := pos.Board().AttacksFrom(E5)
assertSameSquares(t, got, D4, F4)
}
func TestBoardAttacksFromPawnOnAFile(t *testing.T) {
pos := mustPosition(
"4k3/8/8/8/P7/8/8/4K3 w - - 0 1",
)
got := pos.Board().AttacksFrom(A4)
assertSameSquares(t, got, B5)
}
func TestBoardAttacksFromRookStopsAfterBlockers(t *testing.T) {
pos := mustPosition(
"4k3/8/3P4/8/1n1R1b2/8/3p4/4K3 w - - 0 1",
)
got := pos.Board().AttacksFrom(D4)
assertSameSquares(
t,
got,
D5, D6,
D3, D2,
C4, B4,
E4, F4,
)
}
func TestBoardAttacksFromEmptySquare(t *testing.T) {
board := StartingPosition().Board()
got := board.AttacksFrom(E4)
if len(got) != 0 {
t.Fatalf("expected no attacks, got %v", got)
}
}
func TestBoardAttacksFromInvalidSquare(t *testing.T) {
board := StartingPosition().Board()
for _, sq := range []Square{NoSquare, Square(64)} {
if got := board.AttacksFrom(sq); len(got) != 0 {
t.Errorf("AttacksFrom(%d) = %v, want empty", sq, got)
}
}
}
func TestBoardAttacksFromIgnoresPins(t *testing.T) {
pos := mustPosition(
"4r1k1/8/8/8/8/8/4R3/4K3 w - - 0 1",
)
got := pos.Board().AttacksFrom(E2)
// The rook is pinned against the white king, but it still attacks
// horizontally for attack-map purposes.
for _, expected := range []Square{A2, B2, C2, D2, F2, G2, H2} {
found := false
for _, actual := range got {
if actual == expected {
found = true
break
}
}
if !found {
t.Errorf("expected pinned rook to attack %s", expected)
}
}
}
func TestBoardAttacksFromRemainingPiecesAndPawnHEdge(t *testing.T) {
tests := []struct {
name string
fen string
from Square
want []Square
}{
{
name: "knight",
fen: "4k3/8/8/8/4N3/8/8/4K3 w - - 0 1",
from: E4,
want: []Square{C3, C5, D2, D6, F2, F6, G3, G5},
},
{
name: "bishop",
fen: "4k3/8/8/8/4B3/8/8/4K3 w - - 0 1",
from: E4,
want: []Square{A8, B1, B7, C2, C6, D3, D5, F3, F5, G2, G6, H1, H7},
},
{
name: "queen",
fen: "4k3/8/8/8/3Q4/8/8/4K3 w - - 0 1",
from: D4,
want: []Square{A1, A4, A7, B2, B4, B6, C3, C4, C5, D1, D2, D3, D5, D6, D7, D8, E3, E4, E5, F2, F4, F6, G1, G4, G7, H4, H8},
},
{
name: "king",
fen: "4k3/8/8/8/4K3/8/8/8 w - - 0 1",
from: E4,
want: []Square{D3, D4, D5, E3, E5, F3, F4, F5},
},
{
name: "white pawn H file",
fen: "4k3/8/8/8/7P/8/8/4K3 w - - 0 1",
from: H4,
want: []Square{G5},
},
{
name: "black pawn H file",
fen: "4k3/8/8/7p/8/8/8/4K3 b - - 0 1",
from: H5,
want: []Square{G4},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assertSameSquares(t, mustPosition(test.fen).Board().AttacksFrom(test.from), test.want...)
})
}
}