45# include <gtest/gtest.h>
55# include <string_view>
57# include <type_traits>
72 std::string place(
const int line)
74 return "(" + std::string(__FILE__) +
":" + std::to_string(line) +
") | ";
79 template <
class E,
class Raise>
80 std::string message_of(Raise && raise)
84 std::forward<Raise>(raise)();
88 EXPECT_TRUE(
typeid(e) ==
typeid(E)) <<
"thrown: " <<
typeid(e).name();
93 ADD_FAILURE() <<
"an exception of another type";
96 ADD_FAILURE() <<
"no exception";
101 std::string text_of(
const std::string &
message)
103 const size_t at =
message.find(
") | ");
104 return at == std::string::npos ?
message :
message.substr(at + 4);
107 std::string file_of(
const std::string &
message)
109 const size_t at =
message.find(
") | ");
110 if (
message.empty() or
message.front() !=
'(' or at == std::string::npos)
112 const std::string where =
message.substr(1, at - 1);
113 return where.substr(0, where.rfind(
':'));
116 bool ends_with(
const std::string & text,
const std::string & tail)
118 return text.size() >= tail.size()
and
119 text.compare(text.size() - tail.size(), tail.size(), tail) == 0;
130 return out <<
"(" << p.x <<
", " << p.y <<
")";
133 enum Color { Red, Green = 5 };
141 std::string twice(
const std::string & s) {
return s + s; }
144 std::string throw_if_odd(
const int n)
147 throw std::underflow_error(
"the evaluation of an item");
157 explicit Tracked(std::string t) : text(
std::move(t)) {}
159 ~Tracked() { text.assign(text.size(),
'#'); }
162 std::ostream &
operator << (std::ostream &
out,
const Tracked & t)
164 return out << t.text;
168 struct Throws_When_Inserted {};
170 std::ostream &
operator << (std::ostream &
out,
const Throws_When_Inserted &)
172 out <<
"half an item";
173 throw std::length_error(
"the insertion of an item");
177 std::string text_of_another_message(
const int n)
183 catch (
const std::domain_error & e)
185 return text_of(e.what());
192 int streams_watched = 0;
193 int streams_dead = 0;
195 void count_a_dead_stream(
const std::ios_base::event event, std::ios_base &,
198 if (event == std::ios_base::erase_event)
203 struct Watches_Its_Stream {};
205 std::ostream &
operator << (std::ostream &
out,
const Watches_Its_Stream &)
208 out.register_callback(count_a_dead_stream, 0);
209 return out <<
"watched";
216 int strings_taken = 0;
222 static_cast<std::ostream &
>(
out) << str;
223 return std::move(
out);
226 void raise() {
ah_range_error() <<
"an item " << 7 <<
" and another"; }
234# define EXPECT_THE_WHOLE(E, call, items) \
237 std::ostringstream expected; \
239 EXPECT_EQ(message_of<E>([&] { call items; }), \
240 place(__LINE__) + expected.str()); \
247# define EXPECT_THE_ITEMS(E, call, items) \
250 std::ostringstream expected; \
252 const std::string message = message_of<E>([&] { call items; }); \
253 EXPECT_EQ(file_of(message), __FILE__); \
254 EXPECT_EQ(text_of(message), expected.str()); \
259# define EXPECT_THE_TEXT(E, statement, text) \
262 const std::string message = message_of<E>([&] { statement; }); \
263 EXPECT_EQ(file_of(message), __FILE__); \
264 EXPECT_EQ(text_of(message), text); \
269# define EXPECT_THE_MESSAGES_OF(E, base) \
272 EXPECT_THE_WHOLE(E, base ## _if(n == 7), ITEMS); \
273 EXPECT_THE_WHOLE(E, base ## _unless(n != 7), ITEMS); \
274 EXPECT_THE_WHOLE(E, base(), ITEMS); \
275 EXPECT_THE_WHOLE(E, base ## _if_constexpr(true), ITEMS); \
276 EXPECT_THE_WHOLE(E, base ## _unless_constexpr(false), ITEMS); \
280TEST(ErrorMessage, EveryMacroGivesItsPlaceAndItsItems)
283 const std::string s =
"seven";
284# define ITEMS << "value " << n << ", " << s << ' ' << 2.5
300TEST(ErrorMessage, AMessageWithoutItemsIsItsPlace)
307TEST(ErrorMessage, TakesTheItemsThatAStreamTakes)
309 const char *
const text =
"a pointer to chars";
310 const char chars[] =
"an array of chars";
311 const std::string str =
"a string";
312 const std::string_view view =
"a view";
313 const Point point = {3, -4};
314 const Fields fields = {5, 17};
315 const int number = 42;
316 const int *
const address = &number;
317 const void *
const nothing =
nullptr;
321 << text <<
' ' << chars <<
' ' << str <<
' ' << view);
323 <<
'c' <<
static_cast<signed char>(
's')
324 <<
static_cast<unsigned char>(
'u') <<
true <<
false);
326 <<
static_cast<short>(-3) <<
' ' << 7u <<
' ' << -9l <<
' '
327 << 10ul <<
' ' << -11ll <<
' ' << 12ull);
329 << 2.5f <<
' ' << 0.1 <<
' ' << 3.0l <<
' ' << 1e300);
331 << address <<
' ' << nothing <<
' '
332 <<
static_cast<const void *
>(text));
334 << point <<
' ' << Green <<
' ' << fields.low <<
' '
338TEST(ErrorMessage, TakesTheManipulatorsOfAStream)
341 << std::hex << 255 <<
' ' << std::dec << 255 <<
' '
342 << std::oct << 8 <<
' ' << std::boolalpha <<
true);
344 << std::setw(6) << std::setfill(
'*') << 42 <<
' '
345 << std::setprecision(3) << 3.14159 <<
' ' << std::fixed
346 << 2.5 <<
' ' << std::showpos << 7);
348 <<
"a line" << std::endl <<
"and another" << std::flush);
351TEST(ErrorMessage, KeepsTheTemporariesOfItsItemsUntilItThrows)
353 const std::string ab =
"ab";
360 << Tracked(
"alive") <<
' ' << Tracked(twice(ab)),
364TEST(ErrorMessage, EvaluatesItsItemsOnceInOrderAndOnlyToThrow)
367 const auto next = [&calls] {
return ++calls; };
382TEST(ErrorMessage, ReadsEachItemWhenItIsInserted)
388 std::string
value =
"before";
393 const char * word =
"first";
398 char buffer[8] =
"one";
401 << std::strcpy(buffer,
"two"),
404 Point point = {1, 2};
405 const Point other = {3, 4};
408 "(1, 2) then (3, 4)");
411TEST(ErrorMessage, GoesThroughAMacroThatTakesItAsAnArgument)
428TEST(ErrorMessage, TheStreamOfAMessageTakesAPointerOfTheFrame)
432 static_assert(
sizeof(Message_Stream) ==
sizeof(
void *));
433 static_assert(not std::is_copy_constructible_v<Message_Stream>);
434 static_assert(not std::is_copy_assignable_v<Message_Stream>);
436 using Former_Head =
decltype(std::ostringstream() <<
"(" << __FILE__ <<
":"
437 << __LINE__ <<
") | ");
438 static_assert(std::is_same_v<Former_Head, std::ostringstream &&>);
439 using Head =
decltype(std::declval<Message_Stream>().stream());
440 static_assert(std::is_same_v<Head, Former_Head>);
442 Message_Stream
message(
"a file", 7);
443 std::ostringstream && stream =
message.stream();
444 EXPECT_EQ(stream.str(),
"(a file:7) | ");
446 EXPECT_EQ(
message.stream().str(),
"(a file:7) | an item");
452TEST(ErrorMessage, TheStreamDiesWhenItsExceptionLeaves)
454 streams_watched = streams_dead = 0;
455 for (
int i = 0; i < 100; ++i)
459 EXPECT_EQ(streams_dead, 3 * i + 1);
461 << Throws_When_Inserted() <<
" after", std::length_error);
462 EXPECT_EQ(streams_dead, 3 * i + 2);
464 << throw_if_odd(2 * i + 1), std::underflow_error);
465 EXPECT_EQ(streams_dead, 3 * i + 3);
467 EXPECT_EQ(streams_watched, 300);
474TEST(ErrorMessage, AnOverloadOfTheCallerTakesItsItemsAndNotThePlace)
476 overload_of_the_caller::strings_taken = 0;
478 message_of<std::range_error>(overload_of_the_caller::raise);
479 EXPECT_EQ(file_of(
message), __FILE__);
480 EXPECT_EQ(text_of(
message),
"an item 7 and another");
481 EXPECT_EQ(overload_of_the_caller::strings_taken, 2);
486TEST(ErrorMessage, EachThreadWritesItsOwnMessages)
488 constexpr int Num_Threads = 8;
489 constexpr int Num_Messages = 2000;
490 std::atomic<int> wrong = 0;
492 for (
int t = 0; t < Num_Threads; ++t)
493 threads.
append(std::thread([t, &wrong]
495 for (int i = 1; i <= Num_Messages; ++i)
498 ah_runtime_error() <<
"thread " << t <<
", message " << i
499 <<
", " << text_of_another_message(i);
501 catch (
const std::runtime_error & e)
503 if (text_of(e.what()) !=
"thread " + std::to_string(t) +
504 ", message " + std::to_string(i) +
", inner " +
505 std::to_string(i) +
" of inin")
509 for (
int t = 0; t < Num_Threads; ++t)
516TEST(ErrorMessage, AMessageInsideAnotherKeepsBoth)
520 <<
", " << text_of_another_message(0) <<
", end",
521 "outer, inner 7 of inin, nothing, end");
524TEST(ErrorMessage, ACheckedAccessOutOfRangeThrowsWithItsText)
529 message_of<std::out_of_range>([&] { (void) array[3]; });
530 EXPECT_EQ(text_of(
message),
"access out of range");
531 EXPECT_TRUE(ends_with(file_of(
message),
"tpl_memArray.H")) <<
message;
532 EXPECT_EQ(array[2], 3);
538 message_of<std::out_of_range>([&] { (void) array[1]; });
539 EXPECT_EQ(text_of(
message),
"access out of range");
540 EXPECT_EQ(array[0], 1);
546 message_of<std::out_of_range>([&] { (void) vector[1]; });
547 EXPECT_EQ(text_of(
message),
"SmallVector: index out of range");
548 EXPECT_TRUE(ends_with(file_of(
message),
"tpl_small_vector.H")) <<
message;
549 EXPECT_EQ(vector[0], 1);
555 message_of<std::out_of_range>([&] { (void) array.
read(5); });
557 "DynArray::read(5): index out of range; size() is 3");
558 EXPECT_TRUE(ends_with(file_of(
message),
"tpl_dynArray.H")) <<
message;
559 EXPECT_EQ(array.
read(2), 7);
565 message_of<std::out_of_range>([&] { (void) queue[1]; });
566 EXPECT_EQ(text_of(
message),
"queue index out of range");
567 EXPECT_TRUE(ends_with(file_of(
message),
"tpl_arrayQueue.H")) <<
message;
568 EXPECT_EQ(queue[0], 1);
573 message_of<std::out_of_range>([&] { (void)
bits.read_bit(8); });
574 EXPECT_EQ(text_of(
message),
"index out of range");
575 EXPECT_TRUE(ends_with(file_of(
message),
"bitArray.H")) <<
message;
576 EXPECT_EQ(
bits.read_bit(7), 0);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
#define ah_length_error()
Throws std::length_error unconditionally.
#define ah_fatal_error()
Throws an unconditional std::runtime_error (fatal error)
#define ah_logic_error_unless(C)
Throws std::logic_error if condition does NOT hold.
#define ah_invalid_argument()
Throws std::invalid_argument unconditionally.
#define ah_overflow_error()
Throws std::overflow_error unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
#define ah_logic_error()
Throws std::logic_error unconditionally.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
#define ah_underflow_error()
Throws std::underflow_error unconditionally.
#define ah_range_error()
Throws std::range_error unconditionally.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
#define ah_out_of_range_error()
Throws std::out_of_range unconditionally.
#define EXPECT_THE_MESSAGES_OF(E, base)
#define EXPECT_THE_TEXT(E, statement, text)
#define EXPECT_THE_ITEMS(E, call, items)
#define EXPECT_THE_WHOLE(E, call, items)
Space-efficient bit array implementation.
size_t size_t int32_t value
size_t size_t int32_t * out
Queue implemented with a single dynamic array.
T & put(const T &item)
Copy and put an item in the queue.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Contiguous array of bits.
const T & read(const size_t i) const
Read entry i without allocating memory.
T & touch(const size_t i)
Touch the entry i.
Simple, scalable and fast dynamic array.
T & put(const T &item)
Put a copy of item at the end of the sequence.
Represents a point with rectangular coordinates in a 2D plane.
Contiguous dynamic array with N elements of inline storage.
T & append(const T &item)
Append a copy of item.
The stream of the message of a macro that throws.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
unsigned char Color
Color type for RB nodes.
Circular queue implementations backed by arrays.
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Simple, scalable, contiguous dynamic array.
Dynamic array with inline storage (Aleph::SmallVector).