2005-09-24 15:25:20 +00:00
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Unit Test for Loki
|
|
|
|
|
//
|
|
|
|
|
// Copyright Terje Sletteb<65> and Pavel Vozenilek 2002.
|
2006-06-01 12:33:05 +00:00
|
|
|
|
// Copyright Peter K<>mmel, 2006
|
|
|
|
|
|
2005-09-24 15:25:20 +00:00
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
|
// purpose is hereby granted without fee, provided that this copyright and
|
|
|
|
|
// permissions notice appear in all copies and derivatives.
|
|
|
|
|
//
|
|
|
|
|
// This software is provided "as is" without express or implied warranty.
|
|
|
|
|
//
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-06-01 12:33:05 +00:00
|
|
|
|
// $Header:
|
|
|
|
|
|
2005-09-24 15:25:20 +00:00
|
|
|
|
#ifndef FUNCTORTEST_H
|
|
|
|
|
#define FUNCTORTEST_H
|
|
|
|
|
|
|
|
|
|
#include <loki/Functor.h>
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FunctorTest
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2006-06-01 12:33:05 +00:00
|
|
|
|
void free_function(bool &result)
|
|
|
|
|
{
|
2006-06-01 12:35:08 +00:00
|
|
|
|
result=true;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-09-24 15:25:20 +00:00
|
|
|
|
class FunctorTest : public Test
|
|
|
|
|
{
|
|
|
|
|
public:
|
2006-06-01 12:35:08 +00:00
|
|
|
|
FunctorTest() : Test("Functor.h")
|
|
|
|
|
{}
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
virtual void execute(TestResult &result)
|
2005-09-24 15:25:20 +00:00
|
|
|
|
{
|
2006-06-01 12:35:08 +00:00
|
|
|
|
printName(result);
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
using namespace Loki;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
bool r;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
TestFunctor testFunctor;
|
|
|
|
|
TestClass testClass;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2005-10-30 13:49:44 +00:00
|
|
|
|
|
|
|
|
|
#ifndef LOKI_DISABLE_TYPELIST_MACROS
|
2006-06-01 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> function(testFunction);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> function2(testFunction);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> functor(testFunctor);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> functor2(testFunctor);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> classFunctor(&testClass,&TestClass::member);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> classFunctor2(&testClass,&TestClass::member);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> functorCopy(function);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> functorCopy2(function);
|
|
|
|
|
|
|
|
|
|
Functor<void,NullType> bindFunctor(BindFirst(function,testResult));
|
|
|
|
|
Functor<void,NullType> bindFunctor2(BindFirst(function,testResult));
|
|
|
|
|
|
|
|
|
|
Functor<void> chainFunctor(Chain(bindFunctor,bindFunctor));
|
|
|
|
|
Functor<void> chainFunctor2(Chain(bindFunctor,bindFunctor));
|
|
|
|
|
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> member_func(&testClass,&TestClass::member);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> free_func(&free_function);
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> NULL_func;
|
|
|
|
|
Functor<void,LOKI_TYPELIST_1(bool &)> NULL_func0;
|
2005-10-30 13:49:44 +00:00
|
|
|
|
#else
|
2006-06-01 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
Functor<void,Seq<bool &> > function(testFunction);
|
|
|
|
|
Functor<void,Seq<bool &> > function2(testFunction);
|
|
|
|
|
Functor<void,Seq<bool &> > functor(testFunctor);
|
|
|
|
|
Functor<void,Seq<bool &> > functor2(testFunctor);
|
|
|
|
|
Functor<void,Seq<bool &> > classFunctor(&testClass,&TestClass::member);
|
|
|
|
|
Functor<void,Seq<bool &> > classFunctor2(&testClass,&TestClass::member);
|
|
|
|
|
Functor<void,Seq<bool &> > functorCopy(function);
|
|
|
|
|
Functor<void,Seq<bool &> > functorCopy2(function);
|
2006-06-01 13:08:14 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
//TODO:
|
2006-06-01 13:08:14 +00:00
|
|
|
|
// BindFirst and Chainer
|
|
|
|
|
|
2006-06-19 12:46:22 +00:00
|
|
|
|
Functor<void,Seq<bool &> > member_func(&testClass,&TestClass::member);
|
2006-06-01 13:08:14 +00:00
|
|
|
|
Functor<void,Seq<bool &> > free_func(&free_function);
|
|
|
|
|
Functor<void,Seq<bool &> > NULL_func;
|
|
|
|
|
Functor<void,Seq<bool &> > NULL_func0;
|
2005-10-30 13:49:44 +00:00
|
|
|
|
#endif
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
testResult=false;
|
|
|
|
|
function(testResult);
|
|
|
|
|
bool functionResult=testResult;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
testResult=false;
|
|
|
|
|
functor(testResult);
|
|
|
|
|
bool functorResult=testResult;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
testResult=false;
|
|
|
|
|
classFunctor(testResult);
|
|
|
|
|
bool classFunctorResult=testResult;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
testResult=false;
|
|
|
|
|
functorCopy(testResult);
|
|
|
|
|
bool functorCopyResult=testResult;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
#ifdef LOKI_FUNCTORS_ARE_COMPARABLE
|
2006-06-01 12:33:05 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
bool functionCompare = function==function2;
|
|
|
|
|
bool functorCompare = functor!=functor2; // is this a bug?
|
|
|
|
|
bool classFunctorCompare = classFunctor==classFunctor2;
|
|
|
|
|
bool functorCopyCompare = functorCopy==functorCopy2;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
bool free_mem = free_func!=member_func;
|
|
|
|
|
bool mem_free = member_func!=free_func;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
bool null0 = NULL_func == NULL_func0;
|
|
|
|
|
bool null1 = NULL_func != free_func;
|
|
|
|
|
bool null2 = NULL_func != member_func;
|
|
|
|
|
bool null3 = free_func != NULL_func;
|
|
|
|
|
bool null4 = member_func != NULL_func;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
|
|
|
|
|
|
2006-06-01 13:08:14 +00:00
|
|
|
|
#ifndef LOKI_DISABLE_TYPELIST_MACROS
|
2006-06-01 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
bool bindFunctorCompare = bindFunctor==bindFunctor2;
|
|
|
|
|
bool chainFunctorCompare = chainFunctor==chainFunctor2;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
bool compare = functionCompare &&
|
|
|
|
|
functorCompare &&
|
|
|
|
|
classFunctorCompare &&
|
|
|
|
|
functorCopyCompare &&
|
|
|
|
|
mem_free &&
|
|
|
|
|
free_mem &&
|
|
|
|
|
null0 &&
|
|
|
|
|
null1 &&
|
|
|
|
|
null2 &&
|
|
|
|
|
null3 &&
|
|
|
|
|
null4
|
2005-10-30 13:49:44 +00:00
|
|
|
|
#ifndef LOKI_DISABLE_TYPELIST_MACROS
|
2006-06-01 12:35:08 +00:00
|
|
|
|
&& bindFunctorCompare
|
|
|
|
|
&& chainFunctorCompare;
|
2006-06-01 13:08:14 +00:00
|
|
|
|
#else
|
2006-06-19 12:46:22 +00:00
|
|
|
|
;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#else
|
2006-06-01 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
bool compare=true;
|
2006-06-01 12:33:05 +00:00
|
|
|
|
#endif //LOKI_FUNCTORS_ARE_COMPARABLE
|
|
|
|
|
|
|
|
|
|
|
2006-06-01 13:08:14 +00:00
|
|
|
|
#ifndef LOKI_DISABLE_TYPELIST_MACROS
|
2006-06-01 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
testResult=false;
|
|
|
|
|
bindFunctor();
|
|
|
|
|
bool bindFunctorResult=testResult;
|
|
|
|
|
|
|
|
|
|
testResult=false;
|
|
|
|
|
chainFunctor();
|
|
|
|
|
bool chainFunctorResult=testResult;
|
|
|
|
|
|
|
|
|
|
r=functionResult && functorResult && classFunctorResult && functorCopyResult && bindFunctorResult &&
|
|
|
|
|
chainFunctorResult && compare;
|
|
|
|
|
#else
|
|
|
|
|
//TODO!
|
|
|
|
|
r=functionResult && functorResult && classFunctorResult && functorCopyResult && compare;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
testAssert("Functor",r,result);
|
|
|
|
|
|
|
|
|
|
std::cout << '\n';
|
2005-09-24 15:25:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2006-06-01 12:35:08 +00:00
|
|
|
|
static bool testResult;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
static void testFunction(bool &result)
|
2005-09-24 15:25:20 +00:00
|
|
|
|
{
|
2006-06-01 12:35:08 +00:00
|
|
|
|
result=true;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-06-01 12:35:08 +00:00
|
|
|
|
class TestFunctor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void operator()(bool &result)
|
|
|
|
|
{
|
|
|
|
|
result=true;
|
|
|
|
|
}
|
|
|
|
|
bool operator==(const TestFunctor& rhs) const
|
|
|
|
|
{
|
|
|
|
|
const TestFunctor* p = &rhs;
|
|
|
|
|
return this==p;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TestClass
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void member(bool &result)
|
|
|
|
|
{
|
|
|
|
|
result=true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
functorTest;
|
2005-09-24 15:25:20 +00:00
|
|
|
|
|
|
|
|
|
bool FunctorTest::testResult;
|
|
|
|
|
|
|
|
|
|
#ifndef SMALLOBJ_CPP
|
|
|
|
|
# define SMALLOBJ_CPP
|
2006-06-01 12:33:05 +00:00
|
|
|
|
# include "../../src/SmallObj.cpp"
|
2005-09-24 15:25:20 +00:00
|
|
|
|
#endif
|
|
|
|
|
#endif
|