ccl2rpn: more characters to escape (regexp-1).
[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, "1=/my/title t=x",       "dc.title");
83         ccl_qual_fitem(bibset, "r=r",         "date");
84         ccl_qual_fitem(bibset, "r=o",         "x");
85         ccl_qual_fitem(bibset, "dc.title", "title");
86         ccl_qual_fitem(bibset, "term dc.title", "comb");
87         break;
88     case 1:
89         strcpy(tstline, "ti u=4    s=pw t=l,r");
90         ccl_qual_line(bibset, tstline);
91
92         strcpy(tstline, "term 1=1016 s=al,pw t=r  # default term");
93         ccl_qual_line(bibset, tstline);
94
95         strcpy(tstline, "dc.title 1=/my/title t=x");
96         ccl_qual_line(bibset, tstline);
97
98         strcpy(tstline, "date r=r # ordered relation");
99         ccl_qual_line(bibset, tstline);
100
101         strcpy(tstline, "x r=o # ordered relation");
102         ccl_qual_line(bibset, tstline);
103
104         strcpy(tstline, "title dc.title # alias");
105         ccl_qual_line(bibset, tstline);
106
107         strcpy(tstline, "comb term dc.title # combination");
108         ccl_qual_line(bibset, tstline);
109         break;
110     case 2:
111         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
112                      "term 1=1016 s=al,pw t=r\r\n"
113                      "\n"
114                      "dc.title 1=/my/title t=x\n"
115                      "date r=r\n" 
116                      "x r=o\n"
117                      "title dc.title\n"
118                      "comb term dc.title\n"
119             );
120         break;
121     case 3:
122 #if YAZ_HAVE_XML2
123         if (1)
124         {
125             xmlDocPtr doc;
126             int r;
127             const char *addinfo = 0;
128             const char *xml_str = 
129                 "<cclmap>\n"
130                 " <qual name=\"ti\">\n"
131                 "   <attr type=\"u\" value=\"4\"/>\n"
132                 "   <attr type=\"s\" value=\"pw\"/>\n"
133                 "   <attr type=\"t\" value=\"l,r\"/>\n"
134                 " </qual>\n"
135                 " <qual name=\"term\">\n"
136                 "   <attr type=\"1\" value=\"1016\"/>\n"
137                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
138                 "   <attr type=\"t\" value=\"r\"/>\n"
139                 " </qual>\n"
140                 " <qual name=\"dc.title\">\n"
141                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
142                 "   <attr type=\"t\" value=\"x\"/>\n"
143                 " </qual>\n"
144                 " <qual name=\"date\">\n"
145                 "   <attr type=\"r\" value=\"r\"/>\n"
146                 " </qual>\n"
147                 " <qual name=\"x\">\n"
148                 "   <attr type=\"r\" value=\"o\"/>\n"
149                 " </qual>\n"
150                 " <qual name=\"title\">\n"
151                 "   <qual name=\"dc.title\"/>\n"
152                 " </qual>\n"
153                 " <qual name=\"comb\">\n"
154                 "   <qual name=\"term\"/>\n"
155                 "   <qual name=\"dc.title\"/>\n"
156                 " </qual>\n"
157                 "</cclmap>\n";
158             
159             doc = xmlParseMemory(xml_str, strlen(xml_str));
160             YAZ_CHECK(doc);
161
162             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
163             YAZ_CHECK_EQ(r, 0);
164
165             xmlFreeDoc(doc);
166         }
167         break;
168 #else
169         return;
170 #endif
171     default:
172         YAZ_CHECK(0);
173         return;
174     }
175     
176     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
177
178     YAZ_CHECK(tst_ccl_query(bibset, "k\xc3\xb8" "benhavn", "@attr 4=2 @attr 1=1016 k\xc3\xb8" "benhavn "));
179
180     YAZ_CHECK(tst_ccl_query(bibset, "k\xf8" "benhavn", "@attr 4=2 @attr 1=1016 ""k\xf8" "benhavn "));
181
182     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
183     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
184                   "@and "
185                   "@attr 4=2 @attr 1=1016 x1 "
186                   "@attr 4=2 @attr 1=1016 x2 "));
187     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
188     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
189     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
190     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
191
192     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
193                   "@prox 0 1 0 2 k 2 "
194                   "@attr 4=2 @attr 1=1016 a "
195                   "@attr 4=2 @attr 1=1016 b "));
196     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
197                   "@prox 0 1 0 2 k 2 "
198                   "@attr 4=2 @attr 1=1016 a "
199                   "@attr 4=2 @attr 1=1016 b "));
200
201     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
202                   "@prox 0 2 0 2 k 2 "
203                   "@attr 4=2 @attr 1=1016 a "
204                   "@attr 4=2 @attr 1=1016 b "));
205
206     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
207                   "@prox 0 19 0 2 k 2 "
208                   "@attr 4=2 @attr 1=1016 a "
209                   "@attr 4=2 @attr 1=1016 b "));
210
211     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
212                   "@prox 0 1 0 2 k 2 "
213                   "@attr 4=2 @attr 1=1016 spid "
214                   "@attr 4=2 @attr 1=1016 æserne "));
215
216     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
217                   "@prox 0 1 1 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!2b", 
221                   "@prox 0 2 1 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, "date=1980", "@attr 2=3 1980 "));
226     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
227     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
228     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
229     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
230     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980", "@attr 2=2 1980 "));
231     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980", "@attr 2=2 1980 "));
232     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980", "@attr 2=3 -1980 "));
233     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980", "@attr 2=2 1980 "));
234     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
235     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
236     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
237     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
238     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b", "@attr 4=1 @attr 1=4 \"a, b\" "));
239     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b", "@attr 4=2 @attr 1=4 a-b "));
240     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 \"a - b\" "));
241
242     YAZ_CHECK(tst_ccl_query(bibset, "a?", "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
243     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
244                             "@and @attr 4=2 @attr 1=1016 a "
245                             "@attr 4=2 @attr 1=1016 b "));
246
247     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
248                             "@and @attr 4=2 @attr 1=1016 a "
249                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
250
251     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
252                             "@attr 1=/my/title a "));
253
254     YAZ_CHECK(tst_ccl_query(bibset, "title=a?b#\"c?\"", 
255                             "@attr 5=102 @attr 1=/my/title a.*b.c\\\\? "));
256
257     YAZ_CHECK(tst_ccl_query(bibset, "title=\\(", 
258                             "@attr 5=102 @attr 1=/my/title \\\\( "));
259
260     YAZ_CHECK(tst_ccl_query(bibset, "title=.", 
261                             "@attr 5=102 @attr 1=/my/title \\\\. "));
262
263     YAZ_CHECK(tst_ccl_query(bibset, "title=.", 
264                             "@attr 5=102 @attr 1=/my/title \\\\. "));
265
266     YAZ_CHECK(tst_ccl_query(bibset, "title=\".\"", 
267                             "@attr 5=102 @attr 1=/my/title \\\\. "));
268
269     YAZ_CHECK(tst_ccl_query(bibset, "title=?\\?", 
270                             "@attr 5=102 @attr 1=/my/title .*\\\\? "));
271
272     YAZ_CHECK(tst_ccl_query(bibset, "title=\"?\\?\"", 
273                             "@attr 5=102 @attr 1=/my/title \\\\?\\\\? "));
274
275     YAZ_CHECK(tst_ccl_query(bibset, "title=\\\\", 
276                             "@attr 5=102 @attr 1=/my/title \\\\\\\\ "));
277
278     YAZ_CHECK(tst_ccl_query(bibset, "\\\\", 
279                             "@attr 4=2 @attr 1=1016 \\\\ "));
280
281     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
282                             "@or @attr 4=2 @attr 1=1016 a "
283                             "@attr 1=/my/title a "));
284
285     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
286                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
287                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
288
289     YAZ_CHECK(tst_ccl_query(bibset, "\"a\"? \"b?\"", 
290                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
291                             "@attr 4=2 @attr 1=1016 b? "));
292
293     ccl_qual_rm(&bibset);
294 }
295
296 int main(int argc, char **argv)
297 {
298     YAZ_CHECK_INIT(argc, argv);
299     YAZ_CHECK_LOG();
300     tst1(0);
301     tst1(1);
302     tst1(2);
303     tst1(3);
304     YAZ_CHECK_TERM;
305 }
306 /*
307  * Local variables:
308  * c-basic-offset: 4
309  * c-file-style: "Stroustrup"
310  * indent-tabs-mode: nil
311  * End:
312  * vim: shiftwidth=4 tabstop=8 expandtab
313  */
314