ReactOS  0.4.12-dev-919-gfc3b8d5
bind_test.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <functional>
3 
5 
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
7 using namespace std;
8 #endif
9 
10 //
11 // TestCase class
12 //
13 class BindTest : public CPPUNIT_NS::TestCase
14 {
16  CPPUNIT_TEST(bind1st1);
17  CPPUNIT_TEST(bind2nd1);
18  CPPUNIT_TEST(bind2nd2);
19 #if !defined (STLPORT) || \
20  defined (_STLP_NO_EXTENSIONS) || !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
22 #endif
23  CPPUNIT_TEST(bind2nd3);
24  CPPUNIT_TEST(bind_memfn);
26 
27 protected:
28  void bind1st1();
29  void bind2nd1();
30  void bind2nd2();
31  void bind2nd3();
32  void bind_memfn();
33 };
34 
36 
37 class pre_increment : public binary_function<int, int, int> {
38 public:
39  int operator()(int incr, int& val) const
40  { return val += incr; }
41 };
42 
43 class post_increment : public binary_function<int, int, int> {
44 public:
45  int operator()(int& val, int incr) const
46  { return val += incr; }
47 };
48 
49 
50 //
51 // tests implementation
52 //
54 {
55  int array [3] = { 1, 2, 3 };
56  int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
57 
58  CPPUNIT_ASSERT(p == &array[2]);
59  CPPUNIT_ASSERT(array[0] == 1);
60  CPPUNIT_ASSERT(array[1] == 2);
61 
62  for_each((int*)array, (int*)array + 3, bind1st(pre_increment(), 1));
63  CPPUNIT_ASSERT(array[0] == 2);
64  CPPUNIT_ASSERT(array[1] == 3);
65  CPPUNIT_ASSERT(array[2] == 4);
66 
67  for_each((int*)array, (int*)array + 3, bind2nd(post_increment(), 1));
68  CPPUNIT_ASSERT(array[0] == 3);
69  CPPUNIT_ASSERT(array[1] == 4);
70  CPPUNIT_ASSERT(array[2] == 5);
71 }
72 
74 {
75  int array [3] = { 1, 2, 3 };
77 
78  CPPUNIT_ASSERT(array[0]==1);
79  CPPUNIT_ASSERT(array[1]==2);
80  CPPUNIT_ASSERT(array[2]==4);
81 }
83 {
84  int array [3] = { 1, 2, 3 };
85  replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4);
86  CPPUNIT_ASSERT(array[0]==1);
87  CPPUNIT_ASSERT(array[1]==2);
88  CPPUNIT_ASSERT(array[2]==4);
89 }
90 
91 int test_func1 (const int &param1, const int &param2) {
92  return param1 + param2;
93 }
94 
95 int test_func2 (int &param1, int param2) {
96  param1 += param2;
97  return param1 + param2;
98 }
99 
101 {
102 #if defined (STLPORT) && \
103  !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
104  int array[3] = { 1, 2, 3 };
107  CPPUNIT_ASSERT(array[0] == 1);
108  CPPUNIT_ASSERT(array[1] == 2);
109  CPPUNIT_ASSERT(array[2] == 3);
110 
112  CPPUNIT_ASSERT(array[0] == 21);
113  CPPUNIT_ASSERT(array[1] == 22);
114  CPPUNIT_ASSERT(array[2] == 23);
115 #endif
116 }
117 
118 class A
119 {
120  public:
121  A() : m_n( 0 )
122  {}
123 
124  void f( int n ) const {
125 #if defined (STLPORT)
126  _STLP_MUTABLE(A, m_n) = n;
127 #else
128  m_n = n;
129 #endif
130  }
131 
132  int v() const
133  { return m_n; }
134 
135  private:
136  mutable int m_n;
137 };
138 
140 {
141 #if defined (STLPORT) && \
142  !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
143  A array[3];
144 
145  for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
146 
147  CPPUNIT_CHECK( array[0].v() == 12 );
148 #endif
149 }
int test_func1(const int &param1, const int &param2)
Definition: bind_test.cpp:91
GLdouble n
Definition: glext.h:7729
#define CPPUNIT_TEST_SUITE(X)
Definition: cppunit_mini.h:142
pointer_to_unary_function< _Arg, _Result > ptr_fun(_Result(*__f)(_Arg))
#define CPPUNIT_TEST(X)
Definition: cppunit_mini.h:182
Definition: features.h:417
int test_func2(int &param1, int param2)
Definition: bind_test.cpp:95
void bind2nd3()
Definition: bind_test.cpp:100
binder2nd< _Operation > bind2nd(const _Operation &__fn, const _Tp &__x)
Definition: _function.h:252
A()
Definition: bind_test.cpp:121
int operator()(int incr, int &val) const
Definition: bind_test.cpp:39
binder1st< _Operation > bind1st(const _Operation &__fn, const _Tp &__x)
Definition: _function.h:217
#define CPPUNIT_TEST_SUITE_END()
Definition: cppunit_mini.h:191
int v() const
Definition: bind_test.cpp:132
#define _STLP_MUTABLE(type, x)
Definition: features.h:634
void bind_memfn()
Definition: bind_test.cpp:139
GLuint GLfloat * val
Definition: glext.h:7180
_STLP_INLINE_LOOP void replace_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred, const _Tp &__new_value)
Definition: _algo.h:190
#define CPPUNIT_CHECK(X)
Definition: cppunit_mini.h:195
void bind2nd1()
Definition: bind_test.cpp:73
_STLP_BEGIN_NAMESPACE _STLP_INLINE_LOOP _Function for_each(_InputIter __first, _InputIter __last, _Function __f)
Definition: _algo.h:59
void bind1st1()
Definition: bind_test.cpp:53
_STLP_INLINE_LOOP _ForwardIter remove_if(_ForwardIter __first, _ForwardIter __last, _Predicate __pred)
Definition: _algo.h:278
Definition: ttei1.cpp:12
CPPUNIT_TEST_SUITE_REGISTRATION(BindTest)
int operator()(int &val, int incr) const
Definition: bind_test.cpp:45
void bind2nd2()
Definition: bind_test.cpp:82
#define CPPUNIT_ASSERT(X)
Definition: cppunit_mini.h:200
int m_n
Definition: bind_test.cpp:136
mem_fun_ref_t< _Result, _Tp > mem_fun_ref(_Result(_Tp::*__f)())
const GLdouble * v
Definition: gl.h:2040
GLuint GLenum GLenum transform
Definition: glext.h:9407
GLfloat GLfloat p
Definition: glext.h:8902
void f(int n) const
Definition: bind_test.cpp:124