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");
91 ccl_qual_fitem(bibset, "s=sl u=2 u=3", "s2");
94 strcpy(tstline, "ti u=4 s=pw t=l,r");
95 ccl_qual_line(bibset, tstline);
97 strcpy(tstline, "term 1=1016 s=al,pw t=r # default term");
98 ccl_qual_line(bibset, tstline);
100 strcpy(tstline, "reg t=x");
101 ccl_qual_line(bibset, tstline);
103 strcpy(tstline, "z t=z");
104 ccl_qual_line(bibset, tstline);
106 strcpy(tstline, "dc.title 1=/my/title");
107 ccl_qual_line(bibset, tstline);
109 strcpy(tstline, "date r=r,omiteq # ordered relation");
110 ccl_qual_line(bibset, tstline);
112 strcpy(tstline, "x r=o # ordered relation");
113 ccl_qual_line(bibset, tstline);
115 strcpy(tstline, "title dc.title # alias");
116 ccl_qual_line(bibset, tstline);
118 strcpy(tstline, "comb term dc.title # combination");
119 ccl_qual_line(bibset, tstline);
121 strcpy(tstline, "ag s=ag");
122 ccl_qual_line(bibset, tstline);
124 strcpy(tstline, "splitlist s=sl u=2");
125 ccl_qual_line(bibset, tstline);
127 strcpy(tstline, "s2 s=sl u=2 u=3");
128 ccl_qual_line(bibset, tstline);
131 ccl_qual_buf(bibset, "ti u=4 s=pw t=l,r\n"
132 "term 1=1016 s=al,pw t=r\r\n"
136 "dc.title 1=/my/title\n"
140 "comb term dc.title\n"
142 "splitlist s=sl u=2\n"
152 const char *addinfo = 0;
153 const char *xml_str =
155 " <qual name=\"ti\">\n"
156 " <attr type=\"u\" value=\"4\"/>\n"
157 " <attr type=\"s\" value=\"pw\"/>\n"
158 " <attr type=\"t\" value=\"l,r\"/>\n"
160 " <qual name=\"term\">\n"
161 " <attr type=\"1\" value=\"1016\"/>\n"
162 " <attr type=\"s\" value=\"al,pw\"/>\n"
163 " <attr type=\"t\" value=\"r\"/>\n"
165 " <qual name=\"reg\">\n"
166 " <attr type=\"t\" value=\"x\"/>\n"
168 " <qual name=\"z\">\n"
169 " <attr type=\"t\" value=\"z\"/>\n"
171 " <qual name=\"dc.title\">\n"
172 " <attr type=\"1\" value=\"/my/title\"/>\n"
174 " <qual name=\"date\">\n"
175 " <attr type=\"r\" value=\"r,omiteq\"/>\n"
177 " <qual name=\"x\">\n"
178 " <attr type=\"r\" value=\"o\"/>\n"
180 " <qual name=\"title\">\n"
181 " <qual name=\"dc.title\"/>\n"
183 " <qual name=\"comb\">\n"
184 " <qual name=\"term\"/>\n"
185 " <qual name=\"dc.title\"/>\n"
187 " <qual name=\"ag\">\n"
188 " <attr type=\"s\" value=\"ag\"/>\n"
190 " <qual name=\"splitlist\">\n"
191 " <attr type=\"s\" value=\"sl\"/>\n"
192 " <attr type=\"u\" value=\"2\"/>\n"
194 " <qual name=\"s2\">\n"
195 " <attr type=\"s\" value=\"sl\"/>\n"
196 " <attr type=\"u\" value=\"2\"/>\n"
197 " <attr type=\"u\" value=\"3\"/>\n"
201 doc = xmlParseMemory(xml_str, strlen(xml_str));
204 r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
218 YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
220 YAZ_CHECK(tst_ccl_query(bibset, "k\xc3\xb8" "benhavn", "@attr 4=2 @attr 1=1016 k\xc3\xb8" "benhavn "));
222 YAZ_CHECK(tst_ccl_query(bibset, "k\xf8" "benhavn", "@attr 4=2 @attr 1=1016 ""k\xf8" "benhavn "));
224 YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
225 YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
227 "@attr 4=2 @attr 1=1016 x1 "
228 "@attr 4=2 @attr 1=1016 x2 "));
229 YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
230 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
231 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=(x4)", "@attr 1=/my/title x4 "));
232 YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
233 YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
235 YAZ_CHECK(tst_ccl_query(bibset, "a%b",
237 "@attr 4=2 @attr 1=1016 a "
238 "@attr 4=2 @attr 1=1016 b "));
239 YAZ_CHECK(tst_ccl_query(bibset, "a%(b)",
241 "@attr 4=2 @attr 1=1016 a "
242 "@attr 4=2 @attr 1=1016 b "));
243 YAZ_CHECK(tst_ccl_query(bibset, "(a)%(b)",
245 "@attr 4=2 @attr 1=1016 a "
246 "@attr 4=2 @attr 1=1016 b "));
247 YAZ_CHECK(tst_ccl_query(bibset, "a%1b",
249 "@attr 4=2 @attr 1=1016 a "
250 "@attr 4=2 @attr 1=1016 b "));
252 YAZ_CHECK(tst_ccl_query(bibset, "a%2b",
254 "@attr 4=2 @attr 1=1016 a "
255 "@attr 4=2 @attr 1=1016 b "));
257 YAZ_CHECK(tst_ccl_query(bibset, "(a)%2(b)",
259 "@attr 4=2 @attr 1=1016 a "
260 "@attr 4=2 @attr 1=1016 b "));
262 YAZ_CHECK(tst_ccl_query(bibset, "a%19b",
263 "@prox 0 19 0 2 k 2 "
264 "@attr 4=2 @attr 1=1016 a "
265 "@attr 4=2 @attr 1=1016 b "));
267 YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne",
269 "@attr 4=2 @attr 1=1016 spid "
270 "@attr 4=2 @attr 1=1016 æserne "));
272 YAZ_CHECK(tst_ccl_query(bibset, "a!b",
274 "@attr 4=2 @attr 1=1016 a "
275 "@attr 4=2 @attr 1=1016 b "));
276 YAZ_CHECK(tst_ccl_query(bibset, "a!2b",
278 "@attr 4=2 @attr 1=1016 a "
279 "@attr 4=2 @attr 1=1016 b "));
281 YAZ_CHECK(tst_ccl_query(bibset, "a% (b or dc.title=c)",
283 "@attr 4=2 @attr 1=1016 a "
284 "@or @attr 4=2 @attr 1=1016 b "
285 "@attr 4=2 @attr 1=/my/title c "
288 YAZ_CHECK(tst_ccl_query(bibset, "(a b) % (c)",
289 "@prox 0 1 0 2 k 2 @and "
290 "@attr 4=2 @attr 1=1016 a @attr 4=2 @attr 1=1016 b "
291 "@attr 4=2 @attr 1=1016 c " ));
293 YAZ_CHECK(tst_ccl_query(bibset, "date=1980",
295 YAZ_CHECK(tst_ccl_query(bibset, "(date=1980)",
297 YAZ_CHECK(tst_ccl_query(bibset, "date>1980",
299 YAZ_CHECK(tst_ccl_query(bibset, "date>=1980",
301 YAZ_CHECK(tst_ccl_query(bibset, "date<1980",
303 YAZ_CHECK(tst_ccl_query(bibset, "date<=1980",
305 YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990",
306 "@and @attr 2=4 234 @attr 2=2 1990 "));
307 YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990",
308 "@and @attr 2=4 234 @attr 2=2 1990 "));
309 YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990",
310 "@and @attr 2=4 234 @attr 2=2 1990 "));
311 YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990",
312 "@and @attr 2=4 234 @attr 2=2 1990 "));
313 YAZ_CHECK(tst_ccl_query(bibset, "date=-1980",
315 YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980",
317 YAZ_CHECK(tst_ccl_query(bibset, "x=-1980",
318 "@attr 2=3 -1980 "));
319 YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
321 YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
322 "@attr 2=3 -1980 "));
323 YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
324 "@attr 2=3 234-1990 "));
325 YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
326 "@and @attr 2=4 234 @attr 2=2 1990 "));
327 YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
328 "@attr 4=1 @attr 1=4 a,b "));
329 YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b",
330 "@attr 4=1 @attr 1=4 \"a, b\" "));
331 YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b",
332 "@attr 4=2 @attr 1=4 a-b "));
333 YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
334 "@attr 4=1 @attr 1=4 \"a - b\" "));
336 YAZ_CHECK(tst_ccl_query(bibset, "a?",
337 "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
338 YAZ_CHECK(tst_ccl_query(bibset, "a b",
339 "@and @attr 4=2 @attr 1=1016 a "
340 "@attr 4=2 @attr 1=1016 b "));
342 YAZ_CHECK(tst_ccl_query(bibset, "a b?",
343 "@and @attr 4=2 @attr 1=1016 a "
344 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
346 YAZ_CHECK(tst_ccl_query(bibset, "title=a",
347 "@attr 1=/my/title a "));
349 YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"",
350 "@attr 5=102 a.*b.c\\\\? "));
351 YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"",
352 "@attr 5=104 a?b#c\\\\? "));
354 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(",
356 YAZ_CHECK(tst_ccl_query(bibset, "z=\\(",
359 YAZ_CHECK(tst_ccl_query(bibset, "z=a b#",
360 "@attr 5=104 \"a b#\" "));
362 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"",
364 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"",
367 YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
369 YAZ_CHECK(tst_ccl_query(bibset, "z=.",
372 YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"",
374 YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"",
377 YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
378 "@attr 5=102 .*\\\\? "));
379 YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
380 "@attr 5=104 ?\\\\? "));
382 YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
384 YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
387 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
389 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
392 YAZ_CHECK(tst_ccl_query(bibset, "\\\\",
393 "@attr 4=2 @attr 1=1016 \\\\ "));
395 YAZ_CHECK(tst_ccl_query(bibset, "comb=a",
396 "@or @attr 4=2 @attr 1=1016 a "
397 "@attr 1=/my/title a "));
399 YAZ_CHECK(tst_ccl_query(bibset, "a? b?",
400 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
401 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
403 YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"",
404 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
405 "@attr 4=2 @attr 1=1016 b? "));
407 YAZ_CHECK(tst_ccl_query(bibset, "@and",
408 "@attr 4=2 @attr 1=1016 \\@and "));
410 YAZ_CHECK(tst_ccl_query(bibset, "a@and",
411 "@attr 4=2 @attr 1=1016 a@and "));
413 YAZ_CHECK(tst_ccl_query(bibset, "}",
414 "@attr 4=2 @attr 1=1016 } "));
416 YAZ_CHECK(tst_ccl_query(bibset, "{",
417 "@attr 4=2 @attr 1=1016 \"{\" "));
419 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
420 "@attr 4=1 @attr 1=1016 \"a b c\" "));
422 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c \"",
423 "@attr 4=1 @attr 1=1016 \"a b c \" "));
425 YAZ_CHECK(tst_ccl_query(bibset, "ag=a",
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\"",
432 "@and @attr 4=2 \"a b\" @attr 4=1 \"c d\" "));
434 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"c\"",
435 "@attr 4=2 \"a b c\" "));
437 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"\"",
438 "@attr 4=2 \"a b \" "));
440 YAZ_CHECK(tst_ccl_query(bibset, "ag=a \"b c\" d",
442 "@attr 4=2 a @attr 4=1 \"b c\" @attr 4=2 d "));
444 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\"",
445 "@attr 4=1 \"a b c\" "));
447 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\" \"d e\"",
448 "@and @attr 4=1 \"a b c\" @attr 4=1 \"d e\" "));
450 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a", "@attr 1=2 a "));
451 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b", "@or "
452 "@and @attr 1=2 a @attr 1=2 b @attr 1=2 \"a b\" "));
454 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b c", "@or @or "
457 "@or @and @attr 1=2 b @attr 1=2 c "
459 "@and @attr 1=2 \"a b\" @attr 1=2 c "
460 "@attr 1=2 \"a b c\" "));
462 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a?", 0));
463 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b?", 0));
464 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b c?", 0));
465 YAZ_CHECK(tst_ccl_query(bibset, "splitlist=a b c d?", 0));
467 YAZ_CHECK(tst_ccl_query(bibset, "s2=a", "@or @attr 1=2 a @attr 1=3 a "));
469 YAZ_CHECK(tst_ccl_query(bibset, "s2=a b", "@or "
470 "@and " "@or @attr 1=2 a @attr 1=3 a "
471 "@or @attr 1=2 b @attr 1=3 b "
472 "@or @attr 1=2 \"a b\" @attr 1=3 \"a b\" "));
474 YAZ_CHECK(tst_ccl_query(bibset, "s2=a? b", 0));
475 YAZ_CHECK(tst_ccl_query(bibset, "s2=a b?", 0));
476 YAZ_CHECK(tst_ccl_query(bibset, "s2=a b? c", 0));
478 ccl_qual_rm(&bibset);
483 CCL_bibset bibset = ccl_qual_mk();
489 ccl_qual_fitem(bibset, "u=4 s=pw t=l,r", "ti");
490 ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z", "term");
492 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
493 "@attr 5=104 @attr 4=2 @attr 1=1016 a?# "));
495 YAZ_CHECK(tst_ccl_query(bibset, "a b?#",
496 "@and @attr 4=2 @attr 1=1016 a @attr 5=104 @attr 4=2 @attr 1=1016 b?# "));
498 YAZ_CHECK(tst_ccl_query(bibset, "a*",
499 "@attr 4=2 @attr 1=1016 a* "));
501 YAZ_CHECK(tst_ccl_query(bibset, "a?",
502 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
504 ccl_qual_fitem(bibset, "*", "@truncation");
505 YAZ_CHECK(tst_ccl_query(bibset, "a*",
506 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
508 YAZ_CHECK(tst_ccl_query(bibset, "a?",
509 "@attr 4=2 @attr 1=1016 a? "));
511 ccl_qual_fitem(bibset, "?", "@mask");
512 YAZ_CHECK(tst_ccl_query(bibset, "a?",
513 "@attr 5=104 @attr 4=2 @attr 1=1016 a# "));
516 ccl_qual_fitem(bibset, "", "@mask");
517 ccl_qual_fitem(bibset, "", "@truncation");
518 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
519 "@attr 4=2 @attr 1=1016 a?# "));
521 ccl_qual_fitem(bibset, "og", "@and");
522 ccl_qual_fitem(bibset, "eller", "@or");
523 ccl_qual_fitem(bibset, "ikke", "@not");
525 YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
526 "@not @or @and @attr 4=2 @attr 1=1016 a "
527 "@attr 4=2 @attr 1=1016 b "
528 "@attr 4=2 @attr 1=1016 c "
529 "@attr 4=2 @attr 1=1016 d "));
530 ccl_qual_rm(&bibset);
536 CCL_bibset b = ccl_qual_mk();
542 ccl_qual_fitem(b, "2=102", "rk");
543 ccl_qual_fitem(b, "1=9903 r=r", "lex");
544 ccl_qual_fitem(b, "4=109 r=o", "st-numeric");
546 YAZ_CHECK(tst_ccl_query(b, "rk=(lex=9)", "@attr 2=3 @attr 1=9903 9 "));
547 YAZ_CHECK(tst_ccl_query(b, "rk=(lex>9)", "@attr 2=5 @attr 1=9903 9 "));
548 YAZ_CHECK(tst_ccl_query(b, "rk=(lex,st-numeric=300-600)",
550 "@attr 2=4 @attr 4=109 @attr 1=9903 300 "
551 "@attr 2=2 @attr 4=109 @attr 1=9903 600 "));
552 YAZ_CHECK(tst_ccl_query(b, "rk=(lex=9) and b",
553 "@and @attr 2=3 @attr 1=9903 9 b "));
554 YAZ_CHECK(tst_ccl_query(b, "rk=(lex=9 and b)",
555 "@and @attr 2=3 @attr 1=9903 9 @attr 2=102 b "));
559 void tst_addinfo(void)
563 CCL_bibset bibset = ccl_qual_mk();
565 r = ccl_qual_fitem2(bibset, "u=4 s=pw t=l,r", "ti", &addinfo);
566 YAZ_CHECK(r == 0 && addinfo == 0);
568 r = ccl_qual_fitem2(bibset, "1=1016 s=al,pw t=z", "term", &addinfo);
569 YAZ_CHECK(r == 0 && addinfo == 0);
571 r = ccl_qual_fitem2(bibset, "x=", "term", &addinfo);
572 YAZ_CHECK(r != 0 && addinfo != 0);
574 r = ccl_qual_fitem2(bibset, "12=3", "term", &addinfo);
575 YAZ_CHECK(r == 0 && addinfo == 0);
577 r = ccl_qual_fitem2(bibset, "ab=3", "term", &addinfo);
578 YAZ_CHECK(r != 0 && addinfo != 0);
580 r = ccl_qual_fitem2(bibset, "x=ab", "term", &addinfo);
581 YAZ_CHECK(r != 0 && addinfo != 0);
583 r = ccl_qual_fitem2(bibset, "s=ab", "term", &addinfo);
584 YAZ_CHECK(r == 0 && addinfo == 0);
586 ccl_qual_rm(&bibset);
589 int main(int argc, char **argv)
591 YAZ_CHECK_INIT(argc, argv);
605 * c-file-style: "Stroustrup"
606 * indent-tabs-mode: nil
608 * vim: shiftwidth=4 tabstop=8 expandtab