File: test_coordit.cc

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (207 lines) | stat: -rw-r--r-- 5,743 bytes parent folder | download | duplicates (2)
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
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
#include <random>
#include <set>

#include "catch_amalgamated.hpp"

#include "AppHdr.h"

#include "coordit.h"

TEST_CASE("rectangle_iterator", "[single-file]")
{
    SECTION("Generates w*h unique points from top left, bottom right pair")
    {
        const auto x = GENERATE(10, 20, 30, 40);
        const auto y = GENERATE(10, 20, 30, 40);
        const auto w = GENERATE(10, 20, 30, 40);
        const auto h = GENERATE(10, 20, 30, 40);

        CAPTURE(x, y, w, h);

        set<coord_def> points;
        for (rectangle_iterator ri(coord_def(x,y), coord_def(x+w,y+h)); ri; ++ri)
            points.insert(*ri);

        REQUIRE((w >= 0 && h >= 0));
        REQUIRE(points.size() == static_cast<size_t>((w+1)*(h+1)));  // Inclusive.
    }

    SECTION("Generates w*h unique points from centre and halfside")
    {
        const auto x = GENERATE(10, 20, 30, 40);
        const auto y = GENERATE(10, 20, 30, 40);
        const auto halfside = GENERATE(10, 20, 30, 40);

        CAPTURE(x, y, halfside);

        set<coord_def> points;
        for (rectangle_iterator ri(coord_def(x,y), halfside); ri; ++ri)
            points.insert(*ri);

        REQUIRE(halfside >= 0);
        const size_t side_length = static_cast<size_t>(halfside)*2 + 1;
        REQUIRE(points.size() == side_length*side_length);  // Inclusive.
    }
}

TEST_CASE("random_rectangle_iterator", "[single-file]")
{
    SECTION("Generates w*h unique points")
    {
        auto x = GENERATE(10, 20, 30, 40);
        auto y = GENERATE(10, 20, 30, 40);
        auto w = GENERATE(10, 20, 30, 40);
        auto h = GENERATE(10, 20, 30, 40);

        CAPTURE(x, y, w, h);

        set<coord_def> points;
        for (random_rectangle_iterator ri(coord_def(x,y), coord_def(x+w,y+h)); ri; ++ri)
            points.insert(*ri);

        REQUIRE((w >= 0 && h >= 0));
        REQUIRE(points.size() == static_cast<size_t>((w+1)*(h+1)));  // Inclusive.
    }
}

TEST_CASE("orth_adjacent_iterator", "[single-file]")
{
    SECTION("Includes only orthagonally adjacent locations")
    {
        const coord_def where(50, 50);

        vector<coord_def> adjacent;
        for (orth_adjacent_iterator ai(where); ai; ++ai)
            adjacent.push_back(*ai);

        // Right, left, bottom, top.
        const vector<coord_def> expected = {
            coord_def(51, 50),
            coord_def(49, 50),
            coord_def(50, 51),
            coord_def(50, 49),
        };

        REQUIRE(adjacent == expected);
    }

    SECTION("Includes the centre if specified")
    {
        const coord_def where(50, 50);

        vector<coord_def> adjacent;
        for (orth_adjacent_iterator ai(where, false); ai; ++ai)
            adjacent.push_back(*ai);

        // Centre, right, left, bottom, top.
        const vector<coord_def> expected = {
            coord_def(50, 50),
            coord_def(51, 50),
            coord_def(49, 50),
            coord_def(50, 51),
            coord_def(50, 49),
        };

        REQUIRE(adjacent == expected);
    }
}

TEST_CASE("adjacent_iterator", "[single-file]")
{
    SECTION("Includes all adjacent locations")
    {
        const coord_def where(50, 50);

        vector<coord_def> adjacent;
        for (adjacent_iterator ai(where); ai; ++ai)
            adjacent.push_back(*ai);

        // Starts at top-left and rotates counter-clockwise.
        const vector<coord_def> expected = {
            coord_def(49, 49),
            coord_def(49, 50),
            coord_def(49, 51),
            coord_def(50, 51),
            coord_def(51, 51),
            coord_def(51, 50),
            coord_def(51, 49),
            coord_def(50, 49),
        };

        REQUIRE(adjacent == expected);
    }

    SECTION("Includes the centre if specified")
    {
        const coord_def where(50, 50);

        vector<coord_def> adjacent;
        for (adjacent_iterator ai(where, false); ai; ++ai)
            adjacent.push_back(*ai);

        // Centre first, then starts at top-left and rotates counter-clockwise.
        const vector<coord_def> expected = {
            coord_def(50, 50),
            coord_def(49, 49),
            coord_def(49, 50),
            coord_def(49, 51),
            coord_def(50, 51),
            coord_def(51, 51),
            coord_def(51, 50),
            coord_def(51, 49),
            coord_def(50, 49),
        };

        REQUIRE(adjacent == expected);
    }
}

TEST_CASE("distance_iterator", "[single-file]")
{
    SECTION("Includes all adjacent locations for d = 1")
    {
        const coord_def where(50, 50);

        vector<coord_def> points;
        for (distance_iterator di(where, false, true, 1); di; ++di)
            points.push_back(*di);

        // Top to bottom, left to right.
        const vector<coord_def> expected = {
            coord_def(49, 49),
            coord_def(49, 50),
            coord_def(49, 51),
            coord_def(50, 49),
            coord_def(50, 51),
            coord_def(51, 49),
            coord_def(51, 50),
            coord_def(51, 51),
        };

        REQUIRE(points == expected);
    }

    SECTION("Includes the centre if specified")
    {
        const coord_def where(50, 50);

        vector<coord_def> points;
        for (distance_iterator di(where, false, false, 1); di; ++di)
            points.push_back(*di);

        // Centre first, then top to bottom, left to right.
        const vector<coord_def> expected = {
            coord_def(50, 50),
            coord_def(49, 49),
            coord_def(49, 50),
            coord_def(49, 51),
            coord_def(50, 49),
            coord_def(50, 51),
            coord_def(51, 49),
            coord_def(51, 50),
            coord_def(51, 51),
        };

        REQUIRE(points == expected);
    }
}