6b3ff05c1c3345aa0a93f90c82f0a8df0df6aacf
[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.16 2007-04-25 20:52:19 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         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