1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // A set of routines for testing the comparison operators of a type
10 // XXXX6 tests all six comparison operators
11 // XXXX2 tests only op== and op!=
13 // AssertComparisonsXAreNoexcept static_asserts that the operations are all noexcept.
14 // AssertComparisonsXReturnBool static_asserts that the operations return bool.
15 // AssertComparisonsXConvertibleToBool static_asserts that the operations return something convertible to bool.
18 #ifndef TEST_COMPARISONS_H
19 #define TEST_COMPARISONS_H
21 #include <type_traits>
22 #include "test_macros.h"
24 // Test all six comparison operations for sanity
25 template <class T
, class U
= T
>
26 TEST_CONSTEXPR_CXX14
bool testComparisons6(const T
& t1
, const U
& t2
, bool isEqual
, bool isLess
)
30 if (!(t1
== t2
)) return false;
31 if (!(t2
== t1
)) return false;
32 if ( (t1
!= t2
)) return false;
33 if ( (t2
!= t1
)) return false;
34 if ( (t1
< t2
)) return false;
35 if ( (t2
< t1
)) return false;
36 if (!(t1
<= t2
)) return false;
37 if (!(t2
<= t1
)) return false;
38 if ( (t1
> t2
)) return false;
39 if ( (t2
> t1
)) return false;
40 if (!(t1
>= t2
)) return false;
41 if (!(t2
>= t1
)) return false;
45 if ( (t1
== t2
)) return false;
46 if ( (t2
== t1
)) return false;
47 if (!(t1
!= t2
)) return false;
48 if (!(t2
!= t1
)) return false;
49 if (!(t1
< t2
)) return false;
50 if ( (t2
< t1
)) return false;
51 if (!(t1
<= t2
)) return false;
52 if ( (t2
<= t1
)) return false;
53 if ( (t1
> t2
)) return false;
54 if (!(t2
> t1
)) return false;
55 if ( (t1
>= t2
)) return false;
56 if (!(t2
>= t1
)) return false;
60 if ( (t1
== t2
)) return false;
61 if ( (t2
== t1
)) return false;
62 if (!(t1
!= t2
)) return false;
63 if (!(t2
!= t1
)) return false;
64 if ( (t1
< t2
)) return false;
65 if (!(t2
< t1
)) return false;
66 if ( (t1
<= t2
)) return false;
67 if (!(t2
<= t1
)) return false;
68 if (!(t1
> t2
)) return false;
69 if ( (t2
> t1
)) return false;
70 if (!(t1
>= t2
)) return false;
71 if ( (t2
>= t1
)) return false;
77 // Easy call when you can init from something already comparable.
78 template <class T
, class Param
>
79 TEST_CONSTEXPR_CXX14
bool testComparisons6Values(Param val1
, Param val2
)
81 const bool isEqual
= val1
== val2
;
82 const bool isLess
= val1
< val2
;
84 return testComparisons6(T(val1
), T(val2
), isEqual
, isLess
);
87 template <class T
, class U
= T
>
88 void AssertComparisons6AreNoexcept()
90 ASSERT_NOEXCEPT(std::declval
<const T
&>() == std::declval
<const U
&>());
91 ASSERT_NOEXCEPT(std::declval
<const T
&>() != std::declval
<const U
&>());
92 ASSERT_NOEXCEPT(std::declval
<const T
&>() < std::declval
<const U
&>());
93 ASSERT_NOEXCEPT(std::declval
<const T
&>() <= std::declval
<const U
&>());
94 ASSERT_NOEXCEPT(std::declval
<const T
&>() > std::declval
<const U
&>());
95 ASSERT_NOEXCEPT(std::declval
<const T
&>() >= std::declval
<const U
&>());
98 template <class T
, class U
= T
>
99 void AssertComparisons6ReturnBool()
101 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() == std::declval
<const U
&>()), bool);
102 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() != std::declval
<const U
&>()), bool);
103 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() < std::declval
<const U
&>()), bool);
104 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() <= std::declval
<const U
&>()), bool);
105 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() > std::declval
<const U
&>()), bool);
106 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() >= std::declval
<const U
&>()), bool);
110 template <class T
, class U
= T
>
111 void AssertComparisons6ConvertibleToBool()
113 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() == std::declval
<const U
&>()), bool>::value
), "");
114 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() != std::declval
<const U
&>()), bool>::value
), "");
115 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() < std::declval
<const U
&>()), bool>::value
), "");
116 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() <= std::declval
<const U
&>()), bool>::value
), "");
117 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() > std::declval
<const U
&>()), bool>::value
), "");
118 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() >= std::declval
<const U
&>()), bool>::value
), "");
121 // Test all two comparison operations for sanity
122 template <class T
, class U
= T
>
123 TEST_CONSTEXPR_CXX14
bool testComparisons2(const T
& t1
, const U
& t2
, bool isEqual
)
127 if (!(t1
== t2
)) return false;
128 if (!(t2
== t1
)) return false;
129 if ( (t1
!= t2
)) return false;
130 if ( (t2
!= t1
)) return false;
134 if ( (t1
== t2
)) return false;
135 if ( (t2
== t1
)) return false;
136 if (!(t1
!= t2
)) return false;
137 if (!(t2
!= t1
)) return false;
143 // Easy call when you can init from something already comparable.
144 template <class T
, class Param
>
145 TEST_CONSTEXPR_CXX14
bool testComparisons2Values(Param val1
, Param val2
)
147 const bool isEqual
= val1
== val2
;
149 return testComparisons2(T(val1
), T(val2
), isEqual
);
152 template <class T
, class U
= T
>
153 void AssertComparisons2AreNoexcept()
155 ASSERT_NOEXCEPT(std::declval
<const T
&>() == std::declval
<const U
&>());
156 ASSERT_NOEXCEPT(std::declval
<const T
&>() != std::declval
<const U
&>());
159 template <class T
, class U
= T
>
160 void AssertComparisons2ReturnBool()
162 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() == std::declval
<const U
&>()), bool);
163 ASSERT_SAME_TYPE(decltype(std::declval
<const T
&>() != std::declval
<const U
&>()), bool);
167 template <class T
, class U
= T
>
168 void AssertComparisons2ConvertibleToBool()
170 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() == std::declval
<const U
&>()), bool>::value
), "");
171 static_assert((std::is_convertible
<decltype(std::declval
<const T
&>() != std::declval
<const U
&>()), bool>::value
), "");
174 #endif // TEST_COMPARISONS_H