IDZEBRA 2.2.8
scantest.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, 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 <yaz/snprintf.h>
30#include <idzebra/dict.h>
31
33 int b;
34 int a;
37 char **ar;
38};
39
40static int handler(char *name, const char *info, int pos, void *client)
41{
42 struct handle_info *hi = (struct handle_info *) client;
43 int idx;
44 if (pos > 0)
45 idx = hi->a - pos + hi->b;
46 else
47 idx = -pos - 1;
48
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
62int 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);
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
152static 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 yaz_snprintf(w, sizeof(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 yaz_snprintf(w, sizeof(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 yaz_snprintf(w, sizeof(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_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
189 }
190#endif
191#if 1
192 {
193 char *cs[] = {
194 "4498",
195 "4499",
196 "45",
197 "450"};
198 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
199 }
200#endif
201#if 1
202 /* bug 4592 */
203 {
204 char *cs[] = {
205 "4499",
206 "45", /* missing entry ! */
207 "450",
208 "4500"};
209 YAZ_CHECK_EQ(do_scan(dict, 4, 0, "4501", cs, 0, 0, 4), 0);
210 }
211#endif
212#if 1
213 {
214 char *cs[] = {
215 "9996",
216 "9997",
217 "9998",
218 "9999"};
219 YAZ_CHECK_EQ(do_scan(dict, 4, 0, "a", cs, 0, 0, 4), 0);
220 YAZ_CHECK_EQ(do_scan(dict, 3, 1, "9999", cs, 0, 0, 4), 0);
221 }
222#endif
223#if 1
224 {
225 char *cs[] = {
226 "10",
227 "100",
228 "1000",
229 "1001" };
230 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "10", cs, 0, 0, 4), 0);
231 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "1", cs, 0, 0, 4), 0);
232 YAZ_CHECK_EQ(do_scan(dict, 0, 4, " ", cs, 0, 0, 4), 0);
233 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "", cs, 0, 0, 4), 0);
234 }
235#endif
236#if 1
237 for (i = 0; i < 20; i++)
238 YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
239#endif
240}
241
242int main(int argc, char **argv)
243{
244 BFiles bfs = 0;
245 Dict dict = 0;
246 int i;
247 int ret;
248 int before = 0, after = 0, number = 10000;
249 char scan_term[1024];
250 char *arg;
251
252 YAZ_CHECK_INIT(argc, argv);
253
254 strcpy(scan_term, "1004");
255 while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
256 {
257 switch(ret)
258 {
259 case 0:
260 break;
261 case 'b':
262 before = atoi(arg);
263 break;
264 case 'a':
265 after = atoi(arg);
266 break;
267 case 't':
268 if (strlen(arg) >= sizeof(scan_term)-1)
269 {
270 fprintf(stderr, "scan term too long\n");
271 exit(1);
272 }
273 strcpy(scan_term, arg);
274 break;
275 case 'n':
276 number = atoi(arg);
277 break;
278 case 'v':
279 yaz_log_init_level(yaz_log_mask_str(arg));
280 }
281 }
282
283 bfs = bfs_create(".:100M", 0);
284 YAZ_CHECK(bfs);
285 if (bfs)
286 {
287 bf_reset(bfs);
288 dict = dict_open(bfs, "scantest", 100, 1, 0, 0);
289 YAZ_CHECK(dict);
290 }
291 if (dict)
292 {
293 int start = 10;
294 /* Insert "10", "11", "12", .. "99", "100", ... number */
295 for (i = start; i<number; i++)
296 {
297 char w[32];
298 yaz_snprintf(w, sizeof(w), "%d", i);
299 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
300 }
301
302 if (after > 0 || before > 0)
303 do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
304 else
305 tst(dict, start, number);
306
308 }
309 if (bfs)
310 bfs_destroy(bfs);
311 YAZ_CHECK_TERM;
312}
313/*
314 * Local variables:
315 * c-basic-offset: 4
316 * c-file-style: "Stroustrup"
317 * indent-tabs-mode: nil
318 * End:
319 * vim: shiftwidth=4 tabstop=8 expandtab
320 */
321
void bf_reset(BFiles bfs)
Removes register and shadow completely.
Definition bfile.c:268
BFiles bfs_create(const char *spec, const char *base)
creates a Block files collection
Definition bfile.c:56
void bfs_destroy(BFiles bfiles)
destroys a block files handle
Definition bfile.c:73
Zebra dictionary.
Dict dict_open(BFiles bfs, const char *name, int cache, int rw, int compact_flag, int page_size)
open dictionary
Definition open.c:50
int dict_insert(Dict dict, const char *p, int userlen, void *userinfo)
insert item into dictionary
Definition insert.c:439
int dict_scan(Dict dict, char *str, int *before, int *after, void *client, int(*f)(char *name, const char *info, int pos, void *client))
dictionary scan
Definition scan.c:242
int dict_close(Dict dict)
closes dictionary
Definition close.c:32
static Dict dict
Definition dicttest.c:35
static int handler(char *name, const char *info, int pos, void *client)
Definition scantest.c:40
int main(int argc, char **argv)
Definition scantest.c:242
int do_scan(Dict dict, int before, int after, const char *sterm, char **cmp_strs, int verbose, int start_cut, int end_cut)
Definition scantest.c:62
int end_cut
Definition scantest.c:36
char ** ar
Definition scantest.c:37
int start_cut
Definition scantest.c:35
static void tst(void)
Definition tstflock.c:197