~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/test/scr016/TestConstruct01.java

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 2000-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
 
10
 
/*
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.
15
 
 */
16
 
 
17
 
package com.sleepycat.test;
18
 
 
19
 
import com.sleepycat.db.*;
20
 
import java.io.File;
21
 
import java.io.IOException;
22
 
import java.io.FileNotFoundException;
23
 
 
24
 
public class TestConstruct01
25
 
{
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;
30
 
 
31
 
    private int itemcount;      // count the number of items in the database
32
 
    public static boolean verbose_flag = false;
33
 
 
34
 
    public static void ERR(String a)
35
 
    {
36
 
        System.out.println("FAIL: " + a);
37
 
        System.err.println("FAIL: " + a);
38
 
        sysexit(1);
39
 
    }
40
 
 
41
 
    public static void DEBUGOUT(String s)
42
 
    {
43
 
        System.out.println(s);
44
 
    }
45
 
 
46
 
    public static void VERBOSEOUT(String s)
47
 
    {
48
 
        if (verbose_flag)
49
 
            System.out.println(s);
50
 
    }
51
 
 
52
 
    public static void sysexit(int code)
53
 
    {
54
 
        System.exit(code);
55
 
    }
56
 
 
57
 
    private static void check_file_removed(String name, boolean fatal,
58
 
                                           boolean force_remove_first)
59
 
    {
60
 
        File f = new File(name);
61
 
        if (force_remove_first) {
62
 
            f.delete();
63
 
        }
64
 
        if (f.exists()) {
65
 
            if (fatal)
66
 
                System.out.print("FAIL: ");
67
 
            System.out.print("File \"" + name + "\" still exists after run\n");
68
 
            if (fatal)
69
 
                sysexit(1);
70
 
        }
71
 
    }
72
 
 
73
 
 
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.
77
 
    //
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'
80
 
    // variable.
81
 
    //
82
 
    void rundb(Db db, int count, boolean has_env, TestOptions options)
83
 
        throws DbException, FileNotFoundException
84
 
    {
85
 
        String name;
86
 
 
87
 
        if (has_env)
88
 
            name = CONSTRUCT01_DBNAME;
89
 
        else
90
 
            name = CONSTRUCT01_DBFULLPATH;
91
 
 
92
 
        db.set_error_stream(System.err);
93
 
 
94
 
        // We don't really care about the pagesize, but we do want
95
 
        // to make sure adjusting Db specific variables works before
96
 
        // opening the db.
97
 
        //
98
 
        db.set_pagesize(1024);
99
 
        db.open(null, name, null, Db.DB_BTREE,
100
 
                (count != 0) ? 0 : Db.DB_CREATE, 0664);
101
 
 
102
 
 
103
 
        // The bit map of keys we've seen
104
 
        long bitmap = 0;
105
 
 
106
 
        // The bit map of keys we expect to see
107
 
        long expected = (1 << (count+1)) - 1;
108
 
 
109
 
        byte outbuf[] = new byte[count+1];
110
 
        int i;
111
 
        for (i=0; i<count; i++) {
112
 
            outbuf[i] = (byte)('0' + i);
113
 
            //outbuf[i] = System.out.println((byte)('0' + i);
114
 
        }
115
 
        outbuf[i++] = (byte)'x';
116
 
 
117
 
        /*
118
 
         System.out.println("byte: " + ('0' + 0) + ", after: " +
119
 
         (int)'0' + "=" + (int)('0' + 0) +
120
 
         "," + (byte)outbuf[0]);
121
 
         */
122
 
 
123
 
        Dbt key = new Dbt(outbuf, 0, i);
124
 
        Dbt data = new Dbt(outbuf, 0, i);
125
 
 
126
 
        //DEBUGOUT("Put: " + (char)outbuf[0] + ": " + new String(outbuf));
127
 
        db.put(null, key, data, Db.DB_NOOVERWRITE);
128
 
 
129
 
        // Acquire a cursor for the table.
130
 
        Dbc dbcp = db.cursor(null, 0);
131
 
 
132
 
        // Walk through the table, checking
133
 
        Dbt readkey = new Dbt();
134
 
        Dbt readdata = new Dbt();
135
 
        Dbt whoknows = new Dbt();
136
 
 
137
 
        readkey.set_flags(options.dbt_alloc_flags);
138
 
        readdata.set_flags(options.dbt_alloc_flags);
139
 
 
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");
148
 
            }
149
 
            len--;
150
 
            long bit = (1 << len);
151
 
            if (len > count) {
152
 
                ERR("reread length is bad: expect " + count + " got "+ len + " (" + key_string + ")" );
153
 
            }
154
 
            else if (!data_string.equals(key_string)) {
155
 
                ERR("key/data don't match");
156
 
            }
157
 
            else if ((bitmap & bit) != 0) {
158
 
                ERR("key already seen");
159
 
            }
160
 
            else if ((expected & bit) == 0) {
161
 
                ERR("key was not expected");
162
 
            }
163
 
            else {
164
 
                bitmap |= bit;
165
 
                expected &= ~(bit);
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)
170
 
                                         + "), wanted " + i
171
 
                                         + " (" + (int)('0' + i)
172
 
                                         + ") at position " + i + "\n");
