1 // Copyright (c) 2012 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 "ui/gfx/geometry/safe_integer_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(SafeIntegerConversions
, ToFlooredInt
) {
14 float max
= std::numeric_limits
<int>::max();
15 float min
= std::numeric_limits
<int>::min();
16 float infinity
= std::numeric_limits
<float>::infinity();
18 int int_max
= std::numeric_limits
<int>::max();
19 int int_min
= std::numeric_limits
<int>::min();
21 EXPECT_EQ(int_max
, ToFlooredInt(infinity
));
22 EXPECT_EQ(int_max
, ToFlooredInt(max
));
23 EXPECT_EQ(int_max
, ToFlooredInt(max
+ 100));
25 EXPECT_EQ(-101, ToFlooredInt(-100.5f
));
26 EXPECT_EQ(0, ToFlooredInt(0.f
));
27 EXPECT_EQ(100, ToFlooredInt(100.5f
));
29 EXPECT_EQ(int_min
, ToFlooredInt(-infinity
));
30 EXPECT_EQ(int_min
, ToFlooredInt(min
));
31 EXPECT_EQ(int_min
, ToFlooredInt(min
- 100));
34 TEST(SafeIntegerConversions
, ToCeiledInt
) {
35 float max
= std::numeric_limits
<int>::max();
36 float min
= std::numeric_limits
<int>::min();
37 float infinity
= std::numeric_limits
<float>::infinity();
39 int int_max
= std::numeric_limits
<int>::max();
40 int int_min
= std::numeric_limits
<int>::min();
42 EXPECT_EQ(int_max
, ToCeiledInt(infinity
));
43 EXPECT_EQ(int_max
, ToCeiledInt(max
));
44 EXPECT_EQ(int_max
, ToCeiledInt(max
+ 100));
46 EXPECT_EQ(-100, ToCeiledInt(-100.5f
));
47 EXPECT_EQ(0, ToCeiledInt(0.f
));
48 EXPECT_EQ(101, ToCeiledInt(100.5f
));
50 EXPECT_EQ(int_min
, ToCeiledInt(-infinity
));
51 EXPECT_EQ(int_min
, ToCeiledInt(min
));
52 EXPECT_EQ(int_min
, ToCeiledInt(min
- 100));
55 TEST(SafeIntegerConversions
, ToRoundedInt
) {
56 float max
= std::numeric_limits
<int>::max();
57 float min
= std::numeric_limits
<int>::min();
58 float infinity
= std::numeric_limits
<float>::infinity();
60 int int_max
= std::numeric_limits
<int>::max();
61 int int_min
= std::numeric_limits
<int>::min();
63 EXPECT_EQ(int_max
, ToRoundedInt(infinity
));
64 EXPECT_EQ(int_max
, ToRoundedInt(max
));
65 EXPECT_EQ(int_max
, ToRoundedInt(max
+ 100));
67 EXPECT_EQ(-100, ToRoundedInt(-100.1f
));
68 EXPECT_EQ(-101, ToRoundedInt(-100.5f
));
69 EXPECT_EQ(-101, ToRoundedInt(-100.9f
));
70 EXPECT_EQ(0, ToRoundedInt(0.f
));
71 EXPECT_EQ(100, ToRoundedInt(100.1f
));
72 EXPECT_EQ(101, ToRoundedInt(100.5f
));
73 EXPECT_EQ(101, ToRoundedInt(100.9f
));
75 EXPECT_EQ(int_min
, ToRoundedInt(-infinity
));
76 EXPECT_EQ(int_min
, ToRoundedInt(min
));
77 EXPECT_EQ(int_min
, ToRoundedInt(min
- 100));