Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-expr-script.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /* Copyright(C) 2009 Brazil
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
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 "db.h"
20 #include <stdio.h>
21 #include <time.h>
22 
23 #include <gcutter.h>
24 
25 #include "../lib/grn-assertions.h"
26 
27 static gchar *tmp_directory;
28 static gchar *path;
29 static grn_ctx context;
30 static grn_obj *database;
31 static grn_obj *res, *expr;
32 static grn_obj textbuf, intbuf, floatbuf, timebuf;
33 
34 void data_logic_operator(void);
35 void test_logic_operator(gconstpointer data);
36 void data_comparison_operator(void);
37 void test_comparison_operator(gconstpointer data);
39 void test_comparison_operator_syntax_error(gconstpointer data);
40 void data_arithmetic_operator(void);
41 void test_arithmetic_operator(gconstpointer data);
43 void test_arithmetic_operator_error(gconstpointer data);
45 void test_arithmetic_operator_syntax_error(gconstpointer data);
46 void data_not_allow_update(void);
47 void test_not_allow_update(gconstpointer data);
48 void data_valid(void);
49 void test_valid(gconstpointer data);
50 
51 void
53 {
54  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
55  "test-expr-script",
56  NULL);
57 }
58 
59 void
61 {
62  g_free(tmp_directory);
63 }
64 
65 void
66 cut_setup(void)
67 {
68  cut_remove_path(tmp_directory, NULL);
69  g_mkdir_with_parents(tmp_directory, 0700);
70  path = g_build_filename(tmp_directory, "text-expr", NULL);
71  grn_ctx_init(&context, 0);
72  database = grn_db_create(&context, path, NULL);
73 
74  expr = NULL;
75  res = NULL;
76 
77  GRN_TEXT_INIT(&textbuf, 0);
78  GRN_UINT32_INIT(&intbuf, 0);
79  GRN_FLOAT_INIT(&floatbuf, 0);
80  GRN_TIME_INIT(&timebuf, 0);
81 }
82 
83 void
85 {
86  grn_obj_close(&context, &textbuf);
87  grn_obj_close(&context, &intbuf);
88  grn_obj_close(&context, &floatbuf);
89  grn_obj_close(&context, &timebuf);
90 
91  if (res)
92  grn_obj_close(&context, res);
93  if (expr)
94  grn_obj_close(&context, expr);
95 
96  grn_db_close(&context, database);
97  grn_ctx_fin(&context);
98  cut_remove_path(tmp_directory, NULL);
99  g_free(path);
100 }
101 
102 #define PARSE(expr, str, flags) \
103  grn_test_assert(grn_expr_parse(&context, (expr), (str), strlen(str), \
104  body, GRN_OP_MATCH, GRN_OP_AND, flags))
105 
106 static grn_obj *docs, *terms, *body, *created_at, *index_body;
107 static grn_obj *size, *size_in_string, *size_in_float;
108 
109 static void
110 insert_document(const gchar *body_content)
111 {
112  uint32_t s = (uint32_t)strlen(body_content);
113  grn_id docid = grn_table_add(&context, docs, NULL, 0, NULL);
114  const gchar *size_string;
115  struct tm time;
116 
117  GRN_TEXT_SET(&context, &textbuf, body_content, s);
118  grn_test_assert(grn_obj_set_value(&context, body, docid, &textbuf,
119  GRN_OBJ_SET));
120 
121  GRN_UINT32_SET(&context, &intbuf, s);
122  grn_test_assert(grn_obj_set_value(&context, size, docid, &intbuf,
123  GRN_OBJ_SET));
124 
125  size_string = cut_take_printf("%u", s);
126  GRN_TEXT_SET(&context, &textbuf, size_string, strlen(size_string));
127  grn_test_assert(grn_obj_set_value(&context, size_in_string, docid, &textbuf,
128  GRN_OBJ_SET));
129 
130  GRN_FLOAT_SET(&context, &floatbuf, s);
131  grn_test_assert(grn_obj_set_value(&context, size_in_float, docid, &floatbuf,
132  GRN_OBJ_SET));
133 
134  time.tm_sec = s;
135  time.tm_min = 16;
136  time.tm_hour = 15;
137  time.tm_mday = 2;
138  time.tm_mon = 11;
139  time.tm_year = 109;
140  time.tm_wday = 3;
141  time.tm_yday = 336;
142  time.tm_isdst = 0;
143  GRN_TIME_SET(&context, &timebuf, GRN_TIME_PACK(mktime(&time), 0));
144  grn_test_assert(grn_obj_set_value(&context, created_at, docid, &timebuf,
145  GRN_OBJ_SET));
146 }
147 
148 #define INSERT_DOCUMENT(body) \
149  cut_trace(insert_document(body))
150 
151 static void
152 create_documents_table(void)
153 {
154  docs = grn_table_create(&context, "docs", 4, NULL,
156  cut_assert_not_null(docs);
157 
158  size = grn_column_create(&context, docs, "size", 4, NULL,
160  grn_ctx_at(&context, GRN_DB_UINT32));
161  cut_assert_not_null(size);
162 
163  size_in_string = grn_column_create(&context, docs, "size_in_string", 14, NULL,
165  grn_ctx_at(&context, GRN_DB_TEXT));
166  cut_assert_not_null(size_in_string);
167 
168  size_in_float = grn_column_create(&context, docs, "size_in_float", 13, NULL,
170  grn_ctx_at(&context, GRN_DB_FLOAT));
171  cut_assert_not_null(size_in_float);
172 
173  created_at = grn_column_create(&context, docs, "created_at", 10, NULL,
175  grn_ctx_at(&context, GRN_DB_TIME));
176  cut_assert_not_null(created_at);
177 
178  body = grn_column_create(&context, docs, "body", 4, NULL,
180  grn_ctx_at(&context, GRN_DB_TEXT));
181  cut_assert_not_null(body);
182 }
183 
184 static void
185 create_terms_table(void)
186 {
187  terms = grn_table_create(&context, "terms", 5, NULL,
189  grn_ctx_at(&context, GRN_DB_SHORT_TEXT), NULL);
190  cut_assert_not_null(terms);
192  grn_ctx_at(&context, GRN_DB_BIGRAM)));
193 
194  index_body = grn_column_create(&context, terms, "docs_body", 4, NULL,
196  docs);
197  cut_assert_not_null(index_body);
198 
199  GRN_UINT32_SET(&context, &intbuf, grn_obj_id(&context, body));
200  grn_obj_set_info(&context, index_body, GRN_INFO_SOURCE, &intbuf);
201 }
202 
203 static void
204 insert_data(void)
205 {
206  INSERT_DOCUMENT("hoge");
207  INSERT_DOCUMENT("fuga fuga");
208  INSERT_DOCUMENT("moge moge moge");
209  INSERT_DOCUMENT("hoge hoge");
210  INSERT_DOCUMENT("hoge fuga fuga");
211  INSERT_DOCUMENT("hoge moge moge moge");
212  INSERT_DOCUMENT("moge hoge hoge");
213  INSERT_DOCUMENT("moge hoge fuga fuga");
214  INSERT_DOCUMENT("moge hoge moge moge moge");
215  INSERT_DOCUMENT("poyo moge hoge moge moge moge");
216 }
217 
218 static void
219 prepare_data(void)
220 {
221  create_documents_table();
222  create_terms_table();
223  insert_data();
224 }
225 
226 #define ADD_DATUM(label, expected_keys, query) \
227  gcut_add_datum(label, \
228  "expected_keys", G_TYPE_POINTER, expected_keys, \
229  gcut_list_string_free, \
230  "query", G_TYPE_STRING, query, \
231  NULL)
232 
233 static void
234 data_logic_operator_and(void)
235 {
236  ADD_DATUM("&& - 1",
237  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
238  "size <= 9 && 1");
239  ADD_DATUM("&& - 0",
240  gcut_list_string_new(NULL, NULL),
241  "size <= 9 && 0");
242 
243  ADD_DATUM("&& - 0.1",
244  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
245  "size <= 9 && 0.1");
246  ADD_DATUM("&& - -0.1",
247  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
248  "size <= 9 && -0.1");
249  ADD_DATUM("&& - 0.0",
250  gcut_list_string_new(NULL, NULL),
251  "size <= 9 && 0.0");
252 
253  ADD_DATUM("&& - \"abc\"",
254  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
255  "size <= 9 && \"abc\"");
256  ADD_DATUM("&& - \"\"",
257  gcut_list_string_new(NULL, NULL),
258  "size <= 9 && \"\"");
259 }
260 
261 static void
262 data_logic_operator_or(void)
263 {
264  ADD_DATUM("|| - 1",
265  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
266  "size <= 9 && (size == 4 || 1)");
267  ADD_DATUM("|| - 0",
268  gcut_list_string_new("hoge", NULL),
269  "size <= 9 && (size == 4 || 0)");
270 
271  ADD_DATUM("|| - 0.1",
272  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
273  "size <= 9 && (size == 4 || 0.1)");
274  ADD_DATUM("|| - -0.1",
275  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
276  "size <= 9 && (size == 4 || -0.1)");
277  ADD_DATUM("|| - 0.0",
278  gcut_list_string_new("hoge", NULL),
279  "size <= 9 && (size == 4 || 0.0)");
280 
281  ADD_DATUM("|| - \"abc\"",
282  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
283  "size <= 9 && (size == 4 || \"abc\")");
284  ADD_DATUM("|| - \"\"",
285  gcut_list_string_new("hoge", NULL),
286  "size <= 9 && (size == 4 || \"\")");
287 }
288 
289 void
291 {
292  data_logic_operator_and();
293  data_logic_operator_or();
294 }
295 
296 #undef ADD_DATUM
297 
298 void
299 test_logic_operator(gconstpointer data)
300 {
301  grn_obj *v;
302 
303  prepare_data();
304 
305  expr = grn_expr_create(&context, NULL, 0);
306  cut_assert_not_null(expr);
307  v = grn_expr_add_var(&context, expr, NULL, 0);
308  cut_assert_not_null(v);
309  GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs));
310  PARSE(expr,
311  gcut_data_get_string(data, "query"),
313 
314  res = grn_table_select(&context, docs, expr, NULL, GRN_OP_OR);
315  cut_assert_not_null(res);
316  grn_test_assert_select(&context,
317  gcut_data_get_pointer(data, "expected_keys"),
318  res,
319  "body");
320 }
321 
322 void
324 {
325 #define ADD_DATUM(label, expected_keys, query) \
326  gcut_add_datum(label, \
327  "expected_keys", G_TYPE_POINTER, expected_keys, \
328  gcut_list_string_free, \
329  "query", G_TYPE_STRING, query, \
330  NULL)
331 
332  ADD_DATUM("<",
333  gcut_list_string_new("hoge", NULL),
334  "size < 9");
335  ADD_DATUM("<=",
336  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
337  "size <= 9");
338  ADD_DATUM(">",
339  gcut_list_string_new("moge hoge moge moge moge",
340  "poyo moge hoge moge moge moge",
341  NULL),
342  "size > 19");
343  ADD_DATUM(">=",
344  gcut_list_string_new("hoge moge moge moge",
345  "moge hoge fuga fuga",
346  "moge hoge moge moge moge",
347  "poyo moge hoge moge moge moge",
348  NULL),
349  "size >= 19");
350 
351 #undef ADD_DATUM
352 }
353 
354 void
355 test_comparison_operator(gconstpointer data)
356 {
357  grn_obj *v;
358 
359  prepare_data();
360 
361  expr = grn_expr_create(&context, NULL, 0);
362  cut_assert_not_null(expr);
363  v = grn_expr_add_var(&context, expr, NULL, 0);
364  cut_assert_not_null(v);
365  GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs));
366  PARSE(expr,
367  gcut_data_get_string(data, "query"),
369 
370  res = grn_table_select(&context, docs, expr, NULL, GRN_OP_OR);
371  cut_assert_not_null(res);
372  grn_test_assert_select(&context,
373  gcut_data_get_pointer(data, "expected_keys"),
374  res,
375  "body");
376 }
377 
378 void
380 {
381 #define ADD_DATUM(label, message, query) \
382  gcut_add_datum(label, \
383  "message", G_TYPE_STRING, message, \
384  "query", G_TYPE_STRING, query, \
385  NULL)
386 
387  ADD_DATUM("nonexistent ==",
388  cut_take_printf("Syntax error! (nonexistent == 100)"),
389  "nonexistent == 100");
390 
391 #undef ADD_DATUM
392 }
393 
394 void
396 {
397  grn_obj *v;
398  const gchar *query;
399 
400  prepare_data();
401 
402  GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v);
403  query = gcut_data_get_string(data, "query");
405  grn_expr_parse(&context, expr, query, strlen(query),
406  body, GRN_OP_MATCH, GRN_OP_AND,
409  gcut_data_get_string(data, "message"),
410  &context);
411 }
412 
413 #define ADD_DATUM(label, expected_keys, query) \
414  gcut_add_datum(label, \
415  "expected_keys", G_TYPE_POINTER, expected_keys, \
416  gcut_list_string_free, \
417  "query", G_TYPE_STRING, query, \
418  NULL)
419 
420 static void
421 data_arithmetic_operator_assign(void)
422 {
423  ADD_DATUM("integer = integer",
424  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
425  "size <= 9 && (size = 10) && size == 10");
426  ADD_DATUM("integer = float",
427  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
428  "size <= 9 && ((size = 10.1) || 1) && size == 10");
429  ADD_DATUM("integer = string",
430  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
431  "size <= 9 && ((size = \"10\") || 1) && size == 10");
432 
433  ADD_DATUM("float = float",
434  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
435  "size_in_float <= 9.1 && "
436  "((size_in_float = 10.1) || 1) && "
437  "size_in_float == 10.1");
438  ADD_DATUM("float = integer",
439  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
440  "size_in_float <= 9.1 && "
441  "(size_in_float = 10) && "
442  "size_in_float == 10");
443 
444  ADD_DATUM("string = integer",
445  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
446  "size_in_string <= 9 && "
447  "((size_in_string = \"10\") || 1) && "
448  "size_in_string == \"10\"");
449 }
450 
451 static void
452 data_arithmetic_operator_star_assign(void)
453 {
454  ADD_DATUM("integer *= integer",
455  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
456  "size <= 9 && (size *= 10) && size == 90");
457  ADD_DATUM("integer *= float",
458  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
459  "size <= 9 && ((size *= 10.1) || 1) && size == 90");
460  ADD_DATUM("integer *= integer-string",
461  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
462  "size <= 9 && ((size *= \"10\") || 1) && size == 90");
463 
464  ADD_DATUM("float *= float",
465  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
466  "size_in_float <= 9.1 && "
467  "((size_in_float *= 10.1) || 1) && "
468  "90 <= size_in_float && "
469  "size_in_float <= 92");
470  ADD_DATUM("float *= integer",
471  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
472  "size_in_float <= 9.1 && "
473  "((size_in_float *= 10) || 1) && "
474  "89 <= size_in_float && "
475  "size_in_float <= 92");
476  ADD_DATUM("float *= float-string",
477  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
478  "size_in_float <= 9.1 && "
479  "((size_in_float *= \"10.1\") || 1) && "
480  "90 <= size_in_float && "
481  "size_in_float <= 92");
482 }
483 
484 static void
485 data_arithmetic_operator_slash_assign(void)
486 {
487  ADD_DATUM("integer /= integer",
488  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
489  "size <= 9 && (size /= 5) && size == 1");
490  ADD_DATUM("integer /= float",
491  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
492  "size <= 9 && ((size /= 5.1) || 1) && size == 1");
493  ADD_DATUM("integer /= integer-string",
494  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
495  "size <= 9 && ((size /= \"5\") || 1) && size == 1");
496 
497  ADD_DATUM("float /= float",
498  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
499  "size_in_float <= 9.1 && "
500  "((size_in_float /= 5.1) || 1) && "
501  "1 < size_in_float && size_in_float < 2");
502  ADD_DATUM("float /= integer",
503  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
504  "size_in_float <= 9.1 && "
505  "((size_in_float /= 5) || 1) && "
506  "1 < size_in_float && size_in_float < 2");
507  ADD_DATUM("float /= float-string",
508  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
509  "size_in_float <= 9.1 && "
510  "((size_in_float /= \"5.1\") || 1) && "
511  "1 < size_in_float && size_in_float < 2");
512 }
513 
514 static void
515 data_arithmetic_operator_mod_assign(void)
516 {
517  ADD_DATUM("integer %= integer",
518  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
519  "size <= 9 && (size %= 4) && size == 1");
520  ADD_DATUM("integer %= float",
521  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
522  "size <= 9 && ((size %= 4.1) || 1) && size == 1");
523  ADD_DATUM("integer %= integer-string",
524  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
525  "size <= 9 && ((size %= \"4\") || 1) && size == 1");
526 
527  ADD_DATUM("float %= float",
528  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
529  "size_in_float <= 9.1 && "
530  "((size_in_float %= 4.1) || 1) && "
531  "0 < size_in_float && size_in_float < 1.1");
532  ADD_DATUM("float %= integer",
533  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
534  "size_in_float <= 9.1 && "
535  "((size_in_float %= 4) || 1) && "
536  "0 < size_in_float && size_in_float < 1.1");
537  ADD_DATUM("float %= float-string",
538  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
539  "size_in_float <= 9.1 && "
540  "((size_in_float %= \"4.1\") || 1) && "
541  "0 < size_in_float && size_in_float < 1.1");
542 }
543 
544 static void
545 data_arithmetic_operator_plus_assign(void)
546 {
547  ADD_DATUM("integer += integer",
548  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
549  "size <= 9 && (size += 4) && size == 13");
550  ADD_DATUM("integer += float",
551  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
552  "size <= 9 && ((size += 4.1) || 1) && size == 13");
553  ADD_DATUM("integer += integer-string",
554  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
555  "size <= 9 && ((size += \"4\") || 1) && size == 13");
556 
557  ADD_DATUM("float += float",
558  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
559  "size_in_float <= 9.1 && "
560  "((size_in_float += 4.1) || 1) && "
561  "13 < size_in_float && size_in_float < 14");
562  ADD_DATUM("float += integer",
563  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
564  "size_in_float <= 9.1 && "
565  "((size_in_float += 4) || 1) && "
566  "12.9 < size_in_float && size_in_float < 14.0");
567  ADD_DATUM("float += float-string",
568  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
569  "size_in_float <= 9.1 && "
570  "((size_in_float += \"4.1\") || 1) && "
571  "13 < size_in_float && size_in_float < 14");
572 }
573 
574 static void
575 data_arithmetic_operator_minus_assign(void)
576 {
577  ADD_DATUM("integer -= integer",
578  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
579  "size <= 9 && (size -= 4) && size == 5");
580  ADD_DATUM("integer -= float",
581  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
582  "size <= 9 && ((size -= 4.1) || 1) && size == 5");
583  ADD_DATUM("integer -= integer-string",
584  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
585  "size <= 9 && ((size -= \"4\") || 1) && size == 5");
586 
587  ADD_DATUM("integer -= expression",
588  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
589  "size <= 9 && ((size -= (3 + 1)) || 1) && size == 5");
590 
591  ADD_DATUM("float -= float",
592  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
593  "size_in_float <= 9.1 && "
594  "((size_in_float -= 4.1) || 1) && "
595  "4.8 < size_in_float && size_in_float < 5");
596  ADD_DATUM("float -= integer",
597  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
598  "size_in_float <= 9.1 && "
599  "((size_in_float -= 4) || 1) && "
600  "4.8 < size_in_float && size_in_float < 5.1");
601  ADD_DATUM("float -= float-string",
602  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
603  "size_in_float <= 9.1 && "
604  "((size_in_float -= \"4.1\") || 1) && "
605  "4.8 < size_in_float && size_in_float < 5");
606 }
607 
608 static void
609 data_arithmetic_operator_shift_l_assign(void)
610 {
611  ADD_DATUM("integer <<= integer",
612  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
613  "size <= 9 && (size <<= 1) && size == 18");
614  ADD_DATUM("integer <<= float",
615  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
616  "size <= 9 && ((size <<= 1.1) || 1) && size == 18");
617  ADD_DATUM("integer <<= integer-string",
618  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
619  "size <= 9 && ((size <<= \"1\") || 1) && size == 18");
620 
621  ADD_DATUM("float <<= float",
622  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
623  "size_in_float <= 9.1 && "
624  "((size_in_float <<= 1.1) || 1) && "
625  "17.9 < size_in_float && size_in_float < 19");
626  ADD_DATUM("float <<= integer",
627  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
628  "size_in_float <= 9.1 && "
629  "((size_in_float <<= 1) || 1) && "
630  "17.9 < size_in_float && size_in_float < 19");
631  ADD_DATUM("float <<= float-string",
632  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
633  "size_in_float <= 9.1 && "
634  "((size_in_float <<= \"1.1\") || 1) && "
635  "17.9 < size_in_float && size_in_float < 19");
636 }
637 
638 static void
639 data_arithmetic_operator_shift_r_assign(void)
640 {
641  ADD_DATUM("integer >>= integer",
642  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
643  "size <= 9 && (size >>= 1) && size == 4");
644  ADD_DATUM("integer >>= float",
645  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
646  "size <= 9 && ((size >>= 1.1) || 1) && size == 4");
647  ADD_DATUM("integer >>= integer-string",
648  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
649  "size <= 9 && ((size >>= \"1\") || 1) && size == 4");
650 
651  ADD_DATUM("float >>= float",
652  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
653  "size_in_float <= 9.1 && "
654  "((size_in_float >>= 1.1) || 1) && "
655  "3.9 < size_in_float && size_in_float < 5");
656  ADD_DATUM("float >>= integer",
657  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
658  "size_in_float <= 9.1 && "
659  "((size_in_float >>= 1) || 1) && "
660  "3.9 < size_in_float && size_in_float < 5");
661  ADD_DATUM("float >>= float-string",
662  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
663  "size_in_float <= 9.1 && "
664  "((size_in_float >>= \"1.1\") || 1) && "
665  "3.9 < size_in_float && size_in_float < 5");
666 }
667 
668 static void
669 data_arithmetic_operator_shift_rr_assign(void)
670 {
671  ADD_DATUM("integer >>>= integer",
672  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
673  "size <= 9 && (size >>>= 1) && size == 4");
674  ADD_DATUM("integer >>>= float",
675  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
676  "size <= 9 && ((size >>>= 1.1) || 1) && size == 4");
677  ADD_DATUM("integer >>>= integer-string",
678  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
679  "size <= 9 && ((size >>>= \"1\") || 1) && size == 4");
680 
681  ADD_DATUM("float >>>= float",
682  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
683  "size_in_float <= 9.1 && "
684  "((size_in_float >>>= 1.1) || 1) && "
685  "3.9 < size_in_float && size_in_float < 5");
686  ADD_DATUM("float >>>= integer",
687  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
688  "size_in_float <= 9.1 && "
689  "((size_in_float >>>= 1) || 1) && "
690  "3.9 < size_in_float && size_in_float < 5");
691  ADD_DATUM("float >>>= float-string",
692  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
693  "size_in_float <= 9.1 && "
694  "((size_in_float >>>= \"1.1\") || 1) && "
695  "3.9 < size_in_float && size_in_float < 5");
696 }
697 
698 static void
699 data_arithmetic_operator_and_assign(void)
700 {
701  ADD_DATUM("integer &= integer",
702  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
703  "size <= 9 && (size &= 1) && size == 1");
704  ADD_DATUM("integer &= float",
705  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
706  "size <= 9 && ((size &= 1.1) || 1) && size == 1");
707  ADD_DATUM("integer &= integer-string",
708  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
709  "size <= 9 && ((size &= \"1\") || 1) && size == 1");
710 
711  ADD_DATUM("float &= float",
712  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
713  "size_in_float <= 9.1 && "
714  "((size_in_float &= 1.1) || 1) && "
715  "0.9 < size_in_float && size_in_float < 1.1");
716  ADD_DATUM("float &= integer",
717  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
718  "size_in_float <= 9.1 && "
719  "((size_in_float &= 1) || 1) && "
720  "0.9 < size_in_float && size_in_float < 1.1");
721  ADD_DATUM("float &= float-string",
722  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
723  "size_in_float <= 9.1 && "
724  "((size_in_float &= \"1.1\") || 1) && "
725  "0.9 < size_in_float && size_in_float < 1.1");
726 }
727 
728 static void
729 data_arithmetic_operator_xor_assign(void)
730 {
731  ADD_DATUM("integer ^= integer",
732  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
733  "size <= 9 && (size ^= 3) && size == 10");
734  ADD_DATUM("integer ^= float",
735  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
736  "size <= 9 && ((size ^= 3.1) || 1) && size == 10");
737  ADD_DATUM("integer ^= integer-string",
738  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
739  "size <= 9 && ((size ^= \"3\") || 1) && size == 10");
740 
741  ADD_DATUM("float ^= float",
742  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
743  "size_in_float <= 9.1 && "
744  "((size_in_float ^= 3.1) || 1) && "
745  "9.9 < size_in_float && size_in_float < 10.1");
746  ADD_DATUM("float ^= integer",
747  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
748  "size_in_float <= 9.1 && "
749  "((size_in_float ^= 3) || 1) && "
750  "9.9 < size_in_float && size_in_float < 10.1");
751  ADD_DATUM("float ^= float-string",
752  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
753  "size_in_float <= 9.1 && "
754  "((size_in_float ^= \"3.1\") || 1) && "
755  "9.9 < size_in_float && size_in_float < 10.1");
756 }
757 
758 static void
759 data_arithmetic_operator_or_assign(void)
760 {
761  ADD_DATUM("integer |= integer",
762  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
763  "size <= 9 && (size |= 3) && size == 11");
764  ADD_DATUM("integer |= float",
765  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
766  "size <= 9 && ((size |= 3.1) || 1) && size == 11");
767  ADD_DATUM("integer |= integer-string",
768  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
769  "size <= 9 && ((size |= \"3\") || 1) && size == 11");
770 
771  ADD_DATUM("float |= float",
772  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
773  "size_in_float <= 9.1 && "
774  "((size_in_float |= 3.1) || 1) && "
775  "10.9 < size_in_float && size_in_float < 11.1");
776  ADD_DATUM("float |= integer",
777  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
778  "size_in_float <= 9.1 && "
779  "((size_in_float |= 3) || 1) && "
780  "10.9 < size_in_float && size_in_float < 11.1");
781  ADD_DATUM("float |= float-string",
782  gcut_list_string_new("fuga fuga", "hoge hoge", NULL),
783  "size_in_float <= 9.1 && "
784  "((size_in_float |= \"3.1\") || 1) && "
785  "10.9 < size_in_float && size_in_float < 11.1");
786 }
787 
788 static void
789 data_arithmetic_operator_bitwise_or(void)
790 {
791  ADD_DATUM("integer | integer",
792  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
793  "size <= (8 | 1)");
794  ADD_DATUM("-integer | integer",
795  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
796  "size <= (-8 | 1) + 16");
797  ADD_DATUM("float | integer",
798  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
799  "size <= (8.1 | 1)");
800  ADD_DATUM("integer-string | integer",
801  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
802  "size <= (\"8\" | 1)");
803  ADD_DATUM("string | integer",
804  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
805  "size <= (\"abc\" | 1) + 9");
806 }
807 
808 static void
809 data_arithmetic_operator_bitwise_xor(void)
810 {
811  ADD_DATUM("integer ^ integer",
812  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
813  "size <= (24 ^ 17)");
814  ADD_DATUM("-integer ^ integer",
815  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
816  "size <= (-24 ^ 17) + 16");
817  ADD_DATUM("float ^ integer",
818  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
819  "size <= (24.1 ^ 17)");
820  ADD_DATUM("integer-string ^ integer",
821  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
822  "size <= (\"24\" ^ 17)");
823  ADD_DATUM("string ^ integer",
824  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
825  "size <= (\"abc\" ^ 1) + 9");
826 }
827 
828 static void
829 data_arithmetic_operator_bitwise_and(void)
830 {
831  ADD_DATUM("integer & integer",
832  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
833  "size <= (25 & 41)");
834  ADD_DATUM("-integer & integer",
835  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
836  "size <= (-33 & 41)");
837  ADD_DATUM("float & integer",
838  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
839  "size <= (25.3 & 41)");
840  ADD_DATUM("integer-string & integer",
841  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
842  "size <= (\"25\" & 41)");
843  ADD_DATUM("string & integer",
844  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
845  "size <= (\"abc\" & 41) + 9");
846 }
847 
848 static void
849 data_arithmetic_operator_bitwise_not(void)
850 {
851  ADD_DATUM("~integer",
852  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
853  "size <= ~9 + 19");
854  ADD_DATUM("~-integer",
855  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
856  "size <= ~-9 + 1");
857  ADD_DATUM("~float",
858  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
859  "size <= ~9.1 + 19");
860  ADD_DATUM("~integer-string",
861  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
862  "size <= ~\"9\" + 19");
863  ADD_DATUM("~string",
864  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
865  "size <= ~\"abc\" + 10");
866 }
867 
868 static void
869 data_arithmetic_operator_shift_l(void)
870 {
871  ADD_DATUM("integer << integer",
872  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
873  "size <= ((2 << 2) + 1)");
874  ADD_DATUM("float << float",
875  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
876  "size <= ((2.1 << 2.1) + 1)");
877  ADD_DATUM("integer-string << integer-string",
878  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
879  "size <= ((\"2\" << \"2\") + 1)");
880  ADD_DATUM("string << integer-string",
881  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
882  "size <= ((\"abc\" << \"2\") + 9)");
883  ADD_DATUM("integer-string << string",
884  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
885  "size <= ((\"2\" << \"abc\") + 7)");
886 }
887 
888 static void
889 data_arithmetic_operator_shift_r(void)
890 {
891  ADD_DATUM("integer >> integer",
892  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
893  "size <= ((32 >> 3) + 5)");
894  ADD_DATUM("float >> float",
895  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
896  "size <= ((32.1 >> 3.1) + 5)");
897  ADD_DATUM("integer-string >> integer-string",
898  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
899  "size <= ((\"32\" >> \"3\") + 5)");
900  ADD_DATUM("string >> integer-string",
901  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
902  "size <= ((\"abc\" >> \"2\") + 9)");
903  ADD_DATUM("integer-string >> string",
904  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
905  "size <= ((\"32\" >> \"abc\") - 23)");
906  ADD_DATUM("-integer >> integer",
907  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
908  "size <= ((-32 >> 1) + 16 + 9)");
909 }
910 
911 static void
912 data_arithmetic_operator_shift_rr(void)
913 {
914  ADD_DATUM("integer >>> integer",
915  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
916  "size <= ((32 >>> 3) + 5)");
917  ADD_DATUM("float >>> float",
918  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
919  "size <= ((32.1 >>> 3.1) + 5)");
920  ADD_DATUM("integer-string >>> integer-string",
921  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
922  "size <= ((\"32\" >>> \"3\") + 5)");
923  ADD_DATUM("string >>> integer-string",
924  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
925  "size <= ((\"abc\" >>> \"2\") + 9)");
926  ADD_DATUM("integer-string >>> string",
927  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
928  "size <= ((\"32\" >>> \"abc\") - 23)");
929  ADD_DATUM("-integer >>> integer",
930  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
931  cut_take_printf("size <= ((-32 >>> 1) - %d + 9)",
932  G_MAXINT32 - 16));
933 }
934 
935 static void
936 data_arithmetic_operator_plus(void)
937 {
938  ADD_DATUM("+integer",
939  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
940  "size <= +9");
941  ADD_DATUM("integer + integer",
942  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
943  "size <= (4 + 5)");
944  ADD_DATUM("string + string",
945  gcut_list_string_new("fuga fuga", NULL),
946  "body == \"fuga \" + \"fuga\"");
947  ADD_DATUM("string + int",
948  gcut_list_string_new("nick NICK 29", NULL),
949  "body == \"nick NICK \" + 29");
950  ADD_DATUM("int + string",
951  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
952  "size <= 4 + \"5\"");
953  ADD_DATUM("int + float",
954  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
955  "size <= 4 + 5.0001");
956 }
957 
958 static void
959 data_arithmetic_operator_minus(void)
960 {
961  ADD_DATUM("-integer",
962  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
963  "size <= -5 + 14");
964  ADD_DATUM("-integer maximum",
965  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
966  cut_take_printf("size <= "
967  "-%" G_GINT64_FORMAT " + %" G_GINT64_FORMAT,
968  ((gint64)G_MAXINT32) + 1,
969  ((gint64)G_MAXINT32) + 1 + 9));
970  ADD_DATUM("-integer overflow",
971  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
972  cut_take_printf("size <= "
973  "-%" G_GINT64_FORMAT " + %" G_GINT64_FORMAT,
974  ((gint64)G_MAXINT32) + 2,
975  ((gint64)G_MAXINT32) + 2 + 9));
976  ADD_DATUM("-integer64",
977  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
978  cut_take_printf("size <= "
979  "-%" G_GINT64_FORMAT " + "
980  "%" G_GINT64_FORMAT " + 9",
981  G_MAXINT64 / 2, G_MAXINT64 / 2));
982  ADD_DATUM("-integer64 maximum",
983  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
984  cut_take_printf("size <= "
985  "-%" G_GINT64_FORMAT " + "
986  "%" G_GINT64_FORMAT " + 100 + 9",
987  G_MAXINT64,
988  G_MAXINT64 - 100));
989  ADD_DATUM("-integer64 overflow",
990  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
991  cut_take_printf("size <= "
992  "-%" G_GUINT64_FORMAT " + "
993  "%" G_GUINT64_FORMAT " + "
994  "9",
995  ((guint64)(G_MAXINT64)) + 1,
996  ((guint64)(G_MAXINT64)) + 1));
997  ADD_DATUM("-float",
998  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
999  "size <= -5.9 + 14.9");
1000 
1001  ADD_DATUM("integer - integer",
1002  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1003  "size <= 14 - 5");
1004  ADD_DATUM("int - string",
1005  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1006  "size <= 14 - \"5\"");
1007  ADD_DATUM("float - int",
1008  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1009  "size <= 14.1 - 5");
1010 
1011  ADD_DATUM("-variable",
1012  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1013  "size <= (-size + size + 9)");
1014 
1015 }
1016 
1017 static void
1018 data_arithmetic_operator_star(void)
1019 {
1020  ADD_DATUM("integer * integer",
1021  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1022  "size <= 3 * 3");
1023  ADD_DATUM("int * string",
1024  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1025  "size <= 3 * \"3\"");
1026  ADD_DATUM("float * int",
1027  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1028  "size <= 3.1 * 3");
1029 }
1030 
1031 static void
1032 data_arithmetic_operator_slash(void)
1033 {
1034  ADD_DATUM("integer / integer",
1035  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1036  "size <= 27 / 3");
1037  ADD_DATUM("int / string",
1038  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1039  "size <= 27 / \"3\"");
1040  ADD_DATUM("float / int",
1041  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1042  "size <= 27.1 / 3");
1043 }
1044 
1045 static void
1046 data_arithmetic_operator_mod(void)
1047 {
1048  ADD_DATUM("integer % integer",
1049  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1050  "size <= 19 % 10");
1051  ADD_DATUM("int % string",
1052  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1053  "size <= 19 % \"10\"");
1054  ADD_DATUM("float % int",
1055  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1056  "size <= 19.1 % 10");
1057  ADD_DATUM("float % string",
1058  gcut_list_string_new("fuga fuga", "hoge", "hoge hoge", NULL),
1059  "size <= 19.1 % \"9.9\"");
1060 }
1061 
1062 static void
1063 data_arithmetic_operator_incr(void)
1064 {
1065  int time_at_2009_12_2_15_16_0 = 1259734560;
1066 
1067  ADD_DATUM("++integer",
1068  gcut_list_string_new("hoge", NULL),
1069  "++size == 5 && size == 5");
1070  ADD_DATUM("++float",
1071  gcut_list_string_new("hoge", NULL),
1072  "++size_in_float == 5 && size_in_float == 5");
1073  ADD_DATUM("++time",
1074  gcut_list_string_new("hoge", NULL),
1075  cut_take_printf("++created_at == %d && created_at == %d",
1076  time_at_2009_12_2_15_16_0 + 5,
1077  time_at_2009_12_2_15_16_0 + 5));
1078 }
1079 
1080 static void
1081 data_arithmetic_operator_decr(void)
1082 {
1083  int time_at_2009_12_2_15_16_0 = 1259734560;
1084 
1085  ADD_DATUM("--integer",
1086  gcut_list_string_new("hoge", NULL),
1087  "--size <= 3 && size == 3");
1088  ADD_DATUM("--float",
1089  gcut_list_string_new("hoge", NULL),
1090  "--size_in_float <= 3 && size_in_float == 3");
1091  ADD_DATUM("--time",
1092  gcut_list_string_new("hoge", NULL),
1093  cut_take_printf("--created_at == %d && created_at == %d",
1094  time_at_2009_12_2_15_16_0 + 3,
1095  time_at_2009_12_2_15_16_0 + 3));
1096 }
1097 
1098 static void
1099 data_arithmetic_operator_incr_post(void)
1100 {
1101  int time_at_2009_12_2_15_16_0 = 1259734560;
1102 
1103  ADD_DATUM("integer++",
1104  gcut_list_string_new("hoge", NULL),
1105  "size++ <= 4 && size == 5");
1106  ADD_DATUM("float++",
1107  gcut_list_string_new("hoge", NULL),
1108  "size_in_float++ <= 4 && size_in_float == 5");
1109  ADD_DATUM("time++",
1110  gcut_list_string_new("hoge", NULL),
1111  cut_take_printf("created_at++ == %d && created_at == %d",
1112  time_at_2009_12_2_15_16_0 + 4,
1113  time_at_2009_12_2_15_16_0 + 5));
1114 }
1115 
1116 static void
1117 data_arithmetic_operator_decr_post(void)
1118 {
1119  int time_at_2009_12_2_15_16_0 = 1259734560;
1120 
1121  ADD_DATUM("integer--",
1122  gcut_list_string_new("hoge", NULL),
1123  "size-- == 4 && size == 3");
1124  ADD_DATUM("float--",
1125  gcut_list_string_new("hoge", NULL),
1126  "size_in_float-- == 4 && size_in_float == 3");
1127  ADD_DATUM("time--",
1128  gcut_list_string_new("hoge", NULL),
1129  cut_take_printf("created_at-- == %d && created_at == %d",
1130  time_at_2009_12_2_15_16_0 + 4,
1131  time_at_2009_12_2_15_16_0 + 3));
1132 }
1133 
1134 void
1136 {
1137  data_arithmetic_operator_assign();
1138  data_arithmetic_operator_star_assign();
1139  data_arithmetic_operator_slash_assign();
1140  data_arithmetic_operator_mod_assign();
1141  data_arithmetic_operator_plus_assign();
1142  data_arithmetic_operator_minus_assign();
1143  data_arithmetic_operator_shift_l_assign();
1144  data_arithmetic_operator_shift_r_assign();
1145  data_arithmetic_operator_shift_rr_assign();
1146  data_arithmetic_operator_and_assign();
1147  data_arithmetic_operator_xor_assign();
1148  data_arithmetic_operator_or_assign();
1149  data_arithmetic_operator_bitwise_or();
1150  data_arithmetic_operator_bitwise_xor();
1151  data_arithmetic_operator_bitwise_and();
1152  data_arithmetic_operator_bitwise_not();
1153  data_arithmetic_operator_shift_l();
1154  data_arithmetic_operator_shift_r();
1155  data_arithmetic_operator_shift_rr();
1156  data_arithmetic_operator_plus();
1157  data_arithmetic_operator_minus();
1158  data_arithmetic_operator_star();
1159  data_arithmetic_operator_slash();
1160  data_arithmetic_operator_mod();
1161  data_arithmetic_operator_incr();
1162  data_arithmetic_operator_decr();
1163  data_arithmetic_operator_incr_post();
1164  data_arithmetic_operator_decr_post();
1165 }
1166 #undef ADD_DATUM
1167 
1168 void
1169 test_arithmetic_operator(gconstpointer data)
1170 {
1171  grn_obj *v;
1172 
1173  prepare_data();
1174  INSERT_DOCUMENT("nick NICK 29");
1175 
1176  expr = grn_expr_create(&context, NULL, 0);
1177  cut_assert_not_null(expr);
1178  v = grn_expr_add_var(&context, expr, NULL, 0);
1179  cut_assert_not_null(v);
1180  GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs));
1181  PARSE(expr,
1182  gcut_data_get_string(data, "query"),
1184 
1185  res = grn_table_select(&context, docs, expr, NULL, GRN_OP_OR);
1186  cut_assert_not_null(res);
1187  grn_test_assert_context(&context);
1188  grn_test_assert_select(&context,
1189  gcut_data_get_pointer(data, "expected_keys"),
1190  res,
1191  "body");
1192 }
1193 
1194 #define ADD_DATUM(label, rc, message, query) \
1195  gcut_add_datum(label, \
1196  "rc", G_TYPE_UINT, rc, \
1197  "message", G_TYPE_STRING, message, \
1198  "query", G_TYPE_STRING, query, \
1199  NULL)
1200 
1201 static void
1202 data_arithmetic_operator_error_plus(void)
1203 {
1204  ADD_DATUM("int + string",
1206  "not a numerical format: <hoge>",
1207  "size == 100 + \"hoge\"");
1208 }
1209 
1210 static void
1211 data_arithmetic_operator_error_minus(void)
1212 {
1213  ADD_DATUM("string - string",
1215  "\"string\" - \"string\" isn't supported",
1216  "body == \"fuga\" - \"hoge\"");
1217 }
1218 
1219 static void
1220 data_arithmetic_operator_error_star(void)
1221 {
1222  ADD_DATUM("string * string",
1224  "\"string\" * \"string\" isn't supported",
1225  "body == \"fuga\" * \"hoge\"");
1226 }
1227 
1228 static void
1229 data_arithmetic_operator_error_slash(void)
1230 {
1231  ADD_DATUM("string / string",
1233  "\"string\" / \"string\" isn't supported",
1234  "body == \"fuga\" / \"hoge\"");
1235  ADD_DATUM("integer / 0",
1237  "dividend should not be 0",
1238  "size == 10 / 0");
1239 }
1240 
1241 static void
1242 data_arithmetic_operator_error_mod(void)
1243 {
1244  ADD_DATUM("string % string",
1246  "\"string\" % \"string\" isn't supported",
1247  "body == \"fuga\" % \"hoge\"");
1248  ADD_DATUM("integer % 0",
1250  "dividend should not be 0",
1251  "size == 10 % 0");
1252 }
1253 
1254 static void
1255 data_arithmetic_operator_error_incr(void)
1256 {
1257  ADD_DATUM("++string",
1259  cut_take_printf("invalid increment target type: %d "
1260  "(FIXME: type name is needed)",
1261  GRN_DB_TEXT),
1262  "++size_in_string <= 9");
1263 }
1264 
1265 static void
1266 data_arithmetic_operator_error_decr(void)
1267 {
1268  ADD_DATUM("--string",
1270  cut_take_printf("invalid increment target type: %d "
1271  "(FIXME: type name is needed)",
1272  GRN_DB_TEXT),
1273  "--size_in_string <= 7");
1274 
1275 }
1276 
1277 static void
1278 data_arithmetic_operator_error_incr_post(void)
1279 {
1280  ADD_DATUM("string++",
1282  cut_take_printf("invalid increment target type: %d "
1283  "(FIXME: type name is needed)",
1284  GRN_DB_TEXT),
1285  "size_in_string++ <= 8");
1286 }
1287 
1288 static void
1289 data_arithmetic_operator_error_decr_post(void)
1290 {
1291  ADD_DATUM("string--",
1293  cut_take_printf("invalid increment target type: %d "
1294  "(FIXME: type name is needed)",
1295  GRN_DB_TEXT),
1296  "size_in_string-- <= 8");
1297 }
1298 
1299 void
1301 {
1302  data_arithmetic_operator_error_plus();
1303  data_arithmetic_operator_error_minus();
1304  data_arithmetic_operator_error_star();
1305  data_arithmetic_operator_error_slash();
1306  data_arithmetic_operator_error_mod();
1307  data_arithmetic_operator_error_incr();
1308  data_arithmetic_operator_error_decr();
1309  data_arithmetic_operator_error_incr_post();
1310  data_arithmetic_operator_error_decr_post();
1311 }
1312 #undef ADD_DATUM
1313 
1314 void
1316 {
1317  grn_obj *v;
1318 
1319  prepare_data();
1320 
1321  GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v);
1322  PARSE(expr,
1323  gcut_data_get_string(data, "query"),
1325 
1326  res = grn_table_select(&context, docs, expr, NULL, GRN_OP_OR);
1327  cut_assert_not_null(res);
1328  grn_test_assert_error(gcut_data_get_uint(data, "rc"),
1329  gcut_data_get_string(data, "message"),
1330  &context);
1331 }
1332 
1333 void
1335 {
1336 #define ADD_DATUM(label, message, query) \
1337  gcut_add_datum(label, \
1338  "message", G_TYPE_STRING, message, \
1339  "query", G_TYPE_STRING, query, \
1340  NULL)
1341 
1342  ADD_DATUM("++constant",
1343  cut_take_printf("constant can't be incremented (++8 <= 9)"),
1344  "++8 <= 9");
1345  ADD_DATUM("--constant",
1346  cut_take_printf("constant can't be decremented (--10 <= 9)"),
1347  "--10 <= 9");
1348  ADD_DATUM("constant++",
1349  cut_take_printf("constant can't be incremented (8++ <= 9)"),
1350  "8++ <= 9");
1351  ADD_DATUM("constant--",
1352  cut_take_printf("constant can't be decremented (10-- <= 9)"),
1353  "10-- <= 9");
1354 
1355 #undef ADD_DATUM
1356 }
1357 
1358 void
1360 {
1361  grn_obj *v;
1362  const gchar *query;
1363 
1364  prepare_data();
1365 
1366  GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v);
1367  query = gcut_data_get_string(data, "query");
1369  grn_expr_parse(&context, expr, query, strlen(query),
1370  body, GRN_OP_MATCH, GRN_OP_AND,
1374  gcut_data_get_string(data, "message"),
1375  &context);
1376 }
1377 
1378 void
1380 {
1381 #define ADD_DATUM(label, message, query) \
1382  gcut_add_datum(label, \
1383  "message", G_TYPE_STRING, message, \
1384  "query", G_TYPE_STRING, query, \
1385  NULL)
1386 
1387  ADD_DATUM("=",
1388  cut_take_printf("'=' is not allowed (size = 100)"),
1389  "size = 100");
1390  ADD_DATUM("*=",
1391  cut_take_printf("'*=' is not allowed (size *= 10)"),
1392  "size *= 10");
1393  ADD_DATUM("/=",
1394  cut_take_printf("'/=' is not allowed (size /= 10)"),
1395  "size /= 10");
1396  ADD_DATUM("%=",
1397  cut_take_printf("'%%=' is not allowed (size %%= 10)"),
1398  "size %= 10");
1399  ADD_DATUM("+=",
1400  cut_take_printf("'+=' is not allowed (size += 10)"),
1401  "size += 10");
1402  ADD_DATUM("-=",
1403  cut_take_printf("'-=' is not allowed (size -= 10)"),
1404  "size -= 10");
1405  ADD_DATUM("<<=",
1406  cut_take_printf("'<<=' is not allowed (size <<= 10)"),
1407  "size <<= 10");
1408  ADD_DATUM(">>=",
1409  cut_take_printf("'>>=' is not allowed (size >>= 10)"),
1410  "size >>= 10");
1411  ADD_DATUM(">>>=",
1412  cut_take_printf("'>>>=' is not allowed (size >>>= 10)"),
1413  "size >>>= 10");
1414  ADD_DATUM("&=",
1415  cut_take_printf("'&=' is not allowed (size &= 10)"),
1416  "size &= 10");
1417  ADD_DATUM("|=",
1418  cut_take_printf("'|=' is not allowed (size |= 10)"),
1419  "size |= 10");
1420  ADD_DATUM("^=",
1421  cut_take_printf("'^=' is not allowed (size ^= 10)"),
1422  "size ^= 10");
1423  ADD_DATUM("variable++",
1424  cut_take_printf("'++' is not allowed (size++)"),
1425  "size++");
1426  ADD_DATUM("++variable",
1427  cut_take_printf("'++' is not allowed (++size)"),
1428  "++size");
1429  ADD_DATUM("variable--",
1430  cut_take_printf("'--' is not allowed (size--)"),
1431  "size--");
1432  ADD_DATUM("--variable",
1433  cut_take_printf("'--' is not allowed (--size)"),
1434  "--size");
1435 
1436 #undef ADD_DATUM
1437 }
1438 
1439 void
1440 test_not_allow_update(gconstpointer data)
1441 {
1442  grn_obj *v;
1443  const gchar *query;
1444 
1445  prepare_data();
1446 
1447  GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v);
1448  query = gcut_data_get_string(data, "query");
1450  grn_expr_parse(&context, expr, query, strlen(query),
1451  body, GRN_OP_MATCH, GRN_OP_AND,
1454  gcut_data_get_string(data, "message"),
1455  &context);
1456 }
1457 
1458 void
1460 {
1461 #define ADD_DATUM(label, query) \
1462  gcut_add_datum(label, \
1463  "query", G_TYPE_STRING, query, \
1464  NULL)
1465 
1466  ADD_DATUM("quote",
1467  "body == \"hoge\" && size == 4");
1468 
1469 #undef ADD_DATUM
1470 }
1471 
1472 void
1473 test_valid(gconstpointer data)
1474 {
1475  grn_obj *v;
1476  const gchar *query;
1477 
1478  prepare_data();
1479 
1480  GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v);
1481  query = gcut_data_get_string(data, "query");
1482  grn_test_assert(grn_expr_parse(&context, expr, query, strlen(query),
1483  body, GRN_OP_MATCH, GRN_OP_AND,
1485  cut_message("%s", query));
1486 }