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  // test operator new (nothrow)
11 
12 #include <new>
13 #include <cstddef>
14 #include <cassert>
15 #include <limits>
16 
17 int new_handler_called = 0;
18 
19 void new_handler()
20 {
21     ++new_handler_called;
22     std::set_new_handler(0);
23 }
24 
25 bool A_constructed = false;
26 
27 struct A
28 {
29     A() {A_constructed = true;}
30     ~A() {A_constructed = false;}
31 };
32 
33 int main()
34 {
35     std::set_new_handler(new_handler);
36     try
37     {
38         void* vp = operator new (std::numeric_limits<std::size_t>::max(), std::nothrow);
39         assert(new_handler_called == 1);
40         assert(vp == 0);
41     }
42     catch (...)
43     {
44         assert(false);
45     }
46     A* ap = new(std::nothrow) A;
47     assert(ap);
48     assert(A_constructed);
49     delete ap;
50     assert(!A_constructed);
51 }
52