]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_construct/constrained.cc
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
2 // This file is part of the GNU ISO C++ Library. This library is free
3 // software; you can redistribute it and/or modify it under the
4 // terms of the GNU General Public License as published by the
5 // Free Software Foundation; either version 3, or (at your option)
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License along
14 // with this library; see the file COPYING3. If not see
15 // <http://www.gnu.org/licenses/>.
17 // { dg-options "-std=gnu++2a" }
18 // { dg-do run { target c++2a } }
30 #include <testsuite_hooks.h>
31 #include <testsuite_iterators.h>
33 using __gnu_test::test_forward_range
;
35 namespace ranges
= std::ranges
;
41 static_assert(std::default_initializable
<T
>);
42 static_assert(std::equality_comparable
<T
>);
44 for (int k
= 0; k
< 6; k
++)
46 constexpr int size
= 1024;
47 auto buffer
= std::unique_ptr
<char[]>(new char[sizeof(T
)*size
]);
48 std::span
<T
> rx((T
*)buffer
.get(), size
);
51 if constexpr (std::is_fundamental_v
<T
>)
53 std::memset(&t
, 0xCC, sizeof(t
));
59 i
= ranges::uninitialized_default_construct(rx
.begin(), rx
.end());
61 i
= ranges::uninitialized_default_construct(rx
);
63 i
= ranges::uninitialized_default_construct_n(rx
.begin(), 1024);
64 else if constexpr (std::is_fundamental_v
<T
>)
67 i
= ranges::uninitialized_default_construct(rx
.begin(), rx
.end());
69 i
= ranges::uninitialized_default_construct(std::as_const(rx
));
71 i
= ranges::uninitialized_default_construct_n(rx
.begin(), 1024);
75 VERIFY( i
== rx
.end() );
76 VERIFY( ranges::find_if(rx
, [&t
](const T
& v
) { return t
!= v
; }) == i
);
84 static constexpr int limit
= 67;
85 static inline int construct_count
= 0;
86 static inline int destruct_count
= 0;
94 if (construct_count
>= limit
)
108 template<bool test_sized
>
112 constexpr int size
= 100;
113 auto buffer
= std::unique_ptr
<char[]>(new char[sizeof(X
)*size
]);
114 test_forward_range
<X
> rx((X
*)buffer
.get(), (X
*)buffer
.get() + size
);
117 X::construct_count
= 0;
118 X::destruct_count
= 0;
119 if constexpr (test_sized
)
120 ranges::uninitialized_default_construct_n(rx
.begin(), size
);
122 ranges::uninitialized_default_construct(rx
);
123 VERIFY( false && "exception not thrown" );
125 catch (const X::exception
&)
127 VERIFY( X::construct_count
== X::limit
);
128 VERIFY( X::destruct_count
== X::limit
);
140 test01
<std::vector
<char>>();
141 test01
<std::string
>();
142 test01
<std::deque
<double>>();
143 test01
<std::list
<std::vector
<std::deque
<double>>>>();
144 test01
<std::unique_ptr
<std::string
>>();