os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/transform_test.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
#include <string>
sl@0
     2
#include <iterator>
sl@0
     3
#include <vector>
sl@0
     4
#include <algorithm>
sl@0
     5
sl@0
     6
#include "cppunit/cppunit_proxy.h"
sl@0
     7
sl@0
     8
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
     9
using namespace std;
sl@0
    10
#endif
sl@0
    11
sl@0
    12
//
sl@0
    13
// TestCase class
sl@0
    14
//
sl@0
    15
class TransformTest : public CPPUNIT_NS::TestCase
sl@0
    16
{
sl@0
    17
  CPPUNIT_TEST_SUITE(TransformTest);
sl@0
    18
  CPPUNIT_TEST(trnsfrm1);
sl@0
    19
  CPPUNIT_TEST(trnsfrm2);
sl@0
    20
  CPPUNIT_TEST(self_str);
sl@0
    21
  CPPUNIT_TEST_SUITE_END();
sl@0
    22
sl@0
    23
protected:
sl@0
    24
  void trnsfrm1();
sl@0
    25
  void trnsfrm2();
sl@0
    26
  void self_str();
sl@0
    27
sl@0
    28
  static int negate_int(int a_) {
sl@0
    29
    return -a_;
sl@0
    30
  }
sl@0
    31
  static char map_char(char a_, int b_) {
sl@0
    32
    return char(a_ + b_);
sl@0
    33
  }
sl@0
    34
  static char shift( char c ) {
sl@0
    35
    return char(((int)c + 1) % 256);
sl@0
    36
  }
sl@0
    37
};
sl@0
    38
sl@0
    39
CPPUNIT_TEST_SUITE_REGISTRATION(TransformTest);
sl@0
    40
sl@0
    41
//
sl@0
    42
// tests implementation
sl@0
    43
//
sl@0
    44
void TransformTest::trnsfrm1()
sl@0
    45
{
sl@0
    46
  int numbers[6] = { -5, -1, 0, 1, 6, 11 };
sl@0
    47
sl@0
    48
  int result[6];
sl@0
    49
  transform((int*)numbers, (int*)numbers + 6, (int*)result, negate_int);
sl@0
    50
sl@0
    51
  CPPUNIT_ASSERT(result[0]==5);
sl@0
    52
  CPPUNIT_ASSERT(result[1]==1);
sl@0
    53
  CPPUNIT_ASSERT(result[2]==0);
sl@0
    54
  CPPUNIT_ASSERT(result[3]==-1);
sl@0
    55
  CPPUNIT_ASSERT(result[4]==-6);
sl@0
    56
  CPPUNIT_ASSERT(result[5]==-11);
sl@0
    57
}
sl@0
    58
void TransformTest::trnsfrm2()
sl@0
    59
{
sl@0
    60
#if defined (__MVS__)
sl@0
    61
  int trans[] = {-11, 4, -6, -6, -18, 0, 18, -14, 6, 0, -1, -59};
sl@0
    62
#else
sl@0
    63
  int trans[] = {-4, 4, -6, -6, -10, 0, 10, -6, 6, 0, -1, -77};
sl@0
    64
#endif
sl@0
    65
  char n[] = "Larry Mullen";
sl@0
    66
  const size_t count = ::strlen(n);
sl@0
    67
sl@0
    68
  string res;
sl@0
    69
  transform(n, n + count, trans, back_inserter(res), map_char);
sl@0
    70
  CPPUNIT_ASSERT( res == "Hello World!" )
sl@0
    71
}
sl@0
    72
sl@0
    73
void TransformTest::self_str()
sl@0
    74
{
sl@0
    75
  string s( "0123456789abcdefg" );
sl@0
    76
  string r( "123456789:bcdefgh" );
sl@0
    77
  transform( s.begin(), s.end(), s.begin(), shift );
sl@0
    78
  CPPUNIT_ASSERT( s == r );
sl@0
    79
}
sl@0
    80