1 #include <algorithm> 2 #include <functional> 3 4 #include "cppunit/cppunit_proxy.h" 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 { 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) 21 CPPUNIT_IGNORE; 22 #endif 23 CPPUNIT_TEST(bind2nd3); 24 CPPUNIT_TEST(bind_memfn); 25 CPPUNIT_TEST_SUITE_END(); 26 27 protected: 28 void bind1st1(); 29 void bind2nd1(); 30 void bind2nd2(); 31 void bind2nd3(); 32 void bind_memfn(); 33 }; 34 35 CPPUNIT_TEST_SUITE_REGISTRATION(BindTest); 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 // 53 void BindTest::bind1st1() 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 73 void BindTest::bind2nd1() 74 { 75 int array [3] = { 1, 2, 3 }; 76 replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4); 77 78 CPPUNIT_ASSERT(array[0]==1); 79 CPPUNIT_ASSERT(array[1]==2); 80 CPPUNIT_ASSERT(array[2]==4); 81 } 82 void BindTest::bind2nd2() 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 ¶m1, const int ¶m2) { 92 return param1 + param2; 93 } 94 95 int test_func2 (int ¶m1, int param2) { 96 param1 += param2; 97 return param1 + param2; 98 } 99 100 void BindTest::bind2nd3() 101 { 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); 110 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); 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 139 void BindTest::bind_memfn() 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 } 150