Add test for bug 4592: dict_scan misses
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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_LOG, "scan_handler name=%s pos=%d idx=%d", name, pos, idx);
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 #if 1
182     {
183         char *cs[] = {
184             "4497",
185             "4498",
186             "4499",
187             "45"};
188         yaz_log(YLOG_LOG, "---------------------1 ---------------" );
189         YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
190     }
191 #endif
192 #if 1
193     {
194         char *cs[] = {
195             "4498",
196             "4499",
197             "45",
198             "450"};
199         yaz_log(YLOG_LOG, "---------------------2 ---------------" );
200         YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
201     }
202 #endif
203 #if 0
204     /* bug 4592 */
205     {
206         char *cs[] = {
207             "4499",
208             "45", /* missing entry ! */
209             "450",
210             "4500"};
211         yaz_log(YLOG_LOG, "---------------------3 ---------------" );
212         YAZ_CHECK_EQ(do_scan(dict, 4, 0, "4501", cs, 0, 0, 4), 0);
213     }
214 #endif
215 #if 1
216     for (i = 0; i < 20; i++)
217         YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
218 #endif
219 }
220
221 int main(int argc, char **argv)
222 {
223     BFiles bfs = 0;
224     Dict dict = 0;
225     int i;
226     int ret;
227     int before = 0, after = 0, number = 10000;
228     char scan_term[1024];
229     char *arg;
230
231     YAZ_CHECK_INIT(argc, argv);
232
233     strcpy(scan_term, "1004");
234     while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
235     {
236         switch(ret)
237         {
238         case 0:
239             break;
240         case 'b':
241             before = atoi(arg);
242             break;
243         case 'a':
244             after = atoi(arg);
245             break;
246         case 't':
247             if (strlen(arg) >= sizeof(scan_term)-1)
248             {
249                 fprintf(stderr, "scan term too long\n");
250                 exit(1);
251             }
252             strcpy(scan_term, arg);
253             break;
254         case 'n':
255             number = atoi(arg);
256             break;
257         case 'v':
258             yaz_log_init_level(yaz_log_mask_str(arg));
259         }
260     }
261
262     bfs = bfs_create(".:100M", 0);
263     YAZ_CHECK(bfs);
264     if (bfs)
265     {
266         bf_reset(bfs);
267         dict = dict_open(bfs, "dict", 100, 1, 0, 0);
268         YAZ_CHECK(dict);
269     }
270     if (dict)
271     {
272         int start = 10;
273         /* Insert "10", "11", "12", .. "99", "100", ... number */
274         for (i = start; i<number; i++)
275         {
276             char w[32];
277             sprintf(w, "%d", i);
278             YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
279         }
280
281         if (after > 0 || before > 0)
282             do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
283         else
284             tst(dict, start, number);
285
286         dict_close(dict);
287     }
288     if (bfs)
289         bfs_destroy(bfs);
290     YAZ_CHECK_TERM;
291 }
292 /*
293  * Local variables:
294  * c-basic-offset: 4
295  * c-file-style: "Stroustrup"
296  * indent-tabs-mode: nil
297  * End:
298  * vim: shiftwidth=4 tabstop=8 expandtab
299  */
300