Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-table-cursor.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-2013 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 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 
26 void data_table(void);
27 void test_table(gconstpointer data);
28 void data_normalize(void);
29 void test_normalize(gconstpointer data);
30 
31 static grn_logger_info *logger;
32 
33 static gchar *tmp_directory;
34 static const gchar *database_path;
35 
36 static grn_ctx *context;
37 static grn_obj *database;
38 
39 void
41 {
42  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
43  "table-cursor",
44  NULL);
45 }
46 
47 void
49 {
50  g_free(tmp_directory);
51 }
52 
53 static void
54 remove_tmp_directory(void)
55 {
56  cut_remove_path(tmp_directory, NULL);
57 }
58 
59 void
60 cut_setup(void)
61 {
62  remove_tmp_directory();
63  g_mkdir_with_parents(tmp_directory, 0700);
64 
65  context = NULL;
66 
67  logger = setup_grn_logger();
68 
69  context = g_new0(grn_ctx, 1);
70  grn_ctx_init(context, 0);
71 
72  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
73  database = grn_db_create(context, database_path, NULL);
74 }
75 
76 void
78 {
79  grn_obj_close(context, database);
80  grn_ctx_fin(context);
81  g_free(context);
82 
83  teardown_grn_logger(logger);
84 
85  remove_tmp_directory();
86 }
87 
88 void
90 {
91 #define ADD_DATA(label, flags) \
92  gcut_add_datum(label, \
93  "flags", G_TYPE_INT, flags, \
94  NULL)
95 
96  ADD_DATA("no-key", GRN_OBJ_TABLE_NO_KEY);
98  ADD_DATA("patricia trie", GRN_OBJ_TABLE_PAT_KEY);
99 
100 #undef ADD_DATA
101 }
102 
103 void
104 test_table(gconstpointer data)
105 {
106  grn_obj *table;
107  grn_obj_flags flags = gcut_data_get_int(data, "flags");
108  grn_table_cursor *cursor;
109 
110  table = grn_table_create(context, NULL, 0, NULL,
111  flags,
112  get_object("ShortText"),
113  NULL);
114  cursor = grn_table_cursor_open(context, table, NULL, 0, NULL, 0, 0, -1, 0);
115  /* FIXME: grn_test_assert_equal_object() */
116  cut_assert_equal_pointer(table, grn_table_cursor_table(context, cursor));
117 }
118 
119 void
121 {
122 #define ADD_DATA(label, table_type, key) \
123  gcut_add_datum(label, \
124  "table_type", G_TYPE_STRING, table_type, \
125  "key", G_TYPE_STRING, key, \
126  NULL)
127 
128  ADD_DATA("hash table - lower", "TABLE_HASH_KEY", "alice");
129  ADD_DATA("hash table - upper", "TABLE_HASH_KEY", "ALICE");
130  ADD_DATA("hash table - mixed", "TABLE_HASH_KEY", "AlIcE");
131 
132  ADD_DATA("patricia trie - lower", "TABLE_PAT_KEY", "alice");
133  ADD_DATA("patricia trie - upper", "TABLE_PAT_KEY", "ALICE");
134  ADD_DATA("patricia trie - mixed", "TABLE_PAT_KEY", "AlIcE");
135 
136  ADD_DATA("double array trie - lower", "TABLE_DAT_KEY", "alice");
137  ADD_DATA("double array trie - upper", "TABLE_DAT_KEY", "ALICE");
138  ADD_DATA("double array trie - mixed", "TABLE_DAT_KEY", "AlIcE");
139 
140 #undef ADD_DATA
141 }
142 
143 void
144 test_normalize(gconstpointer data)
145 {
146  grn_obj *table;
147  const gchar *search_key = gcut_data_get_string(data, "key");
148  GList *actual_keys = NULL;
149  grn_table_cursor *cursor;
150 
152  cut_take_printf("table_create Users %s ShortText "
153  "--normalizer NormalizerAuto",
154  gcut_data_get_string(data, "table_type")));
155  cut_assert_equal_string(
156  "2",
157  send_command("load --table Users --columns _key\n"
158  "[\n"
159  " [\"Alice\"],\n"
160  " [\"Bob\"]\n"
161  "]"));
162 
163  table = grn_ctx_get(context, "Users", -1);
164  cursor = grn_table_cursor_open(context, table,
165  search_key, strlen(search_key),
166  search_key, strlen(search_key),
167  0, -1, 0);
168  while (grn_table_cursor_next(context, cursor) != GRN_ID_NIL) {
169  void *key;
170  int key_length;
171  key_length = grn_table_cursor_get_key(context, cursor, &key);
172  actual_keys = g_list_append(actual_keys, g_strndup(key, key_length));
173  }
174  grn_table_cursor_close(context, cursor);
175  gcut_take_list(actual_keys, g_free);
176 
177  gcut_assert_equal_list_string(gcut_take_new_list_string("alice", NULL),
178  actual_keys);
179 }
180