40e08c26edd9f7d9a498ec54063c660a2646d55c
[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=234-1990",
268                             "@and @attr 2=4 234 @attr 2=2 1990 "));
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=-1980",
276                             "@attr 2=2 1980 "));
277     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980",
278                             "@attr 2=2 1980 "));
279     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980",
280                             "@attr 2=3 -1980 "));
281     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
282                             "@attr 2=2 1980 "));
283     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
284                             "@attr 2=3 -1980 "));
285     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
286                             "@attr 2=3 234-1990 "));
287     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
288                             "@and @attr 2=4 234 @attr 2=2 1990 "));
289     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
290                             "@attr 4=1 @attr 1=4 a,b "));
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=2 @attr 1=4 a-b "));
295     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
296                             "@attr 4=1 @attr 1=4 \"a - b\" "));
297
298     YAZ_CHECK(tst_ccl_query(bibset, "a?",
299                             "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
300     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
301                             "@and @attr 4=2 @attr 1=1016 a "
302                             "@attr 4=2 @attr 1=1016 b "));
303
304     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
305                             "@and @attr 4=2 @attr 1=1016 a "
306                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
307
308     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
309                             "@attr 1=/my/title a "));
310
311     YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"", 
312                             "@attr 5=102 a.*b.c\\\\? "));
313     YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"", 
314                             "@attr 5=104 a?b#c\\\\? "));
315
316     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(", 
317                             "@attr 5=102 \\\\( "));
318     YAZ_CHECK(tst_ccl_query(bibset, "z=\\(", 
319                             "( "));
320
321     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"", 
322                             "\"\\\"\" "));
323     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"", 
324                             "\"\\\"\" "));
325
326     YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
327                             "@attr 5=102 \\\\. "));
328     YAZ_CHECK(tst_ccl_query(bibset, "z=.",
329                             ". "));
330
331     YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"", 
332                             "@attr 5=102 \\\\. "));
333     YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"", 
334                             ". "));
335
336     YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
337                             "@attr 5=102 .*\\\\? "));
338     YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
339                             "@attr 5=104 ?\\\\? "));
340
341     YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
342                             "@attr 5=102 \\\\?\\\\? "));
343     YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
344                             "@attr 5=104 \\\\?\\\\? "));
345
346     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
347                             "@attr 5=102 \\\\\\\\ "));
348     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
349                             "@attr 5=104 \\\\\\\\ "));
350
351     YAZ_CHECK(tst_ccl_query(bibset, "\\\\", 
352                             "@attr 4=2 @attr 1=1016 \\\\ "));
353
354     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
355                             "@or @attr 4=2 @attr 1=1016 a "
356                             "@attr 1=/my/title a "));
357
358     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
359                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
360                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
361
362     YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"", 
363                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
364                             "@attr 4=2 @attr 1=1016 b? "));
365
366     YAZ_CHECK(tst_ccl_query(bibset, "@and",
367                             "@attr 4=2 @attr 1=1016 \\@and "));
368                             
369     YAZ_CHECK(tst_ccl_query(bibset, "a@and",
370                             "@attr 4=2 @attr 1=1016 a@and "));
371                            
372     YAZ_CHECK(tst_ccl_query(bibset, "}",
373                             "@attr 4=2 @attr 1=1016 } "));
374     
375     YAZ_CHECK(tst_ccl_query(bibset, "{",
376                             "@attr 4=2 @attr 1=1016 \"{\" "));
377
378     YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
379                             "@attr 4=1 @attr 1=1016 \"a b c\" "));
380
381     YAZ_CHECK(tst_ccl_query(bibset, "\"a b  c  \"",
382                             "@attr 4=1 @attr 1=1016 \"a b  c  \" "));
383
384     ccl_qual_rm(&bibset);
385 }
386
387 void tst2(void)
388 {
389     CCL_bibset bibset = ccl_qual_mk();
390
391     YAZ_CHECK(bibset);
392     if (!bibset)
393         return;
394
395     ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
396     ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z",    "term");
397
398     YAZ_CHECK(tst_ccl_query(bibset, "a*",
399                             "@attr 4=2 @attr 1=1016 a* "));
400
401     YAZ_CHECK(tst_ccl_query(bibset, "a?",
402                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
403
404     ccl_qual_fitem(bibset, "*", "@truncation");
405     YAZ_CHECK(tst_ccl_query(bibset, "a*",
406                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
407
408     YAZ_CHECK(tst_ccl_query(bibset, "a?",
409                             "@attr 5=104 @attr 4=2 @attr 1=1016 a\\\\? "));
410
411     ccl_qual_fitem(bibset, "og", "@and");
412     ccl_qual_fitem(bibset, "eller", "@or");
413     ccl_qual_fitem(bibset, "ikke", "@not");
414
415     YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
416                             "@not @or @and @attr 4=2 @attr 1=1016 a "
417                             "@attr 4=2 @attr 1=1016 b "
418                             "@attr 4=2 @attr 1=1016 c "
419                             "@attr 4=2 @attr 1=1016 d "));
420     ccl_qual_rm(&bibset);
421 }
422
423
424 int main(int argc, char **argv)
425 {
426     YAZ_CHECK_INIT(argc, argv);
427     YAZ_CHECK_LOG();
428     tst1(0);
429     tst1(1);
430     tst1(2);
431     tst1(3);
432     tst2();
433     YAZ_CHECK_TERM;
434 }
435 /*
436  * Local variables:
437  * c-basic-offset: 4
438  * c-file-style: "Stroustrup"
439  * indent-tabs-mode: nil
440  * End:
441  * vim: shiftwidth=4 tabstop=8 expandtab
442  */
443