Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-function-edit-distance.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2010-2011 Kouhei Sutou <kou@clear-code.com>
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 <gcutter.h>
20 #include <glib/gstdio.h>
21 
22 #include "../lib/grn-assertions.h"
23 
24 #include <str.h>
25 
26 void data_alphabet(void);
27 void test_alphabet(gconstpointer data);
28 void data_japanese(void);
29 void test_japanese(gconstpointer data);
30 
31 static gchar *tmp_directory;
32 
33 static grn_ctx *context;
34 static grn_obj *database;
35 
36 void
38 {
39  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
40  "function-edit-distance",
41  NULL);
42 }
43 
44 void
46 {
47  g_free(tmp_directory);
48 }
49 
50 static void
51 remove_tmp_directory(void)
52 {
53  cut_remove_path(tmp_directory, NULL);
54 }
55 
56 void
57 cut_setup(void)
58 {
59  const gchar *database_path;
60 
61  remove_tmp_directory();
62  g_mkdir_with_parents(tmp_directory, 0700);
63 
64  context = g_new0(grn_ctx, 1);
65  grn_ctx_init(context, 0);
66 
67  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
68  database = grn_db_create(context, database_path, NULL);
69 }
70 
71 void
73 {
74  grn_obj_close(context, database);
75  grn_ctx_fin(context);
76  g_free(context);
77 
78  remove_tmp_directory();
79 }
80 
81 void
83 {
84 #define ADD_DATUM(label, expected, string) \
85  gcut_add_datum(label, \
86  "expected", G_TYPE_STRING, expected, \
87  "string", G_TYPE_STRING, string, \
88  NULL)
89 
90  ADD_DATUM("same",
91  "[\"mori\",0],"
92  "[\"topo\",3],"
93  "[\"tako\",4],"
94  "[\"tapa\",4],"
95  "[\"tapo\",4],"
96  "[\"yu\",4],"
97  "[\"tapopo\",5],"
98  "[\"gunyara-kun\",10]",
99  "mori");
100 
101  ADD_DATUM("long",
102  "[\"mori\",4],"
103  "[\"tapo\",4],"
104  "[\"tako\",5],"
105  "[\"tapa\",5],"
106  "[\"topo\",5],"
107  "[\"tapopo\",6],"
108  "[\"yu\",8],"
109  "[\"gunyara-kun\",10]",
110  "moritapo");
111 
112  ADD_DATUM("short",
113  "[\"mori\",2],"
114  "[\"yu\",2],"
115  "[\"tako\",3],"
116  "[\"tapo\",3],"
117  "[\"topo\",3],"
118  "[\"tapa\",4],"
119  "[\"tapopo\",5],"
120  "[\"gunyara-kun\",11]",
121  "mo");
122 
123 #undef ADD_DATUM
124 }
125 
126 void
127 test_alphabet(gconstpointer data)
128 {
129  const gchar *expected, *command;
130 
131  assert_send_command("table_create Users TABLE_NO_KEY");
132  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
133  assert_send_command("load --table Users\n"
134  "[\n"
135  "[\"name\"],\n"
136  "[\"mori\"],\n"
137  "[\"gunyara-kun\"],\n"
138  "[\"yu\"],\n"
139  "[\"tapo\"],\n"
140  "[\"tapopo\"],\n"
141  "[\"tapa\"],\n"
142  "[\"tako\"],\n"
143  "[\"topo\"]\n"
144  "]");
145 
146  expected =
147  cut_take_printf("[[[8],"
148  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
149  "%s"
150  "]]",
151  gcut_data_get_string(data, "expected"));
152  command = cut_take_printf("select Users "
153  "--output_columns name,_score "
154  "--filter true "
155  "--sortby _score,name "
156  "--scorer '_score=edit_distance(name, \"%s\")'",
157  gcut_data_get_string(data, "string"));
158 
159  cut_assert_equal_string(expected, send_command(command));
160 }
161 
162 void
164 {
165 #define ADD_DATUM(label, expected, string) \
166  gcut_add_datum(label, \
167  "expected", G_TYPE_STRING, expected, \
168  "string", G_TYPE_STRING, string, \
169  NULL)
170 
171  ADD_DATUM("same",
172  "[\"もり\",0],"
173  "[\"たこ\",2],"
174  "[\"たぱ\",2],"
175  "[\"たぽ\",2],"
176  "[\"とぽ\",2],"
177  "[\"ゆう\",2],"
178  "[\"たぽぽ\",3],"
179  "[\"グニャラくん\",6]",
180  "もり");
181 
182  ADD_DATUM("long",
183  "[\"たぽ\",2],"
184  "[\"もり\",2],"
185  "[\"たこ\",3],"
186  "[\"たぱ\",3],"
187  "[\"たぽぽ\",3],"
188  "[\"とぽ\",3],"
189  "[\"ゆう\",4],"
190  "[\"グニャラくん\",6]",
191  "もりたぽ");
192 
193  ADD_DATUM("short",
194  "[\"もり\",1],"
195  "[\"たこ\",2],"
196  "[\"たぱ\",2],"
197  "[\"たぽ\",2],"
198  "[\"とぽ\",2],"
199  "[\"ゆう\",2],"
200  "[\"たぽぽ\",3],"
201  "[\"グニャラくん\",6]",
202  "も");
203 
204 #undef ADD_DATUM
205 }
206 
207 void
208 test_japanese(gconstpointer data)
209 {
210  const gchar *expected, *command;
211 
212  assert_send_command("table_create Users TABLE_NO_KEY");
213  assert_send_command("column_create Users name COLUMN_SCALAR ShortText");
214  assert_send_command("load --table Users\n"
215  "[\n"
216  "[\"name\"],\n"
217  "[\"もり\"],\n"
218  "[\"グニャラくん\"],\n"
219  "[\"ゆう\"],\n"
220  "[\"たぽ\"],\n"
221  "[\"たぽぽ\"],\n"
222  "[\"たぱ\"],\n"
223  "[\"たこ\"],\n"
224  "[\"とぽ\"]\n"
225  "]");
226 
227  expected =
228  cut_take_printf("[[[8],"
229  "[[\"name\",\"ShortText\"],[\"_score\",\"Int32\"]],"
230  "%s"
231  "]]",
232  gcut_data_get_string(data, "expected"));
233  command = cut_take_printf("select Users "
234  "--output_columns name,_score "
235  "--filter true "
236  "--sortby _score,name "
237  "--scorer '_score=edit_distance(name, \"%s\")'",
238  gcut_data_get_string(data, "string"));
239 
240  cut_assert_equal_string(expected, send_command(command));
241 }