TestConstruct01.java [plain text]
package com.sleepycat.test;
import com.sleepycat.db.*;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
public class TestConstruct01
{
public static final String CONSTRUCT01_DBNAME = "construct01.db";
public static final String CONSTRUCT01_DBDIR = "/tmp";
public static final String CONSTRUCT01_DBFULLPATH =
CONSTRUCT01_DBDIR + "/" + CONSTRUCT01_DBNAME;
private int itemcount; public static boolean verbose_flag = false;
public static void ERR(String a)
{
System.out.println("FAIL: " + a);
System.err.println("FAIL: " + a);
sysexit(1);
}
public static void DEBUGOUT(String s)
{
System.out.println(s);
}
public static void VERBOSEOUT(String s)
{
if (verbose_flag)
System.out.println(s);
}
public static void sysexit(int code)
{
System.exit(code);
}
private static void check_file_removed(String name, boolean fatal,
boolean force_remove_first)
{
File f = new File(name);
if (force_remove_first) {
f.delete();
}
if (f.exists()) {
if (fatal)
System.out.print("FAIL: ");
System.out.print("File \"" + name + "\" still exists after run\n");
if (fatal)
sysexit(1);
}
}
void rundb(Db db, int count, boolean has_env, TestOptions options)
throws DbException, FileNotFoundException
{
String name;
if (has_env)
name = CONSTRUCT01_DBNAME;
else
name = CONSTRUCT01_DBFULLPATH;
db.set_error_stream(System.err);
db.set_pagesize(1024);
db.open(null, name, null, Db.DB_BTREE,
(count != 0) ? 0 : Db.DB_CREATE, 0664);
long bitmap = 0;
long expected = (1 << (count+1)) - 1;
byte outbuf[] = new byte[count+1];
int i;
for (i=0; i<count; i++) {
outbuf[i] = (byte)('0' + i);
}
outbuf[i++] = (byte)'x';
Dbt key = new Dbt(outbuf, 0, i);
Dbt data = new Dbt(outbuf, 0, i);
db.put(null, key, data, Db.DB_NOOVERWRITE);
Dbc dbcp = db.cursor(null, 0);
Dbt readkey = new Dbt();
Dbt readdata = new Dbt();
Dbt whoknows = new Dbt();
readkey.set_flags(options.dbt_alloc_flags);
readdata.set_flags(options.dbt_alloc_flags);
while (dbcp.get(readkey, readdata, Db.DB_NEXT) == 0) {
String key_string =
new String(readkey.get_data(), 0, readkey.get_size());
String data_string =
new String(readdata.get_data(), 0, readkey.get_size());
int len = key_string.length();
if (len <= 0 || key_string.charAt(len-1) != 'x') {
ERR("reread terminator is bad");
}
len--;
long bit = (1 << len);
if (len > count) {
ERR("reread length is bad: expect " + count + " got "+ len + " (" + key_string + ")" );
}
else if (!data_string.equals(key_string)) {
ERR("key/data don't match");
}
else if ((bitmap & bit) != 0) {
ERR("key already seen");
}
else if ((expected & bit) == 0) {
ERR("key was not expected");
}
else {
bitmap |= bit;
expected &= ~(bit);
for (i=0; i<len; i++) {
if (key_string.charAt(i) != ('0' + i)) {
System.out.print(" got " + key_string
+ " (" + (int)key_string.charAt(i)
+ "), wanted " + i
+ " (" + (int)('0' + i)
+ ") at position " + i + "\n");
ERR("key is corrupt");
}
}
}
}
if (expected != 0) {
System.out.print(" expected more keys, bitmap is: " + expected + "\n");
ERR("missing keys in database");
}
dbcp.close();
db.close(0);
}
void t1(TestOptions options)
throws DbException, FileNotFoundException
{
Db db = new Db(null, 0);
rundb(db, itemcount++, false, options);
}
void t2(TestOptions options)
throws DbException, FileNotFoundException
{
Db db = new Db(null, 0);
rundb(db, itemcount++, false, options);
}
void t3(TestOptions options)
throws DbException, FileNotFoundException
{
Db db = new Db(null, 0);
db.set_errpfx("test3");
for (int i=0; i<100; i++)
db.set_errpfx("str" + i);
rundb(db, itemcount++, false, options);
}
void t4(TestOptions options)
throws DbException, FileNotFoundException
{
DbEnv env = new DbEnv(0);
env.open(CONSTRUCT01_DBDIR, Db.DB_CREATE | Db.DB_INIT_MPOOL, 0);
Db db = new Db(env, 0);
db.set_errpfx("test4");
rundb(db, itemcount++, true, options);
env.close(0);
}
void t5(TestOptions options)
throws DbException, FileNotFoundException
{
DbEnv env = new DbEnv(0);
env.open(CONSTRUCT01_DBDIR, Db.DB_CREATE | Db.DB_INIT_MPOOL, 0);
Db db = new Db(env, 0);
db.set_errpfx("test5");
rundb(db, itemcount++, true, options);
Db anotherdb = new Db(env, 0);
anotherdb.set_errpfx("test5");
rundb(anotherdb, itemcount++, true, options);
env.close(0);
}
void t6(TestOptions options)
throws DbException, FileNotFoundException
{
Db db = new Db(null, 0);
DbEnv dbenv = new DbEnv(0);
db.close(0);
dbenv.close(0);
System.gc();
System.runFinalization();
}
void t7(TestOptions options)
throws DbException, FileNotFoundException
{
Db db = new Db(null, 0);
DbEnv dbenv = new DbEnv(0);
System.gc();
System.runFinalization();
}
void removeall(boolean use_db)
{
{
if (use_db) {
try {
Db tmpdb = new Db(null, 0);
tmpdb.remove(CONSTRUCT01_DBFULLPATH, null, 0);
DbEnv tmpenv = new DbEnv(0);
tmpenv.remove(CONSTRUCT01_DBDIR, Db.DB_FORCE);
}
catch (DbException dbe) {
System.err.println("error during remove: " + dbe);
}
catch (FileNotFoundException fnfe) {
}
}
}
check_file_removed(CONSTRUCT01_DBFULLPATH, true, !use_db);
for (int i=0; i<8; i++) {
String fname = "__db.00" + i;
check_file_removed(fname, true, !use_db);
}
}
boolean doall(TestOptions options)
{
itemcount = 0;
try {
removeall((options.testmask & 1) != 0);
for (int item=1; item<32; item++) {
if ((options.testmask & (1 << item)) != 0) {
VERBOSEOUT(" Running test " + item + ":");
switch (item) {
case 1:
t1(options);
break;
case 2:
t2(options);
break;
case 3:
t3(options);
break;
case 4:
t4(options);
break;
case 5:
t5(options);
break;
case 6:
t6(options);
break;
case 7:
t7(options);
break;
default:
ERR("unknown test case: " + item);
break;
}
VERBOSEOUT(" finished.\n");
}
}
removeall((options.testmask & 1) != 0);
options.successcounter++;
return true;
}
catch (DbException dbe) {
ERR("EXCEPTION RECEIVED: " + dbe);
}
catch (FileNotFoundException fnfe) {
ERR("EXCEPTION RECEIVED: " + fnfe);
}
return false;
}
public static void main(String args[])
{
int iterations = 200;
int mask = 0x7f;
check_file_removed(CONSTRUCT01_DBFULLPATH, true, true);
for (int argcnt=0; argcnt<args.length; argcnt++) {
String arg = args[argcnt];
if (arg.charAt(0) == '-') {
mask = 1;
for (int pos=1; pos<arg.length(); pos++) {
char ch = arg.charAt(pos);
if (ch >= '0' && ch <= '9') {
mask |= (1 << (ch - '0'));
}
else if (ch == 'v') {
verbose_flag = true;
}
else {
ERR("Usage: construct01 [-testdigits] count");
}
}
VERBOSEOUT("mask = " + mask);
}
else {
try {
iterations = Integer.parseInt(arg);
if (iterations < 0) {
ERR("Usage: construct01 [-testdigits] count");
}
}
catch (NumberFormatException nfe) {
ERR("EXCEPTION RECEIVED: " + nfe);
}
}
}
System.gc();
System.runFinalization();
VERBOSEOUT("gc complete");
long starttotal = Runtime.getRuntime().totalMemory();
long startfree = Runtime.getRuntime().freeMemory();
TestConstruct01 con = new TestConstruct01();
int[] dbt_flags = { 0, Db.DB_DBT_MALLOC, Db.DB_DBT_REALLOC };
String[] dbt_flags_name = { "default", "malloc", "realloc" };
TestOptions options = new TestOptions();
options.testmask = mask;
for (int flagiter = 0; flagiter < dbt_flags.length; flagiter++) {
options.dbt_alloc_flags = dbt_flags[flagiter];
VERBOSEOUT("Running with DBT alloc flags: " +
dbt_flags_name[flagiter]);
for (int i=0; i<iterations; i++) {
if (iterations != 0) {
VERBOSEOUT("(" + i + "/" + iterations + ") ");
}
VERBOSEOUT("construct01 running:");
if (!con.doall(options)) {
ERR("SOME TEST FAILED");
}
else {
VERBOSEOUT("\nTESTS SUCCESSFUL");
}
System.gc();
System.runFinalization();
VERBOSEOUT("gc complete, bytes free == " + Runtime.getRuntime().freeMemory());
}
}
if (options.successcounter == 600) {
System.out.println("ALL TESTS SUCCESSFUL");
}
else {
System.out.println("***FAIL: " + (600 - options.successcounter) +
" tests did not complete");
}
long endtotal = Runtime.getRuntime().totalMemory();
long endfree = Runtime.getRuntime().freeMemory();
System.out.println("delta for total mem: " + magnitude(endtotal - starttotal));
System.out.println("delta for free mem: " + magnitude(endfree - startfree));
return;
}
static String magnitude(long value)
{
final long max = 10000000;
for (long scale = 10; scale <= max; scale *= 10) {
if (value < scale && value > -scale)
return "<" + scale;
}
return ">" + max;
}
}
class TestOptions
{
int testmask = 0; int dbt_alloc_flags = 0; int successcounter =0;
}