File: T.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (426 lines) | stat: -rw-r--r-- 10,606 bytes parent folder | download | duplicates (7)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <variant>

// template <class ...Types> class variant;

// template <class T>
// variant& operator=(T&&) noexcept(see below);

#include <cassert>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
#include <memory>

#include "test_macros.h"
#include "variant_test_helpers.h"

namespace MetaHelpers {

struct Dummy {
  Dummy() = default;
};

struct ThrowsCtorT {
  ThrowsCtorT(int) noexcept(false) {}
  ThrowsCtorT& operator=(int) noexcept { return *this; }
};

struct ThrowsAssignT {
  ThrowsAssignT(int) noexcept {}
  ThrowsAssignT& operator=(int) noexcept(false) { return *this; }
};

struct NoThrowT {
  NoThrowT(int) noexcept {}
  NoThrowT& operator=(int) noexcept { return *this; }
};

} // namespace MetaHelpers

namespace RuntimeHelpers {
#ifndef TEST_HAS_NO_EXCEPTIONS

struct ThrowsCtorT {
  int value;
  ThrowsCtorT() : value(0) {}
  ThrowsCtorT(int) noexcept(false) { throw 42; }
  ThrowsCtorT& operator=(int v) noexcept {
    value = v;
    return *this;
  }
};

struct MoveCrashes {
  int value;
  MoveCrashes(int v = 0) noexcept : value{v} {}
  MoveCrashes(MoveCrashes&&) noexcept { assert(false); }
  MoveCrashes& operator=(MoveCrashes&&) noexcept {
    assert(false);
    return *this;
  }
  MoveCrashes& operator=(int v) noexcept {
    value = v;
    return *this;
  }
};

struct ThrowsCtorTandMove {
  int value;
  ThrowsCtorTandMove() : value(0) {}
  ThrowsCtorTandMove(int) noexcept(false) { throw 42; }
  ThrowsCtorTandMove(ThrowsCtorTandMove&&) noexcept(false) { assert(false); }
  ThrowsCtorTandMove& operator=(int v) noexcept {
    value = v;
    return *this;
  }
};

struct ThrowsAssignT {
  int value;
  ThrowsAssignT() : value(0) {}
  ThrowsAssignT(int v) noexcept : value(v) {}
  ThrowsAssignT& operator=(int) noexcept(false) { throw 42; }
};

struct NoThrowT {
  int value;
  NoThrowT() : value(0) {}
  NoThrowT(int v) noexcept : value(v) {}
  NoThrowT& operator=(int v) noexcept {
    value = v;
    return *this;
  }
};

#endif // !defined(TEST_HAS_NO_EXCEPTIONS)
} // namespace RuntimeHelpers

constexpr void test_T_assignment_noexcept() {
  using namespace MetaHelpers;
  {
    using V = std::variant<Dummy, NoThrowT>;
    static_assert(std::is_nothrow_assignable<V, int>::value, "");
  }
  {
    using V = std::variant<Dummy, ThrowsCtorT>;
    static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  }
  {
    using V = std::variant<Dummy, ThrowsAssignT>;
    static_assert(!std::is_nothrow_assignable<V, int>::value, "");
  }
}

constexpr void test_T_assignment_sfinae() {
  {
    using V = std::variant<long, long long>;
    static_assert(!std::is_assignable<V, int>::value, "ambiguous");
  }
  {
    using V = std::variant<std::string, std::string>;
    static_assert(!std::is_assignable<V, const char*>::value, "ambiguous");
  }
  {
    using V = std::variant<std::string, void*>;
    static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
  }
  {
    using V = std::variant<std::string, float>;
    static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
  }
  {
    using V = std::variant<std::unique_ptr<int>, bool>;
    static_assert(!std::is_assignable<V, std::unique_ptr<char>>::value, "no explicit bool in operator=");
    struct X {
      operator void*();
    };
    static_assert(!std::is_assignable<V, X>::value, "no boolean conversion in operator=");
    static_assert(std::is_assignable<V, std::false_type>::value, "converted to bool in operator=");
  }
  {
    struct X {};
    struct Y {
      operator X();
    };
    using V = std::variant<X>;
    static_assert(std::is_assignable<V, Y>::value, "regression on user-defined conversions in operator=");
  }
}

TEST_CONSTEXPR_CXX20 void test_T_assignment_basic() {
  {
    std::variant<int> v(43);
    v = 42;
    assert(v.index() == 0);
    assert(std::get<0>(v) == 42);
  }
  {
    std::variant<int, long> v(43l);
    v = 42;
    assert(v.index() == 0);
    assert(std::get<0>(v) == 42);
    v = 43l;
    assert(v.index() == 1);
    assert(std::get<1>(v) == 43);
  }
  {
    std::variant<unsigned, long> v;
    v = 42;
    assert(v.index() == 1);
    assert(std::get<1>(v) == 42);
    v = 43u;
    assert(v.index() == 0);
    assert(std::get<0>(v) == 43);
  }
  {
    std::variant<std::string, bool> v = true;
    v                                 = "bar";
    assert(v.index() == 0);
    assert(std::get<0>(v) == "bar");
  }
}

void test_T_assignment_basic_no_constexpr() {
  std::variant<bool, std::unique_ptr<int>> v;
  v = nullptr;
  assert(v.index() == 1);
  assert(std::get<1>(v) == nullptr);
}

struct TraceStat {
  int construct      = 0;
  int copy_construct = 0;
  int copy_assign    = 0;
  int move_construct = 0;
  int move_assign    = 0;
  int T_copy_assign  = 0;
  int T_move_assign  = 0;
  int destroy        = 0;
};

