1 /* Copyright 2019 Google LLC. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "ruy/check_macros.h"
17
18 #include "ruy/gtest_wrapper.h"
19
20 namespace {
21
22 #define TEST_CONDITION_FOR_FAMILY(family, vacuously_succeeds, condition) \
23 do { \
24 if (vacuously_succeeds || (condition)) { \
25 RUY_##family(condition); \
26 } \
27 } while (false)
28
29 #define TEST_COMPARISON_FOR_FAMILY(family, vacuously_succeeds, op_name, x, op, \
30 y) \
31 do { \
32 if (vacuously_succeeds || ((x)op(y))) { \
33 RUY_##family##_##op_name(x, y); \
34 } \
35 } while (false)
36
37 #ifdef NDEBUG
38 #define TEST_CONDITION(condition) \
39 do { \
40 TEST_CONDITION_FOR_FAMILY(CHECK, false, condition); \
41 } while (false)
42 #define TEST_COMPARISON(op_name, x, op, y) \
43 do { \
44 TEST_COMPARISON_FOR_FAMILY(CHECK, false, op_name, x, op, y); \
45 } while (false)
46 #else
47 #define TEST_CONDITION(condition) \
48 do { \
49 TEST_CONDITION_FOR_FAMILY(CHECK, false, condition); \
50 TEST_CONDITION_FOR_FAMILY(DCHECK, false, condition); \
51 } while (false)
52 #define TEST_COMPARISON(op_name, x, op, y) \
53 do { \
54 TEST_COMPARISON_FOR_FAMILY(CHECK, false, op_name, x, op, y); \
55 TEST_COMPARISON_FOR_FAMILY(DCHECK, false, op_name, x, op, y); \
56 } while (false)
57
58 #endif
59
60 template <typename LhsType, typename RhsType>
TestEqualityComparisons(const LhsType & lhs,const RhsType & rhs)61 void TestEqualityComparisons(const LhsType& lhs, const RhsType& rhs) {
62 RUY_CHECK_EQ(lhs, lhs);
63 TEST_COMPARISON(EQ, lhs, ==, lhs);
64 RUY_CHECK_EQ(lhs, lhs);
65 RUY_CHECK_EQ(lhs, lhs);
66 if (lhs == rhs) {
67 RUY_CHECK_EQ(lhs, rhs);
68 }
69 if (lhs != rhs) {
70 RUY_CHECK_NE(lhs, rhs);
71 }
72 }
73
74 template <typename LhsType, typename RhsType>
TestComparisons(const LhsType & lhs,const RhsType & rhs)75 void TestComparisons(const LhsType& lhs, const RhsType& rhs) {
76 TestEqualityComparisons(lhs, rhs);
77 if (lhs > rhs) {
78 RUY_CHECK_GT(lhs, rhs);
79 }
80 if (lhs >= rhs) {
81 RUY_CHECK_GE(lhs, rhs);
82 }
83 if (lhs < rhs) {
84 RUY_CHECK_LT(lhs, rhs);
85 }
86 if (lhs <= rhs) {
87 RUY_CHECK_LE(lhs, rhs);
88 }
89 }
90
TEST(CheckMacrosTest,IntInt)91 TEST(CheckMacrosTest, IntInt) {
92 TestComparisons(0, 0);
93 TestComparisons(0, 1);
94 TestComparisons(1, -1);
95 TestComparisons(-1, 0);
96 TestComparisons(123, -456);
97 TestComparisons(std::numeric_limits<int>::min(),
98 std::numeric_limits<int>::max());
99 TestComparisons(123, std::numeric_limits<int>::max());
100 TestComparisons(123, std::numeric_limits<int>::min());
101 }
102
TEST(CheckMacrosTest,Uint8Uint8)103 TEST(CheckMacrosTest, Uint8Uint8) {
104 TestComparisons<std::uint8_t, std::uint8_t>(0, 0);
105 TestComparisons<std::uint8_t, std::uint8_t>(255, 0);
106 TestComparisons<std::uint8_t, std::uint8_t>(0, 255);
107 TestComparisons<std::uint8_t, std::uint8_t>(12, 34);
108 }
109
TEST(CheckMacrosTest,Uint8Int)110 TEST(CheckMacrosTest, Uint8Int) {
111 TestComparisons<std::uint8_t, int>(0, std::numeric_limits<int>::min());
112 TestComparisons<std::uint8_t, int>(255, std::numeric_limits<int>::min());
113 TestComparisons<std::uint8_t, int>(0, std::numeric_limits<int>::max());
114 TestComparisons<std::uint8_t, int>(255, std::numeric_limits<int>::max());
115 }
116
TEST(CheckMacrosTest,FloatFloat)117 TEST(CheckMacrosTest, FloatFloat) {
118 TestComparisons(0.f, 0.f);
119 TestComparisons(0.f, 1.f);
120 TestComparisons(1.f, -1.f);
121 TestComparisons(-1.f, 0.f);
122 TestComparisons(123.f, -456.f);
123 TestComparisons(std::numeric_limits<float>::lowest(),
124 std::numeric_limits<float>::max());
125 TestComparisons(123.f, std::numeric_limits<float>::max());
126 TestComparisons(123.f, std::numeric_limits<float>::lowest());
127 }
128
TEST(CheckMacrosTest,IntFloat)129 TEST(CheckMacrosTest, IntFloat) {
130 TestComparisons(0, 0.f);
131 TestComparisons(0, 1.f);
132 TestComparisons(1, -1.f);
133 TestComparisons(-1, 0.f);
134 TestComparisons(123, -456.f);
135 TestComparisons(std::numeric_limits<int>::lowest(),
136 std::numeric_limits<float>::max());
137 TestComparisons(123, std::numeric_limits<float>::max());
138 TestComparisons(123, std::numeric_limits<float>::lowest());
139 }
140
TEST(CheckMacrosTest,EnumClass)141 TEST(CheckMacrosTest, EnumClass) {
142 enum class SomeEnumClass { kA, kB, kC };
143 TestEqualityComparisons(SomeEnumClass::kA, SomeEnumClass::kA);
144 TestEqualityComparisons(SomeEnumClass::kA, SomeEnumClass::kB);
145 TestEqualityComparisons(SomeEnumClass::kC, SomeEnumClass::kB);
146 }
147
148 } // namespace
149
main(int argc,char ** argv)150 int main(int argc, char** argv) {
151 ::testing::InitGoogleTest(&argc, argv);
152 return RUN_ALL_TESTS();
153 }
154