genericopenlibs/cppstdlib/stl/test/unit/nthelm_test.cpp
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <vector>
       
    18 #include <algorithm>
       
    19 #include <functional>
       
    20 
       
    21 #include "cppunit/cppunit_proxy.h"
       
    22 
       
    23 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    24 using namespace std;
       
    25 #endif
       
    26 
       
    27 //
       
    28 // TestCase class
       
    29 //
       
    30 class NthElemTest : public CPPUNIT_NS::TestCase
       
    31 {
       
    32   CPPUNIT_TEST_SUITE(NthElemTest);
       
    33   CPPUNIT_TEST(nthelem0);
       
    34   CPPUNIT_TEST(nthelem1);
       
    35   CPPUNIT_TEST(nthelem2);
       
    36   CPPUNIT_TEST_SUITE_END();
       
    37 
       
    38 protected:
       
    39   void nthelem0();
       
    40   void nthelem1();
       
    41   void nthelem2();
       
    42 };
       
    43 
       
    44 CPPUNIT_TEST_SUITE_REGISTRATION(NthElemTest);
       
    45 
       
    46 //
       
    47 // tests implementation
       
    48 //
       
    49 void NthElemTest::nthelem0()
       
    50 {
       
    51   int numbers[7] = { 5, 2, 4, 1, 0, 3 ,77};
       
    52   nth_element(numbers, numbers + 3, numbers + 6);
       
    53 
       
    54   CPPUNIT_ASSERT(numbers[0]==1);
       
    55   CPPUNIT_ASSERT(numbers[1]==0);
       
    56   CPPUNIT_ASSERT(numbers[2]==2);
       
    57   CPPUNIT_ASSERT(numbers[3]==3);
       
    58   CPPUNIT_ASSERT(numbers[4]==4);
       
    59   CPPUNIT_ASSERT(numbers[5]==5);
       
    60 }
       
    61 void NthElemTest::nthelem1()
       
    62 {
       
    63   //6 8 5 1 7 4 1 5 2 6
       
    64   //1 1 4 2 5 5 6 7 8 6
       
    65   int numbers[10] = { 6, 8, 5, 1, 7, 4, 1, 5, 2, 6 };
       
    66 
       
    67   vector <int> v1(numbers, numbers+10);
       
    68   nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
       
    69 
       
    70   CPPUNIT_ASSERT(v1[0]==1);
       
    71   CPPUNIT_ASSERT(v1[1]==1);
       
    72   CPPUNIT_ASSERT(v1[2]==4);
       
    73   CPPUNIT_ASSERT(v1[3]==2);
       
    74   CPPUNIT_ASSERT(v1[4]==5);
       
    75   CPPUNIT_ASSERT(v1[5]==5);
       
    76   CPPUNIT_ASSERT(v1[6]==6);
       
    77   CPPUNIT_ASSERT(v1[7]==7);
       
    78   CPPUNIT_ASSERT(v1[8]==8);
       
    79   CPPUNIT_ASSERT(v1[9]==6);
       
    80 }
       
    81 void NthElemTest::nthelem2()
       
    82 {
       
    83   //4 5 4 2 1 7 4 3 1 6
       
    84   //6 7 4 4 5 4 3 2 1 1
       
    85 
       
    86   int numbers[10] = { 4, 5, 4, 2, 1, 7, 4, 3, 1, 6 };
       
    87   vector <int> v1(numbers, numbers+10);
       
    88   nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater<int>());
       
    89 
       
    90   CPPUNIT_ASSERT(v1[0]==6);
       
    91   CPPUNIT_ASSERT(v1[1]==7);
       
    92   CPPUNIT_ASSERT(v1[2]==4);
       
    93   CPPUNIT_ASSERT(v1[3]==4);
       
    94   CPPUNIT_ASSERT(v1[4]==5);
       
    95   CPPUNIT_ASSERT(v1[5]==4);
       
    96   CPPUNIT_ASSERT(v1[6]==3);
       
    97   CPPUNIT_ASSERT(v1[7]==2);
       
    98   CPPUNIT_ASSERT(v1[8]==1);
       
    99   CPPUNIT_ASSERT(v1[9]==1);
       
   100 }