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\""));
115 YAZ_CHECK(tst_query("\\\\", "\"\\\\\""));
116 YAZ_CHECK(tst_query("\\\"", "\"\\\"\""));
117 YAZ_CHECK(tst_query("\\x" , "\"x\""));
119 YAZ_CHECK(tst_query("\\*", "\"*\""));
120 YAZ_CHECK(tst_query("\"\\*\"", "\"*\""));
121 YAZ_CHECK(tst_query("\\#", "\"#\""));
122 YAZ_CHECK(tst_query("\"\\#\"", "\"#\""));
124 YAZ_CHECK(tst_query("title=x", "title=\"x\""));
125 YAZ_CHECK(tst_query("title=x or author=y",
126 "(title=\"x\") or (author=\"y\")"));
127 YAZ_CHECK(tst_query("title=x or author=y and date=z",
128 "((title=\"x\") or (author=\"y\")) and (date=\"z\")"));
130 YAZ_CHECK(tst_query("title all \"\"", "title=\"\""));
132 YAZ_CHECK(tst_query("title all x", "title=\"x\""));
133 YAZ_CHECK(tst_query("title all \"x y\"", "title=\"x\" and title=\"y\""));
135 YAZ_CHECK(tst_query("title any x", "title=\"x\""));
136 YAZ_CHECK(tst_query("title any \"x y\"", "title=\"x\" or title=\"y\""));
138 YAZ_CHECK(tst_query("title = \"x y\"", "title=\"x y\""));
140 YAZ_CHECK(tst_query("dc.title=encyclopedia prox dinosaurs",
141 "(dc.title=\"encyclopedia\") % (\"dinosaurs\")"));
142 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3 dinosaurs",
143 "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
144 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=word "
146 "(dc.title=\"encyclopedia\") %3 (\"dinosaurs\")"));
147 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/unit=phrase "
149 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<=3/a=b "
151 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/a=b dinosaurs", 0));
152 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance<3 dinosaurs",
153 "(dc.title=\"encyclopedia\") %2 (\"dinosaurs\")"));
154 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance=3 dinosaurs", 0));
155 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>3 dinosaurs", 0));
156 YAZ_CHECK(tst_query("dc.title=encyclopedia prox/distance>=3 dinosaurs", 0));
157 YAZ_CHECK(tst_query_s("a sortby title", "\"a\"",
158 "title,,1,0,highValue"));
159 YAZ_CHECK(tst_query_s("a sortby dc.title", "\"a\"",
160 "title,dc,1,0,highValue"));
161 YAZ_CHECK(tst_query_s("a sortby title/ascending", "\"a\"",
162 "title,,1,0,highValue"));
163 YAZ_CHECK(tst_query_s("a sortby title/descending", "\"a\"",
164 "title,,0,0,highValue"));
165 YAZ_CHECK(tst_query_s("a sortby title/ignoreCase", "\"a\"",
166 "title,,1,0,highValue"));
167 YAZ_CHECK(tst_query_s("a sortby title/respectCase", "\"a\"",
168 "title,,1,1,highValue"));
169 YAZ_CHECK(tst_query_s("a sortby title/missingOmit", "\"a\"",
171 YAZ_CHECK(tst_query_s("a sortby title/missingFail", "\"a\"",
172 "title,,1,0,abort"));
173 YAZ_CHECK(tst_query_s("a sortby title/missingLow", "\"a\"",
174 "title,,1,0,lowValue"));
175 YAZ_CHECK(tst_query_s("a sortby title/missingHigh", "\"a\"",
176 "title,,1,0,highValue"));
177 YAZ_CHECK(tst_query_s("a sortby title/sort.missingHigh", "\"a\"",
178 "title,,1,0,highValue"));
179 YAZ_CHECK(tst_query_s("a sortby title/bogus", "\"a\"", 0));
181 YAZ_CHECK(tst_query_s("a sortby dc.year dc.author", "\"a\"",
182 "year,dc,1,0,highValue author,dc,1,0,highValue"));
186 int main(int argc, char **argv)
188 YAZ_CHECK_INIT(argc, argv);
196 * c-file-style: "Stroustrup"
197 * indent-tabs-mode: nil
199 * vim: shiftwidth=4 tabstop=8 expandtab