os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/nthelm_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/nthelm_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,84 @@
     1.4 +#include <vector>
     1.5 +#include <algorithm>
     1.6 +#include <functional>
     1.7 +
     1.8 +#include "cppunit/cppunit_proxy.h"
     1.9 +
    1.10 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.11 +using namespace std;
    1.12 +#endif
    1.13 +
    1.14 +//
    1.15 +// TestCase class
    1.16 +//
    1.17 +class NthElemTest : public CPPUNIT_NS::TestCase
    1.18 +{
    1.19 +  CPPUNIT_TEST_SUITE(NthElemTest);
    1.20 +  CPPUNIT_TEST(nthelem0);
    1.21 +  CPPUNIT_TEST(nthelem1);
    1.22 +  CPPUNIT_TEST(nthelem2);
    1.23 +  CPPUNIT_TEST_SUITE_END();
    1.24 +
    1.25 +protected:
    1.26 +  void nthelem0();
    1.27 +  void nthelem1();
    1.28 +  void nthelem2();
    1.29 +};
    1.30 +
    1.31 +CPPUNIT_TEST_SUITE_REGISTRATION(NthElemTest);
    1.32 +
    1.33 +//
    1.34 +// tests implementation
    1.35 +//
    1.36 +void NthElemTest::nthelem0()
    1.37 +{
    1.38 +  int numbers[7] = { 5, 2, 4, 1, 0, 3 ,77};
    1.39 +  nth_element(numbers, numbers + 3, numbers + 6);
    1.40 +
    1.41 +  CPPUNIT_ASSERT(numbers[0]==1);
    1.42 +  CPPUNIT_ASSERT(numbers[1]==0);
    1.43 +  CPPUNIT_ASSERT(numbers[2]==2);
    1.44 +  CPPUNIT_ASSERT(numbers[3]==3);
    1.45 +  CPPUNIT_ASSERT(numbers[4]==4);
    1.46 +  CPPUNIT_ASSERT(numbers[5]==5);
    1.47 +}
    1.48 +void NthElemTest::nthelem1()
    1.49 +{
    1.50 +  //6 8 5 1 7 4 1 5 2 6
    1.51 +  //1 1 4 2 5 5 6 7 8 6
    1.52 +  int numbers[10] = { 6, 8, 5, 1, 7, 4, 1, 5, 2, 6 };
    1.53 +
    1.54 +  vector <int> v1(numbers, numbers+10);
    1.55 +  nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
    1.56 +
    1.57 +  CPPUNIT_ASSERT(v1[0]==1);
    1.58 +  CPPUNIT_ASSERT(v1[1]==1);
    1.59 +  CPPUNIT_ASSERT(v1[2]==4);
    1.60 +  CPPUNIT_ASSERT(v1[3]==2);
    1.61 +  CPPUNIT_ASSERT(v1[4]==5);
    1.62 +  CPPUNIT_ASSERT(v1[5]==5);
    1.63 +  CPPUNIT_ASSERT(v1[6]==6);
    1.64 +  CPPUNIT_ASSERT(v1[7]==7);
    1.65 +  CPPUNIT_ASSERT(v1[8]==8);
    1.66 +  CPPUNIT_ASSERT(v1[9]==6);
    1.67 +}
    1.68 +void NthElemTest::nthelem2()
    1.69 +{
    1.70 +  //4 5 4 2 1 7 4 3 1 6
    1.71 +  //6 7 4 4 5 4 3 2 1 1
    1.72 +
    1.73 +  int numbers[10] = { 4, 5, 4, 2, 1, 7, 4, 3, 1, 6 };
    1.74 +  vector <int> v1(numbers, numbers+10);
    1.75 +  nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater<int>());
    1.76 +
    1.77 +  CPPUNIT_ASSERT(v1[0]==6);
    1.78 +  CPPUNIT_ASSERT(v1[1]==7);
    1.79 +  CPPUNIT_ASSERT(v1[2]==4);
    1.80 +  CPPUNIT_ASSERT(v1[3]==4);
    1.81 +  CPPUNIT_ASSERT(v1[4]==5);
    1.82 +  CPPUNIT_ASSERT(v1[5]==4);
    1.83 +  CPPUNIT_ASSERT(v1[6]==3);
    1.84 +  CPPUNIT_ASSERT(v1[7]==2);
    1.85 +  CPPUNIT_ASSERT(v1[8]==1);
    1.86 +  CPPUNIT_ASSERT(v1[9]==1);
    1.87 +}