Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_errors_stream_failure_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
43# include <gtest/gtest.h>
44
45# include "allocation_failure.H"
46
47# include <cstdio>
48# include <ios>
49# include <new>
50# include <ostream>
51# include <sstream>
52# include <stdexcept>
53# include <string>
54
55namespace
56{
57 struct Place_Failure {};
58
59 bool place_throws = false; // while true, the inserter below throws
60 int streams_dead = 0;
61
62 void count_a_dead_stream(const std::ios_base::event event, std::ios_base &,
63 int)
64 {
65 if (event == std::ios_base::erase_event)
66 ++streams_dead;
67 }
68}
69
70// The constructor of the stream of a message finds this inserter for the
71// strings of the place: it is declared before ah-errors.H and takes a
72// std::ostringstream, where that of the standard library takes its base.
73// The items of a macro go into an rvalue, which it does not take.
74std::ostringstream & operator << (std::ostringstream & out, const char * str)
75{
76 if (place_throws)
77 {
78 out.register_callback(count_a_dead_stream, 0);
79 throw Place_Failure();
80 }
81 static_cast<std::ostream &>(out) << str;
82 return out;
83}
84
85# include <ah-errors.H>
86
87namespace
88{
89 int evaluated = 0;
90
91 int item() { return ++evaluated; }
92
93 // A macro that throws, with two items that count their evaluations
94 void raise()
95 {
96 ah_domain_error() << "an item that is longer than a short string, "
97 << item() << " and " << item();
98 }
99
100 // The message of a std::domain_error, in an array: a string would ask
101 // for memory while a test makes the allocations fail. The message
102 // carries the path of this file: the array has room for it, however
103 // long, and for the rest
104 using Text = char[sizeof(__FILE__) + 128];
105
106 // What raise() did: 'm' if it threw std::bad_alloc; 'e' if it threw
107 // std::domain_error, whose message goes to `text`; and '?' otherwise
108 char outcome_of_raise(Text & text)
109 {
110 text[0] = '\0';
111 try
112 {
113 raise();
114 }
115 catch (const std::bad_alloc &)
116 {
117 return 'm';
118 }
119 catch (const std::domain_error & e)
120 {
121 std::snprintf(text, sizeof(text), "%s", e.what());
122 return 'e';
123 }
124 catch (...)
125 {
126 }
127 return '?';
128 }
129}
130
131// The tests need the operator new of allocation_failure.H
132struct ErrorStreamFailure : public ::testing::Test
133{
134 void SetUp() override
135 {
137 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
138 }
139};
140
141// The stream is the first thing that a macro that throws allocates.
142// Without memory for it, the macro throws std::bad_alloc in the place of
143// its exception, and has not evaluated any item.
144TEST_F(ErrorStreamFailure, WithoutMemoryForItsStreamAMacroThrowsBadAlloc)
145{
146 Text text;
147 evaluated = 0;
148 char outcome = '?';
149 size_t refused = 0;
150 {
151 Fail_At the_first(1);
152 outcome = outcome_of_raise(text);
153 refused = refused_allocations;
154 }
155 EXPECT_EQ(outcome, 'm');
156 EXPECT_EQ(refused, 1u);
157 EXPECT_EQ(evaluated, 0);
158
159 {
160 No_Memory no_memory;
161 outcome = outcome_of_raise(text);
162 refused = refused_allocations;
163 }
164 EXPECT_EQ(outcome, 'm');
165 EXPECT_EQ(refused, 1u);
166 EXPECT_EQ(evaluated, 0);
167
168 EXPECT_EQ(outcome_of_raise(text), 'e'); // with memory, its exception
169 EXPECT_EQ(evaluated, 2);
170}
171
172// Whatever allocation fails while a macro throws, the macro throws
173// std::bad_alloc or its exception, whose message is then the start of the
174// whole one, and its items were all evaluated or, without a stream, none.
175TEST_F(ErrorStreamFailure, AnyAllocationThatFailsGivesBadAllocOrTheException)
176{
177 Text text;
178 evaluated = 0;
179 ASSERT_EQ(outcome_of_raise(text), 'e');
180 const std::string whole = text;
181 const size_t at = whole.find(") | ");
182 ASSERT_NE(at, std::string::npos);
183 EXPECT_EQ(whole.substr(at + 4),
184 "an item that is longer than a short string, 1 and 2");
185
186 size_t without_memory = 0;
187 for (size_t k = 1; k <= 64; ++k)
188 {
189 evaluated = 0;
190 char outcome = '?';
191 size_t refused = 0;
192 {
193 Fail_At the_kth(k);
194 outcome = outcome_of_raise(text);
195 refused = refused_allocations;
196 }
197 if (refused == 0) // the macro asks for less than k allocations
198 {
199 EXPECT_EQ(outcome, 'e');
200 EXPECT_EQ(text, whole);
201 EXPECT_EQ(evaluated, 2);
202 EXPECT_GT(without_memory, 0u);
203 return;
204 }
205 const std::string message = text;
206 if (outcome == 'm')
207 ++without_memory;
208 else
209 {
210 EXPECT_EQ(outcome, 'e') << "allocation " << k;
211 EXPECT_EQ(whole.compare(0, message.size(), message), 0)
212 << "allocation " << k << ": " << message;
213 }
214 if (evaluated == 0)
215 EXPECT_EQ(outcome, 'm') << "allocation " << k;
216 else
217 EXPECT_EQ(evaluated, 2) << "allocation " << k;
218 }
219 ADD_FAILURE() << "a macro that throws asks for more than 64 allocations";
220}
221
222// A place that throws leaves through the constructor of the stream, which
223// frees it: the inserter above made the stream tell when it dies. The
224// macro has not evaluated any item.
225TEST_F(ErrorStreamFailure, APlaceThatThrowsFreesTheStream)
226{
227 evaluated = 0;
228 streams_dead = 0;
229 bool thrown = false;
230 place_throws = true;
231 try
232 {
233 raise();
234 }
235 catch (const Place_Failure &)
236 {
237 thrown = true;
238 }
239 place_throws = false;
240 EXPECT_TRUE(thrown);
241 EXPECT_EQ(streams_dead, 1);
242 EXPECT_EQ(evaluated, 0);
243
244 Text text;
245 EXPECT_EQ(outcome_of_raise(text), 'e'); // and the next message is whole
246 EXPECT_NE(std::string(text).find("1 and 2"), std::string::npos) << text;
247}
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
std::ostringstream & operator<<(std::ostringstream &out, const char *str)
TEST_F(ErrorStreamFailure, WithoutMemoryForItsStreamAMacroThrowsBadAlloc)
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
size_t size_t int32_t * out
Definition ca-c-api.h:120
static int * k