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