File: utils_optional_test.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (313 lines) | stat: -rw-r--r-- 9,696 bytes parent folder | download | duplicates (2)
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
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <mega/utils.h>
#include <mega/utils_optional.h>

using namespace mega;

class HelperClass
{
public:
    HelperClass(const std::string& s):
        str{s} {};

    std::string getStr() const
    {
        return str;
    }

    std::optional<int> toInt() const
    {
        return stringToNumber<int>(str);
    }

private:
    std::string str;
};

TEST(OptMonadicOp, Transform)
{
    const std::optional<std::string> a = "hello";
    const auto getSize = [](const std::string& s) -> int
    {
        return static_cast<int>(s.size());
    };
    // With empty
    auto result = std::optional<std::string>{} | transform(getSize);
    static_assert(std::is_same_v<decltype(result), std::optional<int>>);
    EXPECT_FALSE(result.has_value());

    // With lvalues
    result = a | transform(getSize);
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), 5);

    // With rvalues
    result = std::move(a) | transform(std::move(getSize));
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), 5);

    // With class method
    auto fromClass = std::optional<HelperClass>(std::string{"6"}) | transform(&HelperClass::getStr);
    EXPECT_EQ(fromClass, std::optional<std::string>("6"));
    fromClass = std::optional<HelperClass>() | transform(&HelperClass::getStr);
    EXPECT_EQ(fromClass, std::optional<std::string>());
}

TEST(OptMonadicOp, OrElse)
{
    const std::optional<std::string> a = "hello";
    const auto getEmpty = []
    {
        return std::optional{std::string{"EMPTY"}};
    };
    // With empty
    auto result = std::optional<std::string>{} | or_else(getEmpty);
    static_assert(std::is_same_v<decltype(result), std::remove_const_t<decltype(a)>>);
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), "EMPTY");

    // With lvalues
    result = a | or_else(getEmpty);
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), "hello");

    // With rvalues
    result = std::move(a) | or_else(std::move(getEmpty));
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), "hello");
}

TEST(OptMonadicOp, AndThen)
{
    const std::optional<std::string> a = "5";
    const auto toInt = [](const std::string& s) -> std::optional<int>
    {
        return stringToNumber<int>(s);
    };
    // With empty
    auto result = std::optional<std::string>{} | and_then(toInt);
    static_assert(std::is_same_v<decltype(result), std::optional<int>>);
    EXPECT_FALSE(result.has_value());

    // With lvalues
    result = a | and_then(toInt);
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), 5);

    // With rvalues
    result = std::move(a) | and_then(std::move(toInt));
    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), 5);

    // With class method
    auto fromClass = std::optional<HelperClass>(std::string{"6"}) | and_then(&HelperClass::toInt);
    EXPECT_EQ(fromClass, std::optional<int>(6));
    fromClass = std::optional<HelperClass>(std::string{"not"}) | and_then(&HelperClass::toInt);
    EXPECT_EQ(fromClass, std::optional<int>());
}

/**
 * @brief Reproduction of the example at https://en.cppreference.com/w/cpp/utility/optional/and_then
 */
TEST(OptMonadicOp, Combined)
{
    using namespace std::literals;

    const std::vector<std::optional<std::string>>
        v{"1234", "15 foo", "bar", "42", "5000000000", " 5", std::nullopt, "-43"};
    const auto manipulate = [](auto&& o)
    {
        // Disable format to match the one in the example at cppreference
        // clang-format off
        return (o
            // if optional is nullopt convert it to optional with "" string
            | or_else([]{ return std::optional{""s}; })
            // flatmap from strings to ints (making empty optionals where it fails)
            | and_then(stringToNumber<int>)
            // map int to int + 1
            | transform([](int n) { return n + 1; })
            // convert back to strings
            | transform([](int n) { return std::to_string(n); }))
            // replace all empty optionals that were left by
            // and_then and ignored by transforms with "NaN"
            .value_or("NaN"s);
        // clang-format on
    };

    const std::vector<std::string>
        resultExpected{"1235", "16", "NaN", "43", "NaN", "NaN", "NaN", "-42"};
    std::vector<std::string> result;
    result.reserve(resultExpected.size());
    std::transform(begin(v), end(v), std::back_inserter(result), manipulate);

    EXPECT_THAT(result, testing::ElementsAreArray(resultExpected));
}

