1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/gfx/range/range.h"
7 #include "ui/gfx/range/range_f.h"
12 class RangeTest
: public testing::Test
{
15 typedef testing::Types
<gfx::Range
, gfx::RangeF
> RangeTypes
;
16 TYPED_TEST_CASE(RangeTest
, RangeTypes
);
19 void TestContainsAndIntersects(const T
& r1
,
22 EXPECT_TRUE(r1
.Intersects(r1
));
23 EXPECT_TRUE(r1
.Contains(r1
));
24 EXPECT_EQ(T(10, 12), r1
.Intersect(r1
));
26 EXPECT_FALSE(r1
.Intersects(r2
));
27 EXPECT_FALSE(r1
.Contains(r2
));
28 EXPECT_TRUE(r1
.Intersect(r2
).is_empty());
29 EXPECT_FALSE(r2
.Intersects(r1
));
30 EXPECT_FALSE(r2
.Contains(r1
));
31 EXPECT_TRUE(r2
.Intersect(r1
).is_empty());
33 EXPECT_TRUE(r1
.Intersects(r3
));
34 EXPECT_TRUE(r3
.Intersects(r1
));
35 EXPECT_TRUE(r3
.Contains(r1
));
36 EXPECT_FALSE(r1
.Contains(r3
));
37 EXPECT_EQ(T(10, 12), r1
.Intersect(r3
));
38 EXPECT_EQ(T(10, 12), r3
.Intersect(r1
));
40 EXPECT_TRUE(r2
.Intersects(r3
));
41 EXPECT_TRUE(r3
.Intersects(r2
));
42 EXPECT_FALSE(r3
.Contains(r2
));
43 EXPECT_FALSE(r2
.Contains(r3
));
44 EXPECT_EQ(T(5, 8), r2
.Intersect(r3
));
45 EXPECT_EQ(T(5, 8), r3
.Intersect(r2
));
50 TYPED_TEST(RangeTest
, EmptyInit
) {
52 EXPECT_EQ(0U, r
.start());
53 EXPECT_EQ(0U, r
.end());
54 EXPECT_EQ(0U, r
.length());
55 EXPECT_FALSE(r
.is_reversed());
56 EXPECT_TRUE(r
.is_empty());
57 EXPECT_TRUE(r
.IsValid());
58 EXPECT_EQ(0U, r
.GetMin());
59 EXPECT_EQ(0U, r
.GetMax());
62 TYPED_TEST(RangeTest
, StartEndInit
) {
64 EXPECT_EQ(10U, r
.start());
65 EXPECT_EQ(15U, r
.end());
66 EXPECT_EQ(5U, r
.length());
67 EXPECT_FALSE(r
.is_reversed());
68 EXPECT_FALSE(r
.is_empty());
69 EXPECT_TRUE(r
.IsValid());
70 EXPECT_EQ(10U, r
.GetMin());
71 EXPECT_EQ(15U, r
.GetMax());
74 TYPED_TEST(RangeTest
, StartEndReversedInit
) {
76 EXPECT_EQ(10U, r
.start());
77 EXPECT_EQ(5U, r
.end());
78 EXPECT_EQ(5U, r
.length());
79 EXPECT_TRUE(r
.is_reversed());
80 EXPECT_FALSE(r
.is_empty());
81 EXPECT_TRUE(r
.IsValid());
82 EXPECT_EQ(5U, r
.GetMin());
83 EXPECT_EQ(10U, r
.GetMax());
86 TYPED_TEST(RangeTest
, PositionInit
) {
88 EXPECT_EQ(12U, r
.start());
89 EXPECT_EQ(12U, r
.end());
90 EXPECT_EQ(0U, r
.length());
91 EXPECT_FALSE(r
.is_reversed());
92 EXPECT_TRUE(r
.is_empty());
93 EXPECT_TRUE(r
.IsValid());
94 EXPECT_EQ(12U, r
.GetMin());
95 EXPECT_EQ(12U, r
.GetMax());
98 TYPED_TEST(RangeTest
, InvalidRange
) {
99 TypeParam
r(TypeParam::InvalidRange());
100 EXPECT_EQ(0U, r
.length());
101 EXPECT_EQ(r
.start(), r
.end());
102 EXPECT_EQ(r
.GetMax(), r
.GetMin());
103 EXPECT_FALSE(r
.is_reversed());
104 EXPECT_TRUE(r
.is_empty());
105 EXPECT_FALSE(r
.IsValid());
106 EXPECT_EQ(r
, TypeParam::InvalidRange());
107 EXPECT_TRUE(r
.EqualsIgnoringDirection(TypeParam::InvalidRange()));
110 TYPED_TEST(RangeTest
, Equality
) {
129 TYPED_TEST(RangeTest
, EqualsIgnoringDirection
) {
132 EXPECT_TRUE(r1
.EqualsIgnoringDirection(r2
));
135 TYPED_TEST(RangeTest
, SetStart
) {
137 EXPECT_EQ(10U, r
.start());
138 EXPECT_EQ(10U, r
.length());
141 EXPECT_EQ(42U, r
.start());
142 EXPECT_EQ(20U, r
.end());
143 EXPECT_EQ(22U, r
.length());
144 EXPECT_TRUE(r
.is_reversed());
147 TYPED_TEST(RangeTest
, SetEnd
) {
149 EXPECT_EQ(10U, r
.start());
150 EXPECT_EQ(3U, r
.length());
153 EXPECT_EQ(10U, r
.start());
154 EXPECT_EQ(20U, r
.end());
155 EXPECT_EQ(10U, r
.length());
158 TYPED_TEST(RangeTest
, SetStartAndEnd
) {
162 EXPECT_EQ(1U, r
.start());
163 EXPECT_EQ(5U, r
.end());
164 EXPECT_EQ(4U, r
.length());
165 EXPECT_EQ(1U, r
.GetMin());
166 EXPECT_EQ(5U, r
.GetMax());
169 TYPED_TEST(RangeTest
, ReversedRange
) {
171 EXPECT_EQ(10U, r
.start());
172 EXPECT_EQ(5U, r
.end());
173 EXPECT_EQ(5U, r
.length());
174 EXPECT_TRUE(r
.is_reversed());
175 EXPECT_TRUE(r
.IsValid());
176 EXPECT_EQ(5U, r
.GetMin());
177 EXPECT_EQ(10U, r
.GetMax());
180 TYPED_TEST(RangeTest
, SetReversedRange
) {
183 EXPECT_EQ(25U, r
.start());
184 EXPECT_EQ(20U, r
.end());
185 EXPECT_EQ(5U, r
.length());
186 EXPECT_TRUE(r
.is_reversed());
187 EXPECT_TRUE(r
.IsValid());
190 EXPECT_EQ(25U, r
.start());
191 EXPECT_EQ(21U, r
.end());
192 EXPECT_EQ(4U, r
.length());
193 EXPECT_TRUE(r
.IsValid());
194 EXPECT_EQ(21U, r
.GetMin());
195 EXPECT_EQ(25U, r
.GetMax());
198 TYPED_TEST(RangeTest
, ContainAndIntersect
) {
200 SCOPED_TRACE("contain and intersect");
201 TypeParam
r1(10, 12);
204 TestContainsAndIntersects(r1
, r2
, r3
);
207 SCOPED_TRACE("contain and intersect: reversed");
208 TypeParam
r1(12, 10);
211 TestContainsAndIntersects(r1
, r2
, r3
);
213 // Invalid rect tests
214 TypeParam
r1(10, 12);
216 TypeParam invalid
= r1
.Intersect(r2
);
217 EXPECT_FALSE(invalid
.IsValid());
218 EXPECT_FALSE(invalid
.Contains(invalid
));
219 EXPECT_FALSE(invalid
.Contains(r1
));
220 EXPECT_FALSE(invalid
.Intersects(invalid
));
221 EXPECT_FALSE(invalid
.Intersects(r1
));
222 EXPECT_FALSE(r1
.Contains(invalid
));
223 EXPECT_FALSE(r1
.Intersects(invalid
));
226 TEST(RangeTest
, RangeFConverterTest
) {
227 gfx::RangeF
range_f(1.2f
, 3.9f
);
228 gfx::Range range
= range_f
.Floor();
229 EXPECT_EQ(1U, range
.start());
230 EXPECT_EQ(3U, range
.end());
232 range
= range_f
.Ceil();
233 EXPECT_EQ(2U, range
.start());
234 EXPECT_EQ(4U, range
.end());
236 range
= range_f
.Round();
237 EXPECT_EQ(1U, range
.start());
238 EXPECT_EQ(4U, range
.end());
240 // Test for negative values.
241 range_f
.set_start(-1.2f
);
242 range_f
.set_end(-3.8f
);
243 range
= range_f
.Floor();
244 EXPECT_EQ(0U, range
.start());
245 EXPECT_EQ(0U, range
.end());
247 range
= range_f
.Ceil();
248 EXPECT_EQ(0U, range
.start());
249 EXPECT_EQ(0U, range
.end());
251 range
= range_f
.Round();
252 EXPECT_EQ(0U, range
.start());
253 EXPECT_EQ(0U, range
.end());