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 {
37 namespace ui {
38
TEST(SizeTest,BasicConstructionAndEqualityComparison)39 TEST(SizeTest, BasicConstructionAndEqualityComparison) {
40 Size s(123, 456);
41
42 EXPECT_EQ(123, s.width);
43 EXPECT_EQ(123, s.getWidth());
44
45 EXPECT_EQ(456, s.height);
46 EXPECT_EQ(456, s.getHeight());
47
48 EXPECT_EQ(Size(123, 456), s);
49 EXPECT_NE(Size(456, 123), s);
50 }
51
TEST(SizeTest,BasicLessThanComparison)52 TEST(SizeTest, BasicLessThanComparison) {
53 EXPECT_TRUE(Size(0, 1) < Size(2, 3));
54 EXPECT_FALSE(Size(2, 3) < Size(0, 1));
55
56 EXPECT_TRUE(Size(0, 3) < Size(2, 1));
57 EXPECT_FALSE(Size(2, 1) < Size(0, 3));
58
59 EXPECT_TRUE(Size(0, 1) < Size(0, 3));
60 EXPECT_FALSE(Size(0, 3) < Size(0, 1));
61
62 EXPECT_FALSE(Size(1, 1) < Size(1, 1));
63 }
64
TEST(SizeTest,ValidAndEmpty)65 TEST(SizeTest, ValidAndEmpty) {
66 {
67 Size s;
68 EXPECT_FALSE(s.isValid());
69 EXPECT_FALSE(s.isEmpty());
70 }
71
72 {
73 Size s(-1, -1);
74 EXPECT_FALSE(s.isValid());
75 EXPECT_FALSE(s.isEmpty());
76 }
77
78 {
79 Size s(1, -1000);
80 EXPECT_FALSE(s.isValid());
81 EXPECT_FALSE(s.isEmpty());
82 }
83
84 {
85 Size s(-1000, 1);
86 EXPECT_FALSE(s.isValid());
87 EXPECT_FALSE(s.isEmpty());
88 }
89
90 {
91 Size s(-1000, -1000);
92 EXPECT_FALSE(s.isValid());
93 EXPECT_FALSE(s.isEmpty());
94 }
95
96 {
97 const auto& s = Size::INVALID;
98 EXPECT_FALSE(s.isValid());
99 EXPECT_FALSE(s.isEmpty());
100 }
101
102 {
103 Size s(123, 456);
104 s.makeInvalid();
105 EXPECT_FALSE(s.isValid());
106 EXPECT_FALSE(s.isEmpty());
107 }
108
109 {
110 Size s(0, 0);
111 EXPECT_TRUE(s.isValid());
112 EXPECT_TRUE(s.isEmpty());
113 }
114
115 {
116 const auto& s = Size::EMPTY;
117 EXPECT_TRUE(s.isValid());
118 EXPECT_TRUE(s.isEmpty());
119 }
120
121 {
122 Size s(123, 456);
123 s.clear();
124 EXPECT_TRUE(s.isValid());
125 EXPECT_TRUE(s.isEmpty());
126 }
127
128 {
129 Size s(123, 456);
130 EXPECT_TRUE(s.isValid());
131 EXPECT_FALSE(s.isEmpty());
132 }
133 }
134
TEST(SizeTest,Set)135 TEST(SizeTest, Set) {
136 {
137 Size s;
138 s.setWidth(0);
139 EXPECT_EQ(Size(0, -1), s);
140 }
141
142 {
143 Size s;
144 s.setHeight(0);
145 EXPECT_EQ(Size(-1, 0), s);
146 }
147
148 {
149 Size s;
150 s.set(123, 456);
151 EXPECT_EQ(Size(123, 456), s);
152 }
153 }
154
155 template <typename T, typename U>
ClampTest(T input,U expected)156 void ClampTest(T input, U expected) {
157 // The constructor, set(), setWidth() and setHeight() all allow arbitrary
158 // conversions from other numeric types, and implement clamping if necessary.
159
160 EXPECT_EQ(Size(expected, expected), Size(input, input));
161
162 {
163 Size s;
164 s.set(input, input);
165 EXPECT_EQ(Size(expected, expected), s);
166 }
167
168 {
169 Size s;
170 s.setWidth(input);
171 EXPECT_EQ(expected, s.width);
172 }
173
174 {
175 Size s;
176 s.setHeight(input);
177 EXPECT_EQ(expected, s.height);
178 }
179 }
180
TEST(SizeTest,Int8RangeIsNotClamped)181 TEST(SizeTest, Int8RangeIsNotClamped) {
182 ClampTest(std::numeric_limits<int8_t>::max(), std::numeric_limits<int8_t>::max());
183 ClampTest(int8_t(0), int8_t(0));
184 ClampTest(std::numeric_limits<int8_t>::lowest(), std::numeric_limits<int8_t>::lowest());
185 }
186
TEST(SizeTest,FloatRangeIsClamped)187 TEST(SizeTest, FloatRangeIsClamped) {
188 ClampTest(std::numeric_limits<float>::max(), std::numeric_limits<int32_t>::max());
189 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), std::numeric_limits<float>::max()),
190 std::numeric_limits<int32_t>::max());
191 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::max()),
192 std::numeric_limits<int32_t>::max());
193 ClampTest(nexttowardf(std::numeric_limits<int32_t>::max(), 0),
194 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::max(), 0)));
195 ClampTest(float(0), int32_t(0));
196 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0),
197 static_cast<int32_t>(nexttowardf(std::numeric_limits<int32_t>::lowest(), 0)));
198 ClampTest(static_cast<float>(std::numeric_limits<int32_t>::lowest()),
199 std::numeric_limits<int32_t>::lowest());
200 ClampTest(nexttowardf(std::numeric_limits<int32_t>::lowest(),
201 std::numeric_limits<float>::lowest()),
202 std::numeric_limits<int32_t>::lowest());
203 ClampTest(std::numeric_limits<float>::lowest(), std::numeric_limits<int32_t>::lowest());
204 }
205
TEST(SizeTest,Uint32RangeIsClamped)206 TEST(SizeTest, Uint32RangeIsClamped) {
207 ClampTest(std::numeric_limits<uint32_t>::max(), std::numeric_limits<int32_t>::max());
208 ClampTest(std::numeric_limits<uint32_t>::max() - 1, std::numeric_limits<int32_t>::max());
209 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) + 1,
210 std::numeric_limits<int32_t>::max());
211 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()),
212 std::numeric_limits<int32_t>::max());
213 ClampTest(static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) - 1,
214 std::numeric_limits<int32_t>::max() - 1);
215 ClampTest(uint32_t(0), int32_t(0));
216 }
217
218 } // namespace ui
219 } // namespace android
220