| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // test placement new array |
| 10 | |
| 11 | #include <new> |
| 12 | #include <cassert> |
| 13 | #include <cstddef> |
| 14 | |
| 15 | #include "test_macros.h" |
| 16 | |
| 17 | int A_constructed = 0; |
| 18 | |
| 19 | struct A { |
| 20 | A() { ++A_constructed; } |
| 21 | ~A() { --A_constructed; } |
| 22 | }; |
| 23 | |
| 24 | TEST_CONSTEXPR_OPERATOR_NEW void test_direct_call() { |
| 25 | assert(::operator new[](sizeof(int), &A_constructed) == &A_constructed); |
| 26 | |
| 27 | char ch = '*'; |
| 28 | assert(::operator new[](1, &ch) == &ch); |
| 29 | assert(ch == '*'); |
| 30 | } |
| 31 | |
| 32 | #ifdef __cpp_lib_constexpr_new |
| 33 | static_assert((test_direct_call(), true)); |
| 34 | #endif |
| 35 | |
| 36 | int main(int, char**) { |
| 37 | const std::size_t Size = 3; |
| 38 | // placement new might require additional space. |
| 39 | const std::size_t = 64; |
| 40 | char buf[Size * sizeof(A) + ExtraSize]; |
| 41 | |
| 42 | A* ap = new (buf) A[Size]; |
| 43 | assert((char*)ap >= buf); |
| 44 | assert((char*)ap < (buf + ExtraSize)); |
| 45 | assert(A_constructed == Size); |
| 46 | |
| 47 | test_direct_call(); |
| 48 | return 0; |
| 49 | } |
| 50 | |