Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check_macros_test.cc « ruy - github.com/google/ruy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e7508f1002b25d877a82f4a6c06291fdd82a6fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright 2019 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "ruy/check_macros.h"

#include "ruy/gtest_wrapper.h"

namespace {

#define TEST_CONDITION_FOR_FAMILY(family, vacuously_succeeds, condition) \
  do {                                                                   \
    if (vacuously_succeeds || (condition)) {                             \
      RUY_##family(condition);                                           \
    }                                                                    \
  } while (false)

#define TEST_COMPARISON_FOR_FAMILY(family, vacuously_succeeds, op_name, x, op, \
                                   y)                                          \
  do {                                                                         \
    if (vacuously_succeeds || ((x)op(y))) {                                    \
      RUY_##family##_##op_name(x, y);                                          \
    }                                                                          \
  } while (false)

#ifdef NDEBUG
#define TEST_CONDITION(condition)                       \
  do {                                                  \
    TEST_CONDITION_FOR_FAMILY(CHECK, false, condition); \
  } while (false)
#define TEST_COMPARISON(op_name, x, op, y)                       \
  do {                                                           \
    TEST_COMPARISON_FOR_FAMILY(CHECK, false, op_name, x, op, y); \
  } while (false)
#else
#define TEST_CONDITION(condition)                        \
  do {                                                   \
    TEST_CONDITION_FOR_FAMILY(CHECK, false, condition);  \
    TEST_CONDITION_FOR_FAMILY(DCHECK, false, condition); \
  } while (false)
#define TEST_COMPARISON(op_name, x, op, y)                        \
  do {                                                            \
    TEST_COMPARISON_FOR_FAMILY(CHECK, false, op_name, x, op, y);  \
    TEST_COMPARISON_FOR_FAMILY(DCHECK, false, op_name, x, op, y); \
  } while (false)

#endif

template <typename LhsType, typename RhsType>
void TestEqualityComparisons(const LhsType& lhs, const RhsType& rhs) {
  RUY_CHECK_EQ(lhs, lhs);
  TEST_COMPARISON(EQ, lhs, ==, lhs);
  RUY_CHECK_EQ(lhs, lhs);
  RUY_CHECK_EQ(lhs, lhs);
  if (lhs == rhs) {
    RUY_CHECK_EQ(lhs, rhs);
  }
  if (lhs != rhs) {
    RUY_CHECK_NE(lhs, rhs);
  }
}

template <typename LhsType, typename RhsType>
void TestComparisons(const LhsType& lhs, const RhsType& rhs) {
  TestEqualityComparisons(lhs, rhs);
  if (lhs > rhs) {
    RUY_CHECK_GT(lhs, rhs);
  }
  if (lhs >= rhs) {
    RUY_CHECK_GE(lhs, rhs);
  }
  if (lhs < rhs) {
    RUY_CHECK_LT(lhs, rhs);
  }
  if (lhs <= rhs) {
    RUY_CHECK_LE(lhs, rhs);
  }
}

TEST(CheckMacrosTest, IntInt) {
  TestComparisons(0, 0);
  TestComparisons(0, 1);
  TestComparisons(1, -1);
  TestComparisons(-1, 0);
  TestComparisons(123, -456);
  TestComparisons(std::numeric_limits<int>::min(),
                  std::numeric_limits<int>::max());
  TestComparisons(123, std::numeric_limits<int>::max());
  TestComparisons(123, std::numeric_limits<int>::min());
}

TEST(CheckMacrosTest, Uint8Uint8) {
  TestComparisons<std::uint8_t, std::uint8_t>(0, 0);
  TestComparisons<std::uint8_t, std::uint8_t>(255, 0);
  TestComparisons<std::uint8_t, std::uint8_t>(0, 255);
  TestComparisons<std::uint8_t, std::uint8_t>(12, 34);
}

TEST(CheckMacrosTest, Uint8Int) {
  TestComparisons<std::uint8_t, int>(0, std::numeric_limits<int>::min());
  TestComparisons<std::uint8_t, int>(255, std::numeric_limits<int>::min());
  TestComparisons<std::uint8_t, int>(0, std::numeric_limits<int>::max());
  TestComparisons<std::uint8_t, int>(255, std::numeric_limits<int>::max());
}

TEST(CheckMacrosTest, FloatFloat) {
  TestComparisons(0.f, 0.f);
  TestComparisons(0.f, 1.f);
  TestComparisons(1.f, -1.f);
  TestComparisons(-1.f, 0.f);
  TestComparisons(123.f, -456.f);
  TestComparisons(std::numeric_limits<float>::lowest(),
                  std::numeric_limits<float>::max());
  TestComparisons(123.f, std::numeric_limits<float>::max());
  TestComparisons(123.f, std::numeric_limits<float>::lowest());
}

TEST(CheckMacrosTest, IntFloat) {
  TestComparisons(0, 0.f);
  TestComparisons(0, 1.f);
  TestComparisons(1, -1.f);
  TestComparisons(-1, 0.f);
  TestComparisons(123, -456.f);
  TestComparisons(std::numeric_limits<int>::lowest(),
                  std::numeric_limits<float>::max());
  TestComparisons(123, std::numeric_limits<float>::max());
  TestComparisons(123, std::numeric_limits<float>::lowest());
}

TEST(CheckMacrosTest, EnumClass) {
  enum class SomeEnumClass { kA, kB, kC };
  TestEqualityComparisons(SomeEnumClass::kA, SomeEnumClass::kA);
  TestEqualityComparisons(SomeEnumClass::kA, SomeEnumClass::kB);
  TestEqualityComparisons(SomeEnumClass::kC, SomeEnumClass::kB);
}

}  // namespace

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}