1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
10 #include <yaz/wrbuf.h>
15 static int tst_query_s(const char *cql, const char *expected_ccl,
16 const char *expected_keys)
19 CQL_parser cp = cql_parser_create();
20 int r = cql_parser_string(cp, cql);
24 yaz_log(YLOG_WARN, "cql: parse error: %s", cql);
29 WRBUF w = wrbuf_alloc();
30 r = cql_to_ccl(cql_parser_result(cp), wrbuf_vp_puts, w);
32 if (expected_ccl && r == 0 && strcmp(wrbuf_cstr(w), expected_ccl) == 0)
34 else if (!expected_ccl)
39 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
40 yaz_log(YLOG_WARN, " exp error");
41 yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
47 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
48 yaz_log(YLOG_WARN, " exp ccl %s", expected_ccl);
50 yaz_log(YLOG_WARN, " got ccl %s", wrbuf_cstr(w));
52 yaz_log(YLOG_WARN, " got error");
55 r = cql_sortby_to_sortkeys(cql_parser_result(cp),
57 if (expected_keys && !r && !strcmp(wrbuf_cstr(w), expected_keys))
59 else if (!expected_keys)
64 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
65 yaz_log(YLOG_WARN, " exp error");
66 yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
72 yaz_log(YLOG_WARN, "cql: diff: %s", cql);
73 yaz_log(YLOG_WARN, " exp sortkeys %s", expected_keys);
75 yaz_log(YLOG_WARN, " got sortkeys %s", wrbuf_cstr(w));
77 yaz_log(YLOG_WARN, " got error");
81 cql_parser_destroy(cp);
85 static int tst_query(const char *cql, const char *expected_ccl)
87 return tst_query_s(cql, expected_ccl, "");
92 YAZ_CHECK(tst_query("\"\"", "\"\""));
93 YAZ_CHECK(tst_query("x", "\"x\""));
94 YAZ_CHECK(tst_query("\"x\"", "\"x\""));
95 YAZ_CHECK(tst_query("\"xy\"", "\"xy\""));
97 YAZ_CHECK(tst_query("?", "#"));
98 YAZ_CHECK(tst_query("?a?", "#\"a\"#"));
99 YAZ_CHECK(tst_query("?a", "#\"a\""));
100 YAZ_CHECK(tst_query("a?", "\"a\"#"));
101 YAZ_CHECK(tst_query("\"?\"", "#"));
102 YAZ_CHECK(tst_query("\\?", "\"?\""));
103 YAZ_CHECK(tst_query("\"\\?\"", "\"?\""));
105 YAZ_CHECK(tst_query("*", "?"));
106 YAZ_CHECK(tst_query("*a*", "?\"a\"?"));
107 YAZ_CHECK(tst_query("*a", "?\"a\""));
108 YAZ_CHECK(tst_query("a*", "\"a\"?"));
109 YAZ_CHECK(tst_query("\"*\"", "?"));
110 YAZ_CHECK(tst_query("\\*", "\"*\""));
111 YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
113 YAZ_CHECK(tst_query("a b", "\"a\" \"b\""));
114 YAZ_CHECK(tst_query("ab bc", "\"ab\" \"bc\""));
116 YAZ_CHECK(tst_query("\\\\", "\"\\\\\""));
117 YAZ_CHECK(tst_query("\\\"", "\"\\\"\""));
118 YAZ_CHECK(tst_query("\\x" , "\"x\""));
120 YAZ_CHECK(tst_query("\\*", "\"*\""));
121 YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
122 YAZ_CHECK(tst_query("\\#", "\"#\""));
123 YAZ_CHECK(tst_query("\"\\#\"", "\"#\""));
125 YAZ_CHECK(tst_query("title=x", "title=\"x\""));
126 YAZ_CHECK(tst_query("title=x or author=y",
127 "(title=\"x\") or (author=\"y\")"));
128 YAZ_CHECK(tst_query("title=x or author=y and date=z",
129 "((title=\"x\") or (author=\"y\")) and (date=\"z\")"));
131 YAZ_CHECK(tst_query("title all \"\"", "title=\"\""));
133 YAZ_CHECK(tst_query("title all x", "title=\"x\""));
134 YAZ_CHECK(tst_query("title all x y", "title=\"x\" and title=\"y\""));
135 YAZ_CHECK(tst_query("title all \"x y\"", "title=\"x\" and title=\"y\""));
137 YAZ_CHECK(tst_query("title any x", "title=\"x\""));
138 YAZ_CHECK(tst_query("title any x y", "title=\"x\" or title=\"y\""));
139 YAZ_CHECK(tst_query("title any \"x y\"", "title=\"x\" or title=\"y\""));
141 YAZ_CHECK(tst_query("title = \"x y\"", "title=\"x y\""));
142 YAZ_CHECK(tst_query("title = x y", "title=\"x\" \"y\""));
144 YAZ_CHECK(tst_query("title = x y z", "title=\"x\" \"y\" \"z\""));
146 YAZ_CHECK(tst_query("dc.title=encyclopedia prox dinosaurs",
147 "(dc.title=\"encyclopedia\") % (\"dinosaurs\")"));
148 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3 dinosaurs",
149 "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
150 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=word "
152 "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
153 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=phrase "
155 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/a=b "
157 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/a=b dinosaurs", 0));
158 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<3 dinosaurs",
159 "(dc.title=\"encyclopedia\") %2 (\"dinosaurs\")"));
160 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance=3 dinosaurs", 0));
161 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>3 dinosaurs", 0));
162 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>=3 dinosaurs", 0));
163 YAZ_CHECK(tst_query_s("a sortby title", "\"a\"",
164 "title,,1,0,highValue"));
165 YAZ_CHECK(tst_query_s("a sortby dc.title", "\"a\"",
166 "title,dc,1,0,highValue"));
167 YAZ_CHECK(tst_query_s("a sortby title/ascending", "\"a\"",
168 "title,,1,0,highValue"));
169 YAZ_CHECK(tst_query_s("a sortby title/descending", "\"a\"",
170 "title,,0,0,highValue"));
171 YAZ_CHECK(tst_query_s("a sortby title/ignoreCase", "\"a\"",
172 "title,,1,0,highValue"));
173 YAZ_CHECK(tst_query_s("a sortby title/respectCase", "\"a\"",
174 "title,,1,1,highValue"));
175 YAZ_CHECK(tst_query_s("a sortby title/missingOmit", "\"a\"",
177 YAZ_CHECK(tst_query_s("a sortby title/missingFail", "\"a\"",
178 "title,,1,0,abort"));
179 YAZ_CHECK(tst_query_s("a sortby title/missingLow", "\"a\"",
180 "title,,1,0,lowValue"));
181 YAZ_CHECK(tst_query_s("a sortby title/missingHigh", "\"a\"",
182 "title,,1,0,highValue"));
183 YAZ_CHECK(tst_query_s("a sortby title/sort.missingHigh", "\"a\"",
184 "title,,1,0,highValue"));
185 YAZ_CHECK(tst_query_s("a sortby title/bogus", "\"a\"", 0));
187 YAZ_CHECK(tst_query_s("a sortby dc.year dc.author", "\"a\"",
188 "year,dc,1,0,highValue author,dc,1,0,highValue"));
192 int main(int argc, char **argv)
194 YAZ_CHECK_INIT(argc, argv);
202 * c-file-style: "Stroustrup"
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab