]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.dynamic/new.delete/new.delete.single/new_align_val_t_nothrow.pass.cpp
Vendor import of libc++ trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / std / language.support / support.dynamic / new.delete / new.delete.single / new_align_val_t_nothrow.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
12 // Aligned allocation was not provided before macosx10.12 and as a result we
13 // get availability errors when the deployment target is older than macosx10.13.
14 // However, AppleClang 10 (and older) don't trigger availability errors.
15 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.12
16 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.11
17 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.10
18 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.9
19 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.8
20 // XFAIL: !(apple-clang-9 || apple-clang-10) && availability=macosx10.7
21
22 // On AppleClang 10 (and older), instead of getting an availability failure
23 // like above, we get a link error when we link against a dylib that does
24 // not export the aligned allocation functions.
25 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.12
26 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.11
27 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.10
28 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.9
29 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.8
30 // XFAIL: (apple-clang-9 || apple-clang-10) && with_system_cxx_lib=macosx10.7
31
32 // asan and msan will not call the new handler.
33 // UNSUPPORTED: sanitizer-new-delete
34
35 // FIXME turn this into an XFAIL
36 // UNSUPPORTED: no-aligned-allocation && !gcc
37
38 // On Windows libc++ doesn't provide its own definitions for new/delete
39 // but instead depends on the ones in VCRuntime. However VCRuntime does not
40 // yet provide aligned new/delete definitions so this test fails to compile/link.
41 // XFAIL: LIBCXX-WINDOWS-FIXME
42
43 // test operator new (nothrow)
44
45 #include <new>
46 #include <cstddef>
47 #include <cstdint>
48 #include <cassert>
49 #include <limits>
50
51 #include "test_macros.h"
52
53 constexpr auto OverAligned = __STDCPP_DEFAULT_NEW_ALIGNMENT__ * 2;
54
55 int new_handler_called = 0;
56
57 void my_new_handler()
58 {
59     ++new_handler_called;
60     std::set_new_handler(0);
61 }
62
63 bool A_constructed = false;
64
65 struct alignas(OverAligned) A
66 {
67     A() {A_constructed = true;}
68     ~A() {A_constructed = false;}
69 };
70
71 void test_max_alloc() {
72     std::set_new_handler(my_new_handler);
73     auto do_test = []() {
74         void* vp = operator new (std::numeric_limits<std::size_t>::max(),
75                                  std::align_val_t(OverAligned),
76                                  std::nothrow);
77         assert(new_handler_called == 1);
78         assert(vp == 0);
79     };
80 #ifndef TEST_HAS_NO_EXCEPTIONS
81     try
82     {
83         do_test();
84     }
85     catch (...)
86     {
87         assert(false);
88     }
89 #else
90     do_test();
91 #endif
92 }
93
94 int main()
95 {
96     {
97         A* ap = new(std::nothrow) A;
98         assert(ap);
99         assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0);
100         assert(A_constructed);
101         delete ap;
102         assert(!A_constructed);
103     }
104     {
105         test_max_alloc();
106     }
107 }