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/base/range/range.h"
8 TEST(RangeTest
, EmptyInit
) {
10 EXPECT_EQ(0U, r
.start());
11 EXPECT_EQ(0U, r
.end());
12 EXPECT_EQ(0U, r
.length());
13 EXPECT_FALSE(r
.is_reversed());
14 EXPECT_TRUE(r
.is_empty());
15 EXPECT_TRUE(r
.IsValid());
16 EXPECT_EQ(0U, r
.GetMin());
17 EXPECT_EQ(0U, r
.GetMax());
20 TEST(RangeTest
, StartEndInit
) {
22 EXPECT_EQ(10U, r
.start());
23 EXPECT_EQ(15U, r
.end());
24 EXPECT_EQ(5U, r
.length());
25 EXPECT_FALSE(r
.is_reversed());
26 EXPECT_FALSE(r
.is_empty());
27 EXPECT_TRUE(r
.IsValid());
28 EXPECT_EQ(10U, r
.GetMin());
29 EXPECT_EQ(15U, r
.GetMax());
32 TEST(RangeTest
, StartEndReversedInit
) {
34 EXPECT_EQ(10U, r
.start());
35 EXPECT_EQ(5U, r
.end());
36 EXPECT_EQ(5U, r
.length());
37 EXPECT_TRUE(r
.is_reversed());
38 EXPECT_FALSE(r
.is_empty());
39 EXPECT_TRUE(r
.IsValid());
40 EXPECT_EQ(5U, r
.GetMin());
41 EXPECT_EQ(10U, r
.GetMax());
44 TEST(RangeTest
, PositionInit
) {
46 EXPECT_EQ(12U, r
.start());
47 EXPECT_EQ(12U, r
.end());
48 EXPECT_EQ(0U, r
.length());
49 EXPECT_FALSE(r
.is_reversed());
50 EXPECT_TRUE(r
.is_empty());
51 EXPECT_TRUE(r
.IsValid());
52 EXPECT_EQ(12U, r
.GetMin());
53 EXPECT_EQ(12U, r
.GetMax());
56 TEST(RangeTest
, InvalidRange
) {
57 ui::Range
r(ui::Range::InvalidRange());
58 EXPECT_EQ(0U, r
.length());
59 EXPECT_EQ(r
.start(), r
.end());
60 EXPECT_FALSE(r
.is_reversed());
61 EXPECT_TRUE(r
.is_empty());
62 EXPECT_FALSE(r
.IsValid());
65 TEST(RangeTest
, Equality
) {
84 TEST(RangeTest
, EqualsIgnoringDirection
) {
87 EXPECT_TRUE(r1
.EqualsIgnoringDirection(r2
));
90 TEST(RangeTest
, SetStart
) {
92 EXPECT_EQ(10U, r
.start());
93 EXPECT_EQ(10U, r
.length());
96 EXPECT_EQ(42U, r
.start());
97 EXPECT_EQ(20U, r
.end());
98 EXPECT_EQ(22U, r
.length());
99 EXPECT_TRUE(r
.is_reversed());
102 TEST(RangeTest
, SetEnd
) {
104 EXPECT_EQ(10U, r
.start());
105 EXPECT_EQ(3U, r
.length());
108 EXPECT_EQ(10U, r
.start());
109 EXPECT_EQ(20U, r
.end());
110 EXPECT_EQ(10U, r
.length());
113 TEST(RangeTest
, SetStartAndEnd
) {
117 EXPECT_EQ(1U, r
.start());
118 EXPECT_EQ(5U, r
.end());
119 EXPECT_EQ(4U, r
.length());
120 EXPECT_EQ(1U, r
.GetMin());
121 EXPECT_EQ(5U, r
.GetMax());
124 TEST(RangeTest
, ReversedRange
) {
126 EXPECT_EQ(10U, r
.start());
127 EXPECT_EQ(5U, r
.end());
128 EXPECT_EQ(5U, r
.length());
129 EXPECT_TRUE(r
.is_reversed());
130 EXPECT_TRUE(r
.IsValid());
131 EXPECT_EQ(5U, r
.GetMin());
132 EXPECT_EQ(10U, r
.GetMax());
135 TEST(RangeTest
, SetReversedRange
) {
138 EXPECT_EQ(25U, r
.start());
139 EXPECT_EQ(20U, r
.end());
140 EXPECT_EQ(5U, r
.length());
141 EXPECT_TRUE(r
.is_reversed());
142 EXPECT_TRUE(r
.IsValid());
145 EXPECT_EQ(25U, r
.start());
146 EXPECT_EQ(21U, r
.end());
147 EXPECT_EQ(4U, r
.length());
148 EXPECT_TRUE(r
.IsValid());
149 EXPECT_EQ(21U, r
.GetMin());
150 EXPECT_EQ(25U, r
.GetMax());
153 void TestContainsAndIntersects(const ui::Range
& r1
,
155 const ui::Range
& r3
) {
156 EXPECT_TRUE(r1
.Intersects(r1
));
157 EXPECT_TRUE(r1
.Contains(r1
));
158 EXPECT_EQ(ui::Range(10, 12), r1
.Intersect(r1
));
160 EXPECT_FALSE(r1
.Intersects(r2
));
161 EXPECT_FALSE(r1
.Contains(r2
));
162 EXPECT_TRUE(r1
.Intersect(r2
).is_empty());
163 EXPECT_FALSE(r2
.Intersects(r1
));
164 EXPECT_FALSE(r2
.Contains(r1
));
165 EXPECT_TRUE(r2
.Intersect(r1
).is_empty());
167 EXPECT_TRUE(r1
.Intersects(r3
));
168 EXPECT_TRUE(r3
.Intersects(r1
));
169 EXPECT_TRUE(r3
.Contains(r1
));
170 EXPECT_FALSE(r1
.Contains(r3
));
171 EXPECT_EQ(ui::Range(10, 12), r1
.Intersect(r3
));
172 EXPECT_EQ(ui::Range(10, 12), r3
.Intersect(r1
));
174 EXPECT_TRUE(r2
.Intersects(r3
));
175 EXPECT_TRUE(r3
.Intersects(r2
));
176 EXPECT_FALSE(r3
.Contains(r2
));
177 EXPECT_FALSE(r2
.Contains(r3
));
178 EXPECT_EQ(ui::Range(5, 8), r2
.Intersect(r3
));
179 EXPECT_EQ(ui::Range(5, 8), r3
.Intersect(r2
));
182 TEST(RangeTest
, ContainAndIntersect
) {
184 SCOPED_TRACE("contain and intersect");
185 ui::Range
r1(10, 12);
188 TestContainsAndIntersects(r1
, r2
, r3
);
191 SCOPED_TRACE("contain and intersect: reversed");
192 ui::Range
r1(12, 10);
195 TestContainsAndIntersects(r1
, r2
, r3
);
197 // Invalid rect tests
198 ui::Range
r1(10, 12);
200 ui::Range invalid
= r1
.Intersect(r2
);
201 EXPECT_FALSE(invalid
.IsValid());
202 EXPECT_FALSE(invalid
.Contains(invalid
));
203 EXPECT_FALSE(invalid
.Contains(r1
));
204 EXPECT_FALSE(invalid
.Intersects(invalid
));
205 EXPECT_FALSE(invalid
.Intersects(r1
));
206 EXPECT_FALSE(r1
.Contains(invalid
));
207 EXPECT_FALSE(r1
.Intersects(invalid
));