4 #include "cppunit/cppunit_proxy.h"
6 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
13 class BindTest : public CPPUNIT_NS::TestCase
15 CPPUNIT_TEST_SUITE(BindTest);
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)
23 CPPUNIT_TEST(bind2nd3);
24 CPPUNIT_TEST(bind_memfn);
25 CPPUNIT_TEST_SUITE_END();
35 CPPUNIT_TEST_SUITE_REGISTRATION(BindTest);
37 class pre_increment : public binary_function<int, int, int> {
39 int operator()(int incr, int& val) const
40 { return val += incr; }
43 class post_increment : public binary_function<int, int, int> {
45 int operator()(int& val, int incr) const
46 { return val += incr; }
51 // tests implementation
53 void BindTest::bind1st1()
55 int array [3] = { 1, 2, 3 };
56 int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
58 CPPUNIT_ASSERT(p == &array[2]);
59 CPPUNIT_ASSERT(array[0] == 1);
60 CPPUNIT_ASSERT(array[1] == 2);
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);
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);
73 void BindTest::bind2nd1()
75 int array [3] = { 1, 2, 3 };
76 replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4);
78 CPPUNIT_ASSERT(array[0]==1);
79 CPPUNIT_ASSERT(array[1]==2);
80 CPPUNIT_ASSERT(array[2]==4);
82 void BindTest::bind2nd2()
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);
91 int test_func1 (const int ¶m1, const int ¶m2) {
92 return param1 + param2;
95 int test_func2 (int ¶m1, int param2) {
97 return param1 + param2;
100 void BindTest::bind2nd3()
102 #if defined (STLPORT) && \
103 !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
104 int array[3] = { 1, 2, 3 };
105 transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1));
106 transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1));
107 CPPUNIT_ASSERT(array[0] == 1);
108 CPPUNIT_ASSERT(array[1] == 2);
109 CPPUNIT_ASSERT(array[2] == 3);
111 transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10));
112 CPPUNIT_ASSERT(array[0] == 21);
113 CPPUNIT_ASSERT(array[1] == 22);
114 CPPUNIT_ASSERT(array[2] == 23);
124 void f( int n ) const {
125 #if defined (STLPORT)
126 _STLP_MUTABLE(A, m_n) = n;
139 void BindTest::bind_memfn()
141 #if defined (STLPORT) && \
142 !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
145 for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
147 CPPUNIT_CHECK( array[0].v() == 12 );