31
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include <algorithm>
|
|
18 |
#include <functional>
|
|
19 |
|
|
20 |
#include "cppunit/cppunit_proxy.h"
|
|
21 |
|
|
22 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
|
23 |
using namespace std;
|
|
24 |
#endif
|
|
25 |
|
|
26 |
//
|
|
27 |
// TestCase class
|
|
28 |
//
|
|
29 |
class BindTest : public CPPUNIT_NS::TestCase
|
|
30 |
{
|
|
31 |
CPPUNIT_TEST_SUITE(BindTest);
|
|
32 |
CPPUNIT_TEST(bind1st1);
|
|
33 |
CPPUNIT_TEST(bind2nd1);
|
|
34 |
CPPUNIT_TEST(bind2nd2);
|
|
35 |
#if !defined (STLPORT) || \
|
|
36 |
defined (_STLP_NO_EXTENSIONS) || !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
37 |
CPPUNIT_IGNORE;
|
|
38 |
#endif
|
|
39 |
CPPUNIT_TEST(bind2nd3);
|
|
40 |
CPPUNIT_TEST(bind_memfn);
|
|
41 |
CPPUNIT_TEST_SUITE_END();
|
|
42 |
|
|
43 |
protected:
|
|
44 |
void bind1st1();
|
|
45 |
void bind2nd1();
|
|
46 |
void bind2nd2();
|
|
47 |
void bind2nd3();
|
|
48 |
void bind_memfn();
|
|
49 |
};
|
|
50 |
|
|
51 |
CPPUNIT_TEST_SUITE_REGISTRATION(BindTest);
|
|
52 |
|
|
53 |
//
|
|
54 |
// tests implementation
|
|
55 |
//
|
|
56 |
void BindTest::bind1st1()
|
|
57 |
{
|
|
58 |
int array [3] = { 1, 2, 3 };
|
|
59 |
int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
|
|
60 |
|
|
61 |
CPPUNIT_ASSERT(p==&array[2]);
|
|
62 |
CPPUNIT_ASSERT(array[0]==1);
|
|
63 |
CPPUNIT_ASSERT(array[1]==2);
|
|
64 |
}
|
|
65 |
void BindTest::bind2nd1()
|
|
66 |
{
|
|
67 |
int array [3] = { 1, 2, 3 };
|
|
68 |
replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4);
|
|
69 |
|
|
70 |
CPPUNIT_ASSERT(array[0]==1);
|
|
71 |
CPPUNIT_ASSERT(array[1]==2);
|
|
72 |
CPPUNIT_ASSERT(array[2]==4);
|
|
73 |
}
|
|
74 |
void BindTest::bind2nd2()
|
|
75 |
{
|
|
76 |
int array [3] = { 1, 2, 3 };
|
|
77 |
replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4);
|
|
78 |
CPPUNIT_ASSERT(array[0]==1);
|
|
79 |
CPPUNIT_ASSERT(array[1]==2);
|
|
80 |
CPPUNIT_ASSERT(array[2]==4);
|
|
81 |
}
|
|
82 |
|
|
83 |
int test_func1 (const int ¶m1, const int ¶m2) {
|
|
84 |
return param1 + param2;
|
|
85 |
}
|
|
86 |
|
|
87 |
int test_func2 (int ¶m1, int param2) {
|
|
88 |
param1 += param2;
|
|
89 |
return param1 + param2;
|
|
90 |
}
|
|
91 |
|
|
92 |
void BindTest::bind2nd3()
|
|
93 |
{
|
|
94 |
#if defined (STLPORT) && \
|
|
95 |
!defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
96 |
int array[3] = { 1, 2, 3 };
|
|
97 |
transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1));
|
|
98 |
transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1));
|
|
99 |
CPPUNIT_ASSERT(array[0] == 1);
|
|
100 |
CPPUNIT_ASSERT(array[1] == 2);
|
|
101 |
CPPUNIT_ASSERT(array[2] == 3);
|
|
102 |
|
|
103 |
transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10));
|
|
104 |
CPPUNIT_ASSERT(array[0] == 21);
|
|
105 |
CPPUNIT_ASSERT(array[1] == 22);
|
|
106 |
CPPUNIT_ASSERT(array[2] == 23);
|
|
107 |
#endif
|
|
108 |
}
|
|
109 |
|
|
110 |
class A
|
|
111 |
{
|
|
112 |
public:
|
|
113 |
A() :
|
|
114 |
m_n( 0 )
|
|
115 |
{}
|
|
116 |
|
|
117 |
void f( int n ) const
|
|
118 |
{ m_n = n; }
|
|
119 |
|
|
120 |
int v() const
|
|
121 |
{ return m_n; }
|
|
122 |
|
|
123 |
private:
|
|
124 |
mutable int m_n;
|
|
125 |
};
|
|
126 |
|
|
127 |
void BindTest::bind_memfn()
|
|
128 |
{
|
|
129 |
#if defined (STLPORT) && \
|
|
130 |
!defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
131 |
A array[3];
|
|
132 |
|
|
133 |
for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
|
|
134 |
|
|
135 |
CPPUNIT_CHECK( array[0].v() == 12 );
|
|
136 |
#endif
|
|
137 |
}
|