Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensort_test.cpp
Go to the documentation of this file.
00001 #include <vector> 00002 #include <algorithm> 00003 #include <functional> 00004 00005 #if defined (STLPORT) && defined (_STLP_DEBUG) && defined (_STLP_DEBUG_MODE_THROWS) 00006 # define _STLP_DO_CHECK_BAD_PREDICATE 00007 # include <stdexcept> 00008 #endif 00009 00010 #include "cppunit/cppunit_proxy.h" 00011 00012 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00013 using namespace std; 00014 #endif 00015 00016 // 00017 // TestCase class 00018 // 00019 class SortTest : public CPPUNIT_NS::TestCase 00020 { 00021 CPPUNIT_TEST_SUITE(SortTest); 00022 CPPUNIT_TEST(sort1); 00023 CPPUNIT_TEST(sort2); 00024 CPPUNIT_TEST(sort3); 00025 CPPUNIT_TEST(sort4); 00026 CPPUNIT_TEST(stblsrt1); 00027 CPPUNIT_TEST(stblsrt2); 00028 #if defined (_STLP_DO_CHECK_BAD_PREDICATE) 00029 CPPUNIT_TEST(bad_predicate_detected); 00030 #endif 00031 CPPUNIT_TEST_SUITE_END(); 00032 00033 protected: 00034 void sort1(); 00035 void sort2(); 00036 void sort3(); 00037 void sort4(); 00038 void stblsrt1(); 00039 void stblsrt2(); 00040 void bad_predicate_detected(); 00041 00042 static bool string_less(const char* a_, const char* b_) 00043 { 00044 return strcmp(a_, b_) < 0 ? 1 : 0; 00045 } 00046 }; 00047 00048 CPPUNIT_TEST_SUITE_REGISTRATION(SortTest); 00049 00050 // 00051 // tests implementation 00052 // 00053 void SortTest::stblsrt1() 00054 { 00055 //Check that stable_sort do sort 00056 int numbers[6] = { 1, 50, -10, 11, 42, 19 }; 00057 stable_sort(numbers, numbers + 6); 00058 //-10 1 11 19 42 50 00059 CPPUNIT_ASSERT(numbers[0]==-10); 00060 CPPUNIT_ASSERT(numbers[1]==1); 00061 CPPUNIT_ASSERT(numbers[2]==11); 00062 CPPUNIT_ASSERT(numbers[3]==19); 00063 CPPUNIT_ASSERT(numbers[4]==42); 00064 CPPUNIT_ASSERT(numbers[5]==50); 00065 00066 char const* letters[6] = {"bb", "aa", "ll", "dd", "qq", "cc" }; 00067 stable_sort(letters, letters + 6, string_less); 00068 // aa bb cc dd ll qq 00069 CPPUNIT_ASSERT( strcmp(letters[0], "aa") == 0 ); 00070 CPPUNIT_ASSERT( strcmp(letters[1], "bb") == 0 ); 00071 CPPUNIT_ASSERT( strcmp(letters[2], "cc") == 0 ); 00072 CPPUNIT_ASSERT( strcmp(letters[3], "dd") == 0 ); 00073 CPPUNIT_ASSERT( strcmp(letters[4], "ll") == 0 ); 00074 CPPUNIT_ASSERT( strcmp(letters[5], "qq") == 0 ); 00075 } 00076 00077 struct Data { 00078 Data(int index, int value) 00079 : m_index(index), m_value(value) {} 00080 00081 bool operator == (const Data& other) const 00082 { return m_index == other.m_index && m_value == other.m_value; } 00083 bool operator < (const Data& other) const 00084 { return m_value < other.m_value; } 00085 00086 private: 00087 int m_index, m_value; 00088 }; 00089 00090 void SortTest::stblsrt2() 00091 { 00092 //Check that stable_sort is stable: 00093 Data datas[] = { 00094 Data(0, 10), 00095 Data(1, 8), 00096 Data(2, 6), 00097 Data(3, 6), 00098 Data(4, 6), 00099 Data(5, 4), 00100 Data(6, 9) 00101 }; 00102 stable_sort(datas, datas + 7); 00103 00104 CPPUNIT_ASSERT( datas[0] == Data(5, 4) ); 00105 CPPUNIT_ASSERT( datas[1] == Data(2, 6) ); 00106 CPPUNIT_ASSERT( datas[2] == Data(3, 6) ); 00107 CPPUNIT_ASSERT( datas[3] == Data(4, 6) ); 00108 CPPUNIT_ASSERT( datas[4] == Data(1, 8) ); 00109 CPPUNIT_ASSERT( datas[5] == Data(6, 9) ); 00110 CPPUNIT_ASSERT( datas[6] == Data(0, 10) ); 00111 } 00112 00113 void SortTest::sort1() 00114 { 00115 int numbers[6] = { 1, 50, -10, 11, 42, 19 }; 00116 00117 sort(numbers, numbers + 6); 00118 // -10 1 11 19 42 50 00119 CPPUNIT_ASSERT(numbers[0]==-10); 00120 CPPUNIT_ASSERT(numbers[1]==1); 00121 CPPUNIT_ASSERT(numbers[2]==11); 00122 CPPUNIT_ASSERT(numbers[3]==19); 00123 CPPUNIT_ASSERT(numbers[4]==42); 00124 CPPUNIT_ASSERT(numbers[5]==50); 00125 } 00126 00127 void SortTest::sort2() 00128 { 00129 int numbers[] = { 1, 50, -10, 11, 42, 19 }; 00130 00131 int count = sizeof(numbers) / sizeof(numbers[0]); 00132 sort(numbers, numbers + count, greater<int>()); 00133 00134 // 50 42 19 11 1 -10 00135 CPPUNIT_ASSERT(numbers[5]==-10); 00136 CPPUNIT_ASSERT(numbers[4]==1); 00137 CPPUNIT_ASSERT(numbers[3]==11); 00138 CPPUNIT_ASSERT(numbers[2]==19); 00139 CPPUNIT_ASSERT(numbers[1]==42); 00140 CPPUNIT_ASSERT(numbers[0]==50); 00141 } 00142 00143 void SortTest::sort3() 00144 { 00145 vector<bool> boolVector; 00146 00147 boolVector.push_back( true ); 00148 boolVector.push_back( false ); 00149 00150 sort( boolVector.begin(), boolVector.end() ); 00151 00152 CPPUNIT_ASSERT(boolVector[0]==false); 00153 CPPUNIT_ASSERT(boolVector[1]==true); 00154 } 00155 00156 /* 00157 * A small utility class to check a potential compiler bug 00158 * that can result in a bad sort algorithm behavior. The type 00159 * _Tp of the SortTestFunc has to be SortTestAux without any 00160 * reference qualifier. 00161 */ 00162 struct SortTestAux { 00163 SortTestAux (bool &b) : _b(b) 00164 {} 00165 00166 SortTestAux (SortTestAux const&other) : _b(other._b) { 00167 _b = true; 00168 } 00169 00170 bool &_b; 00171 00172 private: 00173 //explicitely defined as private to avoid warnings: 00174 SortTestAux& operator = (SortTestAux const&); 00175 }; 00176 00177 template <class _Tp> 00178 void SortTestFunc (_Tp) { 00179 } 00180 00181 void SortTest::sort4() 00182 { 00183 bool copy_constructor_called = false; 00184 SortTestAux instance(copy_constructor_called); 00185 SortTestAux &r_instance = instance; 00186 SortTestAux const& rc_instance = instance; 00187 00188 SortTestFunc(r_instance); 00189 CPPUNIT_ASSERT(copy_constructor_called); 00190 copy_constructor_called = false; 00191 SortTestFunc(rc_instance); 00192 CPPUNIT_ASSERT(copy_constructor_called); 00193 } 00194 00195 #if defined (_STLP_DO_CHECK_BAD_PREDICATE) 00196 void SortTest::bad_predicate_detected() 00197 { 00198 int numbers[] = { 0, 0, 1, 0, 0, 1, 0, 0 }; 00199 try { 00200 sort(numbers, numbers + sizeof(numbers) / sizeof(numbers[0]), less_equal<int>()); 00201 00202 //Here is means that no exception has been raised 00203 CPPUNIT_ASSERT( false ); 00204 } 00205 catch (runtime_error const&) 00206 { /*OK bad predicate has been detected.*/ } 00207 00208 try { 00209 stable_sort(numbers, numbers + sizeof(numbers) / sizeof(numbers[0]), less_equal<int>()); 00210 00211 //Here is means that no exception has been raised 00212 CPPUNIT_ASSERT( false ); 00213 } 00214 catch (runtime_error const&) 00215 { /*OK bad predicate has been detected.*/ } 00216 } 00217 #endif Generated on Sat May 26 2012 04:34:45 for ReactOS by
1.7.6.1
|