Added test of bfile/cfile/mfile system
[idzebra-moved-to-github.git] / bfile / tstbfile2.c
1 /* $Id: tstbfile2.c,v 1.1 2006-11-08 12:59:27 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     bf = bf_open(bfs, "file", block_size, 1);
71     YAZ_CHECK(bf);
72     if (bf)
73     {
74         zint bno[2];
75         memset(buf, '\0', block_size);
76
77         bno[0] = 0;
78         bno[1] = 1;
79         while (bno[0] < max_block)
80         {
81             zint next = bno[0] + bno[1];
82
83             sprintf(buf, ZINT_FORMAT, bno[0]);
84             YAZ_CHECK_EQ(bf_write(bf, bno[0], 0, 0, buf), 0);
85
86             bno[0] = bno[1];
87             bno[1] = next;
88         }
89         bf_close(bf);
90     }
91
92     bf = bf_open(bfs, "file", block_size, 0);
93     YAZ_CHECK(bf);
94     if (bf)
95     {
96         zint bno[2];
97
98         bno[0] = 0;
99         bno[1] = 1;
100         while (bno[0] < max_block)
101         {
102             zint next = bno[0] + bno[1];
103             memset(buf, '\0', block_size);
104
105             YAZ_CHECK_EQ(bf_read(bf, bno[0], 0, 0, buf), 1);
106             YAZ_CHECK_EQ(atoi(buf), bno[0]);
107
108             bno[0] = bno[1];
109             bno[1] = next;
110         }
111         bf_close(bf);
112     }
113
114 #if 1
115     bf = bf_open(bfs, "file", block_size, 1);
116     YAZ_CHECK(bf);
117     if (bf)
118     {
119         zint bno = 0;
120         while (bno < max_block)
121         {
122             memset(buf, '\0', block_size);
123
124             sprintf(buf, ZINT_FORMAT, bno);
125             YAZ_CHECK_EQ(bf_write(bf, bno, 0, 0, buf), 0);
126
127             bno = bno + 2;
128         }
129         bf_close(bf);
130     }
131
132     bf = bf_open(bfs, "file", block_size, 0);
133     YAZ_CHECK(bf);
134     if (bf)
135     {
136         zint bno = 0;
137         while (bno < max_block)
138         {
139             memset(buf, '\0', block_size);
140
141             YAZ_CHECK_EQ(bf_read(bf, bno, 0, 0, buf), 1);
142             YAZ_CHECK_EQ(atoi(buf), bno);
143
144             bno = bno + 2;
145         }
146         bf_close(bf);
147     }
148 #endif
149     bfs_destroy(bfs);
150 }
151
152
153 int main(int argc, char **argv)
154 {
155     YAZ_CHECK_INIT(argc, argv);
156     tst();
157     YAZ_CHECK_TERM;
158 }
159