From 56dfa21d26fd51827f37cf42289c97639b44d306 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 11:04:56 +0100 Subject: Add deal parser tests --- .../input/json-parsing/deal_parser.cpp | 24 ++++++++---------- .../input-output/input/json-parsing/deal_parser.h | 3 +-- .../input/json-parsing/rules_parser.cpp | 25 +++++++++---------- .../input-output/input/json-parsing/rules_parser.h | 2 +- src/test/deal_parser_test.cpp | 29 ++++++++++++++++++++++ src/test/spider_test.cpp | 2 ++ 6 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 src/test/deal_parser_test.cpp (limited to 'src') diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp index 17a8ae4..f841c8d 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -48,11 +48,18 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { fill_foundations(gs); } - apply_deal_schema(doc); // Throws an error if it fails + Document d; + d.Parse(deal_schema_json().c_str()); + assert(!d.HasParseError()); + SchemaDocument sd(d); + SchemaValidator validator(sd); + if(!doc.Accept(validator)) { + throw runtime_error(json_helper::schema_err_str(validator)); + } } -void deal_parser::apply_deal_schema(const rapidjson::Document &doc) { - const char* schema_json = R"( +string deal_parser::deal_schema_json() { + string schema_json = R"( { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a solitaire game state to solve", @@ -75,16 +82,7 @@ void deal_parser::apply_deal_schema(const rapidjson::Document &doc) { }, "additionalProperties": false } )"; - - Document d; - d.Parse(schema_json); - assert(!d.HasParseError()); - SchemaDocument schema(d); - SchemaValidator validator(schema); - - if(!doc.Accept(validator)) { - throw runtime_error(json_helper::schema_err_str(validator)); - } + return schema_json; } void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& doc) { diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h index bfb7246..7aa3afe 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -16,14 +16,13 @@ class game_state; class deal_parser { public: static void parse(game_state&, const rapidjson::Document&); -private: static void parse_tableau_piles(game_state&, const rapidjson::Document&); static void parse_hole(game_state&, const rapidjson::Document&); static void parse_cells(game_state&, const rapidjson::Document&); static void parse_stock(game_state&, const rapidjson::Document&); static void parse_reserve(game_state&, const rapidjson::Document&); static void fill_foundations(game_state&); - static void apply_deal_schema(const rapidjson::Document &); + static std::string deal_schema_json(); }; #endif //SOLVITAIRE_DEAL_PARSER_H diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp index 800f90e..3c4bee3 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -272,11 +272,19 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { "must be true"); } - apply_rules_schema(d); // Throws an error if it fails + rules_schema_json(); // Throws an error if it fails + Document vd; + vd.Parse(rules_schema_json().c_str()); + assert(!vd.HasParseError()); + SchemaDocument sd(vd); + SchemaValidator validator(sd); + if(!d.Accept(validator)) { + throw runtime_error(json_helper::schema_err_str(validator)); + } } -void rules_parser::apply_rules_schema(const rapidjson::Document &doc) { - const char* schema_json = R"( +string rules_parser::rules_schema_json() { + string schema_json = R"( { "$schema": "http://json-schema.org/draft-04/schema#", "description": "JSON Schema representing a generic solitaire game", @@ -309,14 +317,5 @@ void rules_parser::apply_rules_schema(const rapidjson::Document &doc) { }, "additionalProperties": false } )"; - - Document d; - d.Parse(schema_json); - assert(!d.HasParseError()); - SchemaDocument schema(d); - SchemaValidator validator(schema); - - if(!doc.Accept(validator)) { - throw runtime_error(json_helper::schema_err_str(validator)); - } + return schema_json; } diff --git a/src/main/input-output/input/json-parsing/rules_parser.h b/src/main/input-output/input/json-parsing/rules_parser.h index 497caaf..fdfe018 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.h +++ b/src/main/input-output/input/json-parsing/rules_parser.h @@ -11,11 +11,11 @@ class rules_parser { public: static const sol_rules from_file(std::string); static const sol_rules from_preset(std::string); + static std::string rules_schema_json(); private: static sol_rules get_default(); static void modify_sol_rules(sol_rules&, rapidjson::Document&); - static void apply_rules_schema(const rapidjson::Document&); }; diff --git a/src/test/deal_parser_test.cpp b/src/test/deal_parser_test.cpp new file mode 100644 index 0000000..77a33ff --- /dev/null +++ b/src/test/deal_parser_test.cpp @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "test_helper.h" +#include "../main/game/game_state.h" +#include "../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(FoundationsDominance, SameSuit) { + test_helper::run_foundations_dominance_test(pol::SAME_SUIT, { + "AC","2C","3C","4C","AH","2H","3H","4H","AS","2S","3S","4S","AD", + "2D","3D","4D"}); +} + +TEST(FoundationsDominance, RedBlack) { + test_helper::run_foundations_dominance_test(pol::RED_BLACK, { + "AC","2C","AH","2H","AS","3H","2S","AD","3C","4H","3S","2D","4C", + "4S","3D","4D"}); +} + +TEST(FoundationsDominance, AnySuit) { + test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { + "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", + "4S","3D","4D"}); +} \ No newline at end of file diff --git a/src/test/spider_test.cpp b/src/test/spider_test.cpp index 0e21f86..ffca592 100644 --- a/src/test/spider_test.cpp +++ b/src/test/spider_test.cpp @@ -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)); } +*/ -- cgit v1.2.1 From 26d2836487847bec34adce7d80009ab6c89fa2c3 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 11:22:44 +0100 Subject: Add rules parsing tests --- src/test/deal_parser_test.cpp | 148 ++++++++++++++-- src/test/rules_parser_test.cpp | 377 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 508 insertions(+), 17 deletions(-) create mode 100644 src/test/rules_parser_test.cpp (limited to 'src') diff --git a/src/test/deal_parser_test.cpp b/src/test/deal_parser_test.cpp index 77a33ff..750913d 100644 --- a/src/test/deal_parser_test.cpp +++ b/src/test/deal_parser_test.cpp @@ -4,26 +4,140 @@ #include -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" +#include +#include -typedef sol_rules::build_policy pol; +#include "../main/input-output/input/json-parsing/deal_parser.h" +#include "../main/input-output/input/json-parsing/json_helper.h" -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"}); +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(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(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(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 +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/rules_parser_test.cpp b/src/test/rules_parser_test.cpp new file mode 100644 index 0000000..f5f3f76 --- /dev/null +++ b/src/test/rules_parser_test.cpp @@ -0,0 +1,377 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include +#include + +#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; + } +} -- cgit v1.2.1 From 8b9855ed10e9817a76f7fda7b3f481c2386b4bc7 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 12:07:18 +0100 Subject: Add card tests --- src/test/card_test.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/card_test.cpp (limited to 'src') diff --git a/src/test/card_test.cpp b/src/test/card_test.cpp new file mode 100644 index 0000000..a991d9c --- /dev/null +++ b/src/test/card_test.cpp @@ -0,0 +1,92 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#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" )); +} -- cgit v1.2.1 From 669a438205ad4eba5b7c100b10312ff30bb5118e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 12:51:48 +0100 Subject: Add pile tests --- src/main/game/pile.cpp | 4 +++ src/main/game/pile.h | 4 ++- src/test/pile_test.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/test/pile_test.cpp (limited to 'src') diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 382fa50..3136681 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -53,6 +53,10 @@ bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } +bool operator!=(const pile& a, const pile& b) { + return a.pile_vec != b.pile_vec; +} + bool operator<(const pile& a, const pile& b) { if (a.size() == b.size()) { return a.pile_vec < b.pile_vec; diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 654f5be..d86ea37 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -33,15 +33,17 @@ public: card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); + friend bool operator!=(const pile&, const pile&); friend bool operator<(const pile&, const pile&); friend bool operator>(const pile&, const pile&); friend bool operator<=(const pile&, const pile&); friend bool operator>=(const pile&, const pile&); -private: + // Modify void place(card); card take(); +private: // Underlying vector std::vector pile_vec; }; diff --git a/src/test/pile_test.cpp b/src/test/pile_test.cpp new file mode 100644 index 0000000..29c617f --- /dev/null +++ b/src/test/pile_test.cpp @@ -0,0 +1,87 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#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); +} -- cgit v1.2.1 From ec02b3eaf85f222335463b3e2e6790f7e2691047 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Wed, 4 Apr 2018 16:00:47 +0100 Subject: Add legal move gen tests and refactor --- src/main/game/game_state.h | 3 +- src/main/game/game_state.legal_moves.cpp | 12 +- src/main/solver/solver.cpp | 11 +- src/test/alpha_star_test.cpp | 28 -- src/test/bakers_dozen_test.cpp | 28 -- src/test/black_hole_test.cpp | 28 -- src/test/canfield_test.cpp | 28 -- src/test/card_test.cpp | 92 ----- src/test/deal_parser_test.cpp | 143 -------- src/test/flower_garden_test.cpp | 28 -- src/test/fortunes_favor_test.cpp | 28 -- src/test/foundations_dominance_test.cpp | 29 -- src/test/free_cell_test.cpp | 28 -- src/test/global_cache_test.cpp | 77 ----- src/test/integration_tests/alpha_star_test.cpp | 28 ++ src/test/integration_tests/bakers_dozen_test.cpp | 28 ++ src/test/integration_tests/black_hole_test.cpp | 28 ++ src/test/integration_tests/canfield_test.cpp | 28 ++ src/test/integration_tests/flower_garden_test.cpp | 28 ++ src/test/integration_tests/fortunes_favor_test.cpp | 28 ++ src/test/integration_tests/free_cell_test.cpp | 28 ++ src/test/integration_tests/somerset_test.cpp | 28 ++ .../integration_tests/spanish_patience_test.cpp | 28 ++ src/test/integration_tests/spider_test.cpp | 30 ++ src/test/pile_test.cpp | 87 ----- src/test/rules_parser_test.cpp | 377 --------------------- src/test/somerset_test.cpp | 28 -- src/test/spanish_patience_test.cpp | 28 -- src/test/spider_test.cpp | 30 -- src/test/unit_tests/card_test.cpp | 92 +++++ src/test/unit_tests/deal_parser_test.cpp | 143 ++++++++ src/test/unit_tests/foundations_dominance_test.cpp | 29 ++ src/test/unit_tests/global_cache_test.cpp | 77 +++++ src/test/unit_tests/legal_move_gen_test.cpp | 318 +++++++++++++++++ src/test/unit_tests/pile_test.cpp | 87 +++++ src/test/unit_tests/rules_parser_test.cpp | 377 +++++++++++++++++++++ 36 files changed, 1422 insertions(+), 1096 deletions(-) delete mode 100644 src/test/alpha_star_test.cpp delete mode 100644 src/test/bakers_dozen_test.cpp delete mode 100644 src/test/black_hole_test.cpp delete mode 100644 src/test/canfield_test.cpp delete mode 100644 src/test/card_test.cpp delete mode 100644 src/test/deal_parser_test.cpp delete mode 100644 src/test/flower_garden_test.cpp delete mode 100644 src/test/fortunes_favor_test.cpp delete mode 100644 src/test/foundations_dominance_test.cpp delete mode 100644 src/test/free_cell_test.cpp delete mode 100644 src/test/global_cache_test.cpp create mode 100644 src/test/integration_tests/alpha_star_test.cpp create mode 100644 src/test/integration_tests/bakers_dozen_test.cpp create mode 100644 src/test/integration_tests/black_hole_test.cpp create mode 100644 src/test/integration_tests/canfield_test.cpp create mode 100644 src/test/integration_tests/flower_garden_test.cpp create mode 100644 src/test/integration_tests/fortunes_favor_test.cpp create mode 100644 src/test/integration_tests/free_cell_test.cpp create mode 100644 src/test/integration_tests/somerset_test.cpp create mode 100644 src/test/integration_tests/spanish_patience_test.cpp create mode 100644 src/test/integration_tests/spider_test.cpp delete mode 100644 src/test/pile_test.cpp delete mode 100644 src/test/rules_parser_test.cpp delete mode 100644 src/test/somerset_test.cpp delete mode 100644 src/test/spanish_patience_test.cpp delete mode 100644 src/test/spider_test.cpp create mode 100644 src/test/unit_tests/card_test.cpp create mode 100644 src/test/unit_tests/deal_parser_test.cpp create mode 100644 src/test/unit_tests/foundations_dominance_test.cpp create mode 100644 src/test/unit_tests/global_cache_test.cpp create mode 100644 src/test/unit_tests/legal_move_gen_test.cpp create mode 100644 src/test/unit_tests/pile_test.cpp create mode 100644 src/test/unit_tests/rules_parser_test.cpp (limited to 'src') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 409fee9..a49db2c 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -30,6 +30,7 @@ public: struct move { move(pile_ref, pile_ref, pile::size_type = 1); bool is_dominance() const; + friend bool operator==(const move&, const move&); const static pile::size_type dominance_flag; pile_ref from; pile_ref to; pile::size_type count; @@ -51,7 +52,7 @@ public: std::vector get_legal_moves( #ifndef NO_CARD_JUST_MOVED - move + move = move(255, 255, 255) #endif ); diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index f0502d1..c137b07 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -22,8 +22,10 @@ vector game_state::get_legal_moves( vector moves; // If we have a stock that can deal to the tableau piles, adds this move - if (rules.stock_deal_t == sdt::TABLEAU_PILES && !piles[stock].empty()) { - // Only does this if all the tableau piles are empty + if (rules.stock_size > 0 + && rules.stock_deal_t == sdt::TABLEAU_PILES + && !piles[stock].empty()) { + // Only does this if none of the tableau piles are empty bool no_tableau_empty = true; for (auto tab_ref : tableau_piles) { if (piles[tab_ref].empty()) { @@ -394,3 +396,9 @@ bool game_state::dominance_blocks_foundation_move(pile_ref target_pile) { return blocked; } + +bool operator==(const game_state::move& a, const game_state::move& b) { + return (a.from == b.from ) + && (a.to == b.to ) + && (a.count == b.count); +} diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 474800c..2fca763 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -61,14 +61,14 @@ solver::sol_state solver::run_with_cutoff( if (cutoff_depth && depth >= *cutoff_depth) { cutoff_triggered = true; - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { // If there is a dominance move available, adds it to the search tree // and repeats. Doesn't cache the state. optional dominance_move = state.get_dominance_move(); if (dominance_move) { // Adds the dominance move as a child of the current search node - add_child(*dominance_move);//TODO+ + add_child(*dominance_move); } else { // Caches the current state bool is_new_state = cache.insert(state); @@ -78,14 +78,14 @@ solver::sol_state solver::run_with_cutoff( // If there are none, reverts to the last node with children if (next_moves.empty()) { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } else { - add_children(next_moves);//TODO+ + add_children(next_moves); } } // If the state is not a new one, reverts to the last node with children else { - states_exhausted = revert_to_last_node_with_children();//TODO- + states_exhausted = revert_to_last_node_with_children(); } } } @@ -175,7 +175,6 @@ void solver::print_solution() const { if (states_searched > 1) { while (!n->children.empty()) { - // TODO print move as well state_copy.make_move(n->move); cout << state_copy << "\n"; n = &n->children.back(); diff --git a/src/test/alpha_star_test.cpp b/src/test/alpha_star_test.cpp deleted file mode 100644 index eeff1be..0000000 --- a/src/test/alpha_star_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/bakers_dozen_test.cpp b/src/test/bakers_dozen_test.cpp deleted file mode 100644 index 0af0669..0000000 --- a/src/test/bakers_dozen_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/black_hole_test.cpp b/src/test/black_hole_test.cpp deleted file mode 100644 index 89ee50a..0000000 --- a/src/test/black_hole_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/canfield_test.cpp b/src/test/canfield_test.cpp deleted file mode 100644 index 63fd615..0000000 --- a/src/test/canfield_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#include "test_helper.h" - -typedef test_helper th; - -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)); -} diff --git a/src/test/card_test.cpp b/src/test/card_test.cpp deleted file mode 100644 index a991d9c..0000000 --- a/src/test/card_test.cpp +++ /dev/null @@ -1,92 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#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/deal_parser_test.cpp b/src/test/deal_parser_test.cpp deleted file mode 100644 index 750913d..0000000 --- a/src/test/deal_parser_test.cpp +++ /dev/null @@ -1,143 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#include -#include - -#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/flower_garden_test.cpp b/src/test/flower_garden_test.cpp deleted file mode 100644 index b1aee9e..0000000 --- a/src/test/flower_garden_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/fortunes_favor_test.cpp b/src/test/fortunes_favor_test.cpp deleted file mode 100644 index eab2e7e..0000000 --- a/src/test/fortunes_favor_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/foundations_dominance_test.cpp b/src/test/foundations_dominance_test.cpp deleted file mode 100644 index 77a33ff..0000000 --- a/src/test/foundations_dominance_test.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" - -typedef sol_rules::build_policy pol; - -TEST(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/src/test/free_cell_test.cpp b/src/test/free_cell_test.cpp deleted file mode 100644 index 507c915..0000000 --- a/src/test/free_cell_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/global_cache_test.cpp b/src/test/global_cache_test.cpp deleted file mode 100644 index 780146b..0000000 --- a/src/test/global_cache_test.cpp +++ /dev/null @@ -1,77 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" - -typedef sol_rules::build_policy pol; - -TEST(GlobalCache, CommutativeTableauPiles) { - sol_rules rules; - rules.tableau_pile_count = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list{{},{},{}}); - global_cache cache(gs); - - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); - - // Test with empty piles - cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); - - // Test with piles of size > 1 - cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); -} - -TEST(GlobalCache, CommutativeReserve) { - sol_rules rules; - rules.reserve_size = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list{{},{},{}}); - global_cache cache(gs); - - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); - - // Test with empty piles - cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); - - // Test with piles of size > 1 - cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); -} - -TEST(GlobalCache, CommutativeCells) { - sol_rules rules; - rules.cells = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list{{},{},{}}); - global_cache cache(gs); - - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); - - // Test with empty piles - cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); -} diff --git a/src/test/integration_tests/alpha_star_test.cpp b/src/test/integration_tests/alpha_star_test.cpp new file mode 100644 index 0000000..c3b23fe --- /dev/null +++ b/src/test/integration_tests/alpha_star_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/bakers_dozen_test.cpp b/src/test/integration_tests/bakers_dozen_test.cpp new file mode 100644 index 0000000..f3d4763 --- /dev/null +++ b/src/test/integration_tests/bakers_dozen_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/black_hole_test.cpp b/src/test/integration_tests/black_hole_test.cpp new file mode 100644 index 0000000..a0f6707 --- /dev/null +++ b/src/test/integration_tests/black_hole_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/canfield_test.cpp b/src/test/integration_tests/canfield_test.cpp new file mode 100644 index 0000000..6a045ea --- /dev/null +++ b/src/test/integration_tests/canfield_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#include "../test_helper.h" + +typedef test_helper th; + +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)); +} diff --git a/src/test/integration_tests/flower_garden_test.cpp b/src/test/integration_tests/flower_garden_test.cpp new file mode 100644 index 0000000..641ccc0 --- /dev/null +++ b/src/test/integration_tests/flower_garden_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/fortunes_favor_test.cpp b/src/test/integration_tests/fortunes_favor_test.cpp new file mode 100644 index 0000000..5511f9e --- /dev/null +++ b/src/test/integration_tests/fortunes_favor_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/free_cell_test.cpp b/src/test/integration_tests/free_cell_test.cpp new file mode 100644 index 0000000..659d86b --- /dev/null +++ b/src/test/integration_tests/free_cell_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/somerset_test.cpp b/src/test/integration_tests/somerset_test.cpp new file mode 100644 index 0000000..fce8e8c --- /dev/null +++ b/src/test/integration_tests/somerset_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/spanish_patience_test.cpp b/src/test/integration_tests/spanish_patience_test.cpp new file mode 100644 index 0000000..da32fd5 --- /dev/null +++ b/src/test/integration_tests/spanish_patience_test.cpp @@ -0,0 +1,28 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/integration_tests/spider_test.cpp b/src/test/integration_tests/spider_test.cpp new file mode 100644 index 0000000..50f16fe --- /dev/null +++ b/src/test/integration_tests/spider_test.cpp @@ -0,0 +1,30 @@ +// +// Created by thecharlesblake on 10/4/17. +// + +#include + +#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/src/test/pile_test.cpp b/src/test/pile_test.cpp deleted file mode 100644 index 29c617f..0000000 --- a/src/test/pile_test.cpp +++ /dev/null @@ -1,87 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#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/rules_parser_test.cpp b/src/test/rules_parser_test.cpp deleted file mode 100644 index f5f3f76..0000000 --- a/src/test/rules_parser_test.cpp +++ /dev/null @@ -1,377 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#include -#include - -#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; - } -} diff --git a/src/test/somerset_test.cpp b/src/test/somerset_test.cpp deleted file mode 100644 index 22cfe5a..0000000 --- a/src/test/somerset_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/spanish_patience_test.cpp b/src/test/spanish_patience_test.cpp deleted file mode 100644 index 8973a53..0000000 --- a/src/test/spanish_patience_test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/src/test/spider_test.cpp b/src/test/spider_test.cpp deleted file mode 100644 index ffca592..0000000 --- a/src/test/spider_test.cpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by thecharlesblake on 10/4/17. -// - -#include - -#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/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 + +#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 + +#include +#include + +#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/unit_tests/foundations_dominance_test.cpp b/src/test/unit_tests/foundations_dominance_test.cpp new file mode 100644 index 0000000..4a7d2aa --- /dev/null +++ b/src/test/unit_tests/foundations_dominance_test.cpp @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(FoundationsDominance, SameSuit) { + test_helper::run_foundations_dominance_test(pol::SAME_SUIT, { + "AC","2C","3C","4C","AH","2H","3H","4H","AS","2S","3S","4S","AD", + "2D","3D","4D"}); +} + +TEST(FoundationsDominance, RedBlack) { + test_helper::run_foundations_dominance_test(pol::RED_BLACK, { + "AC","2C","AH","2H","AS","3H","2S","AD","3C","4H","3S","2D","4C", + "4S","3D","4D"}); +} + +TEST(FoundationsDominance, AnySuit) { + test_helper::run_foundations_dominance_test(pol::ANY_SUIT, { + "AC","2C","AH","2H","AS","2S","AD","3C","3H","3S","2D","4C","4H", + "4S","3D","4D"}); +} \ No newline at end of file diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp new file mode 100644 index 0000000..97490ba --- /dev/null +++ b/src/test/unit_tests/global_cache_test.cpp @@ -0,0 +1,77 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "../test_helper.h" +#include "../../main/game/game_state.h" +#include "../../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(GlobalCache, CommutativeTableauPiles) { + sol_rules rules; + rules.tableau_pile_count = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, std::initializer_list{{},{},{}}); + global_cache cache(gs); + + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + + // Test with piles of size > 1 + cache.clear(); + cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); +} + +TEST(GlobalCache, CommutativeReserve) { + sol_rules rules; + rules.reserve_size = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, std::initializer_list{{},{},{}}); + global_cache cache(gs); + + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + + // Test with piles of size > 1 + cache.clear(); + cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); +} + +TEST(GlobalCache, CommutativeCells) { + sol_rules rules; + rules.cells = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, std::initializer_list{{},{},{}}); + global_cache cache(gs); + + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + + // Test with empty piles + cache.clear(); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); +} 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 + +#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&, vector&); +ostream& operator <<(ostream&, const mv&); +ostream& operator <<(ostream&, const vector&); + +TEST(LegalMoveGen, BuildPolAnySuit) { + sol_rules sr; + sr.build_pol = pol::ANY_SUIT; + sr.tableau_pile_count = 5; + + game_state gs(sr, initializer_list{ + {"2C"}, + {"2H"}, + {"2D"}, + {"2S"}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {"2C"}, + {"2H"}, + {"2S"}, + {"2D"}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, + {"AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, + {"2C", "AC"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, + {"AC", "2C"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, {}, {}, {}, + {"AC"}, + {"AH"}, + {"AS"}, + {"AD"}, + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {"AC","2C","3C"}, // Without a few cards here dominance logic blocks + {}, // removing from foundation + {}, + {}, + {} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {"3D"}, {}, // The cells + {"4H"} + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {}, // The waste + {}, {}, {} // The tableau piles + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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{ + {}, {}, {}, {}, // The foundations + {"AC", "AD"}, // The stock + {"3H"}, {"5D"}, {"7C"} // The tableau piles + }); + vector actual_moves = gs.get_legal_moves(); + + vector 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& exp_moves, vector& 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& 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 + +#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 + +#include +#include + +#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; + } +} -- cgit v1.2.1