Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenslist_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 <algorithm> 00006 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00007 # include <slist> 00008 # if !defined (_STLP_USE_NO_IOSTREAMS) 00009 # include <sstream> 00010 # endif 00011 # include <iterator> 00012 # include <functional> 00013 #endif 00014 00015 #include "cppunit/cppunit_proxy.h" 00016 00017 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00018 using namespace std; 00019 #endif 00020 00021 #if !defined (STLPORT) && defined(__GNUC__) 00022 using namespace __gnu_cxx; 00023 #endif 00024 00025 // 00026 // TestCase class 00027 // 00028 class SlistTest : public CPPUNIT_NS::TestCase 00029 { 00030 CPPUNIT_TEST_SUITE(SlistTest); 00031 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS) || defined (_STLP_USE_NO_IOSTREAMS) 00032 CPPUNIT_IGNORE; 00033 #endif 00034 CPPUNIT_TEST(slist1); 00035 #if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS) 00036 CPPUNIT_STOP_IGNORE; 00037 #endif 00038 CPPUNIT_TEST(erase); 00039 CPPUNIT_TEST(insert); 00040 CPPUNIT_TEST(splice); 00041 CPPUNIT_TEST(allocator_with_state); 00042 CPPUNIT_TEST_SUITE_END(); 00043 00044 protected: 00045 void slist1(); 00046 void erase(); 00047 void insert(); 00048 void splice(); 00049 void allocator_with_state(); 00050 }; 00051 00052 CPPUNIT_TEST_SUITE_REGISTRATION(SlistTest); 00053 00054 // 00055 // tests implementation 00056 // 00057 void SlistTest::slist1() 00058 { 00059 #if defined (STLPORT) && !defined (_STLP_USE_NO_IOSTREAMS) && !defined (_STLP_NO_EXTENSIONS) 00060 /* 00061 original: xlxtss 00062 reversed: sstxlx 00063 removed: sstl 00064 uniqued: stl 00065 sorted: lst 00066 */ 00067 00068 char array [] = { 'x', 'l', 'x', 't', 's', 's' }; 00069 ostringstream os; 00070 ostream_iterator<char> o(os,""); 00071 slist<char> str(array+0, array + 6); 00072 slist<char>::iterator i; 00073 //Check const_iterator construction from iterator 00074 slist<char>::const_iterator ci(i); 00075 slist<char>::const_iterator ci2(ci); 00076 // cout << "reversed: "; 00077 str.reverse(); 00078 for(i = str.begin(); i != str.end(); i++) 00079 os << *i; 00080 stringbuf* buff=os.rdbuf(); 00081 string result=buff->str(); 00082 CPPUNIT_ASSERT(!strcmp(result.c_str(),"sstxlx")); 00083 00084 //cout << "removed: "; 00085 str.remove('x'); 00086 ostringstream os2; 00087 for(i = str.begin(); i != str.end(); i++) 00088 os2 << *i; 00089 buff=os2.rdbuf(); 00090 result=buff->str(); 00091 CPPUNIT_ASSERT(!strcmp(result.c_str(),"sstl")); 00092 00093 00094 //cout << "uniqued: "; 00095 str.unique(); 00096 ostringstream os3; 00097 for(i = str.begin(); i != str.end(); i++) 00098 os3 << *i; 00099 buff=os3.rdbuf(); 00100 result=buff->str(); 00101 CPPUNIT_ASSERT(!strcmp(result.c_str(),"stl")); 00102 00103 //cout << "sorted: "; 00104 str.sort(); 00105 ostringstream os4; 00106 for(i = str.begin(); i != str.end(); i++) 00107 os4 << *i; 00108 buff = os4.rdbuf(); 00109 result = buff->str(); 00110 CPPUNIT_ASSERT(!strcmp(result.c_str(),"lst")); 00111 00112 //A small compilation time check to be activated from time to time: 00113 # if 0 00114 { 00115 slist<char>::iterator sl_char_ite; 00116 slist<int>::iterator sl_int_ite; 00117 CPPUNIT_ASSERT( sl_char_ite != sl_int_ite ); 00118 } 00119 # endif 00120 #endif 00121 } 00122 00123 void SlistTest::erase() 00124 { 00125 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00126 int array[] = { 0, 1, 2, 3, 4 }; 00127 slist<int> sl(array, array + 5); 00128 slist<int>::iterator slit; 00129 00130 slit = sl.erase(sl.begin()); 00131 CPPUNIT_ASSERT( *slit == 1); 00132 00133 ++slit++; ++slit; 00134 slit = sl.erase(sl.begin(), slit); 00135 CPPUNIT_ASSERT( *slit == 3 ); 00136 00137 sl.assign(array, array + 5); 00138 00139 slit = sl.erase_after(sl.begin()); 00140 CPPUNIT_ASSERT( *slit == 2 ); 00141 00142 slit = sl.begin(); ++slit; ++slit; 00143 slit = sl.erase_after(sl.begin(), slit); 00144 CPPUNIT_ASSERT( *slit == 3 ); 00145 00146 sl.erase_after(sl.before_begin()); 00147 CPPUNIT_ASSERT( sl.front() == 3 ); 00148 #endif 00149 } 00150 00151 void SlistTest::insert() 00152 { 00153 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00154 int array[] = { 0, 1, 2, 3, 4 }; 00155 00156 //insert 00157 { 00158 slist<int> sl; 00159 00160 sl.insert(sl.begin(), 5); 00161 CPPUNIT_ASSERT( sl.front() == 5 ); 00162 CPPUNIT_ASSERT( sl.size() == 1 ); 00163 00164 //debug mode check: 00165 //sl.insert(sl.before_begin(), array, array + 5); 00166 00167 sl.insert(sl.begin(), array, array + 5); 00168 CPPUNIT_ASSERT( sl.size() == 6 ); 00169 int i; 00170 slist<int>::iterator slit(sl.begin()); 00171 for (i = 0; slit != sl.end(); ++slit, ++i) { 00172 CPPUNIT_ASSERT( *slit == i ); 00173 } 00174 } 00175 00176 //insert_after 00177 { 00178 slist<int> sl; 00179 00180 //debug check: 00181 //sl.insert_after(sl.begin(), 5); 00182 00183 sl.insert_after(sl.before_begin(), 5); 00184 CPPUNIT_ASSERT( sl.front() == 5 ); 00185 CPPUNIT_ASSERT( sl.size() == 1 ); 00186 00187 sl.insert_after(sl.before_begin(), array, array + 5); 00188 CPPUNIT_ASSERT( sl.size() == 6 ); 00189 int i; 00190 slist<int>::iterator slit(sl.begin()); 00191 for (i = 0; slit != sl.end(); ++slit, ++i) { 00192 CPPUNIT_ASSERT( *slit == i ); 00193 } 00194 } 00195 #endif 00196 } 00197 00198 void SlistTest::splice() 00199 { 00200 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00201 int array[] = { 0, 1, 2, 3, 4 }; 00202 00203 //splice 00204 { 00205 slist<int> sl1(array, array + 5); 00206 slist<int> sl2(array, array + 5); 00207 slist<int>::iterator slit; 00208 00209 //a no op: 00210 sl1.splice(sl1.begin(), sl1, sl1.begin()); 00211 CPPUNIT_ASSERT( sl1 == sl2 ); 00212 00213 slit = sl1.begin(); ++slit; 00214 //a no op: 00215 sl1.splice(slit, sl1, sl1.begin()); 00216 CPPUNIT_ASSERT( sl1 == sl2 ); 00217 00218 sl1.splice(sl1.end(), sl1, sl1.begin()); 00219 slit = sl1.begin(); 00220 CPPUNIT_ASSERT( *(slit++) == 1 ); 00221 CPPUNIT_ASSERT( *(slit++) == 2 ); 00222 CPPUNIT_ASSERT( *(slit++) == 3 ); 00223 CPPUNIT_ASSERT( *(slit++) == 4 ); 00224 CPPUNIT_ASSERT( *slit == 0 ); 00225 sl1.splice(sl1.begin(), sl1, slit); 00226 CPPUNIT_ASSERT( sl1 == sl2 ); 00227 00228 sl1.splice(sl1.begin(), sl2); 00229 size_t i; 00230 for (i = 0, slit = sl1.begin(); slit != sl1.end(); ++slit, ++i) { 00231 if (i == 5) i = 0; 00232 CPPUNIT_ASSERT( *slit == array[i] ); 00233 } 00234 00235 slit = sl1.begin(); 00236 advance(slit, 5); 00237 CPPUNIT_ASSERT( *slit == 0 ); 00238 sl2.splice(sl2.begin(), sl1, sl1.begin(), slit); 00239 CPPUNIT_ASSERT( sl1 == sl2 ); 00240 00241 slit = sl1.begin(); ++slit; 00242 sl1.splice(sl1.begin(), sl1, slit, sl1.end()); 00243 slit = sl1.begin(); 00244 CPPUNIT_ASSERT( *(slit++) == 1 ); 00245 CPPUNIT_ASSERT( *(slit++) == 2 ); 00246 CPPUNIT_ASSERT( *(slit++) == 3 ); 00247 CPPUNIT_ASSERT( *(slit++) == 4 ); 00248 CPPUNIT_ASSERT( *slit == 0 ); 00249 00250 // a no op 00251 sl2.splice(sl2.end(), sl2, sl2.begin(), sl2.end()); 00252 for (i = 0, slit = sl2.begin(); slit != sl2.end(); ++slit, ++i) { 00253 CPPUNIT_ASSERT( i < 5 ); 00254 CPPUNIT_ASSERT( *slit == array[i] ); 00255 } 00256 00257 slit = sl2.begin(); 00258 advance(slit, 3); 00259 sl2.splice(sl2.end(), sl2, sl2.begin(), slit); 00260 slit = sl2.begin(); 00261 CPPUNIT_ASSERT( *(slit++) == 3 ); 00262 CPPUNIT_ASSERT( *(slit++) == 4 ); 00263 CPPUNIT_ASSERT( *(slit++) == 0 ); 00264 CPPUNIT_ASSERT( *(slit++) == 1 ); 00265 CPPUNIT_ASSERT( *slit == 2 ); 00266 } 00267 00268 //splice_after 00269 { 00270 slist<int> sl1(array, array + 5); 00271 slist<int> sl2(array, array + 5); 00272 slist<int>::iterator slit; 00273 00274 //a no op: 00275 sl1.splice_after(sl1.begin(), sl1, sl1.begin()); 00276 CPPUNIT_ASSERT( sl1 == sl2 ); 00277 00278 sl1.splice_after(sl1.before_begin(), sl1, sl1.begin()); 00279 slit = sl1.begin(); 00280 CPPUNIT_ASSERT( *(slit++) == 1 ); 00281 CPPUNIT_ASSERT( *(slit++) == 0 ); 00282 CPPUNIT_ASSERT( *(slit++) == 2 ); 00283 CPPUNIT_ASSERT( *(slit++) == 3 ); 00284 CPPUNIT_ASSERT( *slit == 4 ); 00285 sl1.splice_after(sl1.before_begin(), sl1, sl1.begin()); 00286 CPPUNIT_ASSERT( sl1 == sl2 ); 00287 00288 sl1.splice_after(sl1.before_begin(), sl2); 00289 size_t i; 00290 for (i = 0, slit = sl1.begin(); slit != sl1.end(); ++slit, ++i) { 00291 if (i == 5) i = 0; 00292 CPPUNIT_ASSERT( *slit == array[i] ); 00293 } 00294 00295 slit = sl1.begin(); 00296 advance(slit, 4); 00297 CPPUNIT_ASSERT( *slit == 4 ); 00298 sl2.splice_after(sl2.before_begin(), sl1, sl1.before_begin(), slit); 00299 CPPUNIT_ASSERT( sl1 == sl2 ); 00300 00301 sl1.splice_after(sl1.before_begin(), sl1, sl1.begin(), sl1.previous(sl1.end())); 00302 slit = sl1.begin(); 00303 CPPUNIT_ASSERT( *(slit++) == 1 ); 00304 CPPUNIT_ASSERT( *(slit++) == 2 ); 00305 CPPUNIT_ASSERT( *(slit++) == 3 ); 00306 CPPUNIT_ASSERT( *(slit++) == 4 ); 00307 CPPUNIT_ASSERT( *slit == 0 ); 00308 00309 // a no op 00310 sl2.splice_after(sl2.before_begin(), sl2, sl2.before_begin(), sl2.previous(sl2.end())); 00311 for (i = 0, slit = sl2.begin(); slit != sl2.end(); ++slit, ++i) { 00312 CPPUNIT_ASSERT( i < 5 ); 00313 CPPUNIT_ASSERT( *slit == array[i] ); 00314 } 00315 00316 slit = sl2.begin(); 00317 advance(slit, 2); 00318 sl2.splice_after(sl2.previous(sl2.end()), sl2, sl2.before_begin(), slit); 00319 slit = sl2.begin(); 00320 CPPUNIT_ASSERT( *(slit++) == 3 ); 00321 CPPUNIT_ASSERT( *(slit++) == 4 ); 00322 CPPUNIT_ASSERT( *(slit++) == 0 ); 00323 CPPUNIT_ASSERT( *(slit++) == 1 ); 00324 CPPUNIT_ASSERT( *slit == 2 ); 00325 } 00326 #endif 00327 } 00328 00329 00330 void SlistTest::allocator_with_state() 00331 { 00332 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00333 char buf1[1024]; 00334 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1)); 00335 00336 char buf2[1024]; 00337 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2)); 00338 00339 typedef slist<int, StackAllocator<int> > SlistInt; 00340 { 00341 SlistInt slint1(10, 0, stack1); 00342 SlistInt slint1Cpy(slint1); 00343 00344 SlistInt slint2(10, 1, stack2); 00345 SlistInt slint2Cpy(slint2); 00346 00347 slint1.swap(slint2); 00348 00349 CPPUNIT_ASSERT( slint1.get_allocator().swaped() ); 00350 CPPUNIT_ASSERT( slint2.get_allocator().swaped() ); 00351 00352 CPPUNIT_ASSERT( slint1 == slint2Cpy ); 00353 CPPUNIT_ASSERT( slint2 == slint1Cpy ); 00354 CPPUNIT_ASSERT( slint1.get_allocator() == stack2 ); 00355 CPPUNIT_ASSERT( slint2.get_allocator() == stack1 ); 00356 } 00357 CPPUNIT_CHECK( stack1.ok() ); 00358 CPPUNIT_CHECK( stack2.ok() ); 00359 stack1.reset(); stack2.reset(); 00360 00361 { 00362 SlistInt slint1(stack1); 00363 SlistInt slint1Cpy(slint1); 00364 00365 SlistInt slint2(10, 1, stack2); 00366 SlistInt slint2Cpy(slint2); 00367 00368 slint1.swap(slint2); 00369 00370 CPPUNIT_ASSERT( slint1.get_allocator().swaped() ); 00371 CPPUNIT_ASSERT( slint2.get_allocator().swaped() ); 00372 00373 CPPUNIT_ASSERT( slint1 == slint2Cpy ); 00374 CPPUNIT_ASSERT( slint2 == slint1Cpy ); 00375 CPPUNIT_ASSERT( slint1.get_allocator() == stack2 ); 00376 CPPUNIT_ASSERT( slint2.get_allocator() == stack1 ); 00377 } 00378 CPPUNIT_CHECK( stack1.ok() ); 00379 CPPUNIT_CHECK( stack2.ok() ); 00380 stack1.reset(); stack2.reset(); 00381 00382 { 00383 SlistInt slint1(10, 0, stack1); 00384 SlistInt slint1Cpy(slint1); 00385 00386 SlistInt slint2(stack2); 00387 SlistInt slint2Cpy(slint2); 00388 00389 slint1.swap(slint2); 00390 00391 CPPUNIT_ASSERT( slint1.get_allocator().swaped() ); 00392 CPPUNIT_ASSERT( slint2.get_allocator().swaped() ); 00393 00394 CPPUNIT_ASSERT( slint1 == slint2Cpy ); 00395 CPPUNIT_ASSERT( slint2 == slint1Cpy ); 00396 CPPUNIT_ASSERT( slint1.get_allocator() == stack2 ); 00397 CPPUNIT_ASSERT( slint2.get_allocator() == stack1 ); 00398 } 00399 CPPUNIT_CHECK( stack1.ok() ); 00400 CPPUNIT_CHECK( stack2.ok() ); 00401 stack1.reset(); stack2.reset(); 00402 00403 //splice(iterator, slist) 00404 { 00405 SlistInt slint1(10, 0, stack1); 00406 SlistInt slint2(10, 1, stack2); 00407 00408 slint1.splice(slint1.begin(), slint2); 00409 CPPUNIT_ASSERT( slint1.size() == 20 ); 00410 CPPUNIT_ASSERT( slint2.empty() ); 00411 } 00412 CPPUNIT_CHECK( stack1.ok() ); 00413 CPPUNIT_CHECK( stack2.ok() ); 00414 stack1.reset(); stack2.reset(); 00415 00416 //splice(iterator, slist, iterator) 00417 { 00418 SlistInt slint1(10, 0, stack1); 00419 SlistInt slint2(10, 1, stack2); 00420 00421 slint1.splice(slint1.begin(), slint2, slint2.begin()); 00422 CPPUNIT_ASSERT( slint1.size() == 11 ); 00423 CPPUNIT_ASSERT( slint2.size() == 9 ); 00424 } 00425 CPPUNIT_CHECK( stack1.ok() ); 00426 CPPUNIT_CHECK( stack2.ok() ); 00427 stack1.reset(); stack2.reset(); 00428 00429 //splice(iterator, slist, iterator, iterator) 00430 { 00431 SlistInt slint1(10, 0, stack1); 00432 SlistInt slint2(10, 1, stack2); 00433 00434 SlistInt::iterator lit(slint2.begin()); 00435 advance(lit, 5); 00436 slint1.splice(slint1.begin(), slint2, slint2.begin(), lit); 00437 CPPUNIT_ASSERT( slint1.size() == 15 ); 00438 CPPUNIT_ASSERT( slint2.size() == 5 ); 00439 } 00440 CPPUNIT_CHECK( stack1.ok() ); 00441 CPPUNIT_CHECK( stack2.ok() ); 00442 stack1.reset(); stack2.reset(); 00443 00444 //splice_after(iterator, slist) 00445 { 00446 SlistInt slint1(10, 0, stack1); 00447 SlistInt slint2(10, 1, stack2); 00448 00449 slint1.splice_after(slint1.before_begin(), slint2); 00450 CPPUNIT_ASSERT( slint1.size() == 20 ); 00451 CPPUNIT_ASSERT( slint2.empty() ); 00452 } 00453 CPPUNIT_CHECK( stack1.ok() ); 00454 CPPUNIT_CHECK( stack2.ok() ); 00455 stack1.reset(); stack2.reset(); 00456 00457 //splice_after(iterator, slist, iterator) 00458 { 00459 SlistInt slint1(10, 0, stack1); 00460 SlistInt slint2(10, 1, stack2); 00461 00462 slint1.splice_after(slint1.before_begin(), slint2, slint2.before_begin()); 00463 CPPUNIT_ASSERT( slint1.size() == 11 ); 00464 CPPUNIT_ASSERT( slint2.size() == 9 ); 00465 } 00466 CPPUNIT_CHECK( stack1.ok() ); 00467 CPPUNIT_CHECK( stack2.ok() ); 00468 stack1.reset(); stack2.reset(); 00469 00470 //splice_after(iterator, slist, iterator, iterator) 00471 { 00472 SlistInt slint1(10, 0, stack1); 00473 SlistInt slint2(10, 1, stack2); 00474 00475 SlistInt::iterator lit(slint2.begin()); 00476 advance(lit, 4); 00477 slint1.splice_after(slint1.before_begin(), slint2, slint2.before_begin(), lit); 00478 CPPUNIT_ASSERT( slint1.size() == 15 ); 00479 CPPUNIT_ASSERT( slint2.size() == 5 ); 00480 } 00481 CPPUNIT_CHECK( stack1.ok() ); 00482 CPPUNIT_CHECK( stack2.ok() ); 00483 stack1.reset(); stack2.reset(); 00484 00485 //merge(slist) 00486 { 00487 SlistInt slint1(10, 0, stack1); 00488 SlistInt slint2(10, 1, stack2); 00489 00490 SlistInt slintref(stack2); 00491 slintref.insert_after(slintref.before_begin(), 10, 1); 00492 slintref.insert_after(slintref.before_begin(), 10, 0); 00493 00494 slint1.merge(slint2); 00495 CPPUNIT_ASSERT( slint1.size() == 20 ); 00496 CPPUNIT_ASSERT( slint1 == slintref ); 00497 CPPUNIT_ASSERT( slint2.empty() ); 00498 } 00499 CPPUNIT_CHECK( stack1.ok() ); 00500 CPPUNIT_CHECK( stack2.ok() ); 00501 00502 //merge(slist, predicate) 00503 # if (!defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)) && \ 00504 (!defined (_MSC_VER) || (_MSC_VER >= 1300)) 00505 { 00506 SlistInt slint1(10, 0, stack1); 00507 SlistInt slint2(10, 1, stack2); 00508 00509 SlistInt slintref(stack2); 00510 slintref.insert_after(slintref.before_begin(), 10, 0); 00511 slintref.insert_after(slintref.before_begin(), 10, 1); 00512 00513 slint1.merge(slint2, greater<int>()); 00514 CPPUNIT_ASSERT( slint1.size() == 20 ); 00515 CPPUNIT_ASSERT( slint1 == slintref ); 00516 CPPUNIT_ASSERT( slint2.empty() ); 00517 } 00518 CPPUNIT_CHECK( stack1.ok() ); 00519 CPPUNIT_CHECK( stack2.ok() ); 00520 00521 //sort 00522 { 00523 //This is rather a compile time test. 00524 //We check that sort implementation is correct when list is instanciated 00525 //with an allocator that do not have a default constructor. 00526 SlistInt slint1(stack1); 00527 slint1.push_front(1); 00528 slint1.insert_after(slint1.before_begin(), 10, 0); 00529 greater<int> gt; 00530 slint1.sort(gt); 00531 CPPUNIT_ASSERT( slint1.front() == 1 ); 00532 slint1.sort(); 00533 SlistInt::iterator slit(slint1.begin()); 00534 advance(slit, 10); 00535 CPPUNIT_ASSERT( *slit == 1 ); 00536 } 00537 # endif 00538 #endif 00539 } 00540 00541 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && \ 00542 (!defined (_STLP_USE_PTR_SPECIALIZATIONS) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)) 00543 # if !defined (__DMC__) 00544 /* Simple compilation test: Check that nested types like iterator 00545 * can be access even if type used to instanciate container is not 00546 * yet completely defined. 00547 */ 00548 class IncompleteClass 00549 { 00550 slist<IncompleteClass> instances; 00551 typedef slist<IncompleteClass>::iterator it; 00552 }; 00553 # endif 00554 #endif Generated on Sun May 27 2012 04:35:48 for ReactOS by
1.7.6.1
|