|
1 | 1 | package chess |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
4 | 6 | "testing" |
5 | 7 | ) |
6 | 8 |
|
@@ -382,6 +384,98 @@ func BenchmarkAlgebraicDecodeComplex(b *testing.B) { |
382 | 384 | } |
383 | 385 | } |
384 | 386 |
|
| 387 | +func TestPromotionWithCheck(t *testing.T) { |
| 388 | + promoPos := unsafeFEN("8/1P2k3/8/8/8/8/8/8 w - - 0 1") |
| 389 | + promoMove := &Move{s1: B7, s2: B8, promo: Queen, tags: Check} |
| 390 | + |
| 391 | + algebraicNotation := AlgebraicNotation{} |
| 392 | + result := algebraicNotation.Encode(promoPos, promoMove) |
| 393 | + if result != "b8=Q+" { |
| 394 | + t.Fatalf("Expected 'b8=Q+', got '%s'", result) |
| 395 | + } |
| 396 | + |
| 397 | + longAlgebraicNotation := LongAlgebraicNotation{} |
| 398 | + result = longAlgebraicNotation.Encode(promoPos, promoMove) |
| 399 | + if result != "b7b8=Q+" { |
| 400 | + t.Fatalf("Expected 'b7b8=Q+', got '%s'", result) |
| 401 | + } |
| 402 | +} |
| 403 | + |
| 404 | +func TestPromotionWithCheckFromIssue84(t *testing.T) { |
| 405 | + promoPos := unsafeFEN("8/1P2k3/8/8/8/8/8/8 w - - 0 1") |
| 406 | + promoMove := &Move{s1: B7, s2: B8, promo: Queen} |
| 407 | + promoMove.AddTag(Check) |
| 408 | + |
| 409 | + algebraicNotation := AlgebraicNotation{} |
| 410 | + result := algebraicNotation.Encode(promoPos, promoMove) |
| 411 | + if result != "b8=Q+" { |
| 412 | + t.Fatalf("Algebraic Notation: Expected 'b8=Q+', got '%s'", result) |
| 413 | + } |
| 414 | + |
| 415 | + longAlgebraicNotation := LongAlgebraicNotation{} |
| 416 | + result = longAlgebraicNotation.Encode(promoPos, promoMove) |
| 417 | + if result != "b7b8=Q+" { |
| 418 | + t.Fatalf("Long Algebraic Notation: Expected 'b7b8=Q+', got '%s'", result) |
| 419 | + } |
| 420 | +} |
| 421 | + |
| 422 | +func TestIssue84FullGame(t *testing.T) { |
| 423 | + const pgn = `[Event "?"] |
| 424 | +[Site "?"] |
| 425 | +[Date "????.??.??"] |
| 426 | +[Round "?"] |
| 427 | +[White "?"] |
| 428 | +[Black "?"] |
| 429 | +[Result "*"] |
| 430 | +1. e4 c5 2. Nf3 Nc6 3. Bb5 g6 4. Bxc6 dxc6 5. d3 Bg7 6. h3 e5 7. a3 Nf6 |
| 431 | +8. Nc3 Nd7 9. Be3 Qe7 10. Qd2 O-O 11. O-O f5 12. exf5 gxf5 13. Bh6 Qf6 |
| 432 | +14. Bxg7 Qxg7 15. Qg5 Qxg5 16. Nxg5 Re8 17. Rae1 h6 18. Nf3 c4 19. dxc4 Kf7 |
| 433 | +20. Rd1 Nf6 21. Rd6 e4 22. Nh4 Be6 23. b3 Rg8 24. Ne2 Rad8 25. Rfd1 Rxd6 |
| 434 | +26. Rxd6 Ke7 27. c5 Ne8 28. Rd1 Nf6 29. Nd4 f4 30. Nhf5+ Bxf5 31. Nxf5+ Ke6 |
| 435 | +32. Nxh6 Rg5 33. b4 Rd5 34. Rxd5 cxd5 35. c3 Nd7 36. Ng4 Kf5 37. Kf1 Nf8 |
| 436 | +38. Ke2 Ne6 39. Kd2 Ng5 40. Nh6+ Kg6 41. Ng4 Kf5 42. Nh2 Ke5 43. a4 Ne6 |
| 437 | +44. Nf1 a5 45. g3 axb4 46. cxb4 d4 47. gxf4+ Nxf4 48. a5 Nxh3 49. c6 Kd6 |
| 438 | +50. cxb7 Kc7 51. f3 exf3 52. b8=Q+ Kxb8 53. Kd3 Kb7 54. Kxd4 Ka6 55. Kc5 Nf4 |
| 439 | +56. Kc6 Nd3 57. b5+ Kxa5 58. b6 Nb4+ 59. Kc5 *` |
| 440 | + |
| 441 | + reader := strings.NewReader(pgn) |
| 442 | + pgnObj, err := PGN(reader) |
| 443 | + if err != nil { |
| 444 | + t.Fatalf("Failed to parse PGN: %v", err) |
| 445 | + } |
| 446 | + game := NewGame(pgnObj) |
| 447 | + moves := game.Moves() |
| 448 | + |
| 449 | + for i, mv := range moves { |
| 450 | + moveNum := (i / 2) + 1 |
| 451 | + color := "W" |
| 452 | + if i%2 == 1 { |
| 453 | + color = "B" |
| 454 | + } |
| 455 | + |
| 456 | + _, err := safeEncode(LongAlgebraicNotation{}, mv.Position(), mv) |
| 457 | + if err != nil { |
| 458 | + t.Fatalf("Error: LongAlgebraicNotation.Encode panic at half-move %d (%d. %s): %v", |
| 459 | + i, moveNum, color, err) |
| 460 | + } |
| 461 | + |
| 462 | + _, err = safeEncode(AlgebraicNotation{}, mv.Position(), mv) |
| 463 | + if err != nil { |
| 464 | + t.Fatalf("Error: AlgebraicNotation.Encode panic at half-move %d (%d. %s): %v", |
| 465 | + i, moveNum, color, err) |
| 466 | + } |
| 467 | + } |
| 468 | +} |
| 469 | + |
| 470 | +func safeEncode(notation Encoder, pos *Position, mv *Move) (s string, err error) { |
| 471 | + defer func() { |
| 472 | + if r := recover(); r != nil { |
| 473 | + err = fmt.Errorf("%v", r) |
| 474 | + } |
| 475 | + }() |
| 476 | + return notation.Encode(pos, mv), nil |
| 477 | +} |
| 478 | + |
385 | 479 | // Benchmark promotion scenarios |
386 | 480 | func BenchmarkPromotionEncoding(b *testing.B) { |
387 | 481 | promoPos := unsafeFEN("rnbqkbnr/pPpppppp/8/8/8/8/P1PPPPPP/RNBQKBNR w KQkq - 0 1") |
|
0 commit comments