~ubuntu-branches/ubuntu/precise/rpm/precise-proposed

« back to all changes in this revision

Viewing changes to db/test/scr015/TestTruncate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-06-25 18:57:20 UTC
  • mfrom: (1.1.5 upstream) (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090625185720-617sjskgtgmf09vf
Tags: 4.7.0-7ubuntu1
* Merge from debian unstable, remaining changes:
  - change build depends from libdwarf-dev -> libdw-dev
    (libdwarf-dev is in universe)

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-2004
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id: TestTruncate.cpp,v 1.7 2004/01/28 03:36:34 bostic Exp $
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
 
#include <db_cxx.h>
18
 
#include <iostream.h>
19
 
 
20
 
int main(int argc, char *argv[])
21
 
{
22
 
        try {
23
 
                Db *db = new Db(NULL, 0);
24
 
                db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644);
25
 
 
26
 
                // populate our massive database.
27
 
                // all our strings include null for convenience.
28
 
                // Note we have to cast for idiomatic
29
 
                // usage, since newer gcc requires it.
30
 
                Dbt *keydbt = new Dbt((char*)"key", 4);
31
 
                Dbt *datadbt = new Dbt((char*)"data", 5);
32
 
                db->put(NULL, keydbt, datadbt, 0);
33
 
 
34
 
                // Now, retrieve.  We could use keydbt over again,
35
 
                // but that wouldn't be typical in an application.
36
 
                Dbt *goodkeydbt = new Dbt((char*)"key", 4);
37
 
                Dbt *badkeydbt = new Dbt((char*)"badkey", 7);
38
 
                Dbt *resultdbt = new Dbt();
39
 
                resultdbt->set_flags(DB_DBT_MALLOC);
40
 
 
41
 
                int ret;
42
 
 
43
 
                if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
44
 
                        cout << "get: " << DbEnv::strerror(ret) << "\n";
45
 
                }
46
 
                else {
47
 
                        char *result = (char *)resultdbt->get_data();
48
 
                        cout << "got data: " << result << "\n";
49
 
                }
50
 
 
51
 
                if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) {
52
 
                        // We expect this...
53
 
                        cout << "get using bad key: "
54
 
                             << DbEnv::strerror(ret) << "\n";
55
 
                }
56
 
                else {
57
 
                        char *result = (char *)resultdbt->get_data();
58
 
                        cout << "*** got data using bad key!!: "
59
 
                             << result << "\n";
60
 
                }
61
 
 
62
 
                // Now, truncate and make sure that it's really gone.
63
 
                cout << "truncating data...\n";
64
 
                u_int32_t nrecords;
65
 
                db->truncate(NULL, &nrecords, 0);
66
 
                cout << "truncate returns " << nrecords << "\n";
67
 
                if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
68
 
                        // We expect this...
69
 
                        cout << "after truncate get: "
70
 
                             << DbEnv::strerror(ret) << "\n";
71
 
                }
72
 
                else {
73
 
                        char *result = (char *)resultdbt->get_data();
74
 
                        cout << "got data: " << result << "\n";
75
 
                }
76
 
 
77
 
                db->close(0);
78
 
                cout << "finished test\n";
79
 
        }
80
 
        catch (DbException &dbe) {
81
 
                cerr << "Db Exception: " << dbe.what();
82
 
        }
83
 
        return 0;
84
 
}