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 by replacing only operator new
11 
12 #include <new>
13 #include <cstddef>
14 #include <cstdlib>
15 #include <cassert>
16 #include <limits>
17 
18 int new_called = 0;
19 
operator new(std::size_t s)20 void* operator new(std::size_t s) throw(std::bad_alloc)
21 {
22     ++new_called;
23     return std::malloc(s);
24 }
25 
operator delete(void * p)26 void  operator delete(void* p) throw()
27 {
28     --new_called;
29     std::free(p);
30 }
31 
32 bool A_constructed = false;
33 
34 struct A
35 {
AA36     A() {A_constructed = true;}
~AA37     ~A() {A_constructed = false;}
38 };
39 
main()40 int main()
41 {
42     A* ap = new (std::nothrow) A;
43     assert(ap);
44     assert(A_constructed);
45     assert(new_called);
46     delete ap;
47     assert(!A_constructed);
48     assert(!new_called);
49 }
50