Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-hash.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2009 Kouhei Sutou <kou@cozmixng.org>
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 <hash.h>
20 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 #include "../lib/grn-test-hash-factory.h"
26 #include "../lib/grn-test-hash-assertions.h"
27 
28 static GList *expected_messages;
29 static GrnTestHashFactory *factory;
30 
31 static grn_logger_info *logger;
32 
33 static grn_hash *hash;
34 static grn_hash_cursor *cursor;
35 static grn_id id;
36 static void *value;
37 
38 static uint32_t sample_key;
39 static const gchar *sample_value;
40 static grn_id sample_id;
41 
42 static const gchar *base_dir;
43 
44 static uint32_t key_size;
45 static gchar *not_uint32_size_key;
46 static uint32_t not_uint32_key_size;
47 
48 static void
49 startup_hash_common(void)
50 {
51  not_uint32_size_key = g_strdup("uint32_t size must be 4. "
52  "So this key should have more 4 bytes length. "
53  "And this key should have enough length to "
54  "increment 10000 times. "
55  "But this is too difficult because we need "
56  "to write meaningless grntences that is "
57  "what you are reading now!");
58  not_uint32_key_size = strlen(not_uint32_size_key);
59 }
60 
61 static void
62 shutdown_hash_common(void)
63 {
64  if (not_uint32_size_key) {
65  g_free(not_uint32_size_key);
66  }
67 }
68 
69 static void
70 setup_hash_common(const gchar *default_path_component)
71 {
72  gchar *default_path;
73 
74  logger = setup_grn_logger();
75 
76  factory = grn_test_hash_factory_new();
77  grn_test_hash_factory_set_logger(factory, logger);
78 
79  expected_messages = NULL;
80  hash = NULL;
81  cursor = NULL;
82  id = GRN_ID_NIL;
83 
84  sample_key = 2929;
85  sample_value = cut_take_string(g_strdup("hash test"));
86  sample_id = GRN_ID_NIL;
87 
89  default_path = g_build_filename(base_dir, default_path_component, NULL);
90  grn_test_hash_factory_set_path(factory, default_path);
91  g_free(default_path);
92 
93  key_size = grn_test_hash_factory_get_key_size(factory);
94 
95  cut_remove_path(base_dir, NULL);
96  g_mkdir_with_parents(base_dir, 0755);
97 }
98 
99 static void
100 expected_messages_free(void)
101 {
102  if (expected_messages) {
103  gcut_list_string_free(expected_messages);
104  expected_messages = NULL;
105  }
106 }
107 
108 static void
109 teardown_hash_common(void)
110 {
111  expected_messages_free();
112 
113  if (factory)
114  g_object_unref(factory);
115 
116  if (base_dir) {
117  cut_remove_path(base_dir, NULL);
118  }
119 
120  teardown_grn_logger(logger);
121 }
122 
123 #define context (grn_test_hash_factory_get_context(factory))
124 
125 #define clear_messages() \
126  grn_collect_logger_clear_messages(logger)
127 
128 #define messages() \
129  grn_collect_logger_get_messages(logger)
130 
131 #define cut_assert_create_hash() \
132  grn_test_assert_create_hash(&hash, factory)
133 
134 #define cut_assert_open_hash() \
135  grn_test_assert_open_hash(&hash, factory)
136 
137 #define cut_assert_fail_open_hash() \
138  grn_test_assert_fail_open_hash(&hash, factory)
139 
140 typedef int grn_search_flags;
141 
142 #define GRN_TABLE_ADD (0x01<<6)
143 
144 #define lookup(key, flags) \
145  (((*(flags) & GRN_TABLE_ADD)) \
146  ? grn_hash_add(context, hash, key, key_size, &value, flags) \
147  : grn_hash_get(context, hash, key, key_size, &value))
148 
149 #define cut_assert_lookup(key, flags) do \
150 { \
151  grn_test_assert_not_nil((id = lookup(key, (flags))), \
152  cut_message("flags: <%d>", *(flags))); \
153 } while (0)
154 
155 #define cut_assert_lookup_failed(key, flags) do \
156 { \
157  grn_test_assert_nil(lookup(key, (flags)), \
158  cut_message("flags: <%d>", *(flags))); \
159 } while (0)
160 
161 #define cut_assert_lookup_add(key) do \
162 { \
163  const void *_key; \
164  grn_search_flags flags; \
165  grn_id found_id; \
166  \
167  _key = (key); \
168  if (grn_test_hash_factory_get_flags(factory) & GRN_OBJ_KEY_VAR_SIZE) \
169  key_size = strlen(_key); \
170  \
171  flags = 0; \
172  cut_assert_lookup_failed(_key, &flags); \
173  \
174  flags = GRN_TABLE_ADD; \
175  cut_assert_lookup(_key, &flags); \
176  cut_assert_equal_uint(1, flags & 1); \
177  found_id = id; \
178  if (sample_value) { \
179  strcpy(value, sample_value); \
180  value = NULL; \
181  } \
182  \
183  flags = 0; \
184  cut_assert_lookup(_key, &flags); \
185  cut_assert_equal_uint(found_id, id); \
186  if (sample_value) { \
187  cut_assert_equal_string(sample_value, value); \
188  value = NULL; \
189  } \
190  \
191  flags = GRN_TABLE_ADD; \
192  cut_assert_lookup(_key, &flags); \
193  cut_assert_equal_uint(0, flags & 1); \
194  cut_assert_equal_uint(found_id, id); \
195  if (sample_value) { \
196  cut_assert_equal_string(sample_value, value); \
197  } \
198 } while (0)
199 
200 #define cut_assert_lookup_add_with_value(key, value) do \
201 { \
202  const gchar *_sample_value = sample_value; \
203  sample_value = cut_take_string(g_strdup(value)); \
204  cut_assert_lookup_add(key); \
205  sample_value = _sample_value; \
206 } while (0)
207 
208 #define open_cursor() do \
209 { \
210  GError *error = NULL; \
211  \
212  cursor = grn_test_hash_factory_open_cursor(factory, &error); \
213  gcut_assert_error(error); \
214 } while (0)
215 
216 #define cut_assert_open_cursor() do \
217 { \
218  clear_messages(); \
219  open_cursor(); \
220  cut_assert_equal_g_list_string(NULL, messages()); \
221  cut_assert(cursor); \
222 } while (0)
223 
224 
225 static void
226 set_tiny_flags(void)
227 {
228  grn_test_hash_factory_set_path(factory, NULL);
230 }