📄 client.cpp
字号:
insertResult = m.insert(i4); test(insertResult.first == m.find('b')); test(insertResult.first->second == 7); test(insertResult.second == true); insertResult.first = m.end(); ByteIntMap::value_type i5('c', 8); p = m.insert(m.end(), i5); test(p == m.find('c')); test(p->second == 8); p = m.find('a'); test(p != m.end() && p->second == 0); p.set(1); test(p != m.end() && p->second == 1); // // This is necessary to release the locks held // by p and avoid a self-deadlock // p = m.end(); p = m.find('a'); test(p != m.end() && p->second == 1); cout << "ok" << endl; // // Re-populate // populateDB(connection, m); cout << "testing algorithms... " << flush; for_each(m.begin(), m.end(), ForEachTest); // // Inefficient, but this is just a test. Ensure that both forms of // operator== & != are tested. // ByteIntMap::value_type toFind('n', 13); p = find(m.begin(), m.end(), toFind); test(p != m.end()); test(*p == toFind); test(toFind == *p); test(!(*p != toFind)); test(!(toFind != *p)); p = find_if(m.begin(), m.end(), FindIfTest); test(p->first == 'b'); // // find_first_of. First construct a map with keys n, o, p, // q. The test must find one of the types (it doesn't matter // which since the container doesn't have to maintain sorted // order). // j = find(alphabet.begin(), alphabet.end(), 'n'); map<Byte, const Int> pairs; pairs.insert(pair<const Byte, const Int>(*j, static_cast<Int>(j - alphabet.begin()))); ++j; pairs.insert(pair<const Byte, const Int>(*j, static_cast<Int>(j - alphabet.begin()))); ++j; pairs.insert(pair<const Byte, const Int>(*j, static_cast<Int>(j - alphabet.begin()))); ++j; pairs.insert(pair<const Byte, const Int>(*j, static_cast<Int>(j - alphabet.begin()))); p = find_first_of(m.begin(), m.end(), pairs.begin(), pairs.end()); test(p != m.end()); test(p->first == 'n' || p->first == 'o' || p->first == 'p' || p->first == 'q'); j = find(alphabet.begin(), alphabet.end(), 'n'); 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'); pairs.clear(); for(p = m.begin(); p != m.end(); ++p) { pairs.insert(pair<const Byte, const Int>(p->first, p->second)); } test(pairs.size() == m.size()); map<Byte, const Int>::const_iterator pit; for(pit = pairs.begin(); pit != pairs.end(); ++pit) { p = m.find(pit->first); test(p != m.end()); } cout << "ok" << endl; cout << "testing index ... " << flush; m.clear(); populateDB(connection, m); size_t length = alphabet.size(); for(size_t k = 0; k < length; ++k) { p = m.findByValue(static_cast<Int>(k)); test(p != m.end()); test(p->first == alphabet[k]); test(++p == m.end()); } // // 2 items at 17 // m.put(ByteIntMap::value_type(alphabet[21], static_cast<Int>(17))); p = m.findByValue(17); test(p != m.end()); test(p->first == alphabet[17] || p->first == alphabet[21]); test(++p != m.end()); test(p->first == alphabet[17] || p->first == alphabet[21]); test(++p == m.end()); test(m.valueCount(17) == 2); p = m.findByValue(17); test(p != m.end()); m.erase(p); test(++p != m.end()); test(p->first == alphabet[17] || p->first == alphabet[21]); test(++p == m.end()); test(m.valueCount(17) == 1); p = m.findByValue(17); test(p != m.end()); test(p->first == alphabet[17] || p->first == alphabet[21]); try { p.set(18); test(false); } catch(const DatabaseException&) { // Expected } test(p->first == alphabet[17] || p->first == alphabet[21]); test(++p == m.end()); test(m.valueCount(17) == 1); cout << "ok " << endl; cout << "testing concurrent access... " << flush; m.clear(); populateDB(connection, m); vector<IceUtil::ThreadControl> controls; for(int i = 0; i < 5; ++i) { IceUtil::ThreadPtr rt = new ReadThread(communicator, envName, dbName); controls.push_back(rt->start()); IceUtil::ThreadPtr wt = new WriteThread(communicator, envName, dbName); controls.push_back(wt->start()); } for(vector<IceUtil::ThreadControl>::iterator q = controls.begin(); q != controls.end(); ++q) { q->join(); } cout << "ok" << endl; cout << "testing index creation... " << flush; { IntIdentityMap iim(connection, "intIdentity"); Ice::Identity odd; odd.name = "foo"; odd.category = "odd"; Ice::Identity even; even.name = "bar"; even.category = "even"; TransactionHolder txHolder(connection); for(int i = 0; i < 1000; i++) { if(i % 2 == 0) { iim.put(IntIdentityMap::value_type(i, even)); } else { iim.put(IntIdentityMap::value_type(i, odd)); } } txHolder.commit(); } { IntIdentityMapWithIndex iim(connection, "intIdentity"); test(iim.categoryCount("even") == 500); test(iim.categoryCount("odd") == 500); { int count = 0; IntIdentityMapWithIndex::iterator p = iim.findByCategory("even"); while(p != iim.end()) { test(p->first % 2 == 0); ++p; ++count; } test(count == 500); } { int count = 0; IntIdentityMapWithIndex::iterator p = iim.findByCategory("odd"); while(p != iim.end()) { test(p->first % 2 == 1); ++p; ++count; } test(count == 500); } iim.clear(); } cout << "ok" << endl; cout << "testing sorting... " << flush; { SortedMap sm(connection, "sortedMap"); TransactionHolder txHolder(connection); for(int i = 0; i < 1000; i++) { int k = rand() % 1000; Ice::Identity id; id.name = "foo"; id.category = 'a' + static_cast<char>(k % 26); sm.put(SortedMap::value_type(k, id)); } txHolder.commit(); } { SortedMap sm(connection, "sortedMap"); { for(int i = 0; i < 100; ++i) { int k = rand() % 1000; SortedMap::iterator p = sm.lower_bound(k); if(p != sm.end()) { test(p->first >= k); SortedMap::iterator q = sm.upper_bound(k); if(q == sm.end()) { test(p->first == k); } else { test((p->first == k && q->first > k) || (p->first > k && q->first == p->first)); } } } } { for(int i = 0; i < 100; ++i) { string category; category = static_cast<char>('a' + rand() % 26); SortedMap::iterator p = sm.findByCategory(category); if(p != sm.end()) { SortedMap::iterator q = sm.lowerBoundForCategory(category); test(p == q); do { q++; } while(q != sm.end() && q->second.category == category); if(q != sm.end()) { test(q == sm.upperBoundForCategory(category)); } } else { SortedMap::iterator q = sm.lowerBoundForCategory(category); if(q != sm.end()) { test(p != q); test(q->second.category < category); category = q->second.category; do { q++; } while(q != sm.end() && q->second.category == category); if(q != sm.end()) { test(q == sm.upperBoundForCategory(category)); } } } } } { string category = "z"; SortedMap::iterator p = sm.lowerBoundForCategory(category); while(p != sm.end()) { test(p->second.category <= category); category = p->second.category; // cerr << category << ":" << p->first << endl; ++p; } } sm.clear(); } cout << "ok" << endl; cout << "testing wstring... " << flush; { WstringWstringMap wsm(connection, "wstringMap"); TransactionHolder txHolder(connection); wsm.put(WstringWstringMap::value_type(L"AAAAA", L"aaaaa")); wsm.put(WstringWstringMap::value_type(L"BBBBB", L"bbbbb")); wsm.put(WstringWstringMap::value_type(L"CCCCC", L"ccccc")); wsm.put(WstringWstringMap::value_type(L"DDDDD", L"ddddd")); wsm.put(WstringWstringMap::value_type(L"EEEEE", L"eeeee")); txHolder.commit(); } { WstringWstringMap wsm(connection, "wstringMap"); { WstringWstringMap::iterator p = wsm.find(L"BBBBB"); test(p != wsm.end()); test(p->second == L"bbbbb"); p = wsm.findByValue(L"ddddd"); test(p != wsm.end()); test(p->first == L"DDDDD"); } wsm.clear(); } cout << "ok" << endl; return EXIT_SUCCESS;}intmain(int argc, char* argv[]){ int status; Ice::CommunicatorPtr communicator; string envName = "db"; try { communicator = Ice::initialize(argc, argv); if(argc != 1) { envName = argv[1]; envName += "/"; envName += "db"; } status = run(communicator, envName); } catch(const Ice::Exception& ex) { cerr << ex << endl; status = EXIT_FAILURE; } try { communicator->destroy(); } catch(const Ice::Exception& ex) { cerr << ex << endl; status = EXIT_FAILURE; } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -