]> 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 r290819:
[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 // asan and msan will not call the new handler.
13 // UNSUPPORTED: sanitizer-new-delete
14
15 // FIXME turn this into an XFAIL
16 // UNSUPPORTED: no-aligned-allocation
17
18 // test operator new (nothrow)
19
20 #include <new>
21 #include <cstddef>
22 #include <cstdint>
23 #include <cassert>
24 #include <limits>
25
26 #include "test_macros.h"
27
28 constexpr auto OverAligned = alignof(std::max_align_t) * 2;
29
30 int new_handler_called = 0;
31
32 void new_handler()
33 {
34     ++new_handler_called;
35     std::set_new_handler(0);
36 }
37
38 bool A_constructed = false;
39
40 struct alignas(OverAligned) A
41 {
42     A() {A_constructed = true;}
43     ~A() {A_constructed = false;}
44 };
45
46 void test_max_alloc() {
47     std::set_new_handler(new_handler);
48     auto do_test = []() {
49         void* vp = operator new (std::numeric_limits<std::size_t>::max(),
50                                  std::align_val_t(OverAligned),
51                                  std::nothrow);
52         assert(new_handler_called == 1);
53         assert(vp == 0);
54     };
55 #ifndef TEST_HAS_NO_EXCEPTIONS
56     try
57     {
58         do_test();
59     }
60     catch (...)
61     {
62         assert(false);
63     }
64 #else
65     do_test();
66 #endif
67 }
68
69 int main()
70 {
71     {
72         A* ap = new(std::nothrow) A;
73         assert(ap);
74         assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0);
75         assert(A_constructed);
76         delete ap;
77         assert(!A_constructed);
78     }
79     {
80         test_max_alloc();
81     }
82 }