Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

test-sort_algorithms.cc

00001 /*
00002 *  Name:      test-sort_algorithms.cc
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Test for sort algorithms
00005 *  Date:      $Date: 2003/04/14 00:18:36 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
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 //  L O C A L   T Y P E S
00040 //
00041 
00043 typedef std::vector<int> TVectorInt;
00044 
00045 
00046 //
00047 //  L O C A L   V A R I A B L E S
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 //  L O C A L   F U N C T I O N S
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 }  // _StlSortTest()
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 }  // _QuickSortTest()
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   //  Benchmarks over _tVector3 and _tVector4 is not feasible due to the
00191   //  excessive amount of time needed to finish.
00192   //
00193   
00194   TEST_RETURN_CODE;
00195 
00196 }  // _BubbleSortTest()
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   //  Generation of first array: 
00213   //
00214   //    [1, 1, 1, 2, 1, 1, 1]
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   //  Generation of a sorted version of the first array: 
00226   //  
00227   //    [1, 1, 1, 1, 1, 1, 2]
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   //  Generation of second array: 
00239   //
00240   //    [10, 9, 8, 7, 6, 5, 4]
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   //  Generation of a sorted version of the second array: 
00252   //  
00253   //    [4, 5, 6, 7, 8, 9, 10]
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   //  Check time for entering kzSizeBigVector elements.
00265   //
00266   TEST_START_WATCH;
00267 
00268   //
00269   //  Generation of third array: 
00270   //
00271   //    [kzSizeBigVector, kzSizeBigVector - 1,... 1]
00272   //
00273   for (zIndex = 0; ( zIndex < kzSizeBigVector ) ;++zIndex)
00274   {
00275     _tVector3.push_back (kzSizeBigVector - zIndex);
00276   }
00277 
00278   //
00279   //  Generation of a sorted version of the third array: 
00280   //  
00281   //    [1, 2, 3,... kzSizeBigVector]
00282   //
00283   for (zIndex = 1; ( zIndex <= kzSizeBigVector ) ;++zIndex)
00284   {
00285     _tSortedVector3.push_back (zIndex);
00286   }
00287 
00288   //
00289   //  Generation of fourth array: 
00290   //
00291   //    [rand(), rand(),... rand()]
00292   //
00293   for (zIndex = 0; ( zIndex < kzSizeBigVector ) ;++zIndex)
00294   {
00295     _tVector4.push_back (rand());
00296   }
00297 
00298   //
00299   //  Generation of a sorted version of the fourth array: 
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 }  // main()

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18