Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-geo-in-rectangle-border.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2011 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <gcutter.h>
20 #include <glib/gstdio.h>
21 
22 #include "../lib/grn-assertions.h"
23 
24 #include <str.h>
25 #include <geo.h>
26 
27 #define get(name) grn_ctx_get(context, name, strlen(name))
28 
29 void data_cursor(void);
30 void test_cursor(gconstpointer data);
31 
32 static gchar *tmp_directory;
33 
34 static grn_ctx *context;
35 static grn_obj *database, *points, *short_degree_column;
36 static grn_obj *location_index_column, *result;
37 
38 void
40 {
41  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
42  "geo-in-rectangle-border",
43  NULL);
44 }
45 
46 void
48 {
49  g_free(tmp_directory);
50 }
51 
52 static void
53 remove_tmp_directory(void)
54 {
55  cut_remove_path(tmp_directory, NULL);
56 }
57 
58 static void
59 load_data(void)
60 {
61  assert_send_commands(cut_get_fixture_data_string("ddl.grn", NULL));
62  assert_send_command(cut_get_fixture_data_string("data.grn", NULL));
63 }
64 
65 void
66 cut_setup(void)
67 {
68  const gchar *database_path;
69 
70  cut_set_fixture_data_dir(grn_test_get_base_dir(),
71  "fixtures",
72  "geo",
73  NULL);
74 
75  remove_tmp_directory();
76  g_mkdir_with_parents(tmp_directory, 0700);
77 
78  context = g_new0(grn_ctx, 1);
79  grn_ctx_init(context, 0);
80 
81  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
82  database = grn_db_create(context, database_path, NULL);
83 
84  load_data();
85 
86  points = get("Points");
87  short_degree_column = get("Points.short_degree");
88  location_index_column = get("Locations.point");
89 
90  result = grn_table_create(context,
91  NULL, 0,
92  NULL,
94  points, NULL);
95 }
96 
97 void
99 {
100  grn_obj_unlink(context, result);
101  grn_obj_unlink(context, location_index_column);
102  grn_obj_unlink(context, short_degree_column);
103  grn_obj_unlink(context, points);
104  grn_obj_close(context, database);
105  grn_ctx_fin(context);
106  g_free(context);
107 
108  remove_tmp_directory();
109 }
110 
111 #define ADD_DATA(label, expected, top, left, bottom, right) \
112  gcut_add_datum(label, \
113  "expected", G_TYPE_POINTER, expected, gcut_list_string_free, \
114  "top", G_TYPE_INT, top, \
115  "left", G_TYPE_INT, left, \
116  "bottom", G_TYPE_INT, bottom, \
117  "right", G_TYPE_INT, right, \
118  NULL)
119 
120 static void
121 data_cursor_all_bottom_left(void)
122 {
123  ADD_DATA("all - bottom left - bottom left",
124  gcut_list_string_new(
125  "(03,03)", "(03,04)", "(03,05)", "(03,06)", "(03,07)", "(03,08)",
126  "(04,03)", "(04,04)", "(04,05)", "(04,06)", "(04,07)", "(04,08)",
127  "(05,03)", "(05,04)", "(05,05)", "(05,06)", "(05,07)", "(05,08)",
128  "(06,03)", "(06,04)", "(06,05)", "(06,06)", "(06,07)", "(06,08)",
129  "(07,03)", "(07,04)", "(07,05)", "(07,06)", "(07,07)", "(07,08)",
130  "(08,03)", "(08,04)", "(08,05)", "(08,06)", "(08,07)", "(08,08)",
131  NULL),
132  8, 3,
133  3, 8);
134 
135  ADD_DATA("all - bottom left - top left",
136  gcut_list_string_new(
137  "(07,03)", "(07,04)", "(07,05)", "(07,06)", "(07,07)", "(07,08)",
138  "(08,03)", "(08,04)", "(08,05)", "(08,06)", "(08,07)", "(08,08)",
139  NULL),
140  8, 3,
141  7, 8);
142 
143  ADD_DATA("all - bottom left - bottom right",
144  gcut_list_string_new("(03,07)", "(03,08)",
145  "(04,07)", "(04,08)",
146  "(05,07)", "(05,08)",
147  "(06,07)", "(06,08)",
148  "(07,07)", "(07,08)",
149  "(08,07)", "(08,08)",
150  NULL),
151  8, 7,
152  3, 8);
153 }
154 
155 static void
156 data_cursor_all_top_left(void)
157 {
158  ADD_DATA("all - top left - top left",
159  gcut_list_string_new(
160  "(07,03)", "(07,04)", "(07,05)", "(07,06)", "(07,07)", "(07,08)",
161  "(08,03)", "(08,04)", "(08,05)", "(08,06)", "(08,07)", "(08,08)",
162  "(09,03)", "(09,04)", "(09,05)", "(09,06)", "(09,07)", "(09,08)",
163  "(10,03)", "(10,04)", "(10,05)", "(10,06)", "(10,07)", "(10,08)",
164  "(11,03)", "(11,04)", "(11,05)", "(11,06)", "(11,07)", "(11,08)",
165  "(12,03)", "(12,04)", "(12,05)", "(12,06)", "(12,07)", "(12,08)",
166  NULL),
167  12, 3,
168  7, 8);
169 
170  ADD_DATA("all - top left - top right",
171  gcut_list_string_new("(07,07)", "(07,08)",
172  "(08,07)", "(08,08)",
173  "(09,07)", "(09,08)",
174  "(10,07)", "(10,08)",
175  "(11,07)", "(11,08)",
176  "(12,07)", "(12,08)",
177  NULL),
178  12, 7,
179  7, 8);
180 }
181 
182 static void
183 data_cursor_all_bottom_right(void)
184 {
185  ADD_DATA("all - bottom right - bottom right",
186  gcut_list_string_new(
187  "(03,07)", "(03,08)", "(03,09)", "(03,10)", "(03,11)", "(03,12)",
188  "(04,07)", "(04,08)", "(04,09)", "(04,10)", "(04,11)", "(04,12)",
189  "(05,07)", "(05,08)", "(05,09)", "(05,10)", "(05,11)", "(05,12)",
190  "(06,07)", "(06,08)", "(06,09)", "(06,10)", "(06,11)", "(06,12)",
191  "(07,07)", "(07,08)", "(07,09)", "(07,10)", "(07,11)", "(07,12)",
192  "(08,07)", "(08,08)", "(08,09)", "(08,10)", "(08,11)", "(08,12)",
193  NULL),
194  8, 7,
195  3, 12);
196 
197  ADD_DATA("all - bottom right - top right",
198  gcut_list_string_new(
199  "(07,07)", "(07,08)", "(07,09)", "(07,10)", "(07,11)", "(07,12)",
200  "(08,07)", "(08,08)", "(08,09)", "(08,10)", "(08,11)", "(08,12)",
201  NULL),
202  8, 7,
203  7, 12);
204 }
205 
206 static void
207 data_cursor_all_top_right(void)
208 {
209  ADD_DATA("all - bottom right - top right",
210  gcut_list_string_new(
211  "(07,07)", "(07,08)", "(07,09)", "(07,10)", "(07,11)", "(07,12)",
212  "(08,07)", "(08,08)", "(08,09)", "(08,10)", "(08,11)", "(08,12)",
213  "(09,07)", "(09,08)", "(09,09)", "(09,10)", "(09,11)", "(09,12)",
214  "(10,07)", "(10,08)", "(10,09)", "(10,10)", "(10,11)", "(10,12)",
215  "(11,07)", "(11,08)", "(11,09)", "(11,10)", "(11,11)", "(11,12)",
216  "(12,07)", "(12,08)", "(12,09)", "(12,10)", "(12,11)", "(12,12)",
217  NULL),
218  12, 7,
219  7, 12);
220 }
221 
222 static void
223 data_cursor_all(void)
224 {
225  ADD_DATA("all - minimum",
226  gcut_list_string_new("(07,07)", "(07,08)",
227  "(08,07)", "(08,08)",
228  NULL),
229  8, 7,
230  7, 8);
231 
232 #define ALL_LONGITUDES(latitude) \
233  "(" latitude ",00)", "(" latitude ",01)", \
234  "(" latitude ",02)", "(" latitude ",03)", \
235  "(" latitude ",04)", "(" latitude ",05)", \
236  "(" latitude ",06)", "(" latitude ",07)", \
237  "(" latitude ",08)", "(" latitude ",09)", \
238  "(" latitude ",10)", "(" latitude ",11)", \
239  "(" latitude ",12)", "(" latitude ",13)", \
240  "(" latitude ",14)", "(" latitude ",15)"
241 
242  ADD_DATA("all - maximum",
243  gcut_list_string_new(
244  ALL_LONGITUDES("00"), ALL_LONGITUDES("01"), ALL_LONGITUDES("02"),
245  ALL_LONGITUDES("03"), ALL_LONGITUDES("04"), ALL_LONGITUDES("05"),
246  ALL_LONGITUDES("06"), ALL_LONGITUDES("07"), ALL_LONGITUDES("08"),
247  ALL_LONGITUDES("09"), ALL_LONGITUDES("10"), ALL_LONGITUDES("11"),
248  ALL_LONGITUDES("12"), ALL_LONGITUDES("13"), ALL_LONGITUDES("14"),
249  ALL_LONGITUDES("15"),
250  NULL),
251  15, 0,
252  0, 15);
253 
254 #undef ALL_LONGITUDES
255 
256  data_cursor_all_bottom_left();
257  data_cursor_all_top_left();
258  data_cursor_all_bottom_right();
259  data_cursor_all_top_right();
260 }
261 
262 static void
263 data_cursor_bottom(void)
264 {
265  ADD_DATA("bottom - minimum",
266  gcut_list_string_new("(03,07)", "(03,08)",
267  "(04,07)", "(04,08)",
268  NULL),
269  4, 7,
270  3, 8);
271 
272  ADD_DATA("bottom - left",
273  gcut_list_string_new(
274  "(03,03)", "(03,04)", "(03,05)", "(03,06)", "(03,07)", "(03,08)",
275  "(04,03)", "(04,04)", "(04,05)", "(04,06)", "(04,07)", "(04,08)",
276  NULL),
277  4, 3,
278  3, 8);
279 
280  ADD_DATA("bottom - right",
281  gcut_list_string_new(
282  "(03,07)", "(03,08)", "(03,09)", "(03,10)", "(03,11)", "(03,12)",
283  "(04,07)", "(04,08)", "(04,09)", "(04,10)", "(04,11)", "(04,12)",
284  NULL),
285  4, 7,
286  3, 12);
287 }
288 
289 static void
290 data_cursor_top(void)
291 {
292  ADD_DATA("top - minimum",
293  gcut_list_string_new("(11,07)", "(11,08)",
294  "(12,07)", "(12,08)",
295  NULL),
296  12, 7,
297  11, 8);
298 
299  ADD_DATA("top - left",
300  gcut_list_string_new(
301  "(11,03)", "(11,04)", "(11,05)", "(11,06)", "(11,07)", "(11,08)",
302  "(12,03)", "(12,04)", "(12,05)", "(12,06)", "(12,07)", "(12,08)",
303  NULL),
304  12, 3,
305  11, 8);
306 
307  ADD_DATA("top - right",
308  gcut_list_string_new(
309  "(11,07)", "(11,08)", "(11,09)", "(11,10)", "(11,11)", "(11,12)",
310  "(12,07)", "(12,08)", "(12,09)", "(12,10)", "(12,11)", "(12,12)",
311  NULL),
312  12, 7,
313  11, 12);
314 }
315 
316 static void
317 data_cursor_left(void)
318 {
319  ADD_DATA("left - minimum",
320  gcut_list_string_new("(07,03)", "(07,04)",
321  "(08,03)", "(08,04)",
322  NULL),
323  8, 3,
324  7, 4);
325 
326  ADD_DATA("left - bottom",
327  gcut_list_string_new("(03,03)", "(03,04)",
328  "(04,03)", "(04,04)",
329  "(05,03)", "(05,04)",
330  "(06,03)", "(06,04)",
331  "(07,03)", "(07,04)",
332  "(08,03)", "(08,04)",
333  NULL),
334  8, 3,
335  3, 4);
336 
337  ADD_DATA("left - top",
338  gcut_list_string_new("(07,03)", "(07,04)",
339  "(08,03)", "(08,04)",
340  "(09,03)", "(09,04)",
341  "(10,03)", "(10,04)",
342  "(11,03)", "(11,04)",
343  "(12,03)", "(12,04)",
344  NULL),
345  12, 3,
346  7, 4);
347 }
348 
349 static void
350 data_cursor_right(void)
351 {
352  ADD_DATA("right - minimum",
353  gcut_list_string_new("(07,11)", "(07,12)",
354  "(08,11)", "(08,12)",
355  NULL),
356  8, 11,
357  7, 12);
358 
359  ADD_DATA("right - bottom",
360  gcut_list_string_new("(03,11)", "(03,12)",
361  "(04,11)", "(04,12)",
362  "(05,11)", "(05,12)",
363  "(06,11)", "(06,12)",
364  "(07,11)", "(07,12)",
365  "(08,11)", "(08,12)",
366  NULL),
367  8, 11,
368  3, 12);
369 
370  ADD_DATA("right - top",
371  gcut_list_string_new("(07,11)", "(07,12)",
372  "(08,11)", "(08,12)",
373  "(09,11)", "(09,12)",
374  "(10,11)", "(10,12)",
375  "(11,11)", "(11,12)",
376  "(12,11)", "(12,12)",
377  NULL),
378  12, 11,
379  7, 12);
380 }
381 
382 void
384 {
385  data_cursor_all();
386  data_cursor_bottom();
387  data_cursor_top();
388  data_cursor_left();
389  data_cursor_right();
390 }
391 
392 #undef ADD_DATA
393 
394 static void
395 set_geo_point(grn_obj *geo_point,
396  gint relative_latitude, gint relative_longitude)
397 {
398  gint latitude, longitude;
399  gint base_latitude = (45 * 60 * 60 + 0 * 60) * 1000;
400  gint base_longitude = (90 * 60 * 60 + 0 * 60) * 1000;
401 
402  GRN_WGS84_GEO_POINT_INIT(geo_point, 0);
403  latitude = base_latitude + relative_latitude;
404  longitude = base_longitude + relative_longitude;
405  GRN_GEO_POINT_SET(context, geo_point, latitude, longitude);
406 }
407 
408 void
409 test_cursor(gconstpointer data)
410 {
411  GList *expected, *records = NULL;
412  gint offset = 0;
413  gint limit = -1;
414  grn_obj top_left, bottom_right;
415  grn_obj *geo_cursor;
416  grn_posting *posting;
417  grn_obj short_degree;
418 
419  set_geo_point(&top_left,
420  gcut_data_get_int(data, "top"),
421  gcut_data_get_int(data, "left"));
422  set_geo_point(&bottom_right,
423  gcut_data_get_int(data, "bottom"),
424  gcut_data_get_int(data, "right"));
425  geo_cursor = grn_geo_cursor_open_in_rectangle(context,
426  location_index_column,
427  &top_left,
428  &bottom_right,
429  offset, limit);
430  grn_obj_unlink(context, &top_left);
431  grn_obj_unlink(context, &bottom_right);
432 
433  GRN_TEXT_INIT(&short_degree, 0);
434  while ((posting = grn_geo_cursor_next(context, geo_cursor))) {
435  grn_id point_id = posting->rid;
436 
437  GRN_BULK_REWIND(&short_degree);
438  grn_obj_get_value(context, short_degree_column, point_id, &short_degree);
439  records = g_list_append(records,
440  g_strndup(GRN_TEXT_VALUE(&short_degree),
441  GRN_TEXT_LEN(&short_degree)));
442  }
443  grn_obj_unlink(context, &short_degree);
444  grn_obj_unlink(context, geo_cursor);
445 
446  records = g_list_sort(records, (GCompareFunc)strcmp);
447  gcut_take_list(records, g_free);
448 
449  expected = (GList *)gcut_data_get_pointer(data, "expected");
450  gcut_assert_equal_list_string(expected, records);
451 }
452