2009-06-20 01:28:47 +02:00
|
|
|
|
/*
|
|
|
|
|
|
FunctionErasure(Test) - verify the wrapping of functor object with type erasure
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file function-erasure-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref FunctionErasure_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-06-20 06:11:09 +02:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2009-06-20 01:28:47 +02:00
|
|
|
|
#include "lib/meta/function-erasure.hpp"
|
2009-06-20 06:11:09 +02:00
|
|
|
|
#include "lib/error.hpp"
|
2009-06-20 04:43:52 +02:00
|
|
|
|
#include "meta/dummy-functions.hpp"
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
|
#include <functional>
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
|
|
|
|
|
using ::test::Test;
|
2009-06-20 06:11:09 +02:00
|
|
|
|
using lumiera::error::LUMIERA_ERROR_ASSERTION;
|
|
|
|
|
|
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace meta {
|
2009-06-20 01:28:47 +02:00
|
|
|
|
namespace test {
|
|
|
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
|
using std::function;
|
|
|
|
|
|
using std::placeholders::_1;
|
|
|
|
|
|
using std::placeholders::_2;
|
|
|
|
|
|
using std::bind;
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
typedef FunErasure<StoreFunction> Efun;
|
|
|
|
|
|
typedef FunErasure<StoreFunPtr> Efp;
|
|
|
|
|
|
typedef FunErasure<StoreUncheckedFunPtr> Evoid;
|
2009-06-20 01:28:47 +02:00
|
|
|
|
|
2009-06-26 05:27:24 +02:00
|
|
|
|
template<class HOL>
|
2009-07-04 00:22:16 +02:00
|
|
|
|
struct BuildEmptyFunctor { typedef long (*Type)(int,char); };
|
2009-06-26 05:27:24 +02:00
|
|
|
|
template<>
|
2009-07-04 00:22:16 +02:00
|
|
|
|
struct BuildEmptyFunctor<Efun> { typedef function<long(int,char)> Type; };
|
2009-06-26 05:27:24 +02:00
|
|
|
|
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/*******************************************************************//**
|
2009-06-20 04:43:52 +02:00
|
|
|
|
* @test Create specifically typed functor objects and then wrap them
|
|
|
|
|
|
* into common holder objects, thereby discarding the specific
|
|
|
|
|
|
* signature type information. Later on, the concrete functor
|
|
|
|
|
|
* can be re-accessed, given the exact and specific type.
|
2009-06-20 06:11:09 +02:00
|
|
|
|
*
|
2009-06-20 04:43:52 +02:00
|
|
|
|
* @see control::FunErasure
|
|
|
|
|
|
* @see command-mutation.hpp real world usage example
|
2009-06-20 01:28:47 +02:00
|
|
|
|
*/
|
|
|
|
|
|
class FunctionErasure_test : public Test
|
|
|
|
|
|
{
|
2009-06-20 04:43:52 +02:00
|
|
|
|
virtual void
|
2025-06-07 23:59:57 +02:00
|
|
|
|
run (Arg)
|
2009-06-20 04:43:52 +02:00
|
|
|
|
{
|
|
|
|
|
|
function<void(int,char)> bindFunc = bind (testFunc,_1,_2);
|
2009-06-20 06:11:09 +02:00
|
|
|
|
function<void(int )> pAplFunc = bind (testFunc,_1,'x');
|
|
|
|
|
|
function<void( char)> membFunc = bind (&FunctionErasure_test::testMemberFunction,this, _1);
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
function<int(void)> getterFunc = &returnIt;
|
|
|
|
|
|
|
|
|
|
|
|
check_FunctorContainer( Efun (testFunc)
|
|
|
|
|
|
, Efun (bindFunc)
|
|
|
|
|
|
, Efun (pAplFunc)
|
2009-06-20 06:11:09 +02:00
|
|
|
|
, Efun (membFunc)
|
2009-06-20 04:43:52 +02:00
|
|
|
|
, Efun (getterFunc)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2009-06-20 06:11:09 +02:00
|
|
|
|
check_FunctPtrHolder(Efp(testFunc),Efp(&testFunc), Efp(returnIt));
|
|
|
|
|
|
check_VoidPtrHolder(Evoid(testFunc),Evoid(&testFunc),Evoid(returnIt));
|
2009-06-24 06:01:14 +02:00
|
|
|
|
|
|
|
|
|
|
detect_unboundFunctor(Efun(testFunc), Efun(getterFunc), Efun(membFunc));
|
|
|
|
|
|
detect_unboundFunctor(Efp(testFunc),Efp(&testFunc), Efp(returnIt));
|
|
|
|
|
|
detect_unboundFunctor(Evoid(testFunc),Evoid(&testFunc),Evoid(returnIt));
|
2009-06-20 04:43:52 +02:00
|
|
|
|
}
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
void
|
2009-06-20 06:11:09 +02:00
|
|
|
|
testMemberFunction (char c) ///< for checking bind-to member function
|
2009-06-20 01:28:47 +02:00
|
|
|
|
{
|
2009-06-20 04:43:52 +02:00
|
|
|
|
return testFunc('a'-'A', c);
|
2009-06-20 01:28:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
void
|
|
|
|
|
|
check_FunctorContainer (Efun f1, Efun f2, Efun f3, Efun f4, Efun f5)
|
2009-06-20 01:28:47 +02:00
|
|
|
|
{
|
2009-06-20 04:43:52 +02:00
|
|
|
|
typedef void (Sig1) (int,char);
|
|
|
|
|
|
typedef void (Sig2) (int);
|
|
|
|
|
|
typedef void (Sig3) (char);
|
|
|
|
|
|
typedef int (Sig4) ();
|
|
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
2018-01-12 03:03:25 +01:00
|
|
|
|
f1.getFun<Sig1>() (-11,'M'); // invoke stored std::function...
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 'M'-11);
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
|
|
|
|
|
f2.getFun<Sig1>() (-22,'M');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 'M'-22);
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
|
|
|
|
|
f3.getFun<Sig2>() (-33);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 'x'-33);
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
|
|
|
|
|
f4.getFun<Sig3>() ('U');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 'u');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( 'u' == f5.getFun<Sig4>() () );
|
|
|
|
|
|
CHECK (INSTANCEOF (function<Sig4>, &f5.getFun<Sig4>()));
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
2010-04-02 21:51:39 +02:00
|
|
|
|
#if false ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
|
2009-06-20 04:43:52 +02:00
|
|
|
|
VERIFY_ERROR (ASSERTION, f1.getFun<Sig2>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f1.getFun<Sig3>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f1.getFun<Sig4>() );
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
VERIFY_ERROR (ASSERTION, f2.getFun<Sig2>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f3.getFun<Sig3>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f2.getFun<Sig4>() );
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
VERIFY_ERROR (ASSERTION, f3.getFun<Sig1>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f3.getFun<Sig3>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f3.getFun<Sig4>() );
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
VERIFY_ERROR (ASSERTION, f4.getFun<Sig1>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f4.getFun<Sig2>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f4.getFun<Sig4>() );
|
2009-06-20 06:11:09 +02:00
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
VERIFY_ERROR (ASSERTION, f5.getFun<Sig1>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f5.getFun<Sig2>() );
|
|
|
|
|
|
VERIFY_ERROR (ASSERTION, f5.getFun<Sig3>() );
|
2010-04-02 21:51:39 +02:00
|
|
|
|
#endif ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
|
2009-06-20 01:28:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-20 04:43:52 +02:00
|
|
|
|
void
|
|
|
|
|
|
check_FunctPtrHolder (Efp f1, Efp f2, Efp f3)
|
2009-06-20 01:28:47 +02:00
|
|
|
|
{
|
2009-06-20 04:43:52 +02:00
|
|
|
|
typedef void (*FP)(int,char);
|
|
|
|
|
|
typedef void (&FR)(int,char);
|
|
|
|
|
|
|
|
|
|
|
|
FP fun1 = &f1.getFun<void(int,char)>();
|
|
|
|
|
|
FP fun2 = &f2.getFun<void(int,char)>();
|
|
|
|
|
|
FR fun2r = f2.getFun<void(int,char)>();
|
|
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
2009-06-20 06:11:09 +02:00
|
|
|
|
(*fun1) (10, 'a'); // invoke retrieved function pointer
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
(*fun2) (20, 'b');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a'+20+'b');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
fun2r (30, 'c');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a'+20+'b'+30+'c');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == (f3.getFun<int(void)>()) () );
|
2009-06-20 04:43:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
check_VoidPtrHolder (Evoid f1, Evoid f2, Evoid f3)
|
|
|
|
|
|
{
|
|
|
|
|
|
typedef void (*FP)(int,char);
|
|
|
|
|
|
typedef void (&FR)(int,char);
|
|
|
|
|
|
|
|
|
|
|
|
FP fun1 = &f1.getFun<void(int,char)>();
|
|
|
|
|
|
FP fun2 = &f2.getFun<void(int,char)>();
|
|
|
|
|
|
FR fun2r = f2.getFun<void(int,char)>();
|
|
|
|
|
|
|
|
|
|
|
|
_sum_ = 0;
|
|
|
|
|
|
(*fun1) (10, 'a');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
(*fun2) (20, 'b');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a'+20+'b');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
fun2r (30, 'c');
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == 10+'a'+20+'b'+30+'c');
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (_sum_ == (f3.getFun<int(void)>()) () );
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
|
|
|
|
|
FP bad_fun = &f3.getFun<void(int,char)>();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ((void*)bad_fun == &returnIt); // got wrong function!
|
2009-06-20 04:43:52 +02:00
|
|
|
|
|
2009-06-20 06:11:09 +02:00
|
|
|
|
//(*bad_fun) (11, 'x'); // The compiler would accept this line!
|
|
|
|
|
|
} // likely to result in heap corruption or SEGV
|
2009-06-24 06:01:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-10-04 16:43:49 +02:00
|
|
|
|
template<class HOL>
|
|
|
|
|
|
void
|
|
|
|
|
|
detect_unboundFunctor (HOL h1, HOL h2, HOL h3)
|
2009-06-24 06:01:14 +02:00
|
|
|
|
{
|
2014-05-09 00:49:15 +02:00
|
|
|
|
// fabricate a suitable, unbound functor to wrap...
|
2025-07-05 20:08:18 +02:00
|
|
|
|
using NoFunc = BuildEmptyFunctor<HOL>::Type;
|
2009-07-06 02:25:19 +02:00
|
|
|
|
NoFunc noFunction = NoFunc();
|
2014-05-09 00:49:15 +02:00
|
|
|
|
|
|
|
|
|
|
// wrap this (actually empty) functor into the holder type
|
2009-07-04 00:22:16 +02:00
|
|
|
|
HOL emptyHolder (noFunction);
|
2014-05-09 00:49:15 +02:00
|
|
|
|
|
|
|
|
|
|
// verify the older detects that the wrapped functor is empty
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!emptyHolder);
|
2009-07-06 02:25:19 +02:00
|
|
|
|
|
2014-05-09 00:49:15 +02:00
|
|
|
|
// cross-verify that non-empty functors are not flagged as empty
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( h1 );
|
|
|
|
|
|
CHECK ( h2 );
|
|
|
|
|
|
CHECK ( h3 );
|
2009-06-24 06:01:14 +02:00
|
|
|
|
}
|
2009-06-26 05:27:24 +02:00
|
|
|
|
|
2009-07-06 02:25:19 +02:00
|
|
|
|
|
2009-06-20 01:28:47 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (FunctionErasure_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-03 02:56:50 +01:00
|
|
|
|
}}} // namespace lib::meta::test
|