1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
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
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
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
28 #include <yaz/options.h>
29 #include <idzebra/dict.h>
39 static int handler(char *name, const char *info, int pos, void *client)
41 struct handle_info *hi = (struct handle_info *) client;
44 idx = hi->a - pos + hi->b;
50 if (idx < hi->start_cut || idx > hi->end_cut)
55 hi->ar[idx] = malloc(strlen(name)+1);
56 strcpy(hi->ar[idx], name);
61 int do_scan(Dict dict, int before, int after, const char *sterm,
63 int verbose, int start_cut, int end_cut)
65 struct handle_info hi;
71 strcpy(scan_term, sterm);
72 hi.start_cut = start_cut;
76 hi.ar = malloc(sizeof(char*) * (after+before+1));
77 for (i = 0; i <= after+before; i++)
79 yaz_log(YLOG_DEBUG, "dict_scan before=%d after=%d term=%s",
80 before, after, scan_term);
81 dict_scan (dict, scan_term, &before, &after, &hi, handler);
82 for (i = 0; i < hi.a+hi.b; i++)
86 if (i >= start_cut && i <= end_cut)
90 printf ("--> FAIL i=%d hi.ar[i] == NULL\n", i);
98 printf ("--> FAIL i=%d hi.ar[i] = %s\n", i, hi.ar[i]);
105 if (i >= start_cut && i <= end_cut)
109 printf ("--> FAIL i=%d strs == NULL\n", i);
112 else if (!cmp_strs[i])
114 printf ("--> FAIL i=%d cmp_strs == NULL\n", i);
117 else if (strcmp(cmp_strs[i], hi.ar[i]))
119 printf ("--> FAIL i=%d expected %s\n", i, cmp_strs[i]);
127 printf ("--> FAIL i=%d hi.ar[i] != NULL\n", i);
132 if (verbose || errors)
139 printf ("%s", hi.ar[i]);
151 static void tst(Dict dict, int start, int number)
155 /* insert again with original value again */
156 for (i = start; i < number; i += 100)
161 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2);
163 /* insert again with different value */
164 for (i = start; i < number; i += 100)
169 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
171 /* insert again with original value again */
172 for (i = start; i < number; i += 100)
177 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
187 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
197 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
205 "45", /* missing entry ! */
208 YAZ_CHECK_EQ(do_scan(dict, 4, 0, "4501", cs, 0, 0, 4), 0);
218 YAZ_CHECK_EQ(do_scan(dict, 4, 0, "a", cs, 0, 0, 4), 0);
219 YAZ_CHECK_EQ(do_scan(dict, 3, 1, "9999", cs, 0, 0, 4), 0);
229 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "10", cs, 0, 0, 4), 0);
230 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "1", cs, 0, 0, 4), 0);
231 YAZ_CHECK_EQ(do_scan(dict, 0, 4, " ", cs, 0, 0, 4), 0);
232 YAZ_CHECK_EQ(do_scan(dict, 0, 4, "", cs, 0, 0, 4), 0);
236 for (i = 0; i < 20; i++)
237 YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
241 int main(int argc, char **argv)
247 int before = 0, after = 0, number = 10000;
248 char scan_term[1024];
251 YAZ_CHECK_INIT(argc, argv);
253 strcpy(scan_term, "1004");
254 while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
267 if (strlen(arg) >= sizeof(scan_term)-1)
269 fprintf(stderr, "scan term too long\n");
272 strcpy(scan_term, arg);
278 yaz_log_init_level(yaz_log_mask_str(arg));
282 bfs = bfs_create(".:100M", 0);
287 dict = dict_open(bfs, "dict", 100, 1, 0, 0);
293 /* Insert "10", "11", "12", .. "99", "100", ... number */
294 for (i = start; i<number; i++)
298 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
301 if (after > 0 || before > 0)
302 do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
304 tst(dict, start, number);
315 * c-file-style: "Stroustrup"
316 * indent-tabs-mode: nil
318 * vim: shiftwidth=4 tabstop=8 expandtab