Fix bug in CCL parser that could caused uninialized values
[yaz-moved-to-github.git] / test / tstccl.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstccl.c,v 1.8 2005-04-15 21:47:56 adam Exp $
6  */
7
8 /* CCL test */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <yaz/ccl.h>
13
14 struct ccl_tst {
15     char *query;
16     char *result;
17 };
18
19 static struct ccl_tst query_str[] = {
20     { "x1", "@attr 4=2 @attr 1=1016 x1 "},
21     { "(((((x1)))))", "@attr 4=2 @attr 1=1016 x1 "},
22     {"x1 and x2", "@and @attr 4=2 @attr 1=1016 x1 @attr 4=2 @attr 1=1016 x2 "},
23     { "ti=x3", "@attr 4=2 @attr 1=4 x3 "},
24     { "dc.title=x4", "@attr 1=/my/title x4 "},
25     { "x1 and", 0},
26     { "tix=x5", 0},
27     { "spid%æserne", "@prox 0 1 0 2 k 2 @attr 4=2 @attr 1=1016 spid @attr 4=2 @attr 1=1016 æserne "},
28     { "date=1980", "@attr 2=3 1980 "},
29     { "date=234-1990", "@and @attr 2=4 234 @attr 2=2 1990 "},
30     { "date=234- 1990", "@and @attr 2=4 234 @attr 2=2 1990 "},
31     { "date=234 -1990", "@and @attr 2=4 234 @attr 2=2 1990 "},
32     { "date=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "},
33     { "date=-1980", "@attr 2=2 1980 "},
34     { "date=- 1980", "@attr 2=2 1980 "},
35     { "x=-1980", "@attr 2=3 -1980 "},
36     { "x=- 1980", "@attr 2=2 1980 "},
37     { "x= -1980", "@attr 2=3 -1980 "},
38     { "x=234-1990", "@attr 2=3 234-1990 "},
39     { "x=234 - 1990", "@and @attr 2=4 234 @attr 2=2 1990 "},
40     { "ti=a,b", "@attr 4=1 @attr 1=4 a,b "},
41     { "ti=a, b", "@attr 4=1 @attr 1=4 a,\\ b "},
42     { "ti=a-b", "@attr 4=2 @attr 1=4 a-b "},
43     { "ti=a - b", "@attr 4=1 @attr 1=4 a\\ -\\ b "},
44     {0, 0}
45 };
46
47 void tst1(int pass, int *number_of_errors)
48 {
49     CCL_parser parser = ccl_parser_create ();
50     CCL_bibset bibset = ccl_qual_mk();
51     int i;
52     char tstline[128];
53
54     switch(pass)
55     {
56     case 0:
57         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
58         ccl_qual_fitem(bibset, "1=1016 s=al,pw",    "term");
59         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
60         ccl_qual_fitem(bibset, "r=r",         "date");
61         ccl_qual_fitem(bibset, "r=o",         "x");
62         break;
63     case 1:
64         strcpy(tstline, "ti u=4    s=pw t=l,r");
65         ccl_qual_line(bibset, tstline);
66
67         strcpy(tstline, "term 1=1016 s=al,pw   # default term");
68         ccl_qual_line(bibset, tstline);
69
70         strcpy(tstline, "dc.title 1=/my/title");
71         ccl_qual_line(bibset, tstline);
72
73         strcpy(tstline, "date r=r # ordered relation");
74         ccl_qual_line(bibset, tstline);
75
76         strcpy(tstline, "x r=o # ordered relation");
77         ccl_qual_line(bibset, tstline);
78         break;
79     case 2:
80         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
81                      "term 1=1016 s=al,pw\r\n"
82                      "\n"
83                      "dc.title 1=/my/title\n"
84                      "date r=r\n" 
85                      "x r=o\n"
86             );
87         break;
88     default:
89         exit(23);
90     }
91
92     parser->bibset = bibset;
93
94     for (i = 0; query_str[i].query; i++)
95     {
96         struct ccl_token *token_list;
97         struct ccl_rpn_node *rpn;
98
99         token_list = ccl_parser_tokenize(parser, query_str[i].query);
100         rpn = ccl_parser_find(parser, token_list);
101         ccl_token_del (token_list);
102         if (rpn)
103         {
104             WRBUF wrbuf = wrbuf_alloc();
105             ccl_pquery(wrbuf, rpn);
106
107             if (!query_str[i].result)
108             {
109                 printf ("Failed %s\n", query_str[i].query);
110                 printf (" got:     %s:\n", wrbuf_buf(wrbuf));
111                 printf (" expected failure\n");
112                 (*number_of_errors)++;
113             }
114             else if (strcmp(wrbuf_buf(wrbuf), query_str[i].result))
115             {
116                 printf ("Failed %s\n", query_str[i].query);
117                 printf (" got:     %s:\n", wrbuf_buf(wrbuf));
118                 printf (" expected:%s:\n", query_str[i].result);
119                 (*number_of_errors)++;
120             }
121             ccl_rpn_delete(rpn);
122             wrbuf_free(wrbuf, 1);
123         }
124         else if (query_str[i].result)
125         {
126             printf ("Failed %s\n", query_str[i].query);
127             printf (" got failure\n");
128             printf (" expected:%s:\n", query_str[i].result);
129             (*number_of_errors)++;
130         }
131     }   
132     ccl_parser_destroy (parser);
133     ccl_qual_rm(&bibset);
134 }
135
136 int main(int argc, char **argv)
137 {
138     int number_of_errors = 0;
139     tst1(0, &number_of_errors);
140     if (number_of_errors)
141         exit(1);
142     tst1(1, &number_of_errors);
143     if (number_of_errors)
144         exit(1);
145     tst1(2, &number_of_errors);
146     if (number_of_errors)
147         exit(1);
148     exit(0);
149 }