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,Transpose)64 TEST(SizeTest, Transpose) {
65 Size s(123, 456);
66 s.transpose();
67 EXPECT_EQ(s, Size(456, 123));
68 }
69
TEST(SizeTest,Rotate)70 TEST(SizeTest, Rotate) {
71 {
72 Size s(123, 456);
73 s.rotate(Rotation::Rotation0);
74 EXPECT_EQ(s, Size(123, 456));
75 }
76
77 {
78 Size s(123, 456);
79 s.rotate(Rotation::Rotation90);
80 EXPECT_EQ(s, Size(456, 123));
81 }
82
83 {
84 Size s(123, 456);
85 s.rotate(Rotation::Rotation180);
86 EXPECT_EQ(s, Size(123, 456));
87 }
88
89 {
90 Size s(123, 456);
91 s.rotate(Rotation::Rotation270);
92 EXPECT_EQ(s, Size(456, 123));
93 }
94 }
95
TEST(SizeTest,ValidAndEmpty)96 TEST(SizeTest, ValidAndEmpty) {
97 {
98 Size s;
99 EXPECT_FALSE(s.isValid());
100 EXPECT_FALSE(s.isEmpty());
101 }
102
103 {
104 Size s(-1, -1);
105 EXPECT_FALSE(s.isValid());
106 EXPECT_FALSE(s.isEmpty());
107 }
108
109 {
110 Size s(1, -1000);
111 EXPECT_FALSE(s.isValid());
112 EXPECT_FALSE(s.isEmpty());
113 }
114
115 {
116 Size s(-1000, 1);
117 EXPECT_FALSE(s.isValid());
118 EXPECT_FALSE(s.isEmpty());
119 }
120
121 {
122 Size s(-1000, -1000);
123 EXPECT_FALSE(s.isValid());
124 EXPECT_FALSE(s.isEmpty());
125 }
126
127 {
128 EXPECT_FALSE(kInvalidSize.isValid());
129 EXPECT_FALSE(kInvalidSize.isEmpty());
130 }
131
132 {
133 Size s(123, 456);
134 s.makeInvalid();
135 EXPECT_FALSE(s.isValid());
136 EXPECT_FALSE(s.isEmpty());
137 }
138
139 {
140 Size s(0, 0);
141 EXPECT_TRUE(s.isValid());
142 EXPECT_TRUE(s.isEmpty());
143 }
144
145 {
146 EXPECT_TRUE(kEmptySize.isValid());
147 EXPECT_TRUE(kEmptySize.isEmpty());
148 }
149
150 {
151 Size s(123, 456);
152 s.clear();
153 EXPECT_TRUE(s.isValid());
154 EXPECT_TRUE(s.isEmpty());
155 }
156
157 {
158 Size s(123, 456);
159 EXPECT_TRUE(s.isValid());
160 EXPECT_FALSE(s.isEmpty());
161 }
162 }
163
TEST(SizeTest,Set)164 TEST(SizeTest, Set) {
165 {
166 Size s;
167 s.setWidth(0);
168 EXPECT_EQ(Size(0, -1), s);
169 }
170
171 {
172 Size s;
173 s.setHeight(0);
174 EXPECT_EQ(Size(-1, 0), s);
175 }
176
177 {
178 Size s;
179 s.set(123, 456);
180 EXPECT_EQ(Size(123, 456), s);
181 }
182 }
183
184 template <typename T, typename U>
ClampTest(T input,U expected)185 void ClampTest(T input, U expected) {
186 // The constructor, set(), setWidth() and setHeight() all allow arbitrary
187 // conversions from other numeric types, and implement clamping if necessary.
188
189 EXPECT_EQ(Size(expected, expected), Size(input, input));
190
191 {
192 Size s;
193 s.set(input, input);
194 EXPECT_EQ(Size(expected, expected), s);
195 }
196
197 {
198 Size s;
199 s.setWidth(input);
200 EXPECT_EQ(expected, s.width);
201 }
202
203 {
204 Size s;
205 s.setHeight(input);
206 EXPECT_EQ(expected, s.height);
207 }
208 }
209
TEST(SizeTest,Int8RangeIsNotClamped)210 TEST(SizeTest, Int8RangeIsNotClamped) {
211 ClampTest(std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max());
212 ClampTest(int8_t(0), int8_t(0));
213 ClampTest(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::lowest());
214 }
215
TEST(SizeTest,FloatRangeIsClamped)216 TEST(SizeTest, FloatRangeIsClamped) {
217 ClampTest(std::numeric_limits<float>::max(), std::numeric_limits<int32_t>::max());
218 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), std::numeric_limits<float>::max()),
219 std::numeric_limits<int32_t>::max());
220 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::max()),
221 std::numeric_limits<int32_t>::max());
222 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), 0),
223 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::max(), 0)));
224 ClampTest(float(0), int32_t(0));
225 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0),
226 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0)));
227 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::lowest()),
228 std::numeric_limits<int32_t>::lowest());
229 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(),
230 std::numeric_limits<float>::lowest()),
231 std::numeric_limits<int32_t>::lowest());
232 ClampTest(std::numeric_limits<float>::lowest(), std::numeric_limits<int32_t>::lowest());
233 }
234
TEST(SizeTest,Uint32RangeIsClamped)235 TEST(SizeTest, Uint32RangeIsClamped) {
236 ClampTest(std::numeric_limits<uint32_t>::max(), std::numeric_limits<int32_t>::max());
237 ClampTest(std::numeric_limits<uint32_t>::max() - 1, std::numeric_limits<int32_t>::max());
238 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) + 1,
239 std::numeric_limits<int32_t>::max());
240 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()),
241 std::numeric_limits<int32_t>::max());
242 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) - 1,
243 std::numeric_limits<int32_t>::max() - 1);
244 ClampTest(uint32_t(0), int32_t(0));
245 }
246
247 } // namespace android::ui
248