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