blob: cf094eb65b82b0f97a255d49f6013344f8d300d7 [file] [log] [blame]
Marshall Clowc17628f2018-07-25 04:21:21 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// A set of routines for testing the comparison operators of a type
10//
11// XXXX6 tests all six comparison operators
12// XXXX2 tests only op== and op!=
13//
14// AssertComparisonsXAreNoexcept static_asserts that the operations are all noexcept.
15// AssertComparisonsXReturnBool static_asserts that the operations return bool.
16// AssertComparisonsXConvertibleToBool static_asserts that the operations return something convertible to bool.
17
18
19#ifndef TEST_COMPARISONS_H
20#define TEST_COMPARISONS_H
21
22#include <type_traits>
23#include "test_macros.h"
24
25// Test all six comparison operations for sanity
Marshall Clowf3126c82019-01-15 00:05:05 +000026template <class T, class U = T>
27TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const U& t2, bool isEqual, bool isLess)
Marshall Clowc17628f2018-07-25 04:21:21 +000028{
29 if (isEqual)
30 {
31 if (!(t1 == t2)) return false;
32 if (!(t2 == t1)) return false;
33 if ( (t1 != t2)) return false;
34 if ( (t2 != t1)) return false;
35 if ( (t1 < t2)) return false;
36 if ( (t2 < t1)) return false;
37 if (!(t1 <= t2)) return false;
38 if (!(t2 <= t1)) return false;
39 if ( (t1 > t2)) return false;
40 if ( (t2 > t1)) return false;
41 if (!(t1 >= t2)) return false;
42 if (!(t2 >= t1)) return false;
43 }
44 else if (isLess)
45 {
46 if ( (t1 == t2)) return false;
47 if ( (t2 == t1)) return false;
48 if (!(t1 != t2)) return false;
49 if (!(t2 != t1)) return false;
50 if (!(t1 < t2)) return false;
51 if ( (t2 < t1)) return false;
52 if (!(t1 <= t2)) return false;
53 if ( (t2 <= t1)) return false;
54 if ( (t1 > t2)) return false;
55 if (!(t2 > t1)) return false;
56 if ( (t1 >= t2)) return false;
57 if (!(t2 >= t1)) return false;
58 }
59 else /* greater */
60 {
61 if ( (t1 == t2)) return false;
62 if ( (t2 == t1)) return false;
63 if (!(t1 != t2)) return false;
64 if (!(t2 != t1)) return false;
65 if ( (t1 < t2)) return false;
66 if (!(t2 < t1)) return false;
67 if ( (t1 <= t2)) return false;
68 if (!(t2 <= t1)) return false;
69 if (!(t1 > t2)) return false;
70 if ( (t2 > t1)) return false;
71 if (!(t1 >= t2)) return false;
72 if ( (t2 >= t1)) return false;
73 }
74
75 return true;
76}
77
78// Easy call when you can init from something already comparable.
79template <class T, class Param>
80TEST_CONSTEXPR_CXX14 bool testComparisons6Values(Param val1, Param val2)
81{
82 const bool isEqual = val1 == val2;
83 const bool isLess = val1 < val2;
Stephan T. Lavavejdec89052018-11-14 03:06:06 +000084
Marshall Clow2fd30b42018-08-03 02:58:16 +000085 return testComparisons6(T(val1), T(val2), isEqual, isLess);
Marshall Clowc17628f2018-07-25 04:21:21 +000086}
87
Marshall Clowf3126c82019-01-15 00:05:05 +000088template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +000089void AssertComparisons6AreNoexcept()
90{
Marshall Clowf3126c82019-01-15 00:05:05 +000091 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&>());
96 ASSERT_NOEXCEPT(std::declval<const T&>() >= std::declval<const U&>());
Marshall Clowc17628f2018-07-25 04:21:21 +000097}
98
Marshall Clowf3126c82019-01-15 00:05:05 +000099template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000100void AssertComparisons6ReturnBool()
101{
Marshall Clowf3126c82019-01-15 00:05:05 +0000102 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);
107 ASSERT_SAME_TYPE(decltype(std::declval<const T&>() >= std::declval<const U&>()), bool);
Marshall Clowc17628f2018-07-25 04:21:21 +0000108}
109
110
Marshall Clowf3126c82019-01-15 00:05:05 +0000111template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000112void AssertComparisons6ConvertibleToBool()
113{
Marshall Clowf3126c82019-01-15 00:05:05 +0000114 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), "");
119 static_assert((std::is_convertible<decltype(std::declval<const T&>() >= std::declval<const U&>()), bool>::value), "");
Marshall Clowc17628f2018-07-25 04:21:21 +0000120}
121
Marshall Clowf3126c82019-01-15 00:05:05 +0000122// Test all two comparison operations for sanity
123template <class T, class U = T>
124TEST_CONSTEXPR_CXX14 bool testComparisons2(const T& t1, const U& t2, bool isEqual)
Marshall Clowc17628f2018-07-25 04:21:21 +0000125{
126 if (isEqual)
127 {
128 if (!(t1 == t2)) return false;
129 if (!(t2 == t1)) return false;
130 if ( (t1 != t2)) return false;
131 if ( (t2 != t1)) return false;
132 }
Marshall Clowf3126c82019-01-15 00:05:05 +0000133 else /* not equal */
Marshall Clowc17628f2018-07-25 04:21:21 +0000134 {
135 if ( (t1 == t2)) return false;
136 if ( (t2 == t1)) return false;
137 if (!(t1 != t2)) return false;
138 if (!(t2 != t1)) return false;
139 }
140
141 return true;
142}
143
144// Easy call when you can init from something already comparable.
145template <class T, class Param>
146TEST_CONSTEXPR_CXX14 bool testComparisons2Values(Param val1, Param val2)
147{
148 const bool isEqual = val1 == val2;
Stephan T. Lavavejdec89052018-11-14 03:06:06 +0000149
Marshall Clow2fd30b42018-08-03 02:58:16 +0000150 return testComparisons2(T(val1), T(val2), isEqual);
Marshall Clowc17628f2018-07-25 04:21:21 +0000151}
152
Marshall Clowf3126c82019-01-15 00:05:05 +0000153template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000154void AssertComparisons2AreNoexcept()
155{
Marshall Clowf3126c82019-01-15 00:05:05 +0000156 ASSERT_NOEXCEPT(std::declval<const T&>() == std::declval<const U&>());
157 ASSERT_NOEXCEPT(std::declval<const T&>() != std::declval<const U&>());
Marshall Clowc17628f2018-07-25 04:21:21 +0000158}
159
Marshall Clowf3126c82019-01-15 00:05:05 +0000160template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000161void AssertComparisons2ReturnBool()
162{
Marshall Clowf3126c82019-01-15 00:05:05 +0000163 ASSERT_SAME_TYPE(decltype(std::declval<const T&>() == std::declval<const U&>()), bool);
164 ASSERT_SAME_TYPE(decltype(std::declval<const T&>() != std::declval<const U&>()), bool);
Marshall Clowc17628f2018-07-25 04:21:21 +0000165}
166
167
Marshall Clowf3126c82019-01-15 00:05:05 +0000168template <class T, class U = T>
Marshall Clowc17628f2018-07-25 04:21:21 +0000169void AssertComparisons2ConvertibleToBool()
170{
Marshall Clowf3126c82019-01-15 00:05:05 +0000171 static_assert((std::is_convertible<decltype(std::declval<const T&>() == std::declval<const U&>()), bool>::value), "");
172 static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const U&>()), bool>::value), "");
Marshall Clowc17628f2018-07-25 04:21:21 +0000173}
174
175#endif // TEST_COMPARISONS_H