]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.dynamic/new.delete/new.delete.array/new_align_val_t.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / language.support / support.dynamic / new.delete / new.delete.array / new_align_val_t.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 change this to XFAIL.
16 // UNSUPPORTED: no-aligned-allocation
17
18 // test operator new
19
20 #include <new>
21 #include <cstddef>
22 #include <cassert>
23 #include <cstdint>
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 int A_constructed = 0;
39
40 struct alignas(OverAligned) A
41 {
42     A() { ++A_constructed;}
43     ~A() { --A_constructed;}
44 };
45
46 void test_throw_max_size() {
47 #ifndef TEST_HAS_NO_EXCEPTIONS
48     std::set_new_handler(new_handler);
49     try
50     {
51         void* vp = operator new[] (std::numeric_limits<std::size_t>::max(),
52                                    static_cast<std::align_val_t>(32));
53         ((void)vp);
54         assert(false);
55     }
56     catch (std::bad_alloc&)
57     {
58         assert(new_handler_called == 1);
59     }
60     catch (...)
61     {
62         assert(false);
63     }
64 #endif
65 }
66
67 int main()
68 {
69     {
70         A* ap = new A[2];
71         assert(ap);
72         assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0);
73         assert(A_constructed == 2);
74         delete [] ap;
75         assert(A_constructed == 0);
76     }
77     {
78         test_throw_max_size();
79     }
80 }