Kouhei Sutou
kou****@clear*****
2009年 4月 12日 (日) 12:51:02 JST
須藤です。 In <20090****@epepe*****> "[groonga-dev,00030] Re: grn_obj_set_value()のGRN_OBJ_SETはGRN_OBJ_INCRも含む?" on Sun, 12 Apr 2009 00:53:57 +0900, morit****@razil***** wrote: > ご指摘ありがとうございます! > breakが抜けてました・・ テストを書いてみました。 INCRDECRの方のテストにもなっていると思います。 diff --git a/test/unit/core/Makefile.am b/test/unit/core/Makefile.am index 60c45c6..63759c2 100644 --- a/test/unit/core/Makefile.am +++ b/test/unit/core/Makefile.am @@ -12,7 +12,8 @@ noinst_LTLIBRARIES = \ test-stress.la \ test-public-context.la \ test-query.la \ - test-table.la + test-table.la \ + test-column.la endif INCLUDES = \ @@ -46,3 +47,4 @@ test_stress_la_SOURCES = test-stress.c test_public_context_la_SOURCES = test-public-context.c test_query_la_SOURCES = test-query.c test_table_la_SOURCES = test-table.c +test_column_la_SOURCES = test-column.c diff --git a/test/unit/core/test-column.c b/test/unit/core/test-column.c new file mode 100644 index 0000000..6c15163 --- /dev/null +++ b/test/unit/core/test-column.c @@ -0,0 +1,153 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2009 Kouhei Sutou <kou****@clear*****> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <groonga.h> + +#include <gcutter.h> +#include <glib/gstdio.h> + +#include "../lib/grn-assertions.h" + +#define LOOKUP(name) (grn_ctx_lookup(&context, name, strlen(name))) + +void test_fix_size_set_value_set(void); + +static grn_logger_info *logger; +static grn_ctx context; +static grn_obj *database; +static grn_obj *bookmarks; +static grn_obj *count_column; +static grn_id groonga_bookmark_id; + +static void +create_bookmarks_table(void) +{ + const gchar bookmarks_table_name[] = "bookmarks"; + + bookmarks = grn_table_create(&context, + bookmarks_table_name, + strlen(bookmarks_table_name), + NULL, + GRN_OBJ_TABLE_HASH_KEY, + LOOKUP("<shorttext>"), + 1024, + GRN_ENC_DEFAULT); + grn_test_assert_context(&context); + cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger))); + cut_assert_not_null(bookmarks); +} + +static void +add_count_column_to_bookmarks_table (void) +{ + const gchar count_column_name[] = "count"; + + count_column = grn_column_create(&context, + bookmarks, + count_column_name, + strlen(count_column_name), + NULL, 0, + LOOKUP("<int>")); + grn_test_assert_context(&context); + cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger))); + cut_assert_not_null(count_column); +} + +static void +add_groonga_bookmark(void) +{ + gchar key[] = "groonga"; + grn_search_flags flags = GRN_TABLE_ADD; + + groonga_bookmark_id = grn_table_lookup(&context, bookmarks, + &key, strlen(key), + &flags); + grn_test_assert_context(&context); + cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger))); + grn_test_assert_not_nil(groonga_bookmark_id); +} + +void +setup(void) +{ + logger = setup_grn_logger(); + grn_ctx_init(&context, 0, GRN_ENC_DEFAULT); + database = grn_db_create(&context, NULL, NULL); + + create_bookmarks_table(); + add_count_column_to_bookmarks_table(); + add_groonga_bookmark(); +} + +void +teardown(void) +{ + grn_ctx_fin(&context); + teardown_grn_logger(logger); +} + +void +test_fix_size_set_value_set(void) +{ + gint32 count = 29; + gint32 retrieved_count; + grn_obj *record_value; + grn_obj *retrieved_record_value; + + record_value = grn_obj_open(&context, GRN_BULK, 0, 0); + grn_bulk_write(&context, record_value, (const char *)&count, sizeof(count)); + grn_test_assert(grn_obj_set_value(&context, count_column, groonga_bookmark_id, + record_value, GRN_OBJ_SET)); + + retrieved_record_value = grn_obj_get_value(&context, count_column, + groonga_bookmark_id, NULL); + memcpy(&retrieved_count, + GRN_BULK_HEAD(retrieved_record_value), + GRN_BULK_VSIZE(retrieved_record_value)); + cut_assert_equal_int(count, retrieved_count); +} + +void +test_fix_size_set_value_increment(void) +{ + gint32 count = 29; + gint32 increment_count = 5; + gint32 retrieved_count; + grn_obj *record_value; + grn_obj *retrieved_record_value; + + record_value = grn_obj_open(&context, GRN_BULK, 0, 0); + grn_bulk_write(&context, record_value, (const char *)&count, sizeof(count)); + grn_test_assert(grn_obj_set_value(&context, count_column, groonga_bookmark_id, + record_value, GRN_OBJ_SET)); + grn_obj_close(&context, record_value); + + record_value = grn_obj_open(&context, GRN_BULK, 0, 0); + grn_bulk_write(&context, record_value, + (const char *)&increment_count, sizeof(increment_count)); + grn_test_assert(grn_obj_set_value(&context, count_column, groonga_bookmark_id, + record_value, GRN_OBJ_INCR)); + grn_obj_close(&context, record_value); + + retrieved_record_value = grn_obj_get_value(&context, count_column, + groonga_bookmark_id, NULL); + memcpy(&retrieved_count, + GRN_BULK_HEAD(retrieved_record_value), + GRN_BULK_VSIZE(retrieved_record_value)); + cut_assert_equal_int(count + increment_count, retrieved_count); +} diff --git a/test/unit/lib/grn-assertions.c b/test/unit/lib/grn-assertions.c index a3af69f..8316562 100644 --- a/test/unit/lib/grn-assertions.c +++ b/test/unit/lib/grn-assertions.c @@ -76,3 +76,23 @@ grn_test_assert_not_nil_helper(grn_id id, const gchar *expression) expression, id, GRN_ID_NIL)); } } + +void +grn_test_assert_context_helper (grn_ctx *context, const gchar *expression) +{ + if (!context) { + cut_set_message("context should not NULL"); + cut_assert_null_helper(context, expression); + } else if (context->rc == GRN_SUCCESS) { + cut_test_pass(); + } else { + cut_test_fail(cut_take_printf("<(%s)->rc> != <GRN_SUCCESS>\n" + "expected: <%s> is <%s>\n" + "%s:%d: %s(): %s", + expression, + grn_rc_to_string(context->rc), + grn_rc_to_string(GRN_SUCCESS), + context->errfile, context->errline, + context->errfunc, context->errbuf)); + } +} diff --git a/test/unit/lib/grn-assertions.h b/test/unit/lib/grn-assertions.h index f0f07b8..5691d2c 100644 --- a/test/unit/lib/grn-assertions.h +++ b/test/unit/lib/grn-assertions.h @@ -42,6 +42,11 @@ grn_test_assert_not_nil_helper((expression), #expression), \ grn_test_assert_not_nil(expression)) +#define grn_test_assert_context(expression) \ + cut_trace_with_info_expression( \ + grn_test_assert_context_helper((expression), #expression), \ + grn_test_assert_context(expression)) + void grn_test_assert_helper (grn_rc rc, const gchar *expression); @@ -53,5 +58,7 @@ void grn_test_assert_nil_helper (grn_id id, const gchar *expression); void grn_test_assert_not_nil_helper (grn_id id, const gchar *expression); +void grn_test_assert_context_helper (grn_ctx *context, + const gchar *expression); #endif diff --git a/test/unit/lib/grn-test-utils.c b/test/unit/lib/grn-test-utils.c index 3f1a953..169965d 100644 --- a/test/unit/lib/grn-test-utils.c +++ b/test/unit/lib/grn-test-utils.c @@ -305,6 +305,36 @@ grn_collect_logger_get_messages(grn_logger_info *logger) return context->messages; } +gchar * +grn_collect_logger_to_string(grn_logger_info *logger) +{ + GString *string; + const GList *messages; + + string = g_string_new(NULL); + for (messages = grn_collect_logger_get_messages(logger); + messages; + messages = g_list_next(messages)) { + const gchar *message = messages->data; + g_string_append_printf(string, "%s\n", message); + } + + return g_string_free(string, FALSE); +} + +void +grn_collect_logger_print_messages(grn_logger_info *logger) +{ + const GList *messages; + + for (messages = grn_collect_logger_get_messages(logger); + messages; + messages = g_list_next(messages)) { + const gchar *message = messages->data; + g_print("%s\n", message); + } +} + void grn_collect_logger_free(grn_logger_info *logger) { diff --git a/test/unit/lib/grn-test-utils.h b/test/unit/lib/grn-test-utils.h index 3d78489..5abd7f6 100644 --- a/test/unit/lib/grn-test-utils.h +++ b/test/unit/lib/grn-test-utils.h @@ -41,6 +41,8 @@ const gchar *grn_test_get_base_dir(void); grn_logger_info *grn_collect_logger_new(void); void grn_collect_logger_clear_messages(grn_logger_info *logger); const GList *grn_collect_logger_get_messages(grn_logger_info *logger); +gchar *grn_collect_logger_to_string(grn_logger_info *logger); +void grn_collect_logger_print_messages(grn_logger_info *logger); void grn_collect_logger_free(grn_logger_info *logger); grn_logger_info *setup_grn_logger(void); -- 須藤 功平 <kou****@clear*****> 株式会社クリアコード (http://www.clear-code.com/)