-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgn_test.go
453 lines (369 loc) · 10.9 KB
/
pgn_test.go
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package chess
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
type pgnTest struct {
PostPos *Position
PGN string
}
var validPGNs = []pgnTest{
{
PostPos: unsafeFEN("4r3/6P1/2p2P1k/1p6/pP2p1R1/P1B5/2P2K2/3r4 b - - 0 45"),
PGN: mustParsePGN("fixtures/pgns/0001.pgn"),
},
{
PostPos: unsafeFEN("4r3/6P1/2p2P1k/1p6/pP2p1R1/P1B5/2P2K2/3r4 b - - 0 45"),
PGN: mustParsePGN("fixtures/pgns/0002.pgn"),
},
{
PostPos: unsafeFEN("2r2rk1/pp1bBpp1/2np4/2pp2p1/1bP5/1P4P1/P1QPPPBP/3R1RK1 b - - 0 3"),
PGN: mustParsePGN("fixtures/pgns/0003.pgn"),
},
{
PostPos: unsafeFEN("r3kb1r/2qp1pp1/b1n1p2p/pp2P3/5n1B/1PPQ1N2/P1BN1PPP/R3K2R w KQkq - 1 14"),
PGN: mustParsePGN("fixtures/pgns/0004.pgn"),
},
{
PostPos: unsafeFEN("8/8/6p1/4R3/6kQ/r2P1pP1/5P2/6K1 b - - 3 42"),
PGN: mustParsePGN("fixtures/pgns/0011.pgn"),
},
{
PostPos: StartingPosition(),
PGN: mustParsePGN("fixtures/pgns/0012.pgn"),
},
}
type commentTest struct {
PGN string
MoveNumber int
CommentText string
}
var _ = []commentTest{
{
PGN: mustParsePGN("fixtures/pgns/0005.pgn"),
MoveNumber: 7,
CommentText: `(-0.25 → 0.39) Inaccuracy. cxd4 was best. [%eval 0.39] [%clk 0:05:05]`,
},
{
PGN: mustParsePGN("fixtures/pgns/0009.pgn"),
MoveNumber: 5,
CommentText: `This opening is called the Ruy Lopez.`,
},
{
PGN: mustParsePGN("fixtures/pgns/0010.pgn"),
MoveNumber: 5,
CommentText: `This opening is called the Ruy Lopez.`,
},
}
func BenchmarkPGN(b *testing.B) {
pgn := mustParsePGN("fixtures/pgns/0001.pgn")
b.ResetTimer()
for n := 0; n < b.N; n++ {
opt, _ := PGN(strings.NewReader(pgn))
NewGame(opt)
}
}
func mustParsePGN(fname string) string {
f, err := os.Open(fname)
if err != nil {
panic(err)
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
return string(b)
}
func TestGamesFromPGN(t *testing.T) {
for idx, test := range validPGNs {
reader := strings.NewReader(test.PGN)
scanner := NewScanner(reader)
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn %d: %s", idx, err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn %d: %s", idx, err.Error())
}
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn %d: %s", idx, err.Error())
}
if game == nil {
t.Fatalf("game is nil")
}
}
}
func TestGameWithVariations(t *testing.T) {
pgn := mustParsePGN("fixtures/pgns/variations.pgn")
reader := strings.NewReader(pgn)
scanner := NewScanner(reader)
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn: %s", err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn: %s", err.Error())
}
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s", err.Error())
}
if game == nil {
t.Fatalf("game is nil")
}
if len(game.Moves()) != 7 {
t.Fatalf("game moves are not correct, expected 7, got %d", len(game.Moves()))
}
}
func TestSingleGameFromPGN(t *testing.T) {
pgn := mustParsePGN("fixtures/pgns/single_game.pgn")
reader := strings.NewReader(pgn)
scanner := NewScanner(reader)
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn: %s", err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn: %s", err.Error())
}
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s", err.Error())
}
if game == nil {
t.Fatalf("game is nil")
}
if game.tagPairs["Event"] != "Example" {
t.Fatalf("game event is not correct")
}
if game.tagPairs["Site"] != "Internet" {
t.Fatalf("game site is not correct")
}
if game.tagPairs["Date"] != "2023.12.06" {
t.Fatalf("game date is not correct")
}
if game.tagPairs["Round"] != "1" {
t.Fatalf("game round is not correct")
}
if game.tagPairs["White"] != "Player1" {
t.Fatalf("game white is not correct")
}
if game.tagPairs["Black"] != "Player2" {
t.Fatalf("game black is not correct")
}
if game.tagPairs["Result"] != "1-0" {
t.Fatalf("game result is not correct")
}
// Check moves
if len(game.Moves()) != 6 {
t.Fatalf("game moves are not correct, expected 6, got %d", len(game.Moves()))
}
for i, move := range game.Moves() {
// check move number for each move
// Get the full move number
fullMoveNumber := (i / 2) + 1
if move.Number() != fullMoveNumber {
t.Fatalf("game move %d is not correct, expected full move number %d, got %d", i, fullMoveNumber, move.Number())
}
}
if game.Moves()[0].String() != "e2e4" {
t.Fatalf("game move 1 is not correct, expected e4, got %s", game.Moves()[0].String())
}
// print all moves
moves := game.Moves()
if moves[4].comments == "" {
t.Fatalf("game move 6 is not correct, expected comment, got %s", moves[5].comments)
}
}
func TestBigPgn(t *testing.T) {
pgn := mustParsePGN("fixtures/pgns/big.pgn")
reader := strings.NewReader(pgn)
scanner := NewScanner(reader)
count := 0
for scanner.HasNext() {
count++
t.Run(fmt.Sprintf("big pgn : %d", count), func(t *testing.T) {
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn: %s", err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn: %s", err.Error())
}
raw := scannedGame.Raw
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s | %s", err.Error(), raw[:min(200, len(raw))])
}
if game == nil {
t.Fatalf("game is nil")
}
// check moves number
if len(game.Moves()) == 0 {
t.Fatalf("game moves are not correct, expected 0, got %d", len(game.Moves()))
}
if game.GetTagPair("Variant") == "From Position" {
t.Skip("Skipping test for From Position")
}
for i, move := range game.Moves() {
// check move number for each move
// Get the full move number
fullMoveNumber := (i / 2) + 1
if move.Number() != fullMoveNumber {
t.Log(game.Moves())
t.Log(game)
t.Fatalf("game move %d is not correct, expected full move number %d, got %d", i, fullMoveNumber, move.Number())
}
}
})
}
}
func TestBigBigPgn(t *testing.T) {
t.Skip("This test is too slow")
pgn := mustParsePGN("fixtures/pgns/big_big.pgn")
reader := strings.NewReader(pgn)
scanner := NewScanner(reader)
count := 0
for scanner.HasNext() {
count++
t.Run(fmt.Sprintf("bigbig pgn : %d", count), func(t *testing.T) {
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn: %s", err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn: %s", err.Error())
}
raw := scannedGame.Raw
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s | %s", err.Error(), raw[:min(200, len(raw))])
}
if game == nil {
t.Fatalf("game is nil")
}
})
}
}
func TestCompleteGame(t *testing.T) {
pgn := mustParsePGN("fixtures/pgns/complete_game.pgn")
reader := strings.NewReader(pgn)
scanner := NewScanner(reader)
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("fail to scan game from valid pgn: %s", err.Error())
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("fail to tokenize game from valid pgn: %s", err.Error())
}
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s", err.Error())
}
if game == nil {
t.Fatalf("game is nil")
}
if game.tagPairs["Event"] != "Rated blitz game" {
t.Fatalf("game event is not correct")
}
if game.tagPairs["Site"] != "https://lichess.org/ASZaQYyr" {
t.Fatalf("game site is not correct")
}
if game.tagPairs["Date"] != "2024.12.07" {
t.Fatalf("game date is not correct")
}
if game.tagPairs["White"] != "dangerouschess07" {
t.Fatalf("game white is not correct")
}
if game.tagPairs["Black"] != "GABUZYAN_CHESSMOOD" {
t.Fatalf("game black is not correct")
}
if game.tagPairs["Result"] != "0-1" {
t.Fatalf("game result is not correct")
}
// Check moves
if len(game.Moves()) != 104 {
t.Fatalf("game moves are not correct, expected 52, got %d", len(game.Moves()))
}
if game.Moves()[0].String() != "d2d4" {
t.Fatalf("game move 1 is not correct, expected d4, got %s", game.Moves()[0].String())
}
if game.Moves()[0].comments != "" {
t.Fatalf("game move 1 is not correct, expected no comment, got %s", game.Moves()[0].comments)
}
// print all moves
moves := game.Moves()
if game.Moves()[0].command["eval"] != "0.17" {
t.Fatalf("game move 1 is not correct, expected eval, got %s", game.Moves()[0].command["eval"])
}
if moves[6].comments != "A57 Benko Gambit Declined: Main Line" {
t.Fatalf("game move 4 is not correct, expected comment, got %s", moves[6].comments)
}
if moves[44].nag != "?!" {
t.Fatalf("game move 44 is not correct, expected nag '!?', got %s", moves[44].nag)
}
}
func TestLichessMultipleCommand(t *testing.T) {
file, err := os.Open(filepath.Join("fixtures/pgns", "lichess_multiple_command.pgn"))
if err != nil {
t.Fatalf("Failed to open fixture file: %v", err)
}
scanner := NewScanner(file)
// Test first game
scannedGame, err := scanner.ScanGame()
if err != nil {
t.Fatalf("Failed to read first game: %v", err)
}
tokens, err := TokenizeGame(scannedGame)
if err != nil {
t.Fatalf("Failed to tokenize first game: %v", err)
}
parser := NewParser(tokens)
game, err := parser.Parse()
if err != nil {
t.Fatalf("fail to read games from valid pgn: %s", err.Error())
}
if game == nil {
t.Fatalf("game is nil")
}
if game.tagPairs["Event"] != "Rated blitz game" {
t.Fatalf("game event is not correct")
}
// Check if move one has the correct command
if game.Moves()[0].command["eval"] != "0.0" {
t.Fatalf("game move 1 is not correct, expected eval, got %s", game.Moves()[0].command["eval"])
}
// Check for clock also
if game.Moves()[0].command["clk"] != "0:03:00" {
t.Fatalf("game move 1 is not correct, expected clock, got %s", game.Moves()[0].command["clk"])
}
// Check move 5 for comment and eval
if game.Moves()[4].comments != "E00 Catalan Opening" {
t.Fatalf("game move 5 is not correct, expected comment, got %s", game.Moves()[4].comments)
}
if game.Moves()[4].command["eval"] != "0.14" {
t.Fatalf("game move 5 is not correct, expected eval, got %s", game.Moves()[4].command["eval"])
}
// check for clock
if game.Moves()[4].command["clk"] != "0:02:58" {
t.Fatalf("game move 5 is not correct, expected clock, got %s", game.Moves()[4].command["clk"])
}
}