xref: /aosp_15_r20/external/libcxx/test/support/test_comparisons.h (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*58b9f456SAndroid Build Coastguard Worker //
5*58b9f456SAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*58b9f456SAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*58b9f456SAndroid Build Coastguard Worker //
8*58b9f456SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*58b9f456SAndroid Build Coastguard Worker //  A set of routines for testing the comparison operators of a type
10*58b9f456SAndroid Build Coastguard Worker //
11*58b9f456SAndroid Build Coastguard Worker //      XXXX6 tests all six comparison operators
12*58b9f456SAndroid Build Coastguard Worker //      XXXX2 tests only op== and op!=
13*58b9f456SAndroid Build Coastguard Worker //
14*58b9f456SAndroid Build Coastguard Worker //      AssertComparisonsXAreNoexcept       static_asserts that the operations are all noexcept.
15*58b9f456SAndroid Build Coastguard Worker //      AssertComparisonsXReturnBool        static_asserts that the operations return bool.
16*58b9f456SAndroid Build Coastguard Worker //      AssertComparisonsXConvertibleToBool static_asserts that the operations return something convertible to bool.
17*58b9f456SAndroid Build Coastguard Worker 
18*58b9f456SAndroid Build Coastguard Worker 
19*58b9f456SAndroid Build Coastguard Worker #ifndef TEST_COMPARISONS_H
20*58b9f456SAndroid Build Coastguard Worker #define TEST_COMPARISONS_H
21*58b9f456SAndroid Build Coastguard Worker 
22*58b9f456SAndroid Build Coastguard Worker #include <type_traits>
23*58b9f456SAndroid Build Coastguard Worker #include "test_macros.h"
24*58b9f456SAndroid Build Coastguard Worker 
25*58b9f456SAndroid Build Coastguard Worker //  Test all six comparison operations for sanity
26*58b9f456SAndroid Build Coastguard Worker template <class T>
testComparisons6(const T & t1,const T & t2,bool isEqual,bool isLess)27*58b9f456SAndroid Build Coastguard Worker TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const T& t2, bool isEqual, bool isLess)
28*58b9f456SAndroid Build Coastguard Worker {
29*58b9f456SAndroid Build Coastguard Worker     if (isEqual)
30*58b9f456SAndroid Build Coastguard Worker         {
31*58b9f456SAndroid Build Coastguard Worker         if (!(t1 == t2)) return false;
32*58b9f456SAndroid Build Coastguard Worker         if (!(t2 == t1)) return false;
33*58b9f456SAndroid Build Coastguard Worker         if ( (t1 != t2)) return false;
34*58b9f456SAndroid Build Coastguard Worker         if ( (t2 != t1)) return false;
35*58b9f456SAndroid Build Coastguard Worker         if ( (t1  < t2)) return false;
36*58b9f456SAndroid Build Coastguard Worker         if ( (t2  < t1)) return false;
37*58b9f456SAndroid Build Coastguard Worker         if (!(t1 <= t2)) return false;
38*58b9f456SAndroid Build Coastguard Worker         if (!(t2 <= t1)) return false;
39*58b9f456SAndroid Build Coastguard Worker         if ( (t1  > t2)) return false;
40*58b9f456SAndroid Build Coastguard Worker         if ( (t2  > t1)) return false;
41*58b9f456SAndroid Build Coastguard Worker         if (!(t1 >= t2)) return false;
42*58b9f456SAndroid Build Coastguard Worker         if (!(t2 >= t1)) return false;
43*58b9f456SAndroid Build Coastguard Worker         }
44*58b9f456SAndroid Build Coastguard Worker     else if (isLess)
45*58b9f456SAndroid Build Coastguard Worker         {
46*58b9f456SAndroid Build Coastguard Worker         if ( (t1 == t2)) return false;
47*58b9f456SAndroid Build Coastguard Worker         if ( (t2 == t1)) return false;
48*58b9f456SAndroid Build Coastguard Worker         if (!(t1 != t2)) return false;
49*58b9f456SAndroid Build Coastguard Worker         if (!(t2 != t1)) return false;
50*58b9f456SAndroid Build Coastguard Worker         if (!(t1  < t2)) return false;
51*58b9f456SAndroid Build Coastguard Worker         if ( (t2  < t1)) return false;
52*58b9f456SAndroid Build Coastguard Worker         if (!(t1 <= t2)) return false;
53*58b9f456SAndroid Build Coastguard Worker         if ( (t2 <= t1)) return false;
54*58b9f456SAndroid Build Coastguard Worker         if ( (t1  > t2)) return false;
55*58b9f456SAndroid Build Coastguard Worker         if (!(t2  > t1)) return false;
56*58b9f456SAndroid Build Coastguard Worker         if ( (t1 >= t2)) return false;
57*58b9f456SAndroid Build Coastguard Worker         if (!(t2 >= t1)) return false;
58*58b9f456SAndroid Build Coastguard Worker         }
59*58b9f456SAndroid Build Coastguard Worker     else /* greater */
60*58b9f456SAndroid Build Coastguard Worker         {
61*58b9f456SAndroid Build Coastguard Worker         if ( (t1 == t2)) return false;
62*58b9f456SAndroid Build Coastguard Worker         if ( (t2 == t1)) return false;
63*58b9f456SAndroid Build Coastguard Worker         if (!(t1 != t2)) return false;
64*58b9f456SAndroid Build Coastguard Worker         if (!(t2 != t1)) return false;
65*58b9f456SAndroid Build Coastguard Worker         if ( (t1  < t2)) return false;
66*58b9f456SAndroid Build Coastguard Worker         if (!(t2  < t1)) return false;
67*58b9f456SAndroid Build Coastguard Worker         if ( (t1 <= t2)) return false;
68*58b9f456SAndroid Build Coastguard Worker         if (!(t2 <= t1)) return false;
69*58b9f456SAndroid Build Coastguard Worker         if (!(t1  > t2)) return false;
70*58b9f456SAndroid Build Coastguard Worker         if ( (t2  > t1)) return false;
71*58b9f456SAndroid Build Coastguard Worker         if (!(t1 >= t2)) return false;
72*58b9f456SAndroid Build Coastguard Worker         if ( (t2 >= t1)) return false;
73*58b9f456SAndroid Build Coastguard Worker         }
74*58b9f456SAndroid Build Coastguard Worker 
75*58b9f456SAndroid Build Coastguard Worker     return true;
76*58b9f456SAndroid Build Coastguard Worker }
77*58b9f456SAndroid Build Coastguard Worker 
78*58b9f456SAndroid Build Coastguard Worker //  Easy call when you can init from something already comparable.
79*58b9f456SAndroid Build Coastguard Worker template <class T, class Param>
testComparisons6Values(Param val1,Param val2)80*58b9f456SAndroid Build Coastguard Worker TEST_CONSTEXPR_CXX14 bool testComparisons6Values(Param val1, Param val2)
81*58b9f456SAndroid Build Coastguard Worker {
82*58b9f456SAndroid Build Coastguard Worker     const bool isEqual = val1 == val2;
83*58b9f456SAndroid Build Coastguard Worker     const bool isLess  = val1  < val2;
84*58b9f456SAndroid Build Coastguard Worker 
85*58b9f456SAndroid Build Coastguard Worker     return testComparisons6(T(val1), T(val2), isEqual, isLess);
86*58b9f456SAndroid Build Coastguard Worker }
87*58b9f456SAndroid Build Coastguard Worker 
88*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons6AreNoexcept()89*58b9f456SAndroid Build Coastguard Worker void AssertComparisons6AreNoexcept()
90*58b9f456SAndroid Build Coastguard Worker {
91*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const T&>());
92*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const T&>());
93*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() <  std::declval<const T&>());
94*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() <= std::declval<const T&>());
95*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() >  std::declval<const T&>());
96*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() >= std::declval<const T&>());
97*58b9f456SAndroid Build Coastguard Worker }
98*58b9f456SAndroid Build Coastguard Worker 
99*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons6ReturnBool()100*58b9f456SAndroid Build Coastguard Worker void AssertComparisons6ReturnBool()
101*58b9f456SAndroid Build Coastguard Worker {
102*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() == std::declval<const T&>()), bool);
103*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() != std::declval<const T&>()), bool);
104*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() <  std::declval<const T&>()), bool);
105*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() <= std::declval<const T&>()), bool);
106*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() >  std::declval<const T&>()), bool);
107*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() >= std::declval<const T&>()), bool);
108*58b9f456SAndroid Build Coastguard Worker }
109*58b9f456SAndroid Build Coastguard Worker 
110*58b9f456SAndroid Build Coastguard Worker 
111*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons6ConvertibleToBool()112*58b9f456SAndroid Build Coastguard Worker void AssertComparisons6ConvertibleToBool()
113*58b9f456SAndroid Build Coastguard Worker {
114*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() == std::declval<const T&>()), bool>::value), "");
115*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const T&>()), bool>::value), "");
116*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() <  std::declval<const T&>()), bool>::value), "");
117*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() <= std::declval<const T&>()), bool>::value), "");
118*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() >  std::declval<const T&>()), bool>::value), "");
119*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() >= std::declval<const T&>()), bool>::value), "");
120*58b9f456SAndroid Build Coastguard Worker }
121*58b9f456SAndroid Build Coastguard Worker 
122*58b9f456SAndroid Build Coastguard Worker //  Test all six comparison operations for sanity
123*58b9f456SAndroid Build Coastguard Worker template <class T>
testComparisons2(const T & t1,const T & t2,bool isEqual)124*58b9f456SAndroid Build Coastguard Worker TEST_CONSTEXPR_CXX14 bool testComparisons2(const T& t1, const T& t2, bool isEqual)
125*58b9f456SAndroid Build Coastguard Worker {
126*58b9f456SAndroid Build Coastguard Worker     if (isEqual)
127*58b9f456SAndroid Build Coastguard Worker         {
128*58b9f456SAndroid Build Coastguard Worker         if (!(t1 == t2)) return false;
129*58b9f456SAndroid Build Coastguard Worker         if (!(t2 == t1)) return false;
130*58b9f456SAndroid Build Coastguard Worker         if ( (t1 != t2)) return false;
131*58b9f456SAndroid Build Coastguard Worker         if ( (t2 != t1)) return false;
132*58b9f456SAndroid Build Coastguard Worker         }
133*58b9f456SAndroid Build Coastguard Worker     else /* greater */
134*58b9f456SAndroid Build Coastguard Worker         {
135*58b9f456SAndroid Build Coastguard Worker         if ( (t1 == t2)) return false;
136*58b9f456SAndroid Build Coastguard Worker         if ( (t2 == t1)) return false;
137*58b9f456SAndroid Build Coastguard Worker         if (!(t1 != t2)) return false;
138*58b9f456SAndroid Build Coastguard Worker         if (!(t2 != t1)) return false;
139*58b9f456SAndroid Build Coastguard Worker         }
140*58b9f456SAndroid Build Coastguard Worker 
141*58b9f456SAndroid Build Coastguard Worker     return true;
142*58b9f456SAndroid Build Coastguard Worker }
143*58b9f456SAndroid Build Coastguard Worker 
144*58b9f456SAndroid Build Coastguard Worker //  Easy call when you can init from something already comparable.
145*58b9f456SAndroid Build Coastguard Worker template <class T, class Param>
testComparisons2Values(Param val1,Param val2)146*58b9f456SAndroid Build Coastguard Worker TEST_CONSTEXPR_CXX14 bool testComparisons2Values(Param val1, Param val2)
147*58b9f456SAndroid Build Coastguard Worker {
148*58b9f456SAndroid Build Coastguard Worker     const bool isEqual = val1 == val2;
149*58b9f456SAndroid Build Coastguard Worker 
150*58b9f456SAndroid Build Coastguard Worker     return testComparisons2(T(val1), T(val2), isEqual);
151*58b9f456SAndroid Build Coastguard Worker }
152*58b9f456SAndroid Build Coastguard Worker 
153*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons2AreNoexcept()154*58b9f456SAndroid Build Coastguard Worker void AssertComparisons2AreNoexcept()
155*58b9f456SAndroid Build Coastguard Worker {
156*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const T&>());
157*58b9f456SAndroid Build Coastguard Worker     ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const T&>());
158*58b9f456SAndroid Build Coastguard Worker }
159*58b9f456SAndroid Build Coastguard Worker 
160*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons2ReturnBool()161*58b9f456SAndroid Build Coastguard Worker void AssertComparisons2ReturnBool()
162*58b9f456SAndroid Build Coastguard Worker {
163*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() == std::declval<const T&>()), bool);
164*58b9f456SAndroid Build Coastguard Worker     ASSERT_SAME_TYPE(decltype(std::declval<const T&>() != std::declval<const T&>()), bool);
165*58b9f456SAndroid Build Coastguard Worker }
166*58b9f456SAndroid Build Coastguard Worker 
167*58b9f456SAndroid Build Coastguard Worker 
168*58b9f456SAndroid Build Coastguard Worker template <class T>
AssertComparisons2ConvertibleToBool()169*58b9f456SAndroid Build Coastguard Worker void AssertComparisons2ConvertibleToBool()
170*58b9f456SAndroid Build Coastguard Worker {
171*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() == std::declval<const T&>()), bool>::value), "");
172*58b9f456SAndroid Build Coastguard Worker     static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const T&>()), bool>::value), "");
173*58b9f456SAndroid Build Coastguard Worker }
174*58b9f456SAndroid Build Coastguard Worker 
175*58b9f456SAndroid Build Coastguard Worker #endif // TEST_COMPARISONS_H
176