TCPD libs only used in libyaz's LIBADD
[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
6 /* CCL test */
7
8 #include <string.h>
9 #include <yaz/ccl_xml.h>
10 #include <yaz/log.h>
11 #include <yaz/test.h>
12
13
14 static int tst_ccl_query(CCL_bibset bibset,
15                          const char *query,
16                          const char *result)
17 {
18     CCL_parser parser = ccl_parser_create(bibset);
19     int ret = 0;
20
21     if (parser && bibset)
22     {
23         struct ccl_rpn_node *rpn;
24         
25         rpn = ccl_parser_find_str(parser, query);
26         if (rpn)
27         {
28             /* parse ok. check that result is there and match */
29             WRBUF wrbuf = wrbuf_alloc();
30             ccl_pquery(wrbuf, rpn);
31             
32             /* check expect a result and that it matches */
33             if (result && !strcmp(wrbuf_cstr(wrbuf), result))
34                 ret = 1;
35             else
36             {
37                 yaz_log(YLOG_WARN, "%s: result does not match", query);
38                 yaz_log(YLOG_WARN, " expected %s", result);
39                 yaz_log(YLOG_WARN, " got      %s", wrbuf_cstr(wrbuf));
40                 ret = 0;
41             }
42             ccl_rpn_delete(rpn);
43             wrbuf_destroy(wrbuf);
44         }
45         else 
46         {
47             if (result)
48             {
49                 yaz_log(YLOG_WARN, "%s: parse failed", query);
50                 ret = 0;
51             }
52             else
53                 ret = 1;
54         }
55     }
56     ccl_parser_destroy (parser);
57     return ret;
58 }
59
60 void tst1(int pass)
61 {
62     CCL_bibset bibset = ccl_qual_mk();
63     char tstline[128];
64
65     YAZ_CHECK(bibset);
66     if (!bibset)
67         return;
68
69     switch(pass)
70     {
71     case 0:
72         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
73         ccl_qual_fitem(bibset, "1=1016 s=al,pw t=r",    "term");
74         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
75         ccl_qual_fitem(bibset, "r=r",         "date");
76         ccl_qual_fitem(bibset, "r=o",         "x");
77         ccl_qual_fitem(bibset, "dc.title", "title");
78         ccl_qual_fitem(bibset, "term dc.title", "comb");
79         break;
80     case 1:
81         strcpy(tstline, "ti u=4    s=pw t=l,r");
82         ccl_qual_line(bibset, tstline);
83
84         strcpy(tstline, "term 1=1016 s=al,pw t=r  # default term");
85         ccl_qual_line(bibset, tstline);
86
87         strcpy(tstline, "dc.title 1=/my/title");
88         ccl_qual_line(bibset, tstline);
89
90         strcpy(tstline, "date r=r # ordered relation");
91         ccl_qual_line(bibset, tstline);
92
93         strcpy(tstline, "x r=o # ordered relation");
94         ccl_qual_line(bibset, tstline);
95
96         strcpy(tstline, "title dc.title # alias");
97         ccl_qual_line(bibset, tstline);
98
99         strcpy(tstline, "comb term dc.title # combination");
100         ccl_qual_line(bibset, tstline);
101         break;
102     case 2:
103         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
104                      "term 1=1016 s=al,pw t=r\r\n"
105                      "\n"
106                      "dc.title 1=/my/title\n"
107                      "date r=r\n" 
108                      "x r=o\n"
109                      "title dc.title\n"
110                      "comb term dc.title\n"
111             );
112         break;
113     case 3:
114 #if YAZ_HAVE_XML2
115         if (1)
116         {
117             xmlDocPtr doc;
118             int r;
119             const char *addinfo = 0;
120             const char *xml_str = 
121                 "<cclmap>\n"
122                 " <qual name=\"ti\">\n"
123                 "   <attr type=\"u\" value=\"4\"/>\n"
124                 "   <attr type=\"s\" value=\"pw\"/>\n"
125                 "   <attr type=\"t\" value=\"l,r\"/>\n"
126                 " </qual>\n"
127                 " <qual name=\"term\">\n"
128                 "   <attr type=\"1\" value=\"1016\"/>\n"
129                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
130                 "   <attr type=\"t\" value=\"r\"/>\n"
131                 " </qual>\n"
132                 " <qual name=\"dc.title\">\n"
133                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
134                 " </qual>\n"
135                 " <qual name=\"date\">\n"
136                 "   <attr type=\"r\" value=\"r\"/>\n"
137                 " </qual>\n"
138                 " <qual name=\"x\">\n"
139                 "   <attr type=\"r\" value=\"o\"/>\n"
140                 " </qual>\n"
141                 " <qual name=\"title\">\n"
142                 "   <qual name=\"dc.title\"/>\n"
143                 " </qual>\n"
144                 " <qual name=\"comb\">\n"
145                 "   <qual name=\"term\"/>\n"
146                 "   <qual name=\"dc.title\"/>\n"
147                 " </qual>\n"
148                 "</cclmap>\n";
149             
150             doc = xmlParseMemory(xml_str, strlen(xml_str));
151             YAZ_CHECK(doc);
152
153             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
154             YAZ_CHECK_EQ(r, 0);
155
156             xmlFreeDoc(doc);
157         }
158         break;
159 #else
160         return;
161 #endif
162     default:
163         YAZ_CHECK(0);
164         return;
165     }
166     
167     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
168     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
169     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
170                   "@and "
171                   "@attr 4=2 @attr 1=1016 x1 "
172                   "@attr 4=2 @attr 1=1016 x2 "));
173     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
174     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
175     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
176     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
177
178     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
179                   "@prox 0 1 0 2 k 2 "
180                   "@attr 4=2 @attr 1=1016 a "
181                   "@attr 4=2 @attr 1=1016 b "));
182     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
183                   "@prox 0 1 0 2 k 2 "
184                   "@attr 4=2 @attr 1=1016 a "
185                   "@attr 4=2 @attr 1=1016 b "));
186
187     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
188                   "@prox 0 2 0 2 k 2 "
189                   "@attr 4=2 @attr 1=1016 a "
190                   "@attr 4=2 @attr 1=1016 b "));
191
192     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
193                   "@prox 0 19 0 2 k 2 "
194                   "@attr 4=2 @attr 1=1016 a "
195                   "@attr 4=2 @attr 1=1016 b "));
196
197     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
198                   "@prox 0 1 0 2 k 2 "
199                   "@attr 4=2 @attr 1=1016 spid "
200                   "@attr 4=2 @attr 1=1016 æserne "));
201
202     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
203                   "@prox 0 1 1 2 k 2 "
204                   "@attr 4=2 @attr 1=1016 a "
205                   "@attr 4=2 @attr 1=1016 b "));
206     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
207                   "@prox 0 2 1 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, "date=1980", "@attr 2=3 1980 "));
212     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
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=-1980", "@attr 2=2 1980 "));
217     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980", "@attr 2=2 1980 "));
218     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980", "@attr 2=3 -1980 "));
219     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980", "@attr 2=2 1980 "));
220     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
221     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
222     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
223     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
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=2 @attr 1=4 a-b "));
226     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 \"a - b\" "));
227
228     YAZ_CHECK(tst_ccl_query(bibset, "a?", "@attr 5=1 @attr 4=2 @attr 1=1016 a "));
229     YAZ_CHECK(tst_ccl_query(bibset, "a b", 
230                             "@and @attr 4=2 @attr 1=1016 a "
231                             "@attr 4=2 @attr 1=1016 b "));
232
233     YAZ_CHECK(tst_ccl_query(bibset, "a b?", 
234                             "@and @attr 4=2 @attr 1=1016 a "
235                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
236
237     YAZ_CHECK(tst_ccl_query(bibset, "title=a", 
238                             "@attr 1=/my/title a "));
239
240     YAZ_CHECK(tst_ccl_query(bibset, "comb=a", 
241                             "@or @attr 4=2 @attr 1=1016 a "
242                             "@attr 1=/my/title a "));
243
244     /* Bug #2895 */
245 #if 1
246     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
247                             /* incorrect. */
248                             "@and @attr 4=2 @attr 1=1016 a? "
249                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
250 #else
251     YAZ_CHECK(tst_ccl_query(bibset, "a? b?", 
252                             /* correct */
253                             "@and @attr 5=1 @attr 4=2 @attr 1=1016 a "
254                             "@attr 5=1 @attr 4=2 @attr 1=1016 b "));
255 #endif
256     ccl_qual_rm(&bibset);
257 }
258
259 int main(int argc, char **argv)
260 {
261     YAZ_CHECK_INIT(argc, argv);
262     YAZ_CHECK_LOG();
263     tst1(0);
264     tst1(1);
265     tst1(2);
266     tst1(3);
267     YAZ_CHECK_TERM;
268 }
269 /*
270  * Local variables:
271  * c-basic-offset: 4
272  * c-file-style: "Stroustrup"
273  * indent-tabs-mode: nil
274  * End:
275  * vim: shiftwidth=4 tabstop=8 expandtab
276  */
277