Update copyright year + FSF address
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* $Id: scantest.c,v 1.6 2006-08-14 10:40:09 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <yaz/log.h>
27
28 #include <yaz/options.h>
29 #include <idzebra/dict.h>
30
31 struct handle_info {
32     int b;
33     int a;
34     char **ar;
35 };
36
37 static int handler(char *name, const char *info, int pos, void *client)
38 {
39     struct handle_info *hi = (struct handle_info *) client;
40     int idx;
41     if (pos > 0)
42         idx = hi->a - pos + hi->b;
43     else
44         idx = -pos - 1;
45
46 #if 0
47     printf ("pos=%d idx=%d name=%s\n", pos, idx, name);
48 #endif
49     if (idx < 0)
50         return 0;
51
52     hi->ar[idx] = malloc(strlen(name)+1);
53     strcpy(hi->ar[idx], name);
54     return 0;
55 }
56
57 int tst(Dict dict, int before, int after, char *scan_term, char **cmp_strs,
58         int verbose)
59 {
60     struct handle_info hi;
61     int i;
62     int errors = 0;
63     hi.a = after;
64     hi.b = before;
65     hi.ar = malloc(sizeof(char*) * (after+before+1));
66     for (i = 0; i<after+before; i++)
67         hi.ar[i] = 0;
68     dict_scan (dict, scan_term, &before, &after, &hi, handler);
69     for (i = 0; i<hi.a+hi.b; i++)
70     {
71         if (cmp_strs)
72         {
73             if (!cmp_strs[i])
74             {
75                 printf ("--> FAIL cmp_strs == NULL\n");
76                 errors++;
77             }
78             else if (!hi.ar[i])
79             {
80                 printf ("--> FAIL strs == NULL\n");
81                 errors++;
82             }
83             else if (strcmp(cmp_strs[i], hi.ar[i]))
84             {
85                 printf ("--> FAIL expected %s\n", cmp_strs[i]);
86                 errors++;
87             }
88         }
89         if (verbose || errors)
90         {
91             if (i == hi.b)
92                 printf ("* ");
93             else
94                 printf ("  ");
95             if (hi.ar[i])
96                 printf ("%s", hi.ar[i]);
97             else
98                 printf ("NULL");
99             putchar('\n');
100         }
101         if (hi.ar[i])
102             free(hi.ar[i]);
103     }
104     free(hi.ar);
105     return errors;
106 }
107
108 int main(int argc, char **argv)
109 {
110     BFiles bfs;
111     Dict dict;
112     int i;
113     int errors = 0;
114     int ret;
115     int before = 0, after = 0, number = 10000;
116     char scan_term[1024];
117     char *arg;
118
119     strcpy(scan_term, "1004");
120     while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
121     {
122         switch(ret)
123         {
124         case 0:
125             break;
126         case 'b':
127             before = atoi(arg);
128             break;
129         case 'a':
130             after = atoi(arg);
131             break;
132         case 't':
133             strcpy(scan_term, arg);
134             break;
135         case 'n':
136             number = atoi(arg);
137             break;
138         case 'v':
139             yaz_log_init_level(yaz_log_mask_str(arg));
140         }
141     }
142
143     bfs = bfs_create(".:100M", 0);
144     if (!bfs)
145     {
146         fprintf(stderr, "bfs_create failed\n");
147         exit(1);
148     }
149     dict = dict_open(bfs, "dict", 10, 1, 0, 0);
150     for (i = 10; i<number; i++)
151     {
152         int r;
153         char w[32];
154         sprintf(w, "%d", i);
155         r = dict_insert (dict, w, sizeof(int), &i);
156     }
157
158     if (after > 0 || before > 0)
159         tst(dict, before, after, scan_term, 0, 1);
160     else
161     {
162         if (argc <= 1)
163         {
164             char *cs[] = {
165                 "4497",
166                 "4498",
167                 "4499",
168                 "45"};
169             strcpy(scan_term, "4499");
170             errors += tst(dict, 2, 2, scan_term, cs, 0);
171         }
172         if (argc <= 1)
173         {
174             char *cs[] = {
175                 "4498",
176                 "4499",
177                 "45",
178                 "450"};
179             strcpy(scan_term, "45");
180             errors += tst(dict, 2, 2, scan_term, cs, 0);
181         }
182     }
183     dict_close(dict);
184     bfs_destroy(bfs);
185     if (errors)
186         exit(1);
187     exit(0);
188 }
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * indent-tabs-mode: nil
193  * End:
194  * vim: shiftwidth=4 tabstop=8 expandtab
195  */
196