6ef24cc7ea07fc59a11cced82863e8707c272b1f
[gcc.git] /
1 // Copyright (C) 2020 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
29 #include <testsuite_hooks.h>
30 #include <testsuite_iterators.h>
31
32 using __gnu_test::test_forward_range;
33
34 namespace ranges = std::ranges;
35
36 template<typename T>
37 void
38 test01()
39 {
40 static_assert(std::default_initializable<T>);
41 static_assert(std::equality_comparable<T>);
42
43 for (int k = 0; k < 6; k++)
44 {
45 constexpr int size = 1024;
46 auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
47 std::span<T> rx((T *)buffer.get(), size);
48
49 T t;
50 if constexpr (std::is_fundamental_v<T>)
51 {
52 std::memset(&t, 0xCC, sizeof(t));
53 ranges::fill(rx, t);
54 }
55
56 auto i = rx.cbegin();
57 if (k == 0)
58 i = ranges::uninitialized_default_construct(rx.begin(), rx.end());
59 else if (k == 1)
60 i = ranges::uninitialized_default_construct(rx);
61 else if (k == 2)
62 i = ranges::uninitialized_default_construct_n(rx.begin(), 1024);
63 else if constexpr (std::is_fundamental_v<T>)
64 continue;
65 else if (k == 3)
66 i = ranges::uninitialized_default_construct(rx.cbegin(), rx.cend());
67 else if (k == 4)
68 i = ranges::uninitialized_default_construct(std::as_const(rx));
69 else if (k == 5)
70 i = ranges::uninitialized_default_construct_n(rx.cbegin(), 1024);
71 else
72 __builtin_abort();
73
74 VERIFY( i == rx.cend() );
75 VERIFY( ranges::find_if(rx, [&t](const T& v) { return t != v; }) == i );
76
77 ranges::destroy(rx);
78 }
79 }
80
81 struct X
82 {
83 static constexpr int limit = 67;
84 static inline int construct_count = 0;
85 static inline int destruct_count = 0;
86
87 struct exception {};
88
89 bool live = false;
90
91 X()
92 {
93 if (construct_count >= limit)
94 throw exception{};
95 construct_count++;
96 live = true;
97 }
98
99 ~X()
100 {
101 VERIFY( live );
102 live = false;
103 destruct_count++;
104 }
105 };
106
107 template<bool test_sized>
108 void
109 test02()
110 {
111 constexpr int size = 100;
112 auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
113 test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
114 try
115 {
116 X::construct_count = 0;
117 X::destruct_count = 0;
118 if constexpr (test_sized)
119 ranges::uninitialized_default_construct_n(rx.begin(), size);
120 else
121 ranges::uninitialized_default_construct(rx);
122 VERIFY( false && "exception not thrown" );
123 }
124 catch (const X::exception&)
125 {
126 VERIFY( X::construct_count == X::limit );
127 VERIFY( X::destruct_count == X::limit );
128 }
129 }
130
131 int
132 main()
133 {
134 test01<char>();
135 test01<int>();
136 test01<long long>();
137 test01<float>();
138 test01<double>();
139 test01<std::vector<char>>();
140 test01<std::string>();
141 test01<std::deque<double>>();
142 test01<std::list<std::vector<std::deque<double>>>>();
143 test01<std::unique_ptr<std::string>>();
144
145 test02<false>();
146 test02<true>();
147 }