1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: no-exceptions
10// UNSUPPORTED: sanitizer-new-delete
11
12// Libc++ when built for z/OS doesn't contain the aligned allocation functions,
13// nor does the dynamic library shipped with z/OS.
14// XFAIL: target={{.+}}-zos{{.*}}
15
16#include <new>
17#include <cassert>
18#include <limits>
19#include <cstdlib>
20
21struct construction_key {};
22struct my_bad_alloc : std::bad_alloc {
23 my_bad_alloc(const my_bad_alloc&) : self(this) { std::abort(); }
24 my_bad_alloc(construction_key) : self(this) {}
25 const my_bad_alloc* const self;
26};
27
28int new_handler_called = 0;
29
30void my_new_handler() {
31 ++new_handler_called;
32 throw my_bad_alloc(construction_key());
33}
34
35int main(int, char**) {
36 std::set_new_handler(my_new_handler);
37 try {
38 void* x = operator new(std::numeric_limits<std::size_t>::max(), static_cast<std::align_val_t>(32));
39 (void)x;
40 assert(false);
41 } catch (my_bad_alloc const& e) {
42 assert(new_handler_called == 1);
43 assert(e.self == &e);
44 } catch (...) {
45 assert(false);
46 }
47 return 0;
48}
49

source code of libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.except.pass.cpp