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[]
     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 int A_constructed = 0;
     26 
     27 struct A
     28 {
     29     A() {++A_constructed;}
     30     ~A() {--A_constructed;}
     31 };
     32 
     33 int main()
     34 {
     35     std::set_new_handler(new_handler);
     36     try
     37     {
     38         void*volatile vp = operator new[] (std::numeric_limits<std::size_t>::max());
     39         assert(false);
     40     }
     41     catch (std::bad_alloc&)
     42     {
     43         assert(new_handler_called == 1);
     44     }
     45     catch (...)
     46     {
     47         assert(false);
     48     }
     49     A* ap = new A[3];
     50     assert(ap);
     51     assert(A_constructed == 3);
     52     delete [] ap;
     53     assert(A_constructed == 0);
     54 }
     55