-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathposition_test.go
More file actions
315 lines (281 loc) · 8.48 KB
/
Copy pathposition_test.go
File metadata and controls
315 lines (281 loc) · 8.48 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package chess
import (
"reflect"
"testing"
)
func TestPositionBinary(t *testing.T) {
for _, fen := range validFENs {
pos, err := decodeFEN(fen)
if err != nil {
t.Fatal(err)
}
b, err := pos.MarshalBinary()
if err != nil {
t.Fatal(err)
}
cp := &Position{}
if err := cp.UnmarshalBinary(b); err != nil {
t.Fatal(err)
}
if pos.String() != cp.String() {
t.Fatalf("expected %s but got %s", pos.String(), cp.String())
}
}
}
func TestPositionUpdate(t *testing.T) {
for _, fen := range validFENs {
pos, err := decodeFEN(fen)
if err != nil {
t.Fatal(err)
}
{
np := pos.Update(&pos.ValidMoves()[0])
if pos.Turn().Other() != np.turn {
t.Fatal("expected other turn")
}
if pos.halfMoveClock+1 != np.halfMoveClock {
t.Fatal("expected half move clock increment")
}
if pos.board.String() == np.board.String() {
t.Fatal("expected board update")
}
}
{
np := pos.Update(nil)
if pos.Turn().Other() != np.turn {
t.Fatal("expected other turn")
}
if pos.halfMoveClock+1 != np.halfMoveClock {
t.Fatal("expected half move clock increment")
}
if pos.board.String() != np.board.String() {
t.Fatal("expected same board")
}
}
}
}
func TestPositionPly(t *testing.T) {
tests := []struct {
moveCount int
turn Color
want int
}{
{moveCount: 0, turn: White, want: 0},
{moveCount: 1, turn: White, want: 1},
{moveCount: 1, turn: Black, want: 2},
{moveCount: 2, turn: White, want: 3},
{moveCount: 2, turn: Black, want: 4},
{moveCount: 10, turn: White, want: 19},
{moveCount: 10, turn: Black, want: 20},
}
for _, tt := range tests {
pos := &Position{
moveCount: tt.moveCount,
turn: tt.turn,
}
got := pos.Ply()
if got != tt.want {
t.Errorf("Ply() with moveCount=%d, turn=%v: got %d, want %d", tt.moveCount, tt.turn, got, tt.want)
}
}
}
func TestSamePositionEnPassantFIDECompliance(t *testing.T) {
// FIDE Article 9.2.3: positions are the same only if "the possible
// moves of all the pieces are the same". Per Article 9.2.3.1, an en
// passant square should only matter when a pawn could have been
// captured en passant (i.e., the capture is actually possible).
// Position with en passant square set but no pawn can capture:
// White pawn on e4 (just pushed e2-e4), en passant square e3,
// but no black pawn on d4 or f4 to capture.
posWithIrrelevantEP, err := decodeFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
if err != nil {
t.Fatal(err)
}
// Same board position but without en passant square set.
posWithoutEP, err := decodeFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
if err != nil {
t.Fatal(err)
}
// These should be considered the same position because no en passant
// capture is possible (no black pawn on d4 or f4).
if !posWithIrrelevantEP.samePosition(posWithoutEP) {
t.Error("positions with irrelevant en passant square should be considered the same")
}
// Position where en passant IS possible:
// White pawn on e4, black pawn on d4. En passant square e3.
// Black pawn on d4 can capture en passant on e3.
posWithRelevantEP, err := decodeFEN("rnbqkbnr/ppp1pppp/8/8/3pP3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
if err != nil {
t.Fatal(err)
}
// Same board but without en passant square.
posWithRelevantNoEP, err := decodeFEN("rnbqkbnr/ppp1pppp/8/8/3pP3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
if err != nil {
t.Fatal(err)
}
// These should NOT be considered the same because the en passant
// capture is actually possible.
if posWithRelevantEP.samePosition(posWithRelevantNoEP) {
t.Error("positions with relevant en passant square should be considered different")
}
// Test with black pawn on f4 (right side of e4 pawn).
posWithRelevantEPRight, err := decodeFEN("rnbqkbnr/pppp1ppp/8/8/4Pp2/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
if err != nil {
t.Fatal(err)
}
posWithRelevantEPRightNoEP, err := decodeFEN("rnbqkbnr/pppp1ppp/8/8/4Pp2/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1")
if err != nil {
t.Fatal(err)
}
if posWithRelevantEPRight.samePosition(posWithRelevantEPRightNoEP) {
t.Error("positions with relevant en passant square (right adjacent pawn) should be considered different")
}
}
func TestValidMovesUnsafeEquivalence(t *testing.T) {
for _, fen := range validFENs {
pos, err := decodeFEN(fen)
if err != nil {
t.Fatal(err)
}
safe := pos.ValidMoves()
unsafe := pos.ValidMovesUnsafe()
if !reflect.DeepEqual(safe, unsafe) {
t.Fatalf("ValidMovesUnsafe() differs from ValidMoves() for FEN %s", fen)
}
}
}
func TestValidMovesUnsafeMutation(t *testing.T) {
pos := StartingPosition()
unsafe1 := pos.ValidMovesUnsafe()
unsafe2 := pos.ValidMovesUnsafe()
// Modifying the unsafe slice should affect the position's cached moves
if len(unsafe1) == 0 {
t.Fatal("expected non-zero moves")
}
original := unsafe1[0]
unsafe1[0] = Move{s1: NoSquare, s2: NoSquare}
if unsafe2[0].s1 != NoSquare || unsafe2[0].s2 != NoSquare {
t.Error("modifying ValidMovesUnsafe() slice should affect internal cache")
}
// Reset for other tests
unsafe1[0] = original
}
func TestValidMovesIter(t *testing.T) {
for _, fen := range validFENs {
pos, err := decodeFEN(fen)
if err != nil {
t.Fatal(err)
}
moves := pos.ValidMoves()
var iterMoves []Move
for move := range pos.ValidMovesIter {
iterMoves = append(iterMoves, move)
}
if !reflect.DeepEqual(moves, iterMoves) {
t.Fatalf("ValidMovesIter() yields different moves than ValidMoves() for FEN %s", fen)
}
}
}
func TestValidMovesIterEarlyReturn(t *testing.T) {
pos := StartingPosition()
count := 0
for range pos.ValidMovesIter {
count++
if count == 5 {
break
}
}
if count != 5 {
t.Fatalf("expected early return after 5 moves, got %d", count)
}
}
func BenchmarkValidMovesCopy(b *testing.B) {
pos := StartingPosition()
for i := 0; i < b.N; i++ {
_ = pos.ValidMoves()
}
}
func BenchmarkValidMovesUnsafe(b *testing.B) {
pos := StartingPosition()
for i := 0; i < b.N; i++ {
_ = pos.ValidMovesUnsafe()
}
}
func TestZobristHashConsistency(t *testing.T) {
// Same positions must have the same hash
pos1 := StartingPosition()
pos2 := StartingPosition()
if pos1.ZobristHash() != pos2.ZobristHash() {
t.Fatalf("identical positions have different hashes: %x vs %x", pos1.ZobristHash(), pos2.ZobristHash())
}
// Different positions should have different hashes (with very high probability)
pos3, _ := decodeFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1")
if pos1.ZobristHash() == pos3.ZobristHash() {
t.Fatal("different positions have the same hash (unlikely collision)")
}
}
func TestZobristHashIncrementalCorrectness(t *testing.T) {
for _, fen := range validFENs {
pos, err := decodeFEN(fen)
if err != nil {
t.Fatal(err)
}
moves := pos.ValidMovesUnsafe()
if len(moves) == 0 {
continue
}
for _, m := range moves {
newPos := pos.Update(&m)
// Recompute hash from scratch for the new position
recomputedHash := newPos.computeHash()
if newPos.ZobristHash() != recomputedHash {
t.Fatalf("incremental hash %x != recomputed hash %x for move %s in FEN %s",
newPos.ZobristHash(), recomputedHash, m.String(), fen)
}
}
}
}
func TestZobristHashSamePositionEquivalence(t *testing.T) {
// Test that hash-based samePosition matches the old logic for a variety of positions
for i, fen1 := range validFENs {
pos1, err := decodeFEN(fen1)
if err != nil {
t.Fatal(err)
}
for j, fen2 := range validFENs {
if j > i+10 && j < len(validFENs)-10 {
continue // Sample to keep test fast
}
pos2, err := decodeFEN(fen2)
if err != nil {
t.Fatal(err)
}
// The old samePosition logic (using string comparison)
oldSame := pos1.board.String() == pos2.board.String() &&
pos1.turn == pos2.turn &&
pos1.castleRights.String() == pos2.castleRights.String() &&
pos1.relevantEnPassantSquare() == pos2.relevantEnPassantSquare()
newSame := pos1.samePosition(pos2)
if oldSame != newSame {
t.Fatalf("samePosition mismatch for FENs %s and %s: old=%v new=%v", fen1, fen2, oldSame, newSame)
}
}
}
}
func BenchmarkSamePositionHash(b *testing.B) {
pos1 := StartingPosition()
pos2 := StartingPosition()
for i := 0; i < b.N; i++ {
_ = pos1.samePosition(pos2)
}
}
func BenchmarkSamePositionString(b *testing.B) {
pos1 := StartingPosition()
pos2 := StartingPosition()
for i := 0; i < b.N; i++ {
_ = pos1.board.String() == pos2.board.String() &&
pos1.turn == pos2.turn &&
pos1.castleRights.String() == pos2.castleRights.String() &&
pos1.relevantEnPassantSquare() == pos2.relevantEnPassantSquare()
}
}