42ec35eaa638df8af7d67a509b84f42689b78945
[yaz-moved-to-github.git] / test / test_ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <string.h>
10 #include <yaz/ccl_xml.h>
11 #include <yaz/log.h>
12 #include <yaz/test.h>
13
14
15 static int tst_ccl_query(CCL_bibset bibset,
16                          const char *query,
17                          const char *result)
18 {
19     CCL_parser parser = ccl_parser_create(bibset);
20     int ret = 0;
21
22     if (parser && bibset)
23     {
24         struct ccl_rpn_node *rpn;
25         
26         rpn = ccl_parser_find_str(parser, query);
27         if (rpn)
28         {
29             /* parse ok. check that result is there and match */
30             WRBUF wrbuf = wrbuf_alloc();
31             ccl_pquery(wrbuf, rpn);
32             
33             /* check expect a result and that it matches */
34             if (result && !strcmp(wrbuf_cstr(wrbuf), result))
35                 ret = 1;
36             else
37             {
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));
41                 ret = 0;
42             }
43             ccl_rpn_delete(rpn);
44             wrbuf_destroy(wrbuf);
45         }
46         else 
47         {
48             if (result)
49             {
50                 yaz_log(YLOG_WARN, "%s: parse failed", query);
51                 ret = 0;
52             }
53             else
54                 ret = 1;
55         }
56     }
57     ccl_parser_destroy (parser);
58     return ret;
59 }
60
61 void tst1(int pass)
62 {
63     CCL_bibset bibset = ccl_qual_mk();
64     char tstline[128];
65
66     YAZ_CHECK(bibset);
67     if (!bibset)
68         return;
69
70     if (pass == 1)
71     {
72         CCL_bibset nbibset = ccl_qual_dup(bibset);
73         ccl_qual_rm(&bibset);
74         bibset = nbibset;
75     }
76
77     switch(pass)
78     {
79     case 0:
80         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
81         ccl_qual_fitem(bibset, "1=1016 s=al,pw t=r",    "term");
82         ccl_qual_fitem(bibset, "t=x", "reg");
83         ccl_qual_fitem(bibset, "t=z", "z");
84         ccl_qual_fitem(bibset, "1=/my/title",       "dc.title");
85         ccl_qual_fitem(bibset, "r=r",         "date");
86         ccl_qual_fitem(bibset, "r=o",         "x");
87         ccl_qual_fitem(bibset, "dc.title", "title");
88         ccl_qual_fitem(bibset, "term dc.title", "comb");
89         break;
90     case 1:
91         strcpy(tstline, "ti u=4    s=pw t=l,r");
92         ccl_qual_line(bibset, tstline);
93
94         strcpy(tstline, "term 1=1016 s=al,pw t=r  # default term");
95         ccl_qual_line(bibset, tstline);
96
97         strcpy(tstline, "reg t=x");
98         ccl_qual_line(bibset, tstline);
99
100         strcpy(tstline, "z t=z");
101         ccl_qual_line(bibset, tstline);
102
103         strcpy(tstline, "dc.title 1=/my/title");
104         ccl_qual_line(bibset, tstline);
105
106         strcpy(tstline, "date r=r # ordered relation");
107         ccl_qual_line(bibset, tstline);
108
109         strcpy(tstline, "x r=o # ordered relation");
110         ccl_qual_line(bibset, tstline);
111
112         strcpy(tstline, "title dc.title # alias");
113         ccl_qual_line(bibset, tstline);
114
115         strcpy(tstline, "comb term dc.title # combination");
116         ccl_qual_line(bibset, tstline);
117         break;
118     case 2:
119         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
120                      "term 1=1016 s=al,pw t=r\r\n"
121                      "\n"
122                      "reg t=x\r\n"
123                      "z t=z\r\n"
124                      "dc.title 1=/my/title\n"
125                      "date r=r\n" 
126                      "x r=o\n"
127                      "title dc.title\n"
128                      "comb term dc.title\n"
129             );
130         break;
131     case 3:
132 #if YAZ_HAVE_XML2
133         if (1)
134         {
135             xmlDocPtr doc;
136             int r;
137             const char *addinfo = 0;
138             const char *xml_str = 
139                 "<cclmap>\n"
140                 " <qual name=\"ti\">\n"
141                 "   <attr type=\"u\" value=\"4\"/>\n"
142                 "   <attr type=\"s\" value=\"pw\"/>\n"
143                 "   <attr type=\"t\" value=\"l,r\"/>\n"
144                 " </qual>\n"
145                 " <qual name=\"term\">\n"
146                 "   <attr type=\"1\" value=\"1016\"/>\n"
147                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
148                 "   <attr type=\"t\" value=\"r\"/>\n"
149                 " </qual>\n"
150                 " <qual name=\"reg\">\n"
151                 "   <attr type=\"t\" value=\"x\"/>\n"
152                 " </qual>\n"
153                 " <qual name=\"z\">\n"
154                 "   <attr type=\"t\" value=\"z\"/>\n"
155                 " </qual>\n"
156                 " <qual name=\"dc.title\">\n"
157                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
158                 " </qual>\n"
159                 " <qual name=\"date\">\n"
160                 "   <attr type=\"r\" value=\"r\"/>\n"
161                 " </qual>\n"
162                 " <qual name=\"x\">\n"
163                 "   <attr type=\"r\" value=\"o\"/>\n"
164                 " </qual>\n"
165                 " <qual name=\"title\">\n"
166                 "   <qual name=\"dc.title\"/>\n"
167                 " </qual>\n"
168                 " <qual name=\"comb\">\n"
169                 "   <qual name=\"term\"/>\n"
170                 "   <qual name=\"dc.title\"/>\n"
171                 " </qual>\n"
172                 "</cclmap>\n";
173             
174             doc = xmlParseMemory(xml_str, strlen(xml_str));
175             YAZ_CHECK(doc);
176
177             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
178             YAZ_CHECK_EQ(r, 0);
179
180             xmlFreeDoc(doc);
181         }
182         break;
183 #else
184         return;
185 #endif
186     default:
187         YAZ_CHECK(0);
188         return;
189     }
190     
191     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
192
193     YAZ_CHECK(tst_ccl_query(bibset, "k\xc3\xb8" "benhavn", "@attr 4=2 @attr 1=1016 k\xc3\xb8" "benhavn "));
194
195     YAZ_CHECK(tst_ccl_query(bibset, "k\xf8" "benhavn", "@attr 4=2 @attr 1=1016 ""k\xf8" "benhavn "));
196
197     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
198     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
199                   "@and "
200                   "@attr 4=2 @attr 1=1016 x1 "
201                   "@attr 4=2 @attr 1=1016 x2 "));
202     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
203     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
204     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=(x4)", "@attr 1=/my/title x4 "));
205     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
206     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
207
208     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
209                   "@prox 0 1 0 2 k 2 "
210                   "@attr 4=2 @attr 1=1016 a "
211                   "@attr 4=2 @attr 1=1016 b "));
212     YAZ_CHECK(tst_ccl_query(bibset, "a%(b)", 
213                   "@prox 0 1 0 2 k 2 "
214                   "@attr 4=2 @attr 1=1016 a "
215                   "@attr 4=2 @attr 1=1016 b "));
216     YAZ_CHECK(tst_ccl_query(bibset, "(a)%(b)", 
217                   "@prox 0 1 0 2 k 2 "
218                   "@attr 4=2 @attr 1=1016 a "
219                   "@attr 4=2 @attr 1=1016 b "));
220     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
221                   "@prox 0 1 0 2 k 2 "
222                   "@attr 4=2 @attr 1=1016 a "
223                   "@attr 4=2 @attr 1=1016 b "));
224
225     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
226                   "@prox 0 2 0 2 k 2 "
227                   "@attr 4=2 @attr 1=1016 a "
228                   "@attr 4=2 @attr 1=1016 b "));
229
230     YAZ_CHECK(tst_ccl_query(bibset, "(a)%2(b)", 
231                   "@prox 0 2 0 2 k 2 "
232                   "@attr 4=2 @attr 1=1016 a "
233                   "@attr 4=2 @attr 1=1016 b "));
234
235     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
236                   "@prox 0 19 0 2 k 2 "
237                   "@attr 4=2 @attr 1=1016 a "
238                   "@attr 4=2 @attr 1=1016 b "));
239
240     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
241                   "@prox 0 1 0 2 k 2 "
242                   "@attr 4=2 @attr 1=1016 spid "
243                   "@attr 4=2 @attr 1=1016 æserne "));
244
245     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
246                   "@prox 0 1 1 2 k 2 "
247                   "@attr 4=2 @attr 1=1016 a "
248                   "@attr 4=2 @attr 1=1016 b "));
249     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
250                   "@prox 0 2 1 2 k 2 "
251                   "@attr 4=2 @attr 1=1016 a "
252                   "@attr 4=2 @attr 1=1016 b "));
253
254     YAZ_CHECK(tst_ccl_query(bibset, "a% (b or dc.title=c)", 
255                   "@prox 0 1 0 2 k 2 "
256                   "@attr 4=2 @attr 1=1016 a "
257                   "@or @attr 4=2 @attr 1=1016 b "
258                             "@attr 4=2 @attr 1=1016 @attr 1=/my/title c "));
259
260     YAZ_CHECK(tst_ccl_query(bibset, "(a b) % (c)", 
261                             "@prox 0 1 0 2 k 2 @and "
262                             "@attr 4=2 @attr 1=1016 a @attr 4=2 @attr 1=1016 b "
263                             "@attr 4=2 @attr 1=1016 c " ));
264
265     YAZ_CHECK(tst_ccl_query(bibset, "date=1980",
266                             "@attr 2=3 1980 "));
267     YAZ_CHECK(tst_ccl_query(bibset, "(date=1980)",
268                             "@attr 2=3 1980 "));
269     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990",
270                             "@and @attr 2=4 234 @attr 2=2 1990 "));
271     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990",
272                             "@and @attr 2=4 234 @attr 2=2 1990 "));
273     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990",
274                             "@and @attr 2=4 234 @attr 2=2 1990 "));
275     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990",
276                             "@and @attr 2=4 234 @attr 2=2 1990 "));
277     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980",
278                             "@attr 2=2 1980 "));
279     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980",
280                             "@attr 2=2 1980 "));
281     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980",
282                             "@attr 2=3 -1980 "));
283     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
284                             "@attr 2=2 1980 "));
285     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
286                             "@attr 2=3 -1980 "));
287     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
288                             "@attr 2=3 234-1990 "));
289     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
290                             "@and @attr 2=4 234 @attr 2=2 1990 "));
291     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
292                             "@attr 4=1 @attr 1=4 a,b "));
293     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b",
294                             "@attr 4=1 @attr 1=4 \"a, b\" "));
295     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b",
296                             "@attr 4=2 @attr 1=4 a-b "));
297     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
298                             "@attr 4=1 @attr 1=4 \"a - b\" "));
299
300     YAZ_CHECK(tst_ccl_query(bibset, "a?",
301                             "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
302     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
303                             "@and @attr 4=2 @attr 1=1016 a "
304                             "@attr 4=2 @attr 1=1016 b "));
305
306     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
307                             "@and @attr 4=2 @attr 1=1016 a "
308                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
309
310     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
311                             "@attr 1=/my/title a "));
312
313     YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"", 
314                             "@attr 5=102 a.*b.c\\\\? "));
315     YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"", 
316                             "@attr 5=104 a?b#c\\\\? "));
317
318     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(", 
319                             "@attr 5=102 \\\\( "));
320     YAZ_CHECK(tst_ccl_query(bibset, "z=\\(", 
321                             "( "));
322
323     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"", 
324                             "\"\\\"\" "));
325     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"", 
326                             "\"\\\"\" "));
327
328     YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
329                             "@attr 5=102 \\\\. "));
330     YAZ_CHECK(tst_ccl_query(bibset, "z=.",
331                             ". "));
332
333     YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"", 
334                             "@attr 5=102 \\\\. "));
335     YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"", 
336                             ". "));
337
338     YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
339                             "@attr 5=102 .*\\\\? "));
340     YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
341                             "@attr 5=104 ?\\\\? "));
342
343     YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
344                             "@attr 5=102 \\\\?\\\\? "));
345     YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
346                             "@attr 5=104 \\\\?\\\\? "));
347
348     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
349                             "@attr 5=102 \\\\\\\\ "));
350     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
351                             "@attr 5=104 \\\\\\\\ "));
352
353     YAZ_CHECK(tst_ccl_query(bibset, "\\\\", 
354                             "@attr 4=2 @attr 1=1016 \\\\ "));
355
356     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
357                             "@or @attr 4=2 @attr 1=1016 a "
358                             "@attr 1=/my/title a "));
359
360     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
361                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
362                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
363
364     YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"", 
365                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
366                             "@attr 4=2 @attr 1=1016 b? "));
367
368     YAZ_CHECK(tst_ccl_query(bibset, "@and",
369                             "@attr 4=2 @attr 1=1016 \\@and "));
370                             
371     YAZ_CHECK(tst_ccl_query(bibset, "a@and",
372                             "@attr 4=2 @attr 1=1016 a@and "));
373                            
374     YAZ_CHECK(tst_ccl_query(bibset, "}",
375                             "@attr 4=2 @attr 1=1016 } "));
376     
377     YAZ_CHECK(tst_ccl_query(bibset, "{",
378                             "@attr 4=2 @attr 1=1016 \"{\" "));
379
380     YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
381                             "@attr 4=1 @attr 1=1016 \"a b c\" "));
382
383     YAZ_CHECK(tst_ccl_query(bibset, "\"a b  c  \"",
384                             "@attr 4=1 @attr 1=1016 \"a b  c  \" "));
385
386     ccl_qual_rm(&bibset);
387 }
388
389 void tst2(void)
390 {
391     CCL_bibset bibset = ccl_qual_mk();
392
393     YAZ_CHECK(bibset);
394     if (!bibset)
395         return;
396
397     ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
398     ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z",    "term");
399
400     YAZ_CHECK(tst_ccl_query(bibset, "a*",
401                             "@attr 4=2 @attr 1=1016 a* "));
402
403     YAZ_CHECK(tst_ccl_query(bibset, "a?",
404                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
405
406     ccl_qual_fitem(bibset, "*", "@truncation");
407     YAZ_CHECK(tst_ccl_query(bibset, "a*",
408                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
409
410     YAZ_CHECK(tst_ccl_query(bibset, "a?",
411                             "@attr 5=104 @attr 4=2 @attr 1=1016 a\\\\? "));
412
413     ccl_qual_fitem(bibset, "og", "@and");
414     ccl_qual_fitem(bibset, "eller", "@or");
415     ccl_qual_fitem(bibset, "ikke", "@not");
416
417     YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
418                             "@not @or @and @attr 4=2 @attr 1=1016 a "
419                             "@attr 4=2 @attr 1=1016 b "
420                             "@attr 4=2 @attr 1=1016 c "
421                             "@attr 4=2 @attr 1=1016 d "));
422     ccl_qual_rm(&bibset);
423 }
424
425
426 int main(int argc, char **argv)
427 {
428     YAZ_CHECK_INIT(argc, argv);
429     YAZ_CHECK_LOG();
430     tst1(0);
431     tst1(1);
432     tst1(2);
433     tst1(3);
434     tst2();
435     YAZ_CHECK_TERM;
436 }
437 /*
438  * Local variables:
439  * c-basic-offset: 4
440  * c-file-style: "Stroustrup"
441  * indent-tabs-mode: nil
442  * End:
443  * vim: shiftwidth=4 tabstop=8 expandtab
444  */
445