From 252c7398925b5267963c1a48251cd7ba2d4ec381 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 25 Jan 2018 15:20:54 +0000 Subject: Refactor game_state & cache to remove static hack --- src/main/game/game_state.cpp | 30 +---- src/main/game/game_state.h | 23 ++-- src/main/game/global_cache.cpp | 130 ++++++++++++--------- src/main/game/global_cache.h | 41 ++++--- src/main/game/pile.h | 2 +- .../input/json-parsing/deal_parser.cpp | 24 ++-- src/main/solver/solver.cpp | 10 +- src/test/gloabl_cache_test.cpp | 102 ---------------- src/test/global_cache_test.cpp | 93 +++++++++++++++ src/test/test_helper.cpp | 29 +++-- 10 files changed, 239 insertions(+), 245 deletions(-) delete mode 100644 src/test/gloabl_cache_test.cpp create mode 100644 src/test/global_cache_test.cpp (limited to 'src') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 8075f50..8f45a38 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -28,10 +28,7 @@ typedef sol_rules::stock_deal_type sdt; // A private constructor used by both of the public ones. Initializes all of the // piles and pile refs specified by the rules -game_state::game_state(const sol_rules& s_rules) { - game_state::static_reset(); - game_state::rules = s_rules; - +game_state::game_state(const sol_rules& s_rules) : rules(s_rules) { // If there is a hole, creates pile if (rules.hole) { piles.emplace_back(); @@ -203,31 +200,6 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i) } -//////////////////////////// -// SETUP STATIC VARIABLES // -//////////////////////////// - -sol_rules game_state::rules; -std::vector game_state::tableau_piles; -std::vector game_state::cells; -game_state::pile_ref game_state::stock; -game_state::pile_ref game_state::waste; -std::vector game_state::reserve; -std::vector game_state::foundations; -game_state::pile_ref game_state::hole; - - -void game_state::static_reset() { - tableau_piles.clear(); - cells.clear(); - stock = 0; - waste = 0; - reserve.clear(); - foundations.clear(); - hole = 0; -} - - ///////////////////////// // MODIFYING FUNCTIONS // ///////////////////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8dcffa4..8434bf7 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -18,7 +18,8 @@ class game_state { friend class deal_parser; friend class state_printer; - friend class game_state_pred; + friend class predicate; + friend class hasher; public: // Defines types typedef uint8_t pile_ref; @@ -48,11 +49,7 @@ public: friend std::ostream& operator<< (std::ostream&, const game_state&); - friend std::size_t hash_value(card const&); - friend std::size_t hash_value(game_state const&); - private: - void static_reset(); static std::vector gen_shuffled_deck(int, card::rank_t, bool); // Private constructor @@ -71,14 +68,14 @@ private: card) const; // References to piles - static sol_rules rules; - static std::vector tableau_piles; - static std::vector cells; - static pile_ref stock; - static pile_ref waste; - static std::vector reserve; - static std::vector foundations; - static pile_ref hole; + const sol_rules rules; + std::vector tableau_piles; + std::vector cells; + pile_ref stock; + pile_ref waste; + std::vector reserve; + std::vector foundations; + pile_ref hole; // The core piles std::vector piles; diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 8e3aa8b..1ce8d5d 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -4,6 +4,8 @@ #include +#include + #include "global_cache.h" using namespace std; @@ -12,92 +14,111 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; -bool global_cache::insert(const game_state& gs) { - return u_set.insert(gs).second; + +bool global_cache::insert(const std::vector& piles) { + return u_set.insert(piles).second; } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(gs) > 0; +bool global_cache::contains(const std::vector& piles) const { + return u_set.count(piles) > 0; } void global_cache::clear() { u_set.clear(); } -size_t hash_value(card const& c) { - boost::hash hasher; - -#ifdef SIMPLE_HASH - uint8_t suit_val = static_cast(c.get_suit()); -#else - uint8_t suit_val; - switch (game_state::rules.build_pol) { - case pol::SAME_SUIT: - suit_val = static_cast(c.get_suit()); - break; - case pol::RED_BLACK: - suit_val = static_cast(c.get_colour()); - break; - default: - suit_val = 0; - } -#endif - - auto raw_val = static_cast(suit_val * 13 + c.get_rank()); - return hasher(raw_val); +global_cache::global_cache(const game_state& gs) + : u_set(0, hasher(gs), predicate(gs)) { } -size_t hash_value(pile const& p) { - return hash_range(begin(p.pile_vec), end(p.pile_vec)); +hasher::hasher(const game_state& gs) : init_gs(gs) { } -size_t hash_value(game_state const& gs) { +size_t hasher::operator()(const vector& piles) const { size_t seed = 0; #ifdef SIMPLE_HASH - for (game_state::pile_ref pr = 0; pr < gs.piles.size(); pr++) { - hash_combine(seed, gs.piles[pr]); + for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) { + seed += combine(hash_value(piles[pr])); } #else - boost::hash pile_hasher; - // Using addition for commutative hash - for (game_state::pile_ref pr : game_state::tableau_piles) { - seed += pile_hasher(gs.piles[pr]); + for (game_state::pile_ref pr : init_gs.tableau_piles) { + combine_commutative(seed, hash_value(piles[pr])); } // Using hash_combine for order-dependent hash - for (game_state::pile_ref pr : game_state::foundations) { - hash_combine(seed, gs.piles[pr]); + for (game_state::pile_ref pr : init_gs.foundations) { + combine(seed, hash_value(piles[pr])); } // Commutative - for (game_state::pile_ref pr : game_state::reserve) { - seed += pile_hasher(gs.piles[pr]); + for (game_state::pile_ref pr : init_gs.reserve) { + combine_commutative(seed, hash_value(piles[pr])); } - if (game_state::rules.stock_size > 0) { - hash_combine(seed, gs.piles[game_state::stock]); + if (init_gs.rules.stock_size > 0) { + combine(seed, hash_value(piles[init_gs.stock])); } - if (game_state::rules.stock_deal_t == sdt::WASTE) { - hash_combine(seed, gs.piles[game_state::waste]); + if (init_gs.rules.stock_deal_t == sdt::WASTE) { + combine(seed, hash_value(piles[init_gs.waste])); } - if (game_state::rules.hole) { - hash_combine(seed, gs.piles[game_state::hole]); + if (init_gs.rules.hole) { + combine(seed, hash_value(piles[init_gs.hole])); } // Commutative - for (game_state::pile_ref pr : game_state::cells) { - seed += pile_hasher(gs.piles[pr]); + for (game_state::pile_ref pr : init_gs.cells) { + combine_commutative(seed, hash_value(piles[pr])); } #endif return seed; } -bool game_state_pred::comp_pile(const pile &x, const pile &y) { +size_t hasher::hash_value(pile const& p) const { + std::size_t seed = 0; + + for(auto first = begin(p.pile_vec); first != end(p.pile_vec); ++first) { + combine(seed, hash_value(*first)); + } + return seed; +} + +std::size_t hasher::combine(std::size_t& seed, std::size_t value) const { + return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2); +} + +std::size_t hasher::combine_commutative(std::size_t& seed, std::size_t value) const { + return seed += value; +} + +size_t hasher::hash_value(card const& c) const { + boost::hash boost_hasher; + +#ifdef SIMPLE_HASH + uint8_t suit_val = static_cast(c.get_suit()); +#else + uint8_t suit_val; + switch (init_gs.rules.build_pol) { + case pol::SAME_SUIT: + suit_val = static_cast(c.get_suit()); + break; + case pol::RED_BLACK: + suit_val = static_cast(c.get_colour()); + break; + default: + suit_val = 0; + } +#endif + + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); + return boost_hasher(raw_val); +} + +bool predicate::comp_pile(const pile &x, const pile &y) { // Largest piles first if (x.size() != y.size()) { return x.size() > y.size(); @@ -116,12 +137,12 @@ bool game_state_pred::comp_pile(const pile &x, const pile &y) { return false; } -bool game_state_pred::operator()(const game_state& x, - const game_state& y) const { - if (x.piles.size() != y.piles.size()) return false; +bool predicate::operator()(const std::vector& x, + const std::vector& y) const { + if (x.size() != y.size()) return false; - vector x_piles = x.piles; - vector y_piles = y.piles; + vector x_piles = x; + vector y_piles = y; // Orders the piles from largest to smallest sort(begin(x_piles), end(x_piles), comp_pile); @@ -140,7 +161,7 @@ bool game_state_pred::operator()(const game_state& x, #ifdef SIMPLE_HASH if (cx.get_suit() != cy.get_suit()) return false; #else - switch (game_state::rules.build_pol) { + switch (init_gs.rules.build_pol) { case pol::SAME_SUIT: if (cx.get_suit() != cy.get_suit()) return false; break; @@ -156,3 +177,6 @@ bool game_state_pred::operator()(const game_state& x, return true; } + +predicate::predicate(const game_state& gs) : init_gs(gs) { +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 486006c..0e8fe0c 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -8,27 +8,40 @@ #include #include -#include - #include "sol_rules.h" #include "game_state.h" -struct game_state_pred { - static bool comp_pile(const pile& x, const pile& y); - bool operator() (const game_state& x, const game_state& y) - const; +class predicate { +public: + explicit predicate(const game_state&); + static bool comp_pile(const pile&, const pile&); + bool operator() (const std::vector&, const std::vector&) const; +private: + const game_state& init_gs; }; -class global_cache { -public: - bool insert(const game_state&); - bool contains(const game_state&) const; +struct hasher { + explicit hasher(const game_state&); + std::size_t operator() (const std::vector&) const; + + std::size_t hash_value(const card&) const; + std::size_t hash_value(const pile&) const; + std::size_t combine(std::size_t&, std::size_t) const; + std::size_t combine_commutative(std::size_t&, std::size_t) const; + + const game_state& init_gs; +}; + +struct global_cache { + explicit global_cache(const game_state&); + bool insert(const std::vector&); + bool contains(const std::vector&) const; void clear(); -private: + std::unordered_set< - game_state, - boost::hash, - game_state_pred + std::vector, + hasher, + predicate > u_set; }; diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 411f890..2bf2d5b 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -12,6 +12,7 @@ #include "card.h" class pile { + friend class hasher; public: typedef uint8_t size_type; @@ -33,7 +34,6 @@ public: card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); - friend std::size_t hash_value(pile const&); private: // Underlying vector std::vector pile_vec; diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp index 7cf5f45..09b2065 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -13,32 +13,32 @@ typedef sol_rules::build_policy pol; void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { // Construct tableau piles - if (game_state::rules.tableau_pile_count > 0) { + if (gs.rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); } // Construct hole card - if (game_state::rules.hole) { + if (gs.rules.hole) { parse_hole(gs, doc); } // Construct filled cells - if (game_state::rules.cells) { + if (gs.rules.cells) { parse_cells(gs, doc); } // Construct stock - if (game_state::rules.stock_size > 0) { + if (gs.rules.stock_size > 0) { parse_stock(gs, doc); } // Construct reserve - if (game_state::rules.reserve_size > 0) { + if (gs.rules.reserve_size > 0) { parse_reserve(gs, doc); } // If the foundations begin with cards in them, fill them - if (game_state::rules.foundations_init_card) { + if (gs.rules.foundations_init_card) { fill_foundations(gs); } } @@ -48,7 +48,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); - if (json_tab_piles.Size() != game_state::rules.tableau_pile_count ) { + if (json_tab_piles.Size() != gs.rules.tableau_pile_count ) { json_helper::json_parse_err("Incorrect number of tableau piles"); } @@ -81,7 +81,7 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { const auto json_cell_arr = json_cells.GetArray(); if (json_cell_arr.Empty()) return; - else if (json_cell_arr.Size() != game_state::rules.cells) { + else if (json_cell_arr.Size() != gs.rules.cells) { json_helper::json_parse_err("Incorrect number of cells"); } @@ -101,7 +101,7 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { const Value &json_stock = doc["stock"]; assert(json_stock.IsArray()); - if (json_stock.Size() != game_state::rules.stock_size) { + if (json_stock.Size() != gs.rules.stock_size) { json_helper::json_parse_err("Incorrect stock size"); } @@ -121,20 +121,20 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { // We treat a regular reserve like multiple single-card piles, // but a stacked reserve as a single multiple-card pile - if (json_reserve_piles.Size() != game_state::rules.reserve_size) { + if (json_reserve_piles.Size() != gs.rules.reserve_size) { json_helper::json_parse_err("Incorrect reserve size"); } for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) { assert(json_card_arr[i].IsString()); game_state::pile_ref pr = gs.reserve[0]; - if (!game_state::rules.reserve_stacked) pr += i; + if (!gs.rules.reserve_stacked) pr += i; gs.piles[pr].place(card(json_card_arr[i].GetString())); } } void deal_parser::fill_foundations(game_state &gs) { - auto foundations_count = uint8_t(4 * (game_state::rules.two_decks ? 2:1)); + auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1)); assert(gs.foundations.size() == foundations_count); for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) { diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 19fa79b..072db18 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -16,7 +16,7 @@ using namespace std; using namespace boost; solver::solver(const game_state& gs) - : initial_state(gs), states_searched(0) {} + : cache(gs), initial_state(gs), states_searched(0) {} solver::node::node(const game_state::move m, vector uc) @@ -30,7 +30,7 @@ bool solver::run() { states_searched++; LOG_DEBUG (state); frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - cache.insert(state); + cache.insert(state.get_data()); while (!frontier.empty()) { node& current = frontier.back(); @@ -47,7 +47,7 @@ bool solver::run() { // (unless it's the null first move) if (frontier.size() > 1) { state.undo_move(current.move); - assert(cache.contains(state)); + assert(cache.contains(state.get_data())); LOG_DEBUG (state); } // Returns to the previous state @@ -60,7 +60,7 @@ bool solver::run() { state.make_move(next_move); // Insert the state into the global cache - bool is_new_state = cache.insert(state); + bool is_new_state = cache.insert(state.get_data()); if (is_new_state) { states_searched++; LOG_DEBUG (state); @@ -70,7 +70,7 @@ bool solver::run() { } else { // If we've seen the state before, undoes the move state.undo_move(next_move); - assert(cache.contains(state)); + assert(cache.contains(state.get_data())); } } } diff --git a/src/test/gloabl_cache_test.cpp b/src/test/gloabl_cache_test.cpp deleted file mode 100644 index beb1e0f..0000000 --- a/src/test/gloabl_cache_test.cpp +++ /dev/null @@ -1,102 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#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/global_cache_test.cpp b/src/test/global_cache_test.cpp new file mode 100644 index 0000000..69bfc91 --- /dev/null +++ b/src/test/global_cache_test.cpp @@ -0,0 +1,93 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#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(gs); + + 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")}})); +} + +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(gs); + + 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")}})); +} + +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(gs); + + 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")},{}})); +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 5758165..81f11e7 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -11,7 +11,7 @@ #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) +#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f)) using namespace rapidjson; using namespace std; @@ -36,7 +36,7 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { for (card::rank_t rank = 1; rank <= 13; rank++) { - global_cache cache; + global_cache cache(gs); // c2 is the same as c_ but with a different suit const card c = card(suit_idx, rank); @@ -49,42 +49,39 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, const game_state s_same_col = {{c_same_col}}; const game_state s_dif_rank = {{c_dif_rank}}; - cache.insert(s); + cache.insert(s.get_data()); - ASSERT_TRUE(cache.contains(s)) << "Contains self"; + ASSERT_TRUE(cache.contains(s.get_data())) << "Contains self"; - ASSERT_(assert_dif_col, hash_value(s) == hash_value(s_dif_suit)) + ASSERT_(assert_dif_col, hasher(gs)(s.get_data()) == hasher(gs)(s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, global_cache::game_state_pred()(s, s_dif_suit)) + ASSERT_(assert_dif_col, predicate(gs)(s.get_data(), s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) + ASSERT_(assert_dif_col, cache.contains(s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_same_col, hash_value(s) == hash_value(s_same_col)) + ASSERT_(assert_same_col, hasher(gs)(s.get_data()) == hasher(gs)(s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, global_cache::game_state_pred()(s, s_same_col)) + ASSERT_(assert_same_col, predicate(gs)(s.get_data(), s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, cache.contains(s_same_col)) + ASSERT_(assert_same_col, cache.contains(s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_FALSE(hash_value(s) == hash_value(s_dif_rank)) + ASSERT_FALSE(hasher(gs)(s.get_data()) == hasher(gs)(s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(global_cache::game_state_pred()(s, s_dif_rank)) + ASSERT_FALSE(predicate(gs)(s.get_data(), s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(cache.contains(s_dif_rank)) + ASSERT_FALSE(cache.contains(s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); } } - - rules.tableau_pile_count = 0; - game_state _gs(rules, 0); } -- cgit v1.2.1