Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenswap_test.cpp
Go to the documentation of this file.
00001 #include <vector> 00002 #include <algorithm> 00003 #include <vector> 00004 #include <queue> 00005 00006 #if 0 /* temporary: investigation of problem with swap */ 00007 #include <iostream> 00008 #include <typeinfo> 00009 #endif 00010 00011 #include "cppunit/cppunit_proxy.h" 00012 00013 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00014 using namespace std; 00015 #endif 00016 00017 // 00018 // TestCase class 00019 // 00020 class SwapTest : public CPPUNIT_NS::TestCase 00021 { 00022 CPPUNIT_TEST_SUITE(SwapTest); 00023 CPPUNIT_TEST(swap1); 00024 CPPUNIT_TEST(swprnge1); 00025 CPPUNIT_TEST(swap_container_non_spec); 00026 #if defined (STLPORT) && \ 00027 !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) && !defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) 00028 CPPUNIT_IGNORE; 00029 #endif 00030 CPPUNIT_TEST(swap_container_spec); 00031 CPPUNIT_TEST_SUITE_END(); 00032 00033 protected: 00034 void swap1(); 00035 void swprnge1(); 00036 void swap_container_non_spec(); 00037 void swap_container_spec(); 00038 }; 00039 00040 CPPUNIT_TEST_SUITE_REGISTRATION(SwapTest); 00041 00042 // 00043 // tests implementation 00044 // 00045 void SwapTest::swap1() 00046 { 00047 int a = 42; 00048 int b = 19; 00049 swap(a, b); 00050 00051 CPPUNIT_ASSERT(a==19); 00052 CPPUNIT_ASSERT(b==42); 00053 } 00054 00055 void SwapTest::swprnge1() 00056 { 00057 char word1[] = "World"; 00058 char word2[] = "Hello"; 00059 swap_ranges((char*)word1, (char*)word1 + ::strlen(word1), (char*)word2); 00060 CPPUNIT_ASSERT(!strcmp(word1, "Hello")); 00061 CPPUNIT_ASSERT(!strcmp(word2, "World")); 00062 } 00063 00064 class Obj 00065 { 00066 public: 00067 Obj() : 00068 v( 0 ) 00069 { } 00070 Obj( const Obj& ) : 00071 v( 1 ) 00072 { } 00073 00074 Obj& operator =( const Obj& ) 00075 { v = 2; return *this; } 00076 00077 int v; 00078 }; 00079 00080 /* 00081 * Following two tests check the corectness of specialization of swap(): 00082 * for containers with container::swap method swap( a, b ) should 00083 * use a.swap( b ), but don't try to do this substitution for container 00084 * without swap method (in this case swap should be made via explicit members 00085 * exchange; this assume usage of temporary object) 00086 * 00087 */ 00088 void SwapTest::swap_container_non_spec() 00089 { 00090 queue<Obj> v1; 00091 queue<Obj> v2; 00092 00093 v1.push( Obj() ); 00094 v1.back().v = -1; 00095 v1.push( Obj() ); 00096 v1.back().v = -2; 00097 00098 v2.push( Obj() ); 00099 v2.back().v = 10; 00100 v2.push( Obj() ); 00101 v2.back().v = 11; 00102 v2.push( Obj() ); 00103 v2.back().v = 12; 00104 00105 CPPUNIT_CHECK( v1.size() == 2 ); 00106 CPPUNIT_CHECK( v2.size() == 3 ); 00107 00108 swap( v1, v2 ); // this shouldn't try make it as v1.swap( v2 ), no queue::swap method! 00109 00110 CPPUNIT_CHECK( v1.size() == 3 ); 00111 CPPUNIT_CHECK( v2.size() == 2 ); 00112 00113 // either copy constructor or assignment operator 00114 CPPUNIT_CHECK( v1.front().v == 1 || v1.front().v == 2 ); 00115 CPPUNIT_CHECK( v1.back().v == 1 || v1.back().v == 2 ); 00116 CPPUNIT_CHECK( v2.front().v == 1 || v2.front().v == 2 ); 00117 CPPUNIT_CHECK( v2.back().v == 1 || v2.back().v == 2 ); 00118 } 00119 00120 void SwapTest::swap_container_spec() 00121 { 00122 #if 0 /* temporary: investigation of problem with swap */ 00123 if ( typeid(/* _STLP_PRIV */ _SwapImplemented<vector<Obj> >::_Ret) == typeid(_STLP_PRIV __false_type) ) { 00124 cerr << "false type" << endl; 00125 } else if ( typeid(/* _STLP_PRIV */ _SwapImplemented<vector<Obj> >::_Ret) == typeid(_STLP_PRIV __true_type) ) { 00126 cerr << "true type" << endl; 00127 } else { 00128 cerr << "unknown type" << endl; 00129 } 00130 #endif /* end of temporary */ 00131 #if !defined (STLPORT) || \ 00132 defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER) || defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) 00133 vector<Obj> v1; 00134 vector<Obj> v2; 00135 00136 v1.push_back( Obj() ); 00137 v1.push_back( Obj() ); 00138 00139 v1[0].v = -1; 00140 v1[1].v = -2; 00141 00142 v2.push_back( Obj() ); 00143 v2.push_back( Obj() ); 00144 v2.push_back( Obj() ); 00145 00146 v2[0].v = 10; 00147 v2[1].v = 11; 00148 v2[2].v = 12; 00149 00150 CPPUNIT_CHECK( v1.size() == 2 ); 00151 CPPUNIT_CHECK( v2.size() == 3 ); 00152 00153 swap( v1, v2 ); // this should has effect v1.swap( v2 ) 00154 00155 CPPUNIT_CHECK( v1.size() == 3 ); 00156 CPPUNIT_CHECK( v2.size() == 2 ); 00157 00158 CPPUNIT_CHECK( v1[0].v == 10 ); 00159 CPPUNIT_CHECK( v1[1].v == 11 ); 00160 CPPUNIT_CHECK( v1[2].v == 12 ); 00161 00162 CPPUNIT_CHECK( v2[0].v == -1 ); 00163 CPPUNIT_CHECK( v2[1].v == -2 ); 00164 #endif 00165 } Generated on Sat May 26 2012 04:34:47 for ReactOS by
1.7.6.1
|