00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <algorithm>
00027 #include <cstdlib>
00028 #include <mpcl/util/strategy/bubble_sort_algorithm.hh>
00029 #include <mpcl/util/strategy/quick_sort_algorithm.hh>
00030 #include <mpcl/test.h>
00031 #include <vector>
00032
00033
00034 using mpcl::util::strategy::TBubbleSortAlgorithm;
00035 using mpcl::util::strategy::TQuickSortAlgorithm;
00036
00037
00038
00039
00040
00041
00043 typedef std::vector<int> TVectorInt;
00044
00045
00046
00047
00048
00049
00050 static TVectorInt _tVector1;
00051 static TVectorInt _tVector2;
00052 static TVectorInt _tVector3;
00053 static TVectorInt _tVector4;
00054 static TVectorInt _tSortedVector1;
00055 static TVectorInt _tSortedVector2;
00056 static TVectorInt _tSortedVector3;
00057 static TVectorInt _tSortedVector4;
00058
00059
00060
00061
00062
00063
00064 static int _StlSortTest (void)
00065 {
00066
00067 using std::sort;
00068
00069 TVectorInt tVector;
00070
00071 TEST_INIT ("test for STL sorting algorithms");
00072
00073 tVector = _tVector1;
00074 TEST_START_WATCH;
00075 sort (tVector.begin(), tVector.end());
00076 TEST_STOP_WATCH;
00077
00078 TEST_NUMBERS (_tVector1.size(), tVector.size());
00079 TEST_NUMBERS (true, ( tVector == _tSortedVector1 ));
00080 TEST_DISPLAY_WATCH ("STL sort benchmark");
00081
00082 tVector = _tVector2;
00083 TEST_START_WATCH;
00084 sort (tVector.begin(), tVector.end());
00085 TEST_STOP_WATCH;
00086
00087 TEST_NUMBERS (_tVector2.size(), tVector.size());
00088 TEST_NUMBERS (true, ( tVector == _tSortedVector2 ));
00089 TEST_DISPLAY_WATCH ("STL sort benchmark");
00090
00091 tVector = _tVector3;
00092 TEST_START_WATCH;
00093 sort (tVector.begin(), tVector.end());
00094 TEST_STOP_WATCH;
00095
00096 TEST_NUMBERS (_tVector3.size(), tVector.size());
00097 TEST_NUMBERS (true, ( tVector == _tSortedVector3 ));
00098 TEST_DISPLAY_WATCH ("STL sort benchmark");
00099
00100 tVector = _tVector4;
00101 TEST_START_WATCH;
00102 sort (tVector.begin(), tVector.end());
00103 TEST_STOP_WATCH;
00104
00105 TEST_NUMBERS (_tVector4.size(), tVector.size());
00106 TEST_NUMBERS (true, ( tVector == _tSortedVector4 ));
00107 TEST_DISPLAY_WATCH ("STL sort benchmark");
00108
00109 TEST_RETURN_CODE;
00110
00111 }
00112
00113
00114 static int _QuickSortTest (void)
00115 {
00116
00117 TVectorInt tVector;
00118 TQuickSortAlgorithm<TVectorInt> tQuickSort;
00119
00120 TEST_INIT ("test for class 'TQuickSortAlgorithm'");
00121
00122 tVector = _tVector1;
00123 TEST_START_WATCH;
00124 tQuickSort.execute (tVector);
00125 TEST_STOP_WATCH;
00126
00127 TEST_NUMBERS (_tVector1.size(), tVector.size());
00128 TEST_NUMBERS (true, ( tVector == _tSortedVector1 ));
00129
00130 TEST_DISPLAY_WATCH ("'TQuickSortAlgorithm' sort benchmark");
00131
00132 tVector = _tVector2;
00133 TEST_START_WATCH;
00134 tQuickSort.execute (tVector);
00135 TEST_STOP_WATCH;
00136
00137 TEST_NUMBERS (_tVector2.size(), tVector.size());
00138 TEST_NUMBERS (true, ( tVector == _tSortedVector2 ));
00139 TEST_DISPLAY_WATCH ("'TQuickSortAlgorithm' sort benchmark");
00140
00141 tVector = _tVector3;
00142 TEST_START_WATCH;
00143 tQuickSort.execute (tVector);
00144 TEST_STOP_WATCH;
00145
00146 TEST_NUMBERS (_tVector3.size(), tVector.size());
00147 TEST_NUMBERS (true, ( tVector == _tSortedVector3 ));
00148 TEST_DISPLAY_WATCH ("'TQuickSortAlgorithm' sort benchmark");
00149
00150 tVector = _tVector4;
00151 TEST_START_WATCH;
00152 tQuickSort.execute (tVector);
00153 TEST_STOP_WATCH;
00154
00155 TEST_NUMBERS (_tVector4.size(), tVector.size());
00156 TEST_NUMBERS (true, ( tVector == _tSortedVector4 ));
00157 TEST_DISPLAY_WATCH ("'TQuickSortAlgorithm' sort benchmark");
00158 TEST_RETURN_CODE;
00159
00160 }
00161
00162
00163 static int _BubbleSortTest (void)
00164 {
00165
00166 TVectorInt tVector;
00167 TBubbleSortAlgorithm<TVectorInt> tBubbleSort;
00168
00169 TEST_INIT ("test for class 'TBubbleSortAlgorithm'");
00170
00171 tVector = _tVector1;
00172 TEST_START_WATCH;
00173 tBubbleSort.execute (tVector);
00174 TEST_STOP_WATCH;
00175
00176 TEST_NUMBERS (_tVector1.size(), tVector.size());
00177 TEST_NUMBERS (true, ( tVector == _tSortedVector1 ));
00178 TEST_DISPLAY_WATCH ("'TBubbleSortAlgorithm' sort benchmark");
00179
00180 tVector = _tVector2;
00181 TEST_START_WATCH;
00182 tBubbleSort.execute (tVector);
00183 TEST_STOP_WATCH;
00184
00185 TEST_NUMBERS (_tVector2.size(), tVector.size());
00186 TEST_NUMBERS (true, ( tVector == _tSortedVector2 ));
00187 TEST_DISPLAY_WATCH ("'TBubbleSortAlgorithm' sort benchmark");
00188
00189
00190
00191
00192
00193
00194 TEST_RETURN_CODE;
00195
00196 }
00197
00198
00200 int main (void)
00201 {
00202
00203 using std::sort;
00204
00205 register size_t zIndex;
00206 const size_t kzSizeBigVector = 100000;
00207
00208 TEST_INIT ("tests for Sort Algorithms");
00209 TEST_START_WATCH;
00210
00211
00212
00213
00214
00215
00216 _tVector1.push_back (1);
00217 _tVector1.push_back (1);
00218 _tVector1.push_back (1);
00219 _tVector1.push_back (2);
00220 _tVector1.push_back (1);
00221 _tVector1.push_back (1);
00222 _tVector1.push_back (1);
00223
00224
00225
00226
00227
00228
00229 _tSortedVector1.push_back (1);
00230 _tSortedVector1.push_back (1);
00231 _tSortedVector1.push_back (1);
00232 _tSortedVector1.push_back (1);
00233 _tSortedVector1.push_back (1);
00234 _tSortedVector1.push_back (1);
00235 _tSortedVector1.push_back (2);
00236
00237
00238
00239
00240
00241
00242 _tVector2.push_back (10);
00243 _tVector2.push_back (9);
00244 _tVector2.push_back (8);
00245 _tVector2.push_back (7);
00246 _tVector2.push_back (6);
00247 _tVector2.push_back (5);
00248 _tVector2.push_back (4);
00249
00250
00251
00252
00253
00254
00255 _tSortedVector2.push_back (4);
00256 _tSortedVector2.push_back (5);
00257 _tSortedVector2.push_back (6);
00258 _tSortedVector2.push_back (7);
00259 _tSortedVector2.push_back (8);
00260 _tSortedVector2.push_back (9);
00261 _tSortedVector2.push_back (10);
00262
00263
00264
00265
00266 TEST_START_WATCH;
00267
00268
00269
00270
00271
00272
00273 for (zIndex = 0; ( zIndex < kzSizeBigVector ) ;++zIndex)
00274 {
00275 _tVector3.push_back (kzSizeBigVector - zIndex);
00276 }
00277
00278
00279
00280
00281
00282
00283 for (zIndex = 1; ( zIndex <= kzSizeBigVector ) ;++zIndex)
00284 {
00285 _tSortedVector3.push_back (zIndex);
00286 }
00287
00288
00289
00290
00291
00292
00293 for (zIndex = 0; ( zIndex < kzSizeBigVector ) ;++zIndex)
00294 {
00295 _tVector4.push_back (rand());
00296 }
00297
00298
00299
00300
00301 _tSortedVector4 = _tVector4;
00302 sort (_tSortedVector4.begin(), _tSortedVector4.end());
00303
00304 TEST_STOP_WATCH;
00305 TEST_DISPLAY_WATCH ("STL sort benchmark");
00306
00307 TEST_NUMBERS (kzSizeBigVector, _tVector3.size());
00308 TEST_NUMBERS (kzSizeBigVector, _tSortedVector3.size());
00309 TEST_NUMBERS (kzSizeBigVector, _tVector4.size());
00310 TEST_NUMBERS (kzSizeBigVector, _tSortedVector4.size());
00311
00312 TEST_FUNCTION (_StlSortTest());
00313 TEST_FUNCTION (_QuickSortTest());
00314 TEST_FUNCTION (_BubbleSortTest());
00315
00316 TEST_MEMORY_STATUS;
00317 TEST_RETURN_CODE;
00318
00319 }