f3b04b3b2901e89f9e80b02568ca1df37859af5d
[yaz-moved-to-github.git] / test / test_ccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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     switch(pass)
71     {
72     case 0:
73         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
74         ccl_qual_fitem(bibset, "1=1016 s=al,pw t=r",    "term");
75         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
76         ccl_qual_fitem(bibset, "r=r",         "date");
77         ccl_qual_fitem(bibset, "r=o",         "x");
78         ccl_qual_fitem(bibset, "dc.title", "title");
79         ccl_qual_fitem(bibset, "term dc.title", "comb");
80         break;
81     case 1:
82         strcpy(tstline, "ti u=4    s=pw t=l,r");
83         ccl_qual_line(bibset, tstline);
84
85         strcpy(tstline, "term 1=1016 s=al,pw t=r  # default term");
86         ccl_qual_line(bibset, tstline);
87
88         strcpy(tstline, "dc.title 1=/my/title");
89         ccl_qual_line(bibset, tstline);
90
91         strcpy(tstline, "date r=r # ordered relation");
92         ccl_qual_line(bibset, tstline);
93
94         strcpy(tstline, "x r=o # ordered relation");
95         ccl_qual_line(bibset, tstline);
96
97         strcpy(tstline, "title dc.title # alias");
98         ccl_qual_line(bibset, tstline);
99
100         strcpy(tstline, "comb term dc.title # combination");
101         ccl_qual_line(bibset, tstline);
102         break;
103     case 2:
104         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
105                      "term 1=1016 s=al,pw t=r\r\n"
106                      "\n"
107                      "dc.title 1=/my/title\n"
108                      "date r=r\n" 
109                      "x r=o\n"
110                      "title dc.title\n"
111                      "comb term dc.title\n"
112             );
113         break;
114     case 3:
115 #if YAZ_HAVE_XML2
116         if (1)
117         {
118             xmlDocPtr doc;
119             int r;
120             const char *addinfo = 0;
121             const char *xml_str = 
122                 "<cclmap>\n"
123                 " <qual name=\"ti\">\n"
124                 "   <attr type=\"u\" value=\"4\"/>\n"
125                 "   <attr type=\"s\" value=\"pw\"/>\n"
126                 "   <attr type=\"t\" value=\"l,r\"/>\n"
127                 " </qual>\n"
128                 " <qual name=\"term\">\n"
129                 "   <attr type=\"1\" value=\"1016\"/>\n"
130                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
131                 "   <attr type=\"t\" value=\"r\"/>\n"
132                 " </qual>\n"
133                 " <qual name=\"dc.title\">\n"
134                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
135                 " </qual>\n"
136                 " <qual name=\"date\">\n"
137                 "   <attr type=\"r\" value=\"r\"/>\n"
138                 " </qual>\n"
139                 " <qual name=\"x\">\n"
140                 "   <attr type=\"r\" value=\"o\"/>\n"
141                 " </qual>\n"
142                 " <qual name=\"title\">\n"
143                 "   <qual name=\"dc.title\"/>\n"
144                 " </qual>\n"
145                 " <qual name=\"comb\">\n"
146                 "   <qual name=\"term\"/>\n"
147                 "   <qual name=\"dc.title\"/>\n"
148                 " </qual>\n"
149                 "</cclmap>\n";
150             
151             doc = xmlParseMemory(xml_str, strlen(xml_str));
152             YAZ_CHECK(doc);
153
154             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
155             YAZ_CHECK_EQ(r, 0);
156
157             xmlFreeDoc(doc);
158         }
159         break;
160 #else
161         return;
162 #endif
163     default:
164         YAZ_CHECK(0);
165         return;
166     }
167     
168     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
169     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
170     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
171                   "@and "
172                   "@attr 4=2 @attr 1=1016 x1 "
173                   "@attr 4=2 @attr 1=1016 x2 "));
174     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
175     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
176     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
177     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
178
179     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
180                   "@prox 0 1 0 2 k 2 "
181                   "@attr 4=2 @attr 1=1016 a "
182                   "@attr 4=2 @attr 1=1016 b "));
183     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
184                   "@prox 0 1 0 2 k 2 "
185                   "@attr 4=2 @attr 1=1016 a "
186                   "@attr 4=2 @attr 1=1016 b "));
187
188     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
189                   "@prox 0 2 0 2 k 2 "
190                   "@attr 4=2 @attr 1=1016 a "
191                   "@attr 4=2 @attr 1=1016 b "));
192
193     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
194                   "@prox 0 19 0 2 k 2 "
195                   "@attr 4=2 @attr 1=1016 a "
196                   "@attr 4=2 @attr 1=1016 b "));
197
198     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
199                   "@prox 0 1 0 2 k 2 "
200                   "@attr 4=2 @attr 1=1016 spid "
201                   "@attr 4=2 @attr 1=1016 æserne "));
202
203     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
204                   "@prox 0 1 1 2 k 2 "
205                   "@attr 4=2 @attr 1=1016 a "
206                   "@attr 4=2 @attr 1=1016 b "));
207     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
208                   "@prox 0 2 1 2 k 2 "
209                   "@attr 4=2 @attr 1=1016 a "
210                   "@attr 4=2 @attr 1=1016 b "));
211
212     YAZ_CHECK(tst_ccl_query(bibset, "date=1980", "@attr 2=3 1980 "));
213     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
214     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
215     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
216     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
217     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980", "@attr 2=2 1980 "));
218     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980", "@attr 2=2 1980 "));
219     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980", "@attr 2=3 -1980 "));
220     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980", "@attr 2=2 1980 "));
221     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
222     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
223     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
224     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
225     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b", "@attr 4=1 @attr 1=4 \"a, b\" "));
226     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b", "@attr 4=2 @attr 1=4 a-b "));
227     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 \"a - b\" "));
228
229     YAZ_CHECK(tst_ccl_query(bibset, "a?", "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
230     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
231                             "@and @attr 4=2 @attr 1=1016 a "
232                             "@attr 4=2 @attr 1=1016 b "));
233
234     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
235                             "@and @attr 4=2 @attr 1=1016 a "
236                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
237
238     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
239                             "@attr 1=/my/title a "));
240
241     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
242                             "@or @attr 4=2 @attr 1=1016 a "
243                             "@attr 1=/my/title a "));
244
245     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
246                             /* correct */
247                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
248                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
249     ccl_qual_rm(&bibset);
250 }
251
252 int main(int argc, char **argv)
253 {
254     YAZ_CHECK_INIT(argc, argv);
255     YAZ_CHECK_LOG();
256     tst1(0);
257     tst1(1);
258     tst1(2);
259     tst1(3);
260     YAZ_CHECK_TERM;
261 }
262 /*
263  * Local variables:
264  * c-basic-offset: 4
265  * c-file-style: "Stroustrup"
266  * indent-tabs-mode: nil
267  * End:
268  * vim: shiftwidth=4 tabstop=8 expandtab
269  */
270