micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
function-comparators-inlines.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
43
44#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_FUNCTION_COMPARATORS_INLINES_H_
45#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_FUNCTION_COMPARATORS_INLINES_H_
46
47// ----------------------------------------------------------------------------
48
49#if defined(__cplusplus)
50
51// ----------------------------------------------------------------------------
52
53#if defined(__GNUC__)
54#pragma GCC diagnostic push
55
56#pragma GCC diagnostic ignored "-Waggregate-return"
57#if defined(__clang__)
58#pragma clang diagnostic ignored "-Wc++98-compat"
59#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
60#endif // defined(__clang__)
61#endif // defined(__GNUC__)
62
63// ============================================================================
64
66{
67 // --------------------------------------------------------------------------
68
85 template <class Lhs_T, class Rhs_T>
87 Rhs_T>
88 constexpr auto
89 eq (Lhs_T&& lhs, Rhs_T&& rhs)
90 {
91 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
92 detail::as_operand (std::forward<Rhs_T> (rhs)),
94 }
95
96#if !defined(__DOXYGEN__)
102 template <class Lhs_T, class Rhs_T>
103 requires (not detail::comparable_operands<detail::comparison::equal, Lhs_T,
104 Rhs_T>)
105 constexpr bool
106 eq ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
107 {
109 return false;
110 }
111#endif // !defined(__DOXYGEN__)
112
121 template <class Lhs_T, class Rhs_T>
122 requires detail::comparable_operands<detail::comparison::not_equal, Lhs_T,
123 Rhs_T>
124 constexpr auto
125 ne (Lhs_T&& lhs, Rhs_T&& rhs)
126 {
127 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
128 detail::as_operand (std::forward<Rhs_T> (rhs)),
130 }
131
132#if !defined(__DOXYGEN__)
138 template <class Lhs_T, class Rhs_T>
139 requires (not detail::comparable_operands<detail::comparison::not_equal,
140 Lhs_T, Rhs_T>)
141 constexpr bool
142 ne ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
143 {
145 return false;
146 }
147#endif // !defined(__DOXYGEN__)
148
157 template <class Lhs_T, class Rhs_T>
158 requires detail::comparable_operands<detail::comparison::greater, Lhs_T,
159 Rhs_T>
160 constexpr auto
161 gt (Lhs_T&& lhs, Rhs_T&& rhs)
162 {
163 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
164 detail::as_operand (std::forward<Rhs_T> (rhs)),
165 detail::greater{} };
166 }
167
168#if !defined(__DOXYGEN__)
174 template <class Lhs_T, class Rhs_T>
175 requires (not detail::comparable_operands<detail::comparison::greater,
176 Lhs_T, Rhs_T>)
177 constexpr bool
178 gt ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
179 {
181 return false;
182 }
183#endif // !defined(__DOXYGEN__)
184
193 template <class Lhs_T, class Rhs_T>
194 requires detail::comparable_operands<detail::comparison::greater_equal,
195 Lhs_T, Rhs_T>
196 constexpr auto
197 ge (Lhs_T&& lhs, Rhs_T&& rhs)
198 {
199 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
200 detail::as_operand (std::forward<Rhs_T> (rhs)),
202 }
203
204#if !defined(__DOXYGEN__)
210 template <class Lhs_T, class Rhs_T>
211 requires (not detail::comparable_operands<
213 constexpr bool
214 ge ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
215 {
217 Rhs_T> ();
218 return false;
219 }
220#endif // !defined(__DOXYGEN__)
221
230 template <class Lhs_T, class Rhs_T>
231 requires detail::comparable_operands<detail::comparison::less, Lhs_T,
232 Rhs_T>
233 constexpr auto
234 lt (Lhs_T&& lhs, Rhs_T&& rhs)
235 {
236 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
237 detail::as_operand (std::forward<Rhs_T> (rhs)),
238 detail::less{} };
239 }
240
241#if !defined(__DOXYGEN__)
247 template <class Lhs_T, class Rhs_T>
248 requires (not detail::comparable_operands<detail::comparison::less, Lhs_T,
249 Rhs_T>)
250 constexpr bool
251 lt ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
252 {
254 return false;
255 }
256#endif // !defined(__DOXYGEN__)
257
266 template <class Lhs_T, class Rhs_T>
267 requires detail::comparable_operands<detail::comparison::less_equal, Lhs_T,
268 Rhs_T>
269 constexpr auto
270 le (Lhs_T&& lhs, Rhs_T&& rhs)
271 {
272 return detail::compare_op{ detail::as_operand (std::forward<Lhs_T> (lhs)),
273 detail::as_operand (std::forward<Rhs_T> (rhs)),
275 }
276
277#if !defined(__DOXYGEN__)
283 template <class Lhs_T, class Rhs_T>
284 requires (not detail::comparable_operands<detail::comparison::less_equal,
285 Lhs_T, Rhs_T>)
286 constexpr bool
287 le ([[maybe_unused]] Lhs_T&& lhs, [[maybe_unused]] Rhs_T&& rhs)
288 {
290 return false;
291 }
292#endif // !defined(__DOXYGEN__)
293
301 template <class Lhs_T, class Rhs_T, class Epsilon_T>
302 requires (extension::approx_operand<Lhs_T>
303 and extension::approx_operand<Rhs_T>
304 and extension::approx_operand<Epsilon_T>)
305 constexpr auto
306 approx (Lhs_T&& lhs, Rhs_T&& rhs, Epsilon_T&& epsilon)
307 {
308 return detail::approx_op{ std::forward<Lhs_T> (lhs),
309 std::forward<Rhs_T> (rhs),
310 std::forward<Epsilon_T> (epsilon) };
311 }
312
313 // --------------------------------------------------------------------------
314 // Logical operators.
315
327 template <class Expr_T>
328 requires extension::checkable<std::remove_cvref_t<Expr_T>>
329 constexpr auto
330 negate (Expr_T&& expr)
331 {
333 std::forward<Expr_T> (expr)
334 };
335 }
336
355 template <class First_T, class Second_T, class... Rest_T>
356 requires (extension::checkable<std::remove_cvref_t<First_T>>
357 and extension::checkable<std::remove_cvref_t<Second_T>>
358 and (extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
359 constexpr auto
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 }
375
390 template <class First_T, class Second_T, class... Rest_T>
391 requires (extension::checkable<std::remove_cvref_t<First_T>>
392 and extension::checkable<std::remove_cvref_t<Second_T>>
393 and (extension::checkable<std::remove_cvref_t<Rest_T>> and ...))
394 constexpr auto
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 }
410
411 // --------------------------------------------------------------------------
412} // namespace micro_os_plus::micro_test_plus
413
414#if defined(__GNUC__)
415#pragma GCC diagnostic pop
416#endif // defined(__GNUC__)
417
418// ----------------------------------------------------------------------------
419
420#endif // defined(__cplusplus)
421
422// ----------------------------------------------------------------------------
423
424#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_FUNCTION_COMPARATORS_INLINES_H_
425
426// ----------------------------------------------------------------------------
Concept satisfied when the operands of a comparator can be written and compared.
constexpr auto approx(T value, std::type_identity_t< T > epsilon) noexcept -> detail::value< T >
Deprecated wrapping of a floating-point value with an explicit comparison tolerance.
constexpr auto eq(Lhs_T &&lhs, Rhs_T &&rhs)
Generic equality comparator.
constexpr auto lt(Lhs_T &&lhs, Rhs_T &&rhs)
Generic less than comparator.
constexpr auto ge(Lhs_T &&lhs, Rhs_T &&rhs)
Generic greater than or equal comparator.
constexpr auto gt(Lhs_T &&lhs, Rhs_T &&rhs)
Generic greater than comparator.
constexpr auto le(Lhs_T &&lhs, Rhs_T &&rhs)
Generic less than or equal comparator.
constexpr auto ne(Lhs_T &&lhs, Rhs_T &&rhs)
Generic non-equality comparator.
constexpr auto all_of(First_T &&first, Second_T &&second, Rest_T &&... rest)
Generic logical and operation, for two or more operands.
constexpr auto any_of(First_T &&first, Second_T &&second, Rest_T &&... rest)
Generic logical (inclusive) or operation, for two or more operands.
constexpr auto negate(Expr_T &&expr)
Generic logical not operation.
constexpr auto as_operand(T &&t) noexcept -> decltype(auto)
Prepares an operand of a comparator.
constexpr void reject_comparison(void) noexcept
Stops the compilation of a comparator whose operands are rejected, with a message that tells why.
@ greater_equal
The >= operator, checked by ge().
@ less_equal
The <= operator, checked by le().
@ not_equal
The != operator, checked by ne().
Primary namespace for the µTest++ testing framework.
Logical AND comparator struct template.
Definition detail.h:1083
Approximate comparator struct template.
Definition detail.h:1003
Relational comparator struct template.
Definition detail.h:887
Greater than or equal tag for compare_op.
Definition detail.h:619
Greater than tag for compare_op.
Definition detail.h:560
Less than or equal tag for compare_op.
Definition detail.h:737
Less than tag for compare_op.
Definition detail.h:678
Non-equality tag for compare_op.
Definition detail.h:501
Logical NOT comparator struct template.
Definition detail.h:1195
Logical OR comparator struct template.
Definition detail.h:1138