micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
Function Comparators

Functions for comparing actual and expected values in tests. More...

Functions

template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::eq (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic equality comparator for non-pointer types.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::eq (Lhs_T *lhs, Rhs_T *rhs)
 Pointer equality comparator for any pointer types.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::ge (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic greater than or equal comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::ge (Lhs_T *lhs, Rhs_T *rhs)
 Pointer greater than or equal comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::gt (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic greater than comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::gt (Lhs_T *lhs, Rhs_T *rhs)
 Pointer greater than comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::le (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic less than or equal comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::le (Lhs_T *lhs, Rhs_T *rhs)
 Pointer less than or equal comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::lt (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic less than comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::lt (Lhs_T *lhs, Rhs_T *rhs)
 Pointer less than comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::ne (const Lhs_T &lhs, const Rhs_T &rhs)
 Generic non-equality comparator.
template<class Lhs_T, class Rhs_T>
constexpr auto micro_os_plus::micro_test_plus::ne (Lhs_T *lhs, Rhs_T *rhs)
 Pointer non-equality comparator.

Detailed Description

While any boolean expression can be used in expectations and assumptions, dedicated comparator functions such as eq, ne, lt, le, gt, and ge provide enhanced reporting by explicitly showing the compared values when a check fails.

Using these comparators ensures that failed checks report both actual and expected values, making issues easier to diagnose.

Comparators are available for both fundamental types and standard containers. When comparing containers, each element is assessed individually, and the results are reported in detail.

Examples
ts.test ("Check various conditions", [] (auto& t) {
t.expect (mt::eq (compute_answer (), 42)) << "answer is 42";
t.expect (mt::ne (compute_answer (), 43)) << "answer is not 43";
t.expect (mt::lt (compute_answer (), 43)) << "answer is < 43";
t.expect (mt::le (compute_answer (), 43)) << "answer is <= 43";
t.expect (mt::gt (compute_answer (), 41)) << "answer is > 41";
t.expect (mt::ge (compute_answer (), 42)) << "answer is >= 42";
t.expect (compute_condition ()) << "condition is true";
});

If a comparator check fails, the output clearly indicates the actual and expected values:

Check failed comparisons
✗ actual != 42 FAILED (unit-test.cpp:286, 42 != 42)
✗ FAILED (unit-test.cpp:307, 42 != 42)
✗ 42 != 42_i FAILED (unit-test.cpp:310, 42 != 42)
✗ (actual == 42) and (actual != 42.0) FAILED (unit-test.cpp:781, (42 == 42
and 42.000000 != 42.000000))
Comparing Containers

Containers can be compared for equality or inequality using the same comparator functions. Comparison is performed by iterating through each element and evaluating it individually.

Examples
ts.test ("Check containers", [] (auto& t) {
t.expect (mt::eq (std::vector<int>{ 1, 2 }, std::vector<int>{ 1, 2 }))
<< "vector{ 1, 2 } eq vector{ 1, 2 }";
t.expect (mt::ne (std::vector<int>{ 1, 2, 3 },
std::vector<int>{ 1, 2, 4 }))
<< "vector{ 1, 2, 3 } ne vector{ 1, 2, 4 }";
});

Function Documentation

◆ eq() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::eq ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if the operands are equal.

The eq function template provides a generic equality comparator for any non-pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that two values are equal. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

84 {
85 return detail::eq_<Lhs_T, Rhs_T>{ lhs, rhs };
86 }
Equality comparator struct template.
Definition detail.h:306

◆ eq() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::eq ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the pointers are equal.

The eq function template provides a pointer equality comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that two pointers are equal. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

99 {
100 return detail::eq_<Lhs_T*, Rhs_T*>{ lhs, rhs };
101 }

◆ ge() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::ge ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if lhs is greater than or equal to rhs.

The ge function template provides a generic greater than or equal comparator for any types. It constructs a comparator object that can be used within the µTest++ framework to assert that one value is greater than or equal to another. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

174 {
175 return detail::ge_<Lhs_T, Rhs_T>{ lhs, rhs };
176 }
Greater than or equal comparator struct template.
Definition detail.h:403

◆ ge() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::ge ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the left hand side pointer is greater than or equal to the right hand side pointer.

The ge function template provides a pointer greater than or equal comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that one pointer is greater than or equal to another. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

190 {
191 return detail::ge_<Lhs_T*, Rhs_T*>{ lhs, rhs };
192 }

◆ gt() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::gt ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if lhs is greater than rhs.

The gt function template provides a generic greater than comparator for any types. It constructs a comparator object that can be used within the µTest++ framework to assert that one value is greater than another. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

144 {
145 return detail::gt_<Lhs_T, Rhs_T>{ lhs, rhs };
146 }
Greater than comparator struct template.
Definition detail.h:370

◆ gt() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::gt ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the left hand side pointer is greater than the right hand side pointer.

The gt function template provides a pointer greater than comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that one pointer is greater than another. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

159 {
160 return detail::gt_<Lhs_T*, Rhs_T*>{ lhs, rhs };
161 }

◆ le() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::le ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if lhs is less than or equal to rhs.

The le function template provides a generic less than or equal comparator for any types. It constructs a comparator object that can be used within the µTest++ framework to assert that one value is less than or equal to another. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

235 {
236 return detail::le_<Lhs_T, Rhs_T>{ lhs, rhs };
237 }
Less than or equal comparator struct template.
Definition detail.h:469

◆ le() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::le ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the left hand side pointer is less than or equal to the right hand side pointer.

The le function template provides a pointer less than or equal comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that one pointer is less than or equal to another. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

251 {
252 return detail::le_<Lhs_T*, Rhs_T*>{ lhs, rhs };
253 }

◆ lt() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::lt ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if lhs is less than rhs.

The lt function template provides a generic less than comparator for any types. It constructs a comparator object that can be used within the µTest++ framework to assert that one value is less than another. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

205 {
206 return detail::lt_<Lhs_T, Rhs_T>{ lhs, rhs };
207 }
Less than comparator struct template.
Definition detail.h:436

◆ lt() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::lt ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the left hand side pointer is less than the right hand side pointer.

The lt function template provides a pointer less than comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that one pointer is less than another. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

220 {
221 return detail::lt_<Lhs_T*, Rhs_T*>{ lhs, rhs };
222 }

◆ ne() [1/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::ne ( const Lhs_T & lhs,
const Rhs_T & rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side operand.
Rhs_TType of the right hand side operand.
Parameters
[in]lhsLeft hand side operand.
[in]rhsRight hand side operand.
Returns
A comparator object that evaluates to true if the operands are not equal.

The ne function template provides a generic non-equality comparator for any types. It constructs a comparator object that can be used within the µTest++ framework to assert that two values are not equal. This function is typically used in test expectations and assertions to compare the actual and expected values.

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

114 {
115 return detail::ne_<Lhs_T, Rhs_T>{ lhs, rhs };
116 }
Non-equality comparator struct template.
Definition detail.h:338

◆ ne() [2/2]

template<class Lhs_T, class Rhs_T>
auto micro_os_plus::micro_test_plus::ne ( Lhs_T * lhs,
Rhs_T * rhs )
nodiscardconstexpr
Template Parameters
Lhs_TType of the left hand side pointer operand.
Rhs_TType of the right hand side pointer operand.
Parameters
[in]lhsLeft hand side pointer operand.
[in]rhsRight hand side pointer operand.
Returns
A comparator object that evaluates to true if the pointers are not equal.

The ne function template provides a pointer non-equality comparator for any pointer types. It constructs a comparator object that can be used within the µTest++ framework to assert that two pointers are not equal. This function is typically used in test expectations and assertions to compare the addresses of objects or resources.

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

129 {
130 return detail::ne_<Lhs_T*, Rhs_T*>{ lhs, rhs };
131 }