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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 <type_traits>
#include <utility>
#include "mozilla/Assertions.h"
#include "mozilla/CompactPair.h"
#include "mozilla/Tuple.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"
using mozilla::CompactPair;
using mozilla::Get;
using mozilla::MakeTuple;
using mozilla::MakeUnique;
using mozilla::Tie;
using mozilla::Tuple;
using mozilla::UniquePtr;
using mozilla::Unused;
using std::pair;
#define CHECK(c) \
do { \
bool cond = !!(c); \
MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
} while (false)
// The second argument is the expected type. It's variadic to allow the
// type to contain commas.
#define CHECK_TYPE(expression, ...) \
static_assert(std::is_same_v<decltype(expression), __VA_ARGS__>, \
"Type mismatch!")
struct ConvertibleToInt {
operator int() const { return 42; }
};
static void TestConstruction() {
// Default construction
Tuple<> a;
Unused << a;
Tuple<int> b;
Unused << b;
// Construction from elements
int x = 1, y = 1;
Tuple<int, int> c{x, y};
Tuple<int&, const int&> d{x, y};
x = 42;
y = 42;
CHECK(Get<0>(c) == 1);
CHECK(Get<1>(c) == 1);
CHECK(Get<0>(d) == 42);
CHECK(Get<1>(d) == 42);
// Construction from objects convertible to the element types
Tuple<int, int> e{1.0, ConvertibleToInt{}};
// Copy construction
Tuple<int> x1;
Tuple<int> x2{x1};
Tuple<int, int> f(c);
CHECK(Get<0>(f) == 1);
CHECK(Get<0>(f) == 1);
// Move construction
Tuple<UniquePtr<int>> g{MakeUnique<int>(42)};
Tuple<UniquePtr<int>> h{std::move(g)};
CHECK(Get<0>(g) == nullptr);
CHECK(*Get<0>(h) == 42);
}
static void TestConstructionFromMozPair() {
// Construction from elements
int x = 1, y = 1;
CompactPair<int, int> a{x, y};
CompactPair<int&, const int&> b{x, y};
Tuple<int, int> c(a);
Tuple<int&, const int&> d(b);
x = 42;
y = 42;
CHECK(Get<0>(c) == 1);
CHECK(Get<1>(c) == 1);
CHECK(Get<0>(d) == 42);
CHECK(Get<1>(d) == 42);
}
static void TestConstructionFromStdPair() {
// Construction from elements
int x = 1, y = 1;
pair<int, int> a{x, y};
pair<int&, const int&> b{x, y};
Tuple<int, int> c(a);
Tuple<int&, const int&> d(b);
x = 42;
y = 42;
CHECK(Get<0>(c) == 1);
CHECK(Get<1>(c) == 1);
CHECK(Get<0>(d) == 42);
CHECK(Get<1>(d) == 42);
}
static void TestAssignment() {
// Copy assignment
Tuple<int> a{0};
Tuple<int> b{42};
a = b;
CHECK(Get<0>(a) == 42);
// Assignment to reference member
int i = 0;
int j = 42;
Tuple<int&> c{i};
Tuple<int&> d{j};
c = d;
CHECK(i == 42);
// Move assignment
Tuple<UniquePtr<int>> e{MakeUnique<int>(0)};
Tuple<UniquePtr<int>> f{MakeUnique<int>(42)};
e = std::move(f);
CHECK(*Get<0>(e) == 42);
CHECK(Get<0>(f) == nullptr);
}
static void TestAssignmentFromMozPair() {
// Copy assignment
Tuple<int, int> a{0, 0};
CompactPair<int, int> b{42, 42};
a = b;
CHECK(Get<0>(a) == 42);
CHECK(Get<1>(a) == 42);
// Assignment to reference member
int i = 0;
int j = 0;
int k = 42;
Tuple<int&, int&> c{i, j};
CompactPair<int&, int&> d{k, k};
c = d;
CHECK(i == 42);
CHECK(j == 42);
// Move assignment
Tuple<UniquePtr<int>, UniquePtr<int>> e{MakeUnique<int>(0),
MakeUnique<int>(0)};
CompactPair<UniquePtr<int>, UniquePtr<int>> f{MakeUnique<int>(42),
MakeUnique<int>(42)};
e = std::move(f);
CHECK(*Get<0>(e) == 42);
CHECK(*Get<1>(e) == 42);
CHECK(f.first() == nullptr);
CHECK(f.second() == nullptr);
}
static void TestAssignmentFromStdPair() {
// Copy assignment
Tuple<int, int> a{0, 0};
pair<int, int> b{42, 42};
a = b;
CHECK(Get<0>(a) == 42);
CHECK(Get<1>(a) == 42);
// Assignment to reference member
int i = 0;
int j = 0;
int k = 42;
Tuple<int&, int&> c{i, j};
pair<int&, int&> d{k, k};
c = d;
CHECK(i == 42);
CHECK(j == 42);
// Move assignment.
Tuple<UniquePtr<int>, UniquePtr<int>> e{MakeUnique<int>(0),
MakeUnique<int>(0)};
// XXX: On some platforms std::pair doesn't support move constructor.
pair<UniquePtr<int>, UniquePtr<int>> f;
f.first = MakeUnique<int>(42);
f.second = MakeUnique<int>(42);
e = std::move(f);
CHECK(*Get<0>(e) == 42);
CHECK(*Get<1>(e) == 42);
CHECK(f.first == nullptr);
CHECK(f.second == nullptr);
}
static void TestGet() {
int x = 1;
int y = 2;
int z = 3;
Tuple<int, int&, const int&> tuple(x, y, z);
// Using Get<>() to read elements
CHECK(Get<0>(tuple) == 1);
CHECK(Get<1>(tuple) == 2);
CHECK(Get<2>(tuple) == 3);
// Using Get<>() to write to elements
Get<0>(tuple) = 41;
CHECK(Get<0>(tuple) == 41);
// Writing through reference elements
Get<1>(tuple) = 42;
CHECK(Get<1>(tuple) == 42);
CHECK(y == 42);
}
static void TestMakeTuple() {
auto tuple = MakeTuple(42, 0.5f, 'c');
CHECK_TYPE(tuple, Tuple<int, float, char>);
CHECK(Get<0>(tuple) == 42);
CHECK(Get<1>(tuple) == 0.5f);
CHECK(Get<2>(tuple) == 'c');
// Make sure we don't infer the type to be Tuple<int&>.
int x = 1;
auto tuple2 = MakeTuple(x);
CHECK_TYPE(tuple2, Tuple<int>);
x = 2;
CHECK(Get<0>(tuple2) == 1);
}
static bool TestTieMozPair() {
int i;
float f;
char c;
Tuple<int, float, char> rhs1(42, 0.5f, 'c');
Tie(i, f, c) = rhs1;
CHECK(i == Get<0>(rhs1));
CHECK(f == Get<1>(rhs1));
CHECK(c == Get<2>(rhs1));
// Test conversions
Tuple<ConvertibleToInt, double, unsigned char> rhs2(ConvertibleToInt(), 0.7f,
'd');
Tie(i, f, c) = rhs2;
CHECK(i == Get<0>(rhs2));
CHECK(f == Get<1>(rhs2));
CHECK(c == Get<2>(rhs2));
// Test Pair
CompactPair<int, float> rhs3(42, 1.5f);
Tie(i, f) = rhs3;
CHECK(i == rhs3.first());
CHECK(f == rhs3.second());
return true;
}
static bool TestTie() {
int i;
float f;
char c;
Tuple<int, float, char> rhs1(42, 0.5f, 'c');
Tie(i, f, c) = rhs1;
CHECK(i == Get<0>(rhs1));
CHECK(f == Get<1>(rhs1));
CHECK(c == Get<2>(rhs1));
// Test conversions
Tuple<ConvertibleToInt, double, unsigned char> rhs2(ConvertibleToInt(), 0.7f,
'd');
Tie(i, f, c) = rhs2;
CHECK(i == Get<0>(rhs2));
CHECK(f == Get<1>(rhs2));
CHECK(c == Get<2>(rhs2));
// Test Pair
pair<int, float> rhs3(42, 1.5f);
Tie(i, f) = rhs3;
CHECK(i == rhs3.first);
CHECK(f == rhs3.second);
return true;
}
static bool TestTieIgnore() {
int i;
char c;
Tuple<int, float, char> rhs1(42, 0.5f, 'c');
Tie(i, mozilla::Ignore, c) = rhs1;
CHECK(i == Get<0>(rhs1));
CHECK(c == Get<2>(rhs1));
return true;
}
template <typename... Elements, typename F>
static void CheckForEachCall(const Tuple<Elements...>& aTuple,
F&& CallForEach) {
constexpr std::size_t tupleSize = sizeof...(Elements);
Tuple<Elements...> checkResult;
std::size_t i = 0;
auto createResult = [&](auto& aElem) {
static_assert(tupleSize == 3,
"Need to deal with more/less cases in the switch below");
CHECK(i < tupleSize);
switch (i) {
case 0:
Get<0>(checkResult) = aElem;
break;
case 1:
Get<1>(checkResult) = aElem;
break;
case 2:
Get<2>(checkResult) = aElem;
break;
}
++i;
};
CallForEach(aTuple, createResult);
CHECK(checkResult == aTuple);
}
static bool TestForEach() {
Tuple<int, float, char> tuple = MakeTuple(42, 0.5f, 'c');
CheckForEachCall(
tuple, [](auto& aTuple, auto&& aLambda) { aTuple.ForEach(aLambda); });
CheckForEachCall(
tuple, [](auto& aTuple, auto&& aLambda) { ForEach(aTuple, aLambda); });
CheckForEachCall(tuple, [](auto& aTuple, auto&& aLambda) {
const decltype(aTuple)& constTuple = aTuple;
constTuple.ForEach(aLambda);
});
CheckForEachCall(tuple, [](auto& aTuple, auto&& aLambda) {
const decltype(aTuple)& constTuple = aTuple;
ForEach(constTuple, aLambda);
});
return true;
}
int main() {
TestConstruction();
TestConstructionFromMozPair();
TestConstructionFromStdPair();
TestAssignment();
TestAssignmentFromMozPair();
TestAssignmentFromStdPair();
TestGet();
TestMakeTuple();
TestTie();
TestTieIgnore();
TestTieMozPair();
TestForEach();
return 0;
}
|