TEST(OptMonadicOp, ChainingMixRvaluesAndLvalues)
{
    std::optional<std::string> persistent = "250";
    auto fallback = []
    {
        return std::optional<std::string>{"fallback"};
    };

    auto result = persistent |
                  transform(
                      [](const std::string& s)
                      {
                          return s + "0";
                      }) // lvalue operation: "2500"
                  | and_then(
                        [](std::string s) -> std::optional<int>
                        {
                            return stringToNumber<int>(s); // converts "2500" to 2500
                        }) |
                  transform(
                      [](int n) -> int
                      {
                          return n / 10;
                      }); // should yield 250

    EXPECT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), 250);

    // Also test with a temporary (rvalue) empty optional to trigger the fallback.
    auto resultEmpty = std::optional<std::string>{} |
                       transform(
                           [](const std::string& s)
                           {
                               return s + "0";
                           }) |
                       and_then(
                           [](std::string s) -> std::optional<int>
                           {
                               return stringToNumber<int>(s);
                           }) |
                       transform(
                           [](const int i)
                           {
                               return numberToString(i);
                           }) |
                       or_else(fallback) |
                       transform(
                           [](const std::string& s) -> std::string
                           {
                               return s + "!";
                           });
    EXPECT_TRUE(resultEmpty.has_value());
    EXPECT_EQ(resultEmpty.value(), "fallback!");
}

/**
 * @class MoveTracker
 * @brief Helper struct to validate move semantics
 */
struct MoveTracker
{
    int value;
    static int copy_count;
    static int move_count;

    MoveTracker(int v):
        value(v)
    {}

    MoveTracker(const MoveTracker& other):
        value(other.value)
    {
        ++copy_count;
    }

    MoveTracker(MoveTracker&& other) noexcept:
        value(other.value)
    {
        ++move_count;
    }

    MoveTracker& operator=(const MoveTracker& other)
    {
        value = other.value;
        ++copy_count;
        return *this;
    }

    MoveTracker& operator=(MoveTracker&& other) noexcept
    {
        value = other.value;
        ++move_count;
        return *this;
    }
};

int MoveTracker::copy_count = 0;
int MoveTracker::move_count = 0;

TEST(OptMonadicOp, MoveSemantics)
{
    // Reset counters to ensure a clean test.
    MoveTracker::copy_count = 0;
    MoveTracker::move_count = 0;

    std::optional<MoveTracker> opt{MoveTracker(100)};
    auto result = std::move(opt) |
                  and_then(
                      [](MoveTracker&& mt) -> std::optional<MoveTracker>
                      {
                          mt.value += 50;
                          return std::optional<MoveTracker>{std::move(mt)};
                      }) |
                  transform(
                      [](const MoveTracker& mt) -> int
                      {
                          return mt.value;
                      });
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(*result, 150);
    // Validate that no copies occurred.
    EXPECT_EQ(MoveTracker::copy_count, 0);
    EXPECT_EQ(MoveTracker::move_count, 2);
}

TEST(OptMonadicOp, FallbackAfterConversionFailure)
{
    const std::optional<std::string> invalid = "invalid_number";
    auto fallback = []
    {
        return std::optional<int>{-999};
    };
    auto result = invalid |
                  and_then(
                      [](const std::string& s) -> std::optional<int>
                      {
                          return stringToNumber<int>(s);
                      }) |
                  or_else(fallback) |
                  transform(
                      [](int n) -> std::string
                      {
                          return std::to_string(n);
                      });
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(result.value(), "-999");
}

TEST(OptMonadicOp, NonCopyableMoveOnly)
{
    std::optional<std::unique_ptr<int>> opt = std::make_unique<int>(42);
    auto result = std::move(opt) |
                  and_then(
                      [](std::unique_ptr<int>&& ptr) -> std::optional<std::unique_ptr<int>>
                      {
                          if (ptr)
                              *ptr += 8;
                          return std::optional<std::unique_ptr<int>>{std::move(ptr)};
                      }) |
                  transform(
                      [](const std::unique_ptr<int>& ptr) -> int
                      {
                          return *ptr;
                      });
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(*result, 50);
}