]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.dynamic/new.delete/new.delete.single/new_align_val_t_nothrow_replace.pass.cpp
Vendor import of libc++ trunk r302418:
[FreeBSD/FreeBSD.git] / test / std / language.support / support.dynamic / new.delete / new.delete.single / new_align_val_t_nothrow_replace.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // UNSUPPORTED: sanitizer-new-delete
12
13 // XFAIL: with_system_cxx_lib=macosx10.12
14 // XFAIL: with_system_cxx_lib=macosx10.11
15 // XFAIL: with_system_cxx_lib=macosx10.10
16 // XFAIL: with_system_cxx_lib=macosx10.9
17 // XFAIL: with_system_cxx_lib=macosx10.7
18 // XFAIL: with_system_cxx_lib=macosx10.8
19
20 // XFAIL: no-aligned-allocation
21
22 // On Windows libc++ doesn't provide its own definitions for new/delete
23 // but instead depends on the ones in VCRuntime. However VCRuntime does not
24 // yet provide aligned new/delete definitions so this test fails.
25 // XFAIL: LIBCXX-WINDOWS-FIXME
26
27 // test operator new nothrow by replacing only operator new
28
29 #include <new>
30 #include <cstddef>
31 #include <cstdlib>
32 #include <cassert>
33 #include <limits>
34
35 #include "test_macros.h"
36
37 constexpr auto OverAligned = alignof(std::max_align_t) * 2;
38
39 bool A_constructed = false;
40
41 struct alignas(OverAligned) A
42 {
43     A() {A_constructed = true;}
44     ~A() {A_constructed = false;}
45 };
46
47 bool B_constructed = false;
48
49 struct B {
50   std::max_align_t  member;
51   B() { B_constructed = true; }
52   ~B() { B_constructed = false; }
53 };
54
55 int new_called = 0;
56 alignas(OverAligned) char Buff[OverAligned * 2];
57
58 void* operator new(std::size_t s, std::align_val_t a) TEST_THROW_SPEC(std::bad_alloc)
59 {
60     assert(!new_called);
61     assert(s <= sizeof(Buff));
62     assert(static_cast<std::size_t>(a) == OverAligned);
63     ++new_called;
64     return Buff;
65 }
66
67 void  operator delete(void* p, std::align_val_t a) TEST_NOEXCEPT
68 {
69     assert(p == Buff);
70     assert(static_cast<std::size_t>(a) == OverAligned);
71     assert(new_called);
72     --new_called;
73 }
74
75
76 int main()
77 {
78     {
79         A* ap = new (std::nothrow) A;
80         assert(ap);
81         assert(A_constructed);
82         assert(new_called);
83         delete ap;
84         assert(!A_constructed);
85         assert(!new_called);
86     }
87     {
88         B* bp = new (std::nothrow) B;
89         assert(bp);
90         assert(B_constructed);
91         assert(!new_called);
92         delete bp;
93         assert(!new_called);
94         assert(!B_constructed);
95     }
96 }