From 04cbac28b7d62828dfa43cd77ed8dc790f24237c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 8 Jun 2018 14:21:53 +0100 Subject: Fix built move bug and add unit tests --- src/test/unit_tests/built_group_move_gen_test.cpp | 254 ++++++++++++++++++++++ src/test/unit_tests/legal_move_gen_test.cpp | 62 ++---- 2 files changed, 268 insertions(+), 48 deletions(-) create mode 100644 src/test/unit_tests/built_group_move_gen_test.cpp (limited to 'src/test/unit_tests') diff --git a/src/test/unit_tests/built_group_move_gen_test.cpp b/src/test/unit_tests/built_group_move_gen_test.cpp new file mode 100644 index 0000000..0bdff4d --- /dev/null +++ b/src/test/unit_tests/built_group_move_gen_test.cpp @@ -0,0 +1,254 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; + +using std::vector; +using std::ostream; +using std::initializer_list; + +TEST(BuiltGroupMoveGen, SameSuitEmpty) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::SAME_SUIT; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 0, 1), + move(2, 0, 1), + move(3, 0, 1), + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + + // Built pile moves + move(1, 0, 2), + move(1, 0, 3), + move(1, 4, 2), + move(1, 4, 3) + } + ); +} + +TEST(BuiltGroupMoveGen, RedBlackEmpty) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 0, 1), + move(2, 0, 1), + move(3, 0, 1), + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + + // Built pile moves + move(2, 0, 2), + move(2, 0, 3), + move(2, 4, 2), + move(2, 4, 3), + + move(3, 0, 2), + move(3, 4, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, AnySuitEmpty) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::ANY_SUIT; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 0, 1), + move(2, 0, 1), + move(3, 0, 1), + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + + // Built pile moves + move(1, 0, 2), + move(1, 0, 3), + move(1, 4, 2), + move(1, 4, 3), + + move(2, 0, 2), + move(2, 0, 3), + move(2, 4, 2), + move(2, 4, 3), + + move(3, 0, 2), + move(3, 0, 3), + move(3, 4, 2), + move(3, 4, 3), + } + ); +} + +TEST(BuiltGroupMoveGen, CantMove) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::ANY_SUIT; + sr.move_built_group = false; + + test_helper::run_built_group_test( + sr, + { + {}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 0, 1), + move(2, 0, 1), + move(3, 0, 1), + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1) + } + ); +} + +TEST(BuiltGroupMoveGen, SameSuitOccupied) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::SAME_SUIT; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {"4C"}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + move(0, 4, 1), + + // Built pile moves + move(1, 0, 3), + move(1, 4, 2), + move(1, 4, 3) + } + ); +} + +TEST(BuiltGroupMoveGen, RedBlackOccupied) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {"4C"}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + move(0, 4, 1), + + // Built pile moves + move(2, 0, 3), + move(2, 4, 2), + move(2, 4, 3), + move(3, 4, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, AnySuitOccupied) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.built_group_pol = pol::ANY_SUIT; + sr.move_built_group = true; + + test_helper::run_built_group_test( + sr, + { + {"4C"}, + {"3C","2C","AC"}, // Same suit + {"3H","2S","AH"}, // Red black + {"3D","2D","AS"}, // Any suit + {} + }, + { + // Regular moves + move(1, 4, 1), + move(2, 4, 1), + move(3, 4, 1), + move(0, 4, 1), + + // Built pile moves + move(1, 0, 3), + move(2, 0, 3), + move(3, 0, 3), + + move(1, 4, 2), + move(1, 4, 3), + + move(2, 4, 2), + move(2, 4, 3), + + move(3, 4, 2), + move(3, 4, 3), + } + ); +} diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index 84755f6..f8c9a63 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -16,10 +16,6 @@ using std::vector; using std::ostream; using std::initializer_list; -bool moves_eq(vector&, vector&); -ostream& operator <<(ostream&, const move&); -ostream& operator <<(ostream&, const vector&); - TEST(LegalMoveGen, BuildPolAnySuit) { sol_rules sr; sr.build_pol = pol::ANY_SUIT; @@ -41,7 +37,7 @@ TEST(LegalMoveGen, BuildPolAnySuit) { move(4,3) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, BuildPolRedBlack) { @@ -63,7 +59,7 @@ TEST(LegalMoveGen, BuildPolRedBlack) { move(4,3) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, BuildPolSameSuit) { @@ -84,7 +80,7 @@ TEST(LegalMoveGen, BuildPolSameSuit) { move(4,0) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, BuildPolNoBuild) { @@ -104,7 +100,7 @@ TEST(LegalMoveGen, BuildPolNoBuild) { vector exp_moves = { }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, SpacesPolAny) { @@ -122,7 +118,7 @@ TEST(LegalMoveGen, SpacesPolAny) { move(1, 0) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, SpacesPolKings) { @@ -141,7 +137,7 @@ TEST(LegalMoveGen, SpacesPolKings) { move(2, 0) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, SpacesPolNoBuild) { @@ -158,7 +154,7 @@ TEST(LegalMoveGen, SpacesPolNoBuild) { vector exp_moves = { }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, MoveBuiltGroupTrue) { @@ -177,7 +173,7 @@ TEST(LegalMoveGen, MoveBuiltGroupTrue) { move(1, 0, 2) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, MoveBuiltGroupFalse) { @@ -195,7 +191,7 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) { move(1, 0, 1) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, Foundations) { @@ -219,7 +215,7 @@ TEST(LegalMoveGen, Foundations) { move(7, 3) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, FoundationsRemovable) { @@ -241,7 +237,7 @@ TEST(LegalMoveGen, FoundationsRemovable) { move(0, 4) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, Cells) { @@ -261,7 +257,7 @@ TEST(LegalMoveGen, Cells) { move(2, 1) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, StockDealToWaste) { @@ -283,7 +279,7 @@ TEST(LegalMoveGen, StockDealToWaste) { move(4, 5) }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } TEST(LegalMoveGen, StockDealToTableau) { @@ -304,35 +300,5 @@ TEST(LegalMoveGen, StockDealToTableau) { move(4, 255, 2) // Special stock-to-tableau syntax }; - ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; -} - - - -bool moves_eq(vector& exp_moves, vector& actual_moves) { - if (exp_moves.size() != actual_moves.size()) return false; - - for (move m : actual_moves) { - auto it = find(begin(exp_moves), end(exp_moves), m); - bool found = it != end(exp_moves); - - if (found) exp_moves.erase(it); - else return false; - } - - return true; -} - -ostream& operator <<(ostream& os, const move& m) { - return os << "move:(" << int(m.from) - << "," << int(m.to) - << "," << int(m.count) - << ")"; -} - -ostream& operator <<(ostream& os, const vector& moves) { - for (const move m : moves) { - os << m << ",\n"; - } - return os; + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; } -- cgit v1.2.1