hash_db/test/test1.cpp

Go to the documentation of this file.
00001 /*
00002  * test_hash_db.cpp
00003  *
00004  * Copyright (C) 2006,2010  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 
00015 #include "perf/perf.h"
00016 
00017 
00018 static const char * s_testDbFilename            = "test-db-AUTOGEN.txt";
00019 
00020 
00021 ////////////////////////////////////////////////////////////////////////////////
00022 //
00023 //      static helper methods
00024 //
00025 ////////////////////////////////////////////////////////////////////////////////
00026 
00027 static smart_ptr<Datahash>
00028 getBogusHash
00029 (
00030 IN const char * text
00031 )
00032 {
00033         ASSERT(text, "null");
00034 
00035         smart_ptr<Datahash> h = Datahash::create();
00036         ASSERT(h, "Failed to create empty bogus hash");
00037 
00038         h->insert("foo", "bar");
00039         h->insert("foo", "bar2");
00040         h->insert("x", "y");
00041         h->insert("alpha", "beta");
00042         h->insert("text", text);
00043 
00044         return h;
00045 }
00046 
00047 
00048 
00049 static void
00050 doTest
00051 (
00052 IN const char * filename
00053 )
00054 {
00055         ASSERT(filename, "null");
00056 
00057         smart_ptr<hash_db::Database> db = hash_db::Database::create(filename);
00058         ASSERT(db, "null hash");
00059 
00060         smart_ptr<Datahash> h1 = getBogusHash("first bogus hash");
00061         ASSERT(db->addObject("x", h1),
00062             "Should have succeeded");
00063 
00064         smart_ptr<Datahash> h2 = getBogusHash("second bogus hash");
00065         ASSERT(db->addObject("y", h2),
00066             "Should have succeeded");
00067 
00068         ASSERT(!db->addObject("y", h2),
00069             "Should have failed");
00070 
00071         SetString ids;
00072         db->getIds(ids);
00073         for (SetString::const_iterator i = ids.begin(); i != ids.end(); ++i) {
00074                 DPRINTF("Hash database contains object with id='%s'",
00075                     i->c_str());
00076         }
00077 
00078         ASSERT(db->deleteObject("x"),
00079             "Should have succeeded");
00080 
00081         ASSERT(!db->deleteObject("x"),
00082             "Should have failed");
00083 
00084         db->commit();
00085 
00086         // TODO: need to automate multi-process access!
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 #ifdef WIN32
00109         DPRINTF("Quitting test early: hash_db not supported in Win32");
00110         return 0;
00111 #endif  // WIN32
00112 
00113         int retval = 0;
00114         try {
00115                 doTest(filename);
00116 
00117                 // clean up the file
00118                 std::string lockfile = filename;
00119                 lockfile += ".lock";
00120 
00121                 remove(filename);
00122                 remove(lockfile.c_str());
00123 
00124         } catch (std::exception& e) {
00125                 DPRINTF("Exception: %s", e.what());
00126                 retval = 1;
00127         }
00128 
00129         perf::dumpTimingSummary(std::cerr);
00130 
00131         // all done
00132         return retval;
00133 }
00134