diff options
Diffstat (limited to 'solvitaire/src/test')
76 files changed, 2898 insertions, 0 deletions
diff --git a/solvitaire/src/test/integration_tests/accordion_test.cpp b/solvitaire/src/test/integration_tests/accordion_test.cpp new file mode 100644 index 0000000..bd27dec --- /dev/null +++ b/solvitaire/src/test/integration_tests/accordion_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/accordion/"; +const static std::string type = "simple-accordion"; + +TEST(Accordion, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Accordion, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Accordion, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Accordion, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/alpha_star_test.cpp b/solvitaire/src/test/integration_tests/alpha_star_test.cpp new file mode 100644 index 0000000..c3b23fe --- /dev/null +++ b/solvitaire/src/test/integration_tests/alpha_star_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/alpha_star/"; +const static std::string type = "simple-alpha-star"; + +TEST(AlphaStar, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(AlphaStar, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(AlphaStar, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(AlphaStar, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/bakers_dozen_test.cpp b/solvitaire/src/test/integration_tests/bakers_dozen_test.cpp new file mode 100644 index 0000000..f3d4763 --- /dev/null +++ b/solvitaire/src/test/integration_tests/bakers_dozen_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/bakers_dozen/"; +const static std::string type = "simple-bakers-dozen"; + +TEST(BakersDozen, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(BakersDozen, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(BakersDozen, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(BakersDozen, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/black_hole_test.cpp b/solvitaire/src/test/integration_tests/black_hole_test.cpp new file mode 100644 index 0000000..a0f6707 --- /dev/null +++ b/solvitaire/src/test/integration_tests/black_hole_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/black_hole/"; +const static std::string type = "simple-black-hole"; + +TEST(BlackHole, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(BlackHole, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(BlackHole, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(BlackHole, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/canfield_test.cpp b/solvitaire/src/test/integration_tests/canfield_test.cpp new file mode 100644 index 0000000..0cb14b2 --- /dev/null +++ b/solvitaire/src/test/integration_tests/canfield_test.cpp @@ -0,0 +1,139 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/input-output/input/json-parsing/rules_parser.h" +#include "../../main/game/search-state/game_state.h" + +using std::vector; + +typedef test_helper th; +typedef game_state::streamliner_options so; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + +const static std::string path = "resources/canfield/"; +const static std::string type = "simple-canfield"; + +TEST(Canfield, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Canfield, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Canfield, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Canfield, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} + +TEST(Canfield, FoundationSingleCard) { + sol_rules sr = rules_parser::from_preset("canfield"); + game_state gs(sr, 0, so::NONE); + + ASSERT_EQ(1, test_helper::cards_in_founds(gs)); +} + +TEST(Canfield, FoundationWraps) { + sol_rules sr = rules_parser::from_preset("simple-canfield"); + + game_state gs(sr, string_il{ + {"3C"},{},{},{}, // Foundations + {},{}, // Stock-waste + {}, // Reserve + {"AC"},{},{} // Tableau piles + }); + + vector<move> actual_moves = gs.get_legal_moves(); + vector<move> exp_moves = { + move(move::mtype::regular, 7, 0, 1) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(Canfield, MoveWholePile) { + sol_rules sr = rules_parser::from_preset("simple-canfield"); + + game_state gs(sr, string_il{ + {"AC"},{},{},{}, // Foundations + {},{}, // Stock-waste + {}, // Reserve + {"2C","AH"},{"3H"},{"3C","2D"} // Tableau piles + }); + + vector<move> actual_moves = gs.get_legal_moves(); + vector<move> exp_moves = { + move(move::mtype::built_group, 7, 8, 2), + move(move::mtype::regular, 7, 1, 1), + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(Canfield, MoveWholePileNotSpace) { + sol_rules sr = rules_parser::from_preset("simple-canfield"); + + game_state gs(sr, string_il{ + {"3C"},{},{},{}, // Foundations + {},{}, // Stock-waste + {}, // Reserve + {"2H","3S","AH"},{},{"2D"} // Tableau piles + }); + + vector<move> actual_moves = gs.get_legal_moves(); + vector<move> exp_moves = {}; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(Canfield, AutoReserve) { + sol_rules sr = rules_parser::from_preset("simple-canfield"); + + game_state gs(sr, string_il{ + {"2C"},{},{},{}, // Foundations + {"3D","2D"},{}, // Stock-waste + {"3H"}, // Reserve + {"2H","3S","AH"},{},{"AC"} // Tableau piles + }); + + vector<move> actual_moves = gs.get_legal_moves(); + vector<move> exp_moves = {}; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; + + boost::optional<move> actual_dom_move = gs.get_dominance_move(); + boost::optional<move> exp_dom_move = move(move::mtype::dominance, 6, 8, 1); + + ASSERT_EQ(exp_dom_move, actual_dom_move) << *actual_dom_move; +} + +TEST(Canfield, AutoReserveThenWaste) { + sol_rules sr = rules_parser::from_preset("simple-canfield"); + + game_state gs(sr, string_il{ + {"3C"},{},{},{}, // Foundations + {"2D"},{"2S"}, // Stock-waste + {}, // Reserve + {"2H","3S","AH"},{},{"AD"} // Tableau piles + }); + + vector<move> actual_moves = gs.get_legal_moves(); + vector<move> exp_moves = { + move(move::mtype::stock_k_plus, 4, 8, 0), + move(move::mtype::stock_k_plus, 4, 8, 1) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; + + boost::optional<move> actual_dom_move = gs.get_dominance_move(); + boost::optional<move> exp_dom_move = boost::none; + + ASSERT_EQ(exp_dom_move, actual_dom_move) << *actual_dom_move; +} diff --git a/solvitaire/src/test/integration_tests/flower_garden_test.cpp b/solvitaire/src/test/integration_tests/flower_garden_test.cpp new file mode 100644 index 0000000..641ccc0 --- /dev/null +++ b/solvitaire/src/test/integration_tests/flower_garden_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/flower_garden/"; +const static std::string type = "simple-flower-garden"; + +TEST(FlowerGarden, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FlowerGarden, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FlowerGarden, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FlowerGarden, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/fortunes_favor_test.cpp b/solvitaire/src/test/integration_tests/fortunes_favor_test.cpp new file mode 100644 index 0000000..5511f9e --- /dev/null +++ b/solvitaire/src/test/integration_tests/fortunes_favor_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/fortunes_favor/"; +const static std::string type = "simple-fortunes-favor"; + +TEST(FortunesFavor, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FortunesFavor, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FortunesFavor, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FortunesFavor, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/free_cell_test.cpp b/solvitaire/src/test/integration_tests/free_cell_test.cpp new file mode 100644 index 0000000..659d86b --- /dev/null +++ b/solvitaire/src/test/integration_tests/free_cell_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/free_cell/"; +const static std::string type = "simple-free-cell"; + +TEST(FreeCell, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(FreeCell, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(FreeCell, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(FreeCell, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/gaps_test.cpp b/solvitaire/src/test/integration_tests/gaps_test.cpp new file mode 100644 index 0000000..f7d0ebe --- /dev/null +++ b/solvitaire/src/test/integration_tests/gaps_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/gaps/"; +const static std::string type = "simple-gaps"; + +TEST(Gaps, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Gaps, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Gaps, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Gaps, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/klondike_test.cpp b/solvitaire/src/test/integration_tests/klondike_test.cpp new file mode 100644 index 0000000..d449495 --- /dev/null +++ b/solvitaire/src/test/integration_tests/klondike_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/klondike/"; +const static std::string type = "simple-klondike"; + +TEST(Klondike, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Klondike, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Klondike, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Klondike, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/somerset_test.cpp b/solvitaire/src/test/integration_tests/somerset_test.cpp new file mode 100644 index 0000000..fce8e8c --- /dev/null +++ b/solvitaire/src/test/integration_tests/somerset_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/somerset/"; +const static std::string type = "simple-somerset"; + +TEST(Somerset, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Somerset, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Somerset, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(Somerset, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/spanish_patience_test.cpp b/solvitaire/src/test/integration_tests/spanish_patience_test.cpp new file mode 100644 index 0000000..da32fd5 --- /dev/null +++ b/solvitaire/src/test/integration_tests/spanish_patience_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/spanish_patience/"; +const static std::string type = "simple-spanish-patience"; + +TEST(SpanishPatience, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(SpanishPatience, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(SpanishPatience, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +TEST(SpanishPatience, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} diff --git a/solvitaire/src/test/integration_tests/spider_test.cpp b/solvitaire/src/test/integration_tests/spider_test.cpp new file mode 100644 index 0000000..50f16fe --- /dev/null +++ b/solvitaire/src/test/integration_tests/spider_test.cpp @@ -0,0 +1,30 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" + +typedef test_helper th; + +const static std::string path = "resources/spider/"; +const static std::string type = "simple-spider"; + +TEST(Spider, SimpleSolvable) { + EXPECT_TRUE(th::is_solvable(path + "SimpleSolvable.json", type)); +} + +TEST(Spider, ComplexSolvable) { + EXPECT_TRUE(th::is_solvable(path + "ComplexSolvable.json", type)); +} + +TEST(Spider, SimpleUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "SimpleUnsolvable.json", type)); +} + +/* Taking too long so removed, at least for now +TEST(Spider, ComplexUnsolvable) { + EXPECT_FALSE(th::is_solvable(path + "ComplexUnsolvable.json", type)); +} +*/ diff --git a/solvitaire/src/test/resources/accordion/ComplexSolvable.json b/solvitaire/src/test/resources/accordion/ComplexSolvable.json new file mode 100644 index 0000000..cdb2367 --- /dev/null +++ b/solvitaire/src/test/resources/accordion/ComplexSolvable.json @@ -0,0 +1,3 @@ +{ + "accordion": ["AC","3H","AH","2S","AD","3S","3C","AS","2C","3D"] +} diff --git a/solvitaire/src/test/resources/accordion/ComplexUnsolvable.json b/solvitaire/src/test/resources/accordion/ComplexUnsolvable.json new file mode 100644 index 0000000..09c7d32 --- /dev/null +++ b/solvitaire/src/test/resources/accordion/ComplexUnsolvable.json @@ -0,0 +1,3 @@ +{ + "accordion": ["AD","AS","AH","2S","3C","3S","4C","3H","2C","4D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/accordion/SimpleSolvable.json b/solvitaire/src/test/resources/accordion/SimpleSolvable.json new file mode 100644 index 0000000..00de391 --- /dev/null +++ b/solvitaire/src/test/resources/accordion/SimpleSolvable.json @@ -0,0 +1,3 @@ +{ + "accordion": ["AC","AS","AH","2C","2S","2H","3C","3S","3H","4C"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/accordion/SimpleUnsolvable.json b/solvitaire/src/test/resources/accordion/SimpleUnsolvable.json new file mode 100644 index 0000000..2e1b9c3 --- /dev/null +++ b/solvitaire/src/test/resources/accordion/SimpleUnsolvable.json @@ -0,0 +1,3 @@ +{ + "accordion": ["AC","4S","AH","2S","AD","3S","4C","3H","2C","3D"] +} diff --git a/solvitaire/src/test/resources/alpha_star/ComplexSolvable.json b/solvitaire/src/test/resources/alpha_star/ComplexSolvable.json new file mode 100644 index 0000000..ab6b24e --- /dev/null +++ b/solvitaire/src/test/resources/alpha_star/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["6C","3S","2C","3C","5C","5H","4H"], + ["6S","4S","4C","5S","2D","5D"], + ["6H","6D","2S","2H","3H","4D","3D"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/alpha_star/ComplexUnsolvable.json b/solvitaire/src/test/resources/alpha_star/ComplexUnsolvable.json new file mode 100644 index 0000000..56ea441 --- /dev/null +++ b/solvitaire/src/test/resources/alpha_star/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["6C","2S","2C","3C","5C","5H","4H"], + ["6S","4S","4C","5S","2D","5D"], + ["6H","6D","3S","2H","3H","4D","3D"] + ] +} diff --git a/solvitaire/src/test/resources/alpha_star/SimpleSolvable.json b/solvitaire/src/test/resources/alpha_star/SimpleSolvable.json new file mode 100644 index 0000000..7994021 --- /dev/null +++ b/solvitaire/src/test/resources/alpha_star/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["6C","5S","5C","4C","3C","2S","2C"], + ["6S","6D","5D","4D","3S","3D","2D"], + ["6H","5H","4S","4H","3H","2H"] + ] +} diff --git a/solvitaire/src/test/resources/alpha_star/SimpleUnsolvable.json b/solvitaire/src/test/resources/alpha_star/SimpleUnsolvable.json new file mode 100644 index 0000000..ac6741e --- /dev/null +++ b/solvitaire/src/test/resources/alpha_star/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S","2C","3C","4C","5S","5C","6C"], + ["2D","3D","4S","4D","5D","6S","6D"], + ["2H","3S","3H","4H","5H","6H"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/bakers_dozen/ComplexSolvable.json b/solvitaire/src/test/resources/bakers_dozen/ComplexSolvable.json new file mode 100644 index 0000000..b953413 --- /dev/null +++ b/solvitaire/src/test/resources/bakers_dozen/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AC","2S","AS","3H"], + ["AD","2D","2H","AH"], + ["3D","3C","2C","3S"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/bakers_dozen/ComplexUnsolvable.json b/solvitaire/src/test/resources/bakers_dozen/ComplexUnsolvable.json new file mode 100644 index 0000000..a8247c4 --- /dev/null +++ b/solvitaire/src/test/resources/bakers_dozen/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AC","2S","AS","3C"], + ["AD","2D","2H","AH"], + ["3D","3H","2C","3S"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/bakers_dozen/SimpleSolvable.json b/solvitaire/src/test/resources/bakers_dozen/SimpleSolvable.json new file mode 100644 index 0000000..7c00a71 --- /dev/null +++ b/solvitaire/src/test/resources/bakers_dozen/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["3S","2S","AS","AD"], + ["3H","2D","2H","AH"], + ["3D","3C","2C","AC"] + ] +} diff --git a/solvitaire/src/test/resources/bakers_dozen/SimpleUnsolvable.json b/solvitaire/src/test/resources/bakers_dozen/SimpleUnsolvable.json new file mode 100644 index 0000000..0b39a8d --- /dev/null +++ b/solvitaire/src/test/resources/bakers_dozen/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS","2S","2D","3S"], + ["AD","AH","2H","3H"], + ["AC","2C","3C","3D"] + ] +} diff --git a/solvitaire/src/test/resources/black_hole/ComplexSolvable.json b/solvitaire/src/test/resources/black_hole/ComplexSolvable.json new file mode 100644 index 0000000..15488e9 --- /dev/null +++ b/solvitaire/src/test/resources/black_hole/ComplexSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5D","4D","3D","2D","AD"], + ["5C","4C","3C","2C","4H"], + ["AC","AH","2H","3H","5H"], + ["2S","5S","4S","3S"] + ], + "hole": "AS" +} diff --git a/solvitaire/src/test/resources/black_hole/ComplexUnsolvable.json b/solvitaire/src/test/resources/black_hole/ComplexUnsolvable.json new file mode 100644 index 0000000..ed4ee99 --- /dev/null +++ b/solvitaire/src/test/resources/black_hole/ComplexUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5C","3C","2C","4C","AC"], + ["5D","3D","2D","4D","AD"], + ["5H","3H","4S","4H","AH"], + ["5D","2D","2H","2S"] + ], + "hole": "AS" +} diff --git a/solvitaire/src/test/resources/black_hole/SimpleSolvable.json b/solvitaire/src/test/resources/black_hole/SimpleSolvable.json new file mode 100644 index 0000000..3a7be18 --- /dev/null +++ b/solvitaire/src/test/resources/black_hole/SimpleSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["AD","2D","3D","4D","5D"], + ["AC","2C","3C","4C","5C"], + ["AH","2H","3H","4H","5H"], + ["2S","3S","4S","5S"] + ], + "hole": "AS" +} diff --git a/solvitaire/src/test/resources/black_hole/SimpleUnsolvable.json b/solvitaire/src/test/resources/black_hole/SimpleUnsolvable.json new file mode 100644 index 0000000..d6a5e7c --- /dev/null +++ b/solvitaire/src/test/resources/black_hole/SimpleUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["5D","4D","3D","2D","AD"], + ["5C","4C","3C","2C","AC"], + ["5H","4H","3H","2H","AH"], + ["5S","4S","2S","3S"] + ], + "hole": "AS" +} diff --git a/solvitaire/src/test/resources/canfield/ComplexSolvable.json b/solvitaire/src/test/resources/canfield/ComplexSolvable.json new file mode 100644 index 0000000..2a4c252 --- /dev/null +++ b/solvitaire/src/test/resources/canfield/ComplexSolvable.json @@ -0,0 +1,10 @@ +{ + "foundations": ["2C"], + "tableau piles": [ + ["3D"], + ["3H"], + ["3S"] + ], + "reserve": ["AC","2S","3C"], + "stock": ["AH","2H","AD","AS","2D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/canfield/ComplexUnsolvable.json b/solvitaire/src/test/resources/canfield/ComplexUnsolvable.json new file mode 100644 index 0000000..297d06b --- /dev/null +++ b/solvitaire/src/test/resources/canfield/ComplexUnsolvable.json @@ -0,0 +1,10 @@ +{ + "foundations": ["2C"], + "tableau piles": [ + ["AS","3D"], + ["AC","3H"], + ["3S"] + ], + "reserve": ["AH","2S","3C"], + "stock": ["AD","2H","2D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/canfield/SimpleSolvable.json b/solvitaire/src/test/resources/canfield/SimpleSolvable.json new file mode 100644 index 0000000..3353b5c --- /dev/null +++ b/solvitaire/src/test/resources/canfield/SimpleSolvable.json @@ -0,0 +1,10 @@ +{ + "foundations": ["2C"], + "tableau piles": [ + ["2S"], + ["AC"], + ["3D"] + ], + "reserve": ["AH","3S"], + "stock": ["2H","AS","3H","2D","3C","AD"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/canfield/SimpleUnsolvable.json b/solvitaire/src/test/resources/canfield/SimpleUnsolvable.json new file mode 100644 index 0000000..440c358 --- /dev/null +++ b/solvitaire/src/test/resources/canfield/SimpleUnsolvable.json @@ -0,0 +1,10 @@ +{ + "foundations": ["2C"], + "tableau piles": [ + ["3D"], + ["3H"], + ["3S"] + ], + "reserve": ["2S","AS"], + "stock": ["AC","2H","3C","AH","AD","2D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/flower_garden/ComplexSolvable.json b/solvitaire/src/test/resources/flower_garden/ComplexSolvable.json new file mode 100644 index 0000000..1f1a6e6 --- /dev/null +++ b/solvitaire/src/test/resources/flower_garden/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AH","2D","AC"], + ["AD","2C","3D"], + ["2H","AS","3H"] + ], + "reserve": ["3C","2S","3S"] +} diff --git a/solvitaire/src/test/resources/flower_garden/ComplexUnsolvable.json b/solvitaire/src/test/resources/flower_garden/ComplexUnsolvable.json new file mode 100644 index 0000000..be0ca64 --- /dev/null +++ b/solvitaire/src/test/resources/flower_garden/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2H","2D","AC"], + ["AD","2C","3D"], + ["AH","AS","3H"] + ], + "reserve": ["3C","2S","3S"] +} diff --git a/solvitaire/src/test/resources/flower_garden/SimpleSolvable.json b/solvitaire/src/test/resources/flower_garden/SimpleSolvable.json new file mode 100644 index 0000000..18c680d --- /dev/null +++ b/solvitaire/src/test/resources/flower_garden/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["3C","2C","AC"], + ["3D","2D","AD"], + ["3H","3S","AH"] + ], + "reserve": ["2H","2S","AS"] +} diff --git a/solvitaire/src/test/resources/flower_garden/SimpleUnsolvable.json b/solvitaire/src/test/resources/flower_garden/SimpleUnsolvable.json new file mode 100644 index 0000000..e57e9ca --- /dev/null +++ b/solvitaire/src/test/resources/flower_garden/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AC","2C","3C"], + ["AD","2D","3D"], + ["AH","AS","3H"] + ], + "reserve": ["2H","2S","3S"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/fortunes_favor/ComplexSolvable.json b/solvitaire/src/test/resources/fortunes_favor/ComplexSolvable.json new file mode 100644 index 0000000..f00d600 --- /dev/null +++ b/solvitaire/src/test/resources/fortunes_favor/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","4H"], + ["4C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["2H","3S","4S"] +} diff --git a/solvitaire/src/test/resources/fortunes_favor/ComplexUnsolvable.json b/solvitaire/src/test/resources/fortunes_favor/ComplexUnsolvable.json new file mode 100644 index 0000000..4f54b94 --- /dev/null +++ b/solvitaire/src/test/resources/fortunes_favor/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2H","2D","4H"], + ["2C","3D","3H"], + ["2S","3C","4D"] + ], + "stock": ["4C","3S","4S"] +} diff --git a/solvitaire/src/test/resources/fortunes_favor/SimpleSolvable.json b/solvitaire/src/test/resources/fortunes_favor/SimpleSolvable.json new file mode 100644 index 0000000..0891746 --- /dev/null +++ b/solvitaire/src/test/resources/fortunes_favor/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","2H"], + ["3C","3D","3H"], + ["4C","4D","4H"] + ], + "stock": ["4S","3S","2S"] +} diff --git a/solvitaire/src/test/resources/fortunes_favor/SimpleUnsolvable.json b/solvitaire/src/test/resources/fortunes_favor/SimpleUnsolvable.json new file mode 100644 index 0000000..44d7359 --- /dev/null +++ b/solvitaire/src/test/resources/fortunes_favor/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2C","2D","3C"], + ["2H","3D","3H"], + ["2S","4D","4H"] + ], + "stock": ["4C","3S","4S"] +} diff --git a/solvitaire/src/test/resources/free_cell/ComplexSolvable.json b/solvitaire/src/test/resources/free_cell/ComplexSolvable.json new file mode 100644 index 0000000..ca370ff --- /dev/null +++ b/solvitaire/src/test/resources/free_cell/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AC","2C","4C","3C"], + ["AD","2D","4D","3D"], + ["AH","2H","3H","4H"], + ["AS","2S","3S","4S"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/free_cell/ComplexUnsolvable.json b/solvitaire/src/test/resources/free_cell/ComplexUnsolvable.json new file mode 100644 index 0000000..48ac428 --- /dev/null +++ b/solvitaire/src/test/resources/free_cell/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2S","2C","4D","3C"], + ["2H","2D","4C","3D"], + ["AH","AD","3S","4H"], + ["AS","AC","3H","4S"] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/free_cell/SimpleSolvable.json b/solvitaire/src/test/resources/free_cell/SimpleSolvable.json new file mode 100644 index 0000000..1bebaf2 --- /dev/null +++ b/solvitaire/src/test/resources/free_cell/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["4C","3C","2C","AC"], + ["4D","3D","2D","AD"], + ["4H","3H","2H","AH"], + ["4S","3S","2S","AS"] + ] +} diff --git a/solvitaire/src/test/resources/free_cell/SimpleUnsolvable.json b/solvitaire/src/test/resources/free_cell/SimpleUnsolvable.json new file mode 100644 index 0000000..8cdcc26 --- /dev/null +++ b/solvitaire/src/test/resources/free_cell/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AC","2C","3C","4C"], + ["AD","2D","3D","4D"], + ["AH","2H","3H","4H"], + ["AS","2S","3S","4S"] + ] +} diff --git a/solvitaire/src/test/resources/gaps/ComplexSolvable.json b/solvitaire/src/test/resources/gaps/ComplexSolvable.json new file mode 100644 index 0000000..f9fcb41 --- /dev/null +++ b/solvitaire/src/test/resources/gaps/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "sequences": [ + ["4C","2S","2C",""], + ["2D","3S","4D",""], + ["4S","3C","3D",""], + ["2H","3H","4H",""] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/gaps/ComplexUnsolvable.json b/solvitaire/src/test/resources/gaps/ComplexUnsolvable.json new file mode 100644 index 0000000..2c9de3e --- /dev/null +++ b/solvitaire/src/test/resources/gaps/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "sequences": [ + ["2C","4S","4C",""], + ["3S","2D","4D",""], + ["2S","3C","3D",""], + ["2H","3H","4H",""] + ] +} diff --git a/solvitaire/src/test/resources/gaps/SimpleSolvable.json b/solvitaire/src/test/resources/gaps/SimpleSolvable.json new file mode 100644 index 0000000..2e9cdfe --- /dev/null +++ b/solvitaire/src/test/resources/gaps/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "sequences": [ + ["2C","4S","4C",""], + ["2D","3S","4D",""], + ["2S","3C","3D",""], + ["2H","3H","4H",""] + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/gaps/SimpleUnsolvable.json b/solvitaire/src/test/resources/gaps/SimpleUnsolvable.json new file mode 100644 index 0000000..b0c0529 --- /dev/null +++ b/solvitaire/src/test/resources/gaps/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "sequences": [ + ["2C","3C","4C",""], + ["3S","2D","4D",""], + ["2S","4S","3D",""], + ["2H","3H","4H",""] + ] +} diff --git a/solvitaire/src/test/resources/klondike/ComplexSolvable.json b/solvitaire/src/test/resources/klondike/ComplexSolvable.json new file mode 100644 index 0000000..8711780 --- /dev/null +++ b/solvitaire/src/test/resources/klondike/ComplexSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","4c","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","AS","AC","2D","4H","4D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/klondike/ComplexUnsolvable.json b/solvitaire/src/test/resources/klondike/ComplexUnsolvable.json new file mode 100644 index 0000000..7685ce1 --- /dev/null +++ b/solvitaire/src/test/resources/klondike/ComplexUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","4c","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","4D","AC","2D","4H","AS"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/klondike/SimpleSolvable.json b/solvitaire/src/test/resources/klondike/SimpleSolvable.json new file mode 100644 index 0000000..f54ebbb --- /dev/null +++ b/solvitaire/src/test/resources/klondike/SimpleSolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["3d","2c","Ac","2H"], + ["4h","3h","2S"], + ["4d","3C"], + ["AS"] + ], + "stock": ["3S","4S","4C","2D","AH","AD"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/klondike/SimpleUnsolvable.json b/solvitaire/src/test/resources/klondike/SimpleUnsolvable.json new file mode 100644 index 0000000..21a5854 --- /dev/null +++ b/solvitaire/src/test/resources/klondike/SimpleUnsolvable.json @@ -0,0 +1,9 @@ +{ + "tableau piles": [ + ["As","2c","2d","2H"], + ["Ah","3h","2S"], + ["Ad","3C"], + ["4S"] + ], + "stock": ["3S","4D","AC","4C","4H","3D"] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/somerset/ComplexSolvable.json b/solvitaire/src/test/resources/somerset/ComplexSolvable.json new file mode 100644 index 0000000..b316c50 --- /dev/null +++ b/solvitaire/src/test/resources/somerset/ComplexSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2S","2C","AC"], + ["2D","AD"], + ["2H","AH"], + ["AS"] + ] +} diff --git a/solvitaire/src/test/resources/somerset/ComplexUnsolvable.json b/solvitaire/src/test/resources/somerset/ComplexUnsolvable.json new file mode 100644 index 0000000..09d7864 --- /dev/null +++ b/solvitaire/src/test/resources/somerset/ComplexUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AS","AC","2C"], + ["AD","2D"], + ["AH","2H"], + ["2S"] + ] +} diff --git a/solvitaire/src/test/resources/somerset/SimpleSolvable.json b/solvitaire/src/test/resources/somerset/SimpleSolvable.json new file mode 100644 index 0000000..b316c50 --- /dev/null +++ b/solvitaire/src/test/resources/somerset/SimpleSolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["2S","2C","AC"], + ["2D","AD"], + ["2H","AH"], + ["AS"] + ] +} diff --git a/solvitaire/src/test/resources/somerset/SimpleUnsolvable.json b/solvitaire/src/test/resources/somerset/SimpleUnsolvable.json new file mode 100644 index 0000000..09d7864 --- /dev/null +++ b/solvitaire/src/test/resources/somerset/SimpleUnsolvable.json @@ -0,0 +1,8 @@ +{ + "tableau piles": [ + ["AS","AC","2C"], + ["AD","2D"], + ["AH","2H"], + ["2S"] + ] +} diff --git a/solvitaire/src/test/resources/spanish_patience/ComplexSolvable.json b/solvitaire/src/test/resources/spanish_patience/ComplexSolvable.json new file mode 100644 index 0000000..c48ce47 --- /dev/null +++ b/solvitaire/src/test/resources/spanish_patience/ComplexSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["2C", "AC"] + ] +} diff --git a/solvitaire/src/test/resources/spanish_patience/ComplexUnsolvable.json b/solvitaire/src/test/resources/spanish_patience/ComplexUnsolvable.json new file mode 100644 index 0000000..ba9be59 --- /dev/null +++ b/solvitaire/src/test/resources/spanish_patience/ComplexUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "AH"], + ["AD", "2D", "2H"], + ["AC", "2C"] + ] +} diff --git a/solvitaire/src/test/resources/spanish_patience/SimpleSolvable.json b/solvitaire/src/test/resources/spanish_patience/SimpleSolvable.json new file mode 100644 index 0000000..eacad0e --- /dev/null +++ b/solvitaire/src/test/resources/spanish_patience/SimpleSolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["2S", "AS", "AD"], + ["2D", "2H", "AH"], + ["2C", "AC"] + ] +} diff --git a/solvitaire/src/test/resources/spanish_patience/SimpleUnsolvable.json b/solvitaire/src/test/resources/spanish_patience/SimpleUnsolvable.json new file mode 100644 index 0000000..99a3140 --- /dev/null +++ b/solvitaire/src/test/resources/spanish_patience/SimpleUnsolvable.json @@ -0,0 +1,7 @@ +{ + "tableau piles": [ + ["AS", "2S", "2D"], + ["AD", "AH", "2H"], + ["AC", "2C"] + ] +} diff --git a/solvitaire/src/test/resources/spider/ComplexSolvable.json b/solvitaire/src/test/resources/spider/ComplexSolvable.json new file mode 100644 index 0000000..c7d96e2 --- /dev/null +++ b/solvitaire/src/test/resources/spider/ComplexSolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["2D","AD","3D"], + ["AC","2C","3C"], + ["AS","2S","3S"], + ["3H","2H","AH"] + ], + "stock": [ + "2C","3D","3H","3S", + "AC","AD","AH","AS", + "3C","2D","2H","2S" + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/spider/ComplexUnsolvable.json b/solvitaire/src/test/resources/spider/ComplexUnsolvable.json new file mode 100644 index 0000000..2379c9a --- /dev/null +++ b/solvitaire/src/test/resources/spider/ComplexUnsolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["2D","AD","3D"], + ["AC","2C","3C"], + ["AS","2S","3S"], + ["3H","2H","AH"] + ], + "stock": [ + "3C","3D","3H","3S", + "2C","2D","AH","AS", + "AC","AD","2H","2S" + ] +}
\ No newline at end of file diff --git a/solvitaire/src/test/resources/spider/SimpleSolvable.json b/solvitaire/src/test/resources/spider/SimpleSolvable.json new file mode 100644 index 0000000..f0b9b00 --- /dev/null +++ b/solvitaire/src/test/resources/spider/SimpleSolvable.json @@ -0,0 +1,14 @@ +{ + "tableau piles": [ + ["3C","2C","AC"], + ["3D","2D","AD"], + ["3H","2H","AH"], + ["3S","2S","AS"] + ], + "stock": [ + "3C","2C","AC", + "3D","2D","AD", + "3H","2H","AH", + "3S","2S","AS" + ] +} diff --git a/solvitaire/src/test/resources/spider/SimpleUnsolvable.json b/solvitaire/src/test/resources/spider/SimpleUnsolvable.json new file mode 100644 index 0000000..d11a6dd --- /dev/null +++ b/solvitaire/src/test/resources/spider/SimpleUnsolvable.json @@ -0,0 +1,13 @@ +{ + "tableau piles": [ + ["AC","AC","2C"], + ["AD","AD","2D"], + ["AH","AH","2H"], + ["AS","AS","2S"] + ], + "stock": [ + "3C","3D","3H","3S", + "2C","2D","2H","2S", + "3C","3D","3H","3S" + ] +} diff --git a/solvitaire/src/test/test_helper.cpp b/solvitaire/src/test/test_helper.cpp new file mode 100644 index 0000000..6cc68aa --- /dev/null +++ b/solvitaire/src/test/test_helper.cpp @@ -0,0 +1,148 @@ +// +// Created by thecharlesblake on 12/26/17. +// + +#include <gtest/gtest.h> + +#include "test_helper.h" +#include "../main/game/search-state/game_state.h" +#include "../main/solver/solver.h" +#include "../main/input-output/input/json-parsing/json_helper.h" +#include "../main/input-output/input/json-parsing/rules_parser.h" +#include "../../lib/rapidjson/document.h" + +#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f)) + +using namespace rapidjson; +using std::vector; +using std::ostream; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; +typedef game_state::streamliner_options sos; + + +bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { + const Document in_doc = json_helper::get_file_json(input_file); + const sol_rules rules = rules_parser::from_preset(preset_type); + + game_state gs(rules, in_doc, sos::NONE); + solver sol(gs, 1000000); + + return sol.run().sol_type == solver::result::type::SOLVED; +} + +void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector<card> 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, string_il{ + {}, + {}, + {}, + {}, + {"4C","3C","2C","AC"}, + {"4H","3H","2H","AH"}, + {"4S","3S","2S","AS"}, + {"4D","3D","2D","AD"} + }); + + solver sol(gs, 1000000); + sol.run(); + + auto frontier = sol.get_frontier(); + auto i = std::begin(frontier); + + for (card c : cards) { + i++; + ASSERT_TRUE(i->mv.type == move::mtype::dominance); + ASSERT_TRUE(gs.get_data()[i->mv.from].top_card() == c); + ASSERT_TRUE(i->mv.to >= 0 && i->mv.to <= 4); + gs.make_move(i->mv); + } + ASSERT_TRUE(i->child_moves.empty()); +} + +void test_helper::expected_moves_test(sol_rules sr, + string_il piles, + vector<move> exp_moves) { + game_state gs(sr, piles); + vector<move> actual_moves = gs.get_legal_moves(); + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +bool test_helper::moves_eq(vector<move>& exp_moves, vector<move>& 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; +} + +void test_helper::k_plus_stock_test + ( sol_rules sr + , string_il piles + , vector<move> exp_moves + , move m + , pile::size_type stock_size + , pile::size_type waste_size + , card top_stock + , card top_waste + ) { + + game_state gs(sr, piles); + vector<move> actual_moves = gs.get_legal_moves(); + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; + + gs.make_move(m); + + ASSERT_EQ(gs.get_data()[gs.stock].size(), stock_size) << gs.get_data()[gs.stock].size(); + ASSERT_EQ(gs.get_data()[gs.waste].size(), waste_size) << gs.get_data()[gs.waste].size(); + + if (top_stock == card()) + ASSERT_TRUE(gs.get_data()[gs.stock].empty()); + else + ASSERT_EQ(gs.get_data()[gs.stock].top_card(), top_stock ) << gs.get_data()[gs.stock].top_card().to_string(); + + if (top_waste == card()) + ASSERT_TRUE(gs.get_data()[gs.waste].empty()); + else + ASSERT_EQ(gs.get_data()[gs.waste].top_card(), top_waste ) << gs.get_data()[gs.waste].top_card().to_string(); + + gs.undo_move(m); + + ASSERT_EQ(gs.get_data(), game_state(sr, piles).get_data()) << gs; +} + +uint8_t test_helper::cards_in_founds(game_state& gs) { + uint8_t sz = 0; + for (auto found : gs.foundations) { + sz += gs.piles[found].size(); + } + return sz; +} + +ostream& operator <<(ostream& os, const move& m) { + return os << "move:(" << int(m.from) + << "," << int(m.to) + << "," << int(m.count) + << "," << m.reveal_move + << ")"; +} + +ostream& operator <<(ostream& os, const vector<move>& moves) { + for (const move m : moves) { + os << m << ",\n"; + } + return os; +} diff --git a/solvitaire/src/test/test_helper.h b/solvitaire/src/test/test_helper.h new file mode 100644 index 0000000..89d28cb --- /dev/null +++ b/solvitaire/src/test/test_helper.h @@ -0,0 +1,41 @@ +// +// Created by thecharlesblake on 12/26/17. +// + +#ifndef SOLVITAIRE_TEST_HELPER_H +#define SOLVITAIRE_TEST_HELPER_H + +#include <string> + +#include "../main/game/sol_rules.h" +#include "../main/game/card.h" +#include "../main/game/pile.h" +#include "../main/game/move.h" +#include "../main/game/search-state/game_state.h" + +class test_helper { +public: + static bool is_solvable(const std::string&, const std::string&); + static void run_foundations_dominance_test(sol_rules::build_policy policy, + std::vector<card> cards); + static void expected_moves_test(sol_rules sr, std::initializer_list<std::initializer_list<std::string>>, + std::vector<move>); + static void k_plus_stock_test + ( sol_rules sr + , std::initializer_list<std::initializer_list<std::string>> + , std::vector<move> + , move + , pile::size_type + , pile::size_type + , card + , card + ); + static bool moves_eq(std::vector<move>&, std::vector<move>&); + static uint8_t cards_in_founds(game_state&); +}; + +std::ostream& operator <<(std::ostream&, const move&); +std::ostream& operator <<(std::ostream&, const std::vector<move>&); + + +#endif //SOLVITAIRE_TEST_HELPER_H diff --git a/solvitaire/src/test/unit_tests/built_group_move_gen_test.cpp b/solvitaire/src/test/unit_tests/built_group_move_gen_test.cpp new file mode 100644 index 0000000..5e3a492 --- /dev/null +++ b/solvitaire/src/test/unit_tests/built_group_move_gen_test.cpp @@ -0,0 +1,313 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::built_group_type bgt; + +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 = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {} + }, + { + // Regular moves + move(move::mtype::regular, 1, 0, 1), + move(move::mtype::regular, 2, 0, 1), + move(move::mtype::regular, 3, 0, 1), + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + + // Built group moves + move(move::mtype::built_group, 1, 0, 2), + move(move::mtype::built_group, 1, 0, 3), + move(move::mtype::built_group, 1, 4, 2), + move(move::mtype::built_group, 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 = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {} + }, + { + // Regular moves + move(move::mtype::regular, 1, 0, 1), + move(move::mtype::regular, 2, 0, 1), + move(move::mtype::regular, 3, 0, 1), + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + + // Built group moves + move(move::mtype::built_group, 2, 0, 2), + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 2, 4, 2), + move(move::mtype::built_group, 2, 4, 3), + + move(move::mtype::built_group, 3, 0, 2), + move(move::mtype::built_group, 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 = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {} + }, + { + // Regular moves + move(move::mtype::regular, 1, 0, 1), + move(move::mtype::regular, 2, 0, 1), + move(move::mtype::regular, 3, 0, 1), + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + + // Built group moves + move(move::mtype::built_group, 1, 0, 2), + move(move::mtype::built_group, 1, 0, 3), + move(move::mtype::built_group, 1, 4, 2), + move(move::mtype::built_group, 1, 4, 3), + + move(move::mtype::built_group, 2, 0, 2), + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 2, 4, 2), + move(move::mtype::built_group, 2, 4, 3), + + move(move::mtype::built_group, 3, 0, 2), + move(move::mtype::built_group, 3, 0, 3), + move(move::mtype::built_group, 3, 4, 2), + move(move::mtype::built_group, 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 = bgt::NO; + + test_helper::expected_moves_test( + sr, + { + {}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {} + }, + { + // Regular moves + move(move::mtype::regular, 1, 0, 1), + move(move::mtype::regular, 2, 0, 1), + move(move::mtype::regular, 3, 0, 1), + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1) + } + ); +} + +TEST(BuiltGroupMoveGen, SameSuitOccupied) { + sol_rules sr; + sr.tableau_pile_count = 6; + sr.build_pol = pol::SAME_SUIT; + sr.built_group_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {"4C"}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {}, + {"3C"} + }, + { + // Regular moves + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + move(move::mtype::regular, 5, 0, 1), + + // Built group moves + move(move::mtype::built_group, 1, 0, 3), + move(move::mtype::built_group, 1, 4, 2), + move(move::mtype::built_group, 1, 4, 3), + move(move::mtype::built_group, 1, 5, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, RedBlackOccupied) { + sol_rules sr; + sr.tableau_pile_count = 6; + sr.build_pol = pol::RED_BLACK; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {"4C"}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {}, + {"3C"} + }, + { + // Regular moves + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + + // Built group moves + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 2, 4, 2), + move(move::mtype::built_group, 2, 4, 3), + move(move::mtype::built_group, 3, 4, 2), + move(move::mtype::built_group, 3, 5, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, AnySuitOccupied) { + sol_rules sr; + sr.tableau_pile_count = 6; + sr.build_pol = pol::ANY_SUIT; + sr.built_group_pol = pol::ANY_SUIT; + sr.move_built_group = bgt::YES; + + test_helper::expected_moves_test( + sr, + { + {"4C"}, + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit + {}, + {"3C"} + }, + { + // Regular moves + move(move::mtype::regular, 1, 4, 1), + move(move::mtype::regular, 2, 4, 1), + move(move::mtype::regular, 3, 4, 1), + move(move::mtype::regular, 5, 0, 1), + + // Built group moves + move(move::mtype::built_group, 1, 0, 3), + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 3, 0, 3), + + move(move::mtype::built_group, 1, 4, 2), + move(move::mtype::built_group, 1, 4, 3), + + move(move::mtype::built_group, 2, 4, 2), + move(move::mtype::built_group, 2, 4, 3), + + move(move::mtype::built_group, 3, 4, 2), + move(move::mtype::built_group, 3, 4, 3), + + move(move::mtype::built_group, 1, 5, 2), + move(move::mtype::built_group, 2, 5, 2), + move(move::mtype::built_group, 3, 5, 2) + } + ); +} + +TEST(BuiltGroupMoveGen, KingsOnly) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.build_pol = pol::SAME_SUIT; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = bgt::YES; + sr.spaces_pol = s_pol::KINGS; + + test_helper::expected_moves_test( + sr, + { + {}, + {"KC", "QC", "JC"}, // Same suit + {"KH", "QS", "JH"}, // Red black + {"KD", "QD", "JS"}, // Any suit + {} + }, + { + // Built group moves + move(move::mtype::built_group, 2, 0, 3), + move(move::mtype::built_group, 2, 4, 3) + } + ); +} + +TEST(BuiltGroupMoveGen, NoBuild) { + sol_rules sr; + sr.tableau_pile_count = 5; + sr.build_pol = pol::SAME_SUIT; + sr.built_group_pol = pol::RED_BLACK; + sr.move_built_group = bgt::YES; + sr.spaces_pol = s_pol::NO_BUILD; + + test_helper::expected_moves_test( + sr, + { + {}, + {"KC", "QC", "JC"}, // Same suit + {"KH", "QS", "JH"}, // Red black + {"KD", "QD", "JS"}, // Any suit + {} + }, + {} + ); +} diff --git a/solvitaire/src/test/unit_tests/card_test.cpp b/solvitaire/src/test/unit_tests/card_test.cpp new file mode 100644 index 0000000..346dff9 --- /dev/null +++ b/solvitaire/src/test/unit_tests/card_test.cpp @@ -0,0 +1,92 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/card.h" + +TEST(Card, Rank) { + for (card::rank_t r = 1; r <= 13; r++) { + card::suit_t s = card::suit::Clubs; + card c(s, r); + ASSERT_EQ(r, c.get_rank()); + } +} + +TEST(Card, Suit) { + for (card::suit_t s : { + card::suit::Clubs, + card::suit::Hearts, + card::suit::Spades, + card::suit::Diamonds + }) { + card::rank_t r = 1; + card c(s, r); + ASSERT_EQ(s, c.get_suit()); + } +} + +TEST(Card, Colour) { + card::rank_t r = 1; + + for (card::suit_t s : { + card::suit::Clubs, + card::suit::Spades + }) { + card c(s, r); + ASSERT_EQ(card::colour::Black, c.get_colour()); + } + + for (card::suit_t s : { + card::suit::Hearts, + card::suit::Diamonds + }) { + card c(s, r); + ASSERT_EQ(card::colour::Red, c.get_colour()); + } +} + +TEST(Card, EqOps) { + for (card::rank_t r = 1; r <= 13; r++) { + for (card::suit_t s : { + card::suit::Clubs, + card::suit::Hearts, + card::suit::Spades, + card::suit::Diamonds + }) { + card a(s, r); + card b(s, r); + ASSERT_TRUE (a == b); + ASSERT_FALSE(a != b); + } + } +} + +TEST(Card, RelativeOps) { + for (card::rank_t r = 1; r <= 12; r++) { + for (card::suit_t s : { + card::suit::Clubs, + card::suit::Hearts, + card::suit::Spades, + card::suit::Diamonds + }) { + card a(s, r); + card b(s, card::rank_t(r+1)); + ASSERT_TRUE (a < b); + ASSERT_TRUE (a <= b); + ASSERT_FALSE(a > b); + ASSERT_FALSE(a >= b); + } + } +} + +TEST(Card, StringConstructor) { + ASSERT_EQ(card(card::suit::Spades , card::rank_t(1) ), card("AS" )); + ASSERT_EQ(card(card::suit::Clubs , card::rank_t(1) ), card("Ac" )); + ASSERT_EQ(card(card::suit::Diamonds, card::rank_t(5) ), card("5D" )); + ASSERT_EQ(card(card::suit::Hearts , card::rank_t(11)), card("11H")); + ASSERT_EQ(card(card::suit::Spades , card::rank_t(11)), card("JS" )); + ASSERT_EQ(card(card::suit::Clubs , card::rank_t(13)), card("kc" )); +} diff --git a/solvitaire/src/test/unit_tests/deal_parser_test.cpp b/solvitaire/src/test/unit_tests/deal_parser_test.cpp new file mode 100644 index 0000000..db2bd31 --- /dev/null +++ b/solvitaire/src/test/unit_tests/deal_parser_test.cpp @@ -0,0 +1,143 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../../../lib/rapidjson/stringbuffer.h" +#include "../../../lib/rapidjson/document.h" + +#include "../../main/input-output/input/json-parsing/deal_parser.h" +#include "../../main/input-output/input/json-parsing/json_helper.h" + +using namespace std; +using namespace rapidjson; + +void run_deal_parser_test(bool, const char*); + +TEST(DealParser, SimpleTableauLetter) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["AS"] + ] +} +)"); +} + +TEST(DealParser, SimpleTableauNumber) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["1S"] + ] +} +)"); +} + +TEST(DealParser, IllegalTableauNumber) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["14S"] + ] +} +)"); +} + +TEST(DealParser, IllegalTableauSuit) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["14A"] + ] +} +)"); +} + +TEST(DealParser, SimpleTableauCase) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["kc"] + ] +} +)"); +} + +TEST(DealParser, AdvancedTableau) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C","3C","2S","2c"], + ["5D","qD","3S","1d","2D"], + ["5H","4S","JH","3h","2H"] + ] +} +)"); +} + +TEST(DealParser, Hole) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "hole": "qD" +} +)"); +} + +TEST(DealParser, Stock) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "stock": ["1S","JC","7H"] +} +)"); +} + +TEST(DealParser, Waste) { + run_deal_parser_test(true, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "waste": ["1S","JC","7H"] +} +)"); +} + +TEST(DealParser, IllegalPileType) { + run_deal_parser_test(false, R"( +{ + "tableau piles": [ + ["5S","13C","4C"] + ], + "other": ["1S","JC","7H"] +} +)"); +} + +void run_deal_parser_test(bool validity, const char* json) { + Document in_doc; + in_doc.Parse(json); + if (in_doc.HasParseError()) { + throw runtime_error("invalid json"); + } + + Document val_doc; + val_doc.Parse(deal_parser::deal_schema_json().c_str()); + SchemaDocument sd(val_doc); + SchemaValidator validator(sd); + bool acc = in_doc.Accept(validator); + if (acc) { + ASSERT_TRUE(validity == acc) << "JSON accepted when it should not have " + "been:\n" << json; + } else { + ASSERT_TRUE(validity == acc) << json_helper::schema_err_str(validator) + << "\n" << json; + } +} diff --git a/solvitaire/src/test/unit_tests/face_up_cards_test.cpp b/solvitaire/src/test/unit_tests/face_up_cards_test.cpp new file mode 100644 index 0000000..1311958 --- /dev/null +++ b/solvitaire/src/test/unit_tests/face_up_cards_test.cpp @@ -0,0 +1,317 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" +#include "../../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::face_up_policy fu; +typedef move::mtype mt; +typedef sol_rules::built_group_type bgt; + +using std::vector; +using std::ostream; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + +#define ASSERT_FACE_UP(card) ASSERT_FALSE(card.is_face_down()) << card.to_string() << " should be face up" +#define ASSERT_FACE_DOWN(card) ASSERT_TRUE(card.is_face_down()) << card.to_string() << " should be face down" + + +TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + game_state gs(sr, piles); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_DOWN(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, IgnoreCapsIfFaceUpAll) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::ALL; + + string_il piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + game_state gs(sr, piles); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, MoveTurnsFaceUp) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"4S"}, + {"3s","AS"}, // from + {"2S"} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 1, 2, 1, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2, 1, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[2][0]); +} + +TEST(FaceUpCards, BuiltGroupMoveTurnsFaceUp) { + sol_rules sr; + sr.tableau_pile_count = 2; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3s","2S","AS"}, // from + {} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 1, 1, false), + // Built-group moves + move(move::mtype::built_group, 0, 1, 2, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::built_group, 0, 1, 2, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"4S"}, + {"3s","AS"}, // from + {"2S"} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 1, 2, 1, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2, 1, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[2][0]); +} + +TEST(FaceUpCards, FaceDownNoBuiltGroup) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"4S"}, + {"3s","AS"}, + {"2S"} + }, + { + // Regular moves + move(move::mtype::regular, 1, 2, 1, true) + } + ); +} + +TEST(FaceUpCards, FaceUpBuiltGroup) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"3S"}, + {"2S","AS"}, + {} + }, + { + // Regular moves + move(mt::regular, 1, 2, 1), + + // Built group moves + move(mt::built_group, 1, 0, 2), + move(mt::built_group, 1, 2, 2) + } + ); +} + +TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"4S"}, + {"3s","2S","AS"}, + {"3S"} + }, + { + // Regular moves + move(mt::regular, 2, 0, 1, false), + + // Built group moves + move(mt::built_group, 1, 2, 2, true) + } + ); +} + +TEST(FaceUpCards, UndoTurnsBackFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::regular, 1, 2, 1, true); + gs.make_move(m); + gs.undo_move(m); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_DOWN(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"4S"}, + {"3s","2S","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::built_group, 1, 2, 2, true); + gs.make_move(m); + gs.undo_move(m); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); + ASSERT_FACE_DOWN(gs.get_data()[1][2]); +} + +TEST(FaceUpCards, CacheEncodesFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::RED_BLACK; + sr.build_pol = pol::RED_BLACK; + sr.move_built_group = bgt::YES; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"2C"}, + {"2s","AH"}, + {} + }; + + game_state gs(sr, piles); + lru_cache cache(gs, 1000); + bool new_state; + + gs.make_move(move(mt::regular, 1, 2, 1, true)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to empty pile"; + + gs.make_move(move(mt::regular, 2, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH back to face-up 2S"; + + gs.make_move(move(mt::regular, 1, 0, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to 2C"; + + gs.make_move(move(mt::regular, 0, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_FALSE(new_state) << "AH to 2S"; +} diff --git a/solvitaire/src/test/unit_tests/foundations_dominance_test.cpp b/solvitaire/src/test/unit_tests/foundations_dominance_test.cpp new file mode 100644 index 0000000..8fdc285 --- /dev/null +++ b/solvitaire/src/test/unit_tests/foundations_dominance_test.cpp @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/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"}); +}
\ No newline at end of file diff --git a/solvitaire/src/test/unit_tests/global_cache_test.cpp b/solvitaire/src/test/unit_tests/global_cache_test.cpp new file mode 100644 index 0000000..93ef105 --- /dev/null +++ b/solvitaire/src/test/unit_tests/global_cache_test.cpp @@ -0,0 +1,78 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + +TEST(GlobalCache, CommutativeTableauPiles) { + sol_rules rules; + rules.tableau_pile_count = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, string_il{{},{},{}}); + lru_cache cache(gs, 1000); + + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); + + // Test with piles of size > 1 + cache.clear(); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); +} + +TEST(GlobalCache, CommutativeReserve) { + sol_rules rules; + rules.reserve_size = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, string_il{{},{},{}}); + lru_cache cache(gs, 1000); + + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); + + // Test with piles of size > 1 + cache.clear(); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); +} + +TEST(GlobalCache, CommutativeCells) { + sol_rules rules; + rules.cells = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, string_il{{},{},{}}); + lru_cache cache(gs, 1000); + + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); +} diff --git a/solvitaire/src/test/unit_tests/k_plus_stock_test.cpp b/solvitaire/src/test/unit_tests/k_plus_stock_test.cpp new file mode 100644 index 0000000..dcd4410 --- /dev/null +++ b/solvitaire/src/test/unit_tests/k_plus_stock_test.cpp @@ -0,0 +1,426 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; + +using std::vector; +using std::ostream; +using std::initializer_list; + + +TEST(KPlusStock, NoRedealExact) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 9), + }, + move(move::mtype::stock_k_plus, 0, 3, 9), + 0, 8, + card(), "2D" + ); +} + +TEST(KPlusStock, NoRedealOneExtra) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3), + move(move::mtype::stock_k_plus, 0, 3, 7), + }, + move(move::mtype::stock_k_plus, 0, 3, 7), + 0, 6, + card(), "AD" + ); +} + +TEST(KPlusStock, NoRedealTwoExtra) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = false; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2H","AD","2D","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3) + }, + move(move::mtype::stock_k_plus, 0, 4, 3), + 5, 2, + "3C", "AH" + ); +} + +TEST(KPlusStock, RedealExact) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), + }, + move(move::mtype::stock_k_plus, 0, 3, 9, false, true), + 8, 0, + "AS", card() + ); +} + +TEST(KPlusStock, RedealOneExtra) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","2S","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), + }, + move(move::mtype::stock_k_plus, 0, 3, 7, false, true), + 6, 0, + "AS", card() + ); +} + +TEST(KPlusStock, RedealTwoExtra) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2H","AD","2D","3C","AC","AH","AS"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 4, 3, false, false) + }, + move(move::mtype::stock_k_plus, 0, 4, 3, false, false), + 5, 2, + "3C", "AH" + ); +} + +TEST(KPlusStock, RedealKPlus3Waste) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C"}, + {"AS","AH","AC"}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) + }, + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + 8, 0, + "AS", card() + ); +} + +TEST(KPlusStock, RedealKPlus4Waste) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","AD","3S","2D","2S"}, + {"AS","AH","AC","3C"}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 3, 5, false, true), + move(move::mtype::stock_k_plus, 0, 4, -1, false, false), + move(move::mtype::stock_k_plus, 0, 2, 2, false, false) + }, + move(move::mtype::stock_k_plus, 0, 2, 2, false, false), + 3, 5, + "3S", "2S" + ); +} + +TEST(KPlusStock, RedealKPlus5Waste) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 10; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2S","2D","2H","AD","2S"}, + {"AS","AH","AC","3C","4S"}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 3, 3, false, false), + move(move::mtype::stock_k_plus, 0, 4, -2, false, false), + move(move::mtype::stock_k_plus, 0, 2, 4, false, false) + }, + move(move::mtype::stock_k_plus, 0, 3, 3, false, false), + 2, 7, + "2D", "AD" + ); +} + +TEST(KPlusStock, RedealKPlusEmptyStock) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2D","3S","AD","2S","3C"}, + {"AS","AH","AC"}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + move(move::mtype::stock_k_plus, 0, 4, 0, false, false) + }, + move(move::mtype::stock_k_plus, 0, 3, 6, false, true), + 8, 0, + "AS", card() + ); +} + +TEST(KPlusStock, NoRedealCellFoundationMoves) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = false; + sr.stock_deal_count = 3; + sr.cells = 2; + sr.foundations_present = true; + + test_helper::k_plus_stock_test( + sr, + { + {"AC"}, {}, {}, {}, + {"2D"}, {}, + {"2H","AS","2C","AH","2S","3C"}, + {"3S"} + }, + { + // Cell moves + move(move::mtype::stock_k_plus, 6, 5, 3), + move(move::mtype::stock_k_plus, 6, 5, 6), + + // Foundation moves + move(move::mtype::stock_k_plus, 6, 1, 3), + + // Regular waste move + move(move::mtype::stock_k_plus, 6, 5, 0) + }, + move(move::mtype::stock_k_plus, 6, 1, 3), + 3, 3, + "2C", "2S" + ); +} + +TEST(KPlusStock, RedealCellFoundationMoves) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 3; + sr.cells = 2; + sr.foundations_present = true; + + test_helper::k_plus_stock_test( + sr, + { + {"AC"}, {}, {}, {}, + {"2D"}, {}, + {"2H","AS","2C","AH","2S","3C"}, + {"3S"} + }, + { + // Cell moves + move(move::mtype::stock_k_plus, 6, 5, 3, false, false), + move(move::mtype::stock_k_plus, 6, 5, 6, false, true), + move(move::mtype::stock_k_plus, 6, 5, 2, false, false), + move(move::mtype::stock_k_plus, 6, 5, 5, false, false), + + // Foundation moves + move(move::mtype::stock_k_plus, 6, 1, 3, false, false), + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), + + // Regular waste move + move(move::mtype::stock_k_plus, 6, 5, 0) + }, + move(move::mtype::stock_k_plus, 6, 2, 5, false, false), + 1, 5, + "2H", "2C" + ); +} + +TEST(KPlusStock, RedealCount1BottomStock) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 3, 4, false, true), + 3, 0, + "2D", card() + ); +} + +TEST(KPlusStock, RedealCount1TopStock) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.build_pol = pol::SAME_SUIT; + sr.stock_size = 9; + sr.stock_deal_t = sdt::WASTE; + sr.stock_redeal = true; + sr.stock_deal_count = 1; + + test_helper::k_plus_stock_test( + sr, + { + {"2H","2S","AD","2D"}, + {}, + {"3D"}, + {"3H"}, + {"2C"} + }, + { + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + move(move::mtype::stock_k_plus, 0, 3, 4, false, true) + }, + move(move::mtype::stock_k_plus, 0, 2, 1, false, false), + 3, 0, + "AD", card() + ); +} diff --git a/solvitaire/src/test/unit_tests/legal_move_gen_test.cpp b/solvitaire/src/test/unit_tests/legal_move_gen_test.cpp new file mode 100644 index 0000000..da5c63c --- /dev/null +++ b/solvitaire/src/test/unit_tests/legal_move_gen_test.cpp @@ -0,0 +1,326 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" +#include "../../main/game/move.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::built_group_type bgt; + +using std::vector; +using std::ostream; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + + +TEST(LegalMoveGen, BuildPolAnySuit) { + sol_rules sr; + sr.build_pol = pol::ANY_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, string_il{ + {"2C"}, + {"2H"}, + {"2D"}, + {"2S"}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 4,0), + move(move::mtype::regular, 4,1), + move(move::mtype::regular, 4,2), + move(move::mtype::regular, 4,3) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolRedBlack) { + sol_rules sr; + sr.build_pol = pol::RED_BLACK; + sr.tableau_pile_count = 5; + + game_state gs(sr, string_il{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 4,1), + move(move::mtype::regular, 4,3) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolSameSuit) { + sol_rules sr; + sr.build_pol = pol::SAME_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, string_il{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 4,0) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, BuildPolNoBuild) { + sol_rules sr; + sr.build_pol = pol::NO_BUILD; + sr.tableau_pile_count = 5; + + game_state gs(sr, string_il{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, SpacesPolAny) { + sol_rules sr; + sr.spaces_pol = s_pol::ANY; + sr.tableau_pile_count = 2; + + game_state gs(sr, string_il{ + {}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + // move(move::mtype::regular, 1, 0) // Now forbidden as always pointless + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, SpacesPolKings) { + sol_rules sr; + sr.spaces_pol = s_pol::KINGS; + sr.tableau_pile_count = 3; + + game_state gs(sr, string_il{ + {}, + {"AC"}, + {"3S","KD"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 2, 0) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, SpacesPolNoBuild) { + sol_rules sr; + sr.spaces_pol = s_pol::NO_BUILD; + sr.tableau_pile_count = 2; + + game_state gs(sr, string_il{ + {}, + {"AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupTrue) { + sol_rules sr; + sr.move_built_group = bgt::YES; + sr.tableau_pile_count = 2; + + game_state gs(sr, string_il{ + {}, + {"2C", "AC"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 1, 0, 1), + move(move::mtype::built_group, 1, 0, 2) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupFalse) { + sol_rules sr; + sr.move_built_group = bgt::NO; + sr.tableau_pile_count = 2; + + game_state gs(sr, string_il{ + {}, + {"AC", "2C"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 1, 0, 1) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, Foundations) { + sol_rules sr; + sr.foundations_present = true; + sr.tableau_pile_count = 4; + + game_state gs(sr, string_il{ + {}, {}, {}, {}, + {"AC"}, + {"AH"}, + {"AS"}, + {"AD"}, + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 4, 0), + move(move::mtype::regular, 5, 1), + move(move::mtype::regular, 6, 2), + move(move::mtype::regular, 7, 3) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, FoundationsRemovable) { + sol_rules sr; + sr.foundations_present = true; + sr.foundations_removable = true; + sr.tableau_pile_count = 1; + + game_state gs(sr, string_il{ + {"AC","2C","3C"}, // Without a few cards here dominance logic blocks + {}, // removing from foundation + {}, + {}, + {} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 0, 4) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + + +TEST(LegalMoveGen, TwoDecksFoundations) { + sol_rules sr; + sr.two_decks = true; + sr.foundations_removable = true; + sr.foundations_present = true; + sr.tableau_pile_count = 4; + + game_state gs(sr, string_il{ + {"AC"}, {}, {}, {}, {}, {}, {"AS"}, {}, + {}, + {"AH"}, + {}, + {"AD"}, + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + // Aces up + move(move::mtype::regular, 9, 1), + move(move::mtype::regular, 9, 5), + move(move::mtype::regular, 11, 3), + move(move::mtype::regular, 11, 7), + + // Aces down + move(move::mtype::regular, 0, 8), + move(move::mtype::regular, 0, 10), + move(move::mtype::regular, 6, 8), + move(move::mtype::regular, 6, 10), + + // Aces across (forbidden now) + //move(move::mtype::regular, 9, 8), + //move(move::mtype::regular, 9, 10), + //move(move::mtype::regular, 11, 8), + //move(move::mtype::regular, 11, 10), + + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, Cells) { + sol_rules sr; + sr.cells = 2; + sr.tableau_pile_count = 1; + + game_state gs(sr, string_il{ + {"3D"}, {}, // The cells + {"4H"} + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::regular, 0, 2), + move(move::mtype::regular, 2, 1) + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, StockDealToTableau) { + sol_rules sr; + sr.foundations_present = true; + sr.tableau_pile_count = 3; + sr.stock_size = 2; + sr.stock_deal_t = sdt::TABLEAU_PILES; + + game_state gs(sr, string_il{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {"3H"}, {"5D"}, {"7C"} // The tableau piles + }); + vector<move> actual_moves = gs.get_legal_moves(); + + vector<move> exp_moves = { + move(move::mtype::stock_to_all_tableau, 0, 0, 2) // Special stock-to-tableau syntax + }; + + ASSERT_TRUE(test_helper::moves_eq(exp_moves, actual_moves)) << actual_moves; +} diff --git a/solvitaire/src/test/unit_tests/pile_test.cpp b/solvitaire/src/test/unit_tests/pile_test.cpp new file mode 100644 index 0000000..a5aeb66 --- /dev/null +++ b/solvitaire/src/test/unit_tests/pile_test.cpp @@ -0,0 +1,87 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/pile.h" + +TEST(Pile, Size) { + pile p = {}; + ASSERT_EQ(0, p.size()); + + p = {"AS"}; + ASSERT_EQ(1, p.size()); + + p = {"AS", "KC", "4H"}; + ASSERT_EQ(3, p.size()); +} + +TEST(Pile, TopCard) { + pile p = {"AS"}; + ASSERT_EQ(card("AS"), p.top_card()); + + p = {"AS", "KC", "4H"}; + ASSERT_NE(card("AS"), p.top_card()); + ASSERT_EQ(card("4H"), p.top_card()); +} + +TEST(Pile, Index) { + pile p = {"AS"}; + ASSERT_EQ(card("AS"), p[0]); + + p = {"AS", "KC", "4H"}; + ASSERT_EQ(card("AS"), p[2]); + ASSERT_EQ(card("KC"), p[1]); + ASSERT_EQ(card("4H"), p[0]); +} + +TEST(Pile, Place) { + pile t1 = {}; + pile t2 = {"AS"}; + pile t3 = {"AS", "KC", "4H"}; + + pile p = {}; + ASSERT_EQ(p, t1); + + p.place("AS"); + ASSERT_NE(p, t1); + ASSERT_EQ(p, t2); + + p.place("KC"); + p.place("4H"); + ASSERT_NE(p, t2); + ASSERT_EQ(p, t3); +} + +TEST(Pile, Take) { + pile t1 = {}; + pile t2 = {"AS"}; + pile t3 = {"AS", "KC", "4H"}; + + pile p = {"AS", "KC", "4H"}; + ASSERT_EQ(p, t3); + + p.take(); + p.take(); + ASSERT_NE(p, t3); + ASSERT_EQ(p, t2); + + p.take(); + ASSERT_NE(p, t2); + ASSERT_EQ(p, t1); +} + +TEST(Pile, CompOperators) { + pile p1 = {"3S"}; + pile p2 = {"AS", "AD", "AC"}; + pile p3 = {"2S", "2D", "2C"}; + pile p4 = {"AS", "AD", "AC", "AH"}; + + ASSERT_LT(p1, p2); + ASSERT_LT(p2, p3); + ASSERT_LT(p3, p4); + ASSERT_LT(p1, p4); + ASSERT_EQ(p4, p4); +} |
