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;
48 yaz_log(YLOG_LOG, "scan_handler name=%s pos=%d idx=%d", name, pos, idx);
51 if (idx < hi->start_cut || idx > hi->end_cut)
56 hi->ar[idx] = malloc(strlen(name)+1);
57 strcpy(hi->ar[idx], name);
62 int do_scan(Dict dict, int before, int after, const char *sterm,
64 int verbose, int start_cut, int end_cut)
66 struct handle_info hi;
72 strcpy(scan_term, sterm);
73 hi.start_cut = start_cut;
77 hi.ar = malloc(sizeof(char*) * (after+before+1));
78 for (i = 0; i <= after+before; i++)
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++)
87 if (i >= start_cut && i <= end_cut)
91 printf ("--> FAIL i=%d hi.ar[i] == NULL\n", i);
99 printf ("--> FAIL i=%d hi.ar[i] = %s\n", i, hi.ar[i]);
106 if (i >= start_cut && i <= end_cut)
110 printf ("--> FAIL i=%d strs == NULL\n", i);
113 else if (!cmp_strs[i])
115 printf ("--> FAIL i=%d cmp_strs == NULL\n", i);
118 else if (strcmp(cmp_strs[i], hi.ar[i]))
120 printf ("--> FAIL i=%d expected %s\n", i, cmp_strs[i]);
128 printf ("--> FAIL i=%d hi.ar[i] != NULL\n", i);
133 if (verbose || errors)
140 printf ("%s", hi.ar[i]);
152 static void tst(Dict dict, int start, int number)
156 /* insert again with original value again */
157 for (i = start; i < number; i += 100)
162 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2);
164 /* insert again with different value */
165 for (i = start; i < number; i += 100)
170 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
172 /* insert again with original value again */
173 for (i = start; i < number; i += 100)
178 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1);
188 yaz_log(YLOG_LOG, "---------------------1 ---------------" );
189 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0);
199 yaz_log(YLOG_LOG, "---------------------2 ---------------" );
200 YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0);
208 "45", /* missing entry ! */
211 yaz_log(YLOG_LOG, "---------------------3 ---------------" );
212 YAZ_CHECK_EQ(do_scan(dict, 4, 0, "4501", cs, 0, 0, 4), 0);
216 for (i = 0; i < 20; i++)
217 YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0);
221 int main(int argc, char **argv)
227 int before = 0, after = 0, number = 10000;
228 char scan_term[1024];
231 YAZ_CHECK_INIT(argc, argv);
233 strcpy(scan_term, "1004");
234 while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2)
247 if (strlen(arg) >= sizeof(scan_term)-1)
249 fprintf(stderr, "scan term too long\n");
252 strcpy(scan_term, arg);
258 yaz_log_init_level(yaz_log_mask_str(arg));
262 bfs = bfs_create(".:100M", 0);
267 dict = dict_open(bfs, "dict", 100, 1, 0, 0);
273 /* Insert "10", "11", "12", .. "99", "100", ... number */
274 for (i = start; i<number; i++)
278 YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(int), &i), 0);
281 if (after > 0 || before > 0)
282 do_scan(dict, before, after, scan_term, 0, 1, 0, after+1);
284 tst(dict, start, number);
295 * c-file-style: "Stroustrup"
296 * indent-tabs-mode: nil
298 * vim: shiftwidth=4 tabstop=8 expandtab