sl@0: #include <vector> sl@0: #include <algorithm> sl@0: #include <functional> sl@0: sl@0: #include "cppunit/cppunit_proxy.h" sl@0: sl@0: #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) sl@0: using namespace std; sl@0: #endif sl@0: sl@0: // sl@0: // TestCase class sl@0: // sl@0: class NthElemTest : public CPPUNIT_NS::TestCase sl@0: { sl@0: CPPUNIT_TEST_SUITE(NthElemTest); sl@0: CPPUNIT_TEST(nthelem0); sl@0: CPPUNIT_TEST(nthelem1); sl@0: CPPUNIT_TEST(nthelem2); sl@0: CPPUNIT_TEST_SUITE_END(); sl@0: sl@0: protected: sl@0: void nthelem0(); sl@0: void nthelem1(); sl@0: void nthelem2(); sl@0: }; sl@0: sl@0: CPPUNIT_TEST_SUITE_REGISTRATION(NthElemTest); sl@0: sl@0: // sl@0: // tests implementation sl@0: // sl@0: void NthElemTest::nthelem0() sl@0: { sl@0: int numbers[7] = { 5, 2, 4, 1, 0, 3 ,77}; sl@0: nth_element(numbers, numbers + 3, numbers + 6); sl@0: sl@0: CPPUNIT_ASSERT(numbers[0]==1); sl@0: CPPUNIT_ASSERT(numbers[1]==0); sl@0: CPPUNIT_ASSERT(numbers[2]==2); sl@0: CPPUNIT_ASSERT(numbers[3]==3); sl@0: CPPUNIT_ASSERT(numbers[4]==4); sl@0: CPPUNIT_ASSERT(numbers[5]==5); sl@0: } sl@0: void NthElemTest::nthelem1() sl@0: { sl@0: //6 8 5 1 7 4 1 5 2 6 sl@0: //1 1 4 2 5 5 6 7 8 6 sl@0: int numbers[10] = { 6, 8, 5, 1, 7, 4, 1, 5, 2, 6 }; sl@0: sl@0: vector <int> v1(numbers, numbers+10); sl@0: nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end()); sl@0: sl@0: CPPUNIT_ASSERT(v1[0]==1); sl@0: CPPUNIT_ASSERT(v1[1]==1); sl@0: CPPUNIT_ASSERT(v1[2]==4); sl@0: CPPUNIT_ASSERT(v1[3]==2); sl@0: CPPUNIT_ASSERT(v1[4]==5); sl@0: CPPUNIT_ASSERT(v1[5]==5); sl@0: CPPUNIT_ASSERT(v1[6]==6); sl@0: CPPUNIT_ASSERT(v1[7]==7); sl@0: CPPUNIT_ASSERT(v1[8]==8); sl@0: CPPUNIT_ASSERT(v1[9]==6); sl@0: } sl@0: void NthElemTest::nthelem2() sl@0: { sl@0: //4 5 4 2 1 7 4 3 1 6 sl@0: //6 7 4 4 5 4 3 2 1 1 sl@0: sl@0: int numbers[10] = { 4, 5, 4, 2, 1, 7, 4, 3, 1, 6 }; sl@0: vector <int> v1(numbers, numbers+10); sl@0: nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater<int>()); sl@0: sl@0: CPPUNIT_ASSERT(v1[0]==6); sl@0: CPPUNIT_ASSERT(v1[1]==7); sl@0: CPPUNIT_ASSERT(v1[2]==4); sl@0: CPPUNIT_ASSERT(v1[3]==4); sl@0: CPPUNIT_ASSERT(v1[4]==5); sl@0: CPPUNIT_ASSERT(v1[5]==4); sl@0: CPPUNIT_ASSERT(v1[6]==3); sl@0: CPPUNIT_ASSERT(v1[7]==2); sl@0: CPPUNIT_ASSERT(v1[8]==1); sl@0: CPPUNIT_ASSERT(v1[9]==1); sl@0: }