-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTuple.test.cpp
More file actions
254 lines (202 loc) · 7.64 KB
/
Copy pathTuple.test.cpp
File metadata and controls
254 lines (202 loc) · 7.64 KB
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
#include "Tuple.h"
#include "Tuple.make.h"
#include "Tuple.ops.h"
#include "meta17/Type.ops.h"
#include "meta17/same.h"
#include <gtest/gtest.h>
using namespace tuple17;
using meta17::Index;
using meta17::same;
namespace {
template<typename Type>
constexpr std::size_t alignment(std::size_t offset = 0, std::size_t current = 0) {
return current >= offset ? current : alignment<Type>(offset, current + alignof(Type));
}
} // namespace
TEST(Tuple, basic) {
using T = Tuple<char, int, double>;
const auto offset0 = alignment<char>();
const auto offset1 = alignment<int>(offset0 + sizeof(char));
const auto offset2 = alignment<double>(offset1 + sizeof(int));
const auto offsetT = alignment<T>(offset2 + sizeof(double));
static_assert(T::offset_at<0> == offset0);
static_assert(T::offset_at<1> == offset1);
static_assert(T::offset_at<2> == offset2);
static_assert(T::offset_of<char> == offset0);
static_assert(T::offset_of<int> == offset1);
static_assert(T::offset_of<double> == offset2);
static_assert(T::type_at<0> == type<char>);
static_assert(T::type_at<1> == type<int>);
static_assert(T::type_at<2> == type<double>);
static_assert(T::hasType<char>());
static_assert(T::hasType<int>());
static_assert(T::hasType<double>());
static_assert(!T::hasType<float>());
static_assert(!T::hasType<bool>());
static_assert(!T::hasType<unsigned int>());
static_assert(T::type_at<0> == type<char>);
static_assert(T::type_at<1> == type<int>);
static_assert(T::type_at<2> == type<double>);
static_assert(T::type_at<1> != type<unsigned int>);
static_assert(T::type_at<2> != type<float>);
static_assert(sizeof(T) == offsetT);
static_assert(std::tuple_size<T>::value == 3);
static_assert(type<std::tuple_element<0, T>::type> == type<char>);
static_assert(type<std::tuple_element<1, T>::type> == type<int>);
static_assert(type<std::tuple_element<2, T>::type> == type<double>);
}
TEST(Tuple, bool) {
using T = Tuple<bool, int>;
const auto offset0 = alignment<bool>();
const auto offset1 = alignment<int>(offset0 + sizeof(bool));
const auto offsetT = alignment<T>(offset1 + sizeof(int));
static_assert(T::offset_at<0> == offset0);
static_assert(T::offset_at<1> == offset1);
// Access by type does not work because bool and int are not distinguishable
// static_assert(T::offset_of<bool> == offset0);
// static_assert(T::offset_of<int> == offset1);
static_assert(T::type_at<0> == type<bool>);
static_assert(T::type_at<1> == type<int>);
static_assert(T::hasType<bool>());
static_assert(T::hasType<int>());
static_assert(T::type_at<0> == type<bool>);
static_assert(T::type_at<1> == type<int>);
static_assert(sizeof(T) == offsetT);
static_assert(std::tuple_size<T>::value == 2);
static_assert(type<std::tuple_element<0, T>::type> == type<bool>);
static_assert(type<std::tuple_element<1, T>::type> == type<int>);
}
TEST(Tuple, complex) {
struct Inner {
double d;
int i;
char c;
};
using T = Tuple<Inner, char, Inner>;
const auto offset0 = alignment<Inner>();
const auto offset1 = alignment<char>(offset0 + sizeof(Inner));
const auto offset2 = alignment<Inner>(offset1 + sizeof(char));
const auto offsetT = alignment<T>(offset2 + sizeof(Inner));
static_assert(T::offset_at<0> == offset0);
static_assert(T::offset_at<1> == offset1);
static_assert(T::offset_at<2> == offset2);
// Access by type does not work for Inner because types used multiple times
// static_assert(T::offset_of<Inner> == offset0);
static_assert(T::offset_of<char> == offset1);
// static_assert(T::offset_of<Inner> == offset2);
static_assert(T::type_at<0> == type<Inner>);
static_assert(T::type_at<1> == type<char>);
static_assert(T::type_at<2> == type<Inner>);
static_assert(T::hasType<Inner>());
static_assert(T::hasType<char>());
static_assert(T::type_at<0> == type<Inner>);
static_assert(T::type_at<1> == type<char>);
static_assert(T::type_at<2> == type<Inner>);
auto t = T{};
static_assert(sizeof(t) == offsetT);
static_assert(std::tuple_size<T>::value == 3);
static_assert(type<std::tuple_element<0, T>::type> == type<Inner>);
static_assert(type<std::tuple_element<1, T>::type> == type<char>);
static_assert(type<std::tuple_element<2, T>::type> == type<Inner>);
}
TEST(Tuple, access) {
using meta17::index;
using T = Tuple<char, int, double>;
auto t = T{};
t.at(index<1>) = 23;
t.of<double>() = 4.2;
EXPECT_EQ((t.of(type<int>)), 23);
EXPECT_EQ((t.at(index<2>)), 4.2);
EXPECT_EQ(get<char>(t), '\0');
EXPECT_EQ(get<int>(t), 23);
EXPECT_EQ(get<double>(t), 4.2);
}
TEST(Tuple, copy_move) {
using MultiArg = Tuple<char, int, double>;
auto m0 = MultiArg{};
auto m1 = m0; // copy
auto m2 = std::move(m1); // move copy
EXPECT_EQ(m0, m2);
}
TEST(Tuple, construct_empty) {
using T = Tuple<>;
auto t = T{};
t.visitAll([]() {});
}
TEST(Tuple, construct_single) {
using SingleArg = Tuple<int>;
auto s0 = SingleArg{23};
SingleArg s1(s0); // Test that it won't use the argument forward constructor
EXPECT_EQ(s0, s1);
}
TEST(Tuple, construct_two) {
using TwoArgs = Tuple<int, float>;
auto s0 = TwoArgs{1, 2.3f};
auto s1 = TwoArgs{s0};
EXPECT_EQ(s0, s1);
}
TEST(Tuple, ambigious) {
using Ambiguous = Tuple<char, int, int>;
auto a0 = Ambiguous{};
auto a1 = Ambiguous::from('c');
// auto a1b = Ambiguous::from('c', 23, 32); // not allowed! - arguments are treated out of order!
auto a2 = Ambiguous{'c', 23, 32};
EXPECT_EQ((a2.at<1>()), 23);
EXPECT_EQ((a2.at<2>()), 32);
}
TEST(Tuple, fromTuple) {
using Small = Tuple<char, int, double>;
using Large = Tuple<int, double, char, float>;
auto s = Small{'c', 23, 4.2};
auto l = Large::fromTuple(s);
EXPECT_EQ((l.of<char>()), 'c');
EXPECT_EQ((l.of<double>()), 4.2);
EXPECT_EQ((l.of<int>()), 23);
EXPECT_EQ((l.of<float>()), 0);
}
TEST(Tuple, shrink) {
// Shrinking is not allowed since not all parts can be assigned - code should not compile!
/*
* using Large = Tuple<char, int, double, float>;
* auto l = Large{'c', 23, 4.2, 2.4};
* using Small = MakeTuple<TypePack<int, double, char>>; // other order / bigger
* auto s = Small(l);
*/
}
TEST(Tuple, structuredBinding) {
using T = Tuple<char, int>;
auto v = T{'c', 23};
auto& [a, b] = v;
static_assert(type<decltype(a)> == type<char>);
static_assert(type<decltype(b)> == type<int>);
EXPECT_EQ(a, 'c');
EXPECT_EQ(b, 23);
}
TEST(Tuple, constStructuredBinding) {
using T = Tuple<char, int>;
const auto v = T{'c', 23};
auto& [a, b] = v;
static_assert(type<decltype(a)> == type<const char>);
static_assert(type<decltype(b)> == type<const int>);
EXPECT_EQ(a, 'c');
EXPECT_EQ(b, 23);
}
TEST(Tuple, forStructuredBinding) {
struct X {
int v{};
constexpr X() = default;
constexpr explicit X(int v)
: v(v) {}
};
using T = std::vector<Tuple<char, X>>;
const auto v = T{{'c', X{23}}};
const auto& r = v;
for (const auto& [a, b] : r) {
#if !defined(_MSC_VER) || _MSC_VER >= 1920
static_assert(same<decltype(a), const char>);
static_assert(type<decltype(b)> == type<const X>);
#endif
EXPECT_EQ(a, 'c');
EXPECT_EQ(b.v, 23);
}
}