173
 
                        ERR("key is corrupt");
174
 
                    }
175
 
                }
176
 
            }
177
 
        }
178
 
        if (expected != 0) {
179
 
            System.out.print(" expected more keys, bitmap is: " + expected + "\n");
180
 
            ERR("missing keys in database");
181
 
        }
182
 
        dbcp.close();
183
 
        db.close(0);
184
 
    }
185
 
 
186
 
    void t1(TestOptions options)
187
 
        throws DbException, FileNotFoundException
188
 
    {
189
 
        Db db = new Db(null, 0);
190
 
        rundb(db, itemcount++, false, options);
191
 
    }
192
 
 
193
 
    void t2(TestOptions options)
194
 
        throws DbException, FileNotFoundException
195
 
    {
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);
200
 
    }
201
 
 
202
 
    void t3(TestOptions options)
203
 
        throws DbException, FileNotFoundException
204
 
    {
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);
211
 
    }
212
 
 
213
 
    void t4(TestOptions options)
214
 
        throws DbException, FileNotFoundException
215
 
    {
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);
219
 
        /**/
220
 
            //rundb(db, itemcount++, true, options);
221
 
            db.set_errpfx("test4");
222
 
            rundb(db, itemcount++, true, options);
223
 
            /**/
224
 
            env.close(0);
225
 
    }
226
 
 
227
 
    void t5(TestOptions options)
228
 
        throws DbException, FileNotFoundException
229
 
    {
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);
236
 
        /*
237
 
        env.close(0);
238
 
 
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!
242
 
        */
243
 
        Db anotherdb = new Db(env, 0);
244
 
 
245
 
        //      rundb(anotherdb, itemcount++, true, options);
246
 
        anotherdb.set_errpfx("test5");
247
 
        rundb(anotherdb, itemcount++, true, options);
248
 
        env.close(0);
249
 
    }
250
 
 
251
 
    void t6(TestOptions options)
252
 
        throws DbException, FileNotFoundException
253
 
    {
254
 
        Db db = new Db(null, 0);
255
 
        DbEnv dbenv = new DbEnv(0);
256
 
        db.close(0);
257
 
        dbenv.close(0);
258
 
 
259
 
        System.gc();
260
 
        System.runFinalization();
261
 
    }
262
 
 
263
 
    // By design, t7 leaves a db and dbenv open; it should be detected.
264
 
    void t7(TestOptions options)
265
 
        throws DbException, FileNotFoundException
266
 
    {
267
 
        Db db = new Db(null, 0);
268
 
        DbEnv dbenv = new DbEnv(0);
269
 
 
270
 
        System.gc();
271
 
        System.runFinalization();
272
 
    }
273
 
 
274
 
    // remove any existing environment or database
275
 
    void removeall(boolean use_db)
276
 
    {
277
 
        {
278
 
            if (use_db) {
279
 
                try {
280
 
                    /**/
281
 
                    //memory leak for this:
282
 
                    Db tmpdb = new Db(null, 0);
283
 
                    tmpdb.remove(CONSTRUCT01_DBFULLPATH, null, 0);
284
 
                    /**/
285
 
                    DbEnv tmpenv = new DbEnv(0);
286
 
                    tmpenv.remove(CONSTRUCT01_DBDIR, Db.DB_FORCE);
287
 
                }
288
 
                catch (DbException dbe) {
289
 
                    System.err.println("error during remove: " + dbe);
290
 
                }
291
 
                catch (FileNotFoundException fnfe) {
292
 
                    //expected error:
293
 
                    // System.err.println("error during remove: " + fnfe);
294
 
                }
295
 
            }
296
 
        }
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);
301
 
        }
302
 
    }
303
 
 
304
 
    boolean doall(TestOptions options)
