1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "SizeTest"
18
19 #include <cmath>
20 #include <cstdlib>
21
22 #ifdef __clang__
23 #pragma clang diagnostic push
24 #pragma clang diagnostic error "-Wimplicit-int-float-conversion"
25 #pragma clang diagnostic error "-Wconversion"
26 #endif // __clang__
27
28 #include <ui/Size.h>
29
30 #ifdef __clang__
31 #pragma clang diagnostic pop
32 #endif // __clang__
33
34 #include <gtest/gtest.h>
35
36 namespace android::ui {
37
TEST(SizeTest,BasicConstructionAndEqualityComparison)38 TEST(SizeTest, BasicConstructionAndEqualityComparison) {
39 Size s(123, 456);
40
41 EXPECT_EQ(123, s.width);
42 EXPECT_EQ(123, s.getWidth());
43
44 EXPECT_EQ(456, s.height);
45 EXPECT_EQ(456, s.getHeight());
46
47 EXPECT_EQ(Size(123, 456), s);
48 EXPECT_NE(Size(456, 123), s);
49 }
50
TEST(SizeTest,BasicLessThanComparison)51 TEST(SizeTest, BasicLessThanComparison) {
52 EXPECT_TRUE(Size(0, 1) < Size(2, 3));
53 EXPECT_FALSE(Size(2, 3) < Size(0, 1));
54
55 EXPECT_TRUE(Size(0, 3) < Size(2, 1));
56 EXPECT_FALSE(Size(2, 1) < Size(0, 3));
57
58 EXPECT_TRUE(Size(0, 1) < Size(0, 3));
59 EXPECT_FALSE(Size(0, 3) < Size(0, 1));
60
61 EXPECT_FALSE(Size(1, 1) < Size(1, 1));
62 }
63
TEST(SizeTest,ValidAndEmpty)64 TEST(SizeTest, ValidAndEmpty) {
65 {
66 Size s;
67 EXPECT_FALSE(s.isValid());
68 EXPECT_FALSE(s.isEmpty());
69 }
70
71 {
72 Size s(-1, -1);
73 EXPECT_FALSE(s.isValid());
74 EXPECT_FALSE(s.isEmpty());
75 }
76
77 {
78 Size s(1, -1000);
79 EXPECT_FALSE(s.isValid());
80 EXPECT_FALSE(s.isEmpty());
81 }
82
83 {
84 Size s(-1000, 1);
85 EXPECT_FALSE(s.isValid());
86 EXPECT_FALSE(s.isEmpty());
87 }
88
89 {
90 Size s(-1000, -1000);
91 EXPECT_FALSE(s.isValid());
92 EXPECT_FALSE(s.isEmpty());
93 }
94
95 {
96 const auto& s = Size::INVALID;
97 EXPECT_FALSE(s.isValid());
98 EXPECT_FALSE(s.isEmpty());
99 }
100
101 {
102 Size s(123, 456);
103 s.makeInvalid();
104 EXPECT_FALSE(s.isValid());
105 EXPECT_FALSE(s.isEmpty());
106 }
107
108 {
109 Size s(0, 0);
110 EXPECT_TRUE(s.isValid());
111 EXPECT_TRUE(s.isEmpty());
112 }
113
114 {
115 const auto& s = Size::EMPTY;
116 EXPECT_TRUE(s.isValid());
117 EXPECT_TRUE(s.isEmpty());
118 }
119
120 {
121 Size s(123, 456);
122 s.clear();
123 EXPECT_TRUE(s.isValid());
124 EXPECT_TRUE(s.isEmpty());
125 }
126
127 {
128 Size s(123, 456);
129 EXPECT_TRUE(s.isValid());
130 EXPECT_FALSE(s.isEmpty());
131 }
132 }
133
TEST(SizeTest,Set)134 TEST(SizeTest, Set) {
135 {
136 Size s;
137 s.setWidth(0);
138 EXPECT_EQ(Size(0, -1), s);
139 }
140
141 {
142 Size s;
143 s.setHeight(0);
144 EXPECT_EQ(Size(-1, 0), s);
145 }
146
147 {
148 Size s;
149 s.set(123, 456);
150 EXPECT_EQ(Size(123, 456), s);
151 }
152 }
153
154 template <typename T, typename U>
ClampTest(T input,U expected)155 void ClampTest(T input, U expected) {
156 // The constructor, set(), setWidth() and setHeight() all allow arbitrary
157 // conversions from other numeric types, and implement clamping if necessary.
158
159 EXPECT_EQ(Size(expected, expected), Size(input, input));
160
161 {
162 Size s;
163 s.set(input, input);
164 EXPECT_EQ(Size(expected, expected), s);
165 }
166
167 {
168 Size s;
169 s.setWidth(input);
170 EXPECT_EQ(expected, s.width);
171 }
172
173 {
174 Size s;
175 s.setHeight(input);
176 EXPECT_EQ(expected, s.height);
177 }
178 }
179
TEST(SizeTest,Int8RangeIsNotClamped)180 TEST(SizeTest, Int8RangeIsNotClamped) {
181 ClampTest(std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max());
182 ClampTest(int8_t(0), int8_t(0));
183 ClampTest(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::lowest());
184 }
185
TEST(SizeTest,FloatRangeIsClamped)186 TEST(SizeTest, FloatRangeIsClamped) {
187 ClampTest(std::numeric_limits<float>::max(), std::numeric_limits<int32_t>::max());
188 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), std::numeric_limits<float>::max()),
189 std::numeric_limits<int32_t>::max());
190 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::max()),
191 std::numeric_limits<int32_t>::max());
192 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), 0),
193 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::max(), 0)));
194 ClampTest(float(0), int32_t(0));
195 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0),
196 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0)));
197 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::lowest()),
198 std::numeric_limits<int32_t>::lowest());
199 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(),
200 std::numeric_limits<float>::lowest()),
201 std::numeric_limits<int32_t>::lowest());
202 ClampTest(std::numeric_limits<float>::lowest(), std::numeric_limits<int32_t>::lowest());
203 }
204
TEST(SizeTest,Uint32RangeIsClamped)205 TEST(SizeTest, Uint32RangeIsClamped) {
206 ClampTest(std::numeric_limits<uint32_t>::max(), std::numeric_limits<int32_t>::max());
207 ClampTest(std::numeric_limits<uint32_t>::max() - 1, std::numeric_limits<int32_t>::max());
208 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) + 1,
209 std::numeric_limits<int32_t>::max());
210 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()),
211 std::numeric_limits<int32_t>::max());
212 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) - 1,
213 std::numeric_limits<int32_t>::max() - 1);
214 ClampTest(uint32_t(0), int32_t(0));
215 }
216
217 } // namespace android::ui
218