Happy new year.
[idzebra-moved-to-github.git] / bfile / tstbfile1.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 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 <stdlib.h>
21 #include <string.h>
22 #include <idzebra/bfile.h>
23 #include <yaz/test.h>
24
25 void tst1(BFiles bfs)
26 {
27     int version = 1;
28     BFile bf;
29     const char *more_info = 0;
30
31     bf_reset(bfs);
32     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
33                   /* wr */ 1, "tstmagic",       &version, 0 /* more_info */);
34     YAZ_CHECK(bf);
35     if (!bf)
36         return;
37     bf_xclose(bf, version, "more info");
38     
39     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
40                   /* wr */ 1, "tstmagic",       &version, &more_info);
41     
42     YAZ_CHECK(bf);
43     if (!bf)
44         return;
45
46     YAZ_CHECK(strcmp(more_info, "more info") == 0);
47     bf_xclose(bf, version, 0 /* no more info */);
48 }
49
50 void tst2(BFiles bfs)
51 {
52     int version = 1;
53     int bno, i;
54     zint blocks[1000];
55     BFile bf;
56     bf_reset(bfs);
57
58     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
59                         /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
60     YAZ_CHECK(bf);
61     if (!bf)
62         return;
63     bno = 0;
64     for (i = 1; i<30; i++)
65     {
66         int j;
67         for (j = 0; j<i; j++)
68             blocks[bno + j] = 0;
69         YAZ_CHECK_EQ(bf_alloc(bf, i, blocks + bno), 0);
70         for (j = 0; j < i; j++)
71         {
72             YAZ_CHECK(blocks[bno + j]);
73         }
74         bno += i;
75     }
76     for (i = 0; i<bno; i++)
77     {
78         YAZ_CHECK_EQ(bf_free(bf, 1, blocks + i), 0);
79     }
80     bf_xclose(bf, version, 0);
81 }
82
83 #define BLOCKS 100
84
85 void tst3(BFiles bfs)
86 {
87     int version = 1;
88     int i;
89     zint blocks[BLOCKS];
90     BFile bf;
91     int no_in_use = 0;
92     int pass = 0;
93     bf_reset(bfs);
94
95     for(i = 0; i<BLOCKS; i++)
96         blocks[i] = 0;
97
98     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
99                         /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
100     YAZ_CHECK(bf);
101     if (!bf)
102         return;
103     no_in_use = 0;
104     for (pass = 0; pass < 100; pass++)
105     {
106         int r = random() % 9;
107
108         YAZ_CHECK(no_in_use >= 0);
109         YAZ_CHECK(no_in_use <= BLOCKS);
110         if (r < 5 && (BLOCKS - no_in_use) > 0)
111         {
112             /* alloc phase */
113             int j = 0;
114             zint tblocks[BLOCKS];
115             int left = BLOCKS - no_in_use;
116             int to_alloc = 1 + (random() % left);
117
118             bf_alloc(bf, to_alloc, tblocks);
119             /* transfer from tblocks to blocks */
120             for (i = 0; i<BLOCKS; i++)
121             {
122                 if (blocks[i] == 0)
123                 {
124                     blocks[i] = tblocks[j++];
125                     no_in_use++;
126                     if (j == to_alloc)
127                         break;
128                 }
129             }
130         }
131         else if (r < 8 && no_in_use > 0)
132         {
133             /* free phase */
134             zint tblocks[BLOCKS];
135             int to_free = 1 + (random() % no_in_use);
136             int start = random() % to_free;
137             int j = 0;
138             for (i = 0; i<BLOCKS; i++)
139             {
140                 if (blocks[i])
141                 {
142                     if (j >= start && j < to_free)
143                     {
144                         tblocks[j-start] = blocks[i];
145                         blocks[i] = 0;
146                         no_in_use--;
147                     }
148                     j++;
149                 }
150             }
151             YAZ_CHECK(tblocks[to_free-start-1]);
152             bf_free(bf, to_free - start, tblocks);
153         }
154         else
155         {
156             bf_xclose(bf, version, 0);
157             bf = bf_xopen(bfs, "tst", /* block size */ 32, 
158                           /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
159         }
160     }
161     bf_xclose(bf, version, 0);
162 }
163
164 static void tst(void)
165 {
166     BFiles bfs = bfs_create(0, /* register: current dir, no limit */
167                             0  /* base: current dir */
168         );
169     YAZ_CHECK(bfs);
170     if (!bfs)
171         return;
172
173     tst1(bfs);
174     tst2(bfs);
175     tst3(bfs);
176     bf_reset(bfs);
177     bfs_destroy(bfs);
178 }
179
180 int main(int argc, char **argv)
181 {
182     YAZ_CHECK_INIT(argc, argv);
183     YAZ_CHECK_LOG();
184     tst();
185     YAZ_CHECK_TERM;
186 }
187
188 /*
189  * Local variables:
190  * c-basic-offset: 4
191  * c-file-style: "Stroustrup"
192  * indent-tabs-mode: nil
193  * End:
194  * vim: shiftwidth=4 tabstop=8 expandtab
195  */
196