]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite.cc
libstdc++: Implement std::make_unique_for_overwrite
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / unique_ptr / creation / for_overwrite.cc
CommitLineData
e7a0af84
JW
1// { dg-options "-std=gnu++20" }
2// { dg-do run { target c++2a } }
3
4// Copyright (C) 2020 Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING3. If not see
19// <http://www.gnu.org/licenses/>.
20
21// C++20 20.11.1.5 unique_ptr creation [unique.ptr.create]
22
23#include <memory>
24#include <cstdlib>
25#include <cstring>
26#include <testsuite_hooks.h>
27
28void* operator new(std::size_t n)
29{
30 void* p = std::malloc(n);
31 std::memset(p, 0xaa, n);
32 return p;
33}
34
35void operator delete(void* p) { std::free(p); }
36void operator delete(void* p, std::size_t) { std::free(p); }
37
38void
39test01()
40{
41 std::unique_ptr<int> a = std::make_unique_for_overwrite<int>();
42 VERIFY( a != nullptr );
43 unsigned char buf[sizeof(int)];
44 std::memcpy(buf, a.get(), sizeof(buf));
45 for (unsigned char c : buf)
46 VERIFY( c == 0xaa );
47}
48
49void
50test02()
51{
52 std::unique_ptr<int[]> a = std::make_unique_for_overwrite<int[]>(3);
53 VERIFY( a != nullptr );
54 unsigned char buf[3 * sizeof(int)];
55 std::memcpy(buf, a.get(), sizeof(buf));
56 for (unsigned char c : buf)
57 VERIFY( c == 0xaa );
58}
59
60int
61main()
62{
63 test01();
64 test02();
65}