new_array_replace.pass.cpp 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // test operator new[] replacement by replacing only operator new
  10. #include <new>
  11. #include <cstddef>
  12. #include <cstdlib>
  13. #include <cassert>
  14. #include <limits>
  15. int new_called = 0;
  16. void* operator new(std::size_t s) throw(std::bad_alloc)
  17. {
  18. ++new_called;
  19. return std::malloc(s);
  20. }
  21. void operator delete(void* p) throw()
  22. {
  23. --new_called;
  24. std::free(p);
  25. }
  26. int A_constructed = 0;
  27. struct A
  28. {
  29. A() {++A_constructed;}
  30. ~A() {--A_constructed;}
  31. };
  32. int main()
  33. {
  34. A* ap = new A[3];
  35. assert(ap);
  36. assert(A_constructed == 3);
  37. assert(new_called == 1);
  38. delete [] ap;
  39. assert(A_constructed == 0);
  40. assert(new_called == 0);
  41. }