Log Surgeon
Test & API docs
Loading...
Searching...
No Matches
comparison_test_utils.hpp
Go to the documentation of this file.
1#ifndef LOG_SURGEON_TESTS_COMPARISON_TEST_UTILS_HPP
2#define LOG_SURGEON_TESTS_COMPARISON_TEST_UTILS_HPP
3
4#include <compare>
5#include <cstddef>
6#include <vector>
7
8#include <catch2/catch_message.hpp>
9#include <catch2/catch_test_macros.hpp>
10
12template <typename T>
13concept StronglyThreeWayComparable = std::three_way_comparable<T, std::strong_ordering>;
14
20template <StronglyThreeWayComparable T>
21auto test_equal(T const& lhs, T const& rhs) -> void;
22
28template <StronglyThreeWayComparable T>
29auto test_greater_than(T const& lhs, T const& rhs) -> void;
30
36template <StronglyThreeWayComparable T>
37auto test_less_than(T const& lhs, T const& rhs) -> void;
38
43template <StronglyThreeWayComparable T>
44auto pairwise_comparison_of_strictly_ascending_vector(std::vector<T> const& ordered_vector) -> void;
45
46template <StronglyThreeWayComparable T>
47auto test_equal(T const& lhs, T const& rhs) -> void {
48 REQUIRE((lhs <=> rhs) == std::strong_ordering::equal);
49 REQUIRE(lhs == rhs);
50 REQUIRE(lhs <= rhs);
51 REQUIRE(lhs >= rhs);
52 REQUIRE(rhs == lhs);
53 REQUIRE(rhs <= lhs);
54 REQUIRE(rhs >= lhs);
55
56 REQUIRE_FALSE(lhs != rhs);
57 REQUIRE_FALSE(lhs < rhs);
58 REQUIRE_FALSE(lhs > rhs);
59 REQUIRE_FALSE(rhs != lhs);
60 REQUIRE_FALSE(rhs < lhs);
61 REQUIRE_FALSE(rhs > lhs);
62}
63
64template <StronglyThreeWayComparable T>
65auto test_greater_than(T const& lhs, T const& rhs) -> void {
66 REQUIRE((lhs <=> rhs) == std::strong_ordering::greater);
67 REQUIRE(lhs != rhs);
68 REQUIRE(lhs >= rhs);
69 REQUIRE(lhs > rhs);
70 REQUIRE(rhs != lhs);
71 REQUIRE(rhs <= lhs);
72 REQUIRE(rhs < lhs);
73
74 REQUIRE_FALSE(lhs == rhs);
75 REQUIRE_FALSE(lhs <= rhs);
76 REQUIRE_FALSE(lhs < rhs);
77 REQUIRE_FALSE(rhs == lhs);
78 REQUIRE_FALSE(rhs >= lhs);
79 REQUIRE_FALSE(rhs > lhs);
80}
81
82template <StronglyThreeWayComparable T>
83auto test_less_than(T const& lhs, T const& rhs) -> void {
84 REQUIRE((lhs <=> rhs) == std::strong_ordering::less);
85 REQUIRE(lhs != rhs);
86 REQUIRE(lhs <= rhs);
87 REQUIRE(lhs < rhs);
88 REQUIRE(rhs != lhs);
89 REQUIRE(rhs >= lhs);
90 REQUIRE(rhs > lhs);
91
92 REQUIRE_FALSE(lhs == rhs);
93 REQUIRE_FALSE(lhs >= rhs);
94 REQUIRE_FALSE(lhs > rhs);
95 REQUIRE_FALSE(rhs == lhs);
96 REQUIRE_FALSE(rhs <= lhs);
97 REQUIRE_FALSE(rhs < lhs);
98}
99
100template <StronglyThreeWayComparable T>
101auto pairwise_comparison_of_strictly_ascending_vector(std::vector<T> const& ordered_vector)
102 -> void {
103 for (size_t i{0}; i < ordered_vector.size(); i++) {
104 CAPTURE(i);
105 for (size_t j{0}; j < ordered_vector.size(); j++) {
106 CAPTURE(j);
107 if (i < j) {
108 test_less_than(ordered_vector[i], ordered_vector[j]);
109 } else if (i == j) {
110 test_equal(ordered_vector[i], ordered_vector[j]);
111 } else {
112 test_greater_than(ordered_vector[i], ordered_vector[j]);
113 }
114 }
115 }
116}
117} // namespace log_surgeon::tests
118
119#endif // LOG_SURGEON_TESTS_COMPARISON_TEST_UTILS_HPP
Definition comparison_test_utils.hpp:13
Definition comparison_test_utils.hpp:11
auto test_equal(T const &lhs, T const &rhs) -> void
Definition comparison_test_utils.hpp:47
auto pairwise_comparison_of_strictly_ascending_vector(std::vector< T > const &ordered_vector) -> void
Definition comparison_test_utils.hpp:101
auto test_less_than(T const &lhs, T const &rhs) -> void
Definition comparison_test_utils.hpp:83
auto test_greater_than(T const &lhs, T const &rhs) -> void
Definition comparison_test_utils.hpp:65