genericopenlibs/cppstdlib/stl/test/unit/merge_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 #include <numeric>
       
    21 
       
    22 #include "iota.h"
       
    23 #include "cppunit/cppunit_proxy.h"
       
    24 
       
    25 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    26 using namespace std;
       
    27 #endif
       
    28 
       
    29 //
       
    30 // TestCase class
       
    31 //
       
    32 class MergeTest : public CPPUNIT_NS::TestCase
       
    33 {
       
    34   CPPUNIT_TEST_SUITE(MergeTest);
       
    35   CPPUNIT_TEST(merge0);
       
    36   CPPUNIT_TEST(merge1);
       
    37   CPPUNIT_TEST(merge2);
       
    38   CPPUNIT_TEST_SUITE_END();
       
    39 
       
    40 protected:
       
    41   void merge0();
       
    42   void merge1();
       
    43   void merge2();
       
    44 };
       
    45 
       
    46 CPPUNIT_TEST_SUITE_REGISTRATION(MergeTest);
       
    47 
       
    48 //
       
    49 // tests implementation
       
    50 //
       
    51 void MergeTest::merge0()
       
    52 {
       
    53   int numbers1[5] = { 1, 6, 13, 25, 101 };
       
    54   int numbers2[5] = {-5, 26, 36, 46, 99 };
       
    55 
       
    56   int result[10];
       
    57   merge((int*)numbers1, (int*)numbers1 + 5, (int*)numbers2, (int*)numbers2 + 5, (int*)result);
       
    58 
       
    59   CPPUNIT_ASSERT(result[0]==-5);
       
    60   CPPUNIT_ASSERT(result[1]==1);
       
    61   CPPUNIT_ASSERT(result[2]==6);
       
    62   CPPUNIT_ASSERT(result[3]==13);
       
    63   CPPUNIT_ASSERT(result[4]==25);
       
    64   CPPUNIT_ASSERT(result[5]==26);
       
    65   CPPUNIT_ASSERT(result[6]==36);
       
    66   CPPUNIT_ASSERT(result[7]==46);
       
    67   CPPUNIT_ASSERT(result[8]==99);
       
    68   CPPUNIT_ASSERT(result[9]==101);
       
    69 }
       
    70 void MergeTest::merge1()
       
    71 {
       
    72   vector<int> v1(5);
       
    73   vector<int> v2(v1.size());
       
    74   __iota(v1.begin(), v1.end(), 0);
       
    75   __iota(v2.begin(), v2.end(), 3);
       
    76 
       
    77   vector <int> result(v1.size() + v2.size());
       
    78   merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
       
    79 
       
    80   CPPUNIT_ASSERT(result[0]==0);
       
    81   CPPUNIT_ASSERT(result[1]==1);
       
    82   CPPUNIT_ASSERT(result[2]==2);
       
    83   CPPUNIT_ASSERT(result[3]==3);
       
    84   CPPUNIT_ASSERT(result[4]==3);
       
    85   CPPUNIT_ASSERT(result[5]==4);
       
    86   CPPUNIT_ASSERT(result[6]==4);
       
    87   CPPUNIT_ASSERT(result[7]==5);
       
    88   CPPUNIT_ASSERT(result[8]==6);
       
    89   CPPUNIT_ASSERT(result[9]==7);
       
    90 
       
    91 }
       
    92 void MergeTest::merge2()
       
    93 {
       
    94   vector <int> v1(5);
       
    95   vector <int> v2(v1.size());
       
    96   for (int i = 0; (size_t)i < v1.size(); ++i) {
       
    97     v1[i] = 10 - i;
       
    98     v2[i] =  7 - i;
       
    99   }
       
   100   vector<int> result(v1.size() + v2.size());
       
   101   merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin(), greater<int>() );
       
   102 
       
   103   CPPUNIT_ASSERT(result[0]==10);
       
   104   CPPUNIT_ASSERT(result[1]==9);
       
   105   CPPUNIT_ASSERT(result[2]==8);
       
   106   CPPUNIT_ASSERT(result[3]==7);
       
   107   CPPUNIT_ASSERT(result[4]==7);
       
   108   CPPUNIT_ASSERT(result[5]==6);
       
   109   CPPUNIT_ASSERT(result[6]==6);
       
   110   CPPUNIT_ASSERT(result[7]==5);
       
   111   CPPUNIT_ASSERT(result[8]==4);
       
   112   CPPUNIT_ASSERT(result[9]==3);
       
   113 }