CCL proximity tests
[yaz-moved-to-github.git] / test / tstccl.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstccl.c,v 1.11 2006-07-31 11:42:32 adam Exp $
6  */
7
8 /* CCL test */
9
10 #include <string.h>
11 #include <yaz/ccl.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     default:
102         YAZ_CHECK(0);
103         return;
104     }
105     
106     YAZ_CHECK(tst_ccl_query(bibset, "x1", "@attr 4=2 @attr 1=1016 x1 "));
107     YAZ_CHECK(tst_ccl_query(bibset, "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "));
108     YAZ_CHECK(tst_ccl_query(bibset, "x1 and x2",
109                   "@and "
110                   "@attr 4=2 @attr 1=1016 x1 "
111                   "@attr 4=2 @attr 1=1016 x2 "));
112     YAZ_CHECK(tst_ccl_query(bibset, "ti=x3", "@attr 4=2 @attr 1=4 x3 "));
113     YAZ_CHECK(tst_ccl_query(bibset, "dc.title=x4", "@attr 1=/my/title x4 "));
114     YAZ_CHECK(tst_ccl_query(bibset, "x1 and", 0));
115     YAZ_CHECK(tst_ccl_query(bibset, "tix=x5", 0));
116
117     YAZ_CHECK(tst_ccl_query(bibset, "a%b", 
118                   "@prox 0 1 0 2 k 2 "
119                   "@attr 4=2 @attr 1=1016 a "
120                   "@attr 4=2 @attr 1=1016 b "));
121     YAZ_CHECK(tst_ccl_query(bibset, "a%1b", 
122                   "@prox 0 1 0 2 k 2 "
123                   "@attr 4=2 @attr 1=1016 a "
124                   "@attr 4=2 @attr 1=1016 b "));
125
126     YAZ_CHECK(tst_ccl_query(bibset, "a%2b", 
127                   "@prox 0 2 0 2 k 2 "
128                   "@attr 4=2 @attr 1=1016 a "
129                   "@attr 4=2 @attr 1=1016 b "));
130
131     YAZ_CHECK(tst_ccl_query(bibset, "a%19b", 
132                   "@prox 0 19 0 2 k 2 "
133                   "@attr 4=2 @attr 1=1016 a "
134                   "@attr 4=2 @attr 1=1016 b "));
135
136     YAZ_CHECK(tst_ccl_query(bibset, "spid%æserne", 
137                   "@prox 0 1 0 2 k 2 "
138                   "@attr 4=2 @attr 1=1016 spid "
139                   "@attr 4=2 @attr 1=1016 æserne "));
140
141     YAZ_CHECK(tst_ccl_query(bibset, "a!b", 
142                   "@prox 0 1 1 2 k 2 "
143                   "@attr 4=2 @attr 1=1016 a "
144                   "@attr 4=2 @attr 1=1016 b "));
145     YAZ_CHECK(tst_ccl_query(bibset, "a!2b", 
146                   "@prox 0 2 1 2 k 2 "
147                   "@attr 4=2 @attr 1=1016 a "
148                   "@attr 4=2 @attr 1=1016 b "));
149
150     YAZ_CHECK(tst_ccl_query(bibset, "date=1980", "@attr 2=3 1980 "));
151     YAZ_CHECK(tst_ccl_query(bibset, "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
152     YAZ_CHECK(tst_ccl_query(bibset, "date=234- 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
153     YAZ_CHECK(tst_ccl_query(bibset, "date=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
154     YAZ_CHECK(tst_ccl_query(bibset, "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
155     YAZ_CHECK(tst_ccl_query(bibset, "date=-1980", "@attr 2=2 1980 "));
156     YAZ_CHECK(tst_ccl_query(bibset, "date=- 1980", "@attr 2=2 1980 "));
157     YAZ_CHECK(tst_ccl_query(bibset, "x=-1980", "@attr 2=3 -1980 "));
158     YAZ_CHECK(tst_ccl_query(bibset, "x=- 1980", "@attr 2=2 1980 "));
159     YAZ_CHECK(tst_ccl_query(bibset, "x= -1980", "@attr 2=3 -1980 "));
160     YAZ_CHECK(tst_ccl_query(bibset, "x=234-1990", "@attr 2=3 234-1990 "));
161     YAZ_CHECK(tst_ccl_query(bibset, "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "));
162     YAZ_CHECK(tst_ccl_query(bibset, "ti=a,b", "@attr 4=1 @attr 1=4 a,b "));
163     YAZ_CHECK(tst_ccl_query(bibset, "ti=a, b", "@attr 4=1 @attr 1=4 a,\\ b "));
164     YAZ_CHECK(tst_ccl_query(bibset, "ti=a-b", "@attr 4=2 @attr 1=4 a-b "));
165     YAZ_CHECK(tst_ccl_query(bibset, "ti=a - b", "@attr 4=1 @attr 1=4 a\\ -\\ b "));
166     ccl_qual_rm(&bibset);
167 }
168
169 int main(int argc, char **argv)
170 {
171     YAZ_CHECK_INIT(argc, argv);
172     tst1(0);
173     tst1(1);
174     tst1(2);
175     YAZ_CHECK_TERM;
176 }
177 /*
178  * Local variables:
179  * c-basic-offset: 4
180  * indent-tabs-mode: nil
181  * End:
182  * vim: shiftwidth=4 tabstop=8 expandtab
183  */
184