From 3198d75c8052644d23ebd9900a8f0c9455ebf296 Mon Sep 17 00:00:00 2001 From: Sampo Silvennoinen Date: Thu, 28 Mar 2024 19:01:32 +0200 Subject: [PATCH 1/3] Attacks: add unit tests for near edge of board --- test/attacks_test.dart | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/attacks_test.dart b/test/attacks_test.dart index 637508f..02230eb 100644 --- a/test/attacks_test.dart +++ b/test/attacks_test.dart @@ -16,6 +16,20 @@ void main() { expect(kingAttacks(21), attacks); }); + test('King attacks in near edges', () { + final attacks = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . 1 1 +. . . . . . 1 . +'''); + expect(kingAttacks(7), attacks); + }); + test('Knight attacks', () { final attacks = makeSquareSet(''' . . . . . . . . @@ -30,6 +44,20 @@ void main() { expect(knightAttacks(35), attacks); }); + test('Knight attacks near edges', () { + final attacks = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . 1 . 1 +. . . . 1 . . . +. . . . . . . . +. . . . 1 . . . +'''); + expect(knightAttacks(14), attacks); + }); + test('White pawn attacks', () { final attacks = makeSquareSet(''' . . . . . . . . @@ -44,6 +72,20 @@ void main() { expect(pawnAttacks(Side.white, 11), attacks); }); + test('White pawn attacks near edges', () { + final attacks = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. 1 . . . . . . +. . . . . . . . +. . . . . . . . +'''); + expect(pawnAttacks(Side.white, 8), attacks); + }); + test('Black pawn attacks', () { final attacks = makeSquareSet(''' . . . . . . . . @@ -58,6 +100,20 @@ void main() { expect(pawnAttacks(Side.black, 36), attacks); }); + test('Black pawn attacks near edges', () { + final attacks = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +. . . . . . 1 . +. . . . . . . . +. . . . . . . . +. . . . . . . . +'''); + expect(pawnAttacks(Side.black, 39), attacks); + }); + test('bishop attacks, empty board', () { expect(bishopAttacks(27, SquareSet.empty), makeSquareSet(''' . . . . . . . 1 From c635a23d7b466257998c02306a51007a75ea19d3 Mon Sep 17 00:00:00 2001 From: Sampo Silvennoinen Date: Thu, 28 Mar 2024 19:07:47 +0200 Subject: [PATCH 2/3] Attacks: add unit tests for surrounded cases --- test/attacks_test.dart | 81 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/test/attacks_test.dart b/test/attacks_test.dart index 02230eb..e0dbea8 100644 --- a/test/attacks_test.dart +++ b/test/attacks_test.dart @@ -16,7 +16,7 @@ void main() { expect(kingAttacks(21), attacks); }); - test('King attacks in near edges', () { + test('King attacks near edges', () { final attacks = makeSquareSet(''' . . . . . . . . . . . . . . . . @@ -114,7 +114,7 @@ void main() { expect(pawnAttacks(Side.black, 39), attacks); }); - test('bishop attacks, empty board', () { + test('Bishop attacks, empty board', () { expect(bishopAttacks(27, SquareSet.empty), makeSquareSet(''' . . . . . . . 1 1 . . . . . 1 . @@ -150,7 +150,30 @@ void main() { ''')); }); - test('rook attacks, empty board', () { + test('Bishop attacks, surrounded in occupied board', () { + final occupied = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . 1 1 1 . . +. . . 1 . 1 . . +. . . 1 1 1 . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +'''); + expect(bishopAttacks(36, occupied), makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . 1 . 1 . . +. . . . . . . . +. . . 1 . 1 . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +''')); + }); + + test('Rook attacks, empty board', () { expect(rookAttacks(10, SquareSet.empty), makeSquareSet(''' . . 1 . . . . . . . 1 . . . . . @@ -163,7 +186,7 @@ void main() { ''')); }); - test('rook attacks, occupied board', () { + test('Rook attacks, occupied board', () { final occupied = makeSquareSet(''' . . . . . . . . . . . . . . . . @@ -186,7 +209,30 @@ void main() { ''')); }); - test('queen attacks, empty board', () { + test('Rook attacks, surrounded in occupied board', () { + final occupied = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . 1 1 1 . . +. . . 1 . 1 . . +. . . 1 1 1 . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +'''); + expect(rookAttacks(36, occupied), makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . . 1 . . . +. . . 1 . 1 . . +. . . . 1 . . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +''')); + }); + + test('Queen attacks, empty board', () { expect(queenAttacks(37, SquareSet.empty), makeSquareSet(''' . . 1 . . 1 . . . . . 1 . 1 . 1 @@ -199,7 +245,7 @@ void main() { ''')); }); - test('queen attacks, occupied board', () { + test('Queen attacks, occupied board', () { final occupied = makeSquareSet(''' . . . . . . . . . . . . . . . . @@ -219,6 +265,29 @@ void main() { . . . . . 1 . . . . . . . . . . . . . . . . . . +''')); + }); + + test('Queen attacks, surrounded in occupied board', () { + final occupied = makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . 1 1 1 . . +. . . 1 . 1 . . +. . . 1 1 1 . . +. . . . . . . . +. . . . . . . . +. . . . . . . . +'''); + expect(queenAttacks(36, occupied), makeSquareSet(''' +. . . . . . . . +. . . . . . . . +. . . 1 1 1 . . +. . . 1 . 1 . . +. . . 1 1 1 . . +. . . . . . . . +. . . . . . . . +. . . . . . . . ''')); }); } From ee64f248b4d284496bef62f81b6e5744e40432bc Mon Sep 17 00:00:00 2001 From: Sampo Silvennoinen Date: Thu, 28 Mar 2024 19:24:27 +0200 Subject: [PATCH 3/3] Attacks: add unit tests for legal attack positions --- test/attacks_test.dart | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/attacks_test.dart b/test/attacks_test.dart index e0dbea8..80e367a 100644 --- a/test/attacks_test.dart +++ b/test/attacks_test.dart @@ -290,4 +290,33 @@ void main() { . . . . . . . . ''')); }); + + test('Legal board position asserts for attacks', () { + const illegalBoardPosition = 65; + const emptySquareSet = SquareSet.empty; + + expect(() { + kingAttacks(illegalBoardPosition); + }, throwsA(isA())); + + expect(() { + pawnAttacks(Side.white, illegalBoardPosition); + }, throwsA(isA())); + + expect(() { + knightAttacks(illegalBoardPosition); + }, throwsA(isA())); + + expect(() { + bishopAttacks(illegalBoardPosition, emptySquareSet); + }, throwsA(isA())); + + expect(() { + rookAttacks(illegalBoardPosition, emptySquareSet); + }, throwsA(isA())); + + expect(() { + queenAttacks(illegalBoardPosition, emptySquareSet); + }, throwsA(isA())); + }); }