null+****@clear*****
null+****@clear*****
2010年 8月 24日 (火) 18:05:10 JST
Kouhei Sutou 2010-08-24 09:05:10 +0000 (Tue, 24 Aug 2010) New Revision: c4a20884a60d3f05711342b5fded028243f0a0a0 Log: add tests for edit distance. Added files: test/unit/core/test-function-edit-distance.c Modified files: test/unit/core/Makefile.am Modified: test/unit/core/Makefile.am (+2 -0) =================================================================== --- test/unit/core/Makefile.am 2010-08-24 09:02:15 +0000 (592e988) +++ test/unit/core/Makefile.am 2010-08-24 09:05:10 +0000 (dd15446) @@ -35,6 +35,7 @@ noinst_LTLIBRARIES = \ test-view-operations.la \ test-register.la \ test-function-cast.la \ + test-function-edit-distance.la \ test-store-ja.la \ test-log.la \ test-table-sort-key-from-str.la \ @@ -108,6 +109,7 @@ test_view_la_SOURCES = test-view.c test_view_operations_la_SOURCES = test-view-operations.c test_register_la_SOURCES = test-register.c test_function_cast_la_SOURCES = test-function-cast.c +test_function_edit_distance_la_SOURCES = test-function-edit-distance.c test_store_ja_la_SOURCES = test-store-ja.c test_log_la_SOURCES = test-log.c test_table_sort_key_from_str_la_SOURCES = test-table-sort-key-from-str.c Added: test/unit/core/test-function-edit-distance.c (+159 -0) 100644 =================================================================== --- /dev/null +++ test/unit/core/test-function-edit-distance.c 2010-08-24 09:05:10 +0000 (12f5eb2) @@ -0,0 +1,159 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2010 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 <gcutter.h> +#include <glib/gstdio.h> + +#include "../lib/grn-assertions.h" + +#include <str.h> + +void data_alphabet(void); +void test_alphabet(gconstpointer data); + +static gchar *tmp_directory; + +static grn_ctx *context; +static grn_obj *database; + +void +cut_startup(void) +{ + tmp_directory = g_build_filename(grn_test_get_tmp_dir(), + "function-edit-distance", + NULL); +} + +void +cut_shutdown(void) +{ + g_free(tmp_directory); +} + +static void +remove_tmp_directory(void) +{ + cut_remove_path(tmp_directory, NULL); +} + +void +cut_setup(void) +{ + const gchar *database_path; + + remove_tmp_directory(); + g_mkdir_with_parents(tmp_directory, 0700); + + context = g_new0(grn_ctx, 1); + grn_ctx_init(context, 0); + + database_path = cut_build_path(tmp_directory, "database.groonga", NULL); + database = grn_db_create(context, database_path, NULL); +} + +void +cut_teardown(void) +{ + if (context) { + grn_ctx_fin(context); + g_free(context); + } + + remove_tmp_directory(); +} + +void +data_alphabet(void) +{ +#define ADD_DATUM(label, expected, string) \ + gcut_add_datum(label, \ + "expected", G_TYPE_STRING, expected, \ + "string", G_TYPE_STRING, string, \ + NULL) + + ADD_DATUM("same", + "[\"mori\",0]," + "[\"topo\",3]," + "[\"tako\",4]," + "[\"tapa\",4]," + "[\"tapo\",4]," + "[\"yu\",4]," + "[\"tapopo\",5]," + "[\"gunyara-kun\",10]", + "mori"); + + ADD_DATUM("long", + "[\"mori\",4]," + "[\"tapo\",4]," + "[\"tako\",5]," + "[\"tapa\",5]," + "[\"topo\",5]," + "[\"tapopo\",6]," + "[\"yu\",8]," + "[\"gunyara-kun\",10]", + "moritapo"); + + ADD_DATUM("short", + "[\"mori\",2]," + "[\"yu\",2]," + "[\"tako\",3]," + "[\"tapo\",3]," + "[\"topo\",3]," + "[\"tapa\",4]," + "[\"tapopo\",5]," + "[\"gunyara-kun\",11]", + "mo"); + +#undef ADD_DATUM +} + +void +test_alphabet(gconstpointer data) +{ + const gchar *expected, *command; + + assert_send_command("table_create Users TABLE_NO_KEY"); + assert_send_command("column_create Users name COLUMN_SCALAR ShortText"); + assert_send_command("load --table Users\n" + "[\n" + "[\"name\"],\n" + "[\"mori\"],\n" + "[\"gunyara-kun\"],\n" + "[\"yu\"],\n" + "[\"tapo\"],\n" + "[\"tapopo\"],\n" + "[\"tapa\"],\n" + "[\"tako\"],\n" + "[\"topo\"]\n" + "]"); + + expected = + cut_take_printf("[[[8]," + "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]]," + "%s" + "]]", + gcut_data_get_string(data, "expected")); + command = cut_take_printf("select Users " + "--output_columns name,_score " + "--filter true " + "--sortby _score,name " + "--scorer '_score=edit_distance(name, \"%s\")'", + gcut_data_get_string(data, "string")); + + cut_assert_equal_string(expected, send_command(command)); +}