Skip to main content

Logical functions

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

Functions Index​

template <class First_T, class Second_T, class... Rest_T>
constexpr autoall_of (First_T &&first, Second_T &&second, Rest_T &&... rest)

Generic logical and operation, for two or more operands. More...

template <class First_T, class Second_T, class... Rest_T>
constexpr autoany_of (First_T &&first, Second_T &&second, Rest_T &&... rest)

Generic logical (inclusive) or operation, for two or more operands. More...

template <class Expr_T>
constexpr autonegate (Expr_T &&expr)

Generic logical not operation. More...

Description​

Functions used to compose logical expressions in tests.

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.

Functions​

all_of()​

template <class First_T, class Second_T, class... Rest_T>
auto micro_os_plus::micro_test_plus::all_of (First_T && first, Second_T && second, Rest_T &&... rest)
nodiscard constexpr

Generic logical and operation, for two or more operands.

Template Parameters
First_T

Type of the first operand.

Second_T

Type of the second operand.

Rest_T

Types of the other operands, if any.

Parameters
first

First operand.

second

Second operand.

rest

The 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.

360 all_of (First_T&& first, Second_T&& second, Rest_T&&... rest)
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 }

Reference micro_os_plus::micro_test_plus::all_of.

Referenced by micro_os_plus::micro_test_plus::_and and micro_os_plus::micro_test_plus::all_of.

any_of()​

template <class First_T, class Second_T, class... Rest_T>
auto micro_os_plus::micro_test_plus::any_of (First_T && first, Second_T && second, Rest_T &&... rest)
nodiscard constexpr

Generic logical (inclusive) or operation, for two or more operands.

Template Parameters
First_T

Type of the first operand.

Second_T

Type of the second operand.

Rest_T

Types of the other operands, if any.

Parameters
first

First operand.

second

Second operand.

rest

The 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.

395 any_of (First_T&& first, Second_T&& second, Rest_T&&... rest)
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 }

Reference micro_os_plus::micro_test_plus::any_of.

Referenced by micro_os_plus::micro_test_plus::_or and micro_os_plus::micro_test_plus::any_of.

negate()​

template <class Expr_T>
auto micro_os_plus::micro_test_plus::negate (Expr_T && expr)
nodiscard constexpr

Generic logical not operation.

Template Parameters
Expr_T

Type of the operand.

Parameters
expr

Logical 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.

330 negate (Expr_T&& expr)
331 {
333 std::forward<Expr_T> (expr)
334 };
335 }

Reference micro_os_plus::micro_test_plus::negate.

Referenced by micro_os_plus::micro_test_plus::_not and micro_os_plus::micro_test_plus::negate.


Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.