2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2000-2002
5
* Sleepycat Software. All rights reserved.
11
* Do some regression tests for constructors.
12
* Run normally (without arguments) it is a simple regression test.
13
* Run with a numeric argument, it repeats the regression a number
14
* of times, to try to determine if there are memory leaks.
17
package com.sleepycat.test;
19
import com.sleepycat.db.*;
21
import java.io.IOException;
22
import java.io.FileNotFoundException;
24
public class TestConstruct01
26
public static final String CONSTRUCT01_DBNAME = "construct01.db";
27
public static final String CONSTRUCT01_DBDIR = "/tmp";
28
public static final String CONSTRUCT01_DBFULLPATH =
29
CONSTRUCT01_DBDIR + "/" + CONSTRUCT01_DBNAME;
31
private int itemcount; // count the number of items in the database
32
public static boolean verbose_flag = false;
34
public static void ERR(String a)
36
System.out.println("FAIL: " + a);
37
System.err.println("FAIL: " + a);
41
public static void DEBUGOUT(String s)
43
System.out.println(s);
46
public static void VERBOSEOUT(String s)
49
System.out.println(s);
52
public static void sysexit(int code)
57
private static void check_file_removed(String name, boolean fatal,
58
boolean force_remove_first)
60
File f = new File(name);
61
if (force_remove_first) {
66
System.out.print("FAIL: ");
67
System.out.print("File \"" + name + "\" still exists after run\n");
74
// Check that key/data for 0 - count-1 are already present,
75
// and write a key/data for count. The key and data are
76
// both "0123...N" where N == count-1.
78
// For some reason on Windows, we need to open using the full pathname
79
// of the file when there is no environment, thus the 'has_env'
82
void rundb(Db db, int count, boolean has_env, TestOptions options)
83
throws DbException, FileNotFoundException
88
name = CONSTRUCT01_DBNAME;
90
name = CONSTRUCT01_DBFULLPATH;
92
db.set_error_stream(System.err);
94
// We don't really care about the pagesize, but we do want
95
// to make sure adjusting Db specific variables works before
98
db.set_pagesize(1024);
99
db.open(null, name, null, Db.DB_BTREE,
100
(count != 0) ? 0 : Db.DB_CREATE, 0664);
103
// The bit map of keys we've seen
106
// The bit map of keys we expect to see
107
long expected = (1 << (count+1)) - 1;
109
byte outbuf[] = new byte[count+1];
111
for (i=0; i<count; i++) {
112
outbuf[i] = (byte)('0' + i);
113
//outbuf[i] = System.out.println((byte)('0' + i);
115
outbuf[i++] = (byte)'x';
118
System.out.println("byte: " + ('0' + 0) + ", after: " +
119
(int)'0' + "=" + (int)('0' + 0) +
120
"," + (byte)outbuf[0]);
123
Dbt key = new Dbt(outbuf, 0, i);
124
Dbt data = new Dbt(outbuf, 0, i);
126
//DEBUGOUT("Put: " + (char)outbuf[0] + ": " + new String(outbuf));
127
db.put(null, key, data, Db.DB_NOOVERWRITE);
129
// Acquire a cursor for the table.
130
Dbc dbcp = db.cursor(null, 0);
132
// Walk through the table, checking
133
Dbt readkey = new Dbt();
134
Dbt readdata = new Dbt();
135
Dbt whoknows = new Dbt();
137
readkey.set_flags(options.dbt_alloc_flags);
138
readdata.set_flags(options.dbt_alloc_flags);
140
//DEBUGOUT("Dbc.get");
141
while (dbcp.get(readkey, readdata, Db.DB_NEXT) == 0) {
142
String key_string = new String(readkey.get_data());
143
String data_string = new String(readdata.get_data());
144
//DEBUGOUT("Got: " + key_string + ": " + data_string);
145
int len = key_string.length();
146
if (len <= 0 || key_string.charAt(len-1) != 'x') {
147
ERR("reread terminator is bad");
150
long bit = (1 << len);
152
ERR("reread length is bad: expect " + count + " got "+ len + " (" + key_string + ")" );
154
else if (!data_string.equals(key_string)) {
155
ERR("key/data don't match");
157
else if ((bitmap & bit) != 0) {
158
ERR("key already seen");
160
else if ((expected & bit) == 0) {
161
ERR("key was not expected");
166
for (i=0; i<len; i++) {
167
if (key_string.charAt(i) != ('0' + i)) {
168
System.out.print(" got " + key_string
169
+ " (" + (int)key_string.charAt(i)
171
+ " (" + (int)('0' + i)
172
+ ") at position " + i + "\n");
173
ERR("key is corrupt");
179
System.out.print(" expected more keys, bitmap is: " + expected + "\n");
180
ERR("missing keys in database");
186
void t1(TestOptions options)
187
throws DbException, FileNotFoundException
189
Db db = new Db(null, 0);
190
rundb(db, itemcount++, false, options);
193
void t2(TestOptions options)
194
throws DbException, FileNotFoundException
196
Db db = new Db(null, 0);
197
rundb(db, itemcount++, false, options);
198
// rundb(db, itemcount++, false, options);
199
// rundb(db, itemcount++, false, options);
202
void t3(TestOptions options)
203
throws DbException, FileNotFoundException
205
Db db = new Db(null, 0);
206
// rundb(db, itemcount++, false, options);
207
db.set_errpfx("test3");
208
for (int i=0; i<100; i++)
209
db.set_errpfx("str" + i);
210
rundb(db, itemcount++, false, options);
213
void t4(TestOptions options)
214
throws DbException, FileNotFoundException
216
DbEnv env = new DbEnv(0);
217
env.open(CONSTRUCT01_DBDIR, Db.DB_CREATE | Db.DB_INIT_MPOOL, 0);
218
Db db = new Db(env, 0);
220
//rundb(db, itemcount++, true, options);
221
db.set_errpfx("test4");
222
rundb(db, itemcount++, true, options);
227
void t5(TestOptions options)
228
throws DbException, FileNotFoundException
230
DbEnv env = new DbEnv(0);
231
env.open(CONSTRUCT01_DBDIR, Db.DB_CREATE | Db.DB_INIT_MPOOL, 0);
232
Db db = new Db(env, 0);
233
// rundb(db, itemcount++, true, options);
234
db.set_errpfx("test5");
235
rundb(db, itemcount++, true, options);
239
// reopen the environment, don't recreate
240
env.open(CONSTRUCT01_DBDIR, Db.DB_INIT_MPOOL, 0);
241
// Note we cannot reuse the old Db!
243
Db anotherdb = new Db(env, 0);
245
// rundb(anotherdb, itemcount++, true, options);
246
anotherdb.set_errpfx("test5");
247
rundb(anotherdb, itemcount++, true, options);
251
void t6(TestOptions options)
252
throws DbException, FileNotFoundException
254
Db db = new Db(null, 0);
255
DbEnv dbenv = new DbEnv(0);
260
System.runFinalization();
263
// By design, t7 leaves a db and dbenv open; it should be detected.
264
void t7(TestOptions options)
265
throws DbException, FileNotFoundException
267
Db db = new Db(null, 0);
268
DbEnv dbenv = new DbEnv(0);
271
System.runFinalization();
274
// remove any existing environment or database
275
void removeall(boolean use_db)
281
//memory leak for this:
282
Db tmpdb = new Db(null, 0);
283
tmpdb.remove(CONSTRUCT01_DBFULLPATH, null, 0);
285
DbEnv tmpenv = new DbEnv(0);
286
tmpenv.remove(CONSTRUCT01_DBDIR, Db.DB_FORCE);
288
catch (DbException dbe) {
289
System.err.println("error during remove: " + dbe);
291
catch (FileNotFoundException fnfe) {
293
// System.err.println("error during remove: " + fnfe);
297
check_file_removed(CONSTRUCT01_DBFULLPATH, true, !use_db);
298
for (int i=0; i<8; i++) {
299
String fname = "__db.00" + i;
300
check_file_removed(fname, true, !use_db);
304
boolean doall(TestOptions options)
308
removeall((options.testmask & 1) != 0);
309
for (int item=1; item<32; item++) {
310
if ((options.testmask & (1 << item)) != 0) {
311
VERBOSEOUT(" Running test " + item + ":");
335
ERR("unknown test case: " + item);
338
VERBOSEOUT(" finished.\n");
341
removeall((options.testmask & 1) != 0);
342
options.successcounter++;
345
catch (DbException dbe) {
346
ERR("EXCEPTION RECEIVED: " + dbe);
348
catch (FileNotFoundException fnfe) {
349
ERR("EXCEPTION RECEIVED: " + fnfe);
354
public static void main(String args[])
356
int iterations = 200;
359
// Make sure the database file is removed before we start.
360
check_file_removed(CONSTRUCT01_DBFULLPATH, true, true);
362
for (int argcnt=0; argcnt<args.length; argcnt++) {
363
String arg = args[argcnt];
364
if (arg.charAt(0) == '-') {
365
// keep on lower bit, which means to remove db between tests.
367
for (int pos=1; pos<arg.length(); pos++) {
368
char ch = arg.charAt(pos);
369
if (ch >= '0' && ch <= '9') {
370
mask |= (1 << (ch - '0'));
372
else if (ch == 'v') {
376
ERR("Usage: construct01 [-testdigits] count");
379
VERBOSEOUT("mask = " + mask);
384
iterations = Integer.parseInt(arg);
385
if (iterations < 0) {
386
ERR("Usage: construct01 [-testdigits] count");
389
catch (NumberFormatException nfe) {
390
ERR("EXCEPTION RECEIVED: " + nfe);
395
// Run GC before and after the test to give
396
// a baseline for any Java memory used.
399
System.runFinalization();
400
VERBOSEOUT("gc complete");
401
long starttotal = Runtime.getRuntime().totalMemory();
402
long startfree = Runtime.getRuntime().freeMemory();
404
TestConstruct01 con = new TestConstruct01();
405
int[] dbt_flags = { 0, Db.DB_DBT_MALLOC, Db.DB_DBT_REALLOC };
406
String[] dbt_flags_name = { "default", "malloc", "realloc" };
408
TestOptions options = new TestOptions();
409
options.testmask = mask;
411
for (int flagiter = 0; flagiter < dbt_flags.length; flagiter++) {
412
options.dbt_alloc_flags = dbt_flags[flagiter];
414
VERBOSEOUT("Running with DBT alloc flags: " +
415
dbt_flags_name[flagiter]);
416
for (int i=0; i<iterations; i++) {
417
if (iterations != 0) {
418
VERBOSEOUT("(" + i + "/" + iterations + ") ");
420
VERBOSEOUT("construct01 running:");
421
if (!con.doall(options)) {
422
ERR("SOME TEST FAILED");
425
VERBOSEOUT("\nTESTS SUCCESSFUL");
428
// We continually run GC during the test to keep
429
// the Java memory usage low. That way we can
430
// monitor the total memory usage externally
431
// (e.g. via ps) and verify that we aren't leaking
432
// memory in the JNI or DB layer.
435
System.runFinalization();
436
VERBOSEOUT("gc complete");
440
if (options.successcounter == 600) {
441
System.out.println("ALL TESTS SUCCESSFUL");
444
System.out.println("***FAIL: " + (600 - options.successcounter) +
445
" tests did not complete");
447
long endtotal = Runtime.getRuntime().totalMemory();
448
long endfree = Runtime.getRuntime().freeMemory();
450
System.out.println("delta for total mem: " + magnitude(endtotal - starttotal));
451
System.out.println("delta for free mem: " + magnitude(endfree - startfree));
456
static String magnitude(long value)
458
final long max = 10000000;
459
for (long scale = 10; scale <= max; scale *= 10) {
460
if (value < scale && value > -scale)
470
int testmask = 0; // which tests to run
471
int dbt_alloc_flags = 0; // DB_DBT_* flags to use
472
int successcounter =0;