test_ccl: add special qualifier tests
[yaz-moved-to-github.git] / test / test_ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 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, "x1 and", 0));
205     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
206
207     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
208                   "@prox 0 1 0 2 k 2 "
209                   "@attr 4=2 @attr 1=1016 a "
210                   "@attr 4=2 @attr 1=1016 b "));
211     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
212                   "@prox 0 1 0 2 k 2 "
213                   "@attr 4=2 @attr 1=1016 a "
214                   "@attr 4=2 @attr 1=1016 b "));
215
216     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
217                   "@prox 0 2 0 2 k 2 "
218                   "@attr 4=2 @attr 1=1016 a "
219                   "@attr 4=2 @attr 1=1016 b "));
220
221     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
222                   "@prox 0 19 0 2 k 2 "
223                   "@attr 4=2 @attr 1=1016 a "
224                   "@attr 4=2 @attr 1=1016 b "));
225
226     YAZ_CHECK(tst_ccl_query(bibset, "spid%├Žserne", 
227                   "@prox 0 1 0 2 k 2 "
228                   "@attr 4=2 @attr 1=1016 spid "
229                   "@attr 4=2 @attr 1=1016 ├Žserne "));
230
231     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
232                   "@prox 0 1 1 2 k 2 "
233                   "@attr 4=2 @attr 1=1016 a "
234                   "@attr 4=2 @attr 1=1016 b "));
235     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
236                   "@prox 0 2 1 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, "date=1980",
241                             "@attr 2=3 1980 "));
242     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990",
243                             "@and @attr 2=4 234 @attr 2=2 1990 "));
244     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990",
245                             "@and @attr 2=4 234 @attr 2=2 1990 "));
246     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990",
247                             "@and @attr 2=4 234 @attr 2=2 1990 "));
248     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990",
249                             "@and @attr 2=4 234 @attr 2=2 1990 "));
250     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980",
251                             "@attr 2=2 1980 "));
252     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980",
253                             "@attr 2=2 1980 "));
254     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980",
255                             "@attr 2=3 -1980 "));
256     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980",
257                             "@attr 2=2 1980 "));
258     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980",
259                             "@attr 2=3 -1980 "));
260     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990",
261                             "@attr 2=3 234-1990 "));
262     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990",
263                             "@and @attr 2=4 234 @attr 2=2 1990 "));
264     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b",
265                             "@attr 4=1 @attr 1=4 a,b "));
266     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b",
267                             "@attr 4=1 @attr 1=4 \"a, b\" "));
268     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b",
269                             "@attr 4=2 @attr 1=4 a-b "));
270     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b",
271                             "@attr 4=1 @attr 1=4 \"a - b\" "));
272
273     YAZ_CHECK(tst_ccl_query(bibset, "a?",
274                             "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
275     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
276                             "@and @attr 4=2 @attr 1=1016 a "
277                             "@attr 4=2 @attr 1=1016 b "));
278
279     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
280                             "@and @attr 4=2 @attr 1=1016 a "
281                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
282
283     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
284                             "@attr 1=/my/title a "));
285
286     YAZ_CHECK(tst_ccl_query(bibset, "reg=a?b#\"c?\"", 
287                             "@attr 5=102 a.*b.c\\\\? "));
288     YAZ_CHECK(tst_ccl_query(bibset, "z=a?b#\"c?\"", 
289                             "@attr 5=104 a?b#c\\\\? "));
290
291     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\(", 
292                             "@attr 5=102 \\\\( "));
293     YAZ_CHECK(tst_ccl_query(bibset, "z=\\(", 
294                             "( "));
295
296     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\"", 
297                             "\"\\\"\" "));
298     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\"", 
299                             "\"\\\"\" "));
300
301     YAZ_CHECK(tst_ccl_query(bibset, "reg=.",
302                             "@attr 5=102 \\\\. "));
303     YAZ_CHECK(tst_ccl_query(bibset, "z=.",
304                             ". "));
305
306     YAZ_CHECK(tst_ccl_query(bibset, "reg=\".\"", 
307                             "@attr 5=102 \\\\. "));
308     YAZ_CHECK(tst_ccl_query(bibset, "z=\".\"", 
309                             ". "));
310
311     YAZ_CHECK(tst_ccl_query(bibset, "reg=?\\?",
312                             "@attr 5=102 .*\\\\? "));
313     YAZ_CHECK(tst_ccl_query(bibset, "z=?\\?",
314                             "@attr 5=104 ?\\\\? "));
315
316     YAZ_CHECK(tst_ccl_query(bibset, "reg=\"?\\?\"",
317                             "@attr 5=102 \\\\?\\\\? "));
318     YAZ_CHECK(tst_ccl_query(bibset, "z=\"?\\?\"",
319                             "@attr 5=104 \\\\?\\\\? "));
320
321     YAZ_CHECK(tst_ccl_query(bibset, "reg=\\\\",
322                             "@attr 5=102 \\\\\\\\ "));
323     YAZ_CHECK(tst_ccl_query(bibset, "z=\\\\",
324                             "@attr 5=104 \\\\\\\\ "));
325
326     YAZ_CHECK(tst_ccl_query(bibset, "\\\\", 
327                             "@attr 4=2 @attr 1=1016 \\\\ "));
328
329     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
330                             "@or @attr 4=2 @attr 1=1016 a "
331                             "@attr 1=/my/title a "));
332
333     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
334                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
335                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
336
337     YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"", 
338                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
339                             "@attr 4=2 @attr 1=1016 b? "));
340
341     YAZ_CHECK(tst_ccl_query(bibset, "@and",
342                             "@attr 4=2 @attr 1=1016 \\@and "));
343                             
344     YAZ_CHECK(tst_ccl_query(bibset, "a@and",
345                             "@attr 4=2 @attr 1=1016 a@and "));
346                            
347     YAZ_CHECK(tst_ccl_query(bibset, "}",
348                             "@attr 4=2 @attr 1=1016 } "));
349     
350     YAZ_CHECK(tst_ccl_query(bibset, "{",
351                             "@attr 4=2 @attr 1=1016 \"{\" "));
352
353     YAZ_CHECK(tst_ccl_query(bibset, "\"a b c\"",
354                             "@attr 4=1 @attr 1=1016 \"a b c\" "));
355
356     YAZ_CHECK(tst_ccl_query(bibset, "\"a b  c  \"",
357                             "@attr 4=1 @attr 1=1016 \"a b  c  \" "));
358
359     ccl_qual_rm(&bibset);
360 }
361
362 void tst2(void)
363 {
364     CCL_bibset bibset = ccl_qual_mk();
365
366     YAZ_CHECK(bibset);
367     if (!bibset)
368         return;
369
370     ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
371     ccl_qual_fitem(bibset, "1=1016 s=al,pw t=z",    "term");
372
373     YAZ_CHECK(tst_ccl_query(bibset, "a*",
374                             "@attr 4=2 @attr 1=1016 a* "));
375
376     YAZ_CHECK(tst_ccl_query(bibset, "a?",
377                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
378
379     ccl_qual_fitem(bibset, "*", "@truncation");
380     YAZ_CHECK(tst_ccl_query(bibset, "a*",
381                             "@attr 5=104 @attr 4=2 @attr 1=1016 a? "));
382
383     YAZ_CHECK(tst_ccl_query(bibset, "a?",
384                             "@attr 5=104 @attr 4=2 @attr 1=1016 a\\\\? "));
385
386     ccl_qual_fitem(bibset, "og", "@and");
387     ccl_qual_fitem(bibset, "eller", "@or");
388     ccl_qual_fitem(bibset, "ikke", "@not");
389
390     YAZ_CHECK(tst_ccl_query(bibset, "a og b eller c ikke d",
391                             "@not @or @and @attr 4=2 @attr 1=1016 a "
392                             "@attr 4=2 @attr 1=1016 b "
393                             "@attr 4=2 @attr 1=1016 c "
394                             "@attr 4=2 @attr 1=1016 d "));
395     ccl_qual_rm(&bibset);
396 }
397
398
399 int main(int argc, char **argv)
400 {
401     YAZ_CHECK_INIT(argc, argv);
402     YAZ_CHECK_LOG();
403     tst1(0);
404     tst1(1);
405     tst1(2);
406     tst1(3);
407     tst2();
408     YAZ_CHECK_TERM;
409 }
410 /*
411  * Local variables:
412  * c-basic-offset: 4
413  * c-file-style: "Stroustrup"
414  * indent-tabs-mode: nil
415  * End:
416  * vim: shiftwidth=4 tabstop=8 expandtab
417  */
418