Loosen test a bit
[idzebra-moved-to-github.git] / isamb / tstisamb.c
1 /* $Id: tstisamb.c,v 1.4 2004-06-02 07:51:52 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <string.h>
24 #include <yaz/xmalloc.h>
25 #include <yaz/log.h>
26 #include <isamb.h>
27 #include <assert.h>
28
29 int compare_item(const void *a, const void *b)
30 {
31     int ia, ib;
32
33     memcpy(&ia, a, sizeof(int));
34     memcpy(&ib, b, sizeof(int));
35     return ia - ib;
36 }
37
38 void *code_start(int mode)
39 {
40     return 0;
41 }
42
43 void code_item(int mode, void *p, char **dst, char **src)
44 {
45     memcpy (*dst, *src, sizeof(int));
46     (*dst) += sizeof(int);
47     (*src) += sizeof(int);
48 }
49
50 void code_reset(void *p)
51 {
52 }
53 void code_stop(int mode, void *p)
54 {
55 }
56
57 struct read_info {
58     int no;
59     int max;
60 };
61
62 int code_read(void *vp, char **dst, int *insertMode)
63 {
64     struct read_info *ri = (struct read_info *)vp;
65     int x;
66
67     if (ri->no > ri->max)
68         exit(3);
69     if (ri->no == ri->max)
70         return 0;
71     x = ri->no;
72     memcpy (*dst, &x, sizeof(int));
73     (*dst)+=sizeof(int);
74
75     (ri->no)++;
76     *insertMode = 1;
77     return 1;
78 }
79
80 void tst_forward(ISAMB isb, int n)
81 {
82     ISAMC_I isamc_i;
83     ISAMC_P isamc_p;
84     struct read_info ri;
85     int i;
86     ISAMB_PP pp;
87     char key_buf[10];
88
89     /* insert a number of entries */
90     ri.no = 0;
91     ri.max = n;
92
93     isamc_i.clientData = &ri;
94     isamc_i.read_item = code_read;
95     
96     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
97
98     /* read the entries */
99     pp = isamb_pp_open (isb, isamc_p);
100     
101     for (i = 0; i<ri.max; i++)
102     {
103         int x = -1;
104         int xu = i;
105         isamb_pp_forward(pp, &x, &xu);
106         if (x != xu)
107         {
108             yaz_log(LOG_WARN, "isamb_pp_forward (1). Got %d (expected %d)",
109                     x, xu);
110             exit(4);
111         }
112         ri.no++;
113     }
114     isamb_pp_close(pp);
115     
116     pp = isamb_pp_open (isb, isamc_p);
117     for (i = 0; i<ri.max; i += 100)
118     {
119         int x = -1;
120         int xuntil = i;
121         isamb_pp_forward(pp, &x, &xuntil);
122         if (x > xuntil)
123         {
124             yaz_log(LOG_WARN, "isamb_pp_forward (2). Got %d (expected %d)",
125                     x, xuntil);
126             exit(4);
127         }
128         ri.no++;
129     }
130     isamb_pp_close(pp);
131
132     isamb_unlink(isb, isamc_p);
133 }
134
135 void tst_insert(ISAMB isb, int n)
136 {
137     ISAMC_I isamc_i;
138     ISAMC_P isamc_p;
139     struct read_info ri;
140     ISAMB_PP pp;
141     char key_buf[10];
142
143     /* insert a number of entries */
144     ri.no = 0;
145     ri.max = n;
146
147     isamc_i.clientData = &ri;
148     isamc_i.read_item = code_read;
149     
150     isamc_p = isamb_merge (isb, 0 /* new list */ , &isamc_i);
151
152     /* read the entries */
153     pp = isamb_pp_open (isb, isamc_p);
154     
155     ri.no = 0;
156     while(isamb_pp_read (pp, key_buf))
157     {
158         int x;
159         memcpy (&x, key_buf, sizeof(int));
160         if (x != ri.no)
161         {
162             yaz_log(LOG_WARN, "isamb_pp_read. Got %d (expected %d)",
163                     x, ri.no);
164             exit(3);
165         }
166         ri.no++;
167     }
168     if (ri.no != ri.max)
169     {
170         yaz_log(LOG_WARN, "ri.max != ri.max (%d != %d)", ri.no, ri.max);
171         exit(3);
172     }
173     isamb_pp_close(pp);
174
175     isamb_unlink(isb, isamc_p);
176 }
177
178 static void log_item(int level, const void *b, const char *txt)
179 {
180 }
181
182 int main(int argc, char **argv)
183 {
184     BFiles bfs;
185     ISAMB isb;
186     ISAMC_M method;
187     
188     if (argc == 2)
189         yaz_log_init_level(LOG_ALL);
190         
191     /* setup method (attributes) */
192     method.compare_item = compare_item;
193     method.log_item = log_item;
194     method.code_start = code_start;
195     method.code_item = code_item;
196     method.code_reset = code_reset;
197     method.code_stop = code_stop;
198
199     /* create block system */
200     bfs = bfs_create(0, 0);
201     if (!bfs)
202     {
203         yaz_log(LOG_WARN, "bfs_create failed");
204         exit(1);
205     }
206
207     bf_reset(bfs);
208
209     /* create isam handle */
210     isb = isamb_open (bfs, "isamb", 1, &method, 0);
211     if (!isb)
212     {
213         yaz_log(LOG_WARN, "isamb_open failed");
214         exit(2);
215     }
216     tst_insert(isb, 1);
217     tst_insert(isb, 2);
218     tst_insert(isb, 20);
219     tst_insert(isb, 100);
220     tst_insert(isb, 500);
221     tst_insert(isb, 10000);
222     tst_forward(isb, 10000);
223     /* close isam handle */
224     isamb_close(isb);
225
226     /* exit block system */
227     bfs_destroy(bfs);
228     exit(0);
229     return 0;
230 }