libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

new_nothrow.pass.cpp (1272B)


      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 // asan and msan will not call the new handler.
     13 // UNSUPPORTED: sanitizer-new-delete
     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 bool A_constructed = false;
     31 
     32 struct A
     33 {
     34     A() {A_constructed = true;}
     35     ~A() {A_constructed = false;}
     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* 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;
     56     assert(ap);
     57     assert(A_constructed);
     58     delete ap;
     59     assert(!A_constructed);
     60 }