Bump year. Change Aps->ApS
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* $Id: scantest.c,v 1.3 2005-01-15 19:38:24 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <yaz/options.h>
28 #include <idzebra/dict.h>
29
30 struct handle_info {
31     int b;
32     int a;
33     char **ar;
34 };
35
36 static int handler(char *name, const char *info, int pos, void *client)
37 {
38     struct handle_info *hi = (struct handle_info *) client;
39     int idx;
40     if (pos > 0)
41         idx = hi->a - pos + hi->b;
42     else
43         idx = -pos - 1;
44     hi->ar[idx] = malloc(strlen(name)+1);
45     strcpy(hi->ar[idx], name);
46 #if 0
47     printf ("pos=%d idx=%d name=%s\n", pos, idx, name);
48 #endif
49     return 0;
50 }
51
52 int tst(Dict dict, int before, int after, char *scan_term, char **cmp_strs,
53         int verbose)
54 {
55     struct handle_info hi;
56     int i;
57     int errors = 0;
58     hi.a = after;
59     hi.b = before;
60     hi.ar = malloc(sizeof(char*) * (after+before+1));
61     for (i = 0; i<after+before; i++)
62         hi.ar[i] = 0;
63     dict_scan (dict, scan_term, &before, &after, &hi, handler);
64     for (i = 0; i<hi.a+hi.b; i++)
65     {
66         if (cmp_strs)
67         {
68             if (!cmp_strs[i])
69             {
70                 printf ("--> FAIL cmp_strs == NULL\n");
71                 errors++;
72             }
73             else if (!hi.ar[i])
74             {
75                 printf ("--> FAIL strs == NULL\n");
76                 errors++;
77             }
78             else if (strcmp(cmp_strs[i], hi.ar[i]))
79             {
80                 printf ("--> FAIL expected %s\n", cmp_strs[i]);
81                 errors++;
82             }
83         }
84         if (verbose || errors)
85         {
86             if (i == hi.b)
87                 printf ("* ");
88             else
89                 printf ("  ");
90             if (hi.ar[i])
91                 printf ("%s", hi.ar[i]);
92             else
93                 printf ("NULL");
94             putchar('\n');
95         }
96         if (hi.ar[i])
97             free(hi.ar[i]);
98     }
99     free(hi.ar);
100     return errors;
101 }
102
103 int main(int argc, char **argv)
104 {
105     BFiles bfs;
106     Dict dict;
107     int i;
108     int errors = 0;
109     int ret;
110     int before = 0, after = 0, number = 10000;
111     char scan_term[1024];
112     char *arg;
113
114     strcpy(scan_term, "1004");
115     while ((ret = options("b:a:t:n:", argv, argc, &arg)) != -2)
116     {
117         switch(ret)
118         {
119         case 0:
120             break;
121         case 'b':
122             before = atoi(arg);
123             break;
124         case 'a':
125             after = atoi(arg);
126             break;
127         case 't':
128             strcpy(scan_term, arg);
129             break;
130         case 'n':
131             number = atoi(arg);
132             break;
133         }
134     }
135
136     bfs = bfs_create(".:100M", 0);
137     if (!bfs)
138     {
139         fprintf(stderr, "bfs_create failed\n");
140         exit(1);
141     }
142     dict = dict_open(bfs, "dict", 10, 1, 0, 0);
143     for (i = 10; i<number; i++)
144     {
145         int r;
146         char w[32];
147         sprintf(w, "%d", i);
148         r = dict_insert (dict, w, sizeof(int), &i);
149     }
150
151     if (after > 0 || before > 0)
152         tst(dict, before, after, scan_term, 0, 1);
153     else
154     {
155         if (argc <= 1)
156         {
157             char *cs[] = {
158                 "4497",
159                 "4498",
160                 "4499",
161             "45"};
162             strcpy(scan_term, "4499");
163             errors += tst(dict, 2, 2, scan_term, cs, 0);
164         }
165         if (argc <= 1)
166         {
167             char *cs[] = {
168                 "4498",
169                 "4499",
170                 "45",
171                 "450"};
172             strcpy(scan_term, "45");
173             errors += tst(dict, 2, 2, scan_term, cs, 0);
174         }
175     }
176     dict_close(dict);
177     bfs_destroy(bfs);
178     if (errors)
179         exit(1);
180     exit(0);
181 }