template <bool CtorNoexcept, bool MoveCtorNoexcept>
struct Trace {
  struct T {};

  constexpr Trace(TraceStat* s) noexcept(CtorNoexcept) : stat(s) { ++s->construct; }
  constexpr Trace(T) noexcept(CtorNoexcept) : stat(nullptr) {}
  constexpr Trace(const Trace& o) : stat(o.stat) { ++stat->copy_construct; }
  constexpr Trace(Trace&& o) noexcept(MoveCtorNoexcept) : stat(o.stat) { ++stat->move_construct; }
  constexpr Trace& operator=(const Trace&) {
    ++stat->copy_assign;
    return *this;
  }
  constexpr Trace& operator=(Trace&&) noexcept {
    ++stat->move_assign;
    return *this;
  }

  constexpr Trace& operator=(const T&) {
    ++stat->T_copy_assign;
    return *this;
  }
  constexpr Trace& operator=(T&&) noexcept {
    ++stat->T_move_assign;
    return *this;
  }
  TEST_CONSTEXPR_CXX20 ~Trace() { ++stat->destroy; }

  TraceStat* stat;
};

TEST_CONSTEXPR_CXX20 void test_T_assignment_performs_construction() {
  {
    using V = std::variant<int, Trace<false, false>>;
    TraceStat stat;
    V v{1};
    v = &stat;
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 0);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.destroy == 0);
  }
  {
    using V = std::variant<int, Trace<false, true>>;
    TraceStat stat;
    V v{1};
    v = &stat;
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 1);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.destroy == 1);
  }

  {
    using V = std::variant<int, Trace<true, false>>;
    TraceStat stat;
    V v{1};
    v = &stat;
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 0);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.destroy == 0);
  }

  {
    using V = std::variant<int, Trace<true, true>>;
    TraceStat stat;
    V v{1};
    v = &stat;
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 0);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.destroy == 0);
  }
}

TEST_CONSTEXPR_CXX20 void test_T_assignment_performs_assignment() {
  {
    using V = std::variant<int, Trace<false, false>>;
    TraceStat stat;
    V v{&stat};
    v = Trace<false, false>::T{};
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 0);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.T_copy_assign == 0);
    assert(stat.T_move_assign == 1);
    assert(stat.destroy == 0);
  }
  {
    using V = std::variant<int, Trace<false, false>>;
    TraceStat stat;
    V v{&stat};
    Trace<false, false>::T t;
    v = t;
    assert(stat.construct == 1);
    assert(stat.copy_construct == 0);
    assert(stat.move_construct == 0);
    assert(stat.copy_assign == 0);
    assert(stat.move_assign == 0);
    assert(stat.T_copy_assign == 1);
    assert(stat.T_move_assign == 0);
    assert(stat.destroy == 0);
  }
}

void test_T_assignment_performs_construction_throw() {
  using namespace RuntimeHelpers;
#ifndef TEST_HAS_NO_EXCEPTIONS
  {
    using V = std::variant<std::string, ThrowsCtorT>;
    V v(std::in_place_type<std::string>, "hello");
    try {
      v = 42;
      assert(false);
    } catch (...) { /* ... */
    }
    assert(v.index() == 0);
    assert(std::get<0>(v) == "hello");
  }
  {
    using V = std::variant<ThrowsAssignT, std::string>;
    V v(std::in_place_type<std::string>, "hello");
    v = 42;
    assert(v.index() == 0);
    assert(std::get<0>(v).value == 42);
  }
#endif // TEST_HAS_NO_EXCEPTIONS
}

void test_T_assignment_performs_assignment_throw() {
  using namespace RuntimeHelpers;
#ifndef TEST_HAS_NO_EXCEPTIONS
  {
    using V = std::variant<ThrowsCtorT>;
    V v;
    v = 42;
    assert(v.index() == 0);
    assert(std::get<0>(v).value == 42);
  }
  {
    using V = std::variant<ThrowsCtorT, std::string>;
    V v;
    v = 42;
    assert(v.index() == 0);
    assert(std::get<0>(v).value == 42);
  }
  {
    using V = std::variant<ThrowsAssignT>;
    V v(100);
    try {
      v = 42;
      assert(false);
    } catch (...) { /* ... */
    }
    assert(v.index() == 0);
    assert(std::get<0>(v).value == 100);
  }
  {
    using V = std::variant<std::string, ThrowsAssignT>;
    V v(100);
    try {
      v = 42;
      assert(false);
    } catch (...) { /* ... */
    }
    assert(v.index() == 1);
    assert(std::get<1>(v).value == 100);
  }
#endif // TEST_HAS_NO_EXCEPTIONS
}

TEST_CONSTEXPR_CXX20 void test_T_assignment_vector_bool() {
  std::vector<bool> vec = {true};
  std::variant<bool, int> v;
  v = vec[0];
  assert(v.index() == 0);
  assert(std::get<0>(v) == true);
}

void non_constexpr_test() {
  test_T_assignment_basic_no_constexpr();
  test_T_assignment_performs_construction_throw();
  test_T_assignment_performs_assignment_throw();
}

TEST_CONSTEXPR_CXX20 bool test() {
  test_T_assignment_basic();
  test_T_assignment_performs_construction();
  test_T_assignment_performs_assignment();
  test_T_assignment_noexcept();
  test_T_assignment_sfinae();
  test_T_assignment_vector_bool();

  return true;
}

int main(int, char**) {
  test();
  non_constexpr_test();

#if TEST_STD_VER >= 20
  static_assert(test());
#endif
  return 0;
}