Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmfunptr_test.cpp
Go to the documentation of this file.
00001 #include <functional> 00002 #include <memory> 00003 #include <vector> 00004 #include <algorithm> 00005 00006 #include "cppunit/cppunit_proxy.h" 00007 00008 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00009 using namespace std; 00010 #endif 00011 00012 // 00013 // TestCase class 00014 // 00015 class MemFunPtrTest : public CPPUNIT_NS::TestCase 00016 { 00017 CPPUNIT_TEST_SUITE(MemFunPtrTest); 00018 CPPUNIT_TEST(mem_ptr_fun); 00019 #if defined (STLPORT) && !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00020 //This test require partial template specialization feature to avoid the 00021 //reference to reference problem. No workaround yet for limited compilers. 00022 CPPUNIT_IGNORE; 00023 #endif 00024 CPPUNIT_TEST(find); 00025 CPPUNIT_TEST_SUITE_END(); 00026 00027 protected: 00028 // compile test not neccessary to run but... 00029 void mem_ptr_fun(); 00030 void find(); 00031 }; 00032 00033 CPPUNIT_TEST_SUITE_REGISTRATION(MemFunPtrTest); 00034 00035 #if defined(_STLP_DONT_RETURN_VOID) && (defined(_STLP_NO_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_NO_CLASS_PARTIAL_SPECIALIZATION)) 00036 # define _STLP_DONT_TEST_RETURN_VOID 00037 #endif 00038 //else there is no workaround for the return void bug 00039 00040 struct S1 { } s1; 00041 struct S2 { } s2; 00042 00043 int f1(S1&); 00044 int f2(S1&, S2&); 00045 int f1c(const S1&); 00046 int f2c(const S1&, const S2&); 00047 00048 void vf1(S1&); 00049 void vf2(S1&, S2&); 00050 void vf1c(const S1&); 00051 void vf2c(const S1&, const S2&); 00052 00053 class Class { 00054 public: 00055 int f0(); 00056 int f1(const S1&); 00057 00058 void vf0(); 00059 void vf1(const S1&); 00060 00061 int f0c() const; 00062 int f1c(const S1&) const; 00063 00064 void vf0c() const; 00065 void vf1c(const S1&) const; 00066 }; 00067 00068 // 00069 // tests implementation 00070 // 00071 void MemFunPtrTest::mem_ptr_fun() 00072 { 00073 Class obj; 00074 const Class& objc = obj; 00075 00076 // ptr_fun 00077 00078 ptr_fun(f1)(s1); 00079 ptr_fun(f2)(s1, s2); 00080 00081 ptr_fun(f1c)(s1); 00082 ptr_fun(f2c)(s1, s2); 00083 00084 #ifndef _STLP_DONT_TEST_RETURN_VOID 00085 ptr_fun(vf1)(s1); 00086 ptr_fun(vf2)(s1, s2); 00087 00088 ptr_fun(vf1c)(s1); 00089 ptr_fun(vf2c)(s1, s2); 00090 #endif /* _STLP_DONT_TEST_RETURN_VOID */ 00091 00092 // mem_fun 00093 00094 mem_fun(&Class::f0)(&obj); 00095 mem_fun(&Class::f1)(&obj, s1); 00096 00097 #ifndef _STLP_DONT_TEST_RETURN_VOID 00098 mem_fun(&Class::vf0)(&obj); 00099 mem_fun(&Class::vf1)(&obj, s1); 00100 #endif /* _STLP_DONT_TEST_RETURN_VOID */ 00101 00102 // mem_fun (const) 00103 00104 mem_fun(&Class::f0c)(&objc); 00105 mem_fun(&Class::f1c)(&objc, s1); 00106 00107 #ifndef _STLP_DONT_TEST_RETURN_VOID 00108 mem_fun(&Class::vf0c)(&objc); 00109 mem_fun(&Class::vf1c)(&objc, s1); 00110 #endif /* _STLP_DONT_TEST_RETURN_VOID */ 00111 00112 // mem_fun_ref 00113 00114 mem_fun_ref(&Class::f0)(obj); 00115 mem_fun_ref(&Class::f1)(obj, s1); 00116 00117 #ifndef _STLP_DONT_TEST_RETURN_VOID 00118 mem_fun_ref(&Class::vf0)(obj); 00119 mem_fun_ref(&Class::vf1)(obj, s1); 00120 #endif /* _STLP_DONT_TEST_RETURN_VOID */ 00121 00122 // mem_fun_ref (const) 00123 mem_fun_ref(&Class::f0c)(objc); 00124 mem_fun_ref(&Class::f1c)(objc, s1); 00125 00126 #ifndef _STLP_DONT_TEST_RETURN_VOID 00127 mem_fun_ref(&Class::vf0c)(objc); 00128 mem_fun_ref(&Class::vf1c)(objc, s1); 00129 #endif /* _STLP_DONT_TEST_RETURN_VOID */ 00130 } 00131 int f1(S1&) 00132 {return 1;} 00133 00134 int f2(S1&, S2&) 00135 {return 2;} 00136 00137 int f1c(const S1&) 00138 {return 1;} 00139 00140 int f2c(const S1&, const S2&) 00141 {return 2;} 00142 00143 void vf1(S1&) 00144 {} 00145 00146 void vf2(S1&, S2&) 00147 {} 00148 00149 void vf1c(const S1&) 00150 {} 00151 00152 void vf2c(const S1&, const S2&) 00153 {} 00154 00155 int Class::f0() 00156 {return 0;} 00157 00158 int Class::f1(const S1&) 00159 {return 1;} 00160 00161 void Class::vf0() 00162 {} 00163 00164 void Class::vf1(const S1&) 00165 {} 00166 00167 int Class::f0c() const 00168 {return 0;} 00169 00170 int Class::f1c(const S1&) const 00171 {return 1;} 00172 00173 void Class::vf0c() const 00174 {} 00175 00176 void Class::vf1c(const S1&) const 00177 {} 00178 00179 struct V { 00180 public: 00181 V(int _v) : 00182 v(_v) 00183 { } 00184 00185 bool f( int _v ) const { return (v == _v); } 00186 00187 int v; 00188 #if defined (__DMC__) 00189 V(){} 00190 #endif 00191 }; 00192 00193 void MemFunPtrTest::find() 00194 { 00195 #if !defined (STLPORT) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) 00196 vector<V> v; 00197 00198 v.push_back( V(1) ); 00199 v.push_back( V(2) ); 00200 v.push_back( V(3) ); 00201 00202 // step-by-step complication of work for compiler: 00203 00204 // step 1: 00205 const_mem_fun1_ref_t<bool,V,int> pmf = mem_fun_ref( &V::f ); 00206 binder2nd<const_mem_fun1_ref_t<bool,V,int> > b(pmf, 2); 00207 vector<V>::iterator i = find_if( v.begin(), v.end(), b ); 00208 CPPUNIT_ASSERT(i != v.end()); 00209 CPPUNIT_ASSERT(i->v == 2); 00210 00211 // step 2, just check that compiler understand what pass to bind2nd: 00212 binder2nd<const_mem_fun1_ref_t<bool,V,int> > b2 = bind2nd( pmf, 2 ); 00213 00214 // step 3, the same as step 1, but more intellect from compiler required: 00215 binder2nd<const_mem_fun1_ref_t<bool,V,int> > b3 = bind2nd( mem_fun_ref( &V::f ), 2 ); 00216 00217 vector<V>::iterator j = find_if( v.begin(), v.end(), b3 ); 00218 CPPUNIT_ASSERT(j != v.end()); 00219 CPPUNIT_ASSERT(j->v == 2); 00220 00221 // step 4, more brief, more complex: 00222 vector<V>::iterator k = find_if( v.begin(), v.end(), bind2nd( mem_fun_ref( &V::f ), 2 ) ); 00223 CPPUNIT_ASSERT(k != v.end()); 00224 CPPUNIT_ASSERT(k->v == 2); 00225 #endif 00226 } 00227 00228 #ifdef _STLP_DONT_TEST_RETURN_VOID 00229 # undef _STLP_DONT_TEST_RETURN_VOID 00230 #endif Generated on Sun May 27 2012 04:35:29 for ReactOS by
1.7.6.1
|