-
Notifications
You must be signed in to change notification settings - Fork 280
/
coordinate_test.cpp
382 lines (345 loc) · 13.1 KB
/
coordinate_test.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "catch/catch.hpp"
#include "coordinates.h"
#include "coordinate_conversions.h"
#include "cata_generators.h"
#include "stringmaker.h"
constexpr int num_trials = 5;
static_assert( point::dimension == 2, "" );
static_assert( tripoint::dimension == 3, "" );
static_assert( point_abs_omt::dimension == 2, "" );
static_assert( tripoint_abs_omt::dimension == 3, "" );
TEST_CASE( "coordinate_strings", "[point][coords]" )
{
CHECK( point_abs_omt( point( 3, 4 ) ).to_string() == "(3,4)" );
SECTION( "coord_point_matches_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
point_abs_ms cp( p );
CHECK( p.to_string() == cp.to_string() );
}
}
TEST_CASE( "coordinate_operations", "[point][coords]" )
{
SECTION( "construct_from_raw_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
point_abs_ms cp( p );
CHECK( cp.x() == p.x );
CHECK( cp.y() == p.y );
}
SECTION( "construct_from_raw_tripoint" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
tripoint_abs_ms cp( p );
CHECK( cp.x() == p.x );
CHECK( cp.y() == p.y );
CHECK( cp.z() == p.z );
}
SECTION( "construct_from_values" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
{
point_abs_ms cp( p.x, p.y );
CHECK( cp.x() == p.x );
CHECK( cp.y() == p.y );
}
{
tripoint_abs_ms cp( p.x, p.y, p.z );
CHECK( cp.x() == p.x );
CHECK( cp.y() == p.y );
CHECK( cp.z() == p.z );
}
}
SECTION( "addition" ) {
tripoint t0 = GENERATE( take( num_trials, random_tripoints() ) );
point p0 = t0.xy();
point p1 = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p0, p1 );
tripoint_abs_ms abst0( t0 );
point_abs_ms abs0( p0 );
point_rel_ms rel0( p0 );
point_rel_ms rel1( p1 );
SECTION( "rel + rel -> rel" ) {
point_rel_ms sum = rel0 + rel1;
CHECK( sum.raw() == p0 + p1 );
}
SECTION( "abs + rel -> abs" ) {
point_abs_ms sum = abs0 + rel1;
CHECK( sum.raw() == p0 + p1 );
tripoint_abs_ms sum_t = abst0 + rel1;
CHECK( sum_t.raw() == t0 + p1 );
}
SECTION( "abs + raw -> abs" ) {
point_abs_ms sum = abs0 + p1;
CHECK( sum.raw() == p0 + p1 );
}
SECTION( "rel + abs -> abs" ) {
point_abs_ms sum = rel1 + abs0;
CHECK( sum.raw() == p0 + p1 );
}
SECTION( "rel += rel" ) {
rel0 += rel1;
CHECK( rel0.raw() == p0 + p1 );
}
SECTION( "abs += rel" ) {
abs0 += rel1;
CHECK( abs0.raw() == p0 + p1 );
}
SECTION( "abs += raw" ) {
abs0 += p1;
CHECK( abs0.raw() == p0 + p1 );
}
}
SECTION( "subtraction" ) {
tripoint t0 = GENERATE( take( num_trials, random_tripoints() ) );
point p0 = t0.xy();
point p1 = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p0, p1 );
tripoint_abs_ms abst0( t0 );
point_abs_ms abs0( p0 );
point_abs_ms abs1( p1 );
point_rel_ms rel0( p0 );
point_rel_ms rel1( p1 );
SECTION( "rel - rel -> rel" ) {
point_rel_ms diff = rel0 - rel1;
CHECK( diff.raw() == p0 - p1 );
}
SECTION( "abs - rel -> abs" ) {
point_abs_ms diff = abs0 - rel1;
CHECK( diff.raw() == p0 - p1 );
}
SECTION( "abs - raw -> abs" ) {
point_abs_ms diff = abs0 - p1;
CHECK( diff.raw() == p0 - p1 );
}
SECTION( "abs - abs -> rel" ) {
point_rel_ms diff0 = abs0 - abs1;
CHECK( diff0.raw() == p0 - p1 );
tripoint_rel_ms diff1 = abst0 - abs1;
CHECK( diff1.raw() == t0 - p1 );
}
SECTION( "rel -= rel" ) {
rel0 -= rel1;
CHECK( rel0.raw() == p0 - p1 );
}
SECTION( "abs -= rel" ) {
abs0 -= rel1;
CHECK( abs0.raw() == p0 - p1 );
}
SECTION( "abs -= raw" ) {
abs0 -= p1;
CHECK( abs0.raw() == p0 - p1 );
}
}
}
TEST_CASE( "coordinate_comparison", "[point][coords]" )
{
SECTION( "compare_points" ) {
point p0 = GENERATE( take( num_trials, random_points() ) );
point p1 = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p0, p1 );
point_rel_ms cp0( p0 );
point_rel_ms cp1( p1 );
CAPTURE( cp0, cp1 );
CHECK( ( p0 < p1 ) == ( cp0 < cp1 ) );
CHECK( ( p0 == p1 ) == ( cp0 == cp1 ) );
CHECK( cp0 == cp0 );
CHECK( !( cp0 != cp0 ) );
}
SECTION( "compare_tripoints" ) {
tripoint p0 = GENERATE( take( num_trials, random_tripoints() ) );
tripoint p1 = GENERATE( take( num_trials, random_tripoints() ) );
CAPTURE( p0, p1 );
tripoint_rel_ms cp0( p0 );
tripoint_rel_ms cp1( p1 );
CAPTURE( cp0, cp1 );
CHECK( ( p0 < p1 ) == ( cp0 < cp1 ) );
CHECK( ( p0 == p1 ) == ( cp0 == cp1 ) );
CHECK( cp0 == cp0 );
CHECK( !( cp0 != cp0 ) );
}
}
TEST_CASE( "coordinate_hash", "[point][coords]" )
{
SECTION( "point_hash" ) {
point p = GENERATE( take( num_trials, random_points() ) );
point_abs_ms cp( p );
CHECK( std::hash<point_abs_ms>()( cp ) == std::hash<point>()( p ) );
}
SECTION( "tripoint_hash" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
tripoint_abs_ms cp( p );
CHECK( std::hash<tripoint_abs_ms>()( cp ) == std::hash<tripoint>()( p ) );
}
}
TEST_CASE( "coordinate_conversion_consistency", "[point][coords]" )
{
// Verifies that the new coord_point-based conversions yield the same
// results as the legacy conversion functions.
SECTION( "omt_to_om_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_om new_conversion = project_to<coords::om>( point_abs_omt( p ) );
point old_conversion = omt_to_om_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "omt_to_om_tripoint" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
CAPTURE( p );
tripoint_abs_om new_conversion = project_to<coords::om>( tripoint_abs_omt( p ) );
tripoint old_conversion = omt_to_om_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "omt_to_om_remain_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_om new_conversion;
point_om_omt remainder;
std::tie( new_conversion, remainder ) = project_remain<coords::om>( point_abs_omt( p ) );
point old_conversion = omt_to_om_remain( p );
CHECK( old_conversion == new_conversion.raw() );
CHECK( p == remainder.raw() );
}
SECTION( "sm_to_omt_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_omt new_conversion = project_to<coords::omt>( point_abs_sm( p ) );
point old_conversion = sm_to_omt_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "sm_to_omt_remain_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_omt new_conversion;
point_omt_sm remainder;
std::tie( new_conversion, remainder ) = project_remain<coords::omt>( point_abs_sm( p ) );
point old_conversion = sm_to_omt_remain( p );
CHECK( old_conversion == new_conversion.raw() );
CHECK( p == remainder.raw() );
}
SECTION( "sm_to_om_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_om new_conversion = project_to<coords::om>( point_abs_sm( p ) );
point old_conversion = sm_to_om_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "sm_to_om_remain_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_om new_conversion;
point_om_sm remainder;
std::tie( new_conversion, remainder ) = project_remain<coords::om>( point_abs_sm( p ) );
point old_conversion = sm_to_om_remain( p );
CHECK( old_conversion == new_conversion.raw() );
CHECK( p == remainder.raw() );
}
SECTION( "omt_to_sm_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_sm new_conversion = project_to<coords::sm>( point_abs_omt( p ) );
point old_conversion = omt_to_sm_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "om_to_sm_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_sm new_conversion = project_to<coords::sm>( point_abs_om( p ) );
point old_conversion = om_to_sm_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "ms_to_sm_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_sm new_conversion = project_to<coords::sm>( point_abs_ms( p ) );
point old_conversion = ms_to_sm_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "sm_to_ms_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_ms new_conversion = project_to<coords::ms>( point_abs_sm( p ) );
point old_conversion = sm_to_ms_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "ms_to_omt_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_omt new_conversion = project_to<coords::omt>( point_abs_ms( p ) );
point old_conversion = ms_to_omt_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
SECTION( "ms_to_omt_remain_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_omt new_conversion;
point_omt_ms remainder;
std::tie( new_conversion, remainder ) = project_remain<coords::omt>( point_abs_ms( p ) );
point old_conversion = ms_to_omt_remain( p );
CHECK( old_conversion == new_conversion.raw() );
CHECK( p == remainder.raw() );
}
SECTION( "omt_to_seg_tripoint" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
CAPTURE( p );
tripoint_abs_seg new_conversion = project_to<coords::seg>( tripoint_abs_omt( p ) );
tripoint old_conversion = omt_to_seg_copy( p );
CHECK( old_conversion == new_conversion.raw() );
}
}
TEST_CASE( "combine_is_opposite_of_remain", "[point][coords]" )
{
SECTION( "point_point" ) {
point p = GENERATE( take( num_trials, random_points() ) );
CAPTURE( p );
point_abs_sm orig( p );
point_abs_om quotient;
point_om_sm remainder;
std::tie( quotient, remainder ) = project_remain<coords::om>( orig );
point_abs_sm recombined = project_combine( quotient, remainder );
CHECK( recombined == orig );
}
SECTION( "tripoint_point" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
CAPTURE( p );
tripoint_abs_sm orig( p );
tripoint_abs_om quotient;
point_om_sm remainder;
std::tie( quotient, remainder ) = project_remain<coords::om>( orig );
tripoint_abs_sm recombined = project_combine( quotient, remainder );
CHECK( recombined == orig );
}
SECTION( "point_tripoint" ) {
tripoint p = GENERATE( take( num_trials, random_tripoints() ) );
CAPTURE( p );
tripoint_abs_sm orig( p );
point_abs_om quotient;
tripoint_om_sm remainder;
std::tie( quotient, remainder ) = project_remain<coords::om>( orig );
tripoint_abs_sm recombined = project_combine( quotient, remainder );
CHECK( recombined == orig );
}
}
TEST_CASE( "coord_point_distances", "[point][coords]" )
{
point_abs_omt p0;
point_abs_omt p1( 10, 10 );
tripoint_abs_omt t0;
tripoint_abs_omt t1( 10, 10, 10 );
SECTION( "square" ) {
CHECK( square_dist( p0, p1 ) == 10 );
CHECK( square_dist( t0, t1 ) == 10 );
}
SECTION( "trig" ) {
CHECK( trig_dist( p0, p1 ) == 14 ); // int(10*sqrt(2))
CHECK( trig_dist( t0, t1 ) == 17 ); // int(10*sqrt(3))
}
SECTION( "manhattan" ) {
CHECK( manhattan_dist( p0, p1 ) == 20 );
}
}
TEST_CASE( "coord_point_midpoint", "[point][coords]" )
{
point_abs_omt p0( 2, 2 );
point_abs_omt p1( 8, 17 );
tripoint_abs_omt t0( 2, 2, 2 );
tripoint_abs_omt t1( 8, 17, 5 );
CHECK( midpoint( p0, p1 ) == point_abs_omt( 5, 9 ) );
CHECK( midpoint( t0, t1 ) == tripoint_abs_omt( 5, 9, 3 ) );
}