]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_default_construct/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / 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)
6 // any later version.
7
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.
12
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/>.
16
17 // { dg-options "-std=gnu++2a" }
18 // { dg-do run { target c++2a } }
19
20 #include <algorithm>
21 #include <cstring>
22 #include <deque>
23 #include <list>
24 #include <memory>
25 #include <span>
26 #include <string>
27 #include <vector>
28 #include <utility>
29
30 #include <testsuite_hooks.h>
31 #include <testsuite_iterators.h>
32
33 using __gnu_test::test_forward_range;
34
35 namespace ranges = std::ranges;
36
37 template<typename T>
38 void
39 test01()
40 {
41 static_assert(std::default_initializable<T>);
42 static_assert(std::equality_comparable<T>);
43
44 for (int k = 0; k < 6; k++)
45 {
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);
49
50 T t;
51 if constexpr (std::is_fundamental_v<T>)
52 {
53 std::memset(&t, 0xCC, sizeof(t));
54 ranges::fill(rx, t);
55 }
56
57 auto i = rx.begin();
58 if (k == 0)
59 i = ranges::uninitialized_default_construct(rx.begin(), rx.end());
60 else if (k == 1)
61 i = ranges::uninitialized_default_construct(rx);
62 else if (k == 2)
63 i = ranges::uninitialized_default_construct_n(rx.begin(), 1024);
64 else if constexpr (std::is_fundamental_v<T>)
65 continue;
66 else if (k == 3)
67 i = ranges::uninitialized_default_construct(rx.begin(), rx.end());
68 else if (k == 4)
69 i = ranges::uninitialized_default_construct(std::as_const(rx));
70 else if (k == 5)
71 i = ranges::uninitialized_default_construct_n(rx.begin(), 1024);
72 else
73 __builtin_abort();
74
75 VERIFY( i == rx.end() );
76 VERIFY( ranges::find_if(rx, [&t](const T& v) { return t != v; }) == i );
77
78 ranges::destroy(rx);
79 }
80 }
81
82 struct X
83 {
84 static constexpr int limit = 67;
85 static inline int construct_count = 0;
86 static inline int destruct_count = 0;
87
88 struct exception {};
89
90 bool live = false;
91
92 X()
93 {
94 if (construct_count >= limit)
95 throw exception{};
96 construct_count++;
97 live = true;
98 }
99
100 ~X()
101 {
102 VERIFY( live );
103 live = false;
104 destruct_count++;
105 }
106 };
107
108 template<bool test_sized>
109 void
110 test02()
111 {
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);
115 try
116 {
117 X::construct_count = 0;
118 X::destruct_count = 0;
119 if constexpr (test_sized)
120 ranges::uninitialized_default_construct_n(rx.begin(), size);
121 else
122 ranges::uninitialized_default_construct(rx);
123 VERIFY( false && "exception not thrown" );
124 }
125 catch (const X::exception&)
126 {
127 VERIFY( X::construct_count == X::limit );
128 VERIFY( X::destruct_count == X::limit );
129 }
130 }
131
132 int
133 main()
134 {
135 test01<char>();
136 test01<int>();
137 test01<long long>();
138 test01<float>();
139 test01<double>();
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>>();
145
146 test02<false>();
147 test02<true>();
148 }