First public contribution.
5 #include "cppunit/cppunit_proxy.h"
7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
14 class NthElemTest : public CPPUNIT_NS::TestCase
16 CPPUNIT_TEST_SUITE(NthElemTest);
17 CPPUNIT_TEST(nthelem0);
18 CPPUNIT_TEST(nthelem1);
19 CPPUNIT_TEST(nthelem2);
20 CPPUNIT_TEST_SUITE_END();
28 CPPUNIT_TEST_SUITE_REGISTRATION(NthElemTest);
31 // tests implementation
33 void NthElemTest::nthelem0()
35 int numbers[7] = { 5, 2, 4, 1, 0, 3 ,77};
36 nth_element(numbers, numbers + 3, numbers + 6);
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);
45 void NthElemTest::nthelem1()
49 int numbers[10] = { 6, 8, 5, 1, 7, 4, 1, 5, 2, 6 };
51 vector <int> v1(numbers, numbers+10);
52 nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
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);
65 void NthElemTest::nthelem2()
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>());
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);