new_array.pass.cpp 1.3 KB

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