aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_helper.cpp
blob: e1aa0a7bc50b885e0b9bb4bf6643b95dc7f451e4 (plain) (blame)
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
//
// Created by thecharlesblake on 12/26/17.
//

#include <gtest/gtest.h>

#include "test_helper.h"
#include "../main/game/search-state/game_state.h"
#include "../main/solver/solver.h"
#include "../main/input-output/input/json-parsing/json_helper.h"
#include "../main/input-output/input/json-parsing/rules_parser.h"
#include "../../lib/rapidjson/document.h"

#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f))

using namespace rapidjson;
using std::initializer_list;
using std::vector;
using std::ostream;

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, false);
    solver sol(gs, 1000000);

    return sol.run() == solver::sol_state::solved;
}

void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy,
                                                 std::vector<card> cards) {
    sol_rules rules = rules_parser::from_preset("simple-free-cell");
    rules.build_pol = policy;
    rules.foundations_removable = false;
    rules.cells = 0;

    game_state gs = game_state(rules, std::initializer_list<pile>{
            {},
            {},
            {},
            {},
            {"4C","3C","2C","AC"},
            {"4H","3H","2H","AH"},
            {"4S","3S","2S","AS"},
            {"4D","3D","2D","AD"}
    });

    solver sol(gs, 1000000);
    sol.run();

    auto frontier = sol.get_frontier();
    auto i = std::begin(frontier);

    for (card c : cards) {
        i++;
        ASSERT_TRUE(i->mv.type == move::mtype::dominance);
        ASSERT_TRUE(gs.get_data()[i->mv.from].top_card() == c);
        ASSERT_TRUE(i->mv.to >= 0 && i->mv.to <= 4);
        gs.make_move(i->mv);
    }
    ASSERT_TRUE(i->child_moves.empty());
}

void test_helper::expected_moves_test(sol_rules sr,
                                      initializer_list<pile> piles,
                                      vector<move> exp_moves) {
    game_state gs(sr, piles);
    vector<move> actual_moves = gs.get_legal_moves();

    ASSERT_TRUE(moves_eq(exp_moves, actual_moves)) << actual_moves;
}

bool test_helper::moves_eq(vector<move>& exp_moves, vector<move>& actual_moves) {
    if (exp_moves.size() != actual_moves.size()) return false;

    for (move m : actual_moves) {
        auto it = find(begin(exp_moves), end(exp_moves), m);
        bool found = it != end(exp_moves);

        if (found) exp_moves.erase(it);
        else return false;
    }

    return true;
}
ostream& operator <<(ostream& os, const move& m) {
    return os << "move:(" << int(m.from)
              << ","    << int(m.to)
              << ","     << int(m.count)
              << ")";
}

ostream& operator <<(ostream& os, const vector<move>& moves) {
    for (const move m : moves) {
        os << m << ",\n";
    }
    return os;
}