1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
10 #include <yaz/ccl_xml.h>
15 static int tst_ccl_query(CCL_bibset bibset,
19 CCL_parser parser = ccl_parser_create(bibset);
24 struct ccl_rpn_node *rpn;
26 rpn = ccl_parser_find_str(parser, query);
29 /* parse ok. check that result is there and match */
30 WRBUF wrbuf = wrbuf_alloc();
31 ccl_pquery(wrbuf, rpn);
33 /* check expect a result and that it matches */
34 if (result && !strcmp(wrbuf_cstr(wrbuf), result))
38 yaz_log(YLOG_WARN, "%s: result does not match", query);
39 yaz_log(YLOG_WARN, " expected %s", result);
40 yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(wrbuf));
50 yaz_log(YLOG_WARN, "%s: parse failed", query);
57 ccl_parser_destroy (parser);
63 CCL_bibset bibset = ccl_qual_mk();
72 CCL_bibset nbibset = ccl_qual_dup(bibset);
80 ccl_qual_fitem(bibset, "term dc.title", "comb");
81 ccl_qual_fitem(bibset, "u=4 s=pw t=l,r", "ti");
82 ccl_qual_fitem(bibset, "1=1016 s=al,pw t=r", "term");
83 ccl_qual_fitem(bibset, "t=x", "reg");
84 ccl_qual_fitem(bibset, "t=z", "z");
85 ccl_qual_fitem(bibset, "1=/my/title", "dc.title");
86 ccl_qual_fitem(bibset, "r=r,omiteq", "date");
87 ccl_qual_fitem(bibset, "r=o", "x");
88 ccl_qual_fitem(bibset, "dc.title", "title");
89 ccl_qual_fitem(bibset, "s=ag", "ag");
90 ccl_qual_fitem(bibset, "s=sl u=2", "splitlist");
93 strcpy(tstline, "ti u=4 s=pw t=l,r");
94 ccl_qual_line(bibset, tstline);
96 strcpy(tstline, "term 1=1016 s=al,pw t=r # default term");
97 ccl_qual_line(bibset, tstline);
99 strcpy(tstline, "reg t=x");
100 ccl_qual_line(bibset, tstline);
102 strcpy(tstline, "z t=z");
103 ccl_qual_line(bibset, tstline);
105 strcpy(tstline, "dc.title 1=/my/title");
106 ccl_qual_line(bibset, tstline);
108 strcpy(tstline, "date r=r,omiteq # ordered relation");
109 ccl_qual_line(bibset, tstline);
111 strcpy(tstline, "x r=o # ordered relation");
112 ccl_qual_line(bibset, tstline);
114 strcpy(tstline, "title dc.title # alias");
115 ccl_qual_line(bibset, tstline);
117 strcpy(tstline, "comb term dc.title # combination");
118 ccl_qual_line(bibset, tstline);
120 strcpy(tstline, "ag s=ag");
121 ccl_qual_line(bibset, tstline);
123 strcpy(tstline, "splitlist s=sl u=2");
124 ccl_qual_line(bibset, tstline);
127 ccl_qual_buf(bibset, "ti u=4 s=pw t=l,r\n"
128 "term 1=1016 s=al,pw t=r\r\n"
132 "dc.title 1=/my/title\n"
136 "comb term dc.title\n"
138 "splitlist s=sl u=2\n"
147 const char *addinfo = 0;
148 const char *xml_str =
150 " <qual name=\"ti\">\n"
151 " <attr type=\"u\" value=\"4\"/>\n"
152 " <attr type=\"s\" value=\"pw\"/>\n"
153 " <attr type=\"t\" value=\"l,r\"/>\n"
155 " <qual name=\"term\">\n"
156 " <attr type=\"1\" value=\"1016\"/>\n"
157 " <attr type=\"s\" value=\"al,pw\"/>\n"
158 " <attr type=\"t\" value=\"r\"/>\n"
160 " <qual name=\"reg\">\n"
161 " <attr type=\"t\" value=\"x\"/>\n"
163 " <qual name=\"z\">\n"
164 " <attr type=\"t\" value=\"z\"/>\n"
166 " <qual name=\"dc.title\">\n"
167 " <attr type=\"1\" value=\"/my/title\"/>\n"
169 " <qual name=\"date\">\n"
170 " <attr type=\"r\" value=\"r,omiteq\"/>\n"
172 " <qual name=\"x\">\n"
173 " <attr type=\"r\" value=\"o\"/>\n"
175 " <qual name=\"title\">\n"
176 " <qual name=\"dc.title\"/>\n"
178 " <qual name=\"comb\">\n"
179 " <qual name=\"term\"/>\n"
180 " <qual name=\"dc.title\"/>\n"
182 " <qual name=\"ag\">\n"
183 " <attr type=\"s\" value=\"ag\"/>\n"
185 " <qual name=\"splitlist\">\n"
186 " <attr type=\"s\" value=\"sl\"/>\n"
187 " <attr type=\"u\" value=\"2\"/>\n"
191 doc = xmlParseMemory(xml_str, strlen(xml_str));
194 r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
208 YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
210 YAZ_CHECK(tst_ccl_query(bibset, "k\xc3\xb8" "benhavn", "@attr 4=2 @attr 1=1016 k\xc3\xb8" "benhavn "));
212 YAZ_CHECK(tst_ccl_query(bibset, "k\xf8" "benhavn", "@attr 4=2 @attr 1=1016 ""k\xf8" "benhavn "));
214 YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
215 YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
217 "@attr 4=2 @attr 1=1016 x1 "
218 "@attr 4=2 @attr 1=1016 x2 "));
219 YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
220 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
221 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=(x4)", "@attr 1=/my/title x4 "));
222 YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
223 YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
225 YAZ_CHECK(tst_ccl_query(bibset, "a%b",
227 "@attr 4=2 @attr 1=1016 a "
228 "@attr 4=2 @attr 1=1016 b "));
229 YAZ_CHECK(tst_ccl_query(bibset, "a%(b)",
231 "@attr 4=2 @attr 1=1016 a "
232 "@attr 4=2 @attr 1=1016 b "));
233 YAZ_CHECK(tst_ccl_query(bibset, "(a)%(b)",
235 "@attr 4=2 @attr 1=1016 a "
236 "@attr 4=2 @attr 1=1016 b "));
237 YAZ_CHECK(tst_ccl_query(bibset, "a%1b",
239 "@attr 4=2 @attr 1=1016 a "
240 "@attr 4=2 @attr 1=1016 b "));
242 YAZ_CHECK(tst_ccl_query(bibset, "a%2b",
244 "@attr 4=2 @attr 1=1016 a "
245 "@attr 4=2 @attr 1=1016 b "));
247 YAZ_CHECK(tst_ccl_query(bibset, "(a)%2(b)",
249 "@attr 4=2 @attr 1=1016 a "
250 "@attr 4=2 @attr 1=1016 b "));
252 YAZ_CHECK(tst_ccl_query(bibset, "a%19b",
253 "@prox 0 19 0 2 k 2 "
254 "@attr 4=2 @attr 1=1016 a "
255 "@attr 4=2 @attr 1=1016 b "));
257 YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne",
259 "@attr 4=2 @attr 1=1016 spid "
260 "@attr 4=2 @attr 1=1016 æserne "));
262 YAZ_CHECK(tst_ccl_query(bibset, "a!b",
264 "@attr 4=2 @attr 1=1016 a "
265 "@attr 4=2 @attr 1=1016 b "));
266 YAZ_CHECK(tst_ccl_query(bibset, "a!2b",
268 "@attr 4=2 @attr 1=1016 a "
269 "@attr 4=2 @attr 1=1016 b "));
271 YAZ_CHECK(tst_ccl_query(bibset, "a% (b or dc.title=c)",
273 "@attr 4=2 @attr 1=1016 a "
274 "@or @attr 4=2 @attr 1=1016 b "
275 "@or @attr 4=2 @attr 1=/my/title c "
276 "@attr 4=2 @attr 1=1016 c "
279 YAZ_CHECK(tst_ccl_query(bibset, "(a b) % (c)",
280 "@prox 0 1 0 2 k 2 @and "
281 "@attr 4=2 @attr 1=1016 a @attr 4=2 @attr 1=1016 b "
282 "@attr 4=2 @attr 1=1016 c " ));
284 YAZ_CHECK(tst_ccl_query(bibset, "date=1980",
286 YAZ_CHECK(tst_ccl_query(bibset, "(date=1980)",
288 YAZ_CHECK(tst_ccl_query(bibset, "date>1980",
290 YAZ_CHECK(tst_ccl_query(bibset, "date>=1980",
292 YAZ_CHECK(tst_ccl_query(bibset, "date<1980",
294 YAZ_CHECK(tst_ccl_query(bibset, "date<=1980",
296 YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990",
297 "@and @attr 2=4 234 @attr 2=2 1990 "));
298 YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990",
299 "@and @attr 2=4 234 @attr 2=2 1990 "));
300 YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990",
301 "@and @attr 2=4 234 @attr 2=2 1990 "));
302 YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990",
303 "@and @attr 2=4 234 @attr 2=2 1990 "));
304 YAZ_CHECK(tst_ccl_query(bibset, "date=-1980",
306 YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980",
308 YAZ_CHECK(tst_ccl_query(bibset, "x=-1980",
309 "@attr 2=3 -1980 "));
310 YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
312 YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
313 "@attr 2=3 -1980 "));
314 YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
315 "@attr 2=3 234-1990 "));
316 YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
317 "@and @attr 2=4 234 @attr 2=2 1990 "));
318 YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
319 "@attr 4=1 @attr 1=4 a,b "));
320 YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b",
321 "@attr 4=1 @attr 1=4 \"a, b\" "));
322 YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b",
323 "@attr 4=2 @attr 1=4 a-b "));
324 YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
325 "@attr 4=1 @attr 1=4 \"a - b\" "));
327 YAZ_CHECK(tst_ccl_query(bibset, "a?",
328 "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
329 YAZ_CHECK(tst_ccl_query(bibset, "a b",
330 "@and @attr 4=2 @attr 1=1016 a "
331 "@attr 4=2 @attr 1=1016 b "));
333 YAZ_CHECK(tst_ccl_query(bibset, "a b?",
334 "@and @attr 4=2 @attr 1=1016 a "
335 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
337 YAZ_CHECK(tst_ccl_query(bibset, "title=a",
338 "@attr 1=/my/title a "));
340 YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"",
341 "@attr 5=102 a.*b.c\\\\? "));
342 YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"",
343 "@attr 5=104 a?b#c\\\\? "));
345 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(",
347 YAZ_CHECK(tst_ccl_query(bibset, "z=\\(",
350 YAZ_CHECK(tst_ccl_query(bibset, "z=a b#",
351 "@attr 5=104 \"a b#\" "));
353 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"",
355 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"",
358 YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
360 YAZ_CHECK(tst_ccl_query(bibset, "z=.",
363 YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"",
365 YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"",
368 YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
369 "@attr 5=102 .*\\\\? "));
370 YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
371 "@attr 5=104 ?\\\\? "));
373 YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
375 YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
378 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
380 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
383 YAZ_CHECK(tst_ccl_query(bibset, "\\\\",
384 "@attr 4=2 @attr 1=1016 \\\\ "));
386 YAZ_CHECK(tst_ccl_query(bibset, "comb=a",
387 "@or @attr 4=2 @attr 1=1016 a "
388 "@attr 1=/my/title a "));
390 YAZ_CHECK(tst_ccl_query(bibset, "a? b?",
391 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
392 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
394 YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"",
395 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
396 "@attr 4=2 @attr 1=1016 b? "));
398 YAZ_CHECK(tst_ccl_query(bibset, "@and",
399 "@attr 4=2 @attr 1=1016 \\@and "));
401 YAZ_CHECK(tst_ccl_query(bibset, "a@and",
402 "@attr 4=2 @attr 1=1016 a@and "));
404 YAZ_CHECK(tst_ccl_query(bibset, "}",
405 "@attr 4=2 @attr 1=1016 } "));
407 YAZ_CHECK(tst_ccl_query(bibset, "{",
408 "@attr 4=2 @attr 1=1016 \"{\" "));
410 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
411 "@attr 4=1 @attr 1=1016 \"a b c\" "));
413 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c \"",
414 "@attr 4=1 @attr 1=1016 \"a b c \" "));
416 YAZ_CHECK(tst_ccl_query(bibset, "ag=a",
419 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b",
420 "@attr 4=2 \"a b\" "));
422 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"c d\"",
423 "@and @attr 4=2 \"a b\" @attr 4=1 \"c d\" "));
425 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"c\"",
426 "@attr 4=2 \"a b c\" "));
428 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"\"",
429 "@attr 4=2 \"a b \" "));
431 YAZ_CHECK(tst_ccl_query(bibset, "ag=a \"b c\" d",
433 "@attr 4=2 a @attr 4=1 \"b c\" @attr 4=2 d "));
435 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\"",
436 "@attr 4=1 \"a b c\" "));
438 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\" \"d e\"",
439 "@and @attr 4=1 \"a b c\" @attr 4=1 \"d e\" "));
441 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a", "@attr 1=2 a "));
442 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b", "@or "
443 "@and @attr 1=2 a @attr 1=2 b @attr 1=2 \"a b\" "));
444 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b c", "@or @or @or "
445 "@and @and @attr 1=2 a @attr 1=2 b @attr 1=2 c "
446 "@and @attr 1=2 a @attr 1=2 \"b c\" "
447 "@and @attr 1=2 \"a b\" @attr 1=2 c "
448 "@attr 1=2 \"a b c\" "));
449 ccl_qual_rm(&bibset);
454 CCL_bibset bibset = ccl_qual_mk();
460 ccl_qual_fitem(bibset, "u=4 s=pw t=l,r", "ti");
461 ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z", "term");
463 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
464 "@attr 5=104 @attr 4=2 @attr 1=1016 a?# "));
466 YAZ_CHECK(tst_ccl_query(bibset, "a b?#",
467 "@and @attr 4=2 @attr 1=1016 a @attr 5=104 @attr 4=2 @attr 1=1016 b?# "));
469 YAZ_CHECK(tst_ccl_query(bibset, "a*",
470 "@attr 4=2 @attr 1=1016 a* "));
472 YAZ_CHECK(tst_ccl_query(bibset, "a?",
473 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
475 ccl_qual_fitem(bibset, "*", "@truncation");
476 YAZ_CHECK(tst_ccl_query(bibset, "a*",
477 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
479 YAZ_CHECK(tst_ccl_query(bibset, "a?",
480 "@attr 4=2 @attr 1=1016 a? "));
482 ccl_qual_fitem(bibset, "?", "@mask");
483 YAZ_CHECK(tst_ccl_query(bibset, "a?",
484 "@attr 5=104 @attr 4=2 @attr 1=1016 a# "));
487 ccl_qual_fitem(bibset, "", "@mask");
488 ccl_qual_fitem(bibset, "", "@truncation");
489 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
490 "@attr 4=2 @attr 1=1016 a?# "));
492 ccl_qual_fitem(bibset, "og", "@and");
493 ccl_qual_fitem(bibset, "eller", "@or");
494 ccl_qual_fitem(bibset, "ikke", "@not");
496 YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
497 "@not @or @and @attr 4=2 @attr 1=1016 a "
498 "@attr 4=2 @attr 1=1016 b "
499 "@attr 4=2 @attr 1=1016 c "
500 "@attr 4=2 @attr 1=1016 d "));
501 ccl_qual_rm(&bibset);
505 void tst_addinfo(void)
509 CCL_bibset bibset = ccl_qual_mk();
511 r = ccl_qual_fitem2(bibset, "u=4 s=pw t=l,r", "ti", &addinfo);
512 YAZ_CHECK(r == 0 && addinfo == 0);
514 r = ccl_qual_fitem2(bibset, "1=1016 s=al,pw t=z", "term", &addinfo);
515 YAZ_CHECK(r == 0 && addinfo == 0);
517 r = ccl_qual_fitem2(bibset, "x=", "term", &addinfo);
518 YAZ_CHECK(r != 0 && addinfo != 0);
520 r = ccl_qual_fitem2(bibset, "12=3", "term", &addinfo);
521 YAZ_CHECK(r == 0 && addinfo == 0);
523 r = ccl_qual_fitem2(bibset, "ab=3", "term", &addinfo);
524 YAZ_CHECK(r != 0 && addinfo != 0);
526 r = ccl_qual_fitem2(bibset, "x=ab", "term", &addinfo);
527 YAZ_CHECK(r != 0 && addinfo != 0);
529 r = ccl_qual_fitem2(bibset, "s=ab", "term", &addinfo);
530 YAZ_CHECK(r == 0 && addinfo == 0);
532 ccl_qual_rm(&bibset);
535 int main(int argc, char **argv)
537 YAZ_CHECK_INIT(argc, argv);
550 * c-file-style: "Stroustrup"
551 * indent-tabs-mode: nil
553 * vim: shiftwidth=4 tabstop=8 expandtab