Changed test to that bfs is reconstructed after bfs_reset.
[idzebra-moved-to-github.git] / bfile / tstbfile2.c
1 /* $Id: tstbfile2.c,v 1.2 2006-11-08 13:05:33 adam Exp $
2    Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <yaz/test.h>
29 #include <idzebra/bfile.h>
30
31 void tst(void)
32 {
33     int r;
34     BFiles bfs;
35     BFile bf;
36     char buf[256];
37     int block_size = 16;
38     zint max_block = 200000;
39
40     YAZ_CHECK(block_size <= sizeof(buf));
41     if (!(block_size <= sizeof(buf)))
42         return;
43
44     YAZ_CHECK(max_block * block_size < 4 * 1000000); /* 4M */
45     
46     r = mkdir("register", 0777);
47     YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
48
49     r = mkdir("shadow", 0777);
50     YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
51
52     bfs = bfs_create("register.bad:4M", 0 /* base: current dir */);
53     YAZ_CHECK(!bfs);
54     if (bfs)
55         return;
56
57     bfs = bfs_create("register:4M", 0 /* base: current dir */);
58     YAZ_CHECK(bfs);
59     if (!bfs)
60         return;
61
62     r = bf_cache(bfs, "shadow.bad:4M");
63     YAZ_CHECK_EQ(r, ZEBRA_FAIL);
64
65     r = bf_cache(bfs, "shadow:4M");
66     YAZ_CHECK_EQ(r, ZEBRA_OK);
67
68     bf_reset(bfs);
69
70 #if 1
71     /* we have to destroy bfs after reset. Unfortunately! */
72     bfs_destroy(bfs);
73
74     bfs = bfs_create("register:4M", 0 /* base: current dir */);
75     YAZ_CHECK(bfs);
76     if (!bfs)
77         return;
78
79     r = bf_cache(bfs, "shadow:4M");
80     YAZ_CHECK_EQ(r, ZEBRA_OK);
81 #endif
82
83     bf = bf_open(bfs, "file", block_size, 1);
84     YAZ_CHECK(bf);
85     if (bf)
86     {
87         zint bno[2];
88         memset(buf, '\0', block_size);
89
90         bno[0] = 0;
91         bno[1] = 1;
92         while (bno[0] < max_block)
93         {
94             zint next = bno[0] + bno[1];
95
96             sprintf(buf, ZINT_FORMAT, bno[0]);
97             YAZ_CHECK_EQ(bf_write(bf, bno[0], 0, 0, buf), 0);
98
99             bno[0] = bno[1];
100             bno[1] = next;
101         }
102         bf_close(bf);
103     }
104
105     bf = bf_open(bfs, "file", block_size, 0);
106     YAZ_CHECK(bf);
107     if (bf)
108     {
109         zint bno[2];
110
111         bno[0] = 0;
112         bno[1] = 1;
113         while (bno[0] < max_block)
114         {
115             zint next = bno[0] + bno[1];
116             memset(buf, '\0', block_size);
117
118             YAZ_CHECK_EQ(bf_read(bf, bno[0], 0, 0, buf), 1);
119             YAZ_CHECK_EQ(atoi(buf), bno[0]);
120
121             bno[0] = bno[1];
122             bno[1] = next;
123         }
124         bf_close(bf);
125     }
126
127 #if 1
128     bf = bf_open(bfs, "file", block_size, 1);
129     YAZ_CHECK(bf);
130     if (bf)
131     {
132         zint bno = 0;
133         while (bno < max_block)
134         {
135             memset(buf, '\0', block_size);
136
137             sprintf(buf, ZINT_FORMAT, bno);
138             YAZ_CHECK_EQ(bf_write(bf, bno, 0, 0, buf), 0);
139
140             bno = bno + 2;
141         }
142         bf_close(bf);
143     }
144
145     bf = bf_open(bfs, "file", block_size, 0);
146     YAZ_CHECK(bf);
147     if (bf)
148     {
149         zint bno = 0;
150         while (bno < max_block)
151         {
152             memset(buf, '\0', block_size);
153
154             YAZ_CHECK_EQ(bf_read(bf, bno, 0, 0, buf), 1);
155             YAZ_CHECK_EQ(atoi(buf), bno);
156
157             bno = bno + 2;
158         }
159         bf_close(bf);
160     }
161 #endif
162     bfs_destroy(bfs);
163 }
164
165
166 int main(int argc, char **argv)
167 {
168     YAZ_CHECK_INIT(argc, argv);
169     tst();
170     YAZ_CHECK_TERM;
171 }
172