genericopenlibs/cppstdlib/stl/test/unit/transform_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 <string>
       
    18 #include <iterator>
       
    19 #include <vector>
       
    20 #include <algorithm>
       
    21 
       
    22 #include "cppunit/cppunit_proxy.h"
       
    23 
       
    24 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    25 using namespace std;
       
    26 #endif
       
    27 
       
    28 //
       
    29 // TestCase class
       
    30 //
       
    31 class TransformTest : public CPPUNIT_NS::TestCase
       
    32 {
       
    33   CPPUNIT_TEST_SUITE(TransformTest);
       
    34   CPPUNIT_TEST(trnsfrm1);
       
    35   CPPUNIT_TEST(trnsfrm2);
       
    36   CPPUNIT_TEST(self_str);
       
    37   CPPUNIT_TEST_SUITE_END();
       
    38 
       
    39 protected:
       
    40   void trnsfrm1();
       
    41   void trnsfrm2();
       
    42   void self_str();
       
    43 
       
    44   static int negate_int(int a_) {
       
    45     return -a_;
       
    46   }
       
    47   static char map_char(char a_, int b_) {
       
    48     return char(a_ + b_);
       
    49   }
       
    50   static char shift( char c ) {
       
    51     return char(((int)c + 1) % 256);
       
    52   }
       
    53 };
       
    54 
       
    55 CPPUNIT_TEST_SUITE_REGISTRATION(TransformTest);
       
    56 
       
    57 //
       
    58 // tests implementation
       
    59 //
       
    60 void TransformTest::trnsfrm1()
       
    61 {
       
    62   int numbers[6] = { -5, -1, 0, 1, 6, 11 };
       
    63 
       
    64   int result[6];
       
    65   transform((int*)numbers, (int*)numbers + 6, (int*)result, negate_int);
       
    66 
       
    67   CPPUNIT_ASSERT(result[0]==5);
       
    68   CPPUNIT_ASSERT(result[1]==1);
       
    69   CPPUNIT_ASSERT(result[2]==0);
       
    70   CPPUNIT_ASSERT(result[3]==-1);
       
    71   CPPUNIT_ASSERT(result[4]==-6);
       
    72   CPPUNIT_ASSERT(result[5]==-11);
       
    73 }
       
    74 void TransformTest::trnsfrm2()
       
    75 {
       
    76 #if defined (__MVS__)
       
    77   int trans[] = {-11, 4, -6, -6, -18, 0, 18, -14, 6, 0, -1, -59};
       
    78 #else
       
    79   int trans[] = {-4, 4, -6, -6, -10, 0, 10, -6, 6, 0, -1, -77};
       
    80 #endif
       
    81   char n[] = "Larry Mullen";
       
    82   const size_t count = ::strlen(n);
       
    83 
       
    84   string res;
       
    85   transform(n, n + count, trans, back_inserter(res), map_char);
       
    86   CPPUNIT_ASSERT( res == "Hello World!" )
       
    87 }
       
    88 
       
    89 void TransformTest::self_str()
       
    90 {
       
    91   string s( "0123456789abcdefg" );
       
    92   string r( "123456789:bcdefgh" );
       
    93   transform( s.begin(), s.end(), s.begin(), shift );
       
    94   CPPUNIT_ASSERT( s == r );
       
    95 }
       
    96