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