45#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_DETAIL_INLINES_H_
46#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_DETAIL_INLINES_H_
50#if defined(__cplusplus)
67#pragma GCC diagnostic push
69#pragma GCC diagnostic ignored "-Waggregate-return"
71#pragma clang diagnostic ignored "-Wunknown-warning-option"
72#pragma clang diagnostic ignored "-Wc++98-compat"
73#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
97 constexpr decltype (
auto)
100#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
129 operator bool ()
const
140 constexpr decltype (
auto)
153 template <
class Lhs_T,
class Rhs_T>
154 template <
class L,
class R>
166 template <
class Lhs_T,
class Rhs_T>
168 operator bool ()
const
179 template <
class Lhs_T,
class Rhs_T>
180 constexpr decltype (
auto)
192 template <
class Lhs_T,
class Rhs_T>
193 constexpr decltype (
auto)
201#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
209 template <
class Lhs_T,
class Rhs_T>
215#pragma GCC diagnostic push
216#pragma GCC diagnostic ignored "-Wfloat-equal"
217#pragma GCC diagnostic ignored "-Wconversion"
218#pragma GCC diagnostic ignored "-Wdouble-promotion"
219#pragma GCC diagnostic ignored "-Wsign-compare"
220#if defined(__clang__)
221#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
222#pragma clang diagnostic ignored "-Wpedantic"
225 const auto difference
235 return difference < lhs.epsilon;
239 return difference < rhs.epsilon;
242#pragma GCC diagnostic pop
254#pragma GCC diagnostic push
255#pragma GCC diagnostic ignored "-Wfloat-equal"
256#pragma GCC diagnostic ignored "-Wconversion"
257#pragma GCC diagnostic ignored "-Wdouble-promotion"
258#pragma GCC diagnostic ignored "-Wsign-compare"
259#if defined(__clang__)
260#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
261#pragma clang diagnostic ignored "-Wpedantic"
312 = std::conditional_t<std::is_signed_v<T>, std::make_signed_t<T>,
313 std::make_unsigned_t<T>>;
367 template <
class Lhs_T,
class Rhs_T>
369 = std::is_integral_v<Lhs_T> and std::is_integral_v<Rhs_T>
370 and (not std::is_same_v<Lhs_T, bool>)
371 and (not std::is_same_v<Rhs_T, bool>)
402 template <
class Lhs_T,
class Rhs_T>
404 = std::is_pointer_v<Lhs_T> and std::is_pointer_v<Rhs_T>
405 and (std::is_function_v<std::remove_pointer_t<Lhs_T>>
406 or std::is_function_v<std::remove_pointer_t<Rhs_T>>);
446 using std::operator==;
447 using std::operator!=;
448 using std::operator<;
449 using std::operator>;
450 using std::operator<=;
451 using std::operator>=;
466 template <comparison Op,
class Lhs_T,
class Rhs_T>
469 and
requires (
const Lhs_T& lhs,
const Rhs_T& rhs) { lhs == rhs; })
471 and
requires (
const Lhs_T& lhs,
472 const Rhs_T& rhs) { lhs != rhs; })
474 and
requires (
const Lhs_T& lhs,
475 const Rhs_T& rhs) { lhs > rhs; })
477 and
requires (
const Lhs_T& lhs,
478 const Rhs_T& rhs) { lhs >= rhs; })
480 and
requires (
const Lhs_T& lhs,
481 const Rhs_T& rhs) { lhs < rhs; })
483 and
requires (
const Lhs_T& lhs,
const Rhs_T& rhs) {
507 template <comparison Op,
class Lhs_T,
class Rhs_T>
530 template <comparison Op,
class Lhs_T,
class Rhs_T>
556 template <comparison Op,
class Lhs_T,
class Rhs_T>
563 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
564 "an operand of the comparator cannot be written in "
565 "the report; define an operator<< (mt::"
566 "expression_formatter&, const T&) in the namespace "
571 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
572 "the operands of eq () cannot be compared with ==");
576 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
577 "the operands of ne () cannot be compared with !=");
581 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
582 "the operands of gt () cannot be compared with >");
586 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
587 "the operands of ge () cannot be compared with >=");
591 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
592 "the operands of lt () cannot be compared with <");
596 static_assert (not std::is_same_v<Lhs_T, Lhs_T>,
597 "the operands of le () cannot be compared with <=");
607 template <
class Lhs_T,
class Rhs_T>
611 using std::operator==;
630 template <
class Lhs_T,
class Rhs_T>
634 using std::operator==;
635 using std::operator!=;
653 template <
class Lhs_T,
class Rhs_T>
657 using std::operator>;
675 template <
class Lhs_T,
class Rhs_T>
679 using std::operator>=;
697 template <
class Lhs_T,
class Rhs_T>
701 using std::operator<;
719 template <
class Lhs_T,
class Rhs_T>
723 using std::operator<=;
736#pragma GCC diagnostic pop
753 template <
class Tag_T>
756 std::remove_cvref_t<
decltype (Tag_T::symbol)>>;
777 template <
class Tag_T>
783 static_assert (not std::is_same_v<Tag_T, Tag_T>,
784 "the tag of a comparator needs a static member "
785 "symbol that can be written, such as a const "
790 static_assert (not std::is_same_v<Tag_T, Tag_T>,
791 "the tag of a comparator needs a static member "
792 "function apply (lhs, rhs) that can be called with "
793 "the operands and returns a value convertible to "
814 template <
class Tag_T>
820 static_assert (not std::is_same_v<Tag_T, Tag_T>,
821 "the tag of a predicate needs a static member "
822 "symbol that can be written, such as a const "
827 static_assert (not std::is_same_v<Tag_T, Tag_T>,
828 "the tag of a predicate needs a static member "
829 "function apply (operand) that can be called with "
830 "the operand and returns a value convertible to "
852 template <
class Lhs_T,
class Rhs_T,
class Compare_T>
853 template <
class L,
class R>
866#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
867 using lhs_type = std::remove_cvref_t<L>;
868 using rhs_type = std::remove_cvref_t<R>;
873 return Compare_T::apply (lhs_type::value, rhs_type::value);
878 Compare_T::within_tolerance_result;
882 ? Compare_T::within_tolerance_result
896#if !defined(__DOXYGEN__)
903 template <
class Lhs_T,
class Rhs_T,
class Compare_T>
904 template <
class L,
class R>
939 template <
class Lhs_T,
class Rhs_T,
class Epsilon_T>
943 if constexpr (std::is_integral_v<Lhs_T> and std::is_integral_v<Rhs_T>
944 and std::is_integral_v<Epsilon_T>)
946 bool overflow =
false;
948 = [&overflow] (
auto larger,
949 auto smaller)
constexpr -> std::uintmax_t
951 if constexpr (std::is_signed_v<
decltype (smaller)>)
953 if (smaller < 0 and std::cmp_greater_equal (larger, 0))
958 =
static_cast<std::uintmax_t
> (-(smaller + 1)) + 1u;
959 const auto upper =
static_cast<std::uintmax_t
> (larger);
960 if (upper > std::numeric_limits<std::uintmax_t>::max ()
964 return std::numeric_limits<std::uintmax_t>::max ();
966 return upper + magnitude;
969 return static_cast<std::uintmax_t
> (larger)
970 -
static_cast<std::uintmax_t
> (smaller);
973 const std::uintmax_t difference = std::cmp_less (lhs, rhs)
974 ? distance (rhs, lhs)
975 : distance (lhs, rhs);
976 return !overflow && std::cmp_less_equal (difference, epsilon);
980 using float_type = std::common_type_t<Lhs_T, Rhs_T, Epsilon_T>;
981 const auto left =
static_cast<float_type
> (lhs);
982 const auto right =
static_cast<float_type
> (rhs);
985#pragma GCC diagnostic push
986#pragma GCC diagnostic ignored "-Wfloat-equal"
989 const float_type distance
990 = (left == right) ? float_type{} :
math::abs (left - right);
992#pragma GCC diagnostic pop
995 return distance <= static_cast<float_type> (epsilon);
1007 template <
class Lhs_T,
class Rhs_T,
class Epsilon_T>
1008 template <
class L,
class R,
class E>
1012 std::forward<R> (
rhs),
1017 epsilon_{ std::forward<E> (epsilon) }
1025 template <
class Lhs_T,
class Rhs_T,
class Epsilon_T>
1026 constexpr decltype (
auto)
1040 template <
class Lhs_T,
class Rhs_T>
1041 template <
class L,
class R>
1044 std::forward<R> (
rhs),
1045 static_cast<bool> (
lhs)
1046 and
static_cast<bool> (
rhs) }
1058 template <
class Lhs_T,
class Rhs_T>
1059 template <
class L,
class R>
1062 std::forward<R> (
rhs),
1063 static_cast<bool> (
lhs)
1064 or
static_cast<bool> (
rhs) }
1077 requires (not std::is_same_v<std::remove_cvref_t<U>,
not_op<T>>)
1079 :
unary_op<T>{ std::forward<U> (t), not
static_cast<bool> (t) }
1091 template <
class T,
class Predicate_T>
1101#if !defined(__DOXYGEN__)
1107 template <
class T,
class Predicate_T>
1119#if defined(__cpp_exceptions)
1137 constexpr callable_op::
1138 operator bool ()
const
1157 inline const std::string&
1181#if defined(__GNUC__)
1182#pragma GCC diagnostic push
1183#pragma GCC diagnostic ignored "-Wexceptions"
1186 template <
class Exception_T>
1187 template <
class Callable_T>
1188 requires std::invocable<Callable_T&>
1200 catch (
const Exception_T&)
1204 catch (
const std::exception& e)
1207 e.what () !=
nullptr ? e.what () :
"" };
1218#if defined(__GNUC__)
1219#pragma GCC diagnostic pop
1235 template <
class Callable_T>
1236 requires std::invocable<Callable_T&>
1272 template <
class Callable_T>
1273 requires std::invocable<Callable_T&>
1285 catch (
const std::exception& e)
1288 e.what () !=
nullptr ? e.what () :
"" };
1303#if defined(__GNUC__)
1304#pragma GCC diagnostic push
1306#pragma GCC diagnostic ignored "-Wfloat-equal"
1307#if defined(__clang__)
1308#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
1309#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
1347 requires std::is_arithmetic_v<T>
1351 if constexpr (std::is_same_v<T, long double>)
1353#if defined(_WIN32) \
1354 || (defined(__SIZEOF_LONG_DOUBLE__) \
1355 && __SIZEOF_LONG_DOUBLE__ == __SIZEOF_DOUBLE__)
1363 const auto [ptr, ec] = std::to_chars (buf, buf +
sizeof (buf),
1364 static_cast<double> (v));
1365 if (ec == std::errc{})
1366 buffer.append (buf, ptr);
1378 const int saved_errno = errno;
1380 const int min_precision
1381 = (v > -LDBL_MIN && v < LDBL_MIN) ? 1 : LDBL_DIG;
1382 bool is_exact =
false;
1383 for (
int precision = min_precision; precision <= LDBL_DECIMAL_DIG;
1389#if defined(__GNUC__) && !defined(__clang__)
1390#pragma GCC diagnostic push
1391#pragma GCC diagnostic ignored "-Wformat-truncation"
1393 snprintf (buf,
sizeof (buf),
"%.*Lg", precision, v);
1394#if defined(__GNUC__) && !defined(__clang__)
1395#pragma GCC diagnostic pop
1398 if (v != v || strtold (buf,
nullptr) == v)
1404 errno = saved_errno;
1407 buffer.append (buf);
1416 const auto [ptr, ec] = std::to_chars (buf, buf +
sizeof (buf),
1417 static_cast<double> (v));
1418 if (ec == std::errc{})
1419 buffer.append (buf, ptr);
1426 const auto [ptr, ec] = std::to_chars (buf, buf +
sizeof (buf), v);
1427 if (ec == std::errc{})
1428 buffer.append (buf, ptr);
1432#if defined(__GNUC__)
1433#pragma GCC diagnostic pop
1459 buffer.append (
"0x0");
1464 if constexpr (std::is_function_v<T>)
1466 p =
reinterpret_cast<void*
> (v);
1470 const volatile void* cvp = v;
1471 p =
const_cast<void*
> (cvp);
1474#if defined(__GNUC__)
1475#pragma GCC diagnostic push
1477#if defined(__clang__)
1478#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
1483 snprintf (buf,
sizeof (buf),
"%p", p);
1484 buffer.append (buf);
1486#if defined(__GNUC__)
1487#pragma GCC diagnostic pop
1498 template <
character Char_T>
1500 const Char_T* str) noexcept
1502 size_{ str ==
nullptr ? 0 : std::char_traits<Char_T>::length (str) }
1511 template <
character Char_T>
1513 const Char_T* str, std::size_t capacity) noexcept
1514 :
str_{ str }, size_{ capacity }
1518 const std::size_t terminator
1519 = std::basic_string_view<Char_T>{ str, capacity }.find (Char_T{});
1520 if (terminator != std::basic_string_view<Char_T>::npos)
1530 template <
character Char_T>
1531 constexpr std::basic_string_view<Char_T>
1534 return str_ ==
nullptr ? std::basic_string_view<Char_T>{}
1535 : std::basic_string_view<Char_T>{
str_,
size_ };
1542 template <
character Char_T>
1546 return str_ ==
nullptr;
1554 template <
character Char_T>
1559 if (lhs.is_null () || rhs.is_null ())
1561 return lhs.is_null () && rhs.is_null ();
1563 return lhs.view () == rhs.view ();
1570 template <
character Char_T>
1574 std::type_identity_t<std::basic_string_view<Char_T>> rhs)
noexcept
1576 return !lhs.is_null () && lhs.view () == rhs;
1583 template <
character Char_T>
1587 return lhs.is_null ();
1594 template <
character Char_T>
1595 constexpr std::strong_ordering
1599 if (lhs.is_null () || rhs.is_null ())
1601 return !lhs.is_null () <=> !rhs.is_null ();
1603 return lhs.view () <=> rhs.view ();
1610 template <
character Char_T>
1611 constexpr std::strong_ordering
1614 std::type_identity_t<std::basic_string_view<Char_T>> rhs)
noexcept
1618 return std::strong_ordering::less;
1620 return lhs.view () <=> rhs;
1629 std::size_t count) noexcept
1630 :
first_{ first }, count_{ count }
1646#if defined(__GNUC__)
1647#pragma GCC diagnostic push
1648#if defined(__clang__)
1649#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
1664#if defined(__GNUC__)
1665#pragma GCC diagnostic pop
1677 template <
class T,
class R>
1679 and std::equality_comparable_with<
1684 return std::equal (lhs.
begin (), lhs.
end (), rhs.begin (), rhs.end (),
1685 [] (
const auto& l,
const auto& r)
1686 { return equal_to::apply (l, r); });
1693 template <
class T,
class R>
1694 requires (container_like<R>
1695 and std::three_way_comparable_with<
1700 return std::lexicographical_compare_three_way (lhs.
begin (), lhs.
end (),
1701 rhs.begin (), rhs.end ());
1726 as_operand (T&& t)
noexcept ->
decltype (
auto)
1728 using type = std::remove_cvref_t<T>;
1731 using element_type = std::remove_const_t<
1732 std::remove_extent_t<std::remove_reference_t<T>>>;
1735 && not std::is_volatile_v<element_type>)
1738 t, std::extent_v<type>
1747 else if constexpr (std::is_array_v<type>)
1751 static_cast<const element_type*
> (t), std::extent_v<type>
1754 else if constexpr (std::is_function_v<type>)
1761 return std::forward<T> (t);
1773#if defined(__GNUC__)
1774#pragma GCC diagnostic pop
Concept satisfied by the pointers to the character types.
Concept satisfied by the character types.
Concept satisfied when the operands of a comparator can be written and compared.
Concept satisfied when two operands can be compared by the comparator selected by Op.
Satisfied if the comparison selected by Op of two const objects is well-formed.
Concept satisfied when T provides both begin() and end() member functions returning an iterator and i...
Concept satisfied by two pointers, one of which points to a function.
Concept satisfied when T is a framework compile-time constant.
Concept satisfied by two integer types of different signedness.
Concept satisfied when the tag of a comparator or of a predicate has a symbol that can be written.
Concept satisfied by a tag that defines a custom comparator.
Deprecated concept satisfied when T is a framework floating-point wrapper with a tolerance.
Concept satisfied when a type is a framework expression type.
Concept satisfied by a tag that defines a custom predicate.
Concept satisfied when a type can be appended to the message of a check result via operator<<.
constexpr bool within_tolerance(const Lhs_T &lhs, const Rhs_T &rhs)
Deprecated check whether two operands are equal within the tolerance of the floating-point wrappers.
Names for the checks of the comparison operators, looked up as the apply() functions look them up.
constexpr auto abs(const T t) noexcept -> T
Computes the absolute value of a given comparable value.
constexpr auto min_value(const T &lhs, const T &rhs) noexcept -> const T &
Computes the minimum of two comparable values.
Internal implementation details for the µTest++ framework.
std::remove_cvref_t< decltype(detail::unwrap( std::declval< const std::remove_cvref_t< T > & >()))> unwrapped_t
The type of an operand, after the framework wrappers are removed by detail::unwrap().
void append_number(std::string &buffer, T v)
Appends the shortest decimal representation of a numeric value to a buffer.
constexpr bool operator==(const basic_c_string< Char_T > &lhs, const basic_c_string< Char_T > &rhs) noexcept
Compares two C string operands for equality.
typename comparison_integer< T >::type comparison_integer_t
Alias of the integer type used to compare the type T.
constexpr auto as_operand(T &&t) noexcept -> decltype(auto)
Prepares an operand of a comparator.
decltype(auto) constexpr unwrap(const T &t)
Returns the value wrapped by a framework type, or the operand itself.
void append_pointer(std::string &buffer, T *v)
Appends the hexadecimal address of a pointer to a buffer.
unwrapped_t< decltype(detail::as_operand(std::declval< T >()))> comparison_operand_t
The type with which an operand of a comparator is compared.
constexpr void reject_comparison(void) noexcept
Stops the compilation of a comparator whose operands are rejected, with a message that tells why.
throw_outcome
The kind of exception thrown by the callable of a check.
@ std_exception
A std::exception that was not the expected one was thrown; its what() is kept.
@ none
Nothing was thrown, or the expected exception was thrown.
@ unknown_exception
Another exception was thrown.
constexpr void reject_compare_tag(void) noexcept
Stops the compilation of a compare_op whose tag is rejected, with a message that tells why.
constexpr comparison_integer_t< T > as_comparison_integer(const T value) noexcept
Converts an integer or a character to the type used to compare it.
constexpr void reject_predicate_tag(void) noexcept
Stops the compilation of a predicate_op whose tag is rejected, with a message that tells why.
constexpr bool differ_by_at_most(Lhs_T lhs, Rhs_T rhs, Epsilon_T epsilon)
Tells whether two numbers differ by at most a tolerance.
comparison
The comparison operators, as the template argument of the comparable concept.
@ equal
The == operator, checked by eq().
@ greater_equal
The >= operator, checked by ge().
@ less_equal
The <= operator, checked by le().
@ greater
The > operator, checked by gt().
@ not_equal
The != operator, checked by ne().
@ less
The < operator, checked by lt().
constexpr std::strong_ordering operator<=>(const basic_c_string< Char_T > &lhs, const basic_c_string< Char_T > &rhs) noexcept
Orders two C string operands.
Primary namespace for the µTest++ testing framework.
constexpr and_op(L &&lhs, R &&rhs)
Constructs a logical AND comparator for the given operands.
constexpr approx_op(L &&lhs, R &&rhs, E &&epsilon)
Constructs an approximate comparator for the given operands.
constexpr decltype(auto) epsilon(void) const
Retrieves the tolerance.
Epsilon_T epsilon_
The stored tolerance.
An array operand of a comparator, other than a character array.
std::size_t count_
The number of elements.
const T * first_
The first element.
constexpr const T * begin(void) const noexcept
Returns an iterator to the first element.
constexpr array_operand(const T *first, std::size_t count) noexcept
Constructs the operand from the first element and the size of an array.
constexpr const T * end(void) const noexcept
Returns an iterator past the last element.
A C string operand of a comparator.
constexpr bool is_null(void) const noexcept
Returns whether the operand is a null pointer.
constexpr std::basic_string_view< Char_T > view(void) const noexcept
Returns the characters, as a string view.
constexpr basic_c_string(const Char_T *str) noexcept
Constructs the operand from a pointer to a null-terminated string.
const Char_T * str_
The first character, or nullptr.
std::size_t size_
The number of characters, without the terminator.
constexpr decltype(auto) lhs(void) const
Retrieves the left-hand operand.
Rhs_T rhs_
Stores the right-hand operand.
constexpr binary_op(L &&lhs, R &&rhs, bool value)
Constructs a binary comparator with the given operands and pre-computed result.
const bool value_
Stores the result of the comparison.
Lhs_T lhs_
Stores the left-hand operand.
constexpr decltype(auto) rhs(void) const
Retrieves the right-hand operand.
callable_op(callable_result &&result)
Constructs a callable operator with the pre-computed result.
std::string what_
Stores the what() of the std::exception thrown, when the check failed.
throw_outcome outcome_
Stores what the invocation threw, when the check failed.
throw_outcome outcome(void) const noexcept
Retrieves what the invocation threw.
const std::string & what(void) const noexcept
Retrieves the what() of the std::exception thrown.
const bool value_
Stores the result of the callable invocation.
The result of the invocation of the callable of a check.
constexpr compare_op(L &&lhs, R &&rhs, Compare_T)
Constructs a comparator for the given operands.
std::conditional_t< std::is_signed_v< T >, std::make_signed_t< T >, std::make_unsigned_t< T > > type
The integer type of the same size and signedness.
The integer type used to compare a value of type T.
T type
The type used for the comparison.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with ==.
The exception thrown to stop a test case after a failed require().
Greater than or equal tag for compare_op.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with >=.
Greater than tag for compare_op.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with >.
Less than or equal tag for compare_op.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with <=.
Less than tag for compare_op.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with <.
static constexpr bool apply(const Lhs_T &lhs, const Rhs_T &rhs)
Compares the operands with !=.
constexpr not_op(U &&t)
Constructs a logical NOT comparator for the given operand.
nothrow_op(Callable_T &&func)
Constructs a nothrow checking operator for the given callable.
constexpr or_op(L &&lhs, R &&rhs)
Constructs a logical OR comparator for the given operands.
constexpr predicate_op(U &&t, Predicate_T)
Constructs a predicate for the given operand.
The type of the elements of a range, as iterated by a range-based for statement.
throws_op(Callable_T &&func)
Constructs an exception checking operator for the given callable.
constexpr decltype(auto) operand(void) const
Retrieves the wrapped operand expression.
constexpr unary_op(U &&t, bool value)
Constructs a unary comparator with the given operand and pre-computed result.
const bool value_
Stores the result of the operation.
Struct template representing a generic value, accessible via a getter.