1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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
25 #include <yaz/options.h>
26 #include <idzebra/dict.h>
36 static int handler(char *name, const char *info, int pos, void *client)
38 struct handle_info *hi = (struct handle_info *) client;
41 idx = hi->a - pos + hi->b;
45 yaz_log(YLOG_DEBUG, "pos=%d idx=%d name=%s", pos, idx, name);
48 if (idx < hi->start_cut || idx >= hi->end_cut)
53 hi->ar[idx] = malloc(strlen(name)+1);
54 strcpy(hi->ar[idx], name);
59 int do_scan(Dict dict, int before, int after, const char *sterm,
61 int verbose, int start_cut, int end_cut)
63 struct handle_info hi;
69 strcpy(scan_term, sterm);
70 hi.start_cut = start_cut;
74 hi.ar = malloc(sizeof(char*) * (after+before+1));
75 for (i = 0; i<after+before; i++)
77 yaz_log(YLOG_DEBUG, "dict_scan before=%d after=%d term=%s",
78 before, after, scan_term);
79 dict_scan (dict, scan_term, &before, &after, &hi, handler);
80 for (i = 0; i < hi.a+hi.b; i++)
84 if (i >= start_cut && i < end_cut)
88 printf ("--> FAIL i=%d hi.ar[i] == NULL\n", i);
96 printf ("--> FAIL i=%d hi.ar[i] = %s\n", i, hi.ar[i]);
103 if (i >= start_cut && i < end_cut)
107 printf ("--> FAIL i=%d strs == NULL\n", i);
110 else if (!cmp_strs[i])
112 printf ("--> FAIL i=%d cmp_strs == NULL\n", i);
115 else if (strcmp(cmp_strs[i], hi.ar[i]))
117 printf ("--> FAIL i=%d expected %s\n", i, cmp_strs[i]);
125 printf ("--> FAIL i=%d hi.ar[i] != NULL\n", i);
130 if (verbose || errors)
137 printf ("%s", hi.ar[i]);
149 static void tst(Dict dict, int start, int number)
153 /* insert again with original value again */
154 for (i = start; i < number; i += 100)
159 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2);
161 /* insert again with different value */
162 for (i = start; i < number; i += 100)
167 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
169 /* insert again with original value again */
170 for (i = start; i < number; i += 100)
175 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
184 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
192 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
195 for (i = 0; i < 20; i++)
196 YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
199 int main(int argc, char **argv)
205 int before = 0, after = 0, number = 10000;
206 char scan_term[1024];
209 YAZ_CHECK_INIT(argc, argv);
211 strcpy(scan_term, "1004");
212 while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
225 if (strlen(arg) >= sizeof(scan_term)-1)
227 fprintf(stderr, "scan term too long\n");
230 strcpy(scan_term, arg);
236 yaz_log_init_level(yaz_log_mask_str(arg));
240 bfs = bfs_create(".:100M", 0);
245 dict = dict_open(bfs, "dict", 10, 1, 0, 0);
251 /* Insert "10", "11", "12", .. "99", "100", ... number */
252 for (i = start; i<number; i++)
256 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
259 if (after > 0 || before > 0)
260 do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
262 tst(dict, start, number);
273 * indent-tabs-mode: nil
275 * vim: shiftwidth=4 tabstop=8 expandtab