#include <functional>
#include <memory>
#include <vector>
#include <algorithm>
#include "cppunit/cppunit_proxy.h"
#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
using namespace std;
#endif
//
// TestCase class
//
class MemFunPtrTest : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(MemFunPtrTest);
CPPUNIT_TEST(mem_ptr_fun);
#if defined (STLPORT) && !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
//This test require partial template specialization feature to avoid the
//reference to reference problem. No workaround yet for limited compilers.
CPPUNIT_IGNORE;
#endif
CPPUNIT_TEST(find);
CPPUNIT_TEST_SUITE_END();
protected:
// compile test not neccessary to run but...
void mem_ptr_fun();
void find();
};
CPPUNIT_TEST_SUITE_REGISTRATION(MemFunPtrTest);
#if defined(_STLP_DONT_RETURN_VOID) && (defined(_STLP_NO_MEMBER_TEMPLATE_CLASSES) && defined(_STLP_NO_CLASS_PARTIAL_SPECIALIZATION))
# define _STLP_DONT_TEST_RETURN_VOID
#endif
//else there is no workaround for the return void bug
struct S1 { } s1;
struct S2 { } s2;
int f1(S1&);
int f2(S1&, S2&);
int f1c(const S1&);
int f2c(const S1&, const S2&);
void vf1(S1&);
void vf2(S1&, S2&);
void vf1c(const S1&);
void vf2c(const S1&, const S2&);
class Class {
public:
int f0();
int f1(const S1&);
void vf0();
void vf1(const S1&);
int f0c() const;
int f1c(const S1&) const;
void vf0c() const;
void vf1c(const S1&) const;
};
//
// tests implementation
//
void MemFunPtrTest::mem_ptr_fun()
{
Class obj;
const Class& objc = obj;
// ptr_fun
ptr_fun(f1)(s1);
ptr_fun(f2)(s1, s2);
ptr_fun(f1c)(s1);
ptr_fun(f2c)(s1, s2);
#ifndef _STLP_DONT_TEST_RETURN_VOID
ptr_fun(vf1)(s1);
ptr_fun(vf2)(s1, s2);
ptr_fun(vf1c)(s1);
ptr_fun(vf2c)(s1, s2);
#endif /* _STLP_DONT_TEST_RETURN_VOID */
// mem_fun
mem_fun(&Class::f0)(&obj);
mem_fun(&Class::f1)(&obj, s1);
#ifndef _STLP_DONT_TEST_RETURN_VOID
mem_fun(&Class::vf0)(&obj);
mem_fun(&Class::vf1)(&obj, s1);
#endif /* _STLP_DONT_TEST_RETURN_VOID */
// mem_fun (const)
mem_fun(&Class::f0c)(&objc);
mem_fun(&Class::f1c)(&objc, s1);
#ifndef _STLP_DONT_TEST_RETURN_VOID
mem_fun(&Class::vf0c)(&objc);
mem_fun(&Class::vf1c)(&objc, s1);
#endif /* _STLP_DONT_TEST_RETURN_VOID */
// mem_fun_ref
mem_fun_ref(&Class::f0)(obj);
mem_fun_ref(&Class::f1)(obj, s1);
#ifndef _STLP_DONT_TEST_RETURN_VOID
mem_fun_ref(&Class::vf0)(obj);
mem_fun_ref(&Class::vf1)(obj, s1);
#endif /* _STLP_DONT_TEST_RETURN_VOID */
// mem_fun_ref (const)
mem_fun_ref(&Class::f0c)(objc);
mem_fun_ref(&Class::f1c)(objc, s1);
#ifndef _STLP_DONT_TEST_RETURN_VOID
mem_fun_ref(&Class::vf0c)(objc);
mem_fun_ref(&Class::vf1c)(objc, s1);
#endif /* _STLP_DONT_TEST_RETURN_VOID */
}
int f1(S1&)
{return 1;}
int f2(S1&, S2&)
{return 2;}
int f1c(const S1&)
{return 1;}
int f2c(const S1&, const S2&)
{return 2;}
void vf1(S1&)
{}
void vf2(S1&, S2&)
{}
void vf1c(const S1&)
{}
void vf2c(const S1&, const S2&)
{}
int Class::f0()
{return 0;}
int Class::f1(const S1&)
{return 1;}
void Class::vf0()
{}
void Class::vf1(const S1&)
{}
int Class::f0c() const
{return 0;}
int Class::f1c(const S1&) const
{return 1;}
void Class::vf0c() const
{}
void Class::vf1c(const S1&) const
{}
struct V {
public:
V(int _v) :
v(_v)
{ }
bool f( int _v ) const { return (v == _v); }
int v;
#if defined (__DMC__)
V(){}
#endif
};
void MemFunPtrTest::find()
{
#if !defined (STLPORT) || defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
vector<V> v;
v.push_back( V(1) );
v.push_back( V(2) );
v.push_back( V(3) );
// step-by-step complication of work for compiler:
// step 1:
const_mem_fun1_ref_t<bool,V,int> pmf = mem_fun_ref( &V::f );
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b(pmf, 2);
vector<V>::iterator i = find_if( v.begin(), v.end(), b );
CPPUNIT_ASSERT(i != v.end());
CPPUNIT_ASSERT(i->v == 2);
// step 2, just check that compiler understand what pass to bind2nd:
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b2 = bind2nd( pmf, 2 );
// step 3, the same as step 1, but more intellect from compiler required:
binder2nd<const_mem_fun1_ref_t<bool,V,int> > b3 = bind2nd( mem_fun_ref( &V::f ), 2 );
vector<V>::iterator j = find_if( v.begin(), v.end(), b3 );
CPPUNIT_ASSERT(j != v.end());
CPPUNIT_ASSERT(j->v == 2);
// step 4, more brief, more complex:
vector<V>::iterator k = find_if( v.begin(), v.end(), bind2nd( mem_fun_ref( &V::f ), 2 ) );
CPPUNIT_ASSERT(k != v.end());
CPPUNIT_ASSERT(k->v == 2);
#endif
}
#ifdef _STLP_DONT_TEST_RETURN_VOID
# undef _STLP_DONT_TEST_RETURN_VOID
#endif
没有合适的资源?快使用搜索试试~ 我知道了~
mfunptr_test.rar_The Test
共1个文件
c:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 119 浏览量
2022-09-24
20:48:11
上传
评论
收藏 1KB RAR 举报
温馨提示
This test require partial template specialization feature to avoid the reference to reference problem. No workaround yet for limited compilers.
资源推荐
资源详情
资源评论
收起资源包目录
mfunptr_test.rar (1个子文件)
mfunptr_test.c 5KB
共 1 条
- 1
资源评论
小贝德罗
- 粉丝: 86
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功