aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_functiondef01.cpp
blob: f22efe3f4dd27c8dafa89757c54878b8d1c1a1e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <iostream>
#include <cassert>
#include <cinttypes>
#include <cstring>

#define CATCH_CONFIG_MAIN
#include <catch2/catch_amalgamated.hpp>

#include <jau/function_def.hpp>

using namespace jau;

// Test examples.
class TestFunctionDef01 {
  private:

    int func2a_member(int i) {
        int res = i+100;
        return res;;
    }
    int func2b_member(int i) {
        int res = i+1000;
        return res;;
    }
    static int Func3a_static(int i) {
        int res = i+100;
        return res;;
    }
    static int Func3b_static(int i) {
        int res = i+1000;
        return res;;
    }

    // template<typename R, typename... A>
    typedef FunctionDef<int, int> MyClassFunction;

    struct IntOffset {
        int value;
        IntOffset(int v) : value(v) {}

        IntOffset(const IntOffset &o)
        : value(o.value)
        {
            INFO("IntOffset::copy_ctor");
        }
        IntOffset(IntOffset &&o)
        : value(std::move(o.value))
        {
            INFO("IntOffset::move_ctor");
        }
        IntOffset& operator=(const IntOffset &o) {
            INFO("IntOffset::copy_assign");
            if( &o == this ) {
                return *this;
            }
            value = o.value;
            return *this;
        }
        IntOffset& operator=(IntOffset &&o) {
            INFO("IntOffset::move_assign");
            value = std::move(o.value);
            (void)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) {
        // test std::function identity
        INFO(msg+": FunctionPointer00 Fun f1p == f2p : " + std::to_string( f1 == f2 ) + ", f1p: " + f1.toString() + ", f2 "+f2.toString() );
        int f1r = f1.invoke(value);
        int f2r = f2.invoke(value);
        INFO(msg+": FunctionPointer00 Res f1r == f2r : " + std::to_string( f1r == f2r ) + ", f1r: " + std::to_string( f1r ) + ", f2r "+std::to_string( f2r ) );
        if( expEqual ) {
            REQUIRE(f1r == expRes);
            REQUIRE(f2r == expRes);
            REQUIRE(f1 == f2);
        } else {
            REQUIRE(f1 != f2);
        }
    }
    void test_FunctionPointer01(std::string msg, bool expEqual, MyClassFunction & f1, MyClassFunction &f2) {
        // test std::function identity
        INFO(msg+": FunctionPointer00 Fun f1p == f2p : " + std::to_string( f1 == f2 ) + ", f1p: " + f1.toString() + ", f2 "+f2.toString() );
        if( expEqual ) {
            REQUIRE(f1 == f2);
        } else {
            REQUIRE(f1 != f2);
        }
    }

  public:
    void test01_memberfunc_this() {
        INFO("FuncPtr2_member: bindMemberFunc<int, TestFunctionDef01, int>: START");
        // FunctionDef(TestFunctionDef01 &base, Func1Type func)
        MyClassFunction f2a_1 = bindMemberFunc<int, TestFunctionDef01, int>(this, &TestFunctionDef01::func2a_member);
        MyClassFunction f2a_2 = bindMemberFunc(this, &TestFunctionDef01::func2a_member);
        test_FunctionPointer00("FuncPtr2a_member_11", true, 1, 101, f2a_1, f2a_1);
        test_FunctionPointer00("FuncPtr2a_member_12", true, 1, 101, f2a_1, f2a_2);

        MyClassFunction f2b_1 = bindMemberFunc(this, &TestFunctionDef01::func2b_member);
        MyClassFunction f2b_2 = bindMemberFunc(this, &TestFunctionDef01::func2b_member);
        test_FunctionPointer00("FuncPtr2b_member_11", true, 1, 1001, f2b_1, f2b_1);
        test_FunctionPointer00("FuncPtr2b_member_12", true, 1, 1001, f2b_1, f2b_2);

        test_FunctionPointer00("FuncPtr2ab_member_11", false, 1, 0, f2a_1, f2b_1);
        test_FunctionPointer00("FuncPtr2ab_member_22", false, 1, 0, f2a_2, f2b_2);
        INFO("FuncPtr2_member: bindMemberFunc<int, TestFunctionDef01, int>: END");
    }

    void test02_plainfunc_static() {
        INFO("FuncPtr3_plain: bindPlainFunc<int, int>: START");
        // FunctionDef(Func1Type func)
        MyClassFunction f3a_1 = bindPlainFunc<int, int>(&TestFunctionDef01::Func3a_static);
        MyClassFunction f3a_2 = bindPlainFunc(&TestFunctionDef01::Func3a_static);
        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(&TestFunctionDef01::Func3b_static);
        MyClassFunction f3b_2 = bindPlainFunc(&Func3b_static);
        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_plain_11", false, 1, 0, f3a_1, f3b_1);
        test_FunctionPointer00("FuncPtr3ab_plain_22", false, 1, 0, f3a_2, f3b_2);
        INFO("FuncPtr3_plain: bindPlainFunc<int, int>: END");
    }

