Skip to content

Commit 831a691

Browse files
committed
test: improve decomposer integral comparison operators
1 parent 17d5da6 commit 831a691

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/test_suite/detail/decomposer.hpp

+53-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <type_traits>
1212
#include <string_view>
1313
#include <sstream>
14+
#include <utility>
1415

1516
#if defined(__has_include) && __has_include(<fmt/format.h>)
1617
#include <fmt/format.h>
@@ -112,6 +113,10 @@ namespace test_suite::detail
112113
{
113114
T lhs_;
114115

116+
template <class U>
117+
static constexpr bool integral_comparison =
118+
std::integral<std::decay_t<T>> && std::integral<std::decay_t<U>>;
119+
115120
public:
116121
explicit first_operand(T lhs) : lhs_(lhs) {}
117122

@@ -120,47 +125,89 @@ namespace test_suite::detail
120125
binary_operands<T, U const &>
121126
operator==(first_operand &&lhs, U &&rhs)
122127
{
123-
return {static_cast<bool>( lhs.lhs_ == rhs ), lhs.lhs_, "==", rhs};
128+
if constexpr (integral_comparison<U>)
129+
{
130+
return {std::cmp_equal( lhs.lhs_, rhs ), lhs.lhs_, "==", rhs};
131+
}
132+
else
133+
{
134+
return {static_cast<bool>( lhs.lhs_ == rhs ), lhs.lhs_, "==", rhs};
135+
}
124136
}
125137

126138
template<class U>
127139
friend
128140
binary_operands<T, U const &>
129141
operator!=(first_operand &&lhs, U &&rhs)
130142
{
131-
return {static_cast<bool>( lhs.lhs_ != rhs ), lhs.lhs_, "!=", rhs};
143+
if constexpr (integral_comparison<U>)
144+
{
145+
return {std::cmp_not_equal( lhs.lhs_, rhs ), lhs.lhs_, "!=", rhs};
146+
}
147+
else
148+
{
149+
return {static_cast<bool>( lhs.lhs_ != rhs ), lhs.lhs_, "!=", rhs};
150+
}
132151
}
133152

134153
template<class U>
135154
friend
136155
binary_operands<T, U const &>
137156
operator<(first_operand &&lhs, U &&rhs)
138157
{
139-
return {static_cast<bool>( lhs.lhs_ < rhs ), lhs.lhs_, "<", rhs};
158+
if constexpr (integral_comparison<U>)
159+
{
160+
return {std::cmp_less( lhs.lhs_, rhs ), lhs.lhs_, "<", rhs};
161+
}
162+
else
163+
{
164+
return {static_cast<bool>( lhs.lhs_ < rhs ), lhs.lhs_, "<", rhs};
165+
}
140166
}
141167

142168
template<class U>
143169
friend
144170
binary_operands<T, U const &>
145171
operator<=(first_operand &&lhs, U &&rhs)
146172
{
147-
return {static_cast<bool>( lhs.lhs_ <= rhs ), lhs.lhs_, "<=", rhs};
173+
if constexpr (integral_comparison<U>)
174+
{
175+
return {std::cmp_less_equal( lhs.lhs_, rhs ), lhs.lhs_, "<=", rhs};
176+
}
177+
else
178+
{
179+
return {static_cast<bool>( lhs.lhs_ <= rhs ), lhs.lhs_, "<=", rhs};
180+
}
148181
}
149182

150183
template<class U>
151184
friend
152185
binary_operands<T, U const &>
153186
operator>(first_operand &&lhs, U &&rhs)
154187
{
155-
return {static_cast<bool>( lhs.lhs_ > rhs ), lhs.lhs_, ">", rhs};
188+
if constexpr (integral_comparison<U>)
189+
{
190+
return {std::cmp_greater( lhs.lhs_, rhs ), lhs.lhs_, ">", rhs};
191+
}
192+
else
193+
{
194+
return {static_cast<bool>( lhs.lhs_ > rhs ), lhs.lhs_, ">", rhs};
195+
}
156196
}
157197

158198
template<class U>
159199
friend
160200
binary_operands<T, U const &>
161201
operator>=(first_operand &&lhs, U &&rhs)
162202
{
163-
return {static_cast<bool>( lhs.lhs_ >= rhs ), lhs.lhs_, ">=", rhs};
203+
if constexpr (integral_comparison<U>)
204+
{
205+
return {std::cmp_greater_equal( lhs.lhs_, rhs ), lhs.lhs_, ">=", rhs};
206+
}
207+
else
208+
{
209+
return {static_cast<bool>( lhs.lhs_ >= rhs ), lhs.lhs_, ">=", rhs};
210+
}
164211
}
165212

166213
template<class U>

0 commit comments

Comments
 (0)