44# include <gtest/gtest.h>
50# include <type_traits>
57# if defined(__clang__)
58# pragma clang diagnostic error "-Wdangling-else"
61# pragma clang diagnostic error "-Wignored-attributes"
62# elif defined(__GNUC__)
63# pragma GCC diagnostic error "-Wdangling-else"
70 bool counted(
bool value)
76 size_t occurrences(
const std::string & text,
const std::string & part)
79 for (
size_t pos = text.find(part); pos != std::string::npos;
80 pos = text.find(part, pos + part.size()))
90# define TEST_THE_ELSE_AFTER(NAME, MACRO, E, THROWS, TAIL, HAS_MESSAGE) \
91 TEST(AhErrorsElse, NAME) \
93 for (bool around : {false, true}) \
94 for (bool condition : {false, true}) \
96 SCOPED_TRACE(testing::Message() << "around " << around \
97 << ", condition " << condition); \
98 bool else_ran = false; \
99 bool thrown = false; \
105 MACRO(counted(condition)) TAIL; \
109 catch (const E & e) \
114 EXPECT_EQ(else_ran, not around); \
115 EXPECT_EQ(thrown, around and condition == THROWS); \
116 EXPECT_EQ(evaluations, around ? 1 : 0); \
117 if (thrown and HAS_MESSAGE) \
119 EXPECT_EQ(occurrences(what, "ah_errors_else_test.cc:"), 1u) \
121 EXPECT_EQ(occurrences(what, ") | the message"), 1u) << what; \
129 MACRO(counted(condition)) TAIL; \
135 EXPECT_EQ(thrown, around and condition == THROWS); \
136 EXPECT_EQ(evaluations, around ? 1 : 0); \
141# define TEST_THE_ELSE_AFTER_CONSTEXPR(NAME, MACRO, E, THROWS, TAIL) \
142 TEST(AhErrorsElse, NAME) \
144 for (bool around : {false, true}) \
146 SCOPED_TRACE(testing::Message() << "around " << around); \
147 bool else_ran = false; \
148 bool thrown = false; \
152 MACRO(THROWS) TAIL; \
160 EXPECT_EQ(else_ran, not around); \
161 EXPECT_EQ(thrown, around); \
168 MACRO(not THROWS) TAIL; \
176 EXPECT_EQ(else_ran, not around); \
177 EXPECT_FALSE(thrown); \
183 MACRO(THROWS) TAIL; \
189 EXPECT_EQ(thrown, around); \
195# define TEST_THE_ELSE_AFTER_A_MESSAGE(NAME, MACRO, WRITES, LOCATED) \
196 TEST(AhErrorsElse, NAME) \
198 for (bool around : {false, true}) \
199 for (bool condition : {false, true}) \
201 SCOPED_TRACE(testing::Message() << "around " << around \
202 << ", condition " << condition); \
203 const bool writes = around and condition == WRITES; \
204 std::ostringstream out; \
205 bool else_ran = false; \
208 MACRO(out, counted(condition)) << "the message"; \
211 EXPECT_EQ(else_ran, not around); \
212 EXPECT_EQ(evaluations, around ? 1 : 0); \
213 EXPECT_EQ(occurrences(out.str(), "the message"), writes ? 1u : 0u) \
215 if (writes and LOCATED) \
217 EXPECT_EQ(occurrences(out.str(), "ah_errors_else_test.cc:"), \
221 std::ostringstream alone; \
224 MACRO(alone, counted(condition)) << "the message"; \
225 EXPECT_EQ(evaluations, around ? 1 : 0); \
226 EXPECT_EQ(occurrences(alone.str(), "the message"), \
227 writes ? 1u : 0u) << alone.str(); \
231# define WITH_MESSAGE << "the message"
232# define WITHOUT_MESSAGE
368 EXPECT_EQ(
chain(0, 1), 0);
369 EXPECT_THROW(
chain(0, -1), std::domain_error);
370 EXPECT_EQ(
chain(1, 1), 0);
371 EXPECT_THROW(
chain(1, 0), std::domain_error);
372 EXPECT_EQ(
chain(2, -5), 2);
377TEST(AhErrorsElse, BreakAndContinueAfterAMacro)
380 for (
int i = 0; i < 6; ++i)
396 EXPECT_EQ(
r, 1 + 100 + 2 * (10 + 100) + 1 + 100 + 10 + 100);
402 int not_a_pointer(
bool checked)
412TEST(AhErrorsElse, ConstexprFormInATemplate)
414 EXPECT_EQ(not_a_pointer<int>(
false), 2);
415 EXPECT_EQ(not_a_pointer<int>(
true), 1);
416 EXPECT_EQ(not_a_pointer<int *>(
false), 2);
417 EXPECT_THROW(not_a_pointer<int *>(
true), std::invalid_argument);
424TEST(AhErrorsElse, TheMacrosThatWriteHideNoNameOfTheCaller)
427 const bool ah_warning_once_ =
true;
428 const bool ah_trace_once_ =
true;
429 const bool ah_msg_once_ =
true;
430 std::ostringstream
out;
437 for (
const char * part : {
"<1>",
"<2>",
"<3>",
"<4>",
"<5>",
"<6>"})
439 EXPECT_EQ(occurrences(
out.str(), part), 1u) << part <<
": "
444 std::ostringstream ah_warning_once_, ah_trace_once_, ah_msg_once_;
448 EXPECT_EQ(occurrences(ah_warning_once_.str(),
"<w>"), 1u);
449 EXPECT_EQ(occurrences(ah_trace_once_.str(),
"<t>"), 1u);
450 EXPECT_EQ(occurrences(ah_msg_once_.str(),
"<m>"), 1u);
455TEST(AhErrorsElse, TheMacrosThatThrowHideNoNameOfTheCaller)
457 const bool ah_error_once_ =
true;
468 constexpr bool always =
true;
478 int with_constant_conditions(
int k)
491 return positive<0>();
492 return positive<1>();
499TEST(AhErrorsElse, ConstantConditionsDoNotWarn)
501 EXPECT_THROW(with_constant_conditions(0), std::domain_error);
502 EXPECT_THROW(with_constant_conditions(1), std::domain_error);
503 EXPECT_THROW(with_constant_conditions(2), std::runtime_error);
506 EXPECT_THROW(with_constant_conditions(5), std::invalid_argument);
507 EXPECT_EQ(with_constant_conditions(6), 1);
512 enum Check_Flags :
unsigned { Checked = 1u << 1 };
519 constexpr explicit operator bool() const noexcept {
return on; }
522 template <
unsigned F>
540TEST(AhErrorsElse, ConstexprFormsConvertTheirConditionAsIfConstexpr)
542 EXPECT_THROW(masked<Checked>(1), std::domain_error);
543 EXPECT_EQ(masked<0>(10), 10);
544 EXPECT_THROW(enabled<false>(1), std::invalid_argument);
545 EXPECT_EQ(enabled<true>(100), 100);
560 operator std::ostream & ()
const {
return out; }
566 return Indented{
out};
570 struct Not_A_Stream {};
572 template <
class Tail>
573 constexpr bool throws_with =
requires (Tail && tail)
579 constexpr bool throws_with_the_result_of =
requires (
F f)
589static_assert(throws_with<std::ostringstream &>);
590static_assert(throws_with<std::ostringstream &&>);
591static_assert(throws_with<std::ostream &>);
592static_assert(throws_with<Indented>);
593static_assert(not throws_with<Not_A_Stream>);
594static_assert(not throws_with<int>);
595static_assert(not throws_with<const char *>);
596static_assert(not throws_with_the_result_of<void (*)()>);
601TEST(AhErrorsElse, AMessageThatGivesBackAWrapperOfTheStreamIsThrown)
609 catch (
const std::domain_error & e)
613 EXPECT_NE(what.find(
") | first line\n "), std::string::npos) << what;
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if_constexpr(C)
#define ah_invalid_argument_unless_constexpr(C)
#define ah_overflow_error_unless_constexpr(C)
#define ah_runtime_error_if_constexpr(C)
#define ah_length_error_unless_constexpr(C)
#define ah_overflow_error_unless(C)
Throws std::overflow_error if condition does NOT hold.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_bad_alloc_unless_constexpr(C)
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_logic_error_unless_constexpr(C)
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
#define ah_range_error_if_constexpr(C)
#define ah_domain_error_unless_constexpr(C)
#define ah_out_of_range_error_if_constexpr(C)
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_underflow_error_unless_constexpr(C)
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
#define ah_bad_alloc_unless(C)
Throws std::bad_alloc if condition does NOT hold.
#define ah_bad_alloc_if_constexpr(C)
#define ah_range_error_unless_constexpr(C)
#define ah_logic_error_unless(C)
Throws std::logic_error if condition does NOT hold.
#define ah_out_of_range_error_unless_constexpr(C)
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_runtime_error_unless_constexpr(C)
#define ah_warning_if(out, C)
Emits a warning to a stream if the condition holds.
#define ah_overflow_error_if_constexpr(C)
#define ah_invalid_argument_unless(C)
Throws std::invalid_argument if condition does NOT hold.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_underflow_error_if_constexpr(C)
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
#define ah_length_error_unless(C)
Throws std::length_error if condition does NOT hold.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
#define ah_range_error_unless(C)
Throws std::range_error if condition does NOT hold.
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
#define ah_logic_error_if_constexpr(C)
#define ah_length_error_if_constexpr(C)
#define ah_underflow_error_unless(C)
Throws std::underflow_error if condition does NOT hold.
#define ah_invalid_argument_if_constexpr(C)
#define ah_warning_unless(out, C)
Emits a warning to a stream if the condition does NOT hold.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Conditional tracing macros for debugging.
#define ah_msg_if(out, C)
#define ah_msg_unless(out, C)
#define ah_trace_unless(out, C)
#define ah_trace_if(out, C)
#define TEST_THE_ELSE_AFTER(NAME, MACRO, E, THROWS, TAIL, HAS_MESSAGE)
#define TEST_THE_ELSE_AFTER_CONSTEXPR(NAME, MACRO, E, THROWS, TAIL)
#define TEST_THE_ELSE_AFTER_A_MESSAGE(NAME, MACRO, WRITES, LOCATED)
size_t size_t int32_t value
size_t size_t int32_t * out
Number Theoretic Transform over Z / MOD Z.
Exception class for parsing errors with location information.
static Tree_Node< int > * chain(size_t n)
std::string indent(const size_t count)
constexpr Word masked(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word,...
Main namespace for Aleph-w library functions.
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Comprehensive parsing utilities for text processing and compiler construction.
#define ah_parse_error_if(C)
Throws a ParseError if condition holds.
#define ah_parse_error_unless(C)
Throws a ParseError if condition does NOT hold.
Exception constructor with formatted message.