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");
92 strcpy(tstline, "ti u=4 s=pw t=l,r");
93 ccl_qual_line(bibset, tstline);
95 strcpy(tstline, "term 1=1016 s=al,pw t=r # default term");
96 ccl_qual_line(bibset, tstline);
98 strcpy(tstline, "reg t=x");
99 ccl_qual_line(bibset, tstline);
101 strcpy(tstline, "z t=z");
102 ccl_qual_line(bibset, tstline);
104 strcpy(tstline, "dc.title 1=/my/title");
105 ccl_qual_line(bibset, tstline);
107 strcpy(tstline, "date r=r,omiteq # ordered relation");
108 ccl_qual_line(bibset, tstline);
110 strcpy(tstline, "x r=o # ordered relation");
111 ccl_qual_line(bibset, tstline);
113 strcpy(tstline, "title dc.title # alias");
114 ccl_qual_line(bibset, tstline);
116 strcpy(tstline, "comb term dc.title # combination");
117 ccl_qual_line(bibset, tstline);
119 strcpy(tstline, "ag s=ag");
120 ccl_qual_line(bibset, tstline);
123 ccl_qual_buf(bibset, "ti u=4 s=pw t=l,r\n"
124 "term 1=1016 s=al,pw t=r\r\n"
128 "dc.title 1=/my/title\n"
132 "comb term dc.title\n"
142 const char *addinfo = 0;
143 const char *xml_str =
145 " <qual name=\"ti\">\n"
146 " <attr type=\"u\" value=\"4\"/>\n"
147 " <attr type=\"s\" value=\"pw\"/>\n"
148 " <attr type=\"t\" value=\"l,r\"/>\n"
150 " <qual name=\"term\">\n"
151 " <attr type=\"1\" value=\"1016\"/>\n"
152 " <attr type=\"s\" value=\"al,pw\"/>\n"
153 " <attr type=\"t\" value=\"r\"/>\n"
155 " <qual name=\"reg\">\n"
156 " <attr type=\"t\" value=\"x\"/>\n"
158 " <qual name=\"z\">\n"
159 " <attr type=\"t\" value=\"z\"/>\n"
161 " <qual name=\"dc.title\">\n"
162 " <attr type=\"1\" value=\"/my/title\"/>\n"
164 " <qual name=\"date\">\n"
165 " <attr type=\"r\" value=\"r,omiteq\"/>\n"
167 " <qual name=\"x\">\n"
168 " <attr type=\"r\" value=\"o\"/>\n"
170 " <qual name=\"title\">\n"
171 " <qual name=\"dc.title\"/>\n"
173 " <qual name=\"comb\">\n"
174 " <qual name=\"term\"/>\n"
175 " <qual name=\"dc.title\"/>\n"
177 " <qual name=\"ag\">\n"
178 " <attr type=\"s\" value=\"ag\"/>\n"
182 doc = xmlParseMemory(xml_str, strlen(xml_str));
185 r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
199 YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
201 YAZ_CHECK(tst_ccl_query(bibset, "k\xc3\xb8" "benhavn", "@attr 4=2 @attr 1=1016 k\xc3\xb8" "benhavn "));
203 YAZ_CHECK(tst_ccl_query(bibset, "k\xf8" "benhavn", "@attr 4=2 @attr 1=1016 ""k\xf8" "benhavn "));
205 YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
206 YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
208 "@attr 4=2 @attr 1=1016 x1 "
209 "@attr 4=2 @attr 1=1016 x2 "));
210 YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
211 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
212 YAZ_CHECK(tst_ccl_query(bibset, "dc.title=(x4)", "@attr 1=/my/title x4 "));
213 YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
214 YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
216 YAZ_CHECK(tst_ccl_query(bibset, "a%b",
218 "@attr 4=2 @attr 1=1016 a "
219 "@attr 4=2 @attr 1=1016 b "));
220 YAZ_CHECK(tst_ccl_query(bibset, "a%(b)",
222 "@attr 4=2 @attr 1=1016 a "
223 "@attr 4=2 @attr 1=1016 b "));
224 YAZ_CHECK(tst_ccl_query(bibset, "(a)%(b)",
226 "@attr 4=2 @attr 1=1016 a "
227 "@attr 4=2 @attr 1=1016 b "));
228 YAZ_CHECK(tst_ccl_query(bibset, "a%1b",
230 "@attr 4=2 @attr 1=1016 a "
231 "@attr 4=2 @attr 1=1016 b "));
233 YAZ_CHECK(tst_ccl_query(bibset, "a%2b",
235 "@attr 4=2 @attr 1=1016 a "
236 "@attr 4=2 @attr 1=1016 b "));
238 YAZ_CHECK(tst_ccl_query(bibset, "(a)%2(b)",
240 "@attr 4=2 @attr 1=1016 a "
241 "@attr 4=2 @attr 1=1016 b "));
243 YAZ_CHECK(tst_ccl_query(bibset, "a%19b",
244 "@prox 0 19 0 2 k 2 "
245 "@attr 4=2 @attr 1=1016 a "
246 "@attr 4=2 @attr 1=1016 b "));
248 YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne",
250 "@attr 4=2 @attr 1=1016 spid "
251 "@attr 4=2 @attr 1=1016 æserne "));
253 YAZ_CHECK(tst_ccl_query(bibset, "a!b",
255 "@attr 4=2 @attr 1=1016 a "
256 "@attr 4=2 @attr 1=1016 b "));
257 YAZ_CHECK(tst_ccl_query(bibset, "a!2b",
259 "@attr 4=2 @attr 1=1016 a "
260 "@attr 4=2 @attr 1=1016 b "));
262 YAZ_CHECK(tst_ccl_query(bibset, "a% (b or dc.title=c)",
264 "@attr 4=2 @attr 1=1016 a "
265 "@or @attr 4=2 @attr 1=1016 b "
266 "@or @attr 4=2 @attr 1=/my/title c "
267 "@attr 4=2 @attr 1=1016 c "
270 YAZ_CHECK(tst_ccl_query(bibset, "(a b) % (c)",
271 "@prox 0 1 0 2 k 2 @and "
272 "@attr 4=2 @attr 1=1016 a @attr 4=2 @attr 1=1016 b "
273 "@attr 4=2 @attr 1=1016 c " ));
275 YAZ_CHECK(tst_ccl_query(bibset, "date=1980",
277 YAZ_CHECK(tst_ccl_query(bibset, "(date=1980)",
279 YAZ_CHECK(tst_ccl_query(bibset, "date>1980",
281 YAZ_CHECK(tst_ccl_query(bibset, "date>=1980",
283 YAZ_CHECK(tst_ccl_query(bibset, "date<1980",
285 YAZ_CHECK(tst_ccl_query(bibset, "date<=1980",
287 YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990",
288 "@and @attr 2=4 234 @attr 2=2 1990 "));
289 YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990",
290 "@and @attr 2=4 234 @attr 2=2 1990 "));
291 YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990",
292 "@and @attr 2=4 234 @attr 2=2 1990 "));
293 YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990",
294 "@and @attr 2=4 234 @attr 2=2 1990 "));
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, "x=-1980",
300 "@attr 2=3 -1980 "));
301 YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
303 YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
304 "@attr 2=3 -1980 "));
305 YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
306 "@attr 2=3 234-1990 "));
307 YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
308 "@and @attr 2=4 234 @attr 2=2 1990 "));
309 YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
310 "@attr 4=1 @attr 1=4 a,b "));
311 YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b",
312 "@attr 4=1 @attr 1=4 \"a, b\" "));
313 YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b",
314 "@attr 4=2 @attr 1=4 a-b "));
315 YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
316 "@attr 4=1 @attr 1=4 \"a - b\" "));
318 YAZ_CHECK(tst_ccl_query(bibset, "a?",
319 "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
320 YAZ_CHECK(tst_ccl_query(bibset, "a b",
321 "@and @attr 4=2 @attr 1=1016 a "
322 "@attr 4=2 @attr 1=1016 b "));
324 YAZ_CHECK(tst_ccl_query(bibset, "a b?",
325 "@and @attr 4=2 @attr 1=1016 a "
326 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
328 YAZ_CHECK(tst_ccl_query(bibset, "title=a",
329 "@attr 1=/my/title a "));
331 YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"",
332 "@attr 5=102 a.*b.c\\\\? "));
333 YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"",
334 "@attr 5=104 a?b#c\\\\? "));
336 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(",
338 YAZ_CHECK(tst_ccl_query(bibset, "z=\\(",
341 YAZ_CHECK(tst_ccl_query(bibset, "z=a b#",
342 "@attr 5=104 \"a b#\" "));
344 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"",
346 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"",
349 YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
351 YAZ_CHECK(tst_ccl_query(bibset, "z=.",
354 YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"",
356 YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"",
359 YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
360 "@attr 5=102 .*\\\\? "));
361 YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
362 "@attr 5=104 ?\\\\? "));
364 YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
366 YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
369 YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
371 YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
374 YAZ_CHECK(tst_ccl_query(bibset, "\\\\",
375 "@attr 4=2 @attr 1=1016 \\\\ "));
377 YAZ_CHECK(tst_ccl_query(bibset, "comb=a",
378 "@or @attr 4=2 @attr 1=1016 a "
379 "@attr 1=/my/title a "));
381 YAZ_CHECK(tst_ccl_query(bibset, "a? b?",
382 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
383 "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
385 YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"",
386 "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
387 "@attr 4=2 @attr 1=1016 b? "));
389 YAZ_CHECK(tst_ccl_query(bibset, "@and",
390 "@attr 4=2 @attr 1=1016 \\@and "));
392 YAZ_CHECK(tst_ccl_query(bibset, "a@and",
393 "@attr 4=2 @attr 1=1016 a@and "));
395 YAZ_CHECK(tst_ccl_query(bibset, "}",
396 "@attr 4=2 @attr 1=1016 } "));
398 YAZ_CHECK(tst_ccl_query(bibset, "{",
399 "@attr 4=2 @attr 1=1016 \"{\" "));
401 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
402 "@attr 4=1 @attr 1=1016 \"a b c\" "));
404 YAZ_CHECK(tst_ccl_query(bibset, "\"a b c \"",
405 "@attr 4=1 @attr 1=1016 \"a b c \" "));
407 YAZ_CHECK(tst_ccl_query(bibset, "ag=a",
410 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b",
411 "@attr 4=2 \"a b\" "));
413 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"c d\"",
414 "@and @attr 4=2 \"a b\" @attr 4=1 \"c d\" "));
416 YAZ_CHECK(tst_ccl_query(bibset, "ag=a b \"c\"",
417 "@attr 4=2 \"a b c\" "));
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",
424 "@attr 4=2 a @attr 4=1 \"b c\" @attr 4=2 d "));
426 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\"",
427 "@attr 4=1 \"a b c\" "));
429 YAZ_CHECK(tst_ccl_query(bibset, "ag=\"a b c\" \"d e\"",
430 "@and @attr 4=1 \"a b c\" @attr 4=1 \"d e\" "));
431 ccl_qual_rm(&bibset);
436 CCL_bibset bibset = ccl_qual_mk();
442 ccl_qual_fitem(bibset, "u=4 s=pw t=l,r", "ti");
443 ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z", "term");
445 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
446 "@attr 5=104 @attr 4=2 @attr 1=1016 a?# "));
448 YAZ_CHECK(tst_ccl_query(bibset, "a b?#",
449 "@and @attr 4=2 @attr 1=1016 a @attr 5=104 @attr 4=2 @attr 1=1016 b?# "));
451 YAZ_CHECK(tst_ccl_query(bibset, "a*",
452 "@attr 4=2 @attr 1=1016 a* "));
454 YAZ_CHECK(tst_ccl_query(bibset, "a?",
455 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
457 ccl_qual_fitem(bibset, "*", "@truncation");
458 YAZ_CHECK(tst_ccl_query(bibset, "a*",
459 "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
461 YAZ_CHECK(tst_ccl_query(bibset, "a?",
462 "@attr 4=2 @attr 1=1016 a? "));
464 ccl_qual_fitem(bibset, "?", "@mask");
465 YAZ_CHECK(tst_ccl_query(bibset, "a?",
466 "@attr 5=104 @attr 4=2 @attr 1=1016 a# "));
469 ccl_qual_fitem(bibset, "", "@mask");
470 ccl_qual_fitem(bibset, "", "@truncation");
471 YAZ_CHECK(tst_ccl_query(bibset, "a?#",
472 "@attr 4=2 @attr 1=1016 a?# "));
474 ccl_qual_fitem(bibset, "og", "@and");
475 ccl_qual_fitem(bibset, "eller", "@or");
476 ccl_qual_fitem(bibset, "ikke", "@not");
478 YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
479 "@not @or @and @attr 4=2 @attr 1=1016 a "
480 "@attr 4=2 @attr 1=1016 b "
481 "@attr 4=2 @attr 1=1016 c "
482 "@attr 4=2 @attr 1=1016 d "));
483 ccl_qual_rm(&bibset);
487 void tst_addinfo(void)
491 CCL_bibset bibset = ccl_qual_mk();
493 r = ccl_qual_fitem2(bibset, "u=4 s=pw t=l,r", "ti", &addinfo);
494 YAZ_CHECK(r == 0 && addinfo == 0);
496 r = ccl_qual_fitem2(bibset, "1=1016 s=al,pw t=z", "term", &addinfo);
497 YAZ_CHECK(r == 0 && addinfo == 0);
499 r = ccl_qual_fitem2(bibset, "x=", "term", &addinfo);
500 YAZ_CHECK(r != 0 && addinfo != 0);
502 r = ccl_qual_fitem2(bibset, "12=3", "term", &addinfo);
503 YAZ_CHECK(r == 0 && addinfo == 0);
505 r = ccl_qual_fitem2(bibset, "ab=3", "term", &addinfo);
506 YAZ_CHECK(r != 0 && addinfo != 0);
508 r = ccl_qual_fitem2(bibset, "x=ab", "term", &addinfo);
509 YAZ_CHECK(r != 0 && addinfo != 0);
511 r = ccl_qual_fitem2(bibset, "s=ab", "term", &addinfo);
512 YAZ_CHECK(r == 0 && addinfo == 0);
514 ccl_qual_rm(&bibset);
517 int main(int argc, char **argv)
519 YAZ_CHECK_INIT(argc, argv);
532 * c-file-style: "Stroustrup"
533 * indent-tabs-mode: nil
535 * vim: shiftwidth=4 tabstop=8 expandtab