summaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/common/location/geo_location_test.cpp
blob: 31b844d0fc81853b8c615ecb792e9e3e3b4f2dfd (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
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
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <stdio.h>
#include <vespa/searchlib/common/geo_location.h>
#include <vespa/searchlib/common/geo_location_spec.h>
#include <vespa/searchlib/common/geo_location_parser.h>
#include <vespa/vespalib/gtest/gtest.h>

using search::common::GeoLocation;
using search::common::GeoLocationParser;

using Box = search::common::GeoLocation::Box;
using Point = search::common::GeoLocation::Point;
using Range = search::common::GeoLocation::Range;
using Aspect = search::common::GeoLocation::Aspect;

constexpr int32_t plus_inf = std::numeric_limits<int32_t>::max();
constexpr int32_t minus_inf = std::numeric_limits<int32_t>::min();
constexpr uint32_t u32_inf = std::numeric_limits<uint32_t>::max();

bool is_parseable(const char *str) {
    GeoLocationParser parser;
    return parser.parseOldFormat(str);
}

GeoLocation parse(const char *str) {
    GeoLocationParser parser;
    EXPECT_TRUE(parser.parseOldFormat(str));
    return parser.getGeoLocation();
}

TEST(GeoLocationParserTest, malformed_bounding_boxes_are_not_parseable) {
    EXPECT_TRUE(is_parseable("[2,10,20,30,40]"));
    EXPECT_FALSE(is_parseable("[2,10,20,30,40][2,10,20,30,40]"));
    EXPECT_FALSE(is_parseable("[1,10,20,30,40]"));
    EXPECT_FALSE(is_parseable("[3,10,20,30,40]"));
    EXPECT_FALSE(is_parseable("[2, 10, 20, 30, 40]"));
    EXPECT_FALSE(is_parseable("[2,10,20,30,40"));
    EXPECT_FALSE(is_parseable("[2,10,20,30]"));
    EXPECT_FALSE(is_parseable("[10,20,30,40]"));
}

TEST(GeoLocationParserTest, malformed_circles_are_not_parseable) {
    EXPECT_TRUE(is_parseable("(2,10,20,5,0,0,0)"));
    EXPECT_FALSE(is_parseable("(2,10,20,5,0,0,0)(2,10,20,5,0,0,0)"));
    EXPECT_FALSE(is_parseable("(1,10,20,5,0,0,0)"));
    EXPECT_FALSE(is_parseable("(3,10,20,5,0,0,0)"));
    EXPECT_FALSE(is_parseable("(2, 10, 20, 5, 0, 0, 0)"));
    EXPECT_FALSE(is_parseable("(2,10,20,5)"));
    EXPECT_FALSE(is_parseable("(2,10,20,5,0,0,0"));
    EXPECT_FALSE(is_parseable("(2,10,20,5,0,0,0,1000"));
    EXPECT_FALSE(is_parseable("(10,20,5)"));
}

TEST(GeoLocationParserTest, bounding_boxes_can_be_parsed) {
    auto loc = parse("[2,10,20,30,40]");
    EXPECT_EQ(false, loc.has_point);
    EXPECT_EQ(true, loc.bounding_box.active());
    EXPECT_EQ(0u, loc.x_aspect.multiplier);
    EXPECT_EQ(0, loc.point.x);
    EXPECT_EQ(0, loc.point.y);
    EXPECT_EQ(std::numeric_limits<uint32_t>::max(), loc.radius);
    EXPECT_EQ(10, loc.bounding_box.x.low);
    EXPECT_EQ(20, loc.bounding_box.y.low);
    EXPECT_EQ(30, loc.bounding_box.x.high);
    EXPECT_EQ(40, loc.bounding_box.y.high);
}

TEST(GeoLocationParserTest, circles_can_be_parsed) {
    auto loc = parse("(2,10,20,5,0,0,0)");
    EXPECT_EQ(true, loc.has_point);
    EXPECT_EQ(true, loc.bounding_box.active());
    EXPECT_EQ(0u, loc.x_aspect.multiplier);
    EXPECT_EQ(10, loc.point.x);
    EXPECT_EQ(20, loc.point.y);
    EXPECT_EQ(5u, loc.radius);
    EXPECT_EQ(5, loc.bounding_box.x.low);
    EXPECT_EQ(15, loc.bounding_box.y.low);
    EXPECT_EQ(15, loc.bounding_box.x.high);
    EXPECT_EQ(25, loc.bounding_box.y.high);    
}

TEST(GeoLocationParserTest, circles_can_have_aspect_ratio) {
    auto loc = parse("(2,10,20,5,0,0,0,2147483648)");
    EXPECT_EQ(true, loc.has_point);
    EXPECT_EQ(true, loc.bounding_box.active());
    EXPECT_EQ(2147483648u, loc.x_aspect.multiplier);
    EXPECT_EQ(10, loc.point.x);
    EXPECT_EQ(20, loc.point.y);
    EXPECT_EQ(5u, loc.radius);
    EXPECT_EQ(-1, loc.bounding_box.x.low);
    EXPECT_EQ(15, loc.bounding_box.y.low);
    EXPECT_EQ(21, loc.bounding_box.x.high);
    EXPECT_EQ(25, loc.bounding_box.y.high);
}

TEST(GeoLocationParserTest, bounding_box_can_be_specified_after_circle) {
    auto loc = parse("(2,10,20,5,0,0,0)[2,10,20,30,40]");
    EXPECT_EQ(true, loc.has_point);
    EXPECT_EQ(true, loc.bounding_box.active());
    EXPECT_EQ(0u, loc.x_aspect.multiplier);
    EXPECT_EQ(10, loc.point.x);
    EXPECT_EQ(20, loc.point.y);
    EXPECT_EQ(5u, loc.radius);
    EXPECT_EQ(10, loc.bounding_box.x.low);
    EXPECT_EQ(20, loc.bounding_box.y.low);
    EXPECT_EQ(15, loc.bounding_box.x.high);
    EXPECT_EQ(25, loc.bounding_box.y.high);
}

TEST(GeoLocationParserTest, circles_can_be_specified_after_bounding_box) {
    auto loc = parse("[2,10,20,30,40](2,10,20,5,0,0,0)");
    EXPECT_EQ(true, loc.has_point);
    EXPECT_EQ(true, loc.bounding_box.active());
    EXPECT_EQ(0u, loc.x_aspect.multiplier);
    EXPECT_EQ(10, loc.point.x);
    EXPECT_EQ(20, loc.point.y);
    EXPECT_EQ(5u, loc.radius);
    EXPECT_EQ(10, loc.bounding_box.x.low);
    EXPECT_EQ(20, loc.bounding_box.y.low);
    EXPECT_EQ(15, loc.bounding_box.x.high);
    EXPECT_EQ(25, loc.bounding_box.y.high);    
}

TEST(GeoLocationParserTest, santa_search_gives_non_wrapped_bounding_box) {
    auto loc = parse("(2,122163600,89998536,290112,4,2000,0,109704)");
    EXPECT_GE(loc.bounding_box.x.high, loc.bounding_box.x.low);
    EXPECT_GE(loc.bounding_box.y.high, loc.bounding_box.y.low);
}

TEST(GeoLocationParserTest, near_boundary_search_gives_non_wrapped_bounding_box) {
    auto loc1 = parse("(2,2000000000,2000000000,3000000000,0,1,0)");
    EXPECT_GE(loc1.bounding_box.x.high, loc1.bounding_box.x.low);
    EXPECT_GE(loc1.bounding_box.y.high, loc1.bounding_box.y.low);
    EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);
    EXPECT_EQ(std::numeric_limits<int32_t>::max(), loc1.bounding_box.y.high);    

    auto loc2 = parse("(2,-2000000000,-2000000000,3000000000,0,1,0)");
    EXPECT_GE(loc2.bounding_box.x.high, loc2.bounding_box.x.low);
    EXPECT_GE(loc2.bounding_box.y.high, loc2.bounding_box.y.low);
    EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.x.low);
    EXPECT_EQ(std::numeric_limits<int32_t>::min(), loc2.bounding_box.y.low);
}

