diff options
| author | thecharlesblake | 2018-01-18 13:58:44 +0000 |
|---|---|---|
| committer | thecharlesblake | 2018-01-18 13:58:44 +0000 |
| commit | 55d33af18663a5f2961fe673375523a7c710f7b3 (patch) | |
| tree | 9e41613a8a11fe7f97b0b7ba56d74903882d0689 | |
| parent | Fix sol_rules setup issues (diff) | |
| download | metasol-55d33af18663a5f2961fe673375523a7c710f7b3.tar.gz metasol-55d33af18663a5f2961fe673375523a7c710f7b3.zip | |
Add unit tests for global cache and update CMake
| -rw-r--r-- | CMakeLists.txt | 35 | ||||
| -rw-r--r-- | src/test/gloabl_cache_test.cpp | 102 | ||||
| -rw-r--r-- | src/test/resources/gloabl_cache_test.cpp | 176 | ||||
| -rw-r--r-- | src/test/test_helper.cpp | 68 | ||||
| -rw-r--r-- | src/test/test_helper.h | 3 |
5 files changed, 200 insertions, 184 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 216abe3..d8bae75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,12 +12,33 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(main src/main/main.cpp) set(sources src/main/input-output/input/command_line_helper.cpp - src/main/input-output/input/command_line_helper.h src/main/input-output/output/log_helper.h src/main/game/card.h - src/main/game/card.cpp src/main/input-output/input/json-parsing/deal_parser.h src/main/input-output/input/json-parsing/deal_parser.cpp - src/main/solver/solver.cpp src/main/solver/solver.h src/main/game/game_state.cpp - src/main/game/game_state.h src/main/game/sol_rules.h - src/main/game/pile.cpp src/main/game/pile.h src/main/input-output/input/json-parsing/json_helper.cpp src/main/input-output/input/json-parsing/json_helper.h src/main/input-output/input/sol_preset_types.h src/main/input-output/input/sol_preset_types.cpp src/main/input-output/input/json-parsing/rules_parser.cpp src/main/input-output/input/json-parsing/rules_parser.h src/main/input-output/output/state_printer.cpp src/main/input-output/output/state_printer.h src/main/game/game_state.legal_moves.cpp src/main/game/global_cache.cpp src/main/game/global_cache.h src/main/game/sol_rules.cpp) -set(sources_test src/test/test_helper.cpp + src/main/input-output/input/command_line_helper.h + src/main/input-output/output/log_helper.h + src/main/game/card.h + src/main/game/card.cpp + src/main/input-output/input/json-parsing/deal_parser.h + src/main/input-output/input/json-parsing/deal_parser.cpp + src/main/solver/solver.cpp + src/main/solver/solver.h + src/main/game/game_state.cpp + src/main/game/game_state.h + src/main/game/sol_rules.h + src/main/game/pile.cpp + src/main/game/pile.h + src/main/input-output/input/json-parsing/json_helper.cpp + src/main/input-output/input/json-parsing/json_helper.h + src/main/input-output/input/sol_preset_types.h + src/main/input-output/input/sol_preset_types.cpp + src/main/input-output/input/json-parsing/rules_parser.cpp + src/main/input-output/input/json-parsing/rules_parser.h + src/main/input-output/output/state_printer.cpp + src/main/input-output/output/state_printer.h + src/main/game/game_state.legal_moves.cpp + src/main/game/global_cache.cpp + src/main/game/global_cache.h + src/main/game/sol_rules.cpp) +set(sources_test + src/test/test_helper.cpp src/test/test_helper.h src/test/spanish_patience_test.cpp src/test/black_hole_test.cpp @@ -29,7 +50,7 @@ set(sources_test src/test/test_helper.cpp src/test/somerset_test.cpp src/test/alpha_star_test.cpp src/test/spider_test.cpp - src/test/resources/gloabl_cache_test.cpp) + src/test/gloabl_cache_test.cpp) ############################################################################### ## Rapid JSON setup ########################################################### diff --git a/src/test/gloabl_cache_test.cpp b/src/test/gloabl_cache_test.cpp new file mode 100644 index 0000000..beb1e0f --- /dev/null +++ b/src/test/gloabl_cache_test.cpp @@ -0,0 +1,102 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "test_helper.h" +#include "../main/game/game_state.h" +#include "../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(GlobalCache, BuildSameSuit) { + test_helper::run_card_cache_test(pol::SAME_SUIT, false, false); +} + +TEST(GlobalCache, BuildRedBlack) { + test_helper::run_card_cache_test(pol::RED_BLACK, false, true); +} + +TEST(GlobalCache, BuildAny) { + test_helper::run_card_cache_test(pol::ANY_SUIT, true, true); +} + +TEST(GlobalCache, BuildNone) { + test_helper::run_card_cache_test(pol::NO_BUILD, true, true); +} + +TEST(GlobalCache, CommutativeTableauPiles) { + sol_rules rules; + rules.tableau_pile_count = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache; + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + + // Test with piles of size > 1 + cache.clear(); + cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); + ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); + ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); + + rules.tableau_pile_count = 0; + game_state _gs(rules, 0); +} + +TEST(GlobalCache, CommutativeReserve) { + sol_rules rules; + rules.reserve_size = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache; + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + + // Test with piles of size > 1 + cache.clear(); + cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); + ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); + ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); + + rules.reserve_size = 0; + game_state _gs(rules, 0); +} + +TEST(GlobalCache, CommutativeCells) { + sol_rules rules; + rules.cells = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache; + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + + rules.cells = 0; + game_state _gs(rules, 0); +} diff --git a/src/test/resources/gloabl_cache_test.cpp b/src/test/resources/gloabl_cache_test.cpp deleted file mode 100644 index d8d9d79..0000000 --- a/src/test/resources/gloabl_cache_test.cpp +++ /dev/null @@ -1,176 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include <gtest/gtest.h> -#include "../../main/game/global_cache.h" - -typedef sol_rules::build_policy pol; - -TEST(BuildSameSuit, GlobalCache) { - sol_rules rules; - rules.tableau_pile_count = 1; - rules.build_pol = pol::SAME_SUIT; - global_cache cache(rules); - - for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { - for (card::rank_t rank = 1; rank <= 13; rank++) { - // c2 is the same as c_ but with a different suit - card c = card(card::to_suit(suit_idx), rank); - card c_dif_suit = card(card::to_suit(uint8_t((suit_idx + 1) % 4)), - rank); - card c_dif_rank = card(card::to_suit(suit_idx), - card::rank_t((rank % 13) + 1)); - - cache.insert({{c}}); - - EXPECT_TRUE(cache.contains({{c}})); - EXPECT_TRUE(cache.contains({{c_dif_suit}})); - EXPECT_FALSE(cache.contains({{c_dif_rank}})); - } - } -} - -TEST(BuildRedBlack, GlobalCache) { - sol_rules rules; - rules.tableau_pile_count = 1; - rules.build_pol = pol::RED_BLACK; - global_cache cache(rules); - - for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { - for (card::rank_t rank = 1; rank <= 13; rank++) { - // c2 is the same as c_ but with a different suit - card c = card(card::to_suit(suit_idx), rank); - card c_same_col = card(card::to_suit(uint8_t((suit_idx + 2) % 4)), - rank); - card c_dif_col = card(card::to_suit(uint8_t((suit_idx + 1) % 4)), - rank); - card c_dif_rank = card(card::to_suit(suit_idx), - card::rank_t((rank % 13) + 1)); - - cache.insert({{c}}); - - EXPECT_TRUE(cache.contains({{c}})); - EXPECT_TRUE(cache.contains({{c_same_col}})); - EXPECT_FALSE(cache.contains({{c_dif_col}})); - EXPECT_FALSE(cache.contains({{c_dif_rank}})); - } - } -} - -TEST(BuildAny, GlobalCache) { - sol_rules rules; - rules.tableau_pile_count = 1; - rules.build_pol = pol::ANY_SUIT; - global_cache cache(rules); - - for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { - for (card::rank_t rank = 1; rank <= 13; rank++) { - // c2 is the same as c_ but with a different suit - card c = card(card::to_suit(suit_idx), rank); - card c_same_col = card(card::to_suit(uint8_t((suit_idx + 2) % 4)), - rank); - card c_dif_col = card(card::to_suit(uint8_t((suit_idx + 1) % 4)), - rank); - card c_dif_rank = card(card::to_suit(suit_idx), - card::rank_t((rank % 13) + 1)); - - cache.insert({{c}}); - - EXPECT_TRUE(cache.contains({{c}})); - EXPECT_TRUE(cache.contains({{c_same_col}})); - EXPECT_TRUE(cache.contains({{c_dif_col}})); - EXPECT_FALSE(cache.contains({{c_dif_rank}})); - } - } -} - -TEST(BuildNone, GlobalCache) { - sol_rules rules; - rules.tableau_pile_count = 1; - rules.build_pol = pol::NO_BUILD; - global_cache cache(rules); - - for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { - for (card::rank_t rank = 1; rank <= 13; rank++) { - // c2 is the same as c_ but with a different suit - card c = card(card::to_suit(suit_idx), rank); - card c_same_col = card(card::to_suit(uint8_t((suit_idx + 2) % 4)), - rank); - card c_dif_col = card(card::to_suit(uint8_t((suit_idx + 1) % 4)), - rank); - card c_dif_rank = card(card::to_suit(suit_idx), - card::rank_t((rank % 13) + 1)); - - cache.insert({{c}}); - - EXPECT_TRUE(cache.contains({{c}})); - EXPECT_TRUE(cache.contains({{c_same_col}})); - EXPECT_TRUE(cache.contains({{c_dif_col}})); - EXPECT_FALSE(cache.contains({{c_dif_rank}})); - } - } -} - -// Commutative tableau piles -// Commutative cells -// Commutative reserve -// Other piles not so - -TEST(CommutativeTableauPiles, GlobalCache) { - sol_rules rules; - rules.tableau_pile_count = 3; - rules.build_pol = pol::SAME_SUIT; - global_cache cache(rules); - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - EXPECT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - EXPECT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty piles - cache.insert ({{card("4C")},{card("5D")},{}}); - EXPECT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - EXPECT_FALSE(cache.contains({{},{card("4C")},{}})); - - // Test with piles of size > 1 - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - EXPECT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - EXPECT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); -} - -TEST(CommutativeReserve, GlobalCache) { - sol_rules rules; - rules.reserve_size = 3; - rules.build_pol = pol::SAME_SUIT; - global_cache cache(rules); - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - EXPECT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - EXPECT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty piles - cache.insert ({{card("4C")},{card("5D")},{}}); - EXPECT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - EXPECT_FALSE(cache.contains({{},{card("4C")},{}})); - - // Test with piles of size > 1 - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - EXPECT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - EXPECT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); -} - -TEST(CommutativeCells, GlobalCache) { - sol_rules rules; - rules.cells = 3; - rules.build_pol = pol::SAME_SUIT; - global_cache cache(rules); - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - EXPECT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - EXPECT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty cells - cache.insert ({{card("4C")},{card("5D")},{}}); - EXPECT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - EXPECT_FALSE(cache.contains({{},{card("4C")},{}})); -} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 14218f7..5758165 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -3,6 +3,7 @@ // #include <rapidjson/document.h> +#include <gtest/gtest.h> #include "test_helper.h" #include "../main/game/game_state.h" @@ -10,6 +11,8 @@ #include "../main/input-output/input/json-parsing/json_helper.h" #include "../main/input-output/input/json-parsing/rules_parser.h" +#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == t_f) + using namespace rapidjson; using namespace std; @@ -18,7 +21,70 @@ bool test_helper::is_solvable(const std::string& input_file, const std::string& const sol_rules rules = rules_parser::from_preset(preset_type); game_state gs(rules, in_doc); - solver sol(gs, rules); + solver sol(gs); return sol.run(); } + +void test_helper::run_card_cache_test(sol_rules::build_policy bp, + bool assert_dif_col, + bool assert_same_col) { + sol_rules rules; + rules.tableau_pile_count = 1; + rules.build_pol = bp; + game_state gs(rules, 0); + + for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { + for (card::rank_t rank = 1; rank <= 13; rank++) { + global_cache cache; + + // c2 is the same as c_ but with a different suit + const card c = card(suit_idx, rank); + const card c_dif_col = card(uint8_t((suit_idx + 1) % 4), rank); + const card c_same_col = card(uint8_t((suit_idx + 2) % 4), rank); + const card c_dif_rank = card(suit_idx, card::rank_t((rank % 13) + 1)); + + const game_state s = {{c}}; + const game_state s_dif_suit = {{c_dif_col}}; + const game_state s_same_col = {{c_same_col}}; + const game_state s_dif_rank = {{c_dif_rank}}; + + cache.insert(s); + + ASSERT_TRUE(cache.contains(s)) << "Contains self"; + + ASSERT_(assert_dif_col, hash_value(s) == hash_value(s_dif_suit)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_col.to_string(); + ASSERT_(assert_dif_col, global_cache::game_state_pred()(s, s_dif_suit)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_col.to_string(); + ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_col.to_string(); + + ASSERT_(assert_same_col, hash_value(s) == hash_value(s_same_col)) + << "Insert: " << c.to_string() + << ", Comp: " << c_same_col.to_string(); + ASSERT_(assert_same_col, global_cache::game_state_pred()(s, s_same_col)) + << "Insert: " << c.to_string() + << ", Comp: " << c_same_col.to_string(); + ASSERT_(assert_same_col, cache.contains(s_same_col)) + << "Insert: " << c.to_string() + << ", Comp: " << c_same_col.to_string(); + + ASSERT_FALSE(hash_value(s) == hash_value(s_dif_rank)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_rank.to_string(); + ASSERT_FALSE(global_cache::game_state_pred()(s, s_dif_rank)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_rank.to_string(); + ASSERT_FALSE(cache.contains(s_dif_rank)) + << "Insert: " << c.to_string() + << ", Comp: " << c_dif_rank.to_string(); + } + } + + rules.tableau_pile_count = 0; + game_state _gs(rules, 0); +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 5b8e755..fe7f8c7 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -7,9 +7,12 @@ #include <string> +#include "../main/game/sol_rules.h" + class test_helper { public: static bool is_solvable(const std::string&, const std::string&); + static void run_card_cache_test(sol_rules::build_policy, bool, bool); }; |
