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