9205f0695939b6e8e9f40762b27c9bdb033acd55
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* $Id: scantest.c,v 1.10 2007-01-15 15:10:15 adam Exp $
2    Copyright (C) 1995-2007
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 #include <yaz/test.h>
28 #include <yaz/options.h>
29 #include <idzebra/dict.h>
30
31 struct handle_info {
32     int b;
33     int a;
34     int start_cut;
35     int end_cut;
36     char **ar;
37 };
38
39 static int handler(char *name, const char *info, int pos, void *client)
40 {
41     struct handle_info *hi = (struct handle_info *) client;
42     int idx;
43     if (pos > 0)
44         idx = hi->a - pos + hi->b;
45     else
46         idx = -pos - 1;
47
48     yaz_log(YLOG_DEBUG, "pos=%d idx=%d name=%s", pos, idx, name);
49     if (idx < 0)
50         return 0;
51     if (idx < hi->start_cut || idx >= hi->end_cut)
52     {
53         return 1;
54     }
55
56     hi->ar[idx] = malloc(strlen(name)+1);
57     strcpy(hi->ar[idx], name);
58
59     return 0;
60 }
61
62 int do_scan(Dict dict, int before, int after, const char *sterm,
63             char **cmp_strs,
64             int verbose, int start_cut, int end_cut)
65 {
66     struct handle_info hi;
67     char scan_term[1024];
68
69     int i;
70     int errors = 0;
71
72     strcpy(scan_term, sterm);
73     hi.start_cut = start_cut;
74     hi.end_cut = end_cut;
75     hi.a = after;
76     hi.b = before;
77     hi.ar = malloc(sizeof(char*) * (after+before+1));
78     for (i = 0; i<after+before; i++)
79         hi.ar[i] = 0;
80     yaz_log(YLOG_DEBUG, "dict_scan before=%d after=%d term=%s",
81             before, after, scan_term);
82     dict_scan (dict, scan_term, &before, &after, &hi, handler);
83     for (i = 0; i < hi.a+hi.b; i++)
84     {
85         if (!cmp_strs)
86         {
87             if (i >= start_cut &&  i < end_cut)
88             {
89                 if (!hi.ar[i])
90                 {
91                     printf ("--> FAIL i=%d hi.ar[i] == NULL\n", i);
92                     errors++;
93                 }
94             }
95             else
96             {
97                 if (hi.ar[i])
98                 {
99                     printf ("--> FAIL i=%d hi.ar[i] = %s\n", i, hi.ar[i]);
100                     errors++;
101                 }
102             }
103         }
104         else
105         {
106             if (i >= start_cut && i < end_cut)
107             {
108                 if (!hi.ar[i])
109                 {
110                     printf ("--> FAIL i=%d strs == NULL\n", i);
111                     errors++;
112                 }
113                 else if (!cmp_strs[i])
114                 {
115                     printf ("--> FAIL i=%d cmp_strs == NULL\n", i);
116                     errors++;
117                 }
118                 else if (strcmp(cmp_strs[i], hi.ar[i]))
119                 {
120                     printf ("--> FAIL i=%d expected %s\n", i, cmp_strs[i]);
121                     errors++;
122                 }
123             }
124             else
125             {
126                 if (hi.ar[i])
127                 {
128                     printf ("--> FAIL i=%d hi.ar[i] != NULL\n", i);
129                     errors++;
130                 }
131             }
132         }
133         if (verbose || errors)
134         {
135             if (i == hi.b)
136                 printf ("* ");
137             else
138                 printf ("  ");
139             if (hi.ar[i])
140                 printf ("%s", hi.ar[i]);
141             else
142                 printf ("NULL");
143             putchar('\n');
144         }
145         if (hi.ar[i])
146             free(hi.ar[i]);
147     }
148     free(hi.ar);
149     return errors;
150 }
151
152 static void tst(Dict dict, int start, int number)
153 {
154     int i;
155
156     /* insert again with original value again */
157     for (i = start; i < number; i += 100)
158     {
159         int v = i;
160         char w[32];
161         sprintf(w, "%d", i);
162         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2);
163     }
164     /* insert again with different value */
165     for (i = start; i < number; i += 100)
166     {
167         int v = i-1;
168         char w[32];
169         sprintf(w, "%d", i);
170         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
171     }
172     /* insert again with original value again */
173     for (i = start; i < number; i += 100)
174     {
175         int v = i;
176         char w[32];
177         sprintf(w, "%d", i);
178         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
179     }
180
181     {
182         char *cs[] = {
183             "4497",
184             "4498",
185             "4499",
186             "45"};
187         YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
188     }
189     {
190         char *cs[] = {
191             "4498",
192             "4499",
193             "45",
194             "450"};
195         YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
196     }
197
198     for (i = 0; i < 20; i++)
199         YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
200 }
201
202 int main(int argc, char **argv)
203 {
204     BFiles bfs = 0;
205     Dict dict = 0;
206     int i;
207     int ret;
208     int before = 0, after = 0, number = 10000;
209     char scan_term[1024];
210     char *arg;
211
212     YAZ_CHECK_INIT(argc, argv);
213
214     strcpy(scan_term, "1004");
215     while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
216     {
217         switch(ret)
218         {
219         case 0:
220             break;
221         case 'b':
222             before = atoi(arg);
223             break;
224         case 'a':
225             after = atoi(arg);
226             break;
227         case 't':
228             if (strlen(arg) >= sizeof(scan_term)-1)
229             {
230                 fprintf(stderr, "scan term too long\n");
231                 exit(1);
232             }
233             strcpy(scan_term, arg);
234             break;
235         case 'n':
236             number = atoi(arg);
237             break;
238         case 'v':
239             yaz_log_init_level(yaz_log_mask_str(arg));
240         }
241     }
242
243     bfs = bfs_create(".:100M", 0);
244     YAZ_CHECK(bfs);
245     if (bfs)
246     {
247         bf_reset(bfs);
248         dict = dict_open(bfs, "dict", 10, 1, 0, 0);
249         YAZ_CHECK(dict);
250     }
251     if (dict)
252     {
253         int start = 10;
254         /* Insert "10", "11", "12", .. "99", "100", ... number */
255         for (i = start; i<number; i++)
256         {
257             char w[32];
258             sprintf(w, "%d", i);
259             YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
260         }
261
262         if (after > 0 || before > 0)
263             do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
264         else
265             tst(dict, start, number);
266
267         dict_close(dict);
268     }
269     if (bfs)
270         bfs_destroy(bfs);
271     YAZ_CHECK_TERM;
272 }
273 /*
274  * Local variables:
275  * c-basic-offset: 4
276  * indent-tabs-mode: nil
277  * End:
278  * vim: shiftwidth=4 tabstop=8 expandtab
279  */
280