Ignore tst_rpn2cql generated test program
[yaz-moved-to-github.git] / test / tstccl.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 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/test.h>
11
12
13 static int tst_ccl_query(CCL_bibset bibset,
14                          const char *query,
15                          const char *result)
16 {
17     CCL_parser parser = ccl_parser_create(bibset);
18     int ret = 0;
19
20     if (parser && bibset)
21     {
22         struct ccl_rpn_node *rpn;
23         
24         rpn = ccl_parser_find_str(parser, query);
25         if (rpn)
26         {
27             /* parse ok. check that result is there and match */
28             WRBUF wrbuf = wrbuf_alloc();
29             ccl_pquery(wrbuf, rpn);
30             
31             /* check expect a result and that it matches */
32             if (result && !strcmp(wrbuf_cstr(wrbuf), result))
33                 ret = 1;
34             else
35                 ret = 0;
36             ccl_rpn_delete(rpn);
37             wrbuf_destroy(wrbuf);
38         }
39         else 
40         {
41             if (result)
42                 ret = 0;
43             else
44                 ret = 1;
45         }
46     }
47     ccl_parser_destroy (parser);
48     return ret;
49 }
50
51 void tst1(int pass)
52 {
53     CCL_bibset bibset = ccl_qual_mk();
54     char tstline[128];
55
56     YAZ_CHECK(bibset);
57     if (!bibset)
58         return;
59
60     switch(pass)
61     {
62     case 0:
63         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
64         ccl_qual_fitem(bibset, "1=1016 s=al,pw",    "term");
65         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
66         ccl_qual_fitem(bibset, "r=r",         "date");
67         ccl_qual_fitem(bibset, "r=o",         "x");
68         break;
69     case 1:
70         strcpy(tstline, "ti u=4    s=pw t=l,r");
71         ccl_qual_line(bibset, tstline);
72
73         strcpy(tstline, "term 1=1016 s=al,pw   # default term");
74         ccl_qual_line(bibset, tstline);
75
76         strcpy(tstline, "dc.title 1=/my/title");
77         ccl_qual_line(bibset, tstline);
78
79         strcpy(tstline, "date r=r # ordered relation");
80         ccl_qual_line(bibset, tstline);
81
82         strcpy(tstline, "x r=o # ordered relation");
83         ccl_qual_line(bibset, tstline);
84         break;
85     case 2:
86         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
87                      "term 1=1016 s=al,pw\r\n"
88                      "\n"
89                      "dc.title 1=/my/title\n"
90                      "date r=r\n" 
91                      "x r=o\n"
92             );
93         break;
94     case 3:
95 #if YAZ_HAVE_XML2
96         if (1)
97         {
98             xmlDocPtr doc;
99             int r;
100             const char *addinfo = 0;
101             const char *xml_str = 
102                 "<cclmap>\n"
103                 " <qual name=\"ti\">\n"
104                 "   <attr type=\"u\" value=\"4\"/>\n"
105                 "   <attr type=\"s\" value=\"pw\"/>\n"
106                 "   <attr type=\"t\" value=\"l,r\"/>\n"
107                 " </qual>\n"
108                 " <qual name=\"term\">\n"
109                 "   <attr type=\"1\" value=\"1016\"/>\n"
110                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
111                 " </qual>\n"
112                 " <qual name=\"dc.title\">\n"
113                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
114                 " </qual>\n"
115                 " <qual name=\"date\">\n"
116                 "   <attr type=\"r\" value=\"r\"/>\n"
117                 " </qual>\n"
118                 " <qual name=\"x\">\n"
119                 "   <attr type=\"r\" value=\"o\"/>\n"
120                 " </qual>\n"
121                 "</cclmap>\n";
122             
123             doc = xmlParseMemory(xml_str, strlen(xml_str));
124             YAZ_CHECK(doc);
125
126             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
127             YAZ_CHECK_EQ(r, 0);
128
129             xmlFreeDoc(doc);
130         }
131         break;
132 #else
133         return;
134 #endif
135     default:
136         YAZ_CHECK(0);
137         return;
138     }
139     
140     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
141     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
142     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
143                   "@and "
144                   "@attr 4=2 @attr 1=1016 x1 "
145                   "@attr 4=2 @attr 1=1016 x2 "));
146     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
147     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
148     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
149     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
150
151     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
152                   "@prox 0 1 0 2 k 2 "
153                   "@attr 4=2 @attr 1=1016 a "
154                   "@attr 4=2 @attr 1=1016 b "));
155     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
156                   "@prox 0 1 0 2 k 2 "
157                   "@attr 4=2 @attr 1=1016 a "
158                   "@attr 4=2 @attr 1=1016 b "));
159
160     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
161                   "@prox 0 2 0 2 k 2 "
162                   "@attr 4=2 @attr 1=1016 a "
163                   "@attr 4=2 @attr 1=1016 b "));
164
165     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
166                   "@prox 0 19 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, "spid%æserne", 
171                   "@prox 0 1 0 2 k 2 "
172                   "@attr 4=2 @attr 1=1016 spid "
173                   "@attr 4=2 @attr 1=1016 æserne "));
174
175     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
176                   "@prox 0 1 1 2 k 2 "
177                   "@attr 4=2 @attr 1=1016 a "
178                   "@attr 4=2 @attr 1=1016 b "));
179     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
180                   "@prox 0 2 1 2 k 2 "
181                   "@attr 4=2 @attr 1=1016 a "
182                   "@attr 4=2 @attr 1=1016 b "));
183
184     YAZ_CHECK(tst_ccl_query(bibset, "date=1980", "@attr 2=3 1980 "));
185     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
186     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
187     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
188     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
189     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980", "@attr 2=2 1980 "));
190     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980", "@attr 2=2 1980 "));
191     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980", "@attr 2=3 -1980 "));
192     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980", "@attr 2=2 1980 "));
193     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
194     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
195     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
196     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
197     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b", "@attr 4=1 @attr 1=4 \"a, b\" "));
198     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b", "@attr 4=2 @attr 1=4 a-b "));
199     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 \"a - b\" "));
200     ccl_qual_rm(&bibset);
201 }
202
203 int main(int argc, char **argv)
204 {
205     YAZ_CHECK_INIT(argc, argv);
206     YAZ_CHECK_LOG();
207     tst1(0);
208     tst1(1);
209     tst1(2);
210     tst1(3);
211     YAZ_CHECK_TERM;
212 }
213 /*
214  * Local variables:
215  * c-basic-offset: 4
216  * indent-tabs-mode: nil
217  * End:
218  * vim: shiftwidth=4 tabstop=8 expandtab
219  */
220