From 2a0cfd3af6d5dd5e4c7a49e713f13d65f1ad977a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 16:58:08 +0000 Subject: Add foundations dominance testing --- src/test/foundations_dominance_test.cpp | 29 +++++++++++++++++++++++++++++ src/test/test_helper.cpp | 32 ++++++++++++++++++++++++++++++++ src/test/test_helper.h | 4 ++++ 3 files changed, 65 insertions(+) create mode 100644 src/test/foundations_dominance_test.cpp (limited to 'src/test') diff --git a/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp new file mode 100644 index 0000000..b12a478 --- /dev/null +++ b/src/test/foundations_dominance_test.cpp @@ -0,0 +1,29 @@ +// +// 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(FoundationsDominance, SameSuit) { + test_helper::run_foundations_dominance_test(pol::SAME_SUIT, { + "AC","2C","3C","4C","AH","2H","3H","4H","AS","2S","3S","4S","AD", + "2D","3D","4D"}); +} + +TEST(FoundationsDominance, RedBlack) { + test_helper::run_foundations_dominance_test(pol::RED_BLACK, { + "AC","2C","AH","2H","AS","3H","2S","AD","3C","4H","3S","2D","4C", + "4S","3D","4D"}); +} + +TEST(FoundationsDominance, AnySuit) { + test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { + "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", + "4S","3D","4D"}); +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 9ee1233..246de38 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -78,3 +78,35 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, } } } + +void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector cards) { + sol_rules rules = rules_parser::from_preset("simple-free-cell"); + rules.build_pol = policy; + rules.foundations_removable = false; + rules.cells = 0; + + game_state gs = game_state(rules, std::initializer_list{ + {}, + {}, + {}, + {}, + {"4C","3C","2C","AC"}, + {"4H","3H","2H","AH"}, + {"4S","3S","2S","AS"}, + {"4D","3D","2D","AD"} + }); + + solver sol = solver(game_state(gs)); + sol.run(); + + solver::node const *n = &sol.get_search_tree(); + for (card c : cards) { + n = &n->children.back(); + ASSERT_TRUE(n->move.is_dominance()); + ASSERT_TRUE(gs.get_data()[n->move.from].top_card() == c); + ASSERT_TRUE(n->move.to >= 0 && n->move.to <= 4); + gs.make_move(n->move); + } + ASSERT_TRUE(n->children.empty()); +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index fe7f8c7..b0199af 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -8,11 +8,15 @@ #include #include "../main/game/sol_rules.h" +#include "../main/game/card.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); + + static void run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector cards); }; -- cgit v1.2.1 From 46e31f8be960b9aa00e9bf9218cc712674e4455b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 14 Feb 2018 20:09:59 +0000 Subject: Add multiple runs test for foundations dominance --- src/test/foundations_dominance_test.cpp | 5 ++++ src/test/test_helper.cpp | 44 +++++++++++++++++++++++++++++++++ src/test/test_helper.h | 8 ++++++ 3 files changed, 57 insertions(+) (limited to 'src/test') diff --git a/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp index b12a478..48134f5 100644 --- a/src/test/foundations_dominance_test.cpp +++ b/src/test/foundations_dominance_test.cpp @@ -27,3 +27,8 @@ TEST(FoundationsDominance, AnySuit) { "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", "4S","3D","4D"}); } + +TEST(FoundationsDominance, NoSolutionChange) { + test_helper::optimisation_validity_test( + test_helper::optimisation::AUTO_FOUNDATION_MOVES); +} \ No newline at end of file diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 246de38..37fc32d 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -110,3 +110,47 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, } ASSERT_TRUE(n->children.empty()); } + +void test_helper::optimisation_validity_test(optimisation op) { + for (string preset : { + "simple-alpha-star", + "simple-bakers-dozen", + "simple-black-hole", + "simple-canfield", + "simple-flower-garden", + "simple-fortunes-favor", + "simple-free-cell", + "simple-somerset", + "simple-spanish-patience", + "simple-spider" + }) { + vector solutions_op, solutions_no_op; + + switch (op) { + case optimisation::AUTO_FOUNDATION_MOVES: +#define NO_AUTO_FOUNDATIONS + solutions_no_op = run_many_games(preset); +#undef NO_AUTO_FOUNDATIONS + solutions_op = run_many_games(preset); + break; + } + + ASSERT_TRUE(solutions_op == solutions_no_op) << preset; + } +} + +std::vector test_helper::run_many_games(std::string preset) { +#ifdef NDEBUG + const int run_count = 2000; +#else + const int run_count = 5; +#endif + + vector solutions; + for (int seed = 0; seed < run_count; seed++) { + solver sol(game_state(rules_parser::from_preset(preset), seed)); + solutions.push_back(sol.run()); + } + + return solutions; +} diff --git a/src/test/test_helper.h b/src/test/test_helper.h index b0199af..24acb5f 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -12,11 +12,19 @@ class test_helper { public: + enum class optimisation { + AUTO_FOUNDATION_MOVES + }; + static bool is_solvable(const std::string&, const std::string&); static void run_card_cache_test(sol_rules::build_policy, bool, bool); static void run_foundations_dominance_test(sol_rules::build_policy policy, std::vector cards); + + static void optimisation_validity_test(optimisation); + + static std::vector run_many_games(std::string); }; -- cgit v1.2.1