summaryrefslogtreecommitdiff
path: root/src/test/VariantTest.cpp
blob: b5655730dc37cfd9ac2fdfc766d89ae12cb3e9cf (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "../CommonAPI/SerializableVariant.h"

using namespace CommonAPI;

class VariantTest: public ::testing::Test {

  void SetUp() {
  }

  void TearDown() {
  }
};


struct test1: CommonAPI::SerializableStruct {
    int a;
    std::string b;

    test1() = default;
    test1(const int& a, const std::string& b) :
    a(a), b(b) {
    }

    void readFromInputStream(CommonAPI::InputStream& inputStream) {

    }

    void writeToOutputStream(CommonAPI::OutputStream& outputStream) const {

    }

    static inline void writeToTypeOutputStream(CommonAPI::TypeOutputStream& typeOutputStream) {
    }

};

struct test2: CommonAPI::SerializableStruct {
    int a;
    std::string b;

    test2() = default;
    test2(const int& a, const std::string& b) :
    a(a), b(b) {
    }

    void readFromInputStream(CommonAPI::InputStream& inputStream) {

    }

    void writeToOutputStream(CommonAPI::OutputStream& outputStream) const {

    }

    static inline void writeToTypeOutputStream(CommonAPI::TypeOutputStream& typeOutputStream) {
    }

};


TEST_F(VariantTest, VariantTestPack) {

    int fromInt = 5;
    double fromDouble = 12.344;
    std::string fromString = "123abcsadfaljkawlöfasklöerklöfjasklfjysklfjaskfjsklösdfdko4jdfasdjioögjopefgip3rtgjiprg!";
    Variant<int, double, std::string> myVariant(fromInt);

    Variant<int, double, std::string>* myVariants = new Variant<int, double, std::string>(fromString);

    Variant<int, double, std::string> myVariantf(fromDouble);

    std::string myString = myVariants->get<std::string>();
    EXPECT_EQ(fromString, myString);


    const int& myInt = myVariant.get<int>();
    EXPECT_EQ(myInt, fromInt);

    int vType = myVariant.getValueType();
    EXPECT_EQ(3, vType);

    Variant<int, double, std::string> myVariant2 = myVariant;
    const int& myInt2 = myVariant2.get<int>();
    EXPECT_EQ(myInt2, fromInt);

    Variant<int, double, std::string> myVariant3 = fromInt;
    const int& myInt3 = myVariant3.get<int>();
    EXPECT_EQ(myInt3, fromInt);

    myString = myVariants->get<std::string>();
    EXPECT_EQ(myString, fromString);

    Variant<int, double, std::string> myVariantCopy(myVariant);
    const int& myIntCopy = myVariantCopy.get<int>();
    EXPECT_EQ(myIntCopy, fromInt);

    Variant<int, double, std::string> myVariantCopy2;
    myVariantCopy2 = myVariant;

    const int& myIntCopy2 = myVariantCopy2.get<int>();
    EXPECT_EQ(myIntCopy2, fromInt);

#ifdef __EXCEPTIONS
    EXPECT_ANY_THROW(myVariant.get<double>());
#endif

    EXPECT_TRUE(myVariant.isType<int>());

    EXPECT_FALSE(myVariant.isType<std::string>());

    Variant<int, double, std::string> movedVariant = std::move(myVariant);
    EXPECT_TRUE(movedVariant.isType<int>());
    EXPECT_EQ(fromInt, movedVariant.get<int>());

    const double& myDouble = myVariantf.get<double>();
    EXPECT_EQ(myDouble, fromDouble);

    Variant<int, double, std::string> myVariantsCopy(*myVariants);
    std::string myStringCopy = myVariantsCopy.get<std::string>();
    EXPECT_EQ(myStringCopy, fromString);

    *myVariants = std::string("Hello World");
    myString = myVariants->get<std::string>();
    EXPECT_EQ(myString, "Hello World");

    myStringCopy = myVariantsCopy.get<std::string>();
    EXPECT_EQ(myStringCopy, fromString);

    delete myVariants;

    test1 sourceStruct = {1, "a"};

    Variant<test1, test2> complexSource = sourceStruct;

    Variant<test1, test2> complexTarget = complexSource;
    EXPECT_EQ(1, complexTarget.get<test1>().a);
}

typedef Variant<test1, test2, std::string, uint8_t> ComplexTestVariant;
typedef Variant<ComplexTestVariant, test2, std::string, uint8_t> OuterTestVariant;

struct Container: CommonAPI::SerializableStruct {
    ComplexTestVariant a;
    std::string b;

    Container() = default;
    Container(const ComplexTestVariant& a, const std::string& b) :
    a(a), b(b) {
    }

    void readFromInputStream(CommonAPI::InputStream& inputStream) {

    }

    void writeToOutputStream(CommonAPI::OutputStream& outputStream) const {

    }

    static inline void writeToTypeOutputStream(CommonAPI::TypeOutputStream& typeOutputStream) {
    }

};

struct ContainerOuter: CommonAPI::SerializableStruct {
    OuterTestVariant a;
    std::string b;

    ContainerOuter() = default;
    ContainerOuter(const OuterTestVariant& a, const std::string& b) :
    a(a), b(b) {
    }

    void readFromInputStream(CommonAPI::InputStream& inputStream) {

    }

    void writeToOutputStream(CommonAPI::OutputStream& outputStream) const {

    }

    static inline void writeToTypeOutputStream(CommonAPI::TypeOutputStream& typeOutputStream) {
    }

};

TEST_F(VariantTest, VariantMoveTest) {

    ComplexTestVariant emptyTest;
    Container cont(std::move(emptyTest), "Hello");

    test1 assignStruct(1, "Assign");
    ComplexTestVariant assignTest = assignStruct;
    Container contAss(std::move(assignTest), "Hello");
    EXPECT_EQ("Assign", contAss.a.get<test1>().b);

    test1 constructStruct(1, "Construct");
    ComplexTestVariant constructTest(constructStruct);
    Container contCon(std::move(constructTest), "Hello");
    EXPECT_EQ("Construct", contCon.a.get<test1>().b);

    ComplexTestVariant rec;

    rec = cont.a;

    rec = contAss.a;
    EXPECT_EQ("Assign", rec.get<test1>().b);

    rec = contCon.a;
    EXPECT_EQ("Construct", rec.get<test1>().b);
}

TEST_F(VariantTest, VariantInVariantMoveTest) {

    OuterTestVariant emptyTest;
    ContainerOuter cont(std::move(emptyTest), "Hello");

    test1 assignStruct(1, "Assign");
    ComplexTestVariant assignTest = assignStruct;
    OuterTestVariant assignOuter = assignTest;
    ContainerOuter contAss(std::move(assignOuter), "Hello");
    EXPECT_EQ("Assign", contAss.a.get<ComplexTestVariant>().get<test1>().b);

    test1 constructStruct(1, "Construct");
    ComplexTestVariant constructTest(constructStruct);
    OuterTestVariant constructOuter(constructTest);
    ContainerOuter contCon(std::move(constructOuter), "Hello");
    EXPECT_EQ("Construct", contCon.a.get<ComplexTestVariant>().get<test1>().b);

    ComplexTestVariant rec;

    rec = contAss.a.get<ComplexTestVariant>();
    EXPECT_EQ("Assign", rec.get<test1>().b);


    rec = contCon.a.get<ComplexTestVariant>();
    EXPECT_EQ("Construct", rec.get<test1>().b);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}