    void test03_stdfunc_lambda() {
        INFO("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;
            return res;;
        };
        std::function<int(int i)> func4b_stdlambda = [](int i)->int {
            int res = i+1000;
            return res;;
        };
        MyClassFunction f4a_1 = bindStdFunc<int, int>(100, func4a_stdlambda);
        MyClassFunction f4a_2 = bindStdFunc(100, func4a_stdlambda);
        test_FunctionPointer00("FuncPtr4a_stdlambda_11", true, 1, 101, f4a_1, f4a_1);
        test_FunctionPointer00("FuncPtr4a_stdlambda_12", true, 1, 101, f4a_1, f4a_2);

        MyClassFunction f4b_1 = bindStdFunc(200, func4b_stdlambda);
        MyClassFunction f4b_2 = bindStdFunc(200, func4b_stdlambda);
        test_FunctionPointer00("FuncPtr4b_stdlambda_11", true, 1, 1001, f4b_1, f4b_1);
        test_FunctionPointer00("FuncPtr4b_stdlambda_12", true, 1, 1001, f4b_1, f4b_2);

        test_FunctionPointer00("FuncPtr4ab_stdlambda_11", false, 1, 0, f4a_1, f4b_1);
        test_FunctionPointer00("FuncPtr4ab_stdlambda_22", false, 1, 0, f4a_2, f4b_2);

        MyClassFunction f4a_0 = bindStdFunc<int, int>(100);
        MyClassFunction f4b_0 = bindStdFunc<int, int>(200);
        test_FunctionPointer01("FuncPtr4a_stdlambda_01", true, f4a_0, f4a_1);
        test_FunctionPointer01("FuncPtr4a_stdlambda_02", true, f4a_0, f4a_2);
        test_FunctionPointer01("FuncPtr4b_stdlambda_01", true, f4b_0, f4b_1);
        test_FunctionPointer01("FuncPtr4b_stdlambda_02", true, f4b_0, f4b_2);
        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);
        INFO("FuncPtr4_stdlambda: bindStdFunc<int, int>: END");
    }

    void test04_captfunc_lambda() {
        INFO("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 res = i+10000+offset;
            return res;
        };
        int(*func5b_capture)(int&, int) = [](int& offset, int i)->int {
            int res = i+100000+offset;
            return res;
        };

#if 0
        MyClassFunction f5a_o100_0 = bindCaptureFunc<int, int, int>(offset100,
                [](int& offset, int i)->int {
                    int res = i+10000+offset;
                    return res;;
                } );
        test_FunctionPointer01("FuncPtr5a_o100_capture_00", true, f5a_o100_0, f5a_o100_0);
#endif
        MyClassFunction f5a_o100_1 = bindCaptureFunc<int, int, 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 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);

        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);
        INFO("FuncPtr5_capture: bindCaptureFunc<int, int, int>: END");
    }

    void test05_captfunc_lambda() {
        INFO("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(*func6a_capture)(std::shared_ptr<IntOffset>&, int) = [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int {
            int res = i+10000+sharedOffset->value;
            return res;
        };
        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 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("FuncPtr6a_o100_capture_00", true, f6a_o100_0, f6a_o100_0);
#endif
        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 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("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);
        INFO("FuncPtr6_capture: bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>: END");
    }

    void test06_captfunc_lambda() {
        INFO("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
        INFO("f7a_o100_1 copy_ctor");
        MyClassFunction f7a_o100_1 = bindCaptureFunc<int, IntOffset, int>(offset100, func7a_capture);
        INFO("f7a_o100_1 copy_ctor done");
        INFO("f7a_o100_2 move_ctor");
        MyClassFunction f7a_o100_2 = bindCaptureFunc(IntOffset(100), func7a_capture);
        INFO("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);
        INFO("FuncPtr7_capture: bindCaptureFunc<int, IntOffset, int>: END");
    }
};

METHOD_AS_TEST_CASE( TestFunctionDef01::test01_memberfunc_this,     "Test FunctionDef 01 - 01 memberfunc");
METHOD_AS_TEST_CASE( TestFunctionDef01::test02_plainfunc_static,    "Test FunctionDef 01 - 02 plainfunc");
METHOD_AS_TEST_CASE( TestFunctionDef01::test03_stdfunc_lambda,      "Test FunctionDef 01 - 03 stdfunc");
METHOD_AS_TEST_CASE( TestFunctionDef01::test04_captfunc_lambda,     "Test FunctionDef 01 - 04 captfunc");
METHOD_AS_TEST_CASE( TestFunctionDef01::test05_captfunc_lambda,     "Test FunctionDef 01 - 05 captfunc");
METHOD_AS_TEST_CASE( TestFunctionDef01::test06_captfunc_lambda,     "Test FunctionDef 01 - 06 captfunc");