void check_box(const GeoLocation &location, Box expected)
{
    int32_t lx = expected.x.low;
    int32_t hx = expected.x.high;
    int32_t ly = expected.y.low;
    int32_t hy = expected.y.high;
    EXPECT_TRUE(location.inside_limit(Point{lx,ly}));
    EXPECT_TRUE(location.inside_limit(Point{lx,hy}));
    EXPECT_TRUE(location.inside_limit(Point{hx,ly}));
    EXPECT_TRUE(location.inside_limit(Point{hx,hy}));

    EXPECT_FALSE(location.inside_limit(Point{lx,ly-1}));
    EXPECT_FALSE(location.inside_limit(Point{lx,hy+1}));
    EXPECT_FALSE(location.inside_limit(Point{lx-1,ly}));
    EXPECT_FALSE(location.inside_limit(Point{lx-1,hy}));
    EXPECT_FALSE(location.inside_limit(Point{hx,ly-1}));
    EXPECT_FALSE(location.inside_limit(Point{hx,hy+1}));
    EXPECT_FALSE(location.inside_limit(Point{hx+1,ly}));
    EXPECT_FALSE(location.inside_limit(Point{hx+1,hy}));

    EXPECT_FALSE(location.inside_limit(Point{plus_inf,plus_inf}));
    EXPECT_FALSE(location.inside_limit(Point{minus_inf,minus_inf}));
}

