os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/bind_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 <algorithm>
sl@0
     2
#include <functional>
sl@0
     3
sl@0
     4
#include "cppunit/cppunit_proxy.h"
sl@0
     5
sl@0
     6
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
sl@0
     7
using namespace std;
sl@0
     8
#endif
sl@0
     9
sl@0
    10
//
sl@0
    11
// TestCase class
sl@0
    12
//
sl@0
    13
class BindTest : public CPPUNIT_NS::TestCase
sl@0
    14
{
sl@0
    15
  CPPUNIT_TEST_SUITE(BindTest);
sl@0
    16
  CPPUNIT_TEST(bind1st1);
sl@0
    17
  CPPUNIT_TEST(bind2nd1);
sl@0
    18
  CPPUNIT_TEST(bind2nd2);
sl@0
    19
#if !defined (STLPORT) || \
sl@0
    20
    defined (_STLP_NO_EXTENSIONS) || !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
sl@0
    21
  CPPUNIT_IGNORE;
sl@0
    22
#endif
sl@0
    23
  CPPUNIT_TEST(bind2nd3);
sl@0
    24
  CPPUNIT_TEST(bind_memfn);
sl@0
    25
  CPPUNIT_TEST_SUITE_END();
sl@0
    26
sl@0
    27
protected:
sl@0
    28
  void bind1st1();
sl@0
    29
  void bind2nd1();
sl@0
    30
  void bind2nd2();
sl@0
    31
  void bind2nd3();
sl@0
    32
  void bind_memfn();
sl@0
    33
};
sl@0
    34
sl@0
    35
CPPUNIT_TEST_SUITE_REGISTRATION(BindTest);
sl@0
    36
sl@0
    37
//
sl@0
    38
// tests implementation
sl@0
    39
//
sl@0
    40
void BindTest::bind1st1()
sl@0
    41
{
sl@0
    42
  int array [3] = { 1, 2, 3 };
sl@0
    43
  int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
sl@0
    44
sl@0
    45
  CPPUNIT_ASSERT(p==&array[2]);
sl@0
    46
  CPPUNIT_ASSERT(array[0]==1);
sl@0
    47
  CPPUNIT_ASSERT(array[1]==2);
sl@0
    48
}
sl@0
    49
void BindTest::bind2nd1()
sl@0
    50
{
sl@0
    51
  int array [3] = { 1, 2, 3 };
sl@0
    52
  replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4);
sl@0
    53
sl@0
    54
  CPPUNIT_ASSERT(array[0]==1);
sl@0
    55
  CPPUNIT_ASSERT(array[1]==2);
sl@0
    56
  CPPUNIT_ASSERT(array[2]==4);
sl@0
    57
}
sl@0
    58
void BindTest::bind2nd2()
sl@0
    59
{
sl@0
    60
  int array [3] = { 1, 2, 3 };
sl@0
    61
  replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4);
sl@0
    62
  CPPUNIT_ASSERT(array[0]==1);
sl@0
    63
  CPPUNIT_ASSERT(array[1]==2);
sl@0
    64
  CPPUNIT_ASSERT(array[2]==4);
sl@0
    65
}
sl@0
    66
sl@0
    67
int test_func1 (const int &param1, const int &param2) {
sl@0
    68
  return param1 + param2;
sl@0
    69
}
sl@0
    70
sl@0
    71
int test_func2 (int &param1, int param2) {
sl@0
    72
  param1 += param2;
sl@0
    73
  return param1 + param2;
sl@0
    74
}
sl@0
    75
sl@0
    76
void BindTest::bind2nd3()
sl@0
    77
{
sl@0
    78
#if defined (STLPORT) && \
sl@0
    79
    !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
sl@0
    80
  int array[3] = { 1, 2, 3 };
sl@0
    81
  transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1));
sl@0
    82
  transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1));
sl@0
    83
  CPPUNIT_ASSERT(array[0] == 1);
sl@0
    84
  CPPUNIT_ASSERT(array[1] == 2);
sl@0
    85
  CPPUNIT_ASSERT(array[2] == 3);
sl@0
    86
sl@0
    87
  transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10));
sl@0
    88
  CPPUNIT_ASSERT(array[0] == 21);
sl@0
    89
  CPPUNIT_ASSERT(array[1] == 22);
sl@0
    90
  CPPUNIT_ASSERT(array[2] == 23);
sl@0
    91
#endif
sl@0
    92
}
sl@0
    93
sl@0
    94
class A
sl@0
    95
{
sl@0
    96
  public:
sl@0
    97
    A() :
sl@0
    98
        m_n( 0 )
sl@0
    99
      {}
sl@0
   100
sl@0
   101
    void f( int n ) const
sl@0
   102
      { m_n = n; }
sl@0
   103
sl@0
   104
    int v() const
sl@0
   105
      { return m_n; }
sl@0
   106
sl@0
   107
  private:
sl@0
   108
    mutable int m_n;
sl@0
   109
};
sl@0
   110
sl@0
   111
void BindTest::bind_memfn()
sl@0
   112
{
sl@0
   113
#if defined (STLPORT) && \
sl@0
   114
    !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
sl@0
   115
  A array[3];
sl@0
   116
sl@0
   117
  for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
sl@0
   118
sl@0
   119
  CPPUNIT_CHECK( array[0].v() == 12 );
sl@0
   120
#endif
sl@0
   121
}