summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestTuple.cpp
blob: a85552fef2fb6a3c88d3bbf1a945a7399798f4c4 (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
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
/* -*- 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 "mozilla/Assertions.h"
#include "mozilla/Move.h"
#include "mozilla/Pair.h"
#include "mozilla/Tuple.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Unused.h"

#include <stddef.h>
#include <utility>

using mozilla::Get;
using mozilla::IsSame;
using mozilla::MakeTuple;
using mozilla::MakeUnique;
using mozilla::Move;
using mozilla::Pair;
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(IsSame<decltype(expression), __VA_ARGS__>::value, \
      "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{Move(g)};
  CHECK(Get<0>(g) == nullptr);
  CHECK(*Get<0>(h) == 42);
}

static void
TestConstructionFromMozPair()
{
  // 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
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 = Move(f);
  CHECK(*Get<0>(e) == 42);
  CHECK(Get<0>(f) == nullptr);
}

static void
TestAssignmentFromMozPair()
{
  // 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)};
  Pair<UniquePtr<int>, UniquePtr<int>> f{MakeUnique<int>(42),
                                         MakeUnique<int>(42)};
  e = 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 = 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
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(-1, 1.2f);
  Tie(i, f) = rhs3;
  CHECK(i == rhs3.first());
  CHECK(f == rhs3.second());

  pair<int, float> rhs4(42, 1.5f);
  Tie(i, f) = rhs4;
  CHECK(i == rhs4.first);
  CHECK(f == rhs4.second);

  return true;
}

int
main()
{
  TestConstruction();
  TestConstructionFromMozPair();
  TestConstructionFromStdPair();
  TestAssignment();
  TestAssignmentFromMozPair();
  TestAssignmentFromStdPair();
  TestGet();
  TestMakeTuple();
  TestTie();
  return 0;
}