Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygeniter_test.cpp
Go to the documentation of this file.
00001 #include <vector> 00002 #include <list> 00003 #include <algorithm> 00004 #include <numeric> 00005 00006 #include "iota.h" 00007 #include "cppunit/cppunit_proxy.h" 00008 00009 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00010 using namespace std; 00011 #endif 00012 00013 // 00014 // TestCase class 00015 // 00016 class IterTest : public CPPUNIT_NS::TestCase 00017 { 00018 CPPUNIT_TEST_SUITE(IterTest); 00019 CPPUNIT_TEST(iter1); 00020 CPPUNIT_TEST(iter3); 00021 CPPUNIT_TEST(iter4); 00022 CPPUNIT_TEST(iterswp0); 00023 CPPUNIT_TEST(iterswp1); 00024 CPPUNIT_TEST(iterswp2); 00025 CPPUNIT_TEST(iterswp3); 00026 CPPUNIT_TEST_SUITE_END(); 00027 00028 protected: 00029 void iter1(); 00030 void iter3(); 00031 void iter4(); 00032 void iterswp0(); 00033 void iterswp1(); 00034 void iterswp2(); 00035 void iterswp3(); 00036 }; 00037 00038 CPPUNIT_TEST_SUITE_REGISTRATION(IterTest); 00039 00040 // 00041 // tests implementation 00042 // 00043 void IterTest::iter1() 00044 { 00045 vector<const char*> v; // Vector of character strings. 00046 v.push_back("zippy"); // First element. 00047 v.push_back("motorboy"); // Second element. 00048 typedef vector<const char*> vec; 00049 unsigned counter = 0; 00050 for (vec::iterator i = v.begin(); i != v.end(); ++i, ++counter) { 00051 switch (counter) { 00052 case 0: 00053 CPPUNIT_ASSERT(!strcmp(*i, "zippy")); 00054 break; 00055 case 1: 00056 CPPUNIT_ASSERT(!strcmp(*i, "motorboy")); 00057 break; 00058 default: 00059 CPPUNIT_FAIL; 00060 } 00061 } 00062 } 00063 void IterTest::iter3() 00064 { 00065 typedef vector<const char*> Vec; 00066 Vec v; // Vector of character strings. 00067 v.push_back("zippy"); // First element. 00068 v.push_back("motorboy"); // Second element. 00069 Vec::reverse_iterator it; 00070 unsigned counter = 0; 00071 for (it = v.rbegin(); it != v.rend(); ++it, ++counter) { 00072 switch (counter) { 00073 case 1: 00074 CPPUNIT_ASSERT(!strcmp(*it, "zippy")); 00075 break; 00076 case 0: 00077 CPPUNIT_ASSERT(!strcmp(*it, "motorboy")); 00078 break; 00079 default: 00080 CPPUNIT_FAIL; 00081 } 00082 } 00083 } 00084 void IterTest::iter4() 00085 { 00086 vector<int> v; // Empty vector of integers. 00087 v.push_back(1); 00088 v.push_back(2); 00089 v.push_back(3); 00090 // Position immediately after last item. 00091 vector<int>::iterator i = v.end(); 00092 // Move back one and then access. 00093 CPPUNIT_ASSERT((*--i)==3); 00094 i -= 2; // Jump back two items. 00095 CPPUNIT_ASSERT((*i)==1); 00096 } 00097 void IterTest::iterswp0() 00098 { 00099 int numbers[6] = { 0, 1, 2, 3, 4, 5 }; 00100 00101 iter_swap(numbers, numbers + 3); 00102 00103 CPPUNIT_ASSERT(numbers[0]==3); 00104 CPPUNIT_ASSERT(numbers[1]==1); 00105 CPPUNIT_ASSERT(numbers[2]==2); 00106 CPPUNIT_ASSERT(numbers[3]==0); 00107 CPPUNIT_ASSERT(numbers[4]==4); 00108 CPPUNIT_ASSERT(numbers[5]==5); 00109 00110 } 00111 void IterTest::iterswp1() 00112 { 00113 vector<int> v1(6); 00114 __iota(v1.begin(), v1.end(), 0); 00115 iter_swap( v1.begin(), v1.begin() + 3 ); 00116 00117 CPPUNIT_ASSERT(v1[0]==3); 00118 CPPUNIT_ASSERT(v1[1]==1); 00119 CPPUNIT_ASSERT(v1[2]==2); 00120 CPPUNIT_ASSERT(v1[3]==0); 00121 CPPUNIT_ASSERT(v1[4]==4); 00122 CPPUNIT_ASSERT(v1[5]==5); 00123 } 00124 void IterTest::iterswp2() 00125 { 00126 vector<bool> boolVector; 00127 00128 boolVector.push_back( true ); 00129 boolVector.push_back( false ); 00130 00131 vector<bool>::iterator i1 = boolVector.begin(); 00132 vector<bool>::iterator i2 = boolVector.begin(); 00133 ++i2; 00134 00135 bool v0 = *i1; 00136 bool v1 = *i2; 00137 00138 iter_swap( i1, i2 ); 00139 00140 CPPUNIT_ASSERT(( *i1 == v1 && *i2 == v0 )); 00141 } 00142 00143 00144 void IterTest::iterswp3() 00145 { 00146 vector<int> vvref(10, 10); 00147 vector<int> lvref(10, 20); 00148 00149 vector<vector<int> > vvints(4, vvref); 00150 list<vector<int> > lvints(4, lvref); 00151 00152 iter_swap(vvints.begin(), lvints.begin()); 00153 CPPUNIT_CHECK( vvints.front() == lvref ); 00154 CPPUNIT_CHECK( lvints.front() == vvref ); 00155 00156 //const vector<vector<int> > &cvvints = vvints; 00157 //iter_swap(cvvints.begin(), lvints.begin()); 00158 //iter_swap(lvints.begin(), cvvints.begin()); 00159 00160 #if defined (STLPORT) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00161 int *pvvint = &vvints.front().front(); 00162 int *plvint = &lvints.front().front(); 00163 00164 iter_swap(vvints.begin(), lvints.begin()); 00165 //Check that elements have been swaped: 00166 CPPUNIT_CHECK( pvvint == &lvints.front().front() ); 00167 CPPUNIT_CHECK( plvint == &vvints.front().front() ); 00168 #endif 00169 } Generated on Thu May 24 2012 04:35:54 for ReactOS by
1.7.6.1
|