This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest_btree.c
219 lines (187 loc) · 5.29 KB
/
test_btree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2012 Jon Mayo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btree.h"
static int passes, failures;
/* a copy of a very old rand() */
#define ANS_RAND_MAX 0x7fffffff
static unsigned long ans_seed;
static void
ans_srand(unsigned long seed)
{
ans_seed = seed;
}
static unsigned
ans_rand(void)
{
ans_seed = (1103515245 * ans_seed + 12345) & ANS_RAND_MAX;
return ans_seed;
}
static const char *
randname(unsigned len)
{
static char buf[64];
const char set[] = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
"NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
unsigned i;
assert(len > 0);
if (len >= sizeof(buf) )
len = sizeof(buf) - 1;
if (len <= 0) {
fprintf(stderr, "ERROR:requested length is zero\n");
return NULL;
}
fprintf(stderr, "INFO:len=%d\n", len);
for (i = 0; i < len; i++) {
buf[i] = set[ans_rand() % (sizeof(set) - 1)];
}
buf[i] = 0;
return buf;
}
static int
do_writes(struct btree *bt, int count, int min, int max)
{
char value[64];
int i;
ans_srand(0);
for (i = 0; i < count; i++) {
struct btval key, data;
int rc;
key.data = (void*)randname(ans_rand() % (max - min + 1) + min);
key.size = strlen(key.data);
if (key.size <= 0) {
fprintf(stderr, "ERROR:key is zero length\n");
return 0;
}
data.data = value;
data.size = snprintf(value, sizeof(value), "%X", i);
fprintf(stderr, "INFO:put key '%s'\n", (char*)key.data);
rc = btree_put(bt, &key, &data, 0);
if (rc != BT_SUCCESS) {
fprintf(stderr, "ERROR:failed to write key '%s'\n",
(char*)key.data);
return 0;
}
}
return 1;
}
static int
do_reads(struct btree *bt, int count, int min, int max)
{
char value[64];
size_t value_len;
int i;
ans_srand(0);
for (i = 0; i < count; i++) {
struct btval key, data;
int rc;
key.data = (void*)randname(ans_rand() % (max - min + 1) + min);
key.size = strlen(key.data);
if (key.size <= 0) {
fprintf(stderr, "ERROR:key is zero length\n");
return 0;
}
data.data = NULL;
data.size = 0;
value_len = snprintf(value, sizeof(value), "%X", i);
fprintf(stderr, "INFO:get key '%s'\n", (char*)key.data);
rc = btree_get(bt, &key, &data);
if (rc != BT_SUCCESS) {
fprintf(stderr, "ERROR:failed to read key '%s'\n",
(char*)key.data);
return 0;
}
if (value_len != data.size || memcmp(value, data.data, value_len)) {
fprintf(stderr, "ERROR:unexpected value '%.*s' from key '%s'\n",
(int)data.size, (char*)data.data,
(char*)key.data);
return 0;
}
}
return 1;
}
static int
do_deletes(struct btree *bt, int count, int min, int max)
{
int i;
ans_srand(0);
for (i = 0; i < count; i++) {
struct btval key;
int rc;
key.data = (void*)randname(ans_rand() % (max - min + 1) + min);
key.size = strlen(key.data);
if (key.size <= 0) {
fprintf(stderr, "ERROR:key is zero length\n");
return 0;
}
fprintf(stderr, "INFO:delete key '%s'\n", (char*)key.data);
rc = btree_del(bt, &key, NULL);
if (rc != BT_SUCCESS) {
fprintf(stderr, "ERROR:failed to delete key '%s'\n",
(char*)key.data);
return 0;
}
}
return 1;
}
static void
test(const char *name, int status)
{
if (status)
passes++;
else
failures++;
fprintf(stderr, "%s:%s\n", status ? "PASS" : "FAILURE", name);
}
static void
report(void)
{
fprintf(stderr, "RESULTS:total=%d failures=%d\n",
passes + failures, failures);
}
int
main()
{
struct btree *bt;
const char filename[] = "test.db";
test("btree_open(BT_NOSYNC)",
(bt = btree_open(filename, BT_NOSYNC, 0644)) != NULL);
test("do_writes(50, 1, 33)", do_writes(bt, 50, 1, 33));
test("do_reads(50, 1, 33)", do_reads(bt, 50, 1, 33));
test("do_deletes(50, 1, 33)", do_deletes(bt, 50, 1, 33));
test("btree_close", (btree_close(bt),1));
test("btree_open(!BT_NOSYNC)",
(bt = btree_open(filename, 0, 0644)) != NULL);
test("do_writes(50, 1, 33)", do_writes(bt, 50, 1, 33));
test("do_reads(50, 1, 33)", do_reads(bt, 50, 1, 33));
test("do_deletes(50, 1, 33)", do_deletes(bt, 50, 1, 33));
test("btree_close", (btree_close(bt),1));
test("btree_open(BT_RDONLY)",
(bt = btree_open(filename, BT_RDONLY, 0644)) != NULL);
test("do_reads(50, 1, 33)", do_reads(bt, 50, 1, 33));
test("btree_close", (btree_close(bt),1));
test("btree_open(BT_NOSYNC | BT_REVERSEKEY)",
(bt = btree_open(filename, BT_NOSYNC | BT_REVERSEKEY, 0644)) != NULL);
test("do_writes(50, 1, 33)", do_writes(bt, 50, 1, 33));
test("do_reads(50, 1, 33)", do_reads(bt, 50, 1, 33));
test("do_deletes(50, 1, 33)", do_deletes(bt, 50, 1, 33));
test("btree_close", (btree_close(bt),1));
report();
return failures ? EXIT_FAILURE : EXIT_SUCCESS;
}