Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-table-hook.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-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 <groonga.h>
20 #include <db.h>
21 
22 #include <gcutter.h>
23 #include <glib/gstdio.h>
24 
25 #include "../lib/grn-assertions.h"
26 
27 void test_old_value_zero(void);
28 
29 static gchar *tmp_directory;
30 
31 static grn_ctx *context;
32 static grn_obj *database, *table, *column, *index_table, *result;
33 static grn_table_cursor *cursor;
34 
35 void
37 {
38  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
39  "table-hook",
40  NULL);
41 }
42 
43 void
45 {
46  g_free(tmp_directory);
47 }
48 
49 static void
50 remove_tmp_directory(void)
51 {
52  cut_remove_path(tmp_directory, NULL);
53 }
54 
55 void
56 cut_setup(void)
57 {
58  const gchar *database_path;
59 
60  remove_tmp_directory();
61  g_mkdir_with_parents(tmp_directory, 0700);
62 
63  context = g_new0(grn_ctx, 1);
64  grn_ctx_init(context, 0);
65 
66  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
67  database = grn_db_create(context, database_path, NULL);
68  table = NULL;
69  column = NULL;
70  index_table = NULL;
71  result = NULL;
72  cursor = NULL;
73 }
74 
75 void
77 {
78  if (cursor) {
79  grn_obj_unlink(context, cursor);
80  }
81 
82  if (result) {
83  grn_obj_unlink(context, result);
84  }
85 
86  if (column) {
87  grn_obj_unlink(context, column);
88  }
89 
90  if (table) {
91  grn_obj_unlink(context, table);
92  }
93 
94  if (index_table) {
95  grn_obj_unlink(context, index_table);
96  }
97 
98  grn_obj_close(context, database);
99  grn_ctx_fin(context);
100  g_free(context);
101 
102  remove_tmp_directory();
103 }
104 
105 static void
106 create_int32_table(const gchar *load_data)
107 {
108  const char *table_name = "Data";
109  const char *column_name = "number";
110  const char *index_table_name = "Index";
111 
113  cut_take_printf("table_create %s TABLE_NO_KEY", table_name));
115  cut_take_printf("column_create %s %s COLUMN_SCALAR Int32",
116  table_name, column_name));
118  cut_take_printf("table_create %s TABLE_PAT_KEY Int32",
119  index_table_name));
121  cut_take_printf("column_create %s %s_%s COLUMN_INDEX %s %s",
122  index_table_name,
123  table_name, column_name,
124  table_name, column_name));
126  cut_take_printf("load --table %s\n"
127  "[\n"
128  " [\"%s\"],\n"
129  "%s\n"
130  "]",
131  table_name,
132  column_name,
133  load_data));
134 
135  table = grn_ctx_get(context, table_name, strlen(table_name));
136  column = grn_obj_column(context, table, column_name, strlen(column_name));
137  index_table = grn_ctx_get(context, index_table_name, strlen(index_table_name));
138 }
139 
140 static GList *
141 int_list_new(gint n, gint value, ...)
142 {
143  GList *list = NULL;
144  va_list args;
145  gint i;
146 
147  va_start(args, value);
148  for (i = 0; i < n; i++) {
149  list = g_list_prepend(list, GINT_TO_POINTER(value));
150  value = va_arg(args, gint);
151  }
152  va_end(args);
153 
154  return g_list_reverse(list);
155 }
156 
157 void
159 {
160  grn_id id;
161  GList *expected_keys, *actual_keys = NULL;
162 
163  cut_omit("null value support is required.");
164  create_int32_table(" [0],\n"
165  " [1],\n"
166  " [2],\n"
167  " [3],\n"
168  " [4]");
169 
170  cursor = grn_table_cursor_open(context, index_table,
171  NULL, 0, NULL, 0, 0, -1,
173  grn_test_assert_context(context);
174  while ((id = grn_table_cursor_next(context, cursor))) {
175  gint32 *key;
176  int key_size;
177 
178  key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
179  actual_keys = g_list_append(actual_keys, GINT_TO_POINTER(*key));
180  }
181  gcut_take_list(actual_keys, NULL);
182 
183  expected_keys = int_list_new(5, 0, 1, 2, 3, 4);
184  gcut_take_list(expected_keys, NULL);
185  gcut_assert_equal_list_int(expected_keys, actual_keys);
186 }