os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/ptr2_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 <vector>
sl@0
     2
#include <algorithm>
sl@0
     3
#include <functional>
sl@0
     4
sl@0
     5
#include "cppunit/cppunit_proxy.h"
sl@0
     6
sl@0
     7
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
     8
using namespace std;
sl@0
     9
#endif
sl@0
    10
sl@0
    11
//
sl@0
    12
// TestCase class
sl@0
    13
//
sl@0
    14
class Ptr2Test : public CPPUNIT_NS::TestCase
sl@0
    15
{
sl@0
    16
  CPPUNIT_TEST_SUITE(Ptr2Test);
sl@0
    17
  CPPUNIT_TEST(ptrbin1);
sl@0
    18
  CPPUNIT_TEST(ptrbin2);
sl@0
    19
  CPPUNIT_TEST(ptrun1);
sl@0
    20
  CPPUNIT_TEST(ptrun2);
sl@0
    21
  CPPUNIT_TEST_SUITE_END();
sl@0
    22
sl@0
    23
protected:
sl@0
    24
  void ptrbin1();
sl@0
    25
  void ptrbin2();
sl@0
    26
  void ptrun1();
sl@0
    27
  void ptrun2();
sl@0
    28
};
sl@0
    29
sl@0
    30
CPPUNIT_TEST_SUITE_REGISTRATION(Ptr2Test);
sl@0
    31
sl@0
    32
//
sl@0
    33
// tests implementation
sl@0
    34
//
sl@0
    35
static int sum(int x_, int y_)
sl@0
    36
{
sl@0
    37
  return x_ + y_;
sl@0
    38
}
sl@0
    39
bool even(int n_)
sl@0
    40
{
sl@0
    41
  return(n_ % 2) == 0;
sl@0
    42
}
sl@0
    43
void Ptr2Test::ptrbin1()
sl@0
    44
{
sl@0
    45
  int input1 [4] = { 7, 2, 3, 5 };
sl@0
    46
  int input2 [4] = { 1, 5, 5, 8 };
sl@0
    47
sl@0
    48
  int output [4];
sl@0
    49
  transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, pointer_to_binary_function<int, int, int>(sum));
sl@0
    50
sl@0
    51
  CPPUNIT_ASSERT(output[0]==8);
sl@0
    52
  CPPUNIT_ASSERT(output[1]==7);
sl@0
    53
  CPPUNIT_ASSERT(output[2]==8);
sl@0
    54
  CPPUNIT_ASSERT(output[3]==13);
sl@0
    55
}
sl@0
    56
void Ptr2Test::ptrbin2()
sl@0
    57
{
sl@0
    58
  int input1 [4] = { 7, 2, 3, 5 };
sl@0
    59
  int input2 [4] = { 1, 5, 5, 8 };
sl@0
    60
sl@0
    61
  int output [4];
sl@0
    62
  transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, ptr_fun(sum));
sl@0
    63
sl@0
    64
  CPPUNIT_ASSERT(output[0]==8);
sl@0
    65
  CPPUNIT_ASSERT(output[1]==7);
sl@0
    66
  CPPUNIT_ASSERT(output[2]==8);
sl@0
    67
  CPPUNIT_ASSERT(output[3]==13);
sl@0
    68
}
sl@0
    69
void Ptr2Test::ptrun1()
sl@0
    70
{
sl@0
    71
  int array [3] = { 1, 2, 3 };
sl@0
    72
sl@0
    73
  int* p = find_if((int*)array, (int*)array + 3, pointer_to_unary_function<int, bool>(even));
sl@0
    74
  CPPUNIT_ASSERT(p != array+3);
sl@0
    75
  CPPUNIT_ASSERT(*p==2);
sl@0
    76
}
sl@0
    77
void Ptr2Test::ptrun2()
sl@0
    78
{
sl@0
    79
  int array [3] = { 1, 2, 3 };
sl@0
    80
sl@0
    81
  int* p = find_if((int*)array, (int*)array + 3, ptr_fun(even));
sl@0
    82
  CPPUNIT_ASSERT(p != array+3);
sl@0
    83
  CPPUNIT_ASSERT(*p==2);
sl@0
    84
}