1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H
10 #define TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H
18 #include <type_traits>
22 #include "count_new.h"
23 #include "test_macros.h"
26 int* throw_after_n_
= nullptr;
27 throwing_t() { throw 0; }
29 explicit throwing_t(int& throw_after_n
) : throw_after_n_(&throw_after_n
) {
30 if (throw_after_n
== 0)
35 throwing_t(const throwing_t
& rhs
) : throw_after_n_(rhs
.throw_after_n_
) {
36 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
41 throwing_t
& operator=(const throwing_t
& rhs
) {
42 throw_after_n_
= rhs
.throw_after_n_
;
43 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
49 friend bool operator==(const throwing_t
& lhs
, const throwing_t
& rhs
) {
50 return lhs
.throw_after_n_
== rhs
.throw_after_n_
;
52 friend bool operator!=(const throwing_t
& lhs
, const throwing_t
& rhs
) {
53 return lhs
.throw_after_n_
!= rhs
.throw_after_n_
;
57 #if TEST_STD_VER >= 11
60 struct move_only_throwing_t
{
62 int* throw_after_n_
= nullptr;
63 bool moved_from_
= false;
65 move_only_throwing_t() = default;
67 explicit move_only_throwing_t(const T
& data
, int& throw_after_n
) : data_(data
), throw_after_n_(&throw_after_n
) {
68 if (throw_after_n
== 0)
73 explicit move_only_throwing_t(T
&& data
, int& throw_after_n
) : data_(std::move(data
)), throw_after_n_(&throw_after_n
) {
74 if (throw_after_n
== 0)
79 move_only_throwing_t(const move_only_throwing_t
&) = delete;
80 move_only_throwing_t
& operator=(const move_only_throwing_t
&) = delete;
82 move_only_throwing_t(move_only_throwing_t
&& rhs
) : data_(std::move(rhs
.data_
)), throw_after_n_(rhs
.throw_after_n_
) {
83 rhs
.throw_after_n_
= nullptr;
84 rhs
.moved_from_
= true;
85 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
90 move_only_throwing_t
& operator=(move_only_throwing_t
&& rhs
) {
93 data_
= std::move(rhs
.data_
);
94 throw_after_n_
= rhs
.throw_after_n_
;
95 rhs
.moved_from_
= true;
96 rhs
.throw_after_n_
= nullptr;
97 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
103 friend bool operator==(const move_only_throwing_t
& lhs
, const move_only_throwing_t
& rhs
) {
104 return lhs
.data_
== rhs
.data_
;
106 friend bool operator!=(const move_only_throwing_t
& lhs
, const move_only_throwing_t
& rhs
) {
107 return lhs
.data_
!= rhs
.data_
;
113 template <typename T
>
114 struct throwing_data
{
116 int* throw_after_n_
= nullptr;
117 throwing_data() { throw 0; }
119 throwing_data(const T
& data
, int& throw_after_n
) : data_(data
), throw_after_n_(&throw_after_n
) {
120 if (throw_after_n
== 0)
125 throwing_data(const throwing_data
& rhs
) : data_(rhs
.data_
), throw_after_n_(rhs
.throw_after_n_
) {
126 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
131 throwing_data
& operator=(const throwing_data
& rhs
) {
133 throw_after_n_
= rhs
.throw_after_n_
;
134 if (throw_after_n_
== nullptr || *throw_after_n_
== 0)
140 friend bool operator==(const throwing_data
& lhs
, const throwing_data
& rhs
) {
141 return lhs
.data_
== rhs
.data_
&& lhs
.throw_after_n_
== rhs
.throw_after_n_
;
143 friend bool operator!=(const throwing_data
& lhs
, const throwing_data
& rhs
) { return !(lhs
== rhs
); }
147 struct throwing_allocator
{
148 using value_type
= T
;
150 bool throw_on_copy_
= false;
152 explicit throwing_allocator(bool throw_on_ctor
= true) {
157 explicit throwing_allocator(bool throw_on_ctor
, bool throw_on_copy
) : throw_on_copy_(throw_on_copy
) {
162 throwing_allocator(const throwing_allocator
& rhs
) : throw_on_copy_(rhs
.throw_on_copy_
) {
168 throwing_allocator(const throwing_allocator
<U
>& rhs
) : throw_on_copy_(rhs
.throw_on_copy_
) {
173 T
* allocate(std::size_t n
) { return std::allocator
<T
>().allocate(n
); }
174 void deallocate(T
* ptr
, std::size_t n
) { std::allocator
<T
>().deallocate(ptr
, n
); }
177 friend bool operator==(const throwing_allocator
&, const throwing_allocator
<U
>&) {
182 template <class T
, class IterCat
>
183 struct throwing_iterator
{
184 using iterator_category
= IterCat
;
185 using difference_type
= std::ptrdiff_t;
186 using value_type
= T
;
187 using reference
= T
&;
193 explicit throwing_iterator(int i
= 0, const T
& v
= T()) : i_(i
), v_(v
) {}
195 reference
operator*() {
201 friend bool operator==(const throwing_iterator
& lhs
, const throwing_iterator
& rhs
) { return lhs
.i_
== rhs
.i_
; }
202 friend bool operator!=(const throwing_iterator
& lhs
, const throwing_iterator
& rhs
) { return lhs
.i_
!= rhs
.i_
; }
204 throwing_iterator
& operator++() {
209 throwing_iterator
operator++(int) {
216 inline void check_new_delete_called() {
217 assert(globalMemCounter
.new_called
== globalMemCounter
.delete_called
);
218 assert(globalMemCounter
.new_array_called
== globalMemCounter
.delete_array_called
);
219 assert(globalMemCounter
.aligned_new_called
== globalMemCounter
.aligned_delete_called
);
220 assert(globalMemCounter
.aligned_new_array_called
== globalMemCounter
.aligned_delete_array_called
);
223 template <class T
, typename Alloc
>
224 void use_unspecified_but_valid_state_vector(std::vector
<T
, Alloc
> const& v
) {
225 assert(v
.size() >= 0); // make sure it can be called
226 assert(v
.capacity() >= 0);
227 assert(v
.empty() || !v
.empty());
228 for (auto it
= v
.begin(); it
!= v
.end(); ++it
) {
234 static const std::array
<char, 62> letters
= {
235 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
236 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
237 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
239 inline std::string
getString(std::size_t n
, std::size_t len
) {
242 for (std::size_t i
= 0; i
< len
; ++i
)
243 s
+= letters
[(i
* i
+ n
) % letters
.size()];
247 inline std::vector
<int> getIntegerInputs(std::size_t n
) {
250 for (std::size_t i
= 0; i
< n
; ++i
)
251 v
.push_back(static_cast<int>(i
* i
+ n
));
255 inline std::vector
<std::string
> getStringInputsWithLength(std::size_t n
, std::size_t len
) {
256 std::vector
<std::string
> v
;
258 for (std::size_t i
= 0; i
< n
; ++i
)
259 v
.push_back(getString(i
, len
));
263 #endif // TEST_STD_CONTAINERS_SEQUENCES_VECTOR_COMMON_H