micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
Logical functions

Functions used to compose logical expressions in tests. More...

Functions

template<class First_T, class Second_T, class... Rest_T>
requires (extension::checkable<std::remove_cvref_t<First_T>> and extension::checkable<std::remove_cvref_t<Second_T>> and
(extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
constexpr auto micro_os_plus::micro_test_plus::all_of (First_T &&first, Second_T &&second, Rest_T &&... rest)
 Generic logical and operation, for two or more operands.
template<class First_T, class Second_T, class... Rest_T>
requires (extension::checkable<std::remove_cvref_t<First_T>> and extension::checkable<std::remove_cvref_t<Second_T>> and
(extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
constexpr auto micro_os_plus::micro_test_plus::any_of (First_T &&first, Second_T &&second, Rest_T &&... rest)
 Generic logical (inclusive) or operation, for two or more operands.
template<class Expr_T>
requires extension::checkable<std::remove_cvref_t<Expr_T>>
constexpr auto micro_os_plus::micro_test_plus::negate (Expr_T &&expr)
 Generic logical not operation.

Detailed Description

Three functions combine the conditions of a check, and report the values of all their operands when the check fails:

  • mt::all_of (a, b, ...) is true when all the conditions are true;
  • mt::any_of (a, b, ...) is true when at least one condition is true, including when all are;
  • mt::negate (a) is true when the condition is false.

all_of() and any_of() take two or more operands; a failed check shows them as one group, for example (1 == 1 and 1 == 1 and 1 == 3).

The functions have names, rather than the &&, ||, and ! operators, because the built-in operators would return a plain bool, which loses the values of the operands.

Example
ts.test ("Check compound conditions", [] (auto& t) {
t.expect (mt::all_of (mt::eq (compute_answer (), 42),
mt::eq (compute_float (), 42.0),
mt::lt (compute_answer (), 43)));
});
Differences from the built-in operators
  • All the operands are evaluated before the check; unlike && and ||, the functions do not stop at the first operand that decides the result, so do not write mt::all_of (mt::ne (p, nullptr), mt::eq (*p, 1)).
  • After a using namespace std, an unqualified all_of() or any_of() with three or more operands of the same type selects the standard algorithm, which does not compile, and an unqualified negate() is ambiguous with std::negate; write mt::all_of(), mt::any_of(), and mt::negate().

The overloaded logical operators of the mt::operators namespace are deprecated since 6.0.0, in favour of these functions.

Function Documentation

◆ all_of()

template<class First_T, class Second_T, class... Rest_T>
requires (extension::checkable<std::remove_cvref_t<First_T>> and extension::checkable<std::remove_cvref_t<Second_T>> and
(extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
auto micro_os_plus::micro_test_plus::all_of ( First_T && first,
Second_T && second,
Rest_T &&... rest )
nodiscardconstexpr
Template Parameters
First_TType of the first operand.
Second_TType of the second operand.
Rest_TTypes of the other operands, if any.
Parameters
firstFirst operand.
secondSecond operand.
restThe other operands, if any.
Constraints
Enabled only if each operand satisfies extension::checkable, as for expect().
Returns
An object that evaluates to true if all the operand expressions are true.

The all_of function template provides a generic logical conjunction for any expression types. It constructs a detail::and_op logical conjunction object, which evaluates to true if all the operands are true. For more than two operands, the objects are nested from the left, (a and b) and c, and the expression formatter writes them as a single chain, (a and b and c).

All the operands are evaluated before the check, so, unlike the standard algorithm of the same name, the function does not stop at the first operand that is false.

The name is the same as that of std::all_of: after a using namespace std, an unqualified call with three or more operands of the same type selects the standard algorithm, so the function must be called as mt::all_of().

Definition at line 360 of file function-comparators-inlines.h.

361 {
362 if constexpr (sizeof...(Rest_T) == 0)
363 {
364 return detail::and_op{ std::forward<First_T> (first),
365 std::forward<Second_T> (second) };
366 }
367 else
368 {
370 detail::and_op{ std::forward<First_T> (first),
371 std::forward<Second_T> (second) },
372 std::forward<Rest_T> (rest)...);
373 }
374 }
constexpr auto all_of(First_T &&first, Second_T &&second, Rest_T &&... rest)
Generic logical and operation, for two or more operands.
and_op(Lhs_T &&, Rhs_T &&) -> and_op< stored_operand_t< Lhs_T >, stored_operand_t< Rhs_T > >
Deduction guide for the logical AND comparator.
Logical AND comparator struct template.
Definition detail.h:1083

References all_of().

Referenced by _and(), and all_of().

◆ any_of()

template<class First_T, class Second_T, class... Rest_T>
requires (extension::checkable<std::remove_cvref_t<First_T>> and extension::checkable<std::remove_cvref_t<Second_T>> and
(extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
auto micro_os_plus::micro_test_plus::any_of ( First_T && first,
Second_T && second,
Rest_T &&... rest )
nodiscardconstexpr
Template Parameters
First_TType of the first operand.
Second_TType of the second operand.
Rest_TTypes of the other operands, if any.
Parameters
firstFirst operand.
secondSecond operand.
restThe other operands, if any.
Constraints
Enabled only if each operand satisfies extension::checkable, as for expect().
Returns
An object that evaluates to true if at least one of the operand expressions is true, including when all are.

The any_of function template provides a generic logical disjunction for any expression types. It constructs a detail::or_op logical disjunction object, which evaluates to true if at least one of the operands is true; the disjunction is inclusive, so the result is also true when all the conditions are true. For more than two operands, the objects are nested from the left, and the expression formatter writes them as a single chain, (a or b or c).

All the operands are evaluated before the check, so the function does not stop at the first operand that is true; and, as for all_of(), it must be called as mt::any_of() after a using namespace std.

Definition at line 395 of file function-comparators-inlines.h.

396 {
397 if constexpr (sizeof...(Rest_T) == 0)
398 {
399 return detail::or_op{ std::forward<First_T> (first),
400 std::forward<Second_T> (second) };
401 }
402 else
403 {
405 detail::or_op{ std::forward<First_T> (first),
406 std::forward<Second_T> (second) },
407 std::forward<Rest_T> (rest)...);
408 }
409 }
constexpr auto any_of(First_T &&first, Second_T &&second, Rest_T &&... rest)
Generic logical (inclusive) or operation, for two or more operands.
or_op(Lhs_T &&, Rhs_T &&) -> or_op< stored_operand_t< Lhs_T >, stored_operand_t< Rhs_T > >
Deduction guide for the logical OR comparator.
Logical OR comparator struct template.
Definition detail.h:1138

References any_of().

Referenced by _or(), and any_of().

◆ negate()

template<class Expr_T>
requires extension::checkable<std::remove_cvref_t<Expr_T>>
auto micro_os_plus::micro_test_plus::negate ( Expr_T && expr)
nodiscardconstexpr
Template Parameters
Expr_TType of the operand.
Parameters
exprLogical expression to be negated.
Constraints
Enabled only if the operand satisfies extension::checkable, as for expect(); the values that are always true (C strings, arrays, function pointers) are rejected at compile time.
Returns
An object that evaluates to true if the operand is false.

The negate function template provides a generic logical negation for any expression type. It constructs a detail::not_op logical negator object, which evaluates to true if the operand is false; the template argument is named, since the class template argument deduction would copy an operand that is itself a not_op, instead of negating it.

It is the function form of the ! operator from the micro_test_plus::operators namespace.

Definition at line 330 of file function-comparators-inlines.h.

331 {
333 std::forward<Expr_T> (expr)
334 };
335 }
Logical NOT comparator struct template.
Definition detail.h:1195

References negate().

Referenced by _not(), and negate().