1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
//
// Created by thecharlesblake on 12/26/17.
//
#include <rapidjson/document.h>
#include <gtest/gtest.h>
#include "test_helper.h"
#include "../main/game/game_state.h"
#include "../main/solver/solver.h"
#include "../main/input-output/input/json-parsing/json_helper.h"
#include "../main/input-output/input/json-parsing/rules_parser.h"
#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f))
using namespace rapidjson;
using namespace std;
bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) {
const Document in_doc = json_helper::get_file_json(input_file);
const sol_rules rules = rules_parser::from_preset(preset_type);
game_state gs(rules, in_doc);
solver sol(gs);
return sol.run();
}
void test_helper::run_card_cache_test(sol_rules::build_policy bp,
bool assert_dif_col,
bool assert_same_col) {
sol_rules rules;
rules.tableau_pile_count = 1;
rules.build_pol = bp;
for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) {
for (card::rank_t rank = 1; rank <= 13; rank++) {
// c2 is the same as c_ but with a different suit
const card c = card(suit_idx, rank);
const card c_dif_col = card(uint8_t((suit_idx + 1) % 4), rank);
const card c_same_col = card(uint8_t((suit_idx + 2) % 4), rank);
const card c_dif_rank = card(suit_idx, card::rank_t((rank % 13) + 1));
const game_state s = game_state(rules, initializer_list<pile>{{c}});
const game_state s_dif_suit(rules, initializer_list<pile>{{c_dif_col}});
const game_state s_same_col(rules, initializer_list<pile>{{c_same_col}});
const game_state s_dif_rank(rules, initializer_list<pile>{{c_dif_rank}});
global_cache cache(s);
cache.insert(s);
ASSERT_TRUE(cache.contains(s)) << "Contains self";
ASSERT_(assert_dif_col, hasher(s)(cached_game_state(s))
== hasher(s)(cached_game_state(s_dif_suit)))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_dif_col.to_string();
ASSERT_(assert_dif_col, cache.contains(s_dif_suit))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_dif_col.to_string();
ASSERT_(assert_same_col, hasher(s)(cached_game_state(s))
== hasher(s)(cached_game_state(s_same_col)))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_same_col.to_string();
ASSERT_(assert_same_col, cache.contains(s_same_col))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_same_col.to_string();
ASSERT_FALSE(hasher(s)(cached_game_state(s))
== hasher(s)(cached_game_state(s_dif_rank)))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_dif_rank.to_string();
ASSERT_FALSE(cache.contains(s_dif_rank))
<< "Insert: " << c.to_string()
<< ", Comp: " << c_dif_rank.to_string();
}
}
}
void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy,
std::vector<card> cards) {
sol_rules rules = rules_parser::from_preset("simple-free-cell");
rules.build_pol = policy;
rules.foundations_removable = false;
rules.cells = 0;
game_state gs = game_state(rules, std::initializer_list<pile>{
{},
{},
{},
{},
{"4C","3C","2C","AC"},
{"4H","3H","2H","AH"},
{"4S","3S","2S","AS"},
{"4D","3D","2D","AD"}
});
solver sol = solver(game_state(gs));
sol.run();
solver::node const *n = &sol.get_search_tree();
for (card c : cards) {
n = &n->children.back();
ASSERT_TRUE(n->move.is_dominance());
ASSERT_TRUE(gs.get_data()[n->move.from].top_card() == c);
ASSERT_TRUE(n->move.to >= 0 && n->move.to <= 4);
gs.make_move(n->move);
}
ASSERT_TRUE(n->children.empty());
}
void test_helper::optimisation_validity_test(optimisation op) {
for (string preset : {
"simple-alpha-star",
"simple-bakers-dozen",
"simple-black-hole",
"simple-canfield",
"simple-flower-garden",
"simple-fortunes-favor",
"simple-free-cell",
"simple-somerset",
"simple-spanish-patience",
"simple-spider"
}) {
vector<bool> solutions_op, solutions_no_op;
switch (op) {
case optimisation::AUTO_FOUNDATION_MOVES:
#define NO_AUTO_FOUNDATIONS
solutions_no_op = run_many_games(preset);
#undef NO_AUTO_FOUNDATIONS
solutions_op = run_many_games(preset);
break;
}
ASSERT_TRUE(solutions_op == solutions_no_op) << preset;
}
}
std::vector<bool> test_helper::run_many_games(std::string preset) {
#ifdef NDEBUG
const int run_count = 2000;
#else
const int run_count = 5;
#endif
vector<bool> solutions;
for (int seed = 0; seed < run_count; seed++) {
solver sol(game_state(rules_parser::from_preset(preset), seed));
solutions.push_back(sol.run());
}
return solutions;
}
|