Make internal lookup ec function static. Reformat a little
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* $Id: scantest.c,v 1.7 2006-08-29 12:31:12 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 #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     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     yaz_log(YLOG_DEBUG, "pos=%d idx=%d name=%s", pos, idx, name);
47     if (idx < 0)
48         return 0;
49
50     hi->ar[idx] = malloc(strlen(name)+1);
51     strcpy(hi->ar[idx], name);
52     return 0;
53 }
54
55 int do_scan(Dict dict, int before, int after, char *scan_term, char **cmp_strs,
56             int verbose)
57 {
58     struct handle_info hi;
59     int i;
60     int errors = 0;
61     hi.a = after;
62     hi.b = before;
63     hi.ar = malloc(sizeof(char*) * (after+before+1));
64     for (i = 0; i<after+before; i++)
65         hi.ar[i] = 0;
66     yaz_log(YLOG_DEBUG, "dict_scan before=%d after=%d term=%s",
67             before, after, scan_term);
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 static void tst(Dict dict)
109 {
110     char scan_term[1024];
111     {
112         char *cs[] = {
113             "4497",
114             "4498",
115             "4499",
116             "45"};
117         strcpy(scan_term, "4499");
118         YAZ_CHECK_EQ(do_scan(dict, 2, 2, scan_term, cs, 0), 0);
119     }
120     {
121         char *cs[] = {
122             "4498",
123             "4499",
124             "45",
125             "450"};
126         strcpy(scan_term, "45");
127         YAZ_CHECK_EQ(do_scan(dict, 2, 2, scan_term, cs, 0), 0);
128     }
129 }
130
131 int main(int argc, char **argv)
132 {
133     BFiles bfs = 0;
134     Dict dict = 0;
135     int i;
136     int errors = 0;
137     int ret;
138     int before = 0, after = 0, number = 10000;
139     char scan_term[1024];
140     char *arg;
141
142     YAZ_CHECK_INIT(argc, argv);
143
144     strcpy(scan_term, "1004");
145     while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
146     {
147         switch(ret)
148         {
149         case 0:
150             break;
151         case 'b':
152             before = atoi(arg);
153             break;
154         case 'a':
155             after = atoi(arg);
156             break;
157         case 't':
158             if (strlen(arg) >= sizeof(scan_term)-1)
159             {
160                 fprintf(stderr, "scan term too long\n");
161                 exit(1);
162             }
163             strcpy(scan_term, arg);
164             break;
165         case 'n':
166             number = atoi(arg);
167             break;
168         case 'v':
169             yaz_log_init_level(yaz_log_mask_str(arg));
170         }
171     }
172
173     bfs = bfs_create(".:100M", 0);
174     YAZ_CHECK(bfs);
175     if (bfs)
176     {
177         bf_reset(bfs);
178         dict = dict_open(bfs, "dict", 10, 1, 0, 0);
179         YAZ_CHECK(dict);
180     }
181     if (dict)
182     {
183         /* Insert "10", "11", "12", .. "99", "100", ... number */
184         for (i = 10; i<number; i++)
185         {
186             char w[32];
187             sprintf(w, "%d", i);
188             YAZ_CHECK_EQ(dict_insert (dict, w, sizeof(int), &i), 0);
189         }
190         
191         if (after > 0 || before > 0)
192             do_scan(dict, before, after, scan_term, 0, 1);
193         else
194             tst(dict);
195
196         dict_close(dict);
197     }
198     if (bfs)
199         bfs_destroy(bfs);
200     YAZ_CHECK_TERM;
201 }
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * indent-tabs-mode: nil
206  * End:
207  * vim: shiftwidth=4 tabstop=8 expandtab
208  */
209