File: api_geospatial.cc

package info (click to toggle)
xapian-core 1.4.3-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 21,224 kB
  • sloc: cpp: 113,806; ansic: 8,723; sh: 4,433; perl: 836; makefile: 567; tcl: 317; python: 40
file content (431 lines) | stat: -rw-r--r-- 12,192 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/** @file api_geospatial.cc
 * @brief Tests of geospatial functionality.
 */
/* Copyright 2008 Lemur Consulting Ltd
 * Copyright 2010,2011 Richard Boulton
 * Copyright 2012,2016 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <config.h>
#include "api_geospatial.h"
#include <xapian.h>

#include "apitest.h"
#include "testsuite.h"
#include "testutils.h"

using namespace std;
using namespace Xapian;

// #######################################################################
// # Tests start here

static void
builddb_coords1(Xapian::WritableDatabase &db, const string &)
{
    Xapian::LatLongCoord coord1(10, 10);
    Xapian::LatLongCoord coord2(20, 10);
    Xapian::LatLongCoord coord3(30, 10);

    Xapian::Document doc;
    doc.add_value(0, coord1.serialise());
    db.add_document(doc);

    doc = Xapian::Document();
    doc.add_value(0, coord2.serialise());
    db.add_document(doc);

    doc = Xapian::Document();
    doc.add_value(0, coord3.serialise());
    db.add_document(doc);
}

/// Test behaviour of the LatLongDistancePostingSource
DEFINE_TESTCASE(latlongpostingsource1, backend && writable && !remote && !inmemory) {
    Xapian::Database db = get_database("coords1", builddb_coords1, "");
    Xapian::LatLongCoord coord1(10, 10);
    Xapian::LatLongCoord coord2(20, 10);
    Xapian::LatLongCoord coord3(30, 10);

    Xapian::GreatCircleMetric metric;
    Xapian::LatLongCoords centre;
    centre.append(coord1);
    double coorddist = metric(coord1, coord2);
    TEST_EQUAL_DOUBLE(coorddist, metric(coord2, coord3));

    // Test a search with no range restriction.
    {
	Xapian::LatLongDistancePostingSource ps(0, coord1, metric);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);
	TEST_EQUAL(ps.get_docid(), 1);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist * 2));
	TEST_EQUAL(ps.get_docid(), 3);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with no range restriction and implicit metric.
    {
	Xapian::LatLongDistancePostingSource ps(0, coord1);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);
	TEST_EQUAL(ps.get_docid(), 1);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist * 2));
	TEST_EQUAL(ps.get_docid(), 3);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a tight range restriction
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, metric, coorddist * 0.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a tight range restriction and implicit metric.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, coorddist * 0.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a looser range restriction
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, metric, coorddist);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a looser range restriction and implicit metric.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, coorddist);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a looser range restriction, but not enough to return
    // the next document.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, metric, coorddist * 1.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a looser range restriction, but not enough to return
    // the next document and implicit metric.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, coorddist * 1.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a loose enough range restriction that all docs should
    // be returned.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, metric, coorddist * 2.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist * 2));
	TEST_EQUAL(ps.get_docid(), 3);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    // Test a search with a loose enough range restriction that all docs should
    // be returned and implicit metric.
    {
	Xapian::LatLongDistancePostingSource ps(0, centre, coorddist * 2.5);
	ps.init(db);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1.0);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist));
	TEST_EQUAL(ps.get_docid(), 2);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), false);
	TEST_EQUAL_DOUBLE(ps.get_weight(), 1000.0 / (1000.0 + coorddist * 2));
	TEST_EQUAL(ps.get_docid(), 3);

	ps.next(0.0);
	TEST_EQUAL(ps.at_end(), true);
    }

    return true;
}

// Test various methods of LatLongCoord and LatLongCoords
DEFINE_TESTCASE(latlongcoords1, !backend) {
    LatLongCoord c1(0, 0);
    LatLongCoord c2(1, 0);
    LatLongCoord c3(1, 0);
    LatLongCoord c4(0, 1);

    // Test comparison
    TEST_NOT_EQUAL(c1.get_description(), c2.get_description());
    // Exactly one of these inequalities should be true.
    TEST((c1 < c2) ^ (c2 < c1));
    TEST_EQUAL(c2.get_description(), c3.get_description());
    TEST(!(c2 < c3) && !(c3 < c2));
    TEST_NOT_EQUAL(c3.get_description(), c4.get_description());
    // Exactly one of these inequalities should be true.  This is a regression
    // test for bug found prior to 1.3.0.
    TEST((c3 < c4) ^ (c4 < c3));

    // Test serialisation
    std::string s1 = c1.serialise();
    LatLongCoord c5;
    c4.unserialise(s1);
    TEST(!(c1 < c4 || c4 < c1));
    const char * ptr = s1.data();
    const char * end = ptr + s1.size();
    c5.unserialise(&ptr, end);
    TEST_EQUAL(c1.get_description(), c4.get_description());
    TEST_EQUAL(c1.get_description(), "Xapian::LatLongCoord(0, 0)");
    TEST_EQUAL(ptr, end);

    // Test uninitialised iterator constructor
    LatLongCoordsIterator i1;

    // Test building a set of LatLongCoords
    LatLongCoords g1(c1);
    TEST(!g1.empty());
    TEST_EQUAL(g1.size(), 1);
    TEST_EQUAL(g1.get_description(), "Xapian::LatLongCoords((0, 0))");
    g1.append(c2);
    TEST_EQUAL(g1.size(), 2);
    TEST_EQUAL(g1.get_description(), "Xapian::LatLongCoords((0, 0), (1, 0))");

    // Test iterating through a set of LatLongCoords
    i1 = g1.begin();
    TEST(i1 != g1.end());
    TEST_EQUAL((*i1).serialise(), c1.serialise());
    TEST_EQUAL((*(i1++)).serialise(), c1.serialise());
    TEST(i1 != g1.end());
    TEST_EQUAL((*i1).serialise(), c2.serialise());
    i1 = g1.begin();
    TEST_EQUAL((*(++i1)).serialise(), c2.serialise());
    TEST(i1 != g1.end());
    ++i1;
    TEST(i1 == g1.end());

    // Test that duplicates are allowed in the list of coordinates, now.
    g1.append(c3);
    TEST_EQUAL(g1.size(), 3);
    TEST_EQUAL(g1.get_description(), "Xapian::LatLongCoords((0, 0), (1, 0), (1, 0))");

    // Test building an empty LatLongCoords
    LatLongCoords g2;
    TEST(g2.empty());
    TEST_EQUAL(g2.size(), 0);
    TEST_EQUAL(g2.get_description(), "Xapian::LatLongCoords()");
    TEST(g2.begin() == g2.end());

    return true;
}

// Test various methods of LatLongMetric
DEFINE_TESTCASE(latlongmetric1, !backend) {
    LatLongCoord c1(0, 0);
    LatLongCoord c2(1, 0);
    Xapian::GreatCircleMetric m1;
    double d1 = m1(c1, c2);
    TEST_REL(d1, >, 111226.0);
    TEST_REL(d1, <, 111227.0);

    // Let's make another metric, this time using the radius of mars, so
    // distances should be quite a bit smaller.
    Xapian::GreatCircleMetric m2(3310000);
    double d2 = m2(c1, c2);
    TEST_REL(d2, >, 57770.0);
    TEST_REL(d2, <, 57771.0);

    // Check serialise and unserialise.
    Xapian::Registry registry;
    std::string s1 = m2.serialise();
    const Xapian::LatLongMetric * m3;
    m3 = registry.get_lat_long_metric(m2.name());
    TEST(m3 != NULL);
    m3 = m3->unserialise(s1);
    double d3 = (*m3)(c1, c2);
    TEST_EQUAL_DOUBLE(d2, d3);

    delete m3;

    return true;
}

// Test LatLongMetric on lists of coords.
DEFINE_TESTCASE(latlongmetric2, !backend) {
    LatLongCoord c1(0, 0);
    LatLongCoord c2(1, 0);
    LatLongCoords cl1(c1);
    LatLongCoords cl2(c2);
    string c2_str = c2.serialise();
    string cl2_str = cl2.serialise();
    TEST_EQUAL(c2_str, cl2_str);

    LatLongCoord c2_check(5, 5);
    c2_check.unserialise(c2_str);
    TEST_EQUAL(c2_check.latitude, c2.latitude);
    TEST_EQUAL(c2_check.longitude, c2.longitude);

    Xapian::GreatCircleMetric m1;
    double d1 = m1(c1, c2);
    double dl1 = m1(cl1, cl2);
    TEST_EQUAL(d1, dl1);
    double d1_str = m1(cl1, c2_str);
    TEST_EQUAL(d1, d1_str);

    return true;
}

// Test a LatLongDistanceKeyMaker directly.
DEFINE_TESTCASE(latlongkeymaker1, !backend) {
    Xapian::GreatCircleMetric m1(3310000);
    LatLongCoord c1(0, 0);
    LatLongCoord c2(1, 0);
    LatLongCoord c3(2, 0);
    LatLongCoord c4(3, 0);

    LatLongCoords g1(c1);
    g1.append(c2);

    LatLongDistanceKeyMaker keymaker(0, g1, m1);
    Xapian::Document doc1;
    doc1.add_value(0, g1.serialise());
    Xapian::Document doc2;
    doc2.add_value(0, c3.serialise());
    Xapian::Document doc3;
    doc3.add_value(0, c4.serialise());
    Xapian::Document doc4;

    std::string k1 = keymaker(doc1);
    std::string k2 = keymaker(doc2);
    std::string k3 = keymaker(doc3);
    std::string k4 = keymaker(doc4);
    TEST_REL(k1, <, k2);
    TEST_REL(k2, <, k3);
    TEST_REL(k3, <, k4);

    LatLongDistanceKeyMaker keymaker2(0, g1, m1, 0);
    std::string k3b = keymaker2(doc3);
    std::string k4b = keymaker2(doc4);
    TEST_EQUAL(k3, k3b);
    TEST_REL(k3b, >, k4b);

    return true;
}