1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef TEST_STD_CONTAINERS_SEQUENCES_VECTOR_VECTOR_MODIFIERS_COMMON_H
10#define TEST_STD_CONTAINERS_SEQUENCES_VECTOR_VECTOR_MODIFIERS_COMMON_H
11
12#include "test_macros.h"
13
14#include <type_traits> // for __libcpp_is_trivially_relocatable
15
16#ifndef TEST_HAS_NO_EXCEPTIONS
17struct Throws {
18 Throws() : v_(0) {}
19 Throws(int v) : v_(v) {}
20 Throws(const Throws& rhs) : v_(rhs.v_) {
21 if (sThrows)
22 throw 1;
23 }
24 Throws(Throws&& rhs) : v_(rhs.v_) {
25 if (sThrows)
26 throw 1;
27 }
28 Throws& operator=(const Throws& rhs) {
29 v_ = rhs.v_;
30 return *this;
31 }
32 Throws& operator=(Throws&& rhs) {
33 v_ = rhs.v_;
34 return *this;
35 }
36 int v_;
37 static bool sThrows;
38};
39
40bool Throws::sThrows = false;
41
42struct ThrowingMoveOnly {
43 TEST_CONSTEXPR ThrowingMoveOnly() : value(0), do_throw(false) {}
44 TEST_CONSTEXPR explicit ThrowingMoveOnly(int v) : value(v), do_throw(false) {}
45 TEST_CONSTEXPR explicit ThrowingMoveOnly(int v, bool throw_) : value(v), do_throw(throw_) {}
46
47 ThrowingMoveOnly(const ThrowingMoveOnly& rhs) = delete;
48 ThrowingMoveOnly& operator=(const ThrowingMoveOnly&) = delete;
49
50 TEST_CONSTEXPR_CXX14 ThrowingMoveOnly(ThrowingMoveOnly&& rhs) : value(rhs.value), do_throw(rhs.do_throw) {
51 if (do_throw)
52 throw 1;
53 }
54 TEST_CONSTEXPR_CXX14 ThrowingMoveOnly& operator=(ThrowingMoveOnly&& rhs) {
55 value = rhs.value;
56 do_throw = rhs.do_throw;
57 if (do_throw)
58 throw 1;
59 return *this;
60 }
61
62 TEST_CONSTEXPR_CXX14 friend bool operator==(ThrowingMoveOnly const& lhs, ThrowingMoveOnly const& rhs) {
63 return lhs.value == rhs.value;
64 }
65
66 int value;
67 bool do_throw;
68};
69#endif // TEST_HAS_NO_EXCEPTIONS
70
71struct Tracker {
72 int copy_assignments = 0;
73 int move_assignments = 0;
74};
75
76struct TrackedAssignment {
77 Tracker* tracker_;
78 TEST_CONSTEXPR_CXX14 explicit TrackedAssignment(Tracker* tracker) : tracker_(tracker) {}
79
80 TrackedAssignment(TrackedAssignment const&) = default;
81 TrackedAssignment(TrackedAssignment&&) = default;
82
83 TEST_CONSTEXPR_CXX14 TrackedAssignment& operator=(TrackedAssignment const&) {
84 tracker_->copy_assignments++;
85 return *this;
86 }
87 TEST_CONSTEXPR_CXX14 TrackedAssignment& operator=(TrackedAssignment&&) {
88 tracker_->move_assignments++;
89 return *this;
90 }
91};
92
93struct NonTriviallyRelocatable {
94 int value_;
95 TEST_CONSTEXPR NonTriviallyRelocatable() : value_(0) {}
96 TEST_CONSTEXPR explicit NonTriviallyRelocatable(int v) : value_(v) {}
97 TEST_CONSTEXPR NonTriviallyRelocatable(NonTriviallyRelocatable const& other) : value_(other.value_) {}
98 TEST_CONSTEXPR NonTriviallyRelocatable(NonTriviallyRelocatable&& other) : value_(other.value_) {}
99 TEST_CONSTEXPR_CXX14 NonTriviallyRelocatable& operator=(NonTriviallyRelocatable const& other) {
100 value_ = other.value_;
101 return *this;
102 }
103 TEST_CONSTEXPR_CXX14 NonTriviallyRelocatable& operator=(NonTriviallyRelocatable&& other) {
104 value_ = other.value_;
105 return *this;
106 }
107
108 TEST_CONSTEXPR_CXX14 friend bool operator==(NonTriviallyRelocatable const& a, NonTriviallyRelocatable const& b) {
109 return a.value_ == b.value_;
110 }
111};
112LIBCPP_STATIC_ASSERT(!std::__libcpp_is_trivially_relocatable<NonTriviallyRelocatable>::value, "");
113
114#endif // TEST_STD_CONTAINERS_SEQUENCES_VECTOR_VECTOR_MODIFIERS_COMMON_H
115

source code of libcxx/test/std/containers/sequences/vector/vector.modifiers/common.h