1 /* $Id: tstbfile1.c,v 1.1 2005-03-30 09:25:23 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
25 #include <idzebra/bfile.h>
33 bf = bf_xopen(bfs, "tst", /* block size */ 32,
34 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
36 bf_xclose(bf, version, 0 /* no more info */);
38 bf = bf_xopen(bfs, "tst", /* block size */ 32,
39 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
41 bf_xclose(bf, version, 0 /* no more info */);
52 bf = bf_xopen(bfs, "tst", /* block size */ 32,
53 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
56 for (i = 1; i<30; i++)
61 if (bf_alloc(bf, i, blocks + bno))
63 fprintf(stderr, "bf_alloc failed i=%d bno=%d\n", i, bno);
66 for (j = 0; j < i; j++)
70 fprintf(stderr, "zero block i=%d bno=%d j=%d\n", i, bno, j);
76 for (i = 0; i<bno; i++)
78 if (bf_free(bf, 1, blocks + i))
80 fprintf(stderr, "bf_freec failed i=%d\n", i);
84 bf_xclose(bf, version, 0);
99 for(i = 0; i<BLOCKS; i++)
102 bf = bf_xopen(bfs, "tst", /* block size */ 32,
103 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
106 for (pass = 0; pass < 100; pass++)
108 int r = random() % 9;
110 assert (no_in_use >= 0 && no_in_use <= BLOCKS);
111 if (r < 5 && (BLOCKS - no_in_use) > 0)
115 zint tblocks[BLOCKS];
116 int left = BLOCKS - no_in_use;
117 int to_alloc = 1 + (random() % left);
119 bf_alloc(bf, to_alloc, tblocks);
120 /* transfer from tblocks to blocks */
121 for (i = 0; i<BLOCKS; i++)
125 blocks[i] = tblocks[j++];
132 else if (r < 8 && no_in_use > 0)
135 zint tblocks[BLOCKS];
136 int to_free = 1 + (random() % no_in_use);
137 int start = random() % to_free;
139 for (i = 0; i<BLOCKS; i++)
143 if (j >= start && j < to_free)
145 tblocks[j-start] = blocks[i];
152 assert(tblocks[to_free-start-1]);
153 bf_free(bf, to_free - start, tblocks);
157 bf_xclose(bf, version, 0);
158 bf = bf_xopen(bfs, "tst", /* block size */ 32,
159 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
162 bf_xclose(bf, version, 0);
165 int main(int argc, char **argv)
167 BFiles bfs = bfs_create(0, /* register: current dir, no limit */
168 0 /* base: current dir */