test_bdb.cpp
来自「ncbi源码」· C++ 代码 · 共 2,043 行 · 第 1/3 页
CPP
2,043 行
assert(ret == eBDB_Ok); if (strcmp(buf, test_data) != 0) { cout << "BLOB content comparison error!" << endl; cout << "BLobData:" << buf << endl; assert(0); } buf[0] = 0; cout << "Testing BLOB based cursor" << endl; CBDB_FileCursor cur(blob); cur.SetCondition(CBDB_FileCursor::eEQ); cur.From << 1; const char* tdata = test_data; while (cur.Fetch() == eBDB_Ok) { assert(blob.i2 == 2 || blob.i2 == 3 || blob.i2 == 4); unsigned len = blob.LobSize(); ret = blob.GetData(buf, sizeof(buf)); assert(ret == eBDB_Ok); if (strcmp(buf, tdata) != 0) { int i2 = blob.i2; if (i2 == 4) { assert(strcmp(buf, test_data) ==0); } else { cout << "BLOB content comparison error!" << endl; cout << "BLobData:" << buf << endl; assert(0); } } cout << "Lob len=" << len << endl; tdata = test_data2; } cout << "Testing read stream" << endl; blob.i1 = 1; blob.i2 = 2; CBDB_BLobStream* bstream = blob.CreateStream(); char ch; size_t bytes_read = 0; size_t pending = bstream->PendingCount(); for(int i=0;;++i) { bstream->Read(&ch, 1, &bytes_read); if (bytes_read == 0) break; assert(ch == test_data[i]); size_t pend = bstream->PendingCount(); assert(pend == pending - 1); pending = pend; } delete bstream; cout << "======== BLob file test ok." << endl;}////////////////////////////////////////////////////////////////////// // Id table record delete test////static void s_TEST_BDB_IdTable_DeleteInsert(void){ cout << "======== BDB Id Table Delete/Insert test." << endl; TestDBF1 dbf1; dbf1.Open(s_TestFileName, CBDB_File::eReadWrite); unsigned int search_id = s_RecsInTable / 2; dbf1.IdKey = search_id; EBDB_ErrCode err = dbf1.Delete(); assert(err == eBDB_Ok); dbf1.IdKey = search_id; err = dbf1.Fetch(); assert(err == eBDB_NotFound); dbf1.IdKey = search_id; dbf1.idata = 800; dbf1.str = "123456789"; dbf1.i2.Set(300); dbf1.Insert(); dbf1.IdKey = search_id; err = dbf1.Fetch(); assert(err == eBDB_Ok); // Test UpdateInsert dbf1.Reopen(CBDB_File::eReadWrite); dbf1.IdKey = 2; dbf1.idata = 250; dbf1.str = "test"; err = dbf1.UpdateInsert(); assert(err == eBDB_Ok); dbf1.IdKey = 2; dbf1.idata = 0; err = dbf1.Fetch(); assert(err == eBDB_Ok); int idata = dbf1.idata; assert(idata == 250); dbf1.Close(); cout << "======== BDB Id Table Delete/Insert test ok." << endl;}////////////////////////////////////////////////////////////////////// // BDB string key table fill test////static void s_TEST_BDB_StrTable_Fill(void){ cout << "======== StrKey table filling test." << endl; TestDBF2 dbf; dbf.Open(s_TestFileName2, CBDB_File::eCreate); // Fill the table unsigned i; for (i = 1; i < s_RecsInTable; ++i) { char buf[256]; sprintf(buf, s_StrKeyTempl, i, i); dbf.str_key = buf; dbf.idata = 400+i; sprintf(buf, s_TestStrTempl, i); dbf.str = buf; dbf.i2.Set(i+3); int i2 = dbf.i2; assert (i2 == (int)(i+3)); EBDB_ErrCode err = dbf.Insert(); assert(err == eBDB_Ok); } cout << "Table " << s_TestFileName2 << " loaded ok. Checking consistency." << endl; // Read the table check if all records are in place dbf.Reopen(CBDB_File::eReadOnly); for (i = 1; i < s_RecsInTable; ++i) { char buf[256]; sprintf(buf, s_StrKeyTempl, i, i); dbf.str_key = buf; EBDB_ErrCode err = dbf.Fetch(); assert (err == eBDB_Ok); ValidateRecord(dbf, i); } // for cout << "======== StrKey table filling test ok." << endl;}////////////////////////////////////////////////////////////////////// // Database record structure to test with duplicate records////struct TestDuplicateKeys : public CBDB_File{ CBDB_FieldInt4 idata; CBDB_FieldString str; TestDuplicateKeys() : CBDB_File(CBDB_File::eDuplicatesEnable) { BindKey("idata", &idata); BindData("str", &str, 256); }};////////////////////////////////////////////////////////////////////// // BDB string key table fill test////static void s_TEST_BDB_Duplicates(void){ cout << "======== Duplicate keys test." << endl; TestDuplicateKeys dbf; dbf.Open(s_TestFileName3, CBDB_File::eCreate); // Fill the table dbf.idata = 1; dbf.str = "Str1"; EBDB_ErrCode ret = dbf.Insert(); assert (ret == eBDB_Ok); dbf.idata = 1; dbf.str = "Str11"; ret = dbf.Insert(); assert (ret == eBDB_Ok); dbf.idata = 10; dbf.str = "Str100"; ret = dbf.Insert(); assert (ret == eBDB_Ok); {{ CBDB_FileCursor cur(dbf); cur.SetCondition(CBDB_FileCursor::eEQ); cur.From << 1; unsigned int recs_fetched = 0; while (cur.Fetch() == eBDB_Ok) { unsigned idata = dbf.idata; assert(idata == 1); string str = (const char*)dbf.str; assert(str == "Str1" || str == "Str11"); ++recs_fetched; } assert(recs_fetched == 2); }} dbf.idata = 1; ret = dbf.Delete(); assert (ret == eBDB_Ok); {{ CBDB_FileCursor cur(dbf); cur.SetCondition(CBDB_FileCursor::eEQ); cur.From << 1; unsigned int recs_fetched = 0; while (cur.Fetch() == eBDB_Ok) { assert(0); ++recs_fetched; } assert(recs_fetched == 0); }} cout << "======== Duplicate keys test ok." << endl;}////////////////////////////////////////////////////////////////////// // db_map test////bool CheckMapDataValid_i2s(int first, const string& second){ char szBuf[256]; sprintf(szBuf, "Data%i", first); return second == string(szBuf);}static void s_TEST_db_map(void){ cout << "======== db_map test." << endl; db_map<int, string> i2s; i2s.open(s_db_map1, ios_base::out|ios_base::trunc); i2s.insert(pair<const int, string>(1, "Data1")); i2s.insert(pair<const int, string>(2, "Data2")); i2s.insert(pair<const int, string>(3, "Data3")); string v = i2s[2]; assert(v == "Data2"); {{ db_map<int, string>::const_iterator it(i2s.begin()); while (it.valid()) { bool b = CheckMapDataValid_i2s((*it).first, (*it).second); assert(b); ++it; } }} {{ db_map<int, string>::const_iterator it(i2s.begin()); db_map<int, string>::const_iterator it_end(i2s.end()); for (;it != it_end; ++it) { bool b = CheckMapDataValid_i2s(it->first, it->second); assert(b); } }} i2s.clear(); size_t sz = i2s.size(); assert(sz == 0); // testing string -> int case db_map<string, int> s2i; s2i.open("s2i.db", ios_base::out|ios_base::trunc); s2i.insert(pair<const string, int>("Data1", 1)); s2i.insert(pair<const string, int>("Data2", 2)); s2i.insert(pair<const string, int>("Data3", 3)); int i = s2i[string("Data2")]; assert(i == 2); cout << "======== db_map test ok." << endl;}static void s_TEST_db_multimap(void){ cout << "======== db_multimap test." << endl; db_multimap<int, string> ii2s; ii2s.open(s_db_map2, ios_base::out|ios_base::trunc); ii2s.insert(pair<const int, string>(1, "Data1")); ii2s.insert(pair<const int, string>(2, "Data2")); ii2s.insert(pair<const int, string>(3, "Data3")); ii2s.insert(pair<const int, string>(3, "Data31")); size_t sz = ii2s.size(); assert(sz == 4); sz = 0; {{ db_multimap<int, string>::const_iterator it(ii2s.find(3)); while (it.valid()) { const string& data = it->second; if (sz == 0) { assert(data == "Data3"); } else { assert(data == "Data31"); } ++sz; ++it; } }} assert(sz == 2); ii2s.erase(3); sz = ii2s.size(); assert(sz == 2); cout << "======== db_multimap test ok." << endl;}static void s_TEST_ICache(void){ cout << "======== ICache test." << endl; vector<int> data; data.push_back(10); data.push_back(20); data.push_back(30); data.push_back(40); const void* dp = &data[0]; CBDB_Cache bdb_cache; int top = bdb_cache.GetTimeStampPolicy(); bdb_cache.SetTimeStampPolicy(top, 30); bdb_cache.Open(".", "bcache", CBDB_Cache::eNoLock, 1024*1024 * 100); bdb_cache.Store("test_key1", 1, "", dp, data.size() * sizeof(int)); vector<int> data2; size_t sz = bdb_cache.GetSize("test_key1", 1, ""); assert(sz); sz = sz / sizeof(int); assert(sz == data.size()); data2.resize(sz); void* dp2 = &data2[0]; bool res = bdb_cache.Read("test_key1", 1, "", dp2, sz * sizeof(int)); assert(data2[0] == data[0]); assert(data2[1] == data[1]); assert(data2[2] == data[2]); assert(data2[3] == data[3]);}////////////////////////////////// Test application//class CBDB_Test : public CNcbiApplication{public: void Init(void); int Run(void);};void CBDB_Test::Init(void){ SetDiagPostLevel(eDiag_Warning); SetDiagPostFlag(eDPF_File); SetDiagPostFlag(eDPF_Line); auto_ptr<CArgDescriptions> d(new CArgDescriptions); d->SetUsageContext("test_bdb", "test BDB library"); SetupArgDescriptions(d.release());}int CBDB_Test::Run(void){ cout << "Run BDB test" << endl << endl; try { s_TEST_BDB_Types(); s_TEST_BDB_IdTable_Fill(); // s_TEST_BDB_IdTable_Fill2(); s_TEST_BDB_IdTable_Cursor(); s_TEST_BDB_IdTable_DeleteInsert(); s_TEST_BDB_LOB_File(); s_TEST_BDB_BLOB_File(); s_TEST_BDB_StrTable_Fill(); s_TEST_BDB_Duplicates(); s_TEST_BDB_Transaction(); s_TEST_db_map(); s_TEST_db_multimap(); s_TEST_ICache(); s_TEST_BDB_Query(); s_TEST_BDB_IdTable_FillStress(); } catch (CBDB_ErrnoException& ex) { cout << "Error! DBD errno exception:" << ex.what(); return 1; } catch (CBDB_LibException& ex) { cout << "Error! DBD library exception:" << ex.what(); return 1; } cout << endl; cout << "TEST execution completed successfully!" << endl << endl; return 0;}///////////////////////////////////// APPLICATION OBJECT and MAIN//int main(int argc, const char* argv[]){ // Execute main application function return CBDB_Test().AppMain(argc, argv, 0, eDS_Default, 0);}/* * =========================================================================== * $Log: test_bdb.cpp,v $ * Revision 1000.5 2004/06/01 18:37:53 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.46 * * Revision 1.46 2004/05/25 18:48:51 kuznets * Added cache RAM size parameter to CBDB_Cache::Open. * * Revision 1.45 2004/05/17 20:55:27 gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.44 2004/04/28 12:18:27 kuznets * Remove obsolete IntCache test * * Revision 1.43 2004/03/23 16:37:55 kuznets * Implemented NOT predicate * * Revision 1.42 2004/03/23 14:51:19 kuznets * Implemented logical NOT, <, <=, >, >= * * Revision 1.41 2004/03/12 12:41:50 kuznets * + stress test for cursors * * Revision 1.40 2004/03/10 16:22:22 kuznets * + more tests * * Revision 1.39 2004/03/10 16:19:55 kuznets * + test case for single word queries * * Revision 1.38 2004/03/08 13:36:09 kuznets * One more test case for queries * * Revision 1.37 2004/03/01 14:07:11 kuznets * + test case for queries * * Revision 1.36 2004/02/04 19:16:50 kuznets * Fixed compilation bug (Workshop) * * Revision 1.35 2004/02/04 17:05:47 kuznets * Minor fix (compilation). * * Revision 1.34 2004/02/04 15:13:11 kuznets * + test case for length prefixed strings, when source std::string * contains zero chars. * * Revision 1.33 2003/12/23 22:32:24 ucko * Rename the second s_TEST_BDB_IdTable_Fill, but don't run it yet * because it crashes. :-/ * * Revision 1.32 2003/12/22 18:58:02 kuznets * Added l-string test * * Revision 1.31 2003/12/12 19:13:31 kuznets * Transaction test: minor change file opening option. * * Revision 1.30 2003/12/12 14:09:53 kuznets * + s_TEST_BDB_Transaction() * * Revision 1.29 2003/11/26 13:09:44 kuznets * Re-enables icache test * * Revision 1.28 2003/11/25 19:58:37 kuznets * Temporary disabled ICache test (fails on Solaris) * * Revision 1.27 2003/11/25 19:36:54 kuznets * + test for ICache implementation * * Revision 1.26 2003/11/14 04:31:10 ucko * bytes_read should be size_t, not unsigned. * * Revision 1.25 2003/10/27 14:21:15 kuznets * + DBD dumper test * * Revision 1.24 2003/10/24 13:41:51 kuznets * Tested blob stream PendingCount * * Revision 1.23 2003/10/20 20:15:54 kuznets * Cache test improved * * Revision 1.22 2003/10/20 19:59:03 kuznets * + Unit test for Int cache * * Revision 1.21 2003/10/15 18:14:33 kuznets * Changed test to work with alternative page size and larger cache. * * Revision 1.20 2003/09/29 16:54:58 kuznets * Reverting unnecessary commit * * Revision 1.19 2003/09/29 16:27:07 kuznets * Cleaned up 64-bit compilation warnings * * Revision 1.18 2003/09/17 18:19:02 kuznets * Added test for BLOB streaming * * Revision 1.17 2003/09/16 15:15:16 kuznets * Test corrected to use Int2 field * * Revision 1.16 2003/08/27 20:05:33 kuznets * Added test working with file locking environment * * Revision 1.15 2003/07/25 15:35:59 kuznets * Added simple db_map<string, int> test * * Revision 1.14 2003/07/24 15:44:44 kuznets * Clened up several compiler warnings * * Revision 1.13 2003/07/23 20:23:37 kuznets * + test for clean, erase (db_map) * * Revision 1.12 2003/07/22 19:21:56 kuznets * Added test case for CBDB_File::Attach function * * Revision 1.11 2003/07/22 16:38:30 kuznets * Polishing test * * Revision 1.10 2003/07/22 15:21:17 kuznets * Sketched two tet cases for db_map and db_multimap * * Revision 1.9 2003/07/09 14:29:47 kuznets * Added DB_DUP mode test. * * Revision 1.8 2003/05/27 18:05:08 kuznets * Fixed compilation warnings * * Revision 1.7 2003/05/08 13:44:04 kuznets * Minor test improvements * * Revision 1.6 2003/05/07 14:13:45 kuznets * + test case for cursor reading of BLOB storage. * * Revision 1.5 2003/05/05 20:15:35 kuznets * Added CBDB_BLobFile * * Revision 1.4 2003/05/02 14:10:57 kuznets * Added test for UpdateInsert * * Revision 1.3 2003/04/29 20:50:22 kuznets * Code cleanup * * Revision 1.2 2003/04/29 19:04:13 kuznets * +Test makefiles * * Revision 1.1 2003/04/28 18:12:52 kuznets * Initial revision * * * =========================================================================== */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?