hexmap.tests.cpp

 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
#include <planet/ostream.hpp>
#include <felspar/test.hpp>


namespace {


    auto const suite = felspar::testsuite("hexmap");


    auto const coords = suite.test(
            "coordinates",
            [](auto check) {
                check(planet::hexmap::east.column()) == 2;
                check(planet::hexmap::east.row()) == 0;
                check(planet::hexmap::north_east.column()) == 1;
                check(planet::hexmap::north_east.row()) == 1;
                check(planet::hexmap::north_west.column()) == -1;
                check(planet::hexmap::north_west.row()) == 1;
                check(planet::hexmap::west.column()) == -2;
                check(planet::hexmap::west.row()) == 0;
                check(planet::hexmap::south_west.column()) == -1;
                check(planet::hexmap::south_west.row()) == -1;
                check(planet::hexmap::south_east.column()) == 1;
                check(planet::hexmap::south_east.row()) == -1;

                check(planet::hexmap::coordinates{4, 2}.column()) == 4;
                check(planet::hexmap::coordinates{4, 2}.row()) == 2;
                check(planet::hexmap::coordinates{5, 3}.column()) == 5;
                check(planet::hexmap::coordinates{5, 3}.row()) == 3;

We move an illegal location to a legal one

 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                check(planet::hexmap::coordinates{5, 2}.column()) == 5;
                check(planet::hexmap::coordinates{5, 2}.row()) == 3;
            },
            [](auto check) {
                planet::hexmap::coordinates loc{};
                check(loc.column()) == 0;
                check(loc.row()) == 0;

                check((loc + planet::hexmap::east).column()) == 2;
                check((loc + planet::hexmap::east).row()) == 0;

                check((loc + planet::hexmap::east + planet::hexmap::north_east)
                              .column())
                        == 3;
                check((loc + planet::hexmap::east + planet::hexmap::north_east)
                              .row())
                        == 1;

                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west)
                              .column())
                        == 2;
                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west)
                              .row())
                        == 2;

                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west + planet::hexmap::west)
                              .column())
                        == 0;
                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west + planet::hexmap::west)
                              .row())
                        == 2;

                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west + planet::hexmap::west
                       + planet::hexmap::south_west)
                              .column())
                        == -1;
                check((loc + planet::hexmap::east + planet::hexmap::north_east
                       + planet::hexmap::north_west + planet::hexmap::west
                       + planet::hexmap::south_west)
                              .row())
                        == 1;

                check(loc + planet::hexmap::east + planet::hexmap::north_east
                      + planet::hexmap::north_west + planet::hexmap::west
                      + planet::hexmap::south_west + planet::hexmap::south_east)
                        == loc;
            });


    auto const col_iter = suite.test(
            "coordinates/by_columns",
            [](auto check) {
                auto even =
                        planet::hexmap::coordinates::by_column({0, 0}, {0, 0});
                check(even.next()).is_falsey();

                auto odd =
                        planet::hexmap::coordinates::by_column({1, 1}, {1, 1});
                check(odd.next()).is_falsey();
            },
            [](auto check) {
                auto even =
                        planet::hexmap::coordinates::by_column({0, 2}, {2, 0});
                check(even.next().value()) == planet::hexmap::coordinates{0, 2};
                check(even.next().value()) == planet::hexmap::coordinates{1, 1};
                check(even.next()).is_falsey();

                auto odd =
                        planet::hexmap::coordinates::by_column({1, 3}, {3, 1});
                check(odd.next().value()) == planet::hexmap::coordinates{1, 3};
                check(odd.next().value()) == planet::hexmap::coordinates{2, 2};
                check(odd.next()).is_falsey();
            },
            [](auto check) {
                auto even = planet::hexmap::coordinates::by_column(
                        {-2, 2}, {3, -3});
                check(even.next().value())
                        == planet::hexmap::coordinates{-2, 2};
                check(even.next().value()) == planet::hexmap::coordinates{0, 2};
                check(even.next().value()) == planet::hexmap::coordinates{2, 2};
                check(even.next().value())
                        == planet::hexmap::coordinates{-1, 1};
                check(even.next().value()) == planet::hexmap::coordinates{1, 1};
                check(even.next().value())
                        == planet::hexmap::coordinates{-2, 0};
                check(even.next().value()) == planet::hexmap::coordinates{0, 0};
                check(even.next().value()) == planet::hexmap::coordinates{2, 0};
                check(even.next().value())
                        == planet::hexmap::coordinates{-1, -1};
                check(even.next().value())
                        == planet::hexmap::coordinates{1, -1};
                check(even.next().value())
                        == planet::hexmap::coordinates{-2, -2};
                check(even.next().value())
                        == planet::hexmap::coordinates{0, -2};
                check(even.next().value())
                        == planet::hexmap::coordinates{2, -2};
                check(even.next()).is_falsey();

                auto odd = planet::hexmap::coordinates::by_column(
                        {-1, 3}, {4, -2});
                check(odd.next().value()) == planet::hexmap::coordinates{-1, 3};
                check(odd.next().value()) == planet::hexmap::coordinates{1, 3};
                check(odd.next().value()) == planet::hexmap::coordinates{3, 3};
                check(odd.next().value()) == planet::hexmap::coordinates{0, 2};
                check(odd.next().value()) == planet::hexmap::coordinates{2, 2};
                check(odd.next().value()) == planet::hexmap::coordinates{-1, 1};
                check(odd.next().value()) == planet::hexmap::coordinates{1, 1};
                check(odd.next().value()) == planet::hexmap::coordinates{3, 1};
                check(odd.next().value()) == planet::hexmap::coordinates{0, 0};
                check(odd.next().value()) == planet::hexmap::coordinates{2, 0};
                check(odd.next().value())
                        == planet::hexmap::coordinates{-1, -1};
                check(odd.next().value()) == planet::hexmap::coordinates{1, -1};
                check(odd.next().value()) == planet::hexmap::coordinates{3, -1};
                check(odd.next()).is_falsey();
            });


    auto const dirs = suite.test("directions", [](auto check) {
        planet::hexmap::coordinates from{5, 7}, to{11, 7};
        check(best_direction(from, to)) == planet::hexmap::east;
        to = {11, 13};
        check(best_direction(from, to)) == planet::hexmap::north_east;
        to = {-1, 13};
        check(best_direction(from, to)) == planet::hexmap::north_west;
        to = {-1, 7};
        check(best_direction(from, to)) == planet::hexmap::west;
        to = {-1, 1};
        check(best_direction(from, to)) == planet::hexmap::south_west;
        to = {11, 1};
        check(best_direction(from, to)) == planet::hexmap::south_east;
    });


    auto const world = suite.test("world", [](auto check, auto &log) {
        std::size_t calls{};
        planet::hexmap::world_type<std::pair<long, long>, 4> w{
                {0, 0}, [&calls](auto const p) mutable {
                    ++calls;
                    return std::pair{p.column(), p.row()};
                }};
        check(calls) == 0u;

        check(w[{0, 0}]) == std::pair{0L, 0L};
        check(calls) == 8u;

        check(w[{5, 7}]) == std::pair{5L, 7L};
        check(calls) == 16u;

        auto pos = w.chunks();
        auto p1 = pos.next()->first;
        log << "p1 " << p1.column() << ", " << p1.row() << '\n';
        check(p1) == planet::hexmap::coordinates{0, 0};
        auto p2 = pos.next()->first;
        log << "p2 " << p2.column() << ", " << p2.row() << '\n';
        check(p2) == planet::hexmap::coordinates{4, 4};
        check(pos.next()).is_falsey();
    });


    auto const moves = suite.test("moves", [](auto check) {
        check(planet::hexmap::coordinates{0, 0}.move_distance()) == 0u;
        check(planet::hexmap::coordinates{1, 1}.move_distance()) == 1u;
        check(planet::hexmap::coordinates{2, 0}.move_distance()) == 1u;
        check(planet::hexmap::coordinates{3, 1}.move_distance()) == 2u;
        check(planet::hexmap::coordinates{5, 1}.move_distance()) == 3u;
        check(planet::hexmap::coordinates{6, 2}.move_distance()) == 4u;
    });


    auto const distances = suite.test("signed_distance", [](auto check) {
        check(planet::hexmap::signed_distance({0, 0})) == -1.0f;
        check(planet::hexmap::signed_distance({0.5f, 0})) == -0.5f;
        check(std::abs(planet::hexmap::signed_distance({1.0f, 0}))) <= 0.0e-5f;
        check(planet::hexmap::signed_distance({1.5f, 0})) == 0.5f;
    });


}