aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/unittests/erased_storage_ut.cpp
blob: 194c8e7fce5baa440da26e28fca317decd7f7a81 (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
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
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/memory/erased_storage.h>

#include <library/cpp/int128/int128.h>

#include <library/cpp/yt/misc/guid.h>

#include <util/generic/string.h>
#include <util/system/types.h>

#include <vector>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

struct TWithFieldInitalizer
{
    // NB: This class is not trivially default constructible.
    int Field{};
};

static_assert(!std::is_trivially_default_constructible_v<TWithFieldInitalizer>);

struct TCopyWithSideEffects
{
    TCopyWithSideEffects(const TCopyWithSideEffects&)
    { }
};

static_assert(!std::is_trivially_copy_constructible_v<TCopyWithSideEffects>);

struct TWithSubStruct
{
    TWithFieldInitalizer Field;
};

class TWithPrivateMembers
{
public:
    TWithPrivateMembers() = default;

private:
    [[maybe_unused]] std::array<std::byte, 8> Data_;
};

////////////////////////////////////////////////////////////////////////////////

TEST(TErasedStorageTest, Types)
{
    static_assert(CTriviallyErasable<int>);
    static_assert(CTriviallyErasable<i32>);
    static_assert(CTriviallyErasable<i64>);
    static_assert(CTriviallyErasable<i128>);
    static_assert(CTriviallyErasable<std::array<i128, 2>>);
    static_assert(CTriviallyErasable<TGuid>);
    static_assert(CTriviallyErasable<void*>);
    static_assert(CTriviallyErasable<double>);
    static_assert(CTriviallyErasable<const char*>);
    static_assert(CTriviallyErasable<TWithFieldInitalizer>);
    static_assert(CTriviallyErasable<TWithSubStruct>);
    static_assert(CTriviallyErasable<TWithPrivateMembers>);
    static_assert(CTriviallyErasable<char[8]>);

    static_assert(!CTriviallyErasable<TString>);
    static_assert(!CTriviallyErasable<std::vector<int>>);
    static_assert(!CTriviallyErasable<std::array<i128, 3>>);
    static_assert(!CTriviallyErasable<int&>);
    static_assert(!CTriviallyErasable<const int&>);
    static_assert(!CTriviallyErasable<int&&>);
    static_assert(!CTriviallyErasable<TCopyWithSideEffects>);
}

TEST(TErasedStorageTest, JustWorks)
{
    int var = 42;

    TErasedStorage stor(var);
    EXPECT_EQ(stor.AsConcrete<int>(), 42);

    var = 66;
    EXPECT_EQ(stor.AsConcrete<int>(), 42);
}

TEST(TErasedStorageTest, CopyAssign)
{
    int var = 42;
    TErasedStorage stor(var);
    EXPECT_EQ(stor.AsConcrete<int>(), 42);

    {
        int anotherVar = 77;
        stor = TErasedStorage(anotherVar);
    }
    EXPECT_EQ(stor.AsConcrete<int>(), 77);

    double thirdVar = 9.92145214;
    stor = TErasedStorage(thirdVar);
    EXPECT_DOUBLE_EQ(stor.AsConcrete<double>(), 9.92145214);
}

TEST(TErasedStorageTest, Pointer)
{
    TString message("Hello world");
    TErasedStorage stor(&message);

    EXPECT_EQ(*stor.AsConcrete<TString*>(), TString("Hello world"));
    message = "Goodbye world";

    EXPECT_EQ(*stor.AsConcrete<TString*>(), "Goodbye world");
}

TEST(TErasedStorageTest, MutateStorage)
{
    int var = 0;
    TErasedStorage stor(var);
    EXPECT_EQ(stor.AsConcrete<int>(), 0);

    auto& ref = stor.AsConcrete<int>();
    ref = 88;

    EXPECT_EQ(stor.AsConcrete<int>(), 88);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT