|
1 | 1 | import unittest |
2 | 2 | import rust_pgn_reader_python_binding |
| 3 | +import pyarrow as pa |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class TestPgnExtraction(unittest.TestCase): |
@@ -479,6 +480,148 @@ def test_castling(self): |
479 | 480 |
|
480 | 481 | self.assertTrue(extractor.castling_rights == castling_reference) |
481 | 482 |
|
| 483 | + def test_parse_game_moves_arrow_chunked_array(self): |
| 484 | + pgns = [ |
| 485 | + "1. Nf3 g6 2. b3 Bg7 3. Nc3 e5 4. Bb2 e4 5. Ng1 d6 6. Rb1 a5 7. Nxe4 Bxb2 8. Rxb2 Nf6 9. Nxf6+ Qxf6 10. Rb1 Ra6 11. e3 Rb6 12. d4 a4 13. bxa4 Nc6 14. Rxb6 cxb6 15. Bb5 Bd7 16. Bxc6 Bxc6 17. Nf3 Bxa4 18. O-O O-O 19. Re1 Rc8 20. Re2 d5 21. Ne5 Qf5 22. Qd3 Bxc2 23. Qxf5 Bxf5 24. h3 b5 25. Rb2 f6 26. Ng4 Rc6 27. Nh6+ Kg7 28. Nxf5+ gxf5 29. Rxb5 Rc7 30. Rxd5 Kg6 31. f4 Kh5 32. Rxf5+ Kh4 33. Rxf6 Kg3 34. d5 Rc1# 0-1", |
| 486 | + "1. e4 {asdf} e5 2. Nf3 Nc6 3. Bb5 Nf6 4. O-O {hello} Bc5 5. d3 d6 6. h3 h6 7. c3 O-O", |
| 487 | + ] |
| 488 | + |
| 489 | + # Create a PyArrow ChunkedArray |
| 490 | + arrow_array = pa.array(pgns, type=pa.string()) |
| 491 | + chunked_array = pa.chunked_array([arrow_array]) |
| 492 | + |
| 493 | + extractors = ( |
| 494 | + rust_pgn_reader_python_binding.parse_game_moves_arrow_chunked_array( |
| 495 | + chunked_array |
| 496 | + ) |
| 497 | + ) |
| 498 | + |
| 499 | + comments_reference = [[], ["asdf", "hello"]] |
| 500 | + |
| 501 | + self.assertTrue(extractors[0].comments == comments_reference[0]) |
| 502 | + self.assertTrue(extractors[1].comments == comments_reference[1]) |
| 503 | + |
| 504 | + moves_reference = [ |
| 505 | + [ |
| 506 | + "g1f3", |
| 507 | + "g7g6", |
| 508 | + "b2b3", |
| 509 | + "f8g7", |
| 510 | + "b1c3", |
| 511 | + "e7e5", |
| 512 | + "c1b2", |
| 513 | + "e5e4", |
| 514 | + "f3g1", |
| 515 | + "d7d6", |
| 516 | + "a1b1", |
| 517 | + "a7a5", |
| 518 | + "c3e4", |
| 519 | + "g7b2", |
| 520 | + "b1b2", |
| 521 | + "g8f6", |
| 522 | + "e4f6", |
| 523 | + "d8f6", |
| 524 | + "b2b1", |
| 525 | + "a8a6", |
| 526 | + "e2e3", |
| 527 | + "a6b6", |
| 528 | + "d2d4", |
| 529 | + "a5a4", |
| 530 | + "b3a4", |
| 531 | + "b8c6", |
| 532 | + "b1b6", |
| 533 | + "c7b6", |
| 534 | + "f1b5", |
| 535 | + "c8d7", |
| 536 | + "b5c6", |
| 537 | + "d7c6", |
| 538 | + "g1f3", |
| 539 | + "c6a4", |
| 540 | + "e1g1", |
| 541 | + "e8g8", |
| 542 | + "f1e1", |
| 543 | + "f8c8", |
| 544 | + "e1e2", |
| 545 | + "d6d5", |
| 546 | + "f3e5", |
| 547 | + "f6f5", |
| 548 | + "d1d3", |
| 549 | + "a4c2", |
| 550 | + "d3f5", |
| 551 | + "c2f5", |
| 552 | + "h2h3", |
| 553 | + "b6b5", |
| 554 | + "e2b2", |
| 555 | + "f7f6", |
| 556 | + "e5g4", |
| 557 | + "c8c6", |
| 558 | + "g4h6", |
| 559 | + "g8g7", |
| 560 | + "h6f5", |
| 561 | + "g6f5", |
| 562 | + "b2b5", |
| 563 | + "c6c7", |
| 564 | + "b5d5", |
| 565 | + "g7g6", |
| 566 | + "f2f4", |
| 567 | + "g6h5", |
| 568 | + "d5f5", |
| 569 | + "h5h4", |
| 570 | + "f5f6", |
| 571 | + "h4g3", |
| 572 | + "d4d5", |
| 573 | + "c7c1", |
| 574 | + ], |
| 575 | + [ |
| 576 | + "e2e4", |
| 577 | + "e7e5", |
| 578 | + "g1f3", |
| 579 | + "b8c6", |
| 580 | + "f1b5", |
| 581 | + "g8f6", |
| 582 | + "e1g1", |
| 583 | + "f8c5", |
| 584 | + "d2d3", |
| 585 | + "d7d6", |
| 586 | + "h2h3", |
| 587 | + "h7h6", |
| 588 | + "c2c3", |
| 589 | + "e8g8", |
| 590 | + ], |
| 591 | + ] |
| 592 | + |
| 593 | + self.assertTrue( |
| 594 | + [str(move) for move in extractors[0].moves] == moves_reference[0] |
| 595 | + ) |
| 596 | + self.assertTrue( |
| 597 | + [str(move) for move in extractors[1].moves] == moves_reference[1] |
| 598 | + ) |
| 599 | + |
| 600 | + extractors[0].update_position_status() # Ensure status is calculated |
| 601 | + self.assertTrue(extractors[0].position_status.is_checkmate) |
| 602 | + self.assertFalse(extractors[0].position_status.is_stalemate) |
| 603 | + self.assertTrue(extractors[0].position_status.is_game_over) |
| 604 | + self.assertTrue(extractors[0].position_status.legal_move_count == 0) |
| 605 | + self.assertTrue( |
| 606 | + extractors[0].position_status.turn == True |
| 607 | + ) # White's turn, but Black delivered checkmate |
| 608 | + self.assertTrue( |
| 609 | + extractors[0].position_status.insufficient_material == (False, False) |
| 610 | + ) |
| 611 | + |
| 612 | + self.assertTrue( |
| 613 | + extractors[1].position_status is None |
| 614 | + ) # Not set by default for parse_game_moves_arrow_chunked_array |
| 615 | + extractors[1].update_position_status() |
| 616 | + self.assertFalse(extractors[1].position_status.is_checkmate) |
| 617 | + self.assertFalse(extractors[1].position_status.is_stalemate) |
| 618 | + self.assertFalse(extractors[1].position_status.is_game_over) |
| 619 | + self.assertTrue(extractors[1].position_status.legal_move_count == 36) |
| 620 | + self.assertTrue(extractors[1].position_status.turn == True) # White's turn |
| 621 | + self.assertTrue( |
| 622 | + extractors[1].position_status.insufficient_material == (False, False) |
| 623 | + ) |
| 624 | + |
482 | 625 |
|
483 | 626 | if __name__ == "__main__": |
484 | 627 | unittest.main() |
0 commit comments