xfail dg-final "Sunk statements: 5" on hppa*64*-*-*
[official-gcc.git] / libstdc++-v3 / testsuite / 18_support / new_nothrow.cc
bloba3251f5ad64e85fd9ae8b590f5c148422e74779c
1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run }
19 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
20 // { dg-require-effective-target hosted }
22 #include <new>
23 #include <stdlib.h>
24 #include <testsuite_hooks.h>
26 // PR libstdc++/68210
28 struct MyBadAlloc: std::bad_alloc { };
30 static bool new_fail;
31 static bool bad_alloc_thrown;
32 static unsigned new_called;
33 static unsigned delete_called;
34 static unsigned new_vec_called;
35 static unsigned delete_vec_called;
36 static unsigned new_handler_called;
38 static void new_handler ()
40 if (new_handler_called++)
41 throw MyBadAlloc ();
44 void* operator new (size_t n)
46 static size_t cntr;
48 ++new_called;
50 for ( ; ; ) {
51 if (void *p = new_fail ? 0 : malloc (n + sizeof n)) {
52 *static_cast<size_t*>(p) = ++cntr;
53 return static_cast<size_t*>(p) + 1;
56 if (std::new_handler h = std::set_new_handler (0)) {
57 std::set_new_handler (h);
58 h ();
60 else {
61 bad_alloc_thrown = true;
62 throw MyBadAlloc ();
67 #if __cplusplus >= 201103L
68 #define NOEXCEPT noexcept
69 #else
70 #define NOEXCEPT
71 #endif
73 void operator delete (void *p) NOEXCEPT
75 ++delete_called;
76 if (p)
77 free (static_cast<size_t*>(p) - 1);
80 void* operator new[] (size_t n)
82 ++new_vec_called;
83 return operator new(n);
86 void operator delete[] (void *p) NOEXCEPT
88 ++delete_vec_called;
89 operator delete(p);
92 #if __cplusplus >= 201402L
93 void operator delete (void *p, std::size_t) noexcept
95 ::operator delete(p);
97 void operator delete[] (void *p, std::size_t) noexcept
99 ::operator delete[](p);
101 #endif
103 void init()
105 new_fail = false;
106 new_called = 0;
107 delete_called = 0;
108 new_vec_called = 0;
109 delete_vec_called = 0;
110 new_handler_called = 0;
111 std::set_new_handler (0);
114 void
115 test01()
117 init ();
119 void *p = operator new (1, std::nothrow);
121 VERIFY (p != 0);
122 VERIFY (1 == new_called);
123 VERIFY (0 == new_handler_called);
124 VERIFY (!bad_alloc_thrown);
126 operator delete (p, std::nothrow);
127 VERIFY( 1 == delete_called );
129 new_fail = true;
130 p = operator new (1, std::nothrow);
132 VERIFY (0 == p);
133 VERIFY (2 == new_called);
134 VERIFY (0 == new_handler_called);
135 VERIFY (bad_alloc_thrown);
137 new_fail = true;
138 bad_alloc_thrown = false;
139 std::set_new_handler (new_handler);
140 p = operator new (1, std::nothrow);
142 VERIFY (0 == p);
143 VERIFY (3 == new_called);
144 VERIFY (2 == new_handler_called);
145 VERIFY (!bad_alloc_thrown);
148 void
149 test02()
151 init ();
153 void *p = operator new[] (1, std::nothrow);
155 VERIFY (p != 0);
156 VERIFY (1 == new_called);
157 VERIFY (1 == new_vec_called);
158 VERIFY (0 == new_handler_called);
159 VERIFY (!bad_alloc_thrown);
161 operator delete[] (p, std::nothrow);
162 VERIFY( 1 == delete_called );
163 VERIFY( 1 == delete_vec_called );
165 new_fail = true;
166 p = operator new[] (1, std::nothrow);
168 VERIFY (0 == p);
169 VERIFY (2 == new_called);
170 VERIFY (2 == new_vec_called);
171 VERIFY (0 == new_handler_called);
172 VERIFY (bad_alloc_thrown);
174 new_fail = true;
175 bad_alloc_thrown = false;
176 std::set_new_handler (new_handler);
177 p = operator new[] (1, std::nothrow);
179 VERIFY (0 == p);
180 VERIFY (3 == new_called);
181 VERIFY (3 == new_vec_called);
182 VERIFY (2 == new_handler_called);
183 VERIFY (!bad_alloc_thrown);
187 int main()
189 test01();
190 test02();