|
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 // |
|
38 // tests implementation |
|
39 // |
|
40 void BindTest::bind1st1() |
|
41 { |
|
42 int array [3] = { 1, 2, 3 }; |
|
43 int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2)); |
|
44 |
|
45 CPPUNIT_ASSERT(p==&array[2]); |
|
46 CPPUNIT_ASSERT(array[0]==1); |
|
47 CPPUNIT_ASSERT(array[1]==2); |
|
48 } |
|
49 void BindTest::bind2nd1() |
|
50 { |
|
51 int array [3] = { 1, 2, 3 }; |
|
52 replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4); |
|
53 |
|
54 CPPUNIT_ASSERT(array[0]==1); |
|
55 CPPUNIT_ASSERT(array[1]==2); |
|
56 CPPUNIT_ASSERT(array[2]==4); |
|
57 } |
|
58 void BindTest::bind2nd2() |
|
59 { |
|
60 int array [3] = { 1, 2, 3 }; |
|
61 replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4); |
|
62 CPPUNIT_ASSERT(array[0]==1); |
|
63 CPPUNIT_ASSERT(array[1]==2); |
|
64 CPPUNIT_ASSERT(array[2]==4); |
|
65 } |
|
66 |
|
67 int test_func1 (const int ¶m1, const int ¶m2) { |
|
68 return param1 + param2; |
|
69 } |
|
70 |
|
71 int test_func2 (int ¶m1, int param2) { |
|
72 param1 += param2; |
|
73 return param1 + param2; |
|
74 } |
|
75 |
|
76 void BindTest::bind2nd3() |
|
77 { |
|
78 #if defined (STLPORT) && \ |
|
79 !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) |
|
80 int array[3] = { 1, 2, 3 }; |
|
81 transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1)); |
|
82 transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1)); |
|
83 CPPUNIT_ASSERT(array[0] == 1); |
|
84 CPPUNIT_ASSERT(array[1] == 2); |
|
85 CPPUNIT_ASSERT(array[2] == 3); |
|
86 |
|
87 transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10)); |
|
88 CPPUNIT_ASSERT(array[0] == 21); |
|
89 CPPUNIT_ASSERT(array[1] == 22); |
|
90 CPPUNIT_ASSERT(array[2] == 23); |
|
91 #endif |
|
92 } |
|
93 |
|
94 class A |
|
95 { |
|
96 public: |
|
97 A() : |
|
98 m_n( 0 ) |
|
99 {} |
|
100 |
|
101 void f( int n ) const |
|
102 { m_n = n; } |
|
103 |
|
104 int v() const |
|
105 { return m_n; } |
|
106 |
|
107 private: |
|
108 mutable int m_n; |
|
109 }; |
|
110 |
|
111 void BindTest::bind_memfn() |
|
112 { |
|
113 #if defined (STLPORT) && \ |
|
114 !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) |
|
115 A array[3]; |
|
116 |
|
117 for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) ); |
|
118 |
|
119 CPPUNIT_CHECK( array[0].v() == 12 ); |
|
120 #endif |
|
121 } |