45183c4e23b155e4b8fb3e3c8a14ad02313a2423
[idzebra-moved-to-github.git] / bfile / tstbfile2.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2010 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <yaz/test.h>
26 #include <idzebra/bfile.h>
27
28 void tst(void)
29 {
30     int r;
31     BFiles bfs;
32     BFile bf;
33     char buf[256];
34     int block_size = 16;
35     zint max_block = 200000;
36
37     YAZ_CHECK(block_size <= sizeof(buf));
38     if (!(block_size <= sizeof(buf)))
39         return;
40
41     YAZ_CHECK(max_block * block_size < 4 * 1000000); /* 4M */
42     
43     r = mkdir("register", 0777);
44     YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
45
46     r = mkdir("shadow", 0777);
47     YAZ_CHECK(r == 0 || (r == -1 && errno == EEXIST));
48
49     bfs = bfs_create("register.bad:4M", 0 /* base: current dir */);
50     YAZ_CHECK(!bfs);
51     if (bfs)
52         return;
53
54     bfs = bfs_create("register:4M", 0 /* base: current dir */);
55     YAZ_CHECK(bfs);
56     if (!bfs)
57         return;
58
59     r = bf_cache(bfs, "shadow.bad:4M");
60     YAZ_CHECK_EQ(r, ZEBRA_FAIL);
61
62     r = bf_cache(bfs, "shadow:4M");
63     YAZ_CHECK_EQ(r, ZEBRA_OK);
64
65     bf_reset(bfs);
66
67 #if 1
68     /* we have to destroy bfs after reset. Unfortunately! */
69     bfs_destroy(bfs);
70
71     bfs = bfs_create("register:4M", 0 /* base: current dir */);
72     YAZ_CHECK(bfs);
73     if (!bfs)
74         return;
75
76     r = bf_cache(bfs, "shadow:4M");
77     YAZ_CHECK_EQ(r, ZEBRA_OK);
78 #endif
79
80     yaz_log(YLOG_LOG, "writing file 1");
81     bf = bf_open(bfs, "file", block_size, 1);
82     YAZ_CHECK(bf);
83     if (bf)
84     {
85         zint bno[2];
86         memset(buf, ' ', block_size);
87
88         bno[0] = 0;
89         bno[1] = 1;
90         while (bno[0] < max_block)
91         {
92             zint next = bno[0] + bno[1];
93
94             sprintf(buf, ZINT_FORMAT, bno[0]);
95             YAZ_CHECK_EQ(bf_write(bf, bno[0], 0, 0, buf), 0);
96
97             bno[0] = bno[1];
98             bno[1] = next;
99         }
100         bf_close(bf);
101     }
102
103     yaz_log(YLOG_LOG, "reading file 1");
104     bf = bf_open(bfs, "file", block_size, 0);
105     YAZ_CHECK(bf);
106     if (bf)
107     {
108         zint bno[2];
109
110         bno[0] = 0;
111         bno[1] = 1;
112         while (bno[0] < max_block)
113         {
114             zint next = bno[0] + bno[1];
115             memset(buf, ' ', block_size);
116
117             YAZ_CHECK_EQ(bf_read(bf, bno[0], 0, 0, buf), 1);
118             YAZ_CHECK_EQ(atoi(buf), bno[0]);
119
120             bno[0] = bno[1];
121             bno[1] = next;
122         }
123         bf_close(bf);
124     }
125
126 #if 1
127     yaz_log(YLOG_LOG, "writing file 2");
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, ' ', 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     yaz_log(YLOG_LOG, "reading file 2");
146     bf = bf_open(bfs, "file", block_size, 0);
147     YAZ_CHECK(bf);
148     if (bf)
149     {
150         zint bno = 0;
151         int step = max_block / 50;
152        
153         while (bno < max_block)
154         {
155             memset(buf, ' ', block_size);
156
157             YAZ_CHECK_EQ(bf_read(bf, bno, 0, 0, buf), 1);
158             YAZ_CHECK_EQ(atoi(buf), bno);
159
160             bno = bno + 2*step;
161         }
162         bf_close(bf);
163     }
164 #endif
165     bfs_destroy(bfs);
166 }
167
168
169 int main(int argc, char **argv)
170 {
171     YAZ_CHECK_INIT(argc, argv);
172     YAZ_CHECK_LOG();
173     tst();
174     YAZ_CHECK_TERM;
175 }
176