Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TestSimpleAccess.cpp

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TestSimpleAccess.cpp,v 12.1 2005/06/16 20:24:11 bostic Exp $
00008  */
00009 
00010 /*
00011  * Do some regression tests for constructors.
00012  * Run normally (without arguments) it is a simple regression test.
00013  * Run with a numeric argument, it repeats the regression a number
00014  * of times, to try to determine if there are memory leaks.
00015  */
00016 
00017 #include <db_cxx.h>
00018 #include <iostream.h>
00019 
00020 int main(int argc, char *argv[])
00021 {
00022         try {
00023                 Db *db = new Db(NULL, 0);
00024                 db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644);
00025 
00026                 // populate our massive database.
00027                 // all our strings include null for convenience.
00028                 // Note we have to cast for idiomatic
00029                 // usage, since newer gcc requires it.
00030                 Dbt *keydbt = new Dbt((char *)"key", 4);
00031                 Dbt *datadbt = new Dbt((char *)"data", 5);
00032                 db->put(NULL, keydbt, datadbt, 0);
00033 
00034                 // Now, retrieve.  We could use keydbt over again,
00035                 // but that wouldn't be typical in an application.
00036                 Dbt *goodkeydbt = new Dbt((char *)"key", 4);
00037                 Dbt *badkeydbt = new Dbt((char *)"badkey", 7);
00038                 Dbt *resultdbt = new Dbt();
00039                 resultdbt->set_flags(DB_DBT_MALLOC);
00040 
00041                 int ret;
00042 
00043                 if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
00044                         cout << "get: " << DbEnv::strerror(ret) << "\n";
00045                 }
00046                 else {
00047                         char *result = (char *)resultdbt->get_data();
00048                         cout << "got data: " << result << "\n";
00049                 }
00050 
00051                 if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) {
00052                         // We expect this...
00053                         cout << "get using bad key: "
00054                              << DbEnv::strerror(ret) << "\n";
00055                 }
00056                 else {
00057                         char *result = (char *)resultdbt->get_data();
00058                         cout << "*** got data using bad key!!: "
00059                              << result << "\n";
00060                 }
00061                 cout << "finished test\n";
00062         }
00063         catch (DbException &dbe) {
00064                 cerr << "Db Exception: " << dbe.what();
00065         }
00066         return 0;
00067 }

Generated on Sun Dec 25 12:14:52 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2