305
 
    {
306
 
        itemcount = 0;
307
 
        try {
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 + ":");
312
 
                    switch (item) {
313
 
                        case 1:
314
 
                            t1(options);
315
 
                            break;
316
 
                        case 2:
317
 
                            t2(options);
318
 
                            break;
319
 
                        case 3:
320
 
                            t3(options);
321
 
                            break;
322
 
                        case 4:
323
 
                            t4(options);
324
 
                            break;
325
 
                        case 5:
326
 
                            t5(options);
327
 
                            break;
328
 
                        case 6:
329
 
                            t6(options);
330
 
                            break;
331
 
                        case 7:
332
 
                            t7(options);
333
 
                            break;
334
 
                        default:
335
 
                            ERR("unknown test case: " + item);
336
 
                            break;
337
 
                    }
338
 
                    VERBOSEOUT("  finished.\n");
339
 
                }
340
 
            }
341
 
            removeall((options.testmask & 1) != 0);
342
 
            options.successcounter++;
343
 
            return true;
344
 
        }
345
 
        catch (DbException dbe) {
346
 
            ERR("EXCEPTION RECEIVED: " + dbe);
347
 
        }
348
 
        catch (FileNotFoundException fnfe) {
349
 
            ERR("EXCEPTION RECEIVED: " + fnfe);
350
 
        }
351
 
        return false;
352
 
    }
353
 
 
354
 
    public static void main(String args[])
355
 
    {
356
 
        int iterations = 200;
357
 
        int mask = 0x7f;
358
 
 
359
 
        // Make sure the database file is removed before we start.
360
 
        check_file_removed(CONSTRUCT01_DBFULLPATH, true, true);
361
 
 
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.
366
 
                mask = 1;
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'));
371
 
                    }
372
 
                    else if (ch == 'v') {
373
 
                        verbose_flag = true;
374
 
                    }
375
 
                    else {
376
 
                        ERR("Usage:  construct01 [-testdigits] count");
377
 
                    }
378
 
                }
379
 
                VERBOSEOUT("mask = " + mask);
380
 
 
381
 
            }
382
 
            else {
383
 
                try {
384
 
                    iterations = Integer.parseInt(arg);
385
 
                    if (iterations < 0) {
386
 
                        ERR("Usage:  construct01 [-testdigits] count");
387
 
                    }
388
 
                }
389
 
                catch (NumberFormatException nfe) {
390
 
                    ERR("EXCEPTION RECEIVED: " + nfe);
391
 
                }
392
 
            }
393
 
        }
394
 
 
395
 
        // Run GC before and after the test to give
396
 
        // a baseline for any Java memory used.
397
 
        //
398
 
        System.gc();
399
 
        System.runFinalization();
400
 
        VERBOSEOUT("gc complete");
401
 
        long starttotal = Runtime.getRuntime().totalMemory();
402
 
        long startfree = Runtime.getRuntime().freeMemory();
403
 
 
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" };
407
 
 
408
 
        TestOptions options = new TestOptions();
409
 
        options.testmask = mask;
410
 
 
411
 
        for (int flagiter = 0; flagiter < dbt_flags.length; flagiter++) {
412
 
            options.dbt_alloc_flags = dbt_flags[flagiter];
413
 
 
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 + ") ");
419
 
                }
420
 
                VERBOSEOUT("construct01 running:");
421
 
                if (!con.doall(options)) {
422
 
                    ERR("SOME TEST FAILED");
423
 
                }
424
 
                else {
425
 
                    VERBOSEOUT("\nTESTS SUCCESSFUL");
426
 
                }
427
 
 
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.
433
 
                //
434
 
                System.gc();
435
 
                System.runFinalization();
436
 
                VERBOSEOUT("gc complete");
437
 
            }
438
 
        }
439
 
 
440
 
        if (options.successcounter == 600) {
441
 
            System.out.println("ALL TESTS SUCCESSFUL");
442
 
        }
443
 
        else {
444
 
            System.out.println("***FAIL: " + (600 - options.successcounter) +
445
 
                               " tests did not complete");
446
 
        }
447
 
        long endtotal = Runtime.getRuntime().totalMemory();
448
 
        long endfree = Runtime.getRuntime().freeMemory();
449
 
 
450
 
        System.out.println("delta for total mem: " + magnitude(endtotal - starttotal));
451
 
        System.out.println("delta for free mem: " + magnitude(endfree - startfree));
452
 
 
453
 
        return;
454
 
    }
455
 
 
456
 
    static String magnitude(long value)
457
 
    {
458
 
        final long max = 10000000;
459
 
        for (long scale = 10; scale <= max; scale *= 10) {
460
 
            if (value < scale && value > -scale)
461
 
                return "<" + scale;
462
 
        }
463
 
        return ">" + max;
464
 
    }
465
 
 
466
 
}
467
 
 
468
 
class TestOptions
469
 
{
470
 
    int testmask = 0;           // which tests to run
471
 
    int dbt_alloc_flags = 0;    // DB_DBT_* flags to use
472
 
    int successcounter =0;
473
 
}
474