]> 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 r302418:
[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 // XFAIL: with_system_cxx_lib=macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
16 // XFAIL: with_system_cxx_lib=macosx10.7
17 // XFAIL: with_system_cxx_lib=macosx10.8
18
19 // asan and msan will not call the new handler.
20 // UNSUPPORTED: sanitizer-new-delete
21
22 // FIXME turn this into an XFAIL
23 // UNSUPPORTED: no-aligned-allocation
24
25 // On Windows libc++ doesn't provide its own definitions for new/delete
26 // but instead depends on the ones in VCRuntime. However VCRuntime does not
27 // yet provide aligned new/delete definitions so this test fails to compile/link.
28 // XFAIL: LIBCXX-WINDOWS-FIXME
29
30 // test operator new (nothrow)
31
32 #include <new>
33 #include <cstddef>
34 #include <cstdint>
35 #include <cassert>
36 #include <limits>
37
38 #include "test_macros.h"
39
40 constexpr auto OverAligned = alignof(std::max_align_t) * 2;
41
42 int new_handler_called = 0;
43
44 void my_new_handler()
45 {
46     ++new_handler_called;
47     std::set_new_handler(0);
48 }
49
50 bool A_constructed = false;
51
52 struct alignas(OverAligned) A
53 {
54     A() {A_constructed = true;}
55     ~A() {A_constructed = false;}
56 };
57
58 void test_max_alloc() {
59     std::set_new_handler(my_new_handler);
60     auto do_test = []() {
61         void* vp = operator new (std::numeric_limits<std::size_t>::max(),
62                                  std::align_val_t(OverAligned),
63                                  std::nothrow);
64         assert(new_handler_called == 1);
65         assert(vp == 0);
66     };
67 #ifndef TEST_HAS_NO_EXCEPTIONS
68     try
69     {
70         do_test();
71     }
72     catch (...)
73     {
74         assert(false);
75     }
76 #else
77     do_test();
78 #endif
79 }
80
81 int main()
82 {
83     {
84         A* ap = new(std::nothrow) A;
85         assert(ap);
86         assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0);
87         assert(A_constructed);
88         delete ap;
89         assert(!A_constructed);
90     }
91     {
92         test_max_alloc();
93     }
94 }