Home | History | Annotate | Download | only in new.delete.array
      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 // NOTE: asan and msan will not call the new handler.
     12 // UNSUPPORTED: sanitizer-new-delete
     13 
     14 
     15 #include <new>
     16 #include <cstddef>
     17 #include <cassert>
     18 #include <limits>
     19 
     20 #include "test_macros.h"
     21 
     22 int new_handler_called = 0;
     23 
     24 void my_new_handler()
     25 {
     26     ++new_handler_called;
     27     std::set_new_handler(0);
     28 }
     29 
     30 int A_constructed = 0;
     31 
     32 struct A
     33 {
     34     A() {++A_constructed;}
     35     ~A() {--A_constructed;}
     36 };
     37 
     38 int main()
     39 {
     40     std::set_new_handler(my_new_handler);
     41 #ifndef TEST_HAS_NO_EXCEPTIONS
     42     try
     43 #endif
     44     {
     45         void*volatile vp = operator new [] (std::numeric_limits<std::size_t>::max(), std::nothrow);
     46         assert(new_handler_called == 1);
     47         assert(vp == 0);
     48     }
     49 #ifndef TEST_HAS_NO_EXCEPTIONS
     50     catch (...)
     51     {
     52         assert(false);
     53     }
     54 #endif
     55     A* ap = new(std::nothrow) A[3];
     56     assert(ap);
     57     assert(A_constructed == 3);
     58     delete [] ap;
     59     assert(A_constructed == 0);
     60 }
     61