Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenfind_test.cpp
Go to the documentation of this file.
00001 #include <vector> 00002 #include <algorithm> 00003 00004 #include "cppunit/cppunit_proxy.h" 00005 00006 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00007 using namespace std; 00008 #endif 00009 00010 // 00011 // TestCase class 00012 // 00013 class FindTest : public CPPUNIT_NS::TestCase 00014 { 00015 CPPUNIT_TEST_SUITE(FindTest); 00016 CPPUNIT_TEST(find0); 00017 CPPUNIT_TEST(find1); 00018 CPPUNIT_TEST(findif0); 00019 CPPUNIT_TEST(findif1); 00020 CPPUNIT_TEST(find_char); 00021 CPPUNIT_TEST_SUITE_END(); 00022 00023 protected: 00024 void find0(); 00025 void find1(); 00026 void findif0(); 00027 void findif1(); 00028 void find_char(); 00029 static bool odd(int a_); 00030 static bool div_3(int a_); 00031 }; 00032 00033 CPPUNIT_TEST_SUITE_REGISTRATION(FindTest); 00034 00035 // 00036 // tests implementation 00037 // 00038 void FindTest::find0() 00039 { 00040 int numbers[10] = { 0, 1, 4, 9, 16, 25, 36, 49, 64 }; 00041 00042 int *location = find((int*)numbers, (int*)numbers + 10, 25); 00043 00044 CPPUNIT_ASSERT((location - numbers)==5); 00045 00046 int *out_range = find((int*)numbers, (int*)numbers + 10, 128); 00047 00048 CPPUNIT_ASSERT( out_range == (int *)(numbers + 10) ); 00049 } 00050 00051 struct Key 00052 { 00053 int data; 00054 00055 /* This operator should rather be global and commutative 00056 but implementing it this way show that STLport used to 00057 ask too much from the user code. */ 00058 bool operator == (int d) const 00059 { 00060 return data == d; 00061 } 00062 }; 00063 00064 void FindTest::find1() 00065 { 00066 int years[] = { 1942, 1952, 1962, 1972, 1982, 1992 }; 00067 00068 const unsigned yearCount = sizeof(years) / sizeof(years[0]); 00069 int* location = find((int*)years, (int*)years + yearCount, 1972); 00070 00071 CPPUNIT_ASSERT((location - years)==3); 00072 } 00073 00074 void FindTest::findif0() 00075 { 00076 { 00077 int numbers[6] = { 2, 4, 8, 15, 32, 64 }; 00078 int *location = find_if((int*)numbers, (int*)numbers + 6, odd); 00079 00080 CPPUNIT_ASSERT((location - numbers)==3); 00081 00082 int numbers_even[6] = { 2, 4, 8, 16, 32, 64 }; 00083 00084 int *out_range = find_if((int*)numbers_even, (int*)numbers_even + 6, odd); 00085 00086 CPPUNIT_ASSERT( out_range == (int *)(numbers_even + 6) ); 00087 } 00088 00089 { 00090 Key keys[10] = { {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} }; 00091 Key const* k = find(keys + 0, keys + 10, 5); 00092 CPPUNIT_ASSERT( k == keys + 10 ); 00093 } 00094 } 00095 00096 void FindTest::findif1() 00097 { 00098 typedef vector <int> IntVec; 00099 IntVec v(10); 00100 for(int i = 0; (size_t)i < v.size(); ++i) 00101 v[i] =(i + 1) *(i + 1); 00102 IntVec::iterator iter; 00103 iter = find_if(v.begin(), v.end(), div_3); 00104 CPPUNIT_ASSERT((iter - v.begin())==2); 00105 } 00106 00107 bool FindTest::odd(int a_) 00108 { 00109 return (a_ % 2) != 0; 00110 } 00111 00112 bool FindTest::div_3(int a_) 00113 { 00114 return a_ % 3 ? 0 : 1; 00115 } 00116 00117 void FindTest::find_char() 00118 { 00119 char str[] = "abcdefghij"; 00120 char *pstr = (char*)str; 00121 const char* cpstr = (const char*)str; 00122 size_t str_size = sizeof(str) / sizeof(char); 00123 00124 char *d = find(pstr, pstr + str_size, 'd'); 00125 CPPUNIT_ASSERT( *d == 'd' ); 00126 00127 const char *e = find(cpstr, cpstr + str_size, 'e'); 00128 CPPUNIT_ASSERT( *e == 'e' ); 00129 00130 char *last = find(pstr, pstr + str_size, 'x'); 00131 CPPUNIT_ASSERT( last == pstr + str_size ); 00132 00133 const char *clast = find(cpstr, cpstr + str_size, 'x'); 00134 CPPUNIT_ASSERT( clast == cpstr + str_size ); 00135 } Generated on Mon May 28 2012 04:35:11 for ReactOS by
1.7.6.1
|