22 #include <glib/gstdio.h>
24 #include "../lib/grn-assertions.h"
25 #include "../lib/grn-test-hash-factory.h"
26 #include "../lib/grn-test-hash-assertions.h"
28 static GList *expected_messages;
38 static uint32_t sample_key;
39 static const gchar *sample_value;
44 static uint32_t key_size;
45 static gchar *not_uint32_size_key;
46 static uint32_t not_uint32_key_size;
49 startup_hash_common(
void)
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);
62 shutdown_hash_common(
void)
64 if (not_uint32_size_key) {
65 g_free(not_uint32_size_key);
70 setup_hash_common(
const gchar *default_path_component)
79 expected_messages = NULL;
85 sample_value = cut_take_string(g_strdup(
"hash test"));
89 default_path = g_build_filename(base_dir, default_path_component, NULL);
95 cut_remove_path(base_dir, NULL);
96 g_mkdir_with_parents(base_dir, 0755);
100 expected_messages_free(
void)
102 if (expected_messages) {
103 gcut_list_string_free(expected_messages);
104 expected_messages = NULL;
109 teardown_hash_common(
void)
111 expected_messages_free();
114 g_object_unref(factory);
117 cut_remove_path(base_dir, NULL);
123 #define context (grn_test_hash_factory_get_context(factory))
125 #define clear_messages() \
126 grn_collect_logger_clear_messages(logger)
129 grn_collect_logger_get_messages(logger)
131 #define cut_assert_create_hash() \
132 grn_test_assert_create_hash(&hash, factory)
134 #define cut_assert_open_hash() \
135 grn_test_assert_open_hash(&hash, factory)
137 #define cut_assert_fail_open_hash() \
138 grn_test_assert_fail_open_hash(&hash, factory)
142 #define GRN_TABLE_ADD (0x01<<6)
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))
149 #define cut_assert_lookup(key, flags) do \
151 grn_test_assert_not_nil((id = lookup(key, (flags))), \
152 cut_message("flags: <%d>", *(flags))); \
155 #define cut_assert_lookup_failed(key, flags) do \
157 grn_test_assert_nil(lookup(key, (flags)), \
158 cut_message("flags: <%d>", *(flags))); \
161 #define cut_assert_lookup_add(key) do \
164 grn_search_flags flags; \
168 if (grn_test_hash_factory_get_flags(factory) & GRN_OBJ_KEY_VAR_SIZE) \
169 key_size = strlen(_key); \
172 cut_assert_lookup_failed(_key, &flags); \
174 flags = GRN_TABLE_ADD; \
175 cut_assert_lookup(_key, &flags); \
176 cut_assert_equal_uint(1, flags & 1); \
178 if (sample_value) { \
179 strcpy(value, sample_value); \
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); \
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); \
200 #define cut_assert_lookup_add_with_value(key, value) do \
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; \
208 #define open_cursor() do \
210 GError *error = NULL; \
212 cursor = grn_test_hash_factory_open_cursor(factory, &error); \
213 gcut_assert_error(error); \
216 #define cut_assert_open_cursor() do \
220 cut_assert_equal_g_list_string(NULL, messages()); \
221 cut_assert(cursor); \