diff options
| author | Charlie Blake | 2018-04-04 16:02:31 +0100 |
|---|---|---|
| committer | GitHub | 2018-04-04 16:02:31 +0100 |
| commit | 9bdacbca71f402507ba2a09929a9cadeaadcfdd0 (patch) | |
| tree | 2f6a6473993d6df448f16253950f03c9f5c5031b /src/test | |
| parent | Merge pull request #47 from thecharlesblake/re-add-schema (diff) | |
| parent | Add legal move gen tests and refactor (diff) | |
| download | metasol-9bdacbca71f402507ba2a09929a9cadeaadcfdd0.tar.gz metasol-9bdacbca71f402507ba2a09929a9cadeaadcfdd0.zip | |
Merge pull request #48 from thecharlesblake/wider-testing
Wider testing
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/integration_tests/alpha_star_test.cpp (renamed from src/test/alpha_star_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/bakers_dozen_test.cpp (renamed from src/test/bakers_dozen_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/black_hole_test.cpp (renamed from src/test/black_hole_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/canfield_test.cpp (renamed from src/test/canfield_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/flower_garden_test.cpp (renamed from src/test/flower_garden_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/fortunes_favor_test.cpp (renamed from src/test/fortunes_favor_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/free_cell_test.cpp (renamed from src/test/free_cell_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/somerset_test.cpp (renamed from src/test/somerset_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/spanish_patience_test.cpp (renamed from src/test/spanish_patience_test.cpp) | 2 | ||||
| -rw-r--r-- | src/test/integration_tests/spider_test.cpp (renamed from src/test/spider_test.cpp) | 4 | ||||
| -rw-r--r-- | src/test/unit_tests/card_test.cpp | 92 | ||||
| -rw-r--r-- | src/test/unit_tests/deal_parser_test.cpp | 143 | ||||
| -rw-r--r-- | src/test/unit_tests/foundations_dominance_test.cpp (renamed from src/test/foundations_dominance_test.cpp) | 6 | ||||
| -rw-r--r-- | src/test/unit_tests/global_cache_test.cpp (renamed from src/test/global_cache_test.cpp) | 6 | ||||
| -rw-r--r-- | src/test/unit_tests/legal_move_gen_test.cpp | 318 | ||||
| -rw-r--r-- | src/test/unit_tests/pile_test.cpp | 87 | ||||
| -rw-r--r-- | src/test/unit_tests/rules_parser_test.cpp | 377 |
17 files changed, 1035 insertions, 16 deletions
diff --git a/src/test/alpha_star_test.cpp b/src/test/integration_tests/alpha_star_test.cpp index eeff1be..c3b23fe 100644 --- a/src/test/alpha_star_test.cpp +++ b/src/test/integration_tests/alpha_star_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/bakers_dozen_test.cpp b/src/test/integration_tests/bakers_dozen_test.cpp index 0af0669..f3d4763 100644 --- a/src/test/bakers_dozen_test.cpp +++ b/src/test/integration_tests/bakers_dozen_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/black_hole_test.cpp b/src/test/integration_tests/black_hole_test.cpp index 89ee50a..a0f6707 100644 --- a/src/test/black_hole_test.cpp +++ b/src/test/integration_tests/black_hole_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/canfield_test.cpp b/src/test/integration_tests/canfield_test.cpp index 63fd615..6a045ea 100644 --- a/src/test/canfield_test.cpp +++ b/src/test/integration_tests/canfield_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/flower_garden_test.cpp b/src/test/integration_tests/flower_garden_test.cpp index b1aee9e..641ccc0 100644 --- a/src/test/flower_garden_test.cpp +++ b/src/test/integration_tests/flower_garden_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/fortunes_favor_test.cpp b/src/test/integration_tests/fortunes_favor_test.cpp index eab2e7e..5511f9e 100644 --- a/src/test/fortunes_favor_test.cpp +++ b/src/test/integration_tests/fortunes_favor_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/free_cell_test.cpp b/src/test/integration_tests/free_cell_test.cpp index 507c915..659d86b 100644 --- a/src/test/free_cell_test.cpp +++ b/src/test/integration_tests/free_cell_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/somerset_test.cpp b/src/test/integration_tests/somerset_test.cpp index 22cfe5a..fce8e8c 100644 --- a/src/test/somerset_test.cpp +++ b/src/test/integration_tests/somerset_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/spanish_patience_test.cpp b/src/test/integration_tests/spanish_patience_test.cpp index 8973a53..da32fd5 100644 --- a/src/test/spanish_patience_test.cpp +++ b/src/test/integration_tests/spanish_patience_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; diff --git a/src/test/spider_test.cpp b/src/test/integration_tests/spider_test.cpp index 0e21f86..50f16fe 100644 --- a/src/test/spider_test.cpp +++ b/src/test/integration_tests/spider_test.cpp @@ -4,7 +4,7 @@ #include <gtest/gtest.h> -#include "test_helper.h" +#include "../test_helper.h" typedef test_helper th; @@ -23,6 +23,8 @@ 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/src/test/unit_tests/card_test.cpp b/src/test/unit_tests/card_test.cpp new file mode 100644 index 0000000..346dff9 --- /dev/null +++ b/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/src/test/unit_tests/deal_parser_test.cpp b/src/test/unit_tests/deal_parser_test.cpp new file mode 100644 index 0000000..58e5e0e --- /dev/null +++ b/src/test/unit_tests/deal_parser_test.cpp @@ -0,0 +1,143 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include <rapidjson/stringbuffer.h> +#include <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/src/test/foundations_dominance_test.cpp b/src/test/unit_tests/foundations_dominance_test.cpp index 77a33ff..4a7d2aa 100644 --- a/src/test/foundations_dominance_test.cpp +++ b/src/test/unit_tests/foundations_dominance_test.cpp @@ -4,9 +4,9 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; diff --git a/src/test/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index 780146b..97490ba 100644 --- a/src/test/global_cache_test.cpp +++ b/src/test/unit_tests/global_cache_test.cpp @@ -4,9 +4,9 @@ #include <gtest/gtest.h> -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp new file mode 100644 index 0000000..23955c2 --- /dev/null +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -0,0 +1,318 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/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::stock_deal_type sdt; +typedef game_state::move mv; + +using namespace std; + +bool moves_eq(vector<mv>&, vector<mv>&); +ostream& operator <<(ostream&, const mv&); +ostream& operator <<(ostream&, const vector<mv>&); + +TEST(LegalMoveGen, BuildPolAnySuit) { + sol_rules sr; + sr.build_pol = pol::ANY_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2D"}, + {"2S"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,0), + mv(4,1), + mv(4,2), + mv(4,3) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,1), + mv(4,3) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4,0) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {}, + {"AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupTrue) { + sol_rules sr; + sr.move_built_group = true; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"2C", "AC"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0, 1), + mv(1, 0, 2) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, MoveBuiltGroupFalse) { + sol_rules sr; + sr.move_built_group = false; + sr.tableau_pile_count = 2; + + game_state gs(sr, initializer_list<pile>{ + {}, + {"AC", "2C"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(1, 0, 1) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, Foundations) { + sol_rules sr; + sr.foundations = true; + sr.tableau_pile_count = 4; + + game_state gs(sr, initializer_list<pile>{ + {}, {}, {}, {}, + {"AC"}, + {"AH"}, + {"AS"}, + {"AD"}, + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 0), + mv(5, 1), + mv(6, 2), + mv(7, 3) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, FoundationsRemovable) { + sol_rules sr; + sr.foundations = true; + sr.foundations_removable = true; + sr.tableau_pile_count = 1; + + game_state gs(sr, initializer_list<pile>{ + {"AC","2C","3C"}, // Without a few cards here dominance logic blocks + {}, // removing from foundation + {}, + {}, + {} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(0, 4) + }; + + ASSERT_TRUE(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, initializer_list<pile>{ + {"3D"}, {}, // The cells + {"4H"} + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(0, 1), + mv(0, 2), + mv(2, 1) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, StockDealToWaste) { + sol_rules sr; + sr.foundations = true; + sr.tableau_pile_count = 3; + sr.stock_size = 2; + sr.stock_deal_t = sdt::WASTE; + + game_state gs(sr, initializer_list<pile>{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {}, // The waste + {}, {}, {} // The tableau piles + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 5) + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + +TEST(LegalMoveGen, StockDealToTableau) { + sol_rules sr; + sr.foundations = true; + sr.tableau_pile_count = 3; + sr.stock_size = 2; + sr.stock_deal_t = sdt::TABLEAU_PILES; + + game_state gs(sr, initializer_list<pile>{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {"3H"}, {"5D"}, {"7C"} // The tableau piles + }); + vector<mv> actual_moves = gs.get_legal_moves(); + + vector<mv> exp_moves = { + mv(4, 255, 2) // Special stock-to-tableau syntax + }; + + ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves; +} + + + +bool moves_eq(vector<mv>& exp_moves, vector<mv>& actual_moves) { + if (exp_moves.size() != actual_moves.size()) return false; + + for (mv m : actual_moves) { + auto it = find(begin(exp_moves), end(exp_moves), m); + bool found = it != end(exp_moves); + + if (found) exp_moves.erase(it); + else return false; + } + + return true; +} + +ostream& operator <<(ostream& os, const mv& m) { + return os << "move:(" << int(m.from) + << "," << int(m.to) + << "," << int(m.count) + << ")"; +} + +ostream& operator <<(ostream& os, const vector<mv>& moves) { + for (const mv m : moves) { + os << m << ",\n"; + } + return os; +} diff --git a/src/test/unit_tests/pile_test.cpp b/src/test/unit_tests/pile_test.cpp new file mode 100644 index 0000000..a5aeb66 --- /dev/null +++ b/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); +} diff --git a/src/test/unit_tests/rules_parser_test.cpp b/src/test/unit_tests/rules_parser_test.cpp new file mode 100644 index 0000000..581a536 --- /dev/null +++ b/src/test/unit_tests/rules_parser_test.cpp @@ -0,0 +1,377 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include <rapidjson/stringbuffer.h> +#include <rapidjson/document.h> + +#include "../../main/input-output/input/json-parsing/rules_parser.h" +#include "../../main/input-output/input/json-parsing/json_helper.h" + +using namespace std; +using namespace rapidjson; + +void run_rules_parser_test(bool, const char*); + +// invalid int, build pol, bool, other + +TEST(RulesParser, DefaultGameRules) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 8, + "build policy": "any-suit", + "spaces policy": "any", + "diagonal deal": false, + "move built group": false, + "move built group policy": "same-as-build" + }, + "max rank": 13, + "two decks": false, + "hole": false, + "foundations": true, + "foundations initial card": false, + "foundations removable": false, + "foundations complete piles": false, + "cells": 0, + "stock size": 0, + "stock deal type": "waste", + "reserve size": 0, + "reserve stacked": false +} +)"); +} + +TEST(RulesParser, InvalidType) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": "13" + } +} +)"); +} + +TEST(RulesParser, InvalidNumber) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": 13 + }, + "max rank": 20 +} +)"); +} + +TEST(RulesParser, InvalidEnum) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count": 13, + "build policy": "red-black-green" + } +} +)"); +} + +TEST(RulesParser, InvalidRuleName) { + run_rules_parser_test(false, R"( +{ + "tableau piles": { + "count number": 13 + } +} +)"); +} + +TEST(RulesParser, SpanishPatience) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 13 + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleSpanishPatience) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 2, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, FreeCell) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "build policy": "red-black" + }, + "cells": 4 +} +)"); +} + +TEST(RulesParser, SimpleFreeCell) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "max rank": 4, + "cells": 1 +} +)"); +} + +TEST(RulesParser, BakersDozen) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 13, + "spaces policy": "no-build" + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleBakersDozen) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "spaces policy": "no-build" + }, + "max rank": 3, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, BlackHole) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 17, + "build policy": "no-build" + }, + "hole": true, + "foundations": false +} +)"); +} + +TEST(RulesParser, SimpleBlackHole) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "no-build" + }, + "max rank": 5, + "hole": true, + "foundations": false +} +)"); +} + +TEST(RulesParser, FortunesFavor) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit" + }, + "foundations initial card": true, + "stock size": 36 +} +)"); +} + +TEST(RulesParser, SimpleFortunesFavor) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit" + }, + "max rank": 4, + "foundations initial card": true, + "stock size": 3 +} +)"); +} + +TEST(RulesParser, FlowerGarden) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 6 + }, + "reserve size": 16, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleFlowerGarden) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3 + }, + "max rank": 3, + "reserve size": 3, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, Canfield) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black" + }, + "stock size": 34, + "reserve size": 14, + "reserve stacked": true +} +)"); +} + +TEST(RulesParser, SimpleCanfield) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "red-black" + }, + "max rank": 3, + "stock size": 1, + "reserve size": 2, + "reserve stacked": true +} +)"); +} + +TEST(RulesParser, Somerset) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 10, + "build policy": "red-black", + "diagonal deal": true + }, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, SimpleSomerset) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "red-black", + "diagonal deal": true + }, + "max rank": 2, + "foundations removable": true +} +)"); +} + +TEST(RulesParser, AlphaStar) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 12, + "build policy": "same-suit", + "move built group": true + }, + "foundations initial card": true +} +)"); +} + +TEST(RulesParser, SimpleAlphaStar) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 3, + "build policy": "same-suit", + "move built group": true + }, + "max rank": 5, + "foundations initial card": true +} +)"); +} + +TEST(RulesParser, Spider) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 10, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" + }, + "foundations complete piles": true, + "two decks": true, + "stock size": 50, + "stock deal type": "tableau piles" +} +)"); +} + +TEST(RulesParser, SimpleSpider) { + run_rules_parser_test(true, R"( +{ + "tableau piles": { + "count": 4, + "build policy": "any-suit", + "move built group": true, + "move built group policy": "same-suit" + }, + "foundations complete piles": true, + "two decks": true, + "max rank": 3, + "stock size": 12, + "stock deal type": "tableau piles" +} +)"); +} + +void run_rules_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(rules_parser::rules_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; + } +} |
