1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: TestSimpleAccess.cpp,v 12.6 2008/01/08 20:58:54 bostic Exp $ 7 */ 8 9/* 10 * Do some regression tests for constructors. 11 * Run normally (without arguments) it is a simple regression test. 12 * Run with a numeric argument, it repeats the regression a number 13 * of times, to try to determine if there are memory leaks. 14 */ 15 16#include <db_cxx.h> 17#include <iostream.h> 18 19int main(int argc, char *argv[]) 20{ 21 try { 22 Db *db = new Db(NULL, 0); 23 db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644); 24 25 // populate our massive database. 26 // all our strings include null for convenience. 27 // Note we have to cast for idiomatic 28 // usage, since newer gcc requires it. 29 Dbt *keydbt = new Dbt((char *)"key", 4); 30 Dbt *datadbt = new Dbt((char *)"data", 5); 31 db->put(NULL, keydbt, datadbt, 0); 32 33 // Now, retrieve. We could use keydbt over again, 34 // but that wouldn't be typical in an application. 35 Dbt *goodkeydbt = new Dbt((char *)"key", 4); 36 Dbt *badkeydbt = new Dbt((char *)"badkey", 7); 37 Dbt *resultdbt = new Dbt(); 38 resultdbt->set_flags(DB_DBT_MALLOC); 39 40 int ret; 41 42 if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) { 43 cout << "get: " << DbEnv::strerror(ret) << "\n"; 44 } 45 else { 46 char *result = (char *)resultdbt->get_data(); 47 cout << "got data: " << result << "\n"; 48 } 49 50 if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) { 51 // We expect this... 52 cout << "get using bad key: " 53 << DbEnv::strerror(ret) << "\n"; 54 } 55 else { 56 char *result = (char *)resultdbt->get_data(); 57 cout << "*** got data using bad key!!: " 58 << result << "\n"; 59 } 60 cout << "finished test\n"; 61 } 62 catch (DbException &dbe) { 63 cerr << "Db Exception: " << dbe.what(); 64 } 65 return 0; 66} 67