TEST(GeoLocationTest, invalid_location) {
    GeoLocation invalid;
    EXPECT_FALSE(invalid.valid());
    EXPECT_FALSE(invalid.has_radius());
    EXPECT_FALSE(invalid.can_limit());
    EXPECT_FALSE(invalid.has_point);
    EXPECT_FALSE(invalid.bounding_box.active());
    EXPECT_FALSE(invalid.x_aspect.active());

    EXPECT_EQ(invalid.sq_distance_to(Point{0,0}), 0);
    EXPECT_EQ(invalid.sq_distance_to(Point{999999,999999}), 0);
    EXPECT_EQ(invalid.sq_distance_to(Point{-999999,-999999}), 0);
    EXPECT_EQ(invalid.sq_distance_to(Point{plus_inf,plus_inf}), 0);
    EXPECT_EQ(invalid.sq_distance_to(Point{minus_inf,minus_inf}), 0);

    EXPECT_TRUE(invalid.inside_limit(Point{0,0}));
    EXPECT_TRUE(invalid.inside_limit(Point{999999,999999}));
    EXPECT_TRUE(invalid.inside_limit(Point{-999999,-999999}));
    EXPECT_TRUE(invalid.inside_limit(Point{plus_inf,plus_inf}));
    EXPECT_TRUE(invalid.inside_limit(Point{minus_inf,minus_inf}));
}

TEST(GeoLocationTest, point_location) {
    GeoLocation location(Point{300,-400});
    EXPECT_TRUE(location.valid());
    EXPECT_FALSE(location.has_radius());
    EXPECT_FALSE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_FALSE(location.bounding_box.active());
    EXPECT_FALSE(location.x_aspect.active());

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{300,-400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{300,400}), 640000);

    EXPECT_TRUE(location.inside_limit(Point{0,0}));
    EXPECT_TRUE(location.inside_limit(Point{999999,999999}));
    EXPECT_TRUE(location.inside_limit(Point{-999999,-999999}));
    EXPECT_TRUE(location.inside_limit(Point{plus_inf,plus_inf}));
    EXPECT_TRUE(location.inside_limit(Point{minus_inf,minus_inf}));
}

TEST(GeoLocationTest, point_and_radius) {
    GeoLocation location(Point{300,-400}, 500);
    EXPECT_TRUE(location.valid());
    EXPECT_TRUE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_FALSE(location.x_aspect.active());

    EXPECT_EQ(location.radius, 500);

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{300,-400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{300,400}), 640000);

    EXPECT_TRUE(location.inside_limit(Point{0,0}));
    EXPECT_TRUE(location.inside_limit(Point{-200,-400}));
    EXPECT_TRUE(location.inside_limit(Point{800,-400}));
    EXPECT_TRUE(location.inside_limit(Point{300,-400}));
    EXPECT_TRUE(location.inside_limit(Point{300,100}));
    EXPECT_TRUE(location.inside_limit(Point{300,-900}));

    check_box(location, Box{Range{0,600},{-800,0}});
}

TEST(GeoLocationTest, point_and_aspect) {
    GeoLocation location(Point{600,400}, Aspect{0.5});
    // same: GeoLocation location(Point{600,400}, Aspect{1ul << 31});
    EXPECT_TRUE(location.valid());
    EXPECT_FALSE(location.has_radius());
    EXPECT_FALSE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_FALSE(location.bounding_box.active());
    EXPECT_TRUE(location.x_aspect.active());
    EXPECT_EQ(location.x_aspect.multiplier, 1ul << 31);

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{600,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{1200,800}), 500*500);

    EXPECT_TRUE(location.inside_limit(Point{0,0}));
    EXPECT_TRUE(location.inside_limit(Point{999999,999999}));
    EXPECT_TRUE(location.inside_limit(Point{-999999,-999999}));
    EXPECT_TRUE(location.inside_limit(Point{plus_inf,plus_inf}));
    EXPECT_TRUE(location.inside_limit(Point{minus_inf,minus_inf}));
}

