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