Added include stdlib.h
[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.6 2005-02-01 17:23:36 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     {0, 0}
41 };
42
43 void tst1(int pass, int *number_of_errors)
44 {
45     CCL_parser parser = ccl_parser_create ();
46     CCL_bibset bibset = ccl_qual_mk();
47     int i;
48     char tstline[128];
49
50     switch(pass)
51     {
52     case 0:
53         ccl_qual_fitem(bibset, "u=4    s=pw t=l,r", "ti");
54         ccl_qual_fitem(bibset, "1=1016 s=al,pw",    "term");
55         ccl_qual_fitem(bibset, "1=/my/title",         "dc.title");
56         ccl_qual_fitem(bibset, "r=r",         "date");
57         ccl_qual_fitem(bibset, "r=o",         "x");
58         break;
59     case 1:
60         strcpy(tstline, "ti u=4    s=pw t=l,r");
61         ccl_qual_line(bibset, tstline);
62
63         strcpy(tstline, "term 1=1016 s=al,pw   # default term");
64         ccl_qual_line(bibset, tstline);
65
66         strcpy(tstline, "dc.title 1=/my/title");
67         ccl_qual_line(bibset, tstline);
68
69         strcpy(tstline, "date r=r # ordered relation");
70         ccl_qual_line(bibset, tstline);
71
72         strcpy(tstline, "x r=o # ordered relation");
73         ccl_qual_line(bibset, tstline);
74         break;
75     case 2:
76         ccl_qual_buf(bibset, "ti u=4    s=pw t=l,r\n"
77                      "term 1=1016 s=al,pw\r\n"
78                      "\n"
79                      "dc.title 1=/my/title\n"
80                      "date r=r\n" 
81                      "x r=o\n"
82             );
83         break;
84     default:
85         exit(23);
86     }
87
88     parser->bibset = bibset;
89
90     for (i = 0; query_str[i].query; i++)
91     {
92         struct ccl_token *token_list =
93             ccl_parser_tokenize(parser, query_str[i].query);
94         struct ccl_rpn_node *rpn = ccl_parser_find(parser, token_list);
95         ccl_token_del (token_list);
96         if (rpn)
97         {
98             WRBUF wrbuf = wrbuf_alloc();
99             ccl_pquery(wrbuf, rpn);
100
101             if (!query_str[i].result)
102             {
103                 printf ("Failed %s\n", query_str[i].query);
104                 printf (" got:%s:\n", wrbuf_buf(wrbuf));
105                 printf (" expected failure\n");
106                 (*number_of_errors)++;
107             }
108             else if (strcmp(wrbuf_buf(wrbuf), query_str[i].result))
109             {
110                 printf ("Failed %s\n", query_str[i].query);
111                 printf (" got:%s:\n", wrbuf_buf(wrbuf));
112                 printf (" expected:%s:\n", query_str[i].result);
113                 (*number_of_errors)++;
114             }
115             ccl_rpn_delete(rpn);
116             wrbuf_free(wrbuf, 1);
117         }
118         else if (query_str[i].result)
119         {
120             printf ("Failed %s\n", query_str[i].query);
121             printf (" got failure\n");
122             printf (" expected:%s:\n", query_str[i].result);
123             (*number_of_errors)++;
124         }
125     }   
126     ccl_parser_destroy (parser);
127     ccl_qual_rm(&bibset);
128 }
129
130 int main(int argc, char **argv)
131 {
132     int number_of_errors = 0;
133     tst1(0, &number_of_errors);
134     if (number_of_errors)
135         exit(1);
136     tst1(1, &number_of_errors);
137     if (number_of_errors)
138         exit(1);
139     tst1(2, &number_of_errors);
140     if (number_of_errors)
141         exit(1);
142     exit(0);
143 }