1 /* $Id: tstbfile1.c,v 1.3 2006-05-10 08:13:17 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
26 #include <idzebra/bfile.h>
32 const char *more_info = 0;
35 bf = bf_xopen(bfs, "tst", /* block size */ 32,
36 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
38 bf_xclose(bf, version, "more info");
40 bf = bf_xopen(bfs, "tst", /* block size */ 32,
41 /* wr */ 1, "tstmagic", &version, &more_info);
43 if (strcmp(more_info, "more info"))
45 fprintf(stderr, "tstbfile1: more info data corrupt more_info=%s\n",
49 bf_xclose(bf, version, 0 /* no more info */);
60 bf = bf_xopen(bfs, "tst", /* block size */ 32,
61 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
64 for (i = 1; i<30; i++)
69 if (bf_alloc(bf, i, blocks + bno))
71 fprintf(stderr, "bf_alloc failed i=%d bno=%d\n", i, bno);
74 for (j = 0; j < i; j++)
78 fprintf(stderr, "zero block i=%d bno=%d j=%d\n", i, bno, j);
84 for (i = 0; i<bno; i++)
86 if (bf_free(bf, 1, blocks + i))
88 fprintf(stderr, "bf_freec failed i=%d\n", i);
92 bf_xclose(bf, version, 0);
107 for(i = 0; i<BLOCKS; i++)
110 bf = bf_xopen(bfs, "tst", /* block size */ 32,
111 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
114 for (pass = 0; pass < 100; pass++)
116 int r = random() % 9;
118 assert (no_in_use >= 0 && no_in_use <= BLOCKS);
119 if (r < 5 && (BLOCKS - no_in_use) > 0)
123 zint tblocks[BLOCKS];
124 int left = BLOCKS - no_in_use;
125 int to_alloc = 1 + (random() % left);
127 bf_alloc(bf, to_alloc, tblocks);
128 /* transfer from tblocks to blocks */
129 for (i = 0; i<BLOCKS; i++)
133 blocks[i] = tblocks[j++];
140 else if (r < 8 && no_in_use > 0)
143 zint tblocks[BLOCKS];
144 int to_free = 1 + (random() % no_in_use);
145 int start = random() % to_free;
147 for (i = 0; i<BLOCKS; i++)
151 if (j >= start && j < to_free)
153 tblocks[j-start] = blocks[i];
160 assert(tblocks[to_free-start-1]);
161 bf_free(bf, to_free - start, tblocks);
165 bf_xclose(bf, version, 0);
166 bf = bf_xopen(bfs, "tst", /* block size */ 32,
167 /* wr */ 1, "tstmagic", &version, 0 /* more_info */);
170 bf_xclose(bf, version, 0);
173 int main(int argc, char **argv)
175 BFiles bfs = bfs_create(0, /* register: current dir, no limit */
176 0 /* base: current dir */
191 * indent-tabs-mode: nil
193 * vim: shiftwidth=4 tabstop=8 expandtab