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

« back to all changes in this revision

Viewing changes to libdb/test/scr015/TestConstruct01.cpp

  • 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
 
#ifndef NO_SYSTEM_INCLUDES
18
 
#include <sys/types.h>
19
 
 
20
 
#include <iostream.h>
21
 
#include <errno.h>
22
 
#include <stdlib.h>
23
 
#include <string.h>
24
 
#ifndef _MSC_VER
25
 
#include <unistd.h>
26
 
#endif
27
 
#endif
28
 
 
29
 
#include <iomanip.h>
30
 
#include <db_cxx.h>
31
 
 
32
 
#define ERR(a)  \
33
 
    do { \
34
 
      cout << "FAIL: " << (a) << "\n"; sysexit(1); \
35
 
    } while (0)
36
 
 
37
 
#define ERR2(a1,a2)  \
38
 
    do { \
39
 
      cout << "FAIL: " << (a1) << ": " << (a2) << "\n"; sysexit(1); \
40
 
    } while (0)
41
 
 
42
 
#define ERR3(a1,a2,a3)  \
43
 
    do { \
44
 
      cout << "FAIL: " << (a1) << ": " << (a2) << ": " << (a3) << "\n"; sysexit(1); \
45
 
    } while (0)
46
 
 
47
 
#define CHK(a)   \
48
 
    do { \
49
 
      int _ret; \
50
 
      if ((_ret = (a)) != 0) { \
51
 
         ERR3("DB function " #a " has bad return", _ret, DbEnv::strerror(_ret)); \
52
 
      } \
53
 
    } while (0)
54
 
 
55
 
#ifdef VERBOSE
56
 
#define DEBUGOUT(a)          cout << a << "\n"
57
 
#else
58
 
#define DEBUGOUT(a)
59
 
#endif
60
 
 
61
 
#define CONSTRUCT01_DBNAME         "construct01.db"
62
 
#define CONSTRUCT01_DBDIR          "."
63
 
#define CONSTRUCT01_DBFULLPATH     (CONSTRUCT01_DBDIR "/" CONSTRUCT01_DBNAME)
64
 
 
65
 
int itemcount;                  // count the number of items in the database
66
 
 
67
 
// A good place to put a breakpoint...
68
 
//
69
 
void sysexit(int status)
70
 
{
71
 
        exit(status);
72
 
}
73
 
 
74
 
void check_file_removed(const char *name, int fatal)
75
 
{
76
 
        unlink(name);
77
 
#if 0
78
 
        if (access(name, 0) == 0) {
79
 
                if (fatal)
80
 
                        cout << "FAIL: ";
81
 
                cout << "File \"" << name << "\" still exists after run\n";
82
 
                if (fatal)
83
 
                        sysexit(1);
84
 
        }
85
 
#endif
86
 
}
87
 
 
88
 
// Check that key/data for 0 - count-1 are already present,
89
 
// and write a key/data for count.  The key and data are
90
 
// both "0123...N" where N == count-1.
91
 
//
92
 
// For some reason on Windows, we need to open using the full pathname
93
 
// of the file when there is no environment, thus the 'has_env'
94
 
// variable.
95
 
//
96
 
void rundb(Db *db, int count, int has_env)
97
 
{
98
 
        const char *name;
99
 
 
100
 
        if (has_env)
101
 
                name = CONSTRUCT01_DBNAME;
102
 
        else
103
 
                name = CONSTRUCT01_DBFULLPATH;
104
 
 
105
 
        db->set_error_stream(&cerr);
106
 
 
107
 
        // We don't really care about the pagesize, but we do want
108
 
        // to make sure adjusting Db specific variables works before
109
 
        // opening the db.
110
 
        //
111
 
        CHK(db->set_pagesize(1024));
112
 
        CHK(db->open(NULL, name, NULL, DB_BTREE, count ? 0 : DB_CREATE, 0664));
113
 
 
114
 
        // The bit map of keys we've seen
115
 
        long bitmap = 0;
116
 
 
117
 
        // The bit map of keys we expect to see
118
 
        long expected = (1 << (count+1)) - 1;
119
 
 
120
 
        char outbuf[10];
121
 
        int i;
122
 
        for (i=0; i<count; i++) {
123
 
                outbuf[i] = '0' + i;
124
 
        }
125
 
        outbuf[i++] = '\0';
126
 
        Dbt key(outbuf, i);
127
 
        Dbt data(outbuf, i);
128
 
 
129
 
        DEBUGOUT("Put: " << outbuf);
130
 
        CHK(db->put(0, &key, &data, DB_NOOVERWRITE));
131
 
 
132
 
        // Acquire a cursor for the table.
133
 
        Dbc *dbcp;
134
 
        CHK(db->cursor(NULL, &dbcp, 0));
135
 
 
136
 
        // Walk through the table, checking
137
 
        Dbt readkey;
138
 
        Dbt readdata;
139
 
        while (dbcp->get(&readkey, &readdata, DB_NEXT) == 0) {
140
 
                char *key_string = (char *)readkey.get_data();
141
 
                char *data_string = (char *)readdata.get_data();
142
 
                DEBUGOUT("Got: " << key_string << ": " << data_string);
143
 
                int len = strlen(key_string);
144
 
                long bit = (1 << len);
145
 
                if (len > count) {
146
 
                        ERR("reread length is bad");
147
 
                }
148
 
                else if (strcmp(data_string, key_string) != 0) {
149
 
                        ERR("key/data don't match");
150
 
                }
151
 
                else if ((bitmap & bit) != 0) {
152
 
                        ERR("key already seen");
153
 
                }
154
 
                else if ((expected & bit) == 0) {
155
 
                        ERR("key was not expected");
156
 
                }
157
 
                else {
158
 
                        bitmap |= bit;
159
 
                        expected &= ~(bit);
160
 
                        for (i=0; i<len; i++) {
161
 
                                if (key_string[i] != ('0' + i)) {
162
 
                                        cout << " got " << key_string
163
 
                                             << " (" << (int)key_string[i] << ")"
164
 
                                             << ", wanted " << i
165
 
                                             << " (" << (int)('0' + i) << ")"
166
 
                                             << " at position " << i << "\n";
167
 
                                        ERR("key is corrupt");
168
 
                                }
169
 
                        }
170
 
                }
171
 
        }
172
 
        if (expected != 0) {
173
 
                cout << " expected more keys, bitmap is: " << expected << "\n";
174
 
                ERR("missing keys in database");
175
 
        }
176
 
        CHK(dbcp->close());
177
 
        CHK(db->close(0));
178
 
}
179
 
 
180
 
void t1(int except_flag)
181
 
{
182
 
        cout << "  Running test 1:\n";
183
 
        Db db(0, except_flag);
184
 
        rundb(&db, itemcount++, 0);
185
 
        cout << "  finished.\n";
186
 
}
187
 
 
188
 
void t2(int except_flag)
189
 
{
190
 
        cout << "  Running test 2:\n";
191
 
        Db db(0, except_flag);
192
 
        rundb(&db, itemcount++, 0);
193
 
        cout << "  finished.\n";
194
 
}
195
 
 
196
 
void t3(int except_flag)
197
 
{
198
 
        cout << "  Running test 3:\n";
199
 
        Db db(0, except_flag);
200
 
        rundb(&db, itemcount++, 0);
201
 
        cout << "  finished.\n";
202
 
}
203
 
 
204
 
void t4(int except_flag)
205
 
{
206
 
        cout << "  Running test 4:\n";
207
 
        DbEnv env(except_flag);
208
 
        CHK(env.open(CONSTRUCT01_DBDIR, DB_CREATE | DB_INIT_MPOOL, 0));
209
 
        Db db(&env, 0);
210
 
        CHK(db.close(0));
211
 
        CHK(env.close(0));
212
 
        cout << "  finished.\n";
213
 
}
214
 
 
215
 
void t5(int except_flag)
216
 
{
217
 
        cout << "  Running test 5:\n";
218
 
        DbEnv env(except_flag);
219
 
        CHK(env.open(CONSTRUCT01_DBDIR, DB_CREATE | DB_INIT_MPOOL, 0));
220
 
        Db db(&env, 0);
221
 
        rundb(&db, itemcount++, 1);
222
 
        // Note we cannot reuse the old Db!
223
 
        Db anotherdb(&env, 0);
224
 
 
225
 
        anotherdb.set_errpfx("test5");
226
 
        rundb(&anotherdb, itemcount++, 1);
227
 
        CHK(env.close(0));
228
 
        cout << "  finished.\n";
229
 
}
230
 
 
231
 
void t6(int except_flag)
232
 
{
233
 
        cout << "  Running test 6:\n";
234
 
 
235
 
        /* From user [#2939] */
236
 
        int err;
237
 
 
238
 
        DbEnv* penv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
239
 
        penv->set_cachesize(0, 32 * 1024, 0);
240
 
        penv->open(CONSTRUCT01_DBDIR, DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL, 0);
241
 
 
242
 
        //LEAK: remove this block and leak disappears
243
 
        Db* pdb = new Db(penv,0);
244
 
        if ((err = pdb->close(0)) != 0) {
245
 
                fprintf(stderr, "Error closing Db: %s\n", db_strerror(err));
246
 
        }
247
 
        delete pdb;
248
 
        //LEAK: remove this block and leak disappears
249
 
 
250
 
        if ((err = penv->close(0)) != 0) {
251
 
                fprintf(stderr, "Error closing DbEnv: %s\n", db_strerror(err));
252
 
        }
253
 
        delete penv;
254
 
 
255
 
        // Make sure we get a message from C++ layer reminding us to close.
256
 
        cerr << "expected error: ";
257
 
        {
258
 
                DbEnv foo(DB_CXX_NO_EXCEPTIONS);
259
 
                foo.open(CONSTRUCT01_DBDIR, DB_CREATE, 0);
260
 
        }
261
 
        cerr << "should have received error.\n";
262
 
        cout << "  finished.\n";
263
 
}
264
 
 
265
 
// remove any existing environment or database
266
 
void removeall()
267
 
{
268
 
    {
269
 
        DbEnv tmpenv(DB_CXX_NO_EXCEPTIONS);
270
 
        (void)tmpenv.remove(CONSTRUCT01_DBDIR, DB_FORCE);
271
 
    }
272
 
 
273
 
        check_file_removed(CONSTRUCT01_DBFULLPATH, 1);
274
 
        for (int i=0; i<8; i++) {
275
 
                char buf[20];
276
 
                sprintf(buf, "__db.00%d", i);
277
 
                check_file_removed(buf, 1);
278
 
        }
279
 
}
280
 
 
281
 
int doall(int except_flag)
282
 
{
283
 
        itemcount = 0;
284
 
        try {
285
 
                // before and after the run, removing any
286
 
                // old environment/database.
287
 
                //
288
 
                removeall();
289
 
                t1(except_flag);
290
 
                t2(except_flag);
291
 
                t3(except_flag);
292
 
                t4(except_flag);
293
 
                t5(except_flag);
294
 
                t6(except_flag);
295
 
 
296
 
                removeall();
297
 
                return 0;
298
 
        }
299
 
        catch (DbException &dbe) {
300
 
                ERR2("EXCEPTION RECEIVED", dbe.what());
301
 
        }
302
 
        return 1;
303
 
}
304
 
 
305
 
int main(int argc, char *argv[])
306
 
{
307
 
        int iterations = 1;
308
 
        if (argc > 1) {
309
 
                iterations = atoi(argv[1]);
310
 
                if (iterations < 0) {
311
 
                        ERR("Usage:  construct01 count");
312
 
                }
313
 
        }
314
 
        for (int i=0; i<iterations; i++) {
315
 
                if (iterations != 0) {
316
 
                        cout << "(" << i << "/" << iterations << ") ";
317
 
                }
318
 
                cout << "construct01 running:\n";
319
 
                if (doall(DB_CXX_NO_EXCEPTIONS) != 0) {
320
 
                        ERR("SOME TEST FAILED FOR NO-EXCEPTION TEST");
321
 
                }
322
 
                else if (doall(0) != 0) {
323
 
                        ERR("SOME TEST FAILED FOR EXCEPTION TEST");
324
 
                }
325
 
                else {
326
 
                        cout << "\nALL TESTS SUCCESSFUL\n";
327
 
                }
328
 
        }
329
 
        return 0;
330
 
}