cql2ccl: term code for all/any sequences as well
[yaz-moved-to-github.git] / test / test_cql2ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <string.h>
10 #include <yaz/wrbuf.h>
11 #include <yaz/cql.h>
12 #include <yaz/log.h>
13 #include <yaz/test.h>
14
15
16 static int tst_query(const char *cql, const char *expected_ccl)
17 {
18     int ret = 0;
19     CQL_parser cp = cql_parser_create();
20     int r = cql_parser_string(cp, cql);
21     
22     if (r)
23     {
24         yaz_log(YLOG_WARN, "cql: parse error: %s", cql);
25     }
26     else
27     { /* cql parse OK */
28         WRBUF w = wrbuf_alloc();
29         r = cql_to_ccl(cql_parser_result(cp), wrbuf_vp_puts, w);
30
31         if (expected_ccl && r == 0 && strcmp(wrbuf_cstr(w), expected_ccl) == 0)
32         {
33             ret = 1;
34         }
35         else if (!expected_ccl)
36         {
37             if (r)
38                 ret = 1; /* expected conversion error, OK */
39             else
40                 yaz_log(YLOG_WARN, "cql: expected conversion error: %s", cql);
41         }
42         else
43         {
44             yaz_log(YLOG_WARN, "cql: diff: %s", cql);
45             yaz_log(YLOG_WARN, " expected %s", expected_ccl);
46             yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(w));
47         }
48         wrbuf_destroy(w);
49     }
50     cql_parser_destroy(cp);
51     return ret;
52 }
53
54 static void tst(void)
55 {
56     YAZ_CHECK(tst_query("\"\"", "\"\""));
57     YAZ_CHECK(tst_query("x", "\"x\""));
58     YAZ_CHECK(tst_query("\"x\"", "\"x\""));
59     YAZ_CHECK(tst_query("\"xy\"", "\"xy\""));
60
61     YAZ_CHECK(tst_query("?", "#"));
62     YAZ_CHECK(tst_query("?a?", "#\"a\"#"));
63     YAZ_CHECK(tst_query("?a", "#\"a\""));
64     YAZ_CHECK(tst_query("a?", "\"a\"#"));
65     YAZ_CHECK(tst_query("\"?\"", "#"));
66     YAZ_CHECK(tst_query("\\?", "\"?\""));
67     YAZ_CHECK(tst_query("\"\\?\"", "\"?\""));
68
69     YAZ_CHECK(tst_query("*", "?"));
70     YAZ_CHECK(tst_query("*a*", "?\"a\"?"));
71     YAZ_CHECK(tst_query("*a", "?\"a\""));
72     YAZ_CHECK(tst_query("a*", "\"a\"?"));
73     YAZ_CHECK(tst_query("\"*\"", "?"));
74     YAZ_CHECK(tst_query("\\*", "\"*\""));
75     YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
76
77     YAZ_CHECK(tst_query("a b", "\"a\" \"b\""));
78     YAZ_CHECK(tst_query("ab bc", "\"ab\" \"bc\""));
79
80     YAZ_CHECK(tst_query("\\\\", "\"\\\\\""));
81     YAZ_CHECK(tst_query("\\\"", "\"\\\"\""));
82     YAZ_CHECK(tst_query("\\x" , "\"x\""));
83
84     YAZ_CHECK(tst_query("\\*", "\"*\""));
85     YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
86     YAZ_CHECK(tst_query("\\#", "\"#\""));
87     YAZ_CHECK(tst_query("\"\\#\"", "\"#\""));
88
89     YAZ_CHECK(tst_query("title=x", "title=\"x\""));
90     YAZ_CHECK(tst_query("title=x or author=y",
91                         "(title=\"x\" or author=\"y\")"));
92
93     YAZ_CHECK(tst_query("title all \"\"", "title=\"\""));
94
95     YAZ_CHECK(tst_query("title all x", "title=\"x\""));
96     YAZ_CHECK(tst_query("title all x y", "title=\"x\" and title=\"y\""));
97     YAZ_CHECK(tst_query("title all \"x y\"", "title=\"x\" and title=\"y\""));
98
99     YAZ_CHECK(tst_query("title any x", "title=\"x\""));
100     YAZ_CHECK(tst_query("title any x y", "title=\"x\" or title=\"y\""));
101     YAZ_CHECK(tst_query("title any \"x y\"", "title=\"x\" or title=\"y\""));
102 }
103
104 int main(int argc, char **argv)
105 {
106     YAZ_CHECK_INIT(argc, argv);
107     YAZ_CHECK_LOG();
108     tst();
109     YAZ_CHECK_TERM;
110 }
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * c-file-style: "Stroustrup"
115  * indent-tabs-mode: nil
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */
119