📄 unordered_test.cpp
字号:
#include <vector>#include <algorithm>#include <string>#if defined (STLPORT)# include <unordered_map># include <unordered_set>#endif//#include <iostream>#include "cppunit/cppunit_proxy.h"#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)using namespace std;#endif//// TestCase class//class UnorderedTest : public CPPUNIT_NS::TestCase{ CPPUNIT_TEST_SUITE(UnorderedTest);#if !defined (STLPORT) CPPUNIT_IGNORE;#endif CPPUNIT_TEST(uset); CPPUNIT_TEST(umultiset);#if defined (__DMC__) CPPUNIT_IGNORE;#endif CPPUNIT_TEST(umap); CPPUNIT_STOP_IGNORE; CPPUNIT_TEST(umultimap);#if defined (__DMC__) CPPUNIT_IGNORE;#endif CPPUNIT_TEST(user_case); CPPUNIT_STOP_IGNORE; CPPUNIT_TEST(hash_policy); CPPUNIT_TEST(buckets);#if defined (__DMC__) CPPUNIT_IGNORE;#endif CPPUNIT_TEST(equal_range);#if !defined (_STLP_USE_CONTAINERS_EXTENSION) CPPUNIT_IGNORE;#endif CPPUNIT_TEST(template_methods); CPPUNIT_TEST_SUITE_END();protected: void uset(); void umultiset(); void umap(); void umultimap(); void user_case(); void hash_policy(); void buckets(); void equal_range(); void template_methods();};CPPUNIT_TEST_SUITE_REGISTRATION(UnorderedTest);const int NB_ELEMS = 2000;//// tests implementation//void UnorderedTest::uset(){#if defined (STLPORT) typedef unordered_set<int, hash<int>, equal_to<int> > usettype; usettype us; //Small compilation check of the copy constructor: usettype us2(us); //And assignment operator us = us2; int i; pair<usettype::iterator, bool> ret; for (i = 0; i < NB_ELEMS; ++i) { ret = us.insert(i); CPPUNIT_ASSERT( ret.second ); CPPUNIT_ASSERT( *ret.first == i ); ret = us.insert(i); CPPUNIT_ASSERT( !ret.second ); CPPUNIT_ASSERT( *ret.first == i ); } vector<int> us_val; usettype::local_iterator lit, litEnd; for (i = 0; i < NB_ELEMS; ++i) { lit = us.begin(us.bucket(i)); litEnd = us.end(us.bucket(i)); usettype::size_type bucket_pos = us.bucket(*lit); for (; lit != litEnd; ++lit) { CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos ); us_val.push_back(*lit); } } //A compilation time check to uncomment from time to time { //usettype::iterator it; //CPPUNIT_ASSERT( it != lit ); } sort(us_val.begin(), us_val.end()); for (i = 0; i < NB_ELEMS; ++i) { CPPUNIT_ASSERT( us_val[i] == i ); }#endif}void UnorderedTest::umultiset(){#if defined (STLPORT) typedef unordered_multiset<int, hash<int>, equal_to<int> > usettype; usettype us; int i; usettype::iterator ret; for (i = 0; i < NB_ELEMS; ++i) { ret = us.insert(i); CPPUNIT_ASSERT( *ret == i ); ret = us.insert(i); CPPUNIT_ASSERT( *ret == i ); } CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS ); vector<int> us_val; usettype::local_iterator lit, litEnd; for (i = 0; i < NB_ELEMS; ++i) { lit = us.begin(us.bucket(i)); litEnd = us.end(us.bucket(i)); usettype::size_type bucket_pos = us.bucket(*lit); for (; lit != litEnd; ++lit) { CPPUNIT_ASSERT( us.bucket(*lit) == bucket_pos ); us_val.push_back(*lit); } } sort(us_val.begin(), us_val.end()); for (i = 0; i < NB_ELEMS; ++i) { CPPUNIT_ASSERT( us_val[2 * i] == i ); CPPUNIT_ASSERT( us_val[2 * i + 1] == i ); }#endif}void UnorderedTest::umap(){#if defined (STLPORT) && !defined (__DMC__) typedef unordered_map<int, int, hash<int>, equal_to<int> > umaptype; umaptype us; //Compilation check of the [] operator: umaptype us2; us[0] = us2[0]; us.clear(); { //An other compilation check typedef unordered_map<int, umaptype> uumaptype; uumaptype uus; umaptype const& uref = uus[0]; umaptype ucopy = uus[0]; ucopy = uref; //Avoids warning: //(void*)&uref; } int i; pair<umaptype::iterator, bool> ret; for (i = 0; i < NB_ELEMS; ++i) { umaptype::value_type p1(i, i); ret = us.insert(p1); CPPUNIT_ASSERT( ret.second ); CPPUNIT_ASSERT( *ret.first == p1 ); umaptype::value_type p2(i, i + 1); ret = us.insert(p2); CPPUNIT_ASSERT( !ret.second ); CPPUNIT_ASSERT( *ret.first == p1 ); } { //Lets look for some values to see if everything is normal: umaptype::iterator umit; for (int j = 0; j < NB_ELEMS; j += NB_ELEMS / 100) { umit = us.find(j); CPPUNIT_ASSERT( umit != us.end() ); CPPUNIT_ASSERT( (*umit).second == j ); } } CPPUNIT_ASSERT( us.size() == (size_t)NB_ELEMS ); vector<pair<int, int> > us_val; umaptype::local_iterator lit, litEnd; for (i = 0; i < NB_ELEMS; ++i) { lit = us.begin(us.bucket(i)); litEnd = us.end(us.bucket(i)); umaptype::size_type bucket_pos = us.bucket((*lit).first); for (; lit != litEnd; ++lit) { CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos ); us_val.push_back(make_pair((*lit).first, (*lit).second)); } } sort(us_val.begin(), us_val.end()); for (i = 0; i < NB_ELEMS; ++i) { CPPUNIT_ASSERT( us_val[i] == make_pair(i, i) ); }#endif}void UnorderedTest::umultimap(){#if defined (STLPORT) typedef unordered_multimap<int, int, hash<int>, equal_to<int> > umaptype; umaptype us; int i; umaptype::iterator ret; for (i = 0; i < NB_ELEMS; ++i) { umaptype::value_type p(i, i); ret = us.insert(p); CPPUNIT_ASSERT( *ret == p ); ret = us.insert(p); CPPUNIT_ASSERT( *ret == p ); } CPPUNIT_ASSERT( us.size() == 2 * NB_ELEMS ); typedef pair<int, int> ptype; vector<ptype> us_val; umaptype::local_iterator lit, litEnd; for (i = 0; i < NB_ELEMS; ++i) { lit = us.begin(us.bucket(i)); litEnd = us.end(us.bucket(i)); umaptype::size_type bucket_pos = us.bucket((*lit).first); for (; lit != litEnd; ++lit) { CPPUNIT_ASSERT( us.bucket((*lit).first) == bucket_pos ); us_val.push_back(ptype((*lit).first, (*lit).second)); } } sort(us_val.begin(), us_val.end()); for (i = 0; i < NB_ELEMS; ++i) { ptype p(i, i); CPPUNIT_ASSERT( us_val[i * 2] == p ); CPPUNIT_ASSERT( us_val[i * 2 + 1] == p ); }#endif}void UnorderedTest::user_case(){#if defined (STLPORT) && !defined (__DMC__) typedef unordered_map<int, string> UnorderedMap1; typedef unordered_map<int, UnorderedMap1> UnorderedMap2; UnorderedMap1 foo; UnorderedMap2 bar; foo.insert(UnorderedMap1::value_type(1, string("test1"))); foo.insert(UnorderedMap1::value_type(2, string("test2"))); foo.insert(UnorderedMap1::value_type(3, string("test3"))); foo.insert(UnorderedMap1::value_type(4, string("test4"))); foo.insert(UnorderedMap1::value_type(5, string("test5"))); bar.insert(UnorderedMap2::value_type(0, foo)); UnorderedMap2::iterator it = bar.find(0); CPPUNIT_ASSERT( it != bar.end() ); UnorderedMap1 &body = it->second; UnorderedMap1::iterator cur = body.find(3); CPPUNIT_ASSERT( cur != body.end() ); body.erase(body.begin(), body.end()); CPPUNIT_ASSERT( body.empty() );#endif}void UnorderedTest::hash_policy(){#if defined (STLPORT)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -