Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / range / range_unittest.cc
blob71a81649ab52d3276d0d0a4358110224f5d5b64a
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"
9 namespace {
11 template <typename T>
12 class RangeTest : public testing::Test {
15 typedef testing::Types<gfx::Range, gfx::RangeF> RangeTypes;
16 TYPED_TEST_CASE(RangeTest, RangeTypes);
18 template <typename T>
19 void TestContainsAndIntersects(const T& r1,
20 const T& r2,
21 const T& r3) {
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));
48 } // namespace
50 TYPED_TEST(RangeTest, EmptyInit) {
51 TypeParam r;
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) {
63 TypeParam r(10, 15);
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) {
75 TypeParam r(10, 5);
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) {
87 TypeParam r(12);
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) {
111 TypeParam r1(10, 4);
112 TypeParam r2(10, 4);
113 TypeParam r3(10, 2);
114 EXPECT_EQ(r1, r2);
115 EXPECT_NE(r1, r3);
116 EXPECT_NE(r2, r3);
118 TypeParam r4(11, 4);
119 EXPECT_NE(r1, r4);
120 EXPECT_NE(r2, r4);
121 EXPECT_NE(r3, r4);
123 TypeParam r5(12, 5);
124 EXPECT_NE(r1, r5);
125 EXPECT_NE(r2, r5);
126 EXPECT_NE(r3, r5);
129 TYPED_TEST(RangeTest, EqualsIgnoringDirection) {
130 TypeParam r1(10, 5);
131 TypeParam r2(5, 10);
132 EXPECT_TRUE(r1.EqualsIgnoringDirection(r2));
135 TYPED_TEST(RangeTest, SetStart) {
136 TypeParam r(10, 20);
137 EXPECT_EQ(10U, r.start());
138 EXPECT_EQ(10U, r.length());
140 r.set_start(42);
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) {
148 TypeParam r(10, 13);
149 EXPECT_EQ(10U, r.start());
150 EXPECT_EQ(3U, r.length());
152 r.set_end(20);
153 EXPECT_EQ(10U, r.start());
154 EXPECT_EQ(20U, r.end());
155 EXPECT_EQ(10U, r.length());
158 TYPED_TEST(RangeTest, SetStartAndEnd) {
159 TypeParam r;
160 r.set_end(5);
161 r.set_start(1);
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) {
170 TypeParam r(10, 5);
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) {
181 TypeParam r(10, 20);
182 r.set_start(25);
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());
189 r.set_end(21);
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);
202 TypeParam r2(1, 8);
203 TypeParam r3(5, 12);
204 TestContainsAndIntersects(r1, r2, r3);
207 SCOPED_TRACE("contain and intersect: reversed");
208 TypeParam r1(12, 10);
209 TypeParam r2(8, 1);
210 TypeParam r3(12, 5);
211 TestContainsAndIntersects(r1, r2, r3);
213 // Invalid rect tests
214 TypeParam r1(10, 12);
215 TypeParam r2(8, 1);
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());