2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2000-2004
5
* Sleepycat Software. All rights reserved.
7
* $Id: TestTruncate.cpp,v 1.7 2004/01/28 03:36:34 bostic Exp $
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.
20
int main(int argc, char *argv[])
23
Db *db = new Db(NULL, 0);
24
db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644);
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);
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);
43
if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
44
cout << "get: " << DbEnv::strerror(ret) << "\n";
47
char *result = (char *)resultdbt->get_data();
48
cout << "got data: " << result << "\n";
51
if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) {
53
cout << "get using bad key: "
54
<< DbEnv::strerror(ret) << "\n";
57
char *result = (char *)resultdbt->get_data();
58
cout << "*** got data using bad key!!: "
62
// Now, truncate and make sure that it's really gone.
63
cout << "truncating data...\n";
65
db->truncate(NULL, &nrecords, 0);
66
cout << "truncate returns " << nrecords << "\n";
67
if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
69
cout << "after truncate get: "
70
<< DbEnv::strerror(ret) << "\n";
73
char *result = (char *)resultdbt->get_data();
74
cout << "got data: " << result << "\n";
78
cout << "finished test\n";
80
catch (DbException &dbe) {
81
cerr << "Db Exception: " << dbe.what();