Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-table-sort-key-from-str.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2009-2010 Nobuyoshi Nakada <nakada@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 <groonga.h>
20 
21 #include <gcutter.h>
22 #include <glib/gstdio.h>
23 
24 #include "../lib/grn-assertions.h"
25 
26 void data_valid(void);
27 void test_valid(gconstpointer data);
28 void data_invalid(void);
29 void test_invalid(gconstpointer data);
30 
31 static gchar *tmp_directory;
32 
33 static grn_ctx *context;
34 static grn_obj *database;
35 static grn_obj *table;
36 
37 static unsigned n_keys;
38 static grn_table_sort_key *keys;
39 
40 void
42 {
43  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
44  "table-sort-key-from-str",
45  NULL);
46 }
47 
48 void
50 {
51  g_free(tmp_directory);
52 }
53 
54 static void
55 remove_tmp_directory(void)
56 {
57  cut_remove_path(tmp_directory, NULL);
58 }
59 
60 void
61 cut_setup(void)
62 {
63  const gchar *database_path;
64 
65  remove_tmp_directory();
66  g_mkdir_with_parents(tmp_directory, 0700);
67 
68  context = g_new0(grn_ctx, 1);
69  grn_ctx_init(context, 0);
70 
71  database_path = cut_build_path(tmp_directory, "database.groonga", NULL);
72  database = grn_db_create(context, database_path, NULL);
73 
74  assert_send_command("table_create Test TABLE_PAT_KEY ShortText");
75  assert_send_command("column_create Test name COLUMN_SCALAR Text");
76  table = get_object("Test");
77 
78  n_keys = 0;
79  keys = NULL;
80 }
81 
82 void
84 {
85  if (context) {
86  if (keys)
87  grn_table_sort_key_close(context, keys, n_keys);
88  grn_obj_unlink(context, database);
89  grn_ctx_fin(context);
90  g_free(context);
91  }
92 
93  remove_tmp_directory();
94 }
95 
96 void
97 data_valid(void)
98 {
99 #define ADD_DATUM(str, count) \
100  gcut_add_datum("[" str "] == " #count, \
101  "keys", G_TYPE_STRING, str, \
102  "count", G_TYPE_UINT, count, \
103  NULL)
104 
105  ADD_DATUM("name", 1);
106  ADD_DATUM(" name ", 1);
107  ADD_DATUM("_key", 1);
108  ADD_DATUM("_key, name", 2);
109  ADD_DATUM(" _key, name ", 2);
110  ADD_DATUM("+name", 1);
111  ADD_DATUM(" +name ", 1);
112  ADD_DATUM("+_key", 1);
113  ADD_DATUM("+_key, +name", 2);
114  ADD_DATUM(" +_key, +name ", 2);
115  ADD_DATUM("-name", 1);
116  ADD_DATUM(" -name ", 1);
117  ADD_DATUM("-_key", 1);
118  ADD_DATUM("-_key, -name", 2);
119  ADD_DATUM(" -_key, -name ", 2);
120 
121 #undef ADD_DATUM
122 }
123 
124 void
125 test_valid(gconstpointer data)
126 {
127  unsigned i;
128  const char *str = gcut_data_get_string(data, "keys");
129 
130  keys = grn_table_sort_key_from_str(context, str, strlen(str), table, &n_keys);
131  cut_assert_not_null(keys);
132  cut_assert_equal_uint(gcut_data_get_uint(data, "count"), n_keys);
133  for (i = 0; i < n_keys; ++i) {
134  cut_assert_not_null(keys[i].key);
135  }
136 }
137 
138 void
140 {
141 #define ADD_DATUM(str) \
142  gcut_add_datum("[" str "] is invalid", \
143  "keys", G_TYPE_STRING, str, \
144  NULL)
145 
146  ADD_DATUM("foo");
147  ADD_DATUM("_key, foo");
148  ADD_DATUM("foo, name");
149  ADD_DATUM("name, foo");
150  ADD_DATUM("+ name");
151  ADD_DATUM("- name");
152 
153 #undef ADD_DATUM
154 }
155 
156 void
157 test_invalid(gconstpointer data)
158 {
159  unsigned nkeys;
160  const char *str = gcut_data_get_string(data, "keys");
161  grn_table_sort_key *keys = grn_table_sort_key_from_str(context, str, strlen(str),
162  table, &nkeys);
163  cut_assert_null(keys);
164  cut_assert_equal_uint(0, nkeys);
165 }