TEST(GeoLocationTest, point_radius_and_aspect) {
    GeoLocation location(Point{1200,400}, 500, Aspect{0.25});
    EXPECT_TRUE(location.valid());
    EXPECT_TRUE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_TRUE(location.x_aspect.active());
    EXPECT_EQ(location.x_aspect.multiplier, 1ul << 30);

    EXPECT_EQ(location.radius, 500);

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{1200,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{1240,400}), 100);

    EXPECT_TRUE(location.inside_limit(Point{1200,400}));
    EXPECT_TRUE(location.inside_limit(Point{0,0}));
    EXPECT_TRUE(location.inside_limit(Point{2400,0}));
    EXPECT_TRUE(location.inside_limit(Point{2400,800}));
    EXPECT_TRUE(location.inside_limit(Point{0,800}));
    // note: must be 4 outside since 3*0.25 may be truncated to 0
    EXPECT_FALSE(location.inside_limit(Point{-4,0}));
    EXPECT_FALSE(location.inside_limit(Point{-4,800}));
    EXPECT_FALSE(location.inside_limit(Point{2404,0}));
    EXPECT_FALSE(location.inside_limit(Point{2404,800}));
    EXPECT_FALSE(location.inside_limit(Point{2400,-1}));
    EXPECT_FALSE(location.inside_limit(Point{2400,801}));
    EXPECT_FALSE(location.inside_limit(Point{0,-1}));
    EXPECT_FALSE(location.inside_limit(Point{0,801}));
    EXPECT_FALSE(location.inside_limit(Point{plus_inf,plus_inf}));
    EXPECT_FALSE(location.inside_limit(Point{minus_inf,minus_inf}));
}

TEST(GeoLocationTest, box_location) {
    Box mybox{Range{300,350},Range{400,450}};
    GeoLocation location(mybox);
    EXPECT_TRUE(location.valid());
    EXPECT_FALSE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_FALSE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_FALSE(location.x_aspect.active());

    // currently does not measure distance outside box:
    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{300,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{350,450}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{450,550}), 0);

    EXPECT_TRUE(location.inside_limit(Point{333,444}));
    EXPECT_FALSE(location.inside_limit(Point{0,0}));
    check_box(location, mybox);
}

TEST(GeoLocationTest, box_and_point) {
    Box mybox{Range{287,343},Range{366,401}};
    GeoLocation location(mybox, Point{300,400});
    EXPECT_TRUE(location.valid());
    EXPECT_FALSE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_FALSE(location.x_aspect.active());

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{300,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{300,423}), 23*23);

    check_box(location, mybox);
}

TEST(GeoLocationTest, box_point_and_aspect) {
    Box mybox{Range{-1000,350},Range{-1000,600}};
    GeoLocation location(mybox, Point{600,400}, Aspect{0.5});
    EXPECT_TRUE(location.valid());
    EXPECT_FALSE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_TRUE(location.x_aspect.active());

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{600,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{600,407}), 7*7);
    EXPECT_EQ(location.sq_distance_to(Point{614,400}), 7*7);

    check_box(location, mybox);
}

TEST(GeoLocationTest, box_point_and_radius) {
    Box mybox{Range{-1000,350},Range{-1000,600}};
    GeoLocation location(mybox, Point{300,400}, 500);
    EXPECT_TRUE(location.valid());
    EXPECT_TRUE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_FALSE(location.x_aspect.active());

    EXPECT_EQ(location.radius, 500);

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{300,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{300,423}), 23*23);

    EXPECT_EQ(location.bounding_box.x.low, -200);
    EXPECT_EQ(location.bounding_box.y.low, -100);
    EXPECT_EQ(location.bounding_box.x.high, 350);
    EXPECT_EQ(location.bounding_box.y.high, 600);
}

TEST(GeoLocationTest, box_point_radius_and_aspect) {
    Box mybox{Range{-1000,650},Range{-1000,700}};
    GeoLocation location(mybox, Point{600,400}, 500, Aspect{0.5});
    EXPECT_TRUE(location.valid());
    EXPECT_TRUE(location.has_radius());
    EXPECT_TRUE(location.can_limit());
    EXPECT_TRUE(location.has_point);
    EXPECT_TRUE(location.bounding_box.active());
    EXPECT_TRUE(location.x_aspect.active());

    EXPECT_EQ(location.radius, 500);

    EXPECT_EQ(location.sq_distance_to(Point{0,0}), 500*500);
    EXPECT_EQ(location.sq_distance_to(Point{600,400}), 0);
    EXPECT_EQ(location.sq_distance_to(Point{600,407}), 7*7);
    EXPECT_EQ(location.sq_distance_to(Point{614,400}), 7*7);

    EXPECT_GE(location.bounding_box.x.low, -402);
    EXPECT_LE(location.bounding_box.x.low, -400);
    EXPECT_EQ(location.bounding_box.y.low, -100);
    EXPECT_EQ(location.bounding_box.x.high, 650);
    EXPECT_EQ(location.bounding_box.y.high, 700);
}

GTEST_MAIN_RUN_ALL_TESTS()