blob: 96a75039f3e280995b104793d93e4f8ec7a1e7ee [file] [log] [blame]
Marshall Clowc17628f2018-07-25 04:21:21 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clowc17628f2018-07-25 04:21:21 +00006//
7//===----------------------------------------------------------------------===//
8// A set of routines for testing the comparison operators of a type
9//
10// XXXX6 tests all six comparison operators
11// XXXX2 tests only op== and op!=
12//
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.
16
17
18#ifndef TEST_COMPARISONS_H
19#define TEST_COMPARISONS_H
20
21#include <type_traits>
22#include "test_macros.h"
23
24// Test all six comparison operations for sanity
Marshall Clowf3126c82019-01-15 00:05:05 +000025template <class T, class U = T>
26TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const U& t2, bool isEqual, bool isLess)
Marshall Clowc17628f2018-07-25 04:21:21 +000027{
28 if (isEqual)
29 {
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;
42 }
43 else if (isLess)
44 {
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;
57 }
58 else /* greater */
59 {
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;
72 }
73
74 return true;
75}
76
77// Easy call when you can init from something already comparable.
78template <class T, class Param>
79TEST_CONSTEXPR_CXX14 bool testComparisons6Values(Param val1, Param val2)
80{
81 const bool isEqual = val1 == val2;
82 const bool isLess = val1 < val2;
Stephan T. Lavavejdec89052018-11-14 03:06:06 +000083
Marshall Clow2fd30b42018-08-03 02:58:16 +000084 return testComparisons6(T(val1), T(val2), isEqual, isLess);
Marshall Clowc17628f2018-07-25 04:21:21 +000085}
86
Marshall Clowf3126c82019-01-15 00:05:05 +000087template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +000088void AssertComparisons6AreNoexcept()
89{
Marshall Clowf3126c82019-01-15 00:05:05 +000090 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&>());
Marshall Clowc17628f2018-07-25 04:21:21 +000096}
97
Marshall Clowf3126c82019-01-15 00:05:05 +000098template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +000099void AssertComparisons6ReturnBool()
100{
Marshall Clowf3126c82019-01-15 00:05:05 +0000101 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);
Marshall Clowc17628f2018-07-25 04:21:21 +0000107}
108
109
Marshall Clowf3126c82019-01-15 00:05:05 +0000110template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000111void AssertComparisons6ConvertibleToBool()
112{
Marshall Clowf3126c82019-01-15 00:05:05 +0000113 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), "");
Marshall Clowc17628f2018-07-25 04:21:21 +0000119}
120
Marshall Clowf3126c82019-01-15 00:05:05 +0000121// Test all two comparison operations for sanity
122template <class T, class U = T>
123TEST_CONSTEXPR_CXX14 bool testComparisons2(const T& t1, const U& t2, bool isEqual)
Marshall Clowc17628f2018-07-25 04:21:21 +0000124{
125 if (isEqual)
126 {
127 if (!(t1 == t2)) return false;
128 if (!(t2 == t1)) return false;
129 if ( (t1 != t2)) return false;
130 if ( (t2 != t1)) return false;
131 }
Marshall Clowf3126c82019-01-15 00:05:05 +0000132 else /* not equal */
Marshall Clowc17628f2018-07-25 04:21:21 +0000133 {
134 if ( (t1 == t2)) return false;
135 if ( (t2 == t1)) return false;
136 if (!(t1 != t2)) return false;
137 if (!(t2 != t1)) return false;
138 }
139
140 return true;
141}
142
143// Easy call when you can init from something already comparable.
144template <class T, class Param>
145TEST_CONSTEXPR_CXX14 bool testComparisons2Values(Param val1, Param val2)
146{
147 const bool isEqual = val1 == val2;
Stephan T. Lavavejdec89052018-11-14 03:06:06 +0000148
Marshall Clow2fd30b42018-08-03 02:58:16 +0000149 return testComparisons2(T(val1), T(val2), isEqual);
Marshall Clowc17628f2018-07-25 04:21:21 +0000150}
151
Marshall Clowf3126c82019-01-15 00:05:05 +0000152template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000153void AssertComparisons2AreNoexcept()
154{
Marshall Clowf3126c82019-01-15 00:05:05 +0000155 ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const U&>());
156 ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const U&>());
Marshall Clowc17628f2018-07-25 04:21:21 +0000157}
158
Marshall Clowf3126c82019-01-15 00:05:05 +0000159template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000160void AssertComparisons2ReturnBool()
161{
Marshall Clowf3126c82019-01-15 00:05:05 +0000162 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);
Marshall Clowc17628f2018-07-25 04:21:21 +0000164}
165
166
Marshall Clowf3126c82019-01-15 00:05:05 +0000167template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000168void AssertComparisons2ConvertibleToBool()
169{
Marshall Clowf3126c82019-01-15 00:05:05 +0000170 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), "");
Marshall Clowc17628f2018-07-25 04:21:21 +0000172}
173
174#endif // TEST_COMPARISONS_H