aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/overloaded_ut.cpp
blob: cb45a9143d4a4c7c576c8f31719b50ac75f8941f (plain) (blame)
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
#include <util/generic/overloaded.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/variant.h>
#include <util/generic/algorithm.h>

#include <tuple>

namespace {
    struct TType1 {};
    struct TType2 {};
    struct TType3 {};
}

Y_UNIT_TEST_SUITE(TOverloadedTest) {
    Y_UNIT_TEST(StaticTest) {
        auto f = TOverloaded{
            [](const TType1&) {},
            [](const TType2&) {},
            [](const TType3&) {}}; 
        using F = decltype(f);
        static_assert(std::is_invocable_v<F, TType1>);
        static_assert(std::is_invocable_v<F, TType2>);
        static_assert(std::is_invocable_v<F, TType3>);
        static_assert(!std::is_invocable_v<F, int>);
        static_assert(!std::is_invocable_v<F, double>);
    }

    Y_UNIT_TEST(VariantTest) {
        std::variant<int, double, TType1> v = 5;
        int res = 0;
        std::visit(TOverloaded{
                       [&](int val) { res = val; },
                       [&](double) { res = -1; },
                       [&](TType1) { res = -1; }},
                   v);
        UNIT_ASSERT_VALUES_EQUAL(res, 5);
    }

    Y_UNIT_TEST(TupleTest) {
        std::tuple<int, double, bool, int> t{5, 3.14, true, 20}; 
        TString res;

        ForEach(t, TOverloaded{
                       [&](int val) { res += "(int) " + ToString(val) + ' '; }, 
                       [&](double val) { res += "(double) " + ToString(val) + ' '; }, 
                       [&](bool val) { res += "(bool) " + ToString(val) + ' '; }, 
                   }); 

        UNIT_ASSERT_VALUES_EQUAL(res, "(int) 5 (double) 3.14 (bool) 1 (int) 20 ");
    }

    Y_UNIT_TEST(ImplicitConversionsTest) {
        using TTestVariant = std::variant<int, double, char>;

        // Purposefully exhibit inexact overload matched with implicit type
        // conversions

        // All cases implicitly cast to int
        auto matchAsInt = [](TTestVariant var) {
            return std::visit(TOverloaded{
                                  [](int val) { return val; },
                              }, var);
        };

        UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{17.77}), 17);
        UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{12345}), 12345);
        UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{'X'}), 88);

        // All cases implicitly cast to double
        auto matchAsDouble = [](TTestVariant var) {
            return std::visit(TOverloaded{
                                  [](double val) { return val; },
                              }, var);
        };

        UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{17.77}), 17.77);
        UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{12345}), 12345.0);
        UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{'X'}), 88.0);
    }
}