1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
10 #include <yaz/wrbuf.h>
11 #include <yaz/sortspec.h>
15 static int cql(const char *arg, const char *expected_result)
17 ODR odr = odr_createmem(ODR_ENCODE);
18 Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
23 yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
27 WRBUF w = wrbuf_alloc();
28 int r = yaz_sort_spec_to_cql(sort_spec, w);
30 if (!expected_result && r)
32 else if (expected_result && r == 0)
34 if (strcmp(wrbuf_cstr(w), expected_result) == 0)
38 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
39 yaz_log(YLOG_WARN, " expected %s", expected_result);
40 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
45 yaz_log(YLOG_WARN, "sort: diff %s", arg);
46 yaz_log(YLOG_WARN, " expected %s", expected_result);
47 yaz_log(YLOG_WARN, " got error %d", r);
51 yaz_log(YLOG_WARN, "sort: diff %s", arg);
52 yaz_log(YLOG_WARN, " expected error");
53 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
61 static int type7(const char *arg, const char *expected_result)
63 ODR odr = odr_createmem(ODR_ENCODE);
64 Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
69 yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
73 WRBUF w = wrbuf_alloc();
77 r = yaz_sort_spec_to_type7(sort_spec, w);
79 if (!expected_result && r)
81 else if (expected_result && r == 0)
83 if (strcmp(wrbuf_cstr(w), expected_result) == 0)
87 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
88 yaz_log(YLOG_WARN, " expected %s", expected_result);
89 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
94 yaz_log(YLOG_WARN, "sort: diff %s", arg);
95 yaz_log(YLOG_WARN, " expected %s", expected_result);
96 yaz_log(YLOG_WARN, " got error %d", r);
100 yaz_log(YLOG_WARN, "sort: diff %s", arg);
101 yaz_log(YLOG_WARN, " expected error");
102 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
110 static int strategy_sortkeys(const char *arg, const char *expected_result, int (*strategy) (Z_SortKeySpecList *, WRBUF))
112 ODR odr = odr_createmem(ODR_ENCODE);
113 Z_SortKeySpecList *sort_spec = yaz_sort_spec(odr, arg);
118 yaz_log(YLOG_WARN, "yaz_sort_spec : parse error: %s", arg);
122 WRBUF w = wrbuf_alloc();
123 int r = (strategy)(sort_spec, w);
125 if (!expected_result && r)
127 else if (expected_result && r == 0)
129 if (strcmp(wrbuf_cstr(w), expected_result) == 0)
133 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
134 yaz_log(YLOG_WARN, " expected %s", expected_result);
135 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
140 yaz_log(YLOG_WARN, "sort: diff %s", arg);
141 yaz_log(YLOG_WARN, " expected %s", expected_result);
142 yaz_log(YLOG_WARN, " got error %d", r);
146 yaz_log(YLOG_WARN, "sort: diff %s", arg);
147 yaz_log(YLOG_WARN, " expected error");
148 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
156 static int srw_sortkeys(const char *arg, const char *expected_result) {
157 return strategy_sortkeys(arg, expected_result, yaz_sort_spec_to_srw_sortkeys);
160 static int solr_sortkeys(const char *arg, const char *expected_result) {
161 return strategy_sortkeys(arg, expected_result, yaz_sort_spec_to_solr_sortkeys);
166 static int check_sortkeys_to_sort_spec(const char *arg,
167 const char *expected_result, int (*sort_strategy)(const char *, WRBUF))
169 WRBUF w = wrbuf_alloc();
171 int r = sort_strategy(arg, w);
173 if (!expected_result && r)
175 else if (expected_result && r == 0)
177 if (strcmp(wrbuf_cstr(w), expected_result) == 0)
181 yaz_log(YLOG_WARN, "sort: diff: %s", arg);
182 yaz_log(YLOG_WARN, " expected %s", expected_result);
183 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
188 yaz_log(YLOG_WARN, "sort: diff %s", arg);
189 yaz_log(YLOG_WARN, " expected %s", expected_result);
190 yaz_log(YLOG_WARN, " got error %d", r);
194 yaz_log(YLOG_WARN, "sort: diff %s", arg);
195 yaz_log(YLOG_WARN, " expected error");
196 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w));
202 static int check_srw_sortkeys_to_sort_spec(const char *arg, const char *expected_result) {
203 return check_sortkeys_to_sort_spec(arg, expected_result, yaz_srw_sortkeys_to_sort_spec);
206 static int check_solr_sortkeys_to_sort_spec(const char *arg, const char *expected_result) {
207 return check_sortkeys_to_sort_spec(arg, expected_result, yaz_solr_sortkeys_to_sort_spec);
210 static void tst(void)
212 YAZ_CHECK(cql("title a",
213 " SORTBY title/ascending/ignoreCase"));
214 YAZ_CHECK(cql("title a date ds",
215 " SORTBY title/ascending/ignoreCase"
216 " date/descending/respectCase"));
217 YAZ_CHECK(cql("1=4,2=3 a", 0));
218 YAZ_CHECK(cql("date a=1900",
219 " SORTBY date/ascending/ignoreCase/missingValue=1900"));
221 YAZ_CHECK(type7("title a",
222 "@or q @attr 1=title @attr 7=1 0"));
223 YAZ_CHECK(type7("title a date ds",
224 "@or @or q @attr 1=title @attr 7=1 0"
225 " @attr 1=date @attr 7=2 1"));
226 YAZ_CHECK(type7("1=4,2=3 a",
227 "@or q @attr 1=4 @attr 2=3 @attr 7=1 0"));
228 YAZ_CHECK(type7("date a=1900",
229 "@or q @attr 1=date @attr 7=1 0"));
231 YAZ_CHECK(srw_sortkeys("title a",
232 "title,,1,0,highValue"));
233 YAZ_CHECK(srw_sortkeys("title a date ds",
234 "title,,1,0,highValue "
235 "date,,0,1,highValue"));
236 YAZ_CHECK(srw_sortkeys("1=4,2=3 a", 0));
237 YAZ_CHECK(srw_sortkeys("date a=1900",
239 YAZ_CHECK(check_srw_sortkeys_to_sort_spec(
243 YAZ_CHECK(solr_sortkeys("title a",
245 YAZ_CHECK(solr_sortkeys("title a date ds",
248 YAZ_CHECK(solr_sortkeys("1=4,2=3 a", 0));
250 YAZ_CHECK(check_solr_sortkeys_to_sort_spec(
256 int main(int argc, char **argv)
258 YAZ_CHECK_INIT(argc, argv);
266 * c-file-style: "Stroustrup"
267 * indent-tabs-mode: nil
269 * vim: shiftwidth=4 tabstop=8 expandtab