genericopenlibs/cppstdlib/stl/test/unit/ptr2_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 Ptr2Test : public CPPUNIT_NS::TestCase
       
    31 {
       
    32   CPPUNIT_TEST_SUITE(Ptr2Test);
       
    33   CPPUNIT_TEST(ptrbin1);
       
    34   CPPUNIT_TEST(ptrbin2);
       
    35   CPPUNIT_TEST(ptrun1);
       
    36   CPPUNIT_TEST(ptrun2);
       
    37   CPPUNIT_TEST_SUITE_END();
       
    38 
       
    39 protected:
       
    40   void ptrbin1();
       
    41   void ptrbin2();
       
    42   void ptrun1();
       
    43   void ptrun2();
       
    44 };
       
    45 
       
    46 CPPUNIT_TEST_SUITE_REGISTRATION(Ptr2Test);
       
    47 
       
    48 //
       
    49 // tests implementation
       
    50 //
       
    51 static int sum(int x_, int y_)
       
    52 {
       
    53   return x_ + y_;
       
    54 }
       
    55 bool even(int n_)
       
    56 {
       
    57   return(n_ % 2) == 0;
       
    58 }
       
    59 void Ptr2Test::ptrbin1()
       
    60 {
       
    61   int input1 [4] = { 7, 2, 3, 5 };
       
    62   int input2 [4] = { 1, 5, 5, 8 };
       
    63 
       
    64   int output [4];
       
    65   transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, pointer_to_binary_function<int, int, int>(sum));
       
    66 
       
    67   CPPUNIT_ASSERT(output[0]==8);
       
    68   CPPUNIT_ASSERT(output[1]==7);
       
    69   CPPUNIT_ASSERT(output[2]==8);
       
    70   CPPUNIT_ASSERT(output[3]==13);
       
    71 }
       
    72 void Ptr2Test::ptrbin2()
       
    73 {
       
    74   int input1 [4] = { 7, 2, 3, 5 };
       
    75   int input2 [4] = { 1, 5, 5, 8 };
       
    76 
       
    77   int output [4];
       
    78   transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, ptr_fun(sum));
       
    79 
       
    80   CPPUNIT_ASSERT(output[0]==8);
       
    81   CPPUNIT_ASSERT(output[1]==7);
       
    82   CPPUNIT_ASSERT(output[2]==8);
       
    83   CPPUNIT_ASSERT(output[3]==13);
       
    84 }
       
    85 void Ptr2Test::ptrun1()
       
    86 {
       
    87   int array [3] = { 1, 2, 3 };
       
    88 
       
    89   int* p = find_if((int*)array, (int*)array + 3, pointer_to_unary_function<int, bool>(even));
       
    90   CPPUNIT_ASSERT(p != array+3);
       
    91   CPPUNIT_ASSERT(*p==2);
       
    92 }
       
    93 void Ptr2Test::ptrun2()
       
    94 {
       
    95   int array [3] = { 1, 2, 3 };
       
    96 
       
    97   int* p = find_if((int*)array, (int*)array + 3, ptr_fun(even));
       
    98   CPPUNIT_ASSERT(p != array+3);
       
    99   CPPUNIT_ASSERT(*p==2);
       
   100 }