Test of proper return value of dict_insert
[idzebra-moved-to-github.git] / dict / scantest.c
1 /* $Id: scantest.c,v 1.8 2006-08-29 13:39:48 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, int start, int number)
109 {
110     int i;
111     char scan_term[1024];
112
113     /* insert again with original value again */
114     for (i = start; i < number; i += 100)
115     {
116         int v = i;
117         char w[32];
118         sprintf(w, "%d", i);
119         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2);
120     }
121     /* insert again with differnt value */
122     for (i = start; i < number; i += 100)
123     {
124         int v = i-1;
125         char w[32];
126         sprintf(w, "%d", i);
127         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
128     }
129     /* insert again with original value again */
130     for (i = start; i < number; i += 100)
131     {
132         int v = i;
133         char w[32];
134         sprintf(w, "%d", i);
135         YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
136     }
137
138     {
139         char *cs[] = {
140             "4497",
141             "4498",
142             "4499",
143             "45"};
144         strcpy(scan_term, "4499");
145         YAZ_CHECK_EQ(do_scan(dict, 2, 2, scan_term, cs, 0), 0);
146     }
147     {
148         char *cs[] = {
149             "4498",
150             "4499",
151             "45",
152             "450"};
153         strcpy(scan_term, "45");
154         YAZ_CHECK_EQ(do_scan(dict, 2, 2, scan_term, cs, 0), 0);
155     }
156     
157 }
158
159 int main(int argc, char **argv)
160 {
161     BFiles bfs = 0;
162     Dict dict = 0;
163     int i;
164     int ret;
165     int before = 0, after = 0, number = 10000;
166     char scan_term[1024];
167     char *arg;
168
169     YAZ_CHECK_INIT(argc, argv);
170
171     strcpy(scan_term, "1004");
172     while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
173     {
174         switch(ret)
175         {
176         case 0:
177             break;
178         case 'b':
179             before = atoi(arg);
180             break;
181         case 'a':
182             after = atoi(arg);
183             break;
184         case 't':
185             if (strlen(arg) >= sizeof(scan_term)-1)
186             {
187                 fprintf(stderr, "scan term too long\n");
188                 exit(1);
189             }
190             strcpy(scan_term, arg);
191             break;
192         case 'n':
193             number = atoi(arg);
194             break;
195         case 'v':
196             yaz_log_init_level(yaz_log_mask_str(arg));
197         }
198     }
199
200     bfs = bfs_create(".:100M", 0);
201     YAZ_CHECK(bfs);
202     if (bfs)
203     {
204         bf_reset(bfs);
205         dict = dict_open(bfs, "dict", 10, 1, 0, 0);
206         YAZ_CHECK(dict);
207     }
208     if (dict)
209     {
210         int start = 10;
211         /* Insert "10", "11", "12", .. "99", "100", ... number */
212         for (i = start; i<number; i++)
213         {
214             char w[32];
215             sprintf(w, "%d", i);
216             YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
217         }
218
219         if (after > 0 || before > 0)
220             do_scan(dict, before, after, scan_term, 0, 1);
221         else
222             tst(dict, start, number);
223
224         dict_close(dict);
225     }
226     if (bfs)
227         bfs_destroy(bfs);
228     YAZ_CHECK_TERM;
229 }
230 /*
231  * Local variables:
232  * c-basic-offset: 4
233  * indent-tabs-mode: nil
234  * End:
235  * vim: shiftwidth=4 tabstop=8 expandtab
236  */
237