~aleteoryx/muditaos

ref: 97a2c8592674d5458a9caafd1d26f4185f9da13d muditaos/module-utils/utility/tests/test-Utility-ConditionalInvoke.cpp -rw-r--r-- 5.6 KiB
97a2c859 — Lefucjusz [MOS-1063] Fix device freeze after onboarding in Spanish 1 year, 9 months ago
                                                                                
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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include <Utility.hpp>

struct SomeStruct
{
    int value;
};

class ExampleClass
{
  public:
    int function1(int x, const SomeStruct &s)
    {
        return x + s.value;
    }

    void function2()
    {}

    int function3(int x, SomeStruct &s)
    {
        return x + s.value;
    }

    static int static_function1(int x, const SomeStruct &s)
    {
        return x + s.value;
    }

    static void static_function2()
    {}

    static int static_function3(int x, SomeStruct &s)
    {
        return x + s.value;
    }
};

int global_function1(int x, const SomeStruct &s)
{
    return x + s.value;
}

void global_function2()
{}

int global_function3(int x, SomeStruct &s)
{
    return x + s.value;
}

TEST_CASE("Successful global function call")
{
    const auto guard = []() { return true; };

    const auto [called1, ret1] = utility::conditionally_invoke(guard, &global_function1, 10, SomeStruct{5});
    REQUIRE(called1 == true);
    REQUIRE(ret1 == 15);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, &global_function1, x, s1);
    REQUIRE(called2 == true);
    REQUIRE(ret2 == 20);

    const auto called3 = utility::conditionally_invoke(guard, &global_function2);
    REQUIRE(called3 == true);

    SomeStruct s2{15};
    const auto [called4, ret4] = utility::conditionally_invoke(guard, &global_function3, 10, std::ref(s2));
    REQUIRE(called4 == true);
    REQUIRE(ret4 == 25);
}

TEST_CASE("Failed global function call")
{
    const auto guard = []() { return false; };

    const auto [called1, ret1] = utility::conditionally_invoke(guard, &global_function1, 10, SomeStruct{5});
    REQUIRE(called1 == false);
    REQUIRE(ret1 == 0);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, &global_function1, x, s1);
    REQUIRE(called2 == false);
    REQUIRE(ret2 == 0);

    const auto called3 = utility::conditionally_invoke(guard, &global_function2);
    REQUIRE(called3 == false);

    SomeStruct s2{15};
    const auto [called4, ret4] = utility::conditionally_invoke(guard, &global_function3, 10, std::ref(s2));
    REQUIRE(called4 == false);
    REQUIRE(ret4 == 0);
}

TEST_CASE("Successful class static function call")
{
    const auto guard = []() { return true; };

    const auto [called1, ret1] =
        utility::conditionally_invoke(guard, &ExampleClass::static_function1, 10, SomeStruct{5});
    REQUIRE(called1 == true);
    REQUIRE(ret1 == 15);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, &ExampleClass::static_function1, x, s1);
    REQUIRE(called2 == true);
    REQUIRE(ret2 == 20);

    const auto called3 = utility::conditionally_invoke(guard, &ExampleClass::static_function2);
    REQUIRE(called3 == true);

    SomeStruct s2{15};
    const auto [called4, ret4] =
        utility::conditionally_invoke(guard, &ExampleClass::static_function3, 10, std::ref(s2));
    REQUIRE(called4 == true);
    REQUIRE(ret4 == 25);
}

TEST_CASE("Failed class static function call")
{
    const auto guard = []() { return false; };

    const auto [called1, ret1] =
        utility::conditionally_invoke(guard, &ExampleClass::static_function1, 10, SomeStruct{5});
    REQUIRE(called1 == false);
    REQUIRE(ret1 == 0);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, &ExampleClass::static_function1, x, s1);
    REQUIRE(called2 == false);
    REQUIRE(ret2 == 0);

    const auto called3 = utility::conditionally_invoke(guard, &ExampleClass::static_function2);
    REQUIRE(called3 == false);

    SomeStruct s2{15};
    const auto [called4, ret4] =
        utility::conditionally_invoke(guard, &ExampleClass::static_function3, 10, std::ref(s2));
    REQUIRE(called4 == false);
    REQUIRE(ret4 == 0);
}

TEST_CASE("Successful class member function call")
{
    ExampleClass instance;
    const auto guard = []() { return true; };

    const auto [called1, ret1] =
        utility::conditionally_invoke(guard, instance, &ExampleClass::function1, 10, SomeStruct{5});
    REQUIRE(called1 == true);
    REQUIRE(ret1 == 15);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, instance, &ExampleClass::function1, x, s1);
    REQUIRE(called2 == true);
    REQUIRE(ret2 == 20);

    const auto called3 = utility::conditionally_invoke(guard, instance, &ExampleClass::function2);
    REQUIRE(called3 == true);

    SomeStruct s2{15};
    const auto [called4, ret4] =
        utility::conditionally_invoke(guard, instance, &ExampleClass::function3, 10, std::ref(s2));
    REQUIRE(called4 == true);
    REQUIRE(ret4 == 25);
}

TEST_CASE("Failed class member function call")
{
    ExampleClass instance;
    const auto guard = []() { return false; };

    const auto [called1, ret1] =
        utility::conditionally_invoke(guard, instance, &ExampleClass::function1, 10, SomeStruct{5});
    REQUIRE(called1 == false);
    REQUIRE(ret1 == 0);

    int x = 10;
    SomeStruct s1{10};
    const auto [called2, ret2] = utility::conditionally_invoke(guard, instance, &ExampleClass::function1, x, s1);
    REQUIRE(called2 == false);
    REQUIRE(ret2 == 0);

    const auto called3 = utility::conditionally_invoke(guard, instance, &ExampleClass::function2);
    REQUIRE(called3 == false);

    SomeStruct s2{15};
    const auto [called4, ret4] =
        utility::conditionally_invoke(guard, instance, &ExampleClass::function3, 10, std::ref(s2));
    REQUIRE(called4 == false);
    REQUIRE(ret4 == 0);
}