Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygendeque_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 <deque> 00006 #include <algorithm> 00007 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00008 # include <stdexcept> 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 DequeTest : public CPPUNIT_NS::TestCase 00021 { 00022 CPPUNIT_TEST_SUITE(DequeTest); 00023 CPPUNIT_TEST(deque1); 00024 CPPUNIT_TEST(at); 00025 CPPUNIT_TEST(insert); 00026 CPPUNIT_TEST(erase); 00027 CPPUNIT_TEST(auto_ref); 00028 CPPUNIT_TEST(allocator_with_state); 00029 #if defined (STLPORT) && defined (_STLP_NO_MEMBER_TEMPLATES) 00030 CPPUNIT_IGNORE; 00031 #endif 00032 CPPUNIT_TEST(optimizations_check); 00033 CPPUNIT_TEST_SUITE_END(); 00034 00035 protected: 00036 void deque1(); 00037 void insert(); 00038 void erase(); 00039 void at(); 00040 void auto_ref(); 00041 void allocator_with_state(); 00042 void optimizations_check(); 00043 }; 00044 00045 CPPUNIT_TEST_SUITE_REGISTRATION(DequeTest); 00046 00047 // 00048 // tests implementation 00049 // 00050 void DequeTest::deque1() 00051 { 00052 deque<int> d; 00053 d.push_back(4); 00054 d.push_back(9); 00055 d.push_back(16); 00056 d.push_front(1); 00057 00058 CPPUNIT_ASSERT( d[0] == 1 ); 00059 CPPUNIT_ASSERT( d[1] == 4 ); 00060 CPPUNIT_ASSERT( d[2] == 9 ); 00061 CPPUNIT_ASSERT( d[3] == 16 ); 00062 00063 d.pop_front(); 00064 d[2] = 25; 00065 00066 CPPUNIT_ASSERT( d[0] == 4 ); 00067 CPPUNIT_ASSERT( d[1] == 9 ); 00068 CPPUNIT_ASSERT( d[2] == 25 ); 00069 00070 //Some compile time tests: 00071 deque<int>::iterator dit = d.begin(); 00072 deque<int>::const_iterator cdit(d.begin()); 00073 CPPUNIT_ASSERT( (dit - cdit) == 0 ); 00074 CPPUNIT_ASSERT( (cdit - dit) == 0 ); 00075 CPPUNIT_ASSERT( (dit - dit) == 0 ); 00076 CPPUNIT_ASSERT( (cdit - cdit) == 0 ); 00077 CPPUNIT_ASSERT(!((dit < cdit) || (dit > cdit) || (dit != cdit) || !(dit <= cdit) || !(dit >= cdit))); 00078 } 00079 00080 void DequeTest::insert() 00081 { 00082 deque<int> d; 00083 d.push_back(0); 00084 d.push_back(1); 00085 d.push_back(2); 00086 CPPUNIT_ASSERT( d.size() == 3 ); 00087 00088 deque<int>::iterator dit; 00089 00090 //Insertion before begin: 00091 dit = d.insert(d.begin(), 3); 00092 CPPUNIT_ASSERT( dit != d.end() ); 00093 CPPUNIT_CHECK( *dit == 3 ); 00094 CPPUNIT_ASSERT( d.size() == 4 ); 00095 CPPUNIT_ASSERT( d[0] == 3 ); 00096 00097 //Insertion after begin: 00098 dit = d.insert(d.begin() + 1, 4); 00099 CPPUNIT_ASSERT( dit != d.end() ); 00100 CPPUNIT_CHECK( *dit == 4 ); 00101 CPPUNIT_ASSERT( d.size() == 5 ); 00102 CPPUNIT_ASSERT( d[1] == 4 ); 00103 00104 //Insertion at end: 00105 dit = d.insert(d.end(), 5); 00106 CPPUNIT_ASSERT( dit != d.end() ); 00107 CPPUNIT_CHECK( *dit == 5 ); 00108 CPPUNIT_ASSERT( d.size() == 6 ); 00109 CPPUNIT_ASSERT( d[5] == 5 ); 00110 00111 //Insertion before last element: 00112 dit = d.insert(d.end() - 1, 6); 00113 CPPUNIT_ASSERT( dit != d.end() ); 00114 CPPUNIT_CHECK( *dit == 6 ); 00115 CPPUNIT_ASSERT( d.size() == 7 ); 00116 CPPUNIT_ASSERT( d[5] == 6 ); 00117 00118 //Insertion of several elements before begin 00119 d.insert(d.begin(), 2, 7); 00120 CPPUNIT_ASSERT( d.size() == 9 ); 00121 CPPUNIT_ASSERT( d[0] == 7 ); 00122 CPPUNIT_ASSERT( d[1] == 7 ); 00123 00124 //Insertion of several elements after begin 00125 //There is more elements to insert than elements before insertion position 00126 d.insert(d.begin() + 1, 2, 8); 00127 CPPUNIT_ASSERT( d.size() == 11 ); 00128 CPPUNIT_ASSERT( d[1] == 8 ); 00129 CPPUNIT_ASSERT( d[2] == 8 ); 00130 00131 //There is less elements to insert than elements before insertion position 00132 d.insert(d.begin() + 3, 2, 9); 00133 CPPUNIT_ASSERT( d.size() == 13 ); 00134 CPPUNIT_ASSERT( d[3] == 9 ); 00135 CPPUNIT_ASSERT( d[4] == 9 ); 00136 00137 //Insertion of several elements at end: 00138 d.insert(d.end(), 2, 10); 00139 CPPUNIT_ASSERT( d.size() == 15 ); 00140 CPPUNIT_ASSERT( d[14] == 10 ); 00141 CPPUNIT_ASSERT( d[13] == 10 ); 00142 00143 //Insertion of several elements before last: 00144 //There is more elements to insert than elements after insertion position 00145 d.insert(d.end() - 1, 2, 11); 00146 CPPUNIT_ASSERT( d.size() == 17 ); 00147 CPPUNIT_ASSERT( d[15] == 11 ); 00148 CPPUNIT_ASSERT( d[14] == 11 ); 00149 00150 //There is less elements to insert than elements after insertion position 00151 d.insert(d.end() - 3, 2, 12); 00152 CPPUNIT_ASSERT( d.size() == 19 ); 00153 CPPUNIT_ASSERT( d[15] == 12 ); 00154 CPPUNIT_ASSERT( d[14] == 12 ); 00155 } 00156 00157 void DequeTest::at() { 00158 deque<int> d; 00159 deque<int> const& cd = d; 00160 00161 d.push_back(10); 00162 CPPUNIT_ASSERT( d.at(0) == 10 ); 00163 d.at(0) = 20; 00164 CPPUNIT_ASSERT( cd.at(0) == 20 ); 00165 00166 #if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00167 for (;;) { 00168 try { 00169 d.at(1) = 20; 00170 CPPUNIT_ASSERT(false); 00171 } 00172 catch (out_of_range const&) { 00173 return; 00174 } 00175 catch (...) { 00176 CPPUNIT_ASSERT(false); 00177 } 00178 } 00179 #endif 00180 } 00181 00182 void DequeTest::auto_ref() 00183 { 00184 int i; 00185 deque<int> ref; 00186 for (i = 0; i < 5; ++i) { 00187 ref.push_back(i); 00188 } 00189 00190 deque<deque<int> > d_d_int(1, ref); 00191 d_d_int.push_back(d_d_int[0]); 00192 d_d_int.push_back(ref); 00193 d_d_int.push_back(d_d_int[0]); 00194 d_d_int.push_back(d_d_int[0]); 00195 d_d_int.push_back(ref); 00196 00197 for (i = 0; i < 5; ++i) { 00198 CPPUNIT_ASSERT( d_d_int[i] == ref ); 00199 } 00200 } 00201 00202 void DequeTest::allocator_with_state() 00203 { 00204 char buf1[1024]; 00205 StackAllocator<int> stack1(buf1, buf1 + sizeof(buf1)); 00206 00207 char buf2[1024]; 00208 StackAllocator<int> stack2(buf2, buf2 + sizeof(buf2)); 00209 00210 { 00211 typedef deque<int, StackAllocator<int> > DequeInt; 00212 DequeInt dint1(10, 0, stack1); 00213 DequeInt dint1Cpy(dint1); 00214 00215 DequeInt dint2(10, 1, stack2); 00216 DequeInt dint2Cpy(dint2); 00217 00218 dint1.swap(dint2); 00219 00220 CPPUNIT_ASSERT( dint1.get_allocator().swaped() ); 00221 CPPUNIT_ASSERT( dint2.get_allocator().swaped() ); 00222 00223 CPPUNIT_ASSERT( dint1 == dint2Cpy ); 00224 CPPUNIT_ASSERT( dint2 == dint1Cpy ); 00225 CPPUNIT_ASSERT( dint1.get_allocator() == stack2 ); 00226 CPPUNIT_ASSERT( dint2.get_allocator() == stack1 ); 00227 } 00228 CPPUNIT_ASSERT( stack1.ok() ); 00229 CPPUNIT_ASSERT( stack2.ok() ); 00230 } 00231 00232 struct Point { 00233 int x, y; 00234 }; 00235 00236 struct PointEx : public Point { 00237 PointEx() : builtFromBase(false) {} 00238 PointEx(const Point&) : builtFromBase(true) {} 00239 00240 bool builtFromBase; 00241 }; 00242 00243 #if defined (STLPORT) 00244 # if defined (_STLP_USE_NAMESPACES) 00245 namespace std { 00246 # endif 00247 _STLP_TEMPLATE_NULL 00248 struct __type_traits<PointEx> { 00249 typedef __false_type has_trivial_default_constructor; 00250 typedef __true_type has_trivial_copy_constructor; 00251 typedef __true_type has_trivial_assignment_operator; 00252 typedef __true_type has_trivial_destructor; 00253 typedef __true_type is_POD_type; 00254 }; 00255 # if defined (_STLP_USE_NAMESPACES) 00256 } 00257 # endif 00258 #endif 00259 00260 //This test check that deque implementation do not over optimize 00261 //operation as PointEx copy constructor is trivial 00262 void DequeTest::optimizations_check() 00263 { 00264 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) 00265 deque<Point> d1(1); 00266 CPPUNIT_ASSERT( d1.size() == 1 ); 00267 00268 deque<PointEx> d2(d1.begin(), d1.end()); 00269 CPPUNIT_ASSERT( d2.size() == 1 ); 00270 CPPUNIT_ASSERT( d2[0].builtFromBase == true ); 00271 00272 d2.insert(d2.end(), d1.begin(), d1.end()); 00273 CPPUNIT_ASSERT( d2.size() == 2 ); 00274 CPPUNIT_ASSERT( d2[1].builtFromBase == true ); 00275 #endif 00276 } 00277 00278 void DequeTest::erase() 00279 { 00280 deque<int> dint; 00281 dint.push_back(3); 00282 dint.push_front(2); 00283 dint.push_back(4); 00284 dint.push_front(1); 00285 dint.push_back(5); 00286 dint.push_front(0); 00287 dint.push_back(6); 00288 00289 deque<int>::iterator it(dint.begin() + 1); 00290 CPPUNIT_ASSERT( *it == 1 ); 00291 00292 dint.erase(dint.begin()); 00293 CPPUNIT_ASSERT( *it == 1 ); 00294 00295 it = dint.end() - 2; 00296 CPPUNIT_ASSERT( *it == 5 ); 00297 00298 dint.erase(dint.end() - 1); 00299 CPPUNIT_ASSERT( *it == 5 ); 00300 00301 dint.push_back(6); 00302 dint.push_front(0); 00303 00304 it = dint.begin() + 2; 00305 CPPUNIT_ASSERT( *it == 2 ); 00306 00307 dint.erase(dint.begin(), dint.begin() + 2); 00308 CPPUNIT_ASSERT( *it == 2 ); 00309 00310 it = dint.end() - 3; 00311 CPPUNIT_ASSERT( *it == 4 ); 00312 00313 dint.erase(dint.end() - 2, dint.end()); 00314 CPPUNIT_ASSERT( *it == 4 ); 00315 } 00316 00317 #if (!defined (STLPORT) || \ 00318 (!defined (_STLP_USE_PTR_SPECIALIZATIONS) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION))) && \ 00319 (!defined (_MSC_VER) || (_MSC_VER > 1400)) && \ 00320 (!defined(__GNUC__) || (__GNUC__ < 4) || (__GNUC_MINOR__ < 3)) 00321 /* Simple compilation test: Check that nested types like iterator 00322 * can be access even if type used to instanciate container is not 00323 * yet completely defined. 00324 */ 00325 class IncompleteClass 00326 { 00327 deque<IncompleteClass> instances; 00328 typedef deque<IncompleteClass>::size_type size; 00329 }; 00330 #endif Generated on Fri May 25 2012 04:33:51 for ReactOS by
1.7.6.1
|