Fix sample PQF
[yaz-moved-to-github.git] / ccl / tstccl.c
1 /*
2  * Copyright (c) 2002-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: tstccl.c,v 1.2 2003-06-24 23:03:04 adam Exp $
6  */
7
8 /* CCL test */
9
10 #include <yaz/ccl.h>
11
12 struct ccl_tst {
13     char *query;
14     char *result;
15 };
16
17 static struct ccl_tst query_str[] = {
18     { "x1", "@attr 4=2 @attr 1=1016 x1 "},
19     { "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "},
20     {"x1 and x2", "@and @attr 4=2 @attr 1=1016 x1 @attr 4=2 @attr 1=1016 x2 "},
21     { "ti=x3", "@attr 4=2 @attr 1=4 x3 "},
22     { "dc.title=x4", "@attr 1=/my/title x4 "},
23     { "x1 and", 0},
24     { "tix=x5", 0},
25     {0, 0}
26 };
27
28 void tst1(void)
29 {
30     CCL_parser parser = ccl_parser_create ();
31     CCL_bibset bibset = ccl_qual_mk();
32     int i;
33
34     ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
35     ccl_qual_fitem(bibset, "1=1016 s=al,pw",    "term");
36     ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
37
38     parser->bibset = bibset;
39
40     for (i = 0; query_str[i].query; i++)
41     {
42         struct ccl_token *token_list =
43             ccl_parser_tokenize(parser, query_str[i].query);
44         struct ccl_rpn_node *rpn = ccl_parser_find(parser, token_list);
45         ccl_token_del (token_list);
46         if (rpn)
47         {
48             WRBUF wrbuf = wrbuf_alloc();
49             ccl_pquery(wrbuf, rpn);
50
51             if (!query_str[i].result)
52             {
53                 printf ("Failed %s\n", query_str[i].query);
54                 printf (" got:%s:\n", wrbuf_buf(wrbuf));
55                 printf (" expected failure\n");
56                 exit(3);
57             }
58             else if (strcmp(wrbuf_buf(wrbuf), query_str[i].result))
59             {
60                 printf ("Failed %s\n", query_str[i].query);
61                 printf (" got:%s:\n", wrbuf_buf(wrbuf));
62                 printf (" expected:%s:\n", query_str[i].result);
63                 exit(2);
64             }
65             ccl_rpn_delete(rpn);
66             wrbuf_free(wrbuf, 1);
67         }
68         else if (query_str[i].result)
69         {
70             printf ("Failed %s\n", query_str[i].query);
71             printf (" got failure\n");
72             printf (" expected:%s:\n", query_str[i].result);
73             exit(4);
74         }
75     }   
76     ccl_parser_destroy (parser);
77     ccl_qual_rm(&bibset);
78 }
79
80 int main(int argc, char **argv)
81 {
82     tst1();
83     exit(0);
84 }