aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-04 15:29:25 +0200
committerSven Gothel <[email protected]>2020-05-04 15:29:25 +0200
commit72f865fe2a1371944d10a1c3fee1fd1a9b54c5cd (patch)
treecfcd508cf83577a53e39b2ed4e21614e7ca2a121 /test
parenteeccdf56d283d4bc9b33f87be32c974db1727051 (diff)
FunctionDef: Enhance efficiency and create InvocationFunc<..>::clone() and FunctionDef<..>::cloneFunction() ...
Enhance efficiency - Equality: Check pointer first - CaptureInvocationFunc function spec uses 'I& data', by reference - CaptureInvocationFunc ctor supports 'I&& data' move-ctor and 'const I& data' copy-ctor w/ later copy (instead relying of in place) - Added FunctionDef ctor for naked InvocationFunc<..> for convenience - Added FunctionDef getFunction(), allowing user to 'see' the InvocationFunc<..> shared instance. +++ FunctionDef: Create InvocationFunc<..>::clone() and FunctionDef<..>::cloneFunction() ... - This allows user to reproduce an identical InvocationFunc<..> polymorph instance (copy-ctor) - Such instance can be used to reproduce an identical FunctionDef later on, here intended for a listener vector equality test for removal. Allowing not to use any intermediate data types.
Diffstat (limited to 'test')
-rw-r--r--test/direct_bt/test_functiondef01.cpp151
1 files changed, 122 insertions, 29 deletions
diff --git a/test/direct_bt/test_functiondef01.cpp b/test/direct_bt/test_functiondef01.cpp
index 2294861..83b20fb 100644
--- a/test/direct_bt/test_functiondef01.cpp
+++ b/test/direct_bt/test_functiondef01.cpp
@@ -36,6 +36,41 @@ class Cppunit_tests : public Cppunit {
struct IntOffset {
int value;
IntOffset(int v) : value(v) {}
+
+ IntOffset(const IntOffset &o)
+ : value(o.value)
+ {
+ PRINTM("IntOffset::copy_ctor");
+ }
+ IntOffset(IntOffset &&o)
+ : value(std::move(o.value))
+ {
+ PRINTM("IntOffset::move_ctor");
+ }
+ IntOffset& operator=(const IntOffset &o) {
+ PRINTM("IntOffset::copy_assign");
+ if( &o == this ) {
+ return *this;
+ }
+ value = o.value;
+ return *this;
+ }
+ IntOffset& operator=(IntOffset &&o) {
+ PRINTM("IntOffset::move_assign");
+ value == std::move(o.value);
+ return *this;
+ }
+
+ bool operator==(const IntOffset& rhs) const {
+ if( &rhs == this ) {
+ return true;
+ }
+ return value == rhs.value;
+ }
+
+ bool operator!=(const IntOffset& rhs) const
+ { return !( *this == rhs ); }
+
};
void test_FunctionPointer00(std::string msg, bool expEqual, int value, int expRes, MyClassFunction & f1, MyClassFunction &f2) {
@@ -66,6 +101,7 @@ class Cppunit_tests : public Cppunit {
void single_test() override {
{
+ PRINTM("FuncPtr2_member: bindMemberFunc<int, Cppunit_tests, int>: START");
// FunctionDef(Cppunit_tests &base, Func1Type func)
MyClassFunction f2a_1 = bindMemberFunc<int, Cppunit_tests, int>(this, &Cppunit_tests::func2a_member);
MyClassFunction f2a_2 = bindMemberFunc(this, &Cppunit_tests::func2a_member);
@@ -79,23 +115,27 @@ class Cppunit_tests : public Cppunit {
test_FunctionPointer00("FuncPtr2ab_member_11", false, 1, 0, f2a_1, f2b_1);
test_FunctionPointer00("FuncPtr2ab_member_22", false, 1, 0, f2a_2, f2b_2);
+ PRINTM("FuncPtr2_member: bindMemberFunc<int, Cppunit_tests, int>: END");
}
{
+ PRINTM("FuncPtr3_plain: bindPlainFunc<int, int>: START");
// FunctionDef(Func1Type func)
MyClassFunction f3a_1 = bindPlainFunc<int, int>(&Cppunit_tests::Func3a_static);
MyClassFunction f3a_2 = bindPlainFunc(&Cppunit_tests::Func3a_static);
- test_FunctionPointer00("FuncPtr3a_static_11", true, 1, 101, f3a_1, f3a_1);
- test_FunctionPointer00("FuncPtr3a_static_12", true, 1, 101, f3a_1, f3a_2);
+ test_FunctionPointer00("FuncPtr3a_plain_11", true, 1, 101, f3a_1, f3a_1);
+ test_FunctionPointer00("FuncPtr3a_plain_12", true, 1, 101, f3a_1, f3a_2);
MyClassFunction f3b_1 = bindPlainFunc(&Cppunit_tests::Func3b_static);
MyClassFunction f3b_2 = bindPlainFunc(&Func3b_static);
- test_FunctionPointer00("FuncPtr3b_static_11", true, 1, 1001, f3b_1, f3b_1);
- test_FunctionPointer00("FuncPtr3b_static_12", true, 1, 1001, f3b_1, f3b_2);
+ test_FunctionPointer00("FuncPtr3b_plain_11", true, 1, 1001, f3b_1, f3b_1);
+ test_FunctionPointer00("FuncPtr3b_plain_12", true, 1, 1001, f3b_1, f3b_2);
- test_FunctionPointer00("FuncPtr3ab_static_11", false, 1, 0, f3a_1, f3b_1);
- test_FunctionPointer00("FuncPtr3ab_static_22", false, 1, 0, f3a_2, f3b_2);
+ test_FunctionPointer00("FuncPtr3ab_plain_11", false, 1, 0, f3a_1, f3b_1);
+ test_FunctionPointer00("FuncPtr3ab_plain_22", false, 1, 0, f3a_2, f3b_2);
+ PRINTM("FuncPtr3_plain: bindPlainFunc<int, int>: END");
}
{
+ PRINTM("FuncPtr4_stdlambda: bindStdFunc<int, int>: START");
// FunctionDef(Func1Type func) <int, int>
std::function<int(int i)> func4a_stdlambda = [](int i)->int {
int res = i+100;
@@ -127,18 +167,20 @@ class Cppunit_tests : public Cppunit {
test_FunctionPointer01("FuncPtr4ab_stdlambda_00", false, f4a_0, f4b_0);
test_FunctionPointer01("FuncPtr4ab_stdlambda_01", false, f4a_0, f4b_1);
test_FunctionPointer01("FuncPtr4ab_stdlambda_10", false, f4a_1, f4b_0);
+ PRINTM("FuncPtr4_stdlambda: bindStdFunc<int, int>: END");
}
{
+ PRINTM("FuncPtr5_capture: bindCaptureFunc<int, int, int>: START");
// bindCaptureFunc(I& data, R(*func)(I&, A...))
// FunctionDef(Func1Type func) <int, int>
int offset100 = 100;
int offset1000 = 1000;
- int(*func5a_capture)(int, int) = [](int offset, int i)->int {
+ int(*func5a_capture)(int&, int) = [](int& offset, int i)->int {
int res = i+10000+offset;
return res;
};
- int(*func5b_capture)(int, int) = [](int offset, int i)->int {
+ int(*func5b_capture)(int&, int) = [](int& offset, int i)->int {
int res = i+100000+offset;
return res;
};
@@ -169,48 +211,99 @@ class Cppunit_tests : public Cppunit {
test_FunctionPointer00("FuncPtr5ab_o100_capture_11", false, 1, 0, f5a_o100_1, f5b_o100_1);
test_FunctionPointer00("FuncPtr5ab_o100_capture_22", false, 1, 0, f5a_o100_2, f5b_o100_2);
+ PRINTM("FuncPtr5_capture: bindCaptureFunc<int, int, int>: END");
}
{
+ PRINTM("FuncPtr6_capture: bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>: START");
// bindCaptureFunc(I& data, R(*func)(I&, A...))
// FunctionDef(Func1Type func) <int, int>
std::shared_ptr<IntOffset> offset100(new IntOffset(100));
std::shared_ptr<IntOffset> offset1000(new IntOffset(1000));
- int(*func5a_capture)(std::shared_ptr<IntOffset>, int) = [](std::shared_ptr<IntOffset> sharedOffset, int i)->int {
+ int(*func6a_capture)(std::shared_ptr<IntOffset>&, int) = [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int {
int res = i+10000+sharedOffset->value;
return res;
};
- int(*func5b_capture)(std::shared_ptr<IntOffset>, int) = [](std::shared_ptr<IntOffset> sharedOffset, int i)->int {
+ int(*func6b_capture)(std::shared_ptr<IntOffset>&, int) = [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int {
int res = i+100000+sharedOffset->value;
return res;
};
#if 0
- MyClassFunction f5a_o100_0 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100,
- [](std::shared_ptr<IntOffset> sharedOffset, int i)->int {
+ MyClassFunction f6a_o100_0 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100,
+ [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int {
int res = i+10000+sharedOffset->value;
return res;;
} );
- test_FunctionPointer01("FuncPtr5a_o100_capture_00", true, f5a_o100_0, f5a_o100_0);
+ test_FunctionPointer01("FuncPtr6a_o100_capture_00", true, f6a_o100_0, f6a_o100_0);
#endif
- MyClassFunction f5a_o100_1 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100, func5a_capture);
- MyClassFunction f5a_o100_2 = bindCaptureFunc(offset100, func5a_capture);
- test_FunctionPointer01("FuncPtr5a_o100_capture_12", true, f5a_o100_1, f5a_o100_2);
- test_FunctionPointer00("FuncPtr5a_o100_capture_11", true, 1, 10101, f5a_o100_1, f5a_o100_1);
- test_FunctionPointer00("FuncPtr5a_o100_capture_12", true, 1, 10101, f5a_o100_1, f5a_o100_2);
- // test_FunctionPointer01("FuncPtr5a_o100_capture_01", false, f5a_o100_0, f5a_o100_1);
- MyClassFunction f5a_o1000_1 = bindCaptureFunc(offset1000, func5a_capture);
- MyClassFunction f5a_o1000_2 = bindCaptureFunc(offset1000, func5a_capture);
- test_FunctionPointer01("FuncPtr5a_o1000_capture_12", true, f5a_o1000_1, f5a_o1000_2);
- test_FunctionPointer01("FuncPtr5a_o100_o1000_capture_11", false, f5a_o100_1, f5a_o1000_1);
+ MyClassFunction f6a_o100_1 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100, func6a_capture);
+ MyClassFunction f6a_o100_2 = bindCaptureFunc(offset100, func6a_capture);
+ test_FunctionPointer01("FuncPtr6a_o100_capture_12", true, f6a_o100_1, f6a_o100_2);
+ test_FunctionPointer00("FuncPtr6a_o100_capture_11", true, 1, 10101, f6a_o100_1, f6a_o100_1);
+ test_FunctionPointer00("FuncPtr6a_o100_capture_12", true, 1, 10101, f6a_o100_1, f6a_o100_2);
+ // test_FunctionPointer01("FuncPtr6a_o100_capture_01", false, f6a_o100_0, f6a_o100_1);
+ MyClassFunction f6a_o1000_1 = bindCaptureFunc(offset1000, func6a_capture);
+ MyClassFunction f6a_o1000_2 = bindCaptureFunc(offset1000, func6a_capture);
+ test_FunctionPointer01("FuncPtr6a_o1000_capture_12", true, f6a_o1000_1, f6a_o1000_2);
+ test_FunctionPointer01("FuncPtr6a_o100_o1000_capture_11", false, f6a_o100_1, f6a_o1000_1);
- MyClassFunction f5b_o100_1 = bindCaptureFunc(offset100, func5b_capture);
- MyClassFunction f5b_o100_2 = bindCaptureFunc(offset100, func5b_capture);
- test_FunctionPointer00("FuncPtr5b_o100_capture_11", true, 1, 100101, f5b_o100_1, f5b_o100_1);
- test_FunctionPointer00("FuncPtr5b_o100_capture_12", true, 1, 100101, f5b_o100_1, f5b_o100_2);
+ MyClassFunction f6b_o100_1 = bindCaptureFunc(offset100, func6b_capture);
+ MyClassFunction f6b_o100_2 = bindCaptureFunc(offset100, func6b_capture);
+ test_FunctionPointer00("FuncPtr6b_o100_capture_11", true, 1, 100101, f6b_o100_1, f6b_o100_1);
+ test_FunctionPointer00("FuncPtr6b_o100_capture_12", true, 1, 100101, f6b_o100_1, f6b_o100_2);
- test_FunctionPointer00("FuncPtr5ab_o100_capture_11", false, 1, 0, f5a_o100_1, f5b_o100_1);
- test_FunctionPointer00("FuncPtr5ab_o100_capture_22", false, 1, 0, f5a_o100_2, f5b_o100_2);
+ test_FunctionPointer00("FuncPtr6ab_o100_capture_11", false, 1, 0, f6a_o100_1, f6b_o100_1);
+ test_FunctionPointer00("FuncPtr6ab_o100_capture_22", false, 1, 0, f6a_o100_2, f6b_o100_2);
+ PRINTM("FuncPtr6_capture: bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>: END");
+ }
+ {
+ PRINTM("FuncPtr7_capture: bindCaptureFunc<int, IntOffset, int>: START");
+ // bindCaptureFunc(I& data, R(*func)(I&, A...))
+ // FunctionDef(Func1Type func) <int, int>
+ IntOffset offset100(100);
+ IntOffset offset1000(1000);
+
+ int(*func7a_capture)(IntOffset&, int) = [](IntOffset& sharedOffset, int i)->int {
+ int res = i+10000+sharedOffset.value;
+ return res;
+ };
+ int(*func7b_capture)(IntOffset&, int) = [](IntOffset& sharedOffset, int i)->int {
+ int res = i+100000+sharedOffset.value;
+ return res;
+ };
+
+#if 0
+ MyClassFunction f7a_o100_0 = bindCaptureFunc<int, IntOffset, int>(offset100,
+ [](IntOffset& sharedOffset, int i)->int {
+ int res = i+10000+sharedOffset.value;
+ return res;;
+ } );
+ test_FunctionPointer01("FuncPtr7a_o100_capture_00", true, f7a_o100_0, f7a_o100_0);
+#endif
+ PRINTM("f7a_o100_1 copy_ctor");
+ MyClassFunction f7a_o100_1 = bindCaptureFunc<int, IntOffset, int>(offset100, func7a_capture);
+ PRINTM("f7a_o100_1 copy_ctor done");
+ PRINTM("f7a_o100_2 move_ctor");
+ MyClassFunction f7a_o100_2 = bindCaptureFunc(IntOffset(100), func7a_capture);
+ PRINTM("f7a_o100_2 move_ctor done");
+ test_FunctionPointer01("FuncPtr7a_o100_capture_12", true, f7a_o100_1, f7a_o100_2);
+ test_FunctionPointer00("FuncPtr7a_o100_capture_11", true, 1, 10101, f7a_o100_1, f7a_o100_1);
+ test_FunctionPointer00("FuncPtr7a_o100_capture_12", true, 1, 10101, f7a_o100_1, f7a_o100_2);
+ // test_FunctionPointer01("FuncPtr7a_o100_capture_01", false, f7a_o100_0, f7a_o100_1);
+ MyClassFunction f7a_o1000_1 = bindCaptureFunc(offset1000, func7a_capture);
+ MyClassFunction f7a_o1000_2 = bindCaptureFunc(offset1000, func7a_capture);
+ test_FunctionPointer01("FuncPtr7a_o1000_capture_12", true, f7a_o1000_1, f7a_o1000_2);
+ test_FunctionPointer01("FuncPtr7a_o100_o1000_capture_11", false, f7a_o100_1, f7a_o1000_1);
+
+ MyClassFunction f7b_o100_1 = bindCaptureFunc(offset100, func7b_capture);
+ MyClassFunction f7b_o100_2 = bindCaptureFunc(offset100, func7b_capture);
+ test_FunctionPointer00("FuncPtr7b_o100_capture_11", true, 1, 100101, f7b_o100_1, f7b_o100_1);
+ test_FunctionPointer00("FuncPtr7b_o100_capture_12", true, 1, 100101, f7b_o100_1, f7b_o100_2);
+
+ test_FunctionPointer00("FuncPtr7ab_o100_capture_11", false, 1, 0, f7a_o100_1, f7b_o100_1);
+ test_FunctionPointer00("FuncPtr7ab_o100_capture_22", false, 1, 0, f7a_o100_2, f7b_o100_2);
+ PRINTM("FuncPtr7_capture: bindCaptureFunc<int, IntOffset, int>: END");
}
}
};