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