libcxx

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

new_array_replace.pass.cpp (1259B)


      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[] replacement by replacing only operator new
     11 
     12 // UNSUPPORTED: sanitizer-new-delete
     13 // XFAIL: libcpp-no-vcruntime
     14 
     15 
     16 #include <new>
     17 #include <cstddef>
     18 #include <cstdlib>
     19 #include <cassert>
     20 #include <limits>
     21 
     22 #include "test_macros.h"
     23 
     24 int new_called = 0;
     25 
     26 void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
     27 {
     28     ++new_called;
     29     void* ret = std::malloc(s);
     30     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
     31     return  ret;
     32 }
     33 
     34 void  operator delete(void* p) TEST_NOEXCEPT
     35 {
     36     --new_called;
     37     std::free(p);
     38 }
     39 
     40 int A_constructed = 0;
     41 
     42 struct A
     43 {
     44     A() {++A_constructed;}
     45     ~A() {--A_constructed;}
     46 };
     47 
     48 int main()
     49 {
     50     A *ap = new A[3];
     51     DoNotOptimize(ap);
     52     assert(ap);
     53     assert(A_constructed == 3);
     54     assert(new_called == 1);
     55     delete [] ap;
     56     DoNotOptimize(ap);
     57     assert(A_constructed == 0);
     58     assert(new_called == 0);
     59 }