Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenalg_test.cpp
Go to the documentation of this file.
00001 #include <list> 00002 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00003 # include <slist> 00004 #endif 00005 #include <deque> 00006 #include <vector> 00007 #include <algorithm> 00008 #include <functional> 00009 #include <map> 00010 #include <string> 00011 00012 #include "cppunit/cppunit_proxy.h" 00013 00014 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00015 using namespace std; 00016 #endif 00017 00018 // 00019 // TestCase class 00020 // 00021 class AlgTest : public CPPUNIT_NS::TestCase 00022 { 00023 CPPUNIT_TEST_SUITE(AlgTest); 00024 CPPUNIT_TEST(min_max); 00025 CPPUNIT_TEST(count_test); 00026 CPPUNIT_TEST(sort_test); 00027 CPPUNIT_TEST(search_n_test); 00028 CPPUNIT_TEST(find_first_of_test); 00029 CPPUNIT_TEST(find_first_of_nsc_test); 00030 CPPUNIT_TEST_SUITE_END(); 00031 00032 protected: 00033 void min_max(); 00034 void count_test(); 00035 void sort_test(); 00036 void search_n_test(); 00037 void find_first_of_test(); 00038 void find_first_of_nsc_test(); 00039 }; 00040 00041 CPPUNIT_TEST_SUITE_REGISTRATION(AlgTest); 00042 00043 // 00044 // tests implementation 00045 // 00046 void AlgTest::min_max() 00047 { 00048 int i = min(4, 7); 00049 CPPUNIT_ASSERT( i == 4 ); 00050 char c = max('a', 'z'); 00051 CPPUNIT_ASSERT( c == 'z' ); 00052 00053 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00054 c = min('a', 'z', greater<char>()); 00055 CPPUNIT_ASSERT( c == 'z' ); 00056 i = max(4, 7, greater<int>()); 00057 CPPUNIT_ASSERT( i == 4 ); 00058 #endif 00059 } 00060 00061 void AlgTest::count_test() 00062 { 00063 { 00064 int i[] = { 1, 4, 2, 8, 2, 2 }; 00065 int n = count(i, i + 6, 2); 00066 CPPUNIT_ASSERT(n==3); 00067 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS) 00068 n = 0; 00069 count(i, i + 6, 2, n); 00070 CPPUNIT_ASSERT(n==3); 00071 #endif 00072 } 00073 { 00074 vector<int> i; 00075 i.push_back(1); 00076 i.push_back(4); 00077 i.push_back(2); 00078 i.push_back(8); 00079 i.push_back(2); 00080 i.push_back(2); 00081 int n = count(i.begin(), i.end(), 2); 00082 CPPUNIT_ASSERT(n==3); 00083 #if defined (STLPORT) && !defined (_STLP_NO_ANACHRONISMS) 00084 n = 0; 00085 count(i.begin(), i.end(), 2, n); 00086 CPPUNIT_ASSERT(n==3); 00087 #endif 00088 } 00089 } 00090 00091 void AlgTest::sort_test() 00092 { 00093 { 00094 vector<int> years; 00095 years.push_back(1962); 00096 years.push_back(1992); 00097 years.push_back(2001); 00098 years.push_back(1999); 00099 sort(years.begin(), years.end()); 00100 CPPUNIT_ASSERT(years[0]==1962); 00101 CPPUNIT_ASSERT(years[1]==1992); 00102 CPPUNIT_ASSERT(years[2]==1999); 00103 CPPUNIT_ASSERT(years[3]==2001); 00104 } 00105 { 00106 deque<int> years; 00107 years.push_back(1962); 00108 years.push_back(1992); 00109 years.push_back(2001); 00110 years.push_back(1999); 00111 sort(years.begin(), years.end()); // <-- changed! 00112 CPPUNIT_ASSERT(years[0]==1962); 00113 CPPUNIT_ASSERT(years[1]==1992); 00114 CPPUNIT_ASSERT(years[2]==1999); 00115 CPPUNIT_ASSERT(years[3]==2001); 00116 } 00117 } 00118 00119 #define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0]) 00120 00121 void AlgTest::search_n_test() 00122 { 00123 int ints[] = {0, 1, 2, 3, 3, 4, 4, 4, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}; 00124 00125 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00126 //search_n 00127 //Forward iterator 00128 { 00129 slist<int> slint(ints, ints + ARRAY_SIZE(ints)); 00130 slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 2); 00131 CPPUNIT_ASSERT( slit != slint.end() ); 00132 CPPUNIT_ASSERT( *(slit++) == 2 ); 00133 CPPUNIT_ASSERT( *slit == 2 ); 00134 } 00135 #endif 00136 00137 //Bidirectionnal iterator 00138 { 00139 list<int> lint(ints, ints + ARRAY_SIZE(ints)); 00140 list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 3); 00141 CPPUNIT_ASSERT( lit != lint.end() ); 00142 CPPUNIT_ASSERT( *(lit++) == 3 ); 00143 CPPUNIT_ASSERT( *(lit++) == 3 ); 00144 CPPUNIT_ASSERT( *lit == 3 ); 00145 } 00146 00147 //Random access iterator 00148 { 00149 deque<int> dint(ints, ints + ARRAY_SIZE(ints)); 00150 deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 4); 00151 CPPUNIT_ASSERT( dit != dint.end() ); 00152 CPPUNIT_ASSERT( *(dit++) == 4 ); 00153 CPPUNIT_ASSERT( *(dit++) == 4 ); 00154 CPPUNIT_ASSERT( *(dit++) == 4 ); 00155 CPPUNIT_ASSERT( *dit == 4 ); 00156 } 00157 00158 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00159 //search_n with predicate 00160 //Forward iterator 00161 { 00162 slist<int> slint(ints, ints + ARRAY_SIZE(ints)); 00163 slist<int>::iterator slit = search_n(slint.begin(), slint.end(), 2, 1, greater<int>()); 00164 CPPUNIT_ASSERT( slit != slint.end() ); 00165 CPPUNIT_ASSERT( *(slit++) > 1 ); 00166 CPPUNIT_ASSERT( *slit > 2 ); 00167 } 00168 #endif 00169 00170 //Bidirectionnal iterator 00171 { 00172 list<int> lint(ints, ints + ARRAY_SIZE(ints)); 00173 list<int>::iterator lit = search_n(lint.begin(), lint.end(), 3, 2, greater<int>()); 00174 CPPUNIT_ASSERT( lit != lint.end() ); 00175 CPPUNIT_ASSERT( *(lit++) > 2 ); 00176 CPPUNIT_ASSERT( *(lit++) > 2 ); 00177 CPPUNIT_ASSERT( *lit > 2 ); 00178 } 00179 00180 //Random access iterator 00181 { 00182 deque<int> dint(ints, ints + ARRAY_SIZE(ints)); 00183 deque<int>::iterator dit = search_n(dint.begin(), dint.end(), 4, 3, greater<int>()); 00184 CPPUNIT_ASSERT( dit != dint.end() ); 00185 CPPUNIT_ASSERT( *(dit++) > 3 ); 00186 CPPUNIT_ASSERT( *(dit++) > 3 ); 00187 CPPUNIT_ASSERT( *(dit++) > 3 ); 00188 CPPUNIT_ASSERT( *dit > 3 ); 00189 } 00190 00191 // test for bug reported by Jim Xochellis 00192 { 00193 int array[] = {0, 0, 1, 0, 1, 1}; 00194 int* array_end = array + sizeof(array) / sizeof(*array); 00195 CPPUNIT_ASSERT(search_n(array, array_end, 3, 1) == array_end); 00196 } 00197 00198 // test for bug with counter == 1, reported by Timmie Smith 00199 { 00200 int array[] = {0, 1, 2, 3, 4, 5}; 00201 int* array_end = array + sizeof(array) / sizeof(*array); 00202 CPPUNIT_ASSERT( search_n(array, array_end, 1, 1, equal_to<int>() ) == &array[1] ); 00203 } 00204 } 00205 00206 struct MyIntComparable { 00207 MyIntComparable(int val) : _val(val) {} 00208 bool operator == (const MyIntComparable& other) const 00209 { return _val == other._val; } 00210 00211 private: 00212 int _val; 00213 }; 00214 00215 void AlgTest::find_first_of_test() 00216 { 00217 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00218 slist<int> intsl; 00219 intsl.push_front(1); 00220 intsl.push_front(2); 00221 00222 { 00223 vector<int> intv; 00224 intv.push_back(0); 00225 intv.push_back(1); 00226 intv.push_back(2); 00227 intv.push_back(3); 00228 00229 vector<int>::iterator first; 00230 first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end()); 00231 CPPUNIT_ASSERT( first != intv.end() ); 00232 CPPUNIT_ASSERT( *first == 1 ); 00233 } 00234 { 00235 vector<int> intv; 00236 intv.push_back(3); 00237 intv.push_back(2); 00238 intv.push_back(1); 00239 intv.push_back(0); 00240 00241 vector<int>::iterator first; 00242 first = find_first_of(intv.begin(), intv.end(), intsl.begin(), intsl.end()); 00243 CPPUNIT_ASSERT( first != intv.end() ); 00244 CPPUNIT_ASSERT( *first == 2 ); 00245 } 00246 #endif 00247 00248 list<int> intl; 00249 intl.push_front(1); 00250 intl.push_front(2); 00251 00252 { 00253 vector<int> intv; 00254 intv.push_back(0); 00255 intv.push_back(1); 00256 intv.push_back(2); 00257 intv.push_back(3); 00258 00259 vector<int>::iterator first; 00260 first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end()); 00261 CPPUNIT_ASSERT( first != intv.end() ); 00262 CPPUNIT_ASSERT( *first == 1 ); 00263 } 00264 { 00265 vector<int> intv; 00266 intv.push_back(3); 00267 intv.push_back(2); 00268 intv.push_back(1); 00269 intv.push_back(0); 00270 00271 vector<int>::iterator first; 00272 first = find_first_of(intv.begin(), intv.end(), intl.begin(), intl.end()); 00273 CPPUNIT_ASSERT( first != intv.end() ); 00274 CPPUNIT_ASSERT( *first == 2 ); 00275 } 00276 { 00277 char chars[] = {1, 2}; 00278 00279 vector<int> intv; 00280 intv.push_back(0); 00281 intv.push_back(1); 00282 intv.push_back(2); 00283 intv.push_back(3); 00284 00285 vector<int>::iterator first; 00286 first = find_first_of(intv.begin(), intv.end(), chars, chars + sizeof(chars)); 00287 CPPUNIT_ASSERT( first != intv.end() ); 00288 CPPUNIT_ASSERT( *first == 1 ); 00289 } 00290 { 00291 unsigned char chars[] = {1, 2, 255}; 00292 00293 vector<int> intv; 00294 intv.push_back(-10); 00295 intv.push_back(1029); 00296 intv.push_back(255); 00297 intv.push_back(4); 00298 00299 vector<int>::iterator first; 00300 first = find_first_of(intv.begin(), intv.end(), chars, chars + sizeof(chars)); 00301 CPPUNIT_ASSERT( first != intv.end() ); 00302 CPPUNIT_ASSERT( *first == 255 ); 00303 } 00304 { 00305 signed char chars[] = {93, 2, -101, 13}; 00306 00307 vector<int> intv; 00308 intv.push_back(-10); 00309 intv.push_back(1029); 00310 intv.push_back(-2035); 00311 intv.push_back(-101); 00312 intv.push_back(4); 00313 00314 vector<int>::iterator first; 00315 first = find_first_of(intv.begin(), intv.end(), chars, chars + sizeof(chars)); 00316 CPPUNIT_ASSERT( first != intv.end() ); 00317 CPPUNIT_ASSERT( *first == -101 ); 00318 } 00319 { 00320 char chars[] = {1, 2}; 00321 00322 vector<MyIntComparable> intv; 00323 intv.push_back(0); 00324 intv.push_back(1); 00325 intv.push_back(2); 00326 intv.push_back(3); 00327 00328 vector<MyIntComparable>::iterator first; 00329 first = find_first_of(intv.begin(), intv.end(), chars, chars + sizeof(chars)); 00330 CPPUNIT_ASSERT( first != intv.end() ); 00331 CPPUNIT_ASSERT( *first == 1 ); 00332 } 00333 } 00334 00335 typedef pair<int, string> Pair; 00336 00337 struct ValueFinder : 00338 public binary_function<const Pair&, const string&, bool> 00339 { 00340 bool operator () ( const Pair &p, const string& value ) const 00341 { return p.second == value; } 00342 }; 00343 00344 void AlgTest::find_first_of_nsc_test() 00345 { 00346 // Non-symmetrical comparator 00347 00348 map<int, string> m; 00349 vector<string> values; 00350 00351 m[1] = "one"; 00352 m[4] = "four"; 00353 m[10] = "ten"; 00354 m[20] = "twenty"; 00355 00356 values.push_back( "four" ); 00357 values.push_back( "ten" ); 00358 00359 map<int, string>::iterator i = find_first_of(m.begin(), m.end(), values.begin(), values.end(), ValueFinder()); 00360 00361 CPPUNIT_ASSERT( i != m.end() ); 00362 CPPUNIT_ASSERT( i->first == 4 || i->first == 10 ); 00363 CPPUNIT_ASSERT( i->second == "four" || i->second == "ten" ); 00364 } Generated on Mon May 28 2012 04:35:04 for ReactOS by
1.7.6.1
|