Repository navigation
Expand file tree
/
Copy pathboard_test.go
More file actions
203 lines (185 loc) · 5.36 KB
/
Copy pathboard_test.go
File metadata and controls
203 lines (185 loc) · 5.36 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
package chess
import (
"fmt"
"testing"
)
func TestBoardTextSerialization(t *testing.T) {
fen := "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
b := &Board{}
if err := b.UnmarshalText([]byte(fen)); err != nil {
t.Fatal("recieved unexpected error", err)
}
txt, err := b.MarshalText()
if err != nil {
t.Fatal("recieved unexpected error", err)
}
if fen != string(txt) {
t.Fatalf("fen expected board string %s but got %s", fen, string(txt))
}
}
func TestBoardBinarySerialization(t *testing.T) {
g := NewGame()
board := g.Position().Board()
b, err := board.MarshalBinary()
if err != nil {
t.Fatal("recieved unexpected error", err)
}
cpBoard := &Board{}
err = cpBoard.UnmarshalBinary(b)
if err != nil {
t.Fatal("recieved unexpected error", err)
}
s := "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
if s != cpBoard.String() {
t.Fatalf("expected board string %s but got %s", s, cpBoard.String())
}
}
func TestBoardRotation(t *testing.T) {
fens := []string{
"RP4pr/NP4pn/BP4pb/QP4pq/KP4pk/BP4pb/NP4pn/RP4pr",
"RNBKQBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbkqbnr",
"rp4PR/np4PN/bp4PB/kp4PK/qp4PQ/bp4PB/np4PN/rp4PR",
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",
}
g := NewGame()
board := g.Position().Board()
for i := range fens {
var err error
board, err = board.Rotate()
if err != nil {
t.Fatalf("Rotate() error = %v", err)
}
if fens[i] != board.String() {
t.Fatalf("expected board string %s but got %s", fens[i], board.String())
}
}
}
func TestBoardFlip(t *testing.T) {
g := NewGame()
board := g.Position().Board()
var err error
board, err = board.Flip(UpDown)
if err != nil {
t.Fatalf("Flip(UpDown) error = %v", err)
}
b := "RNBQKBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbqkbnr"
if b != board.String() {
t.Fatalf("expected board string %s but got %s", b, board.String())
}
board, err = board.Flip(UpDown)
if err != nil {
t.Fatalf("Flip(UpDown) error = %v", err)
}
b = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
if b != board.String() {
t.Fatalf("expected board string %s but got %s", b, board.String())
}
board, err = board.Flip(LeftRight)
if err != nil {
t.Fatalf("Flip(LeftRight) error = %v", err)
}
b = "rnbkqbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR"
if b != board.String() {
t.Fatalf("expected board string %s but got %s", b, board.String())
}
board, err = board.Flip(LeftRight)
if err != nil {
t.Fatalf("Flip(LeftRight) error = %v", err)
}
b = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
if b != board.String() {
t.Fatalf("expected board string %s but got %s", b, board.String())
}
}
func TestBoardTranspose(t *testing.T) {
g := NewGame()
board := g.Position().Board()
var err error
board, err = board.Transpose()
if err != nil {
t.Fatalf("Transpose() error = %v", err)
}
b := "rp4PR/np4PN/bp4PB/qp4PQ/kp4PK/bp4PB/np4PN/rp4PR"
if b != board.String() {
t.Fatalf("expected board string %s but got %s", b, board.String())
}
}
// TestMailboxConsistency verifies that the mailbox array stays in sync with bitboards
// across various board operations including moves, flips, transposes, and rotations.
func TestMailboxConsistency(t *testing.T) {
g := NewGame()
pos := g.Position()
// Test 1: Starting position
if err := verifyMailboxConsistency(pos.Board()); err != nil {
t.Fatalf("starting position mailbox inconsistent: %v", err)
}
// Test 2: After several moves
moves := []string{"e2e4", "e7e5", "g1f3", "b8c6", "f1c4", "g8f6", "d2d3", "d7d5"}
for _, moveStr := range moves {
move, err := UCINotation{}.Decode(g.Position(), moveStr)
if err != nil {
t.Fatalf("failed to parse move %s: %v", moveStr, err)
}
if err := g.Move(move, nil); err != nil {
t.Fatalf("failed to play move %s: %v", moveStr, err)
}
}
if err := verifyMailboxConsistency(g.Position().Board()); err != nil {
t.Fatalf("after moves mailbox inconsistent: %v", err)
}
// Test 3: Board transformations
board := g.Position().Board()
flipped, err := board.Flip(UpDown)
if err != nil {
t.Fatalf("Flip error: %v", err)
}
if err := verifyMailboxConsistency(flipped); err != nil {
t.Fatalf("flipped board mailbox inconsistent: %v", err)
}
transposed, err := board.Transpose()
if err != nil {
t.Fatalf("Transpose error: %v", err)
}
if err := verifyMailboxConsistency(transposed); err != nil {
t.Fatalf("transposed board mailbox inconsistent: %v", err)
}
rotated, err := board.Rotate()
if err != nil {
t.Fatalf("Rotate error: %v", err)
}
if err := verifyMailboxConsistency(rotated); err != nil {
t.Fatalf("rotated board mailbox inconsistent: %v", err)
}
}
// verifyMailboxConsistency checks that every square in the mailbox matches the
// piece found by scanning bitboards.
func verifyMailboxConsistency(b *Board) error {
for sq := range numOfSquaresInBoard {
square := Square(sq)
mailboxPiece := b.Piece(square)
bitboardPiece := NoPiece
// Find which piece (if any) occupies this square in bitboards
for _, p := range allPieces {
if b.bbForPiece(p).Occupied(square) {
bitboardPiece = p
break
}
}
if mailboxPiece != bitboardPiece {
return fmt.Errorf("mailbox/bitboard mismatch at %s: mailbox=%s, bitboard=%s",
square.String(), mailboxPiece.String(), bitboardPiece.String())
}
}
return nil
}
func BenchmarkPieceMailbox(b *testing.B) {
g := NewGame()
board := g.Position().Board()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = board.Piece(A1)
_ = board.Piece(E4)
_ = board.Piece(H8)
}
}