Added a record for testing of bug #1778.
[yaz-moved-to-github.git] / test / tstccl.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstccl.c,v 1.17 2007-04-27 09:48:28 adam Exp $
6  */
7
8 /* CCL test */
9
10 #include <string.h>
11 #include <yaz/ccl_xml.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                 ret = 0;
38             ccl_rpn_delete(rpn);
39             wrbuf_destroy(wrbuf);
40         }
41         else 
42         {
43             if (result)
44                 ret = 0;
45             else
46                 ret = 1;
47         }
48     }
49     ccl_parser_destroy (parser);
50     return ret;
51 }
52
53 void tst1(int pass)
54 {
55     CCL_bibset bibset = ccl_qual_mk();
56     char tstline[128];
57
58     YAZ_CHECK(bibset);
59     if (!bibset)
60         return;
61
62     switch(pass)
63     {
64     case 0:
65         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
66         ccl_qual_fitem(bibset, "1=1016 s=al,pw",    "term");
67         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
68         ccl_qual_fitem(bibset, "r=r",         "date");
69         ccl_qual_fitem(bibset, "r=o",         "x");
70         break;
71     case 1:
72         strcpy(tstline, "ti u=4    s=pw t=l,r");
73         ccl_qual_line(bibset, tstline);
74
75         strcpy(tstline, "term 1=1016 s=al,pw   # default term");
76         ccl_qual_line(bibset, tstline);
77
78         strcpy(tstline, "dc.title 1=/my/title");
79         ccl_qual_line(bibset, tstline);
80
81         strcpy(tstline, "date r=r # ordered relation");
82         ccl_qual_line(bibset, tstline);
83
84         strcpy(tstline, "x r=o # ordered relation");
85         ccl_qual_line(bibset, tstline);
86         break;
87     case 2:
88         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
89                      "term 1=1016 s=al,pw\r\n"
90                      "\n"
91                      "dc.title 1=/my/title\n"
92                      "date r=r\n" 
93                      "x r=o\n"
94             );
95         break;
96     case 3:
97 #if YAZ_HAVE_XML2
98         if (1)
99         {
100             xmlDocPtr doc;
101             int r;
102             const char *addinfo = 0;
103             const char *xml_str = 
104                 "<cclmap>\n"
105                 " <qual name=\"ti\">\n"
106                 "   <attr type=\"u\" value=\"4\"/>\n"
107                 "   <attr type=\"s\" value=\"pw\"/>\n"
108                 "   <attr type=\"t\" value=\"l,r\"/>\n"
109                 " </qual>\n"
110                 " <qual name=\"term\">\n"
111                 "   <attr type=\"1\" value=\"1016\"/>\n"
112                 "   <attr type=\"s\" value=\"al,pw\"/>\n"
113                 " </qual>\n"
114                 " <qual name=\"dc.title\">\n"
115                 "   <attr type=\"1\" value=\"/my/title\"/>\n"
116                 " </qual>\n"
117                 " <qual name=\"date\">\n"
118                 "   <attr type=\"r\" value=\"r\"/>\n"
119                 " </qual>\n"
120                 " <qual name=\"x\">\n"
121                 "   <attr type=\"r\" value=\"o\"/>\n"
122                 " </qual>\n"
123                 "</cclmap>\n";
124             
125             doc = xmlParseMemory(xml_str, strlen(xml_str));
126             YAZ_CHECK(doc);
127
128             r = ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo);
129             YAZ_CHECK_EQ(r, 0);
130
131             xmlFreeDoc(doc);
132         }
133         break;
134 #else
135         return;
136 #endif
137     default:
138         YAZ_CHECK(0);
139         return;
140     }
141     
142     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
143     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
144     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
145                   "@and "
146                   "@attr 4=2 @attr 1=1016 x1 "
147                   "@attr 4=2 @attr 1=1016 x2 "));
148     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
149     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
150     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
151     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
152
153     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
154                   "@prox 0 1 0 2 k 2 "
155                   "@attr 4=2 @attr 1=1016 a "
156                   "@attr 4=2 @attr 1=1016 b "));
157     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
158                   "@prox 0 1 0 2 k 2 "
159                   "@attr 4=2 @attr 1=1016 a "
160                   "@attr 4=2 @attr 1=1016 b "));
161
162     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
163                   "@prox 0 2 0 2 k 2 "
164                   "@attr 4=2 @attr 1=1016 a "
165                   "@attr 4=2 @attr 1=1016 b "));
166
167     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
168                   "@prox 0 19 0 2 k 2 "
169                   "@attr 4=2 @attr 1=1016 a "
170                   "@attr 4=2 @attr 1=1016 b "));
171
172     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
173                   "@prox 0 1 0 2 k 2 "
174                   "@attr 4=2 @attr 1=1016 spid "
175                   "@attr 4=2 @attr 1=1016 æserne "));
176
177     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
178                   "@prox 0 1 1 2 k 2 "
179                   "@attr 4=2 @attr 1=1016 a "
180                   "@attr 4=2 @attr 1=1016 b "));
181     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
182                   "@prox 0 2 1 2 k 2 "
183                   "@attr 4=2 @attr 1=1016 a "
184                   "@attr 4=2 @attr 1=1016 b "));
185
186     YAZ_CHECK(tst_ccl_query(bibset, "date=1980", "@attr 2=3 1980 "));
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=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
190     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
191     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980", "@attr 2=2 1980 "));
192     YAZ_CHECK(tst_ccl_query(bibset, "date=- 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=- 1980", "@attr 2=2 1980 "));
195     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
196     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
197     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
198     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
199     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b", "@attr 4=1 @attr 1=4 a,\\ b "));
200     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b", "@attr 4=2 @attr 1=4 a-b "));
201     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 a\\ -\\ b "));
202     ccl_qual_rm(&bibset);
203 }
204
205 int main(int argc, char **argv)
206 {
207     YAZ_CHECK_INIT(argc, argv);
208     YAZ_CHECK_LOG();
209     tst1(0);
210     tst1(1);
211     tst1(2);
212     tst1(3);
213     YAZ_CHECK_TERM;
214 }
215 /*
216  * Local variables:
217  * c-basic-offset: 4
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */
222