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