]>
Commit | Line | Data |
---|---|---|
9a0b518a | 1 | // { dg-options "-std=gnu++20 -fno-lifetime-dse -O0" } |
e7a0af84 | 2 | // { dg-do run { target c++2a } } |
dec1eb4c | 3 | // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } } |
7cc9022f | 4 | // { dg-require-effective-target hosted } |
e7a0af84 | 5 | |
83ffe9cd | 6 | // Copyright (C) 2020-2023 Free Software Foundation, Inc. |
e7a0af84 JW |
7 | // |
8 | // This file is part of the GNU ISO C++ Library. This library is free | |
9 | // software; you can redistribute it and/or modify it under the | |
10 | // terms of the GNU General Public License as published by the | |
11 | // Free Software Foundation; either version 3, or (at your option) | |
12 | // any later version. | |
13 | ||
14 | // This library is distributed in the hope that it will be useful, | |
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | // GNU General Public License for more details. | |
18 | ||
19 | // You should have received a copy of the GNU General Public License along | |
20 | // with this library; see the file COPYING3. If not see | |
21 | // <http://www.gnu.org/licenses/>. | |
22 | ||
23 | // C++20 20.11.1.5 unique_ptr creation [unique.ptr.create] | |
24 | ||
25 | #include <memory> | |
9a0b518a JW |
26 | |
27 | #ifndef __cpp_lib_smart_ptr_for_overwrite | |
28 | # error "Feature-test macro for make_unique_for_overwrite missing in <memory>" | |
29 | #elif __cpp_lib_smart_ptr_for_overwrite < 202002L | |
30 | # error "Feature-test macro for make_unique_for_overwrite has wrong value in <memory>" | |
31 | #endif | |
32 | ||
e7a0af84 JW |
33 | #include <cstdlib> |
34 | #include <cstring> | |
35 | #include <testsuite_hooks.h> | |
36 | ||
37 | void* operator new(std::size_t n) | |
38 | { | |
39 | void* p = std::malloc(n); | |
40 | std::memset(p, 0xaa, n); | |
41 | return p; | |
42 | } | |
43 | ||
bbcb84bb JW |
44 | void operator delete(void* p) noexcept { std::free(p); } |
45 | void operator delete(void* p, std::size_t) noexcept { std::free(p); } | |
e7a0af84 JW |
46 | |
47 | void | |
48 | test01() | |
49 | { | |
50 | std::unique_ptr<int> a = std::make_unique_for_overwrite<int>(); | |
51 | VERIFY( a != nullptr ); | |
52 | unsigned char buf[sizeof(int)]; | |
53 | std::memcpy(buf, a.get(), sizeof(buf)); | |
54 | for (unsigned char c : buf) | |
55 | VERIFY( c == 0xaa ); | |
56 | } | |
57 | ||
58 | void | |
59 | test02() | |
60 | { | |
61 | std::unique_ptr<int[]> a = std::make_unique_for_overwrite<int[]>(3); | |
62 | VERIFY( a != nullptr ); | |
63 | unsigned char buf[3 * sizeof(int)]; | |
64 | std::memcpy(buf, a.get(), sizeof(buf)); | |
65 | for (unsigned char c : buf) | |
66 | VERIFY( c == 0xaa ); | |
67 | } | |
68 | ||
9a0b518a JW |
69 | void |
70 | test03() | |
71 | { | |
72 | // Type with non-trivial initialization should still be default-initialized. | |
73 | struct NonTriv | |
74 | { | |
75 | int init = 0xbb; | |
76 | int uninit; | |
77 | }; | |
78 | std::unique_ptr<NonTriv> a = std::make_unique_for_overwrite<NonTriv>(); | |
79 | VERIFY( a->init == 0xbb ); | |
80 | std::unique_ptr<NonTriv[]> b = std::make_unique_for_overwrite<NonTriv[]>(2); | |
81 | VERIFY( b[1].init == 0xbb ); | |
82 | } | |
83 | ||
e7a0af84 JW |
84 | int |
85 | main() | |
86 | { | |
87 | test01(); | |
88 | test02(); | |
9a0b518a | 89 | test03(); |
e7a0af84 | 90 | } |