Home | History | Annotate | Download | only in unit
      1 #include <vector>
      2 #include <algorithm>
      3 #include <functional>
      4 
      5 #include "cppunit/cppunit_proxy.h"
      6 
      7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      8 using namespace std;
      9 #endif
     10 
     11 //
     12 // TestCase class
     13 //
     14 class NthElemTest : public CPPUNIT_NS::TestCase
     15 {
     16   CPPUNIT_TEST_SUITE(NthElemTest);
     17   CPPUNIT_TEST(nthelem0);
     18   CPPUNIT_TEST(nthelem1);
     19   CPPUNIT_TEST(nthelem2);
     20   CPPUNIT_TEST_SUITE_END();
     21 
     22 protected:
     23   void nthelem0();
     24   void nthelem1();
     25   void nthelem2();
     26 };
     27 
     28 CPPUNIT_TEST_SUITE_REGISTRATION(NthElemTest);
     29 
     30 //
     31 // tests implementation
     32 //
     33 void NthElemTest::nthelem0()
     34 {
     35   int numbers[7] = { 5, 2, 4, 1, 0, 3 ,77};
     36   nth_element(numbers, numbers + 3, numbers + 6);
     37 
     38   CPPUNIT_ASSERT(numbers[0]==1);
     39   CPPUNIT_ASSERT(numbers[1]==0);
     40   CPPUNIT_ASSERT(numbers[2]==2);
     41   CPPUNIT_ASSERT(numbers[3]==3);
     42   CPPUNIT_ASSERT(numbers[4]==4);
     43   CPPUNIT_ASSERT(numbers[5]==5);
     44 }
     45 void NthElemTest::nthelem1()
     46 {
     47   //6 8 5 1 7 4 1 5 2 6
     48   //1 1 4 2 5 5 6 7 8 6
     49   int numbers[10] = { 6, 8, 5, 1, 7, 4, 1, 5, 2, 6 };
     50 
     51   vector <int> v1(numbers, numbers+10);
     52   nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
     53 
     54   CPPUNIT_ASSERT(v1[0]==1);
     55   CPPUNIT_ASSERT(v1[1]==1);
     56   CPPUNIT_ASSERT(v1[2]==4);
     57   CPPUNIT_ASSERT(v1[3]==2);
     58   CPPUNIT_ASSERT(v1[4]==5);
     59   CPPUNIT_ASSERT(v1[5]==5);
     60   CPPUNIT_ASSERT(v1[6]==6);
     61   CPPUNIT_ASSERT(v1[7]==7);
     62   CPPUNIT_ASSERT(v1[8]==8);
     63   CPPUNIT_ASSERT(v1[9]==6);
     64 }
     65 void NthElemTest::nthelem2()
     66 {
     67   //4 5 4 2 1 7 4 3 1 6
     68   //6 7 4 4 5 4 3 2 1 1
     69 
     70   int numbers[10] = { 4, 5, 4, 2, 1, 7, 4, 3, 1, 6 };
     71   vector <int> v1(numbers, numbers+10);
     72   nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater<int>());
     73 
     74   CPPUNIT_ASSERT(v1[0]==6);
     75   CPPUNIT_ASSERT(v1[1]==7);
     76   CPPUNIT_ASSERT(v1[2]==4);
     77   CPPUNIT_ASSERT(v1[3]==4);
     78   CPPUNIT_ASSERT(v1[4]==5);
     79   CPPUNIT_ASSERT(v1[5]==4);
     80   CPPUNIT_ASSERT(v1[6]==3);
     81   CPPUNIT_ASSERT(v1[7]==2);
     82   CPPUNIT_ASSERT(v1[8]==1);
     83   CPPUNIT_ASSERT(v1[9]==1);
     84 }
     85