os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/mfunptr_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/mfunptr_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,230 @@
     1.4 +#include <functional>
     1.5 +#include <memory>
     1.6 +#include <vector>
     1.7 +#include <algorithm>
     1.8 +
     1.9 +#include "cppunit/cppunit_proxy.h"
    1.10 +
    1.11 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
    1.12 +using namespace std;
    1.13 +#endif
    1.14 +
    1.15 +//
    1.16 +// TestCase class
    1.17 +//
    1.18 +class MemFunPtrTest : public CPPUNIT_NS::TestCase
    1.19 +{
    1.20 +  CPPUNIT_TEST_SUITE(MemFunPtrTest);
    1.21 +  CPPUNIT_TEST(mem_ptr_fun);
    1.22 +#if defined (STLPORT) && !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
    1.23 +  //This test require partial template specialization feature to avoid the
    1.24 +  //reference to reference problem. No workaround yet for limited compilers.
    1.25 +  CPPUNIT_IGNORE;
    1.26 +#endif
    1.27 +  CPPUNIT_TEST(find);
    1.28 +  CPPUNIT_TEST_SUITE_END();
    1.29 +
    1.30 +protected:
    1.31 +  // compile test not neccessary to run but...
    1.32 +  void mem_ptr_fun();
    1.33 +  void find();
    1.34 +};
    1.35 +
    1.36 +CPPUNIT_TEST_SUITE_REGISTRATION(MemFunPtrTest);
    1.37 +
    1.38 +#if defined(_STLP_DONT_RETURN_VOID) && (defined(_STLP_NO_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_NO_CLASS_PARTIAL_SPECIALIZATION))
    1.39 +#  define _STLP_DONT_TEST_RETURN_VOID
    1.40 +#endif
    1.41 +//else there is no workaround for the return void bug
    1.42 +
    1.43 +struct S1 { } s1;
    1.44 +struct S2 { } s2;
    1.45 +
    1.46 +int f1(S1&);
    1.47 +int f2(S1&, S2&);
    1.48 +int f1c(const S1&);
    1.49 +int f2c(const S1&, const S2&);
    1.50 +
    1.51 +void vf1(S1&);
    1.52 +void vf2(S1&, S2&);
    1.53 +void vf1c(const S1&);
    1.54 +void vf2c(const S1&, const S2&);
    1.55 +
    1.56 +class Class {
    1.57 +public:
    1.58 +  int f0();
    1.59 +  int f1(const S1&);
    1.60 +
    1.61 +  void vf0();
    1.62 +  void vf1(const S1&);
    1.63 +
    1.64 +  int f0c() const;
    1.65 +  int f1c(const S1&) const;
    1.66 +
    1.67 +  void vf0c() const;
    1.68 +  void vf1c(const S1&) const;
    1.69 +};
    1.70 +
    1.71 +//
    1.72 +// tests implementation
    1.73 +//
    1.74 +void MemFunPtrTest::mem_ptr_fun()
    1.75 +{
    1.76 +  Class obj;
    1.77 +  const Class& objc = obj;
    1.78 +
    1.79 +  // ptr_fun
    1.80 +
    1.81 +  ptr_fun(f1)(s1);
    1.82 +  ptr_fun(f2)(s1, s2);
    1.83 +
    1.84 +  ptr_fun(f1c)(s1);
    1.85 +  ptr_fun(f2c)(s1, s2);
    1.86 +
    1.87 +#ifndef _STLP_DONT_TEST_RETURN_VOID
    1.88 +  ptr_fun(vf1)(s1);
    1.89 +  ptr_fun(vf2)(s1, s2);
    1.90 +
    1.91 +  ptr_fun(vf1c)(s1);
    1.92 +  ptr_fun(vf2c)(s1, s2);
    1.93 +#endif /* _STLP_DONT_TEST_RETURN_VOID */
    1.94 +
    1.95 +  // mem_fun
    1.96 +
    1.97 +  mem_fun(&Class::f0)(&obj);
    1.98 +  mem_fun(&Class::f1)(&obj, s1);
    1.99 +
   1.100 +#ifndef _STLP_DONT_TEST_RETURN_VOID
   1.101 +  mem_fun(&Class::vf0)(&obj);
   1.102 +  mem_fun(&Class::vf1)(&obj, s1);
   1.103 +#endif /* _STLP_DONT_TEST_RETURN_VOID */
   1.104 +
   1.105 +  // mem_fun (const)
   1.106 +
   1.107 +  mem_fun(&Class::f0c)(&objc);
   1.108 +  mem_fun(&Class::f1c)(&objc, s1);
   1.109 +
   1.110 +#ifndef _STLP_DONT_TEST_RETURN_VOID
   1.111 +  mem_fun(&Class::vf0c)(&objc);
   1.112 +  mem_fun(&Class::vf1c)(&objc, s1);
   1.113 +#endif /* _STLP_DONT_TEST_RETURN_VOID */
   1.114 +
   1.115 +  // mem_fun_ref
   1.116 +
   1.117 +  mem_fun_ref(&Class::f0)(obj);
   1.118 +  mem_fun_ref(&Class::f1)(obj, s1);
   1.119 +
   1.120 +#ifndef _STLP_DONT_TEST_RETURN_VOID
   1.121 +  mem_fun_ref(&Class::vf0)(obj);
   1.122 +  mem_fun_ref(&Class::vf1)(obj, s1);
   1.123 +#endif /* _STLP_DONT_TEST_RETURN_VOID */
   1.124 +
   1.125 +  // mem_fun_ref (const)
   1.126 +  mem_fun_ref(&Class::f0c)(objc);
   1.127 +  mem_fun_ref(&Class::f1c)(objc, s1);
   1.128 +
   1.129 +#ifndef _STLP_DONT_TEST_RETURN_VOID
   1.130 +  mem_fun_ref(&Class::vf0c)(objc);
   1.131 +  mem_fun_ref(&Class::vf1c)(objc, s1);
   1.132 +#endif /* _STLP_DONT_TEST_RETURN_VOID */
   1.133 +}
   1.134 +int f1(S1&)
   1.135 +{return 1;}
   1.136 +
   1.137 +int f2(S1&, S2&)
   1.138 +{return 2;}
   1.139 +
   1.140 +int f1c(const S1&)
   1.141 +{return 1;}
   1.142 +
   1.143 +int f2c(const S1&, const S2&)
   1.144 +{return 2;}
   1.145 +
   1.146 +void vf1(S1&)
   1.147 +{}
   1.148 +
   1.149 +void vf2(S1&, S2&)
   1.150 +{}
   1.151 +
   1.152 +void vf1c(const S1&)
   1.153 +{}
   1.154 +
   1.155 +void vf2c(const S1&, const S2&)
   1.156 +{}
   1.157 +
   1.158 +int Class::f0()
   1.159 +{return 0;}
   1.160 +
   1.161 +int Class::f1(const S1&)
   1.162 +{return 1;}
   1.163 +
   1.164 +void Class::vf0()
   1.165 +{}
   1.166 +
   1.167 +void Class::vf1(const S1&)
   1.168 +{}
   1.169 +
   1.170 +int Class::f0c() const
   1.171 +{return 0;}
   1.172 +
   1.173 +int Class::f1c(const S1&) const
   1.174 +{return 1;}
   1.175 +
   1.176 +void Class::vf0c() const
   1.177 +{}
   1.178 +
   1.179 +void Class::vf1c(const S1&) const
   1.180 +{}
   1.181 +
   1.182 +struct V {
   1.183 +  public:
   1.184 +    V(int _v) :
   1.185 +      v(_v)
   1.186 +    { }
   1.187 +
   1.188 +  bool f( int _v ) const { return (v == _v); }
   1.189 +
   1.190 +  int v;
   1.191 +#if defined (__DMC__)
   1.192 +  V(){}
   1.193 +#endif
   1.194 +};
   1.195 +
   1.196 +void MemFunPtrTest::find()
   1.197 +{
   1.198 +#if !defined (STLPORT) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
   1.199 +  vector<V> v;
   1.200 +
   1.201 +  v.push_back( V(1) );
   1.202 +  v.push_back( V(2) );
   1.203 +  v.push_back( V(3) );
   1.204 +
   1.205 +  // step-by-step complication of work for compiler:
   1.206 +
   1.207 +  // step 1:
   1.208 +  const_mem_fun1_ref_t<bool,V,int> pmf = mem_fun_ref( &V::f );
   1.209 +  binder2nd<const_mem_fun1_ref_t<bool,V,int> > b(pmf, 2);
   1.210 +  vector<V>::iterator i = find_if( v.begin(), v.end(), b );
   1.211 +  CPPUNIT_ASSERT(i != v.end());
   1.212 +  CPPUNIT_ASSERT(i->v == 2);
   1.213 +
   1.214 +  // step 2, just check that compiler understand what pass to bind2nd:
   1.215 +  binder2nd<const_mem_fun1_ref_t<bool,V,int> > b2 = bind2nd( pmf, 2 );
   1.216 +
   1.217 +  // step 3, the same as step 1, but more intellect from compiler required:
   1.218 +  binder2nd<const_mem_fun1_ref_t<bool,V,int> > b3 = bind2nd( mem_fun_ref( &V::f ), 2 );
   1.219 +
   1.220 +  vector<V>::iterator j = find_if( v.begin(), v.end(), b3 );
   1.221 +  CPPUNIT_ASSERT(j != v.end());
   1.222 +  CPPUNIT_ASSERT(j->v == 2);
   1.223 +
   1.224 +  // step 4, more brief, more complex:
   1.225 +  vector<V>::iterator k = find_if( v.begin(), v.end(), bind2nd( mem_fun_ref( &V::f ), 2 ) );
   1.226 +  CPPUNIT_ASSERT(k != v.end());
   1.227 +  CPPUNIT_ASSERT(k->v == 2);
   1.228 +#endif
   1.229 +}
   1.230 +
   1.231 +#ifdef _STLP_DONT_TEST_RETURN_VOID
   1.232 +#  undef _STLP_DONT_TEST_RETURN_VOID
   1.233 +#endif