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