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 <functional>
|
|
18 |
#include <memory>
|
|
19 |
#include <vector>
|
|
20 |
#include <algorithm>
|
|
21 |
|
|
22 |
#include "cppunit/cppunit_proxy.h"
|
|
23 |
|
|
24 |
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
|
25 |
using namespace std;
|
|
26 |
#endif
|
|
27 |
|
|
28 |
//
|
|
29 |
// TestCase class
|
|
30 |
//
|
|
31 |
class MemFunPtrTest : public CPPUNIT_NS::TestCase
|
|
32 |
{
|
|
33 |
CPPUNIT_TEST_SUITE(MemFunPtrTest);
|
|
34 |
CPPUNIT_TEST(mem_ptr_fun);
|
|
35 |
#if defined (STLPORT) && !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
36 |
//This test require partial template specialization feature to avoid the
|
|
37 |
//reference to reference problem. No workaround yet for limited compilers.
|
|
38 |
CPPUNIT_IGNORE;
|
|
39 |
#endif
|
|
40 |
CPPUNIT_TEST(find);
|
|
41 |
CPPUNIT_TEST_SUITE_END();
|
|
42 |
|
|
43 |
protected:
|
|
44 |
// compile test not neccessary to run but...
|
|
45 |
void mem_ptr_fun();
|
|
46 |
void find();
|
|
47 |
};
|
|
48 |
|
|
49 |
CPPUNIT_TEST_SUITE_REGISTRATION(MemFunPtrTest);
|
|
50 |
|
|
51 |
#if defined(_STLP_DONT_RETURN_VOID) && (defined(_STLP_NO_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_NO_CLASS_PARTIAL_SPECIALIZATION))
|
|
52 |
# define _STLP_DONT_TEST_RETURN_VOID
|
|
53 |
#endif
|
|
54 |
//else there is no workaround for the return void bug
|
|
55 |
|
|
56 |
struct S1 { } s1;
|
|
57 |
struct S2 { } s2;
|
|
58 |
|
|
59 |
int f1(S1&);
|
|
60 |
int f2(S1&, S2&);
|
|
61 |
int f1c(const S1&);
|
|
62 |
int f2c(const S1&, const S2&);
|
|
63 |
|
|
64 |
void vf1(S1&);
|
|
65 |
void vf2(S1&, S2&);
|
|
66 |
void vf1c(const S1&);
|
|
67 |
void vf2c(const S1&, const S2&);
|
|
68 |
|
|
69 |
class Class {
|
|
70 |
public:
|
|
71 |
int f0();
|
|
72 |
int f1(const S1&);
|
|
73 |
|
|
74 |
void vf0();
|
|
75 |
void vf1(const S1&);
|
|
76 |
|
|
77 |
int f0c() const;
|
|
78 |
int f1c(const S1&) const;
|
|
79 |
|
|
80 |
void vf0c() const;
|
|
81 |
void vf1c(const S1&) const;
|
|
82 |
};
|
|
83 |
|
|
84 |
//
|
|
85 |
// tests implementation
|
|
86 |
//
|
|
87 |
void MemFunPtrTest::mem_ptr_fun()
|
|
88 |
{
|
|
89 |
Class obj;
|
|
90 |
const Class& objc = obj;
|
|
91 |
|
|
92 |
// ptr_fun
|
|
93 |
|
|
94 |
ptr_fun(f1)(s1);
|
|
95 |
ptr_fun(f2)(s1, s2);
|
|
96 |
|
|
97 |
ptr_fun(f1c)(s1);
|
|
98 |
ptr_fun(f2c)(s1, s2);
|
|
99 |
|
|
100 |
#ifndef _STLP_DONT_TEST_RETURN_VOID
|
|
101 |
ptr_fun(vf1)(s1);
|
|
102 |
ptr_fun(vf2)(s1, s2);
|
|
103 |
|
|
104 |
ptr_fun(vf1c)(s1);
|
|
105 |
ptr_fun(vf2c)(s1, s2);
|
|
106 |
#endif /* _STLP_DONT_TEST_RETURN_VOID */
|
|
107 |
|
|
108 |
// mem_fun
|
|
109 |
|
|
110 |
mem_fun(&Class::f0)(&obj);
|
|
111 |
mem_fun(&Class::f1)(&obj, s1);
|
|
112 |
|
|
113 |
#ifndef _STLP_DONT_TEST_RETURN_VOID
|
|
114 |
mem_fun(&Class::vf0)(&obj);
|
|
115 |
mem_fun(&Class::vf1)(&obj, s1);
|
|
116 |
#endif /* _STLP_DONT_TEST_RETURN_VOID */
|
|
117 |
|
|
118 |
// mem_fun (const)
|
|
119 |
|
|
120 |
mem_fun(&Class::f0c)(&objc);
|
|
121 |
mem_fun(&Class::f1c)(&objc, s1);
|
|
122 |
|
|
123 |
#ifndef _STLP_DONT_TEST_RETURN_VOID
|
|
124 |
mem_fun(&Class::vf0c)(&objc);
|
|
125 |
mem_fun(&Class::vf1c)(&objc, s1);
|
|
126 |
#endif /* _STLP_DONT_TEST_RETURN_VOID */
|
|
127 |
|
|
128 |
// mem_fun_ref
|
|
129 |
|
|
130 |
mem_fun_ref(&Class::f0)(obj);
|
|
131 |
mem_fun_ref(&Class::f1)(obj, s1);
|
|
132 |
|
|
133 |
#ifndef _STLP_DONT_TEST_RETURN_VOID
|
|
134 |
mem_fun_ref(&Class::vf0)(obj);
|
|
135 |
mem_fun_ref(&Class::vf1)(obj, s1);
|
|
136 |
#endif /* _STLP_DONT_TEST_RETURN_VOID */
|
|
137 |
|
|
138 |
// mem_fun_ref (const)
|
|
139 |
mem_fun_ref(&Class::f0c)(objc);
|
|
140 |
mem_fun_ref(&Class::f1c)(objc, s1);
|
|
141 |
|
|
142 |
#ifndef _STLP_DONT_TEST_RETURN_VOID
|
|
143 |
mem_fun_ref(&Class::vf0c)(objc);
|
|
144 |
mem_fun_ref(&Class::vf1c)(objc, s1);
|
|
145 |
#endif /* _STLP_DONT_TEST_RETURN_VOID */
|
|
146 |
}
|
|
147 |
int f1(S1&)
|
|
148 |
{return 1;}
|
|
149 |
|
|
150 |
int f2(S1&, S2&)
|
|
151 |
{return 2;}
|
|
152 |
|
|
153 |
int f1c(const S1&)
|
|
154 |
{return 1;}
|
|
155 |
|
|
156 |
int f2c(const S1&, const S2&)
|
|
157 |
{return 2;}
|
|
158 |
|
|
159 |
void vf1(S1&)
|
|
160 |
{}
|
|
161 |
|
|
162 |
void vf2(S1&, S2&)
|
|
163 |
{}
|
|
164 |
|
|
165 |
void vf1c(const S1&)
|
|
166 |
{}
|
|
167 |
|
|
168 |
void vf2c(const S1&, const S2&)
|
|
169 |
{}
|
|
170 |
|
|
171 |
int Class::f0()
|
|
172 |
{return 0;}
|
|
173 |
|
|
174 |
int Class::f1(const S1&)
|
|
175 |
{return 1;}
|
|
176 |
|
|
177 |
void Class::vf0()
|
|
178 |
{}
|
|
179 |
|
|
180 |
void Class::vf1(const S1&)
|
|
181 |
{}
|
|
182 |
|
|
183 |
int Class::f0c() const
|
|
184 |
{return 0;}
|
|
185 |
|
|
186 |
int Class::f1c(const S1&) const
|
|
187 |
{return 1;}
|
|
188 |
|
|
189 |
void Class::vf0c() const
|
|
190 |
{}
|
|
191 |
|
|
192 |
void Class::vf1c(const S1&) const
|
|
193 |
{}
|
|
194 |
|
|
195 |
struct V {
|
|
196 |
public:
|
|
197 |
V(int _v) :
|
|
198 |
v(_v)
|
|
199 |
{ }
|
|
200 |
|
|
201 |
bool f( int _v ) const { return (v == _v); }
|
|
202 |
|
|
203 |
int v;
|
|
204 |
#if defined (__DMC__)
|
|
205 |
V(){}
|
|
206 |
#endif
|
|
207 |
};
|
|
208 |
|
|
209 |
void MemFunPtrTest::find()
|
|
210 |
{
|
|
211 |
#if !defined (STLPORT) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
|
|
212 |
vector<V> v;
|
|
213 |
|
|
214 |
v.push_back( V(1) );
|
|
215 |
v.push_back( V(2) );
|
|
216 |
v.push_back( V(3) );
|
|
217 |
|
|
218 |
// step-by-step complication of work for compiler:
|
|
219 |
|
|
220 |
// step 1:
|
|
221 |
const_mem_fun1_ref_t<bool,V,int> pmf = mem_fun_ref( &V::f );
|
|
222 |
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b(pmf, 2);
|
|
223 |
vector<V>::iterator i = find_if( v.begin(), v.end(), b );
|
|
224 |
CPPUNIT_ASSERT(i != v.end());
|
|
225 |
CPPUNIT_ASSERT(i->v == 2);
|
|
226 |
|
|
227 |
// step 2, just check that compiler understand what pass to bind2nd:
|
|
228 |
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b2 = bind2nd( pmf, 2 );
|
|
229 |
|
|
230 |
// step 3, the same as step 1, but more intellect from compiler required:
|
|
231 |
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b3 = bind2nd( mem_fun_ref( &V::f ), 2 );
|
|
232 |
|
|
233 |
vector<V>::iterator j = find_if( v.begin(), v.end(), b3 );
|
|
234 |
CPPUNIT_ASSERT(j != v.end());
|
|
235 |
CPPUNIT_ASSERT(j->v == 2);
|
|
236 |
|
|
237 |
// step 4, more brief, more complex:
|
|
238 |
vector<V>::iterator k = find_if( v.begin(), v.end(), bind2nd( mem_fun_ref( &V::f ), 2 ) );
|
|
239 |
CPPUNIT_ASSERT(k != v.end());
|
|
240 |
CPPUNIT_ASSERT(k->v == 2);
|
|
241 |
#endif
|
|
242 |
}
|
|
243 |
|
|
244 |
#ifdef _STLP_DONT_TEST_RETURN_VOID
|
|
245 |
# undef _STLP_DONT_TEST_RETURN_VOID
|
|
246 |
#endif
|