ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

partial_test.cpp
Go to the documentation of this file.
00001 #include <numeric>
00002 #include <vector>
00003 #include <algorithm>
00004 #include <functional>
00005 
00006 #if defined (STLPORT) && defined (_STLP_DEBUG) && defined (_STLP_DEBUG_MODE_THROWS)
00007 #  define _STLP_DO_CHECK_BAD_PREDICATE
00008 #  include <stdexcept>
00009 #endif
00010 
00011 #include "iota.h"
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 PartialTest : public CPPUNIT_NS::TestCase
00022 {
00023   CPPUNIT_TEST_SUITE(PartialTest);
00024   CPPUNIT_TEST(parsrt0);
00025   CPPUNIT_TEST(parsrt1);
00026   CPPUNIT_TEST(parsrt2);
00027   CPPUNIT_TEST(parsrtc0);
00028   CPPUNIT_TEST(parsrtc1);
00029   CPPUNIT_TEST(parsrtc2);
00030 #if defined (_STLP_DO_CHECK_BAD_PREDICATE)
00031   CPPUNIT_TEST(bad_predicate_detected);
00032 #endif
00033   CPPUNIT_TEST(partsum0);
00034   CPPUNIT_TEST(partsum1);
00035   CPPUNIT_TEST(partsum2);
00036   CPPUNIT_TEST_SUITE_END();
00037 
00038 protected:
00039   void parsrt0();
00040   void parsrt1();
00041   void parsrt2();
00042   void parsrtc0();
00043   void parsrtc1();
00044   void parsrtc2();
00045   void partsum0();
00046   void partsum1();
00047   void partsum2();
00048   void bad_predicate_detected();
00049 
00050   static bool str_compare(const char* a_, const char* b_)
00051   {
00052     return strcmp(a_, b_) < 0 ? 1 : 0;
00053   }
00054 };
00055 
00056 CPPUNIT_TEST_SUITE_REGISTRATION(PartialTest);
00057 
00058 //
00059 // tests implementation
00060 //
00061 void PartialTest::parsrt0()
00062 {
00063   int numbers[6] = { 5, 2, 4, 3, 1, 6 };
00064 
00065   partial_sort((int*)numbers, (int*)numbers + 3, (int*)numbers + 6);
00066 
00067   // 1 2 3 5 4 6
00068   CPPUNIT_ASSERT(numbers[0]==1);
00069   CPPUNIT_ASSERT(numbers[1]==2);
00070   CPPUNIT_ASSERT(numbers[2]==3);
00071   CPPUNIT_ASSERT(numbers[3]==5);
00072   CPPUNIT_ASSERT(numbers[4]==4);
00073   CPPUNIT_ASSERT(numbers[5]==6);
00074 }
00075 
00076 void PartialTest::parsrt1()
00077 {
00078   // 8 8 5 3 7 6 5 3 2 4
00079   // 2 3 3 4 5 8 8 7 6 5
00080   int numbers[10] ={ 8, 8, 5, 3, 7, 6, 5, 3, 2, 4 };
00081 
00082   vector <int> v1(numbers, numbers+10);
00083   partial_sort(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
00084 
00085   CPPUNIT_ASSERT(v1[0]==2);
00086   CPPUNIT_ASSERT(v1[1]==3);
00087   CPPUNIT_ASSERT(v1[2]==3);
00088   CPPUNIT_ASSERT(v1[3]==4);
00089   CPPUNIT_ASSERT(v1[4]==5);
00090   CPPUNIT_ASSERT(v1[5]==8);
00091   CPPUNIT_ASSERT(v1[6]==8);
00092   CPPUNIT_ASSERT(v1[7]==7);
00093   CPPUNIT_ASSERT(v1[8]==6);
00094   CPPUNIT_ASSERT(v1[9]==5);
00095 }
00096 
00097 void PartialTest::parsrt2()
00098 {
00099   char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" };
00100 
00101   const unsigned nameSize = sizeof(names) / sizeof(names[0]);
00102   vector <char const*> v1(nameSize);
00103   for(size_t i = 0; i < v1.size(); i++)
00104     v1[i] = names[i];
00105 
00106   partial_sort(v1.begin(), v1.begin() + nameSize / 2, v1.end(), str_compare);
00107 
00108   // aa bb cc ff ee dd
00109   CPPUNIT_ASSERT( strcmp(v1[0], "aa") == 0 );
00110   CPPUNIT_ASSERT( v1[0] == names[0] );
00111   CPPUNIT_ASSERT( strcmp(v1[1], "bb") == 0 );
00112   CPPUNIT_ASSERT( v1[1] == names[5] );
00113   CPPUNIT_ASSERT( strcmp(v1[2], "cc") == 0 );
00114   CPPUNIT_ASSERT( v1[2] == names[4] );
00115   CPPUNIT_ASSERT( strcmp(v1[3], "ff") == 0 );
00116   CPPUNIT_ASSERT( v1[3] == names[1] );
00117   CPPUNIT_ASSERT( strcmp(v1[4], "ee") == 0 );
00118   CPPUNIT_ASSERT( v1[4] == names[3] );
00119   CPPUNIT_ASSERT( strcmp(v1[5], "dd") == 0 );
00120   CPPUNIT_ASSERT( v1[5] == names[2] );
00121 }
00122 
00123 void PartialTest::parsrtc0()
00124 {
00125   int numbers[6] = { 5, 2, 4, 3, 1, 6 };
00126 
00127   int result[3];
00128   partial_sort_copy((int*)numbers, (int*)numbers + 6, (int*)result, (int*)result + 3);
00129   //1 2 3
00130   CPPUNIT_ASSERT(result[0]==1);
00131   CPPUNIT_ASSERT(result[1]==2);
00132   CPPUNIT_ASSERT(result[2]==3);
00133 }
00134 
00135 void PartialTest::parsrtc1()
00136 {
00137   int numbers[10] ={ 3, 0, 4, 3, 2, 8, 2, 7, 7, 5 };
00138 
00139   //3 0 4 3 2 8 2 7 7 5
00140   //0 2 2 3 3
00141 
00142   vector <int> v1(numbers, numbers+10);
00143   vector <int> result(5);
00144 
00145   partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end());
00146   CPPUNIT_ASSERT(result[0]==0);
00147   CPPUNIT_ASSERT(result[1]==2);
00148   CPPUNIT_ASSERT(result[2]==2);
00149   CPPUNIT_ASSERT(result[3]==3);
00150   CPPUNIT_ASSERT(result[4]==3);
00151 }
00152 
00153 void PartialTest::parsrtc2()
00154 {
00155   char const* names[] = { "aa", "ff", "dd", "ee", "cc", "bb" };
00156 
00157   const unsigned nameSize = sizeof(names) / sizeof(names[0]);
00158   vector <char const*> v1(nameSize);
00159   for(size_t i = 0; i < v1.size(); i++)
00160     v1[i] = names[i];
00161   vector <char const*> result(3);
00162   partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end(), str_compare);
00163 
00164   // aa bb cc
00165   CPPUNIT_ASSERT( strcmp( result[0], "aa" ) == 0 );
00166   CPPUNIT_ASSERT( result[0] == names[0] );
00167   CPPUNIT_ASSERT( strcmp( result[1], "bb" ) == 0 );
00168   CPPUNIT_ASSERT( result[1] == names[5] );
00169   CPPUNIT_ASSERT( strcmp( result[2], "cc" ) == 0 );
00170   CPPUNIT_ASSERT( result[2] == names[4] );
00171 }
00172 
00173 #if defined (_STLP_DO_CHECK_BAD_PREDICATE)
00174 void PartialTest::bad_predicate_detected()
00175 {
00176   int numbers[] = { 0, 0, 1, 0, 0, 1, 0, 0 };
00177   const size_t s = sizeof(numbers) / sizeof(numbers[0]);
00178 
00179   try {
00180     partial_sort(numbers, numbers + s / 2, numbers + s, less_equal<int>());
00181 
00182     //Here is means that no exception has been raised
00183     CPPUNIT_ASSERT( false );
00184   }
00185   catch (runtime_error const&)
00186   { /*OK bad predicate has been detected.*/ }
00187 
00188   try {
00189     vector<int> result(s);
00190     partial_sort_copy(numbers, numbers + s, result.begin(), result.end(), less_equal<int>());
00191 
00192     //Here is means that no exception has been raised
00193     CPPUNIT_ASSERT( false );
00194   }
00195   catch (runtime_error const&)
00196   { /*OK bad predicate has been detected.*/ }
00197 }
00198 #endif
00199 
00200 void PartialTest::partsum0()
00201 {
00202   int numbers[6] = { 1, 2, 3, 4, 5, 6 };
00203 
00204   int result[6];
00205   partial_sum((int*)numbers, (int*)numbers + 6, (int*)result);
00206 
00207   // 1 3 6 10 15 21
00208   CPPUNIT_ASSERT(result[0]==1);
00209   CPPUNIT_ASSERT(result[1]==3);
00210   CPPUNIT_ASSERT(result[2]==6);
00211   CPPUNIT_ASSERT(result[3]==10);
00212   CPPUNIT_ASSERT(result[4]==15);
00213   CPPUNIT_ASSERT(result[5]==21);
00214 }
00215 
00216 void PartialTest::partsum1()
00217 {
00218   vector <int> v1(10);
00219   __iota(v1.begin(), v1.end(), 0);
00220   vector <int> v2(v1.size());
00221   partial_sum(v1.begin(), v1.end(), v2.begin());
00222 
00223   // 0 1 3 6 10 15 21 28 36 45
00224   CPPUNIT_ASSERT(v2[0]==0);
00225   CPPUNIT_ASSERT(v2[1]==1);
00226   CPPUNIT_ASSERT(v2[2]==3);
00227   CPPUNIT_ASSERT(v2[3]==6);
00228   CPPUNIT_ASSERT(v2[4]==10);
00229   CPPUNIT_ASSERT(v2[5]==15);
00230   CPPUNIT_ASSERT(v2[6]==21);
00231   CPPUNIT_ASSERT(v2[7]==28);
00232   CPPUNIT_ASSERT(v2[8]==36);
00233   CPPUNIT_ASSERT(v2[9]==45);
00234 }
00235 
00236 void PartialTest::partsum2()
00237 {
00238   vector <int> v1(5);
00239   __iota(v1.begin(), v1.end(), 1);
00240   vector <int> v2(v1.size());
00241   partial_sum(v1.begin(), v1.end(), v2.begin(), multiplies<int>());
00242   // 1 2 6 24 120
00243   CPPUNIT_ASSERT(v2[0]==1);
00244   CPPUNIT_ASSERT(v2[1]==2);
00245   CPPUNIT_ASSERT(v2[2]==6);
00246   CPPUNIT_ASSERT(v2[3]==24);
00247   CPPUNIT_ASSERT(v2[4]==120);
00248 }

Generated on Sun May 27 2012 04:35:39 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.