test_hash_db.cpp

Go to the documentation of this file.
00001 /*
00002  * test_hash_db.cpp
00003  *
00004  * Copyright (C) 2006  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Tests the hash_db::Database objects
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "hash_db/hash_db.h"
00012 
00013 #include <iostream>
00014 #include <unistd.h>             // sleep()
00015 
00016 #include "perf/perf.h"
00017 
00018 
00019 static const char * s_testDbFilename            = "test-db-AUTOGEN.txt";
00020 
00021 
00022 ////////////////////////////////////////////////////////////////////////////////
00023 //
00024 //      static helper methods
00025 //
00026 ////////////////////////////////////////////////////////////////////////////////
00027 
00028 static smart_ptr<Datahash>
00029 getBogusHash
00030 (
00031 IN const char * text
00032 )
00033 {
00034         ASSERT(text, "null");
00035 
00036         smart_ptr<Datahash> h = Datahash::create();
00037         ASSERT(h, "Failed to create empty bogus hash");
00038 
00039         h->insert("foo", "bar");
00040         h->insert("foo", "bar2");
00041         h->insert("x", "y");
00042         h->insert("alpha", "beta");
00043         h->insert("text", text);
00044 
00045         return h;
00046 }
00047 
00048 
00049 
00050 static void
00051 doTest
00052 (
00053 IN const char * filename
00054 )
00055 {
00056         ASSERT(filename, "null");
00057 
00058         smart_ptr<hash_db::Database> db = hash_db::Database::create(filename);
00059         ASSERT(db, "null hash");
00060 
00061         smart_ptr<Datahash> h1 = getBogusHash("first bogus hash");
00062         ASSERT(db->addObject("x", h1),
00063             "Should have succeeded");
00064 
00065         smart_ptr<Datahash> h2 = getBogusHash("second bogus hash");
00066         ASSERT(db->addObject("y", h2),
00067             "Should have succeeded");
00068 
00069         ASSERT(!db->addObject("y", h2),
00070             "Should have failed");
00071 
00072         SetString ids;
00073         db->getIds(ids);
00074         for (SetString::const_iterator i = ids.begin(); i != ids.end(); ++i) {
00075                 DPRINTF("Hash database contains object with id='%s'",
00076                     i->c_str());
00077         }
00078 
00079         ASSERT(db->deleteObject("x"),
00080             "Should have succeeded");
00081 
00082         ASSERT(!db->deleteObject("x"),
00083             "Should have failed");
00084 
00085         db->commit();
00086 
00087         DPRINTF("Sleeping for 15 seconds so you can test multi-process access.");
00088         sleep(15);
00089 }
00090 
00091 
00092 
00093 ////////////////////////////////////////////////////////////////////////////////
00094 //
00095 //      entry point
00096 //
00097 ////////////////////////////////////////////////////////////////////////////////
00098 
00099 int
00100 main
00101 (
00102 IN int argc,
00103 IN const char * argv[]
00104 )
00105 {
00106         const char * filename = s_testDbFilename;
00107 
00108         int retval = 0;
00109         try {
00110                 doTest(filename);
00111 
00112                 // clean up the file
00113                 std::string lockfile = filename;
00114                 lockfile += ".lock";
00115 
00116                 remove(filename);
00117                 remove(lockfile.c_str());
00118 
00119         } catch (std::exception& e) {
00120                 DPRINTF("Exception: %s", e.what());
00121                 retval = 1;
00122         }
00123 
00124         perf::dumpTimingSummary(std::cerr);
00125 
00126         // all done
00127         return retval;
00128 }
00129