Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmap_test.cpp
Go to the documentation of this file.
00001 //Has to be first for StackAllocator swap overload to be taken 00002 //into account (at least using GCC 4.0.1) 00003 #include "stack_allocator.h" 00004 00005 #include <map> 00006 #include <algorithm> 00007 00008 #include "cppunit/cppunit_proxy.h" 00009 00010 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00011 using namespace std; 00012 #endif 00013 00014 // 00015 // TestCase class 00016 // 00017 class MapTest : public CPPUNIT_NS::TestCase 00018 { 00019 CPPUNIT_TEST_SUITE(MapTest); 00020 CPPUNIT_TEST(map1); 00021 CPPUNIT_TEST(mmap1); 00022 CPPUNIT_TEST(mmap2); 00023 CPPUNIT_TEST(iterators); 00024 CPPUNIT_TEST(equal_range); 00025 CPPUNIT_TEST(allocator_with_state); 00026 #if !defined (STLPORT) || !defined (_STLP_USE_CONTAINERS_EXTENSION) 00027 CPPUNIT_IGNORE; 00028 #endif 00029 CPPUNIT_TEST(template_methods); 00030 CPPUNIT_TEST_SUITE_END(); 00031 00032 protected: 00033 void map1(); 00034 void mmap1(); 00035 void mmap2(); 00036 void iterators(); 00037 void equal_range(); 00038 void allocator_with_state(); 00039 void template_methods(); 00040 }; 00041 00042 CPPUNIT_TEST_SUITE_REGISTRATION(MapTest); 00043 00044 // 00045 // tests implementation 00046 // 00047 void MapTest::map1() 00048 { 00049 typedef map<char, int, less<char> > maptype; 00050 maptype m; 00051 // Store mappings between roman numerals and decimals. 00052 m['l'] = 50; 00053 m['x'] = 20; // Deliberate mistake. 00054 m['v'] = 5; 00055 m['i'] = 1; 00056 // cout << "m['x'] = " << m['x'] << endl; 00057 CPPUNIT_ASSERT( m['x']== 20 ); 00058 m['x'] = 10; // Correct mistake. 00059 CPPUNIT_ASSERT( m['x']== 10 ); 00060 CPPUNIT_ASSERT( m['z']== 0 ); 00061 //cout << "m['z'] = " << m['z'] << endl; // Note default value is added. 00062 CPPUNIT_ASSERT( m.count('z') == 1 ); 00063 //cout << "m.count('z') = " << m.count('z') << endl; 00064 pair<maptype::iterator, bool> p = m.insert(pair<const char, int>('c', 100)); 00065 CPPUNIT_ASSERT( p.second ); 00066 CPPUNIT_ASSERT( p.first != m.end() ); 00067 CPPUNIT_ASSERT( (*p.first).first == 'c' ); 00068 CPPUNIT_ASSERT( (*p.first).second == 100 ); 00069 00070 p = m.insert(pair<const char, int>('c', 100)); 00071 CPPUNIT_ASSERT( !p.second ); // already existing pair 00072 CPPUNIT_ASSERT( p.first != m.end() ); 00073 CPPUNIT_ASSERT( (*p.first).first == 'c' ); 00074 CPPUNIT_ASSERT( (*p.first).second == 100 ); 00075 } 00076 00077 void MapTest::mmap1() 00078 { 00079 typedef multimap<char, int, less<char> > mmap; 00080 mmap m; 00081 CPPUNIT_ASSERT(m.count('X')==0); 00082 00083 m.insert(pair<const char, int>('X', 10)); // Standard way. 00084 CPPUNIT_ASSERT(m.count('X')==1); 00085 00086 m.insert(pair<const char, int>('X', 20)); // jbuck: standard way 00087 CPPUNIT_ASSERT(m.count('X')==2); 00088 00089 m.insert(pair<const char, int>('Y', 32)); // jbuck: standard way 00090 mmap::iterator i = m.find('X'); // Find first match. 00091 #ifndef _STLP_CONST 00092 # define _STLP_CONST const 00093 #endif 00094 pair<_STLP_CONST char, int> p('X', 10); 00095 CPPUNIT_ASSERT(*i == p); 00096 CPPUNIT_ASSERT((*i).first == 'X'); 00097 CPPUNIT_ASSERT((*i).second == 10); 00098 i++; 00099 CPPUNIT_ASSERT((*i).first == 'X'); 00100 CPPUNIT_ASSERT((*i).second == 20); 00101 i++; 00102 CPPUNIT_ASSERT((*i).first == 'Y'); 00103 CPPUNIT_ASSERT((*i).second == 32); 00104 i++; 00105 CPPUNIT_ASSERT(i == m.end()); 00106 00107 size_t count = m.erase('X'); 00108 CPPUNIT_ASSERT(count==2); 00109 } 00110 void MapTest::mmap2() 00111 { 00112 typedef pair<const int, char> pair_type; 00113 00114 pair_type p1(3, 'c'); 00115 pair_type p2(6, 'f'); 00116 pair_type p3(1, 'a'); 00117 pair_type p4(2, 'b'); 00118 pair_type p5(3, 'x'); 00119 pair_type p6(6, 'f'); 00120 00121 typedef multimap<int, char, less<int> > mmap; 00122 00123 pair_type array [] = { 00124 p1, 00125 p2, 00126 p3, 00127 p4, 00128 p5, 00129 p6 00130 }; 00131 00132 mmap m(array + 0, array + 6); 00133 mmap::iterator i; 00134 i = m.lower_bound(3); 00135 CPPUNIT_ASSERT((*i).first==3); 00136 CPPUNIT_ASSERT((*i).second=='c'); 00137 00138 i = m.upper_bound(3); 00139 CPPUNIT_ASSERT((*i).first==6); 00140 CPPUNIT_ASSERT((*i).second=='f'); 00141 } 00142 00143 00144 void MapTest::iterators() 00145 { 00146 typedef map<int, char, less<int> > int_map; 00147 int_map imap; 00148 { 00149 int_map::iterator ite(imap.begin()); 00150 int_map::const_iterator cite(imap.begin()); 00151 CPPUNIT_ASSERT( ite == cite ); 00152 CPPUNIT_ASSERT( !(ite != cite) ); 00153 CPPUNIT_ASSERT( cite == ite ); 00154 CPPUNIT_ASSERT( !(cite != ite) ); 00155 } 00156 00157 typedef multimap<int, char, less<int> > mmap; 00158 typedef mmap::value_type pair_type; 00159 00160 pair_type p1(3, 'c'); 00161 pair_type p2(6, 'f'); 00162 pair_type p3(1, 'a'); 00163 pair_type p4(2, 'b'); 00164 pair_type p5(3, 'x'); 00165 pair_type p6(6, 'f'); 00166 00167 pair_type array [] = { 00168 p1, 00169 p2, 00170 p3, 00171 p4, 00172 p5, 00173 p6 00174 }; 00175 00176 mmap m(array+0, array + 6); 00177 00178 { 00179 mmap::iterator ite(m.begin()); 00180 mmap::const_iterator cite(m.begin()); 00181 //test compare between const_iterator and iterator 00182 CPPUNIT_ASSERT( ite == cite ); 00183 CPPUNIT_ASSERT( !(ite != cite) ); 00184 CPPUNIT_ASSERT( cite == ite ); 00185 CPPUNIT_ASSERT( !(cite != ite) ); 00186 } 00187 00188 #if 0 00189 /* 00190 * A check that map and multimap iterators are NOT comparable 00191 * the following code should generate a compile time error 00192 */ 00193 { 00194 int_map::iterator mite(imap.begin()); 00195 int_map::const_iterator mcite(imap.begin()); 00196 mmap::iterator mmite(m.begin()); 00197 mmap::const_iterator mmcite(m.begin()); 00198 CPPUNIT_ASSERT( !(mite == mmite) ); 00199 CPPUNIT_ASSERT( !(mcite == mmcite) ); 00200 CPPUNIT_ASSERT( mite != mmite ); 00201 CPPUNIT_ASSERT( mcite != mmcite ); 00202 CPPUNIT_ASSERT( !(mite == mmcite) ); 00203 CPPUNIT_ASSERT( !(mite == mmcite) ); 00204 CPPUNIT_ASSERT( mite != mmcite ); 00205 CPPUNIT_ASSERT( mite != mmcite ); 00206 } 00207 00208 #endif 00209 00210 mmap::reverse_iterator ri = m.rbegin(); 00211 CPPUNIT_ASSERT( ri != m.rend() ); 00212 CPPUNIT_ASSERT( ri == m.rbegin() ); 00213 CPPUNIT_ASSERT( (*ri).first == 6 ); 00214 CPPUNIT_ASSERT( (*ri++).second == 'f' ); 00215 CPPUNIT_ASSERT( (*ri).first == 6 ); 00216 CPPUNIT_ASSERT( (*ri).second == 'f' ); 00217 00218 mmap const& cm = m; 00219 mmap::const_reverse_iterator rci = cm.rbegin(); 00220 CPPUNIT_ASSERT( rci != cm.rend() ); 00221 CPPUNIT_ASSERT( (*rci).first == 6 ); 00222 CPPUNIT_ASSERT( (*rci++).second == 'f' ); 00223 CPPUNIT_ASSERT( (*rci).first == 6 ); 00224 CPPUNIT_ASSERT( (*rci).second == 'f' ); 00225 } 00226 00227 void MapTest::equal_range() 00228 { 00229 typedef map<char, int, less<char> > maptype; 00230 { 00231 maptype m; 00232 m['x'] = 10; 00233 00234 pair<maptype::iterator, maptype::iterator> ret; 00235 ret = m.equal_range('x'); 00236 CPPUNIT_ASSERT( ret.first != ret.second ); 00237 CPPUNIT_ASSERT( (*(ret.first)).first == 'x' ); 00238 CPPUNIT_ASSERT( (*(ret.first)).second == 10 ); 00239 CPPUNIT_ASSERT( ++(ret.first) == ret.second ); 00240 } 00241 { 00242 { 00243 maptype m; 00244 00245 maptype::iterator i = m.lower_bound( 'x' ); 00246 CPPUNIT_ASSERT( i == m.end() ); 00247 00248 i = m.upper_bound( 'x' ); 00249 CPPUNIT_ASSERT( i == m.end() ); 00250 00251 pair<maptype::iterator, maptype::iterator> ret; 00252 ret = m.equal_range('x'); 00253 CPPUNIT_ASSERT( ret.first == ret.second ); 00254 CPPUNIT_ASSERT( ret.first == m.end() ); 00255 } 00256 00257 { 00258 const maptype m; 00259 pair<maptype::const_iterator, maptype::const_iterator> ret; 00260 ret = m.equal_range('x'); 00261 CPPUNIT_ASSERT( ret.first == ret.second ); 00262 CPPUNIT_ASSERT( ret.first == m.end() ); 00263 } 00264 } 00265 } 00266 00267 void MapTest::allocator_with_state() 00268 { 00269 char buf1[1024]; 00270 StackAllocator<pair<const int, int> > stack1(buf1, buf1 + sizeof(buf1)); 00271 00272 char buf2[1024]; 00273 StackAllocator<pair<const int, int> > stack2(buf2, buf2 + sizeof(buf2)); 00274 00275 { 00276 typedef map<int, int, less<int>, StackAllocator<pair<const int, int> > > MapInt; 00277 less<int> intLess; 00278 MapInt mint1(intLess, stack1); 00279 int i; 00280 for (i = 0; i < 5; ++i) 00281 mint1.insert(MapInt::value_type(i, i)); 00282 MapInt mint1Cpy(mint1); 00283 00284 MapInt mint2(intLess, stack2); 00285 for (; i < 10; ++i) 00286 mint2.insert(MapInt::value_type(i, i)); 00287 MapInt mint2Cpy(mint2); 00288 00289 mint1.swap(mint2); 00290 00291 CPPUNIT_ASSERT( mint1.get_allocator().swaped() ); 00292 CPPUNIT_ASSERT( mint2.get_allocator().swaped() ); 00293 00294 CPPUNIT_ASSERT( mint1 == mint2Cpy ); 00295 CPPUNIT_ASSERT( mint2 == mint1Cpy ); 00296 CPPUNIT_ASSERT( mint1.get_allocator() == stack2 ); 00297 CPPUNIT_ASSERT( mint2.get_allocator() == stack1 ); 00298 } 00299 CPPUNIT_ASSERT( stack1.ok() ); 00300 CPPUNIT_ASSERT( stack2.ok() ); 00301 } 00302 00303 struct Key 00304 { 00305 Key() : m_data(0) {} 00306 explicit Key(int data) : m_data(data) {} 00307 00308 int m_data; 00309 }; 00310 00311 struct KeyCmp 00312 { 00313 bool operator () (Key lhs, Key rhs) const 00314 { return lhs.m_data < rhs.m_data; } 00315 00316 bool operator () (Key lhs, int rhs) const 00317 { return lhs.m_data < rhs; } 00318 00319 bool operator () (int lhs, Key rhs) const 00320 { return lhs < rhs.m_data; } 00321 }; 00322 00323 struct KeyCmpPtr 00324 { 00325 bool operator () (Key const volatile *lhs, Key const volatile *rhs) const 00326 { return (*lhs).m_data < (*rhs).m_data; } 00327 00328 bool operator () (Key const volatile *lhs, int rhs) const 00329 { return (*lhs).m_data < rhs; } 00330 00331 bool operator () (int lhs, Key const volatile *rhs) const 00332 { return lhs < (*rhs).m_data; } 00333 }; 00334 00335 void MapTest::template_methods() 00336 { 00337 #if defined (STLPORT) && defined (_STLP_USE_CONTAINERS_EXTENSION) 00338 { 00339 typedef map<Key, int, KeyCmp> Container; 00340 typedef Container::value_type value; 00341 Container cont; 00342 cont.insert(value(Key(1), 1)); 00343 cont.insert(value(Key(2), 2)); 00344 cont.insert(value(Key(3), 3)); 00345 cont.insert(value(Key(4), 4)); 00346 00347 CPPUNIT_ASSERT( cont.count(Key(1)) == 1 ); 00348 CPPUNIT_ASSERT( cont.count(1) == 1 ); 00349 CPPUNIT_ASSERT( cont.count(5) == 0 ); 00350 00351 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); 00352 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); 00353 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); 00354 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); 00355 00356 Container const& ccont = cont; 00357 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); 00358 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); 00359 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); 00360 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) ); 00361 } 00362 00363 { 00364 typedef map<Key*, int, KeyCmpPtr> Container; 00365 typedef Container::value_type value; 00366 Container cont; 00367 Key key1(1), key2(2), key3(3), key4(4); 00368 cont.insert(value(&key1, 1)); 00369 cont.insert(value(&key2, 2)); 00370 cont.insert(value(&key3, 3)); 00371 cont.insert(value(&key4, 4)); 00372 00373 CPPUNIT_ASSERT( cont.count(1) == 1 ); 00374 CPPUNIT_ASSERT( cont.count(5) == 0 ); 00375 00376 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); 00377 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); 00378 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); 00379 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); 00380 00381 Container const& ccont = cont; 00382 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); 00383 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); 00384 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); 00385 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) ); 00386 } 00387 { 00388 typedef multimap<Key, int, KeyCmp> Container; 00389 typedef Container::value_type value; 00390 Container cont; 00391 cont.insert(value(Key(1), 1)); 00392 cont.insert(value(Key(2), 2)); 00393 cont.insert(value(Key(3), 3)); 00394 cont.insert(value(Key(4), 4)); 00395 00396 CPPUNIT_ASSERT( cont.count(Key(1)) == 1 ); 00397 CPPUNIT_ASSERT( cont.count(1) == 1 ); 00398 CPPUNIT_ASSERT( cont.count(5) == 0 ); 00399 00400 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); 00401 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); 00402 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); 00403 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); 00404 00405 Container const& ccont = cont; 00406 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); 00407 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); 00408 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); 00409 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.end(), ccont.end()) ); 00410 } 00411 00412 { 00413 typedef multimap<Key const volatile*, int, KeyCmpPtr> Container; 00414 typedef Container::value_type value; 00415 Container cont; 00416 Key key1(1), key2(2), key3(3), key4(4); 00417 cont.insert(value(&key1, 1)); 00418 cont.insert(value(&key2, 2)); 00419 cont.insert(value(&key3, 3)); 00420 cont.insert(value(&key4, 4)); 00421 00422 CPPUNIT_ASSERT( cont.count(1) == 1 ); 00423 CPPUNIT_ASSERT( cont.count(5) == 0 ); 00424 00425 CPPUNIT_ASSERT( cont.find(2) != cont.end() ); 00426 CPPUNIT_ASSERT( cont.lower_bound(2) != cont.end() ); 00427 CPPUNIT_ASSERT( cont.upper_bound(2) != cont.end() ); 00428 CPPUNIT_ASSERT( cont.equal_range(2) != make_pair(cont.begin(), cont.end()) ); 00429 00430 Container const& ccont = cont; 00431 CPPUNIT_ASSERT( ccont.find(2) != ccont.end() ); 00432 CPPUNIT_ASSERT( ccont.lower_bound(2) != ccont.end() ); 00433 CPPUNIT_ASSERT( ccont.upper_bound(2) != ccont.end() ); 00434 CPPUNIT_ASSERT( ccont.equal_range(2) != make_pair(ccont.begin(), ccont.end()) ); 00435 } 00436 #endif 00437 } 00438 00439 #if !defined (STLPORT) || \ 00440 !defined (_STLP_USE_PTR_SPECIALIZATIONS) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00441 # if !defined (__DMC__) 00442 /* Simple compilation test: Check that nested types like iterator 00443 * can be access even if type used to instanciate container is not 00444 * yet completely defined. 00445 */ 00446 class IncompleteClass 00447 { 00448 map<IncompleteClass, IncompleteClass> instances; 00449 typedef map<IncompleteClass, IncompleteClass>::iterator it; 00450 multimap<IncompleteClass, IncompleteClass> minstances; 00451 typedef multimap<IncompleteClass, IncompleteClass>::iterator mit; 00452 }; 00453 # endif 00454 #endif Generated on Sun May 27 2012 04:35:29 for ReactOS by
1.7.6.1
|