1 /* $Id: tstisamb.c,v 1.25 2006-10-30 16:21:29 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
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
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
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
26 #include <yaz/xmalloc.h>
27 #include <idzebra/isamb.h>
30 static void log_item(int level, const void *b, const char *txt)
33 memcpy(&x, b, sizeof(int));
34 yaz_log(YLOG_DEBUG, "%s %d", txt, x);
37 static void log_pr(const char *txt)
39 yaz_log(YLOG_DEBUG, "%s", txt);
42 int compare_item(const void *a, const void *b)
46 memcpy(&ia, a, sizeof(int));
47 memcpy(&ib, b, sizeof(int));
55 void *code_start(void)
60 void code_item(void *p, char **dst, const char **src)
62 memcpy (*dst, *src, sizeof(int));
63 (*dst) += sizeof(int);
64 (*src) += sizeof(int);
67 void code_reset(void *p)
70 void code_stop(void *p)
83 int code_read(void *vp, char **dst, int *insertMode)
85 struct read_info *ri = (struct read_info *)vp;
88 if (ri->no >= ri->max)
93 memcpy (*dst, &x, sizeof(int));
96 ri->val = ri->val + ri->step;
97 *insertMode = ri->insertMode;
100 yaz_log(YLOG_DEBUG, "%d %5d", ri->insertMode, x);
105 void tst_insert(ISAMB isb, int n)
114 /* insert a number of entries */
122 isamc_i.clientData = &ri;
123 isamc_i.read_item = code_read;
125 isamc_p = 0; /* new list */
126 isamb_merge (isb, &isamc_p , &isamc_i);
128 /* read the entries */
129 pp = isamb_pp_open (isb, isamc_p, 1);
132 while(isamb_pp_read (pp, key_buf))
135 memcpy (&x, key_buf, sizeof(int));
138 yaz_log(YLOG_WARN, "isamb_pp_read. n=%d Got %d (expected %d)",
143 yaz_log(YLOG_LOG, "isamb_pp_read. n=%d Got %d",
148 if (ri.val != ri.max)
150 yaz_log(YLOG_WARN, "ri.max != ri.max (%d != %d)", ri.val, ri.max);
153 isamb_dump(isb, isamc_p, log_pr);
158 /* delete a number of entries (even ones) */
166 isamc_i.clientData = &ri;
167 isamc_i.read_item = code_read;
169 isamb_merge (isb, &isamc_p , &isamc_i);
171 /* delete a number of entries (odd ones) */
179 isamc_i.clientData = &ri;
180 isamc_i.read_item = code_read;
182 isamb_merge (isb, &isamc_p, &isamc_i);
186 yaz_log(YLOG_WARN, "isamb_merge did not return empty list n=%d",
192 void tst_forward(ISAMB isb, int n)
200 /* insert a number of entries */
208 isamc_i.clientData = &ri;
209 isamc_i.read_item = code_read;
212 isamb_merge (isb, &isamc_p, &isamc_i);
214 /* read the entries */
215 pp = isamb_pp_open (isb, isamc_p, 1);
217 for (i = 0; i<ri.max; i +=2 )
221 isamb_pp_forward(pp, &x, &xu);
222 if (x != xu && xu != x+1)
224 yaz_log(YLOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
232 pp = isamb_pp_open (isb, isamc_p, 1);
233 for (i = 0; i<ri.max; i += 100)
237 isamb_pp_forward(pp, &x, &xu);
238 if (x != xu && xu != x+1)
240 yaz_log(YLOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
248 isamb_unlink(isb, isamc_p);
251 void tst_x(ISAMB isb)
257 isamc_i.clientData = &ri;
258 isamc_i.read_item = code_read;
266 isamb_merge (isb, &isamb_p , &isamc_i);
275 isamb_merge (isb, &isamb_p , &isamc_i);
278 void tst_append(ISAMB isb, int n)
286 for (i = 0; i < n; i += chunk)
288 /* insert a number of entries */
296 isamc_i.clientData = &ri;
297 isamc_i.read_item = code_read;
299 isamb_merge (isb, &isamb_p , &isamc_i);
304 struct random_read_info {
311 int tst_random_read(void *vp, char **dst, int *insertMode)
313 struct random_read_info *ri = (struct random_read_info *)vp;
316 while(ri->idx < ri->max && ri->delta[ri->idx] == ri->level)
321 if (ri->idx >= ri->max)
324 if (ri->delta[ri->idx] > 0)
335 memcpy (*dst, &x, sizeof(int));
338 yaz_log(YLOG_DEBUG, "%d %5d", *insertMode, x);
342 void tst_random(ISAMB isb, int n, int rounds, int max_dups)
346 int *freq = malloc(sizeof(int) * n);
347 int *delta = malloc(sizeof(int) * n);
349 for (i = 0; i<n; i++)
352 for (j = 0; j<rounds; j++)
354 yaz_log(YLOG_DEBUG, "round %d", j);
355 for (i = 0; i<n; i++)
358 delta[i] = (rand() % (1+max_dups)) - freq[i];
365 struct random_read_info ri;
372 isamc_i.clientData = &ri;
373 isamc_i.read_item = tst_random_read;
375 isamb_merge (isb, &isamb_p , &isamc_i);
378 yaz_log(YLOG_DEBUG, "dump %d", j);
379 isamb_dump(isb, isamb_p, log_pr);
381 yaz_log(YLOG_DEBUG, "----------------------------");
382 for (i = 0; i<n; i++)
387 for (i = 0; i<n; i++)
390 yaz_log(YLOG_WARN, "isamb_merge returned 0, but "
391 "freq is non-empty");
400 ISAMB_PP pp = isamb_pp_open (isb, isamb_p, 1);
402 yaz_log(YLOG_DEBUG, "test %d", j);
404 while(isamb_pp_read (pp, key_buf))
407 memcpy (&x, key_buf, sizeof(int));
408 yaz_log(YLOG_DEBUG, "Got %d", x);
409 while (idx < n && freq[idx] == level)
416 yaz_log(YLOG_WARN, "tst_random: Extra item: %d", x);
421 yaz_log(YLOG_WARN, "tst_random: Mismatch %d != %d",
427 while (idx < n && freq[idx] == level)
434 yaz_log(YLOG_WARN, "tst_random: Missing item: %d", idx);
444 /* \fn void tst_minsert(ISAMB isb, int n)
445 \brief insert inserts n identical keys, removes n/2, then n-n/2 ..
446 \param isb ISAMB handle
447 \param n number of keys
449 void tst_minsert(ISAMB isb, int n)
455 isamc_i.clientData = &ri;
457 /* all have same value = 1 */
461 isamc_i.read_item = code_read;
468 isamb_merge (isb, &isamb_p , &isamc_i);
470 isamb_dump(isb, isamb_p, log_pr);
477 isamb_merge (isb, &isamb_p , &isamc_i);
484 isamb_merge (isb, &isamb_p , &isamc_i);
487 yaz_log(YLOG_WARN, "tst_minsert: isamb_merge should be empty n=%d",
493 /* tests for identical keys.. ISAMB does not handle that, so some of the
496 static void identical_keys_tests(ISAMB isb)
499 tst_minsert(isb, 10);
502 tst_minsert(isb, 600); /* still fails */
505 tst_random(isb, 20, 200, 1);
508 tst_random(isb, 5, 200, 2);
512 tst_random(isb, 250, 10, 4);
515 /* fails if both are executed */
516 tst_random(isb, 20000, 10, 4);
517 tst_random(isb, 20000, 10, 10);
520 tst_random(isb, 250, 100, 10);
524 int main(int argc, char **argv)
531 yaz_log_init_level(YLOG_ALL);
533 /* setup method (attributes) */
534 method.compare_item = compare_item;
535 method.log_item = log_item;
536 method.codec.start = code_start;
537 method.codec.encode = code_item;
538 method.codec.decode = code_item;
539 method.codec.reset = code_reset;
540 method.codec.stop = code_stop;
542 /* create block system */
543 bfs = bfs_create(0, 0);
546 yaz_log(YLOG_WARN, "bfs_create failed");
552 /* create isam handle */
553 isb = isamb_open (bfs, "isamb", 1, &method, 0);
556 yaz_log(YLOG_WARN, "isamb_open failed");
563 tst_insert(isb, 100);
564 tst_insert(isb, 500);
565 tst_insert(isb, 10000);
567 tst_forward(isb, 10000);
571 tst_append(isb, 1000);
575 identical_keys_tests(isb);
579 /* exit block system */
587 * indent-tabs-mode: nil
589 * vim: shiftwidth=4 tabstop=8 expandtab