diff options
author | Mark Spruiell <mes@zeroc.com> | 2002-08-27 18:54:22 +0000 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2002-08-27 18:54:22 +0000 |
commit | 3da21da286efccfe217d80ba05bc3fd11e44eb70 (patch) | |
tree | d585b2b65d8bae82355a6d1ec579b2aa4d3d5359 /cpp/test/Freeze/dbmap/Client.cpp | |
parent | use XML as default for freeze dictionaries (diff) | |
download | ice-3da21da286efccfe217d80ba05bc3fd11e44eb70.tar.bz2 ice-3da21da286efccfe217d80ba05bc3fd11e44eb70.tar.xz ice-3da21da286efccfe217d80ba05bc3fd11e44eb70.zip |
test both XML and binary encodings
Diffstat (limited to 'cpp/test/Freeze/dbmap/Client.cpp')
-rw-r--r-- | cpp/test/Freeze/dbmap/Client.cpp | 235 |
1 files changed, 100 insertions, 135 deletions
diff --git a/cpp/test/Freeze/dbmap/Client.cpp b/cpp/test/Freeze/dbmap/Client.cpp index a8cd760e550..c20729156c1 100644 --- a/cpp/test/Freeze/dbmap/Client.cpp +++ b/cpp/test/Freeze/dbmap/Client.cpp @@ -9,8 +9,9 @@ // ********************************************************************** #include <Freeze/Freeze.h> -#include <IceXML/StreamI.h> #include <TestCommon.h> +#include <ByteIntMapXML.h> +#include <ByteIntMapBinary.h> #include <algorithm> @@ -18,114 +19,56 @@ using namespace std; using namespace Ice; using namespace Freeze; -class KeyCodec -{ -public: - - typedef char value_type; - - static void - write(const char& key, Freeze::Key& bytes, const ::Ice::CommunicatorPtr& communicator) - { - ostringstream os; - os << "<data>"; - Ice::StreamPtr stream = new ::IceXML::StreamI(communicator, os); - stream->writeByte("Key", key); - os << "</data>"; - bytes.resize(os.str().size()); - memcpy(&bytes[0], os.str().data(), os.str().size()); - } - - static void - read(char& key, const Freeze::Key& bytes, const ::Ice::CommunicatorPtr& communicator) - { - // - // COMPILERFIX: string data(bytes.begin(), bytes.end()); - // - // This won't link with STLport 4.5.3 stldebug version. - // - string data; - data.assign(&bytes[0], bytes.size()); - - istringstream is(data); - Ice::StreamPtr stream = new ::IceXML::StreamI(communicator, is, false); - key = stream->readByte("Key"); - } -}; +static Byte alphabetChars[] = "abcdefghijklmnopqrstuvwxyz"; +vector<Byte> alphabet; -class ValueCodec +static void +ForEachTest(const pair<Byte, Int>&) { -public: - - typedef Ice::Int value_type; - - static void - write(const Ice::Int& value, Freeze::Value& bytes, const ::Ice::CommunicatorPtr& communicator) - { - ostringstream os; - os << "<data>"; - Ice::StreamPtr stream = new ::IceXML::StreamI(communicator, os); - stream->writeInt("Value", value); - os << "</data>"; - bytes.resize(os.str().size()); - memcpy(&bytes[0], os.str().data(), os.str().size()); - } - - static void - read(Ice::Int& value, const Freeze::Value& bytes, const ::Ice::CommunicatorPtr& communicator) - { - // - // COMPILERFIX: string data(bytes.begin(), bytes.end()); - // - // This won't link with STLport 4.5.3 stldebug version. - // - string data; - data.assign(&bytes[0], bytes.size()); - - istringstream is(data); - Ice::StreamPtr stream = new ::IceXML::StreamI(communicator, is, false); - value = stream->readInt("Value"); - } -}; +} -typedef DBMap<char, int, KeyCodec, ValueCodec> CharIntMap; +static bool +FindIfTest(const pair<Byte, Int>& p) +{ + return p.first == 'b'; +} -static char alphabetChars[] = "abcdefghijklmnopqrstuvwxyz"; -vector<char> alphabet; +static bool +FindFirstOfTest(const pair<Byte, Int>& p, Byte q) +{ + return p.first == q; +} +template<class MAP> static void -populateDB(CharIntMap& m) +populateDB(MAP& m) { - alphabet.assign(alphabetChars, alphabetChars + sizeof(alphabetChars)-1); + alphabet.assign(alphabetChars, alphabetChars + sizeof(alphabetChars) - 1); - for(vector<char>::const_iterator j = alphabet.begin() ; j != alphabet.end(); ++j) + for(vector<Byte>::const_iterator j = alphabet.begin(); j != alphabet.end(); ++j) { m.insert(make_pair(*j, j-alphabet.begin())); } } +template<class MAP> static int -run(int argc, char* argv[], const DBPtr& db) +run(int argc, char* argv[], MAP& m) { - CharIntMap m(db); - // // Populate the database with the alphabet // populateDB(m); - //char key; - //int value; + vector<Byte>::const_iterator j; + typename MAP::iterator p; + typename MAP::iterator cp; - vector<char>::const_iterator j; - CharIntMap::iterator p; - CharIntMap::const_iterator cp; - - cout << "testing populate... "; + cout << " testing populate... "; // // First try non-const iterator // - for(j = alphabet.begin() ; j != alphabet.end() ; ++j) + for(j = alphabet.begin(); j != alphabet.end(); ++j) { p = m.find(*j); test(p != m.end()); @@ -134,7 +77,7 @@ run(int argc, char* argv[], const DBPtr& db) // // Next try const iterator // - for(j = alphabet.begin() ; j != alphabet.end() ; ++j) + for(j = alphabet.begin(); j != alphabet.end(); ++j) { cp = m.find(*j); test(cp != m.end()); @@ -145,7 +88,7 @@ run(int argc, char* argv[], const DBPtr& db) test(m.size() == alphabet.size()); cout << "ok" << endl; - cout << "testing map::find... "; + cout << " testing map::find... "; j = find(alphabet.begin(), alphabet.end(), 'n'); cp = m.find(*j); @@ -153,7 +96,7 @@ run(int argc, char* argv[], const DBPtr& db) test(cp->first == 'n' && cp->second == j - alphabet.begin()); cout << "ok" << endl; - cout << "testing erase... "; + cout << " testing erase... "; // // erase first offset characters (first offset characters is @@ -161,23 +104,23 @@ run(int argc, char* argv[], const DBPtr& db) // the map). // int offset = 3; - vector<char> chars; - chars.push_back('a'); - chars.push_back('b'); - chars.push_back('c'); - for(j = chars.begin(); j != chars.end(); ++j) + vector<Byte> bytes; + bytes.push_back('a'); + bytes.push_back('b'); + bytes.push_back('c'); + for(j = bytes.begin(); j != bytes.end(); ++j) { p = m.find(*j); test(p != m.end()); m.erase(p); p = m.find(*j); test(p == m.end()); - vector<char>::iterator r = find(alphabet.begin(), alphabet.end(), *j); + vector<Byte>::iterator r = find(alphabet.begin(), alphabet.end(), *j); test(r != alphabet.end()); alphabet.erase(r); } - for(j = alphabet.begin() ; j != alphabet.end() ; ++j) + for(j = alphabet.begin(); j != alphabet.end(); ++j) { cp = m.find(*j); test(cp != m.end()); @@ -187,16 +130,16 @@ run(int argc, char* argv[], const DBPtr& db) cout << "ok" << endl; // - // Get a iterator for the deleted element - this should fail. + // Get an iterator for the deleted element - this should fail. // - cout << "testing map::find (again)... "; + cout << " testing map::find (again)... "; cp = m.find('a'); test(cp == m.end()); cout << "ok" << endl; - cout << "testing iterators... "; + cout << " testing iterators... "; p = m.begin(); - CharIntMap::iterator p2 = p; + typename MAP::iterator p2 = p; // // Verify both iterators point at the same element, and that @@ -230,7 +173,7 @@ run(int argc, char* argv[], const DBPtr& db) // Verify cloned cursors are independent // test(p->first != 'n' && p->second != 13); - pair<char, int> data = *p; + pair<Byte, Int> data = *p; ++p; test(p->first != data.first && p->second != data.second); @@ -252,18 +195,28 @@ run(int argc, char* argv[], const DBPtr& db) // // Test writing into an iterator. // - cout << "testing iterator.set... "; + cout << " testing iterator.set... "; p = m.find('d'); test(p != m.end() && p->second == 3); test(m.find('a') == m.end()); - m.insert(CharIntMap::value_type('a', 1)); + typename MAP::value_type i1('a', 1); + m.insert(i1); + // + // Note: VC++ won't accept this + // + //m.insert(typename MAP::value_type('a', 1)); p = m.find('a'); test(p != m.end() && p->second == 1); - m.insert(CharIntMap::value_type('a', 0)); + typename MAP::value_type i2('a', 0); + m.insert(i2); + // + // Note: VC++ won't accept this + // + //m.insert(typename MAP::value_type('a', 0)); p = m.find('a'); test(p != m.end() && p->second == 0); @@ -280,16 +233,15 @@ run(int argc, char* argv[], const DBPtr& db) // populateDB(m); - cout << "testing algorithms... "; + cout << " testing algorithms... "; - void ForEachTest(const CharIntMap::value_type&); for_each(m.begin(), m.end(), ForEachTest); // // Inefficient, but this is just a test. Ensure that both forms of // operator== & != are tested. // - CharIntMap::value_type toFind('n', 13); + typename MAP::value_type toFind('n', 13); p = find(m.begin(), m.end(), toFind); test(p != m.end()); @@ -298,8 +250,6 @@ run(int argc, char* argv[], const DBPtr& db) test(!(*p != toFind)); test(!(toFind != *p)); - - bool FindIfTest(const CharIntMap::value_type&); p = find_if(m.begin(), m.end(), FindIfTest); test(p->first == 'b'); @@ -310,7 +260,7 @@ run(int argc, char* argv[], const DBPtr& db) // order). // j = find(alphabet.begin(), alphabet.end(), 'n'); - vector< pair <char, int> > pairs; + vector< pair <Byte, Int> > pairs; pairs.push_back(make_pair(*j, j - alphabet.begin())); ++j; pairs.push_back(make_pair(*j, j - alphabet.begin())); @@ -324,7 +274,6 @@ run(int argc, char* argv[], const DBPtr& db) test(p->first == 'n' || p->first == 'o' || p->first == 'p' || p->first == 'q'); j = find(alphabet.begin(), alphabet.end(), 'n'); - bool FindFirstOfTest(const CharIntMap::value_type& p, char q); p = find_first_of(m.begin(), m.end(), j, j + 4, FindFirstOfTest); test(p != m.end()); test(p->first == 'n' || p->first == 'o' || p->first == 'p' || p->first == 'q'); @@ -333,8 +282,8 @@ run(int argc, char* argv[], const DBPtr& db) copy(m.begin(), m.end(), back_inserter(pairs)); test(pairs.size() == m.size()); - vector<pair<char, int> >::const_iterator pit; - for(pit = pairs.begin() ; pit != pairs.end() ; ++pit) + vector<pair<Byte, Int> >::const_iterator pit; + for(pit = pairs.begin(); pit != pairs.end(); ++pit) { p = m.find(pit->first); test(p != m.end()); @@ -344,23 +293,6 @@ run(int argc, char* argv[], const DBPtr& db) return EXIT_SUCCESS; } -void -ForEachTest(const CharIntMap::value_type&) -{ -} - -bool -FindIfTest(const CharIntMap::value_type& p) -{ - return p.first == 'b'; -} - -bool -FindFirstOfTest(const CharIntMap::value_type& p, char q) -{ - return p.first == q; -} - int main(int argc, char* argv[]) { @@ -368,7 +300,7 @@ main(int argc, char* argv[]) Ice::CommunicatorPtr communicator; DBEnvironmentPtr dbEnv; string dbEnvDir = "db"; - DBPtr db; + DBPtr xmlDB, binaryDB; try { @@ -380,8 +312,17 @@ main(int argc, char* argv[]) dbEnvDir += "db"; } dbEnv = Freeze::initialize(communicator, dbEnvDir); - db = dbEnv->openDB("test", true); - status = run(argc, argv, db); + xmlDB = dbEnv->openDB("xml", true); + ByteIntMapXML xml(xmlDB); + cout << "testing XML encoding..." << endl; + status = run(argc, argv, xml); + if(status == EXIT_SUCCESS) + { + binaryDB = dbEnv->openDB("binary", true); + ByteIntMapBinary binary(binaryDB); + cout << "testing binary encoding..." << endl; + status = run(argc, argv, binary); + } } catch(const Ice::Exception& ex) { @@ -389,11 +330,35 @@ main(int argc, char* argv[]) status = EXIT_FAILURE; } - if(db) + if(xmlDB) + { + try + { + xmlDB->close(); + } + catch(const DBException& ex) + { + cerr << argv[0] << ": " << ex << ": " << ex.message << endl; + status = EXIT_FAILURE; + } + catch(const Exception& ex) + { + cerr << argv[0] << ": " << ex << endl; + status = EXIT_FAILURE; + } + catch(...) + { + cerr << argv[0] << ": unknown exception" << endl; + status = EXIT_FAILURE; + } + xmlDB = 0; + } + + if(binaryDB) { try { - db->close(); + binaryDB->close(); } catch(const DBException& ex) { @@ -410,7 +375,7 @@ main(int argc, char* argv[]) cerr << argv[0] << ": unknown exception" << endl; status = EXIT_FAILURE; } - db = 0; + binaryDB = 0; } if(dbEnv) |