]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.dynamic/new.delete/new.delete.array/new_array_nothrow_replace.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / language.support / support.dynamic / new.delete / new.delete.array / new_array_nothrow_replace.pass.cpp
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 by replacing only operator new
11
12 // UNSUPPORTED: sanitizer-new-delete
13
14 #include <new>
15 #include <cstddef>
16 #include <cstdlib>
17 #include <cassert>
18 #include <limits>
19
20 #include "test_macros.h"
21
22 int new_called = 0;
23
24 void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc)
25 {
26     ++new_called;
27     void* ret = std::malloc(s);
28     if (!ret) std::abort(); // placate MSVC's unchecked malloc warning
29     return  ret;
30 }
31
32 void  operator delete(void* p) TEST_NOEXCEPT
33 {
34     --new_called;
35     std::free(p);
36 }
37
38 volatile int A_constructed = 0;
39
40 struct A
41 {
42     A() {++A_constructed;}
43     ~A() {--A_constructed;}
44 };
45
46 A* volatile ap;
47
48 int main()
49 {
50     ap = new (std::nothrow) A[3];
51     assert(ap);
52     assert(A_constructed == 3);
53     assert(new_called);
54     delete [] ap;
55     assert(A_constructed == 0);
56     assert(!new_called);
57 }