]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/specialized_algorithms/memory_management_tools/1.cc
1 // Copyright (C) 2016-2020 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-options "-std=gnu++17" }
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
33 ~DelCount() { ++del_count
; }
40 if (++ctor_count
== throw_after
) {
41 std::ostringstream os
;
42 os
<< "ThrowAfterN(), ctor_count: " << ctor_count
43 << " throw_after: " << throw_after
<< std::endl
;
44 throw std::runtime_error(os
.str());
47 ThrowAfterN(ThrowAfterN
&&)
49 if (++ctor_count
== throw_after
) {
50 std::ostringstream os
;
51 os
<< "ThrowAfterN(), ctor_count: " << ctor_count
52 << " throw_after: " << throw_after
<< std::endl
;
53 throw std::runtime_error(os
.str());
60 using FwdIteratorRange
61 = __gnu_test::test_container
<T
, __gnu_test::forward_iterator_wrapper
>;
65 char test_data
[] = "123456";
66 FwdIteratorRange
<char> r(test_data
);
67 std::uninitialized_default_construct(std::begin(r
), std::end(r
));
68 VERIFY(std::string(test_data
) == "123456");
73 char test_data
[] = "123456";
74 FwdIteratorRange
<char> r(test_data
);
75 std::uninitialized_value_construct(std::begin(r
), std::end(r
));
76 VERIFY(std::string(test_data
, 6) == std::string("\0\0\0\0\0\0", 6));
81 char test_data
[] = "123456";
82 FwdIteratorRange
<char> r(test_data
);
83 auto end
= std::uninitialized_default_construct_n(std::begin(r
), 6);
84 VERIFY(std::string(test_data
) == "123456");
85 VERIFY( end
== std::next(r
.begin(), 6) );
90 char test_data
[] = "123456";
91 FwdIteratorRange
<char> r(test_data
);
92 auto end
= std::uninitialized_value_construct_n(std::begin(r
), 5);
93 VERIFY(std::string(test_data
, 6) == std::string("\0\0\0\0\0" "6", 6));
94 VERIFY( end
== std::next(r
.begin(), 5) );
100 DelCount
* x
= (DelCount
*)malloc(sizeof(DelCount
));
102 std::destroy_at(&x
[0]);
103 VERIFY(del_count
== 1);
111 DelCount
* x
= (DelCount
*)malloc(sizeof(DelCount
)*10);
112 for (int i
= 0; i
< 10; ++i
) new (x
+i
) DelCount
;
113 std::destroy(x
, x
+10);
114 VERIFY(del_count
== 10);
122 DelCount
* x
= (DelCount
*)malloc(sizeof(DelCount
)*10);
123 for (int i
= 0; i
< 10; ++i
) new (x
+i
) DelCount
;
124 auto end
= std::destroy_n(x
, 10);
125 VERIFY(del_count
== 10);
126 VERIFY( end
== x
+ 10 );
133 MoveOnly() : val(-1) { }
134 MoveOnly(MoveOnly
&& m
) : val(m
.val
) { m
.val
= -1; }
141 for (int i
= 0; i
< 10; ++i
) source
[i
].val
= i
;
142 FwdIteratorRange
<MoveOnly
> src(source
);
143 MoveOnly
* target
= (MoveOnly
*)malloc(sizeof(MoveOnly
)*10);
144 FwdIteratorRange
<MoveOnly
> tgt(target
, target
+10);
145 auto end
= std::uninitialized_move(src
.begin(), src
.end(), tgt
.begin());
146 VERIFY( end
== std::next(tgt
.begin(), 10) );
147 for (const auto& x
: source
) VERIFY( x
.val
== -1 );
148 for (int i
= 0; i
< 10; ++i
) VERIFY( target
[i
].val
== i
);
149 auto end2
= std::destroy_n(tgt
.begin(), 10);
150 VERIFY( end2
== std::next(tgt
.begin(), 10) );
157 for (int i
= 0; i
< 10; ++i
) source
[i
].val
= i
;
158 FwdIteratorRange
<MoveOnly
> src(source
);
159 MoveOnly
* target
= (MoveOnly
*)malloc(sizeof(MoveOnly
)*10);
160 FwdIteratorRange
<MoveOnly
> tgt(target
, target
+10);
161 auto end
= std::uninitialized_move_n(src
.begin(), 10, tgt
.begin());
162 VERIFY( end
.first
== std::next(src
.begin(), 10) );
163 VERIFY( end
.second
== std::next(tgt
.begin(), 10) );
164 for (const auto& x
: source
) VERIFY( x
.val
== -1 );
165 for (int i
= 0; i
< 10; ++i
) VERIFY( target
[i
].val
== i
);
166 auto end2
= std::destroy_n(tgt
.begin(), 10);
167 VERIFY( end2
== std::next(tgt
.begin(), 10) );
173 char* x
= (char*)malloc(sizeof(char)*10);
174 for (int i
= 0; i
< 10; ++i
) new (x
+i
) char;
175 FwdIteratorRange
<char> r(x
, x
+10);
176 std::destroy(r
.begin(), r
.end());
182 char* x
= (char*)malloc(sizeof(char)*10);
183 for (int i
= 0; i
< 10; ++i
) new (x
+i
) char;
184 FwdIteratorRange
<char> r(x
, x
+10);
185 auto end
= std::destroy_n(r
.begin(), 10);
186 VERIFY( end
== std::next(r
.begin(), 10) );
195 ThrowAfterN
* target
=
196 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
198 std::uninitialized_default_construct(target
, target
+10);
202 VERIFY(ctor_count
== 5);
203 VERIFY(del_count
== 5);
214 ThrowAfterN
* target
=
215 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
217 std::uninitialized_value_construct(target
, target
+10);
221 VERIFY(ctor_count
== 5);
222 VERIFY(del_count
== 5);
233 ThrowAfterN
* target
=
234 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
236 std::uninitialized_default_construct_n(target
, 10);
240 VERIFY(ctor_count
== 5);
241 VERIFY(del_count
== 5);
252 ThrowAfterN
* target
=
253 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
255 std::uninitialized_value_construct_n(target
, 10);
259 VERIFY(ctor_count
== 5);
260 VERIFY(del_count
== 5);
268 std::vector
<ThrowAfterN
> source(10);
273 ThrowAfterN
* target
=
274 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
276 std::uninitialized_move(source
.begin(), source
.end(), target
);
280 VERIFY(ctor_count
== 5);
281 VERIFY(del_count
== 5);
289 std::vector
<ThrowAfterN
> source(10);
293 ThrowAfterN
* target
=
294 (ThrowAfterN
*)malloc(sizeof(ThrowAfterN
)*10);
296 std::uninitialized_move_n(source
.begin(), 10, target
);
300 VERIFY(ctor_count
== 5);
301 VERIFY(del_count
== 5);
309 char test_source
[] = "123456";
310 char test_target
[] = "000000";
311 std::uninitialized_move(std::begin(test_source
),
312 std::end(test_source
),
314 VERIFY(std::string(test_target
) == "123456");
319 char test_source
[] = "123456";
320 char test_target
[] = "000000";
321 auto end
= std::uninitialized_move_n(std::begin(test_source
),
324 VERIFY(std::string(test_target
) == "123456");
325 VERIFY( end
.first
== test_source
+ 6 );
326 VERIFY( end
.second
== test_target
+ 6 );