null+****@clear*****
null+****@clear*****
2010年 7月 7日 (水) 11:29:11 JST
Kouhei Sutou 2010-07-07 02:29:11 +0000 (Wed, 07 Jul 2010) New Revision: 4418901a45b75de5d46698bebfe1989499abbaf2 Log: add tests for near cursor on UInt32. [#383] Modified files: test/unit/core/test-table-patricia-trie-cursor.c Modified: test/unit/core/test-table-patricia-trie-cursor.c (+136 -25) =================================================================== --- test/unit/core/test-table-patricia-trie-cursor.c 2010-07-07 02:00:48 +0000 (14e4e44) +++ test/unit/core/test-table-patricia-trie-cursor.c 2010-07-07 02:29:11 +0000 (4c290f2) @@ -25,6 +25,8 @@ void data_prefix_short_text(void); void test_prefix_short_text(gpointer data); +void data_near_uint32(void); +void test_near_uint32(gpointer data); static gchar *tmp_directory; @@ -87,6 +89,58 @@ cut_teardown(void) } } +static void +create_short_text_table(void) +{ + const char *table_name = "ShortTextPat"; + + assert_send_commands( + cut_take_printf("table_create %s TABLE_PAT_KEY ShortText", table_name)); + assert_send_commands( + cut_take_printf("load --table %s\n" + "[\n" + " [\"_key\"],\n" + " [\"abra\"]," + " [\"abracada\"]," + " [\"abracadabra\"]," + " [\"abubu\"]," + " [\"あ\"]," + " [\"ああ\"]," + " [\"あああ\"]," + " [\"い\"]" + "]", + table_name)); + + table = grn_ctx_get(context, table_name, strlen(table_name)); +} + +static void +create_uint32_table(void) +{ + const char *table_name = "UInt32Pat"; + + assert_send_commands( + cut_take_printf("table_create %s TABLE_PAT_KEY UInt32", table_name)); + assert_send_commands( + cut_take_printf("load --table %s\n" + "[\n" + " [\"_key\"],\n" + " [%u]," + " [%u]," + " [%u]," + " [%u]," + " [%u]" + "]", + table_name, + 0x00000000U, + 0x00000004U, + 0x00000080U, + 0xdeadbeefU, + 0xffffffffU)); + + table = grn_ctx_get(context, table_name, strlen(table_name)); +} + void data_prefix_short_text(void) { @@ -158,31 +212,6 @@ data_prefix_short_text(void) #undef ADD_DATA } -static void -create_short_text_table(void) -{ - const char *table_name = "ShortTextPat"; - - assert_send_commands( - cut_take_printf("table_create %s TABLE_PAT_KEY ShortText", table_name)); - assert_send_commands( - cut_take_printf("load --table %s\n" - "[\n" - " [\"_key\"],\n" - " [\"abra\"]," - " [\"abracada\"]," - " [\"abracadabra\"]," - " [\"abubu\"]," - " [\"あ\"]," - " [\"ああ\"]," - " [\"あああ\"]," - " [\"い\"]" - "]", - table_name)); - - table = grn_ctx_get(context, table_name, strlen(table_name)); -} - void test_prefix_short_text(gpointer data) { @@ -216,3 +245,85 @@ test_prefix_short_text(gpointer data) expected_keys = gcut_data_get_pointer(data, "expected"); gcut_assert_equal_list_string(expected_keys, actual_keys); } + +static GList * +uint_list_new(gint n, guint value, ...) +{ + GList *list = NULL; + va_list args; + gint i; + + va_start(args, value); + for (i = 0; i < n; i++) { + list = g_list_prepend(list, GUINT_TO_POINTER(value)); + value = va_arg(args, guint); + } + va_end(args); + + return g_list_reverse(list); +} + +void +data_near_uint32(void) +{ +#define ADD_DATA(label, expected, min_length, max, offset, limit, flags) \ + gcut_add_datum(label, \ + "expected", G_TYPE_POINTER, \ + expected, g_list_free, \ + "min-length", G_TYPE_INT, min_length, \ + "max", G_TYPE_UINT, max, \ + "offset", G_TYPE_INT, offset, \ + "limit", G_TYPE_INT, limit, \ + "flags", G_TYPE_INT, flags, \ + NULL) + + ADD_DATA("no limit", + uint_list_new(5, + 0x00000000U, 0x00000004U, 0x00000080U, + 0xdeadbeefU, 0xffffffffU), + 0, 0, + 0, -1, + 0); + ADD_DATA("min limit", + uint_list_new(3, 0x00000000U, 0x00000004U, 0x00000080U), + 1, 0, + 0, -1, + 0); + +#undef ADD_DATA +} + +void +test_near_uint32(gpointer data) +{ + grn_id id; + int min_length, offset, limit, flags; + guint32 max; + const GList *expected_keys; + GList *actual_keys = NULL; + + create_uint32_table(); + + min_length = gcut_data_get_int(data, "min-length"); + max = gcut_data_get_uint(data, "max"); + offset = gcut_data_get_int(data, "offset"); + limit = gcut_data_get_int(data, "limit"); + flags = gcut_data_get_int(data, "flags"); + cursor = grn_table_cursor_open(context, table, + NULL, min_length, + &max, sizeof(max), + offset, limit, + flags | GRN_CURSOR_PREFIX); + grn_test_assert_context(context); + while ((id = grn_table_cursor_next(context, cursor))) { + guint32 *key; + int key_length; + + key_length = grn_table_cursor_get_key(context, cursor, (void **)&key); + actual_keys = g_list_append(actual_keys, GUINT_TO_POINTER(*key)); + } + gcut_take_list(actual_keys, NULL); + + expected_keys = gcut_data_get_pointer(data, "expected"); + gcut_assert_equal_list_uint(expected_keys, actual_keys); +}