Moved zebrautl.h to idzebra/util.h.
[idzebra-moved-to-github.git] / bfile / tstbfile1.c
1 /* $Id: tstbfile1.c,v 1.1 2005-03-30 09:25:23 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <idzebra/bfile.h>
26
27 void tst1(BFiles bfs)
28 {
29     int version = 1;
30     BFile bf;
31
32     bf_reset(bfs);
33     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
34                   /* wr */ 1, "tstmagic",       &version, 0 /* more_info */);
35     
36     bf_xclose(bf, version, 0 /* no more info */);
37     
38     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
39                   /* wr */ 1, "tstmagic",       &version, 0 /* more_info */);
40     
41     bf_xclose(bf, version, 0 /* no more info */);
42 }
43
44 void tst2(BFiles bfs)
45 {
46     int version = 1;
47     int bno, i;
48     zint blocks[1000];
49     BFile bf;
50     bf_reset(bfs);
51
52     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
53                         /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
54
55     bno = 0;
56     for (i = 1; i<30; i++)
57     {
58         int j;
59         for (j = 0; j<i; j++)
60             blocks[bno + j] = 0;
61         if (bf_alloc(bf, i, blocks + bno))
62         {
63             fprintf(stderr, "bf_alloc failed i=%d bno=%d\n", i, bno);
64             exit(1);
65         }
66         for (j = 0; j < i; j++)
67         {
68             if (!blocks[bno + j])
69             {
70                 fprintf(stderr, "zero block i=%d bno=%d j=%d\n", i, bno, j);
71                 exit(1);
72             }
73         }
74         bno += i;
75     }
76     for (i = 0; i<bno; i++)
77     {
78         if (bf_free(bf, 1, blocks + i))
79         {
80             fprintf(stderr, "bf_freec failed i=%d\n", i);
81             exit(1);
82         }
83     }
84     bf_xclose(bf, version, 0);
85 }
86
87 #define BLOCKS 100
88
89 void tst3(BFiles bfs)
90 {
91     int version = 1;
92     int i;
93     zint blocks[BLOCKS];
94     BFile bf;
95     int no_in_use = 0;
96     int pass = 0;
97     bf_reset(bfs);
98
99     for(i = 0; i<BLOCKS; i++)
100         blocks[i] = 0;
101
102     bf = bf_xopen(bfs, "tst", /* block size */ 32, 
103                         /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
104
105     no_in_use = 0;
106     for (pass = 0; pass < 100; pass++)
107     {
108         int r = random() % 9;
109
110         assert (no_in_use >= 0 && no_in_use <= BLOCKS);
111         if (r < 5 && (BLOCKS - no_in_use) > 0)
112         {
113             /* alloc phase */
114             int j = 0;
115             zint tblocks[BLOCKS];
116             int left = BLOCKS - no_in_use;
117             int to_alloc = 1 + (random() % left);
118
119             bf_alloc(bf, to_alloc, tblocks);
120             /* transfer from tblocks to blocks */
121             for (i = 0; i<BLOCKS; i++)
122             {
123                 if (blocks[i] == 0)
124                 {
125                     blocks[i] = tblocks[j++];
126                     no_in_use++;
127                     if (j == to_alloc)
128                         break;
129                 }
130             }
131         }
132         else if (r < 8 && no_in_use > 0)
133         {
134             /* free phase */
135             zint tblocks[BLOCKS];
136             int to_free = 1 + (random() % no_in_use);
137             int start = random() % to_free;
138             int j = 0;
139             for (i = 0; i<BLOCKS; i++)
140             {
141                 if (blocks[i])
142                 {
143                     if (j >= start && j < to_free)
144                     {
145                         tblocks[j-start] = blocks[i];
146                         blocks[i] = 0;
147                         no_in_use--;
148                     }
149                     j++;
150                 }
151             }
152             assert(tblocks[to_free-start-1]);
153             bf_free(bf, to_free - start, tblocks);
154         }
155         else
156         {
157             bf_xclose(bf, version, 0);
158             bf = bf_xopen(bfs, "tst", /* block size */ 32, 
159                           /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
160         }
161     }
162     bf_xclose(bf, version, 0);
163 }
164
165 int main(int argc, char **argv)
166 {
167     BFiles bfs = bfs_create(0, /* register: current dir, no limit */
168                             0  /* base: current dir */
169         );
170     if (!bfs)
171         exit(1);
172
173     tst1(bfs);
174     tst2(bfs);
175     tst3(bfs);
176     bf_reset(bfs);
177     bfs_destroy(bfs);
178     exit(0);
179 }