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/safe_integer_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(SafeIntegerConversions
, ClampToInt
) {
14 EXPECT_EQ(0, ClampToInt(std::numeric_limits
<float>::quiet_NaN()));
16 float max
= std::numeric_limits
<int>::max();
17 float min
= std::numeric_limits
<int>::min();
18 float infinity
= std::numeric_limits
<float>::infinity();
20 int int_max
= std::numeric_limits
<int>::max();
21 int int_min
= std::numeric_limits
<int>::min();
23 EXPECT_EQ(int_max
, ClampToInt(infinity
));
24 EXPECT_EQ(int_max
, ClampToInt(max
));
25 EXPECT_EQ(int_max
, ClampToInt(max
+ 100));
27 EXPECT_EQ(-100, ClampToInt(-100.5f
));
28 EXPECT_EQ(0, ClampToInt(0));
29 EXPECT_EQ(100, ClampToInt(100.5f
));
31 EXPECT_EQ(int_min
, ClampToInt(-infinity
));
32 EXPECT_EQ(int_min
, ClampToInt(min
));
33 EXPECT_EQ(int_min
, ClampToInt(min
- 100));
36 TEST(SafeIntegerConversions
, ToFlooredInt
) {
37 EXPECT_EQ(0, ToFlooredInt(std::numeric_limits
<float>::quiet_NaN()));
39 float max
= std::numeric_limits
<int>::max();
40 float min
= std::numeric_limits
<int>::min();
41 float infinity
= std::numeric_limits
<float>::infinity();
43 int int_max
= std::numeric_limits
<int>::max();
44 int int_min
= std::numeric_limits
<int>::min();
46 EXPECT_EQ(int_max
, ToFlooredInt(infinity
));
47 EXPECT_EQ(int_max
, ToFlooredInt(max
));
48 EXPECT_EQ(int_max
, ToFlooredInt(max
+ 100));
50 EXPECT_EQ(-101, ToFlooredInt(-100.5f
));
51 EXPECT_EQ(0, ToFlooredInt(0));
52 EXPECT_EQ(100, ToFlooredInt(100.5f
));
54 EXPECT_EQ(int_min
, ToFlooredInt(-infinity
));
55 EXPECT_EQ(int_min
, ToFlooredInt(min
));
56 EXPECT_EQ(int_min
, ToFlooredInt(min
- 100));
59 TEST(SafeIntegerConversions
, ToCeiledInt
) {
60 EXPECT_EQ(0, ToCeiledInt(std::numeric_limits
<float>::quiet_NaN()));
62 float max
= std::numeric_limits
<int>::max();
63 float min
= std::numeric_limits
<int>::min();
64 float infinity
= std::numeric_limits
<float>::infinity();
66 int int_max
= std::numeric_limits
<int>::max();
67 int int_min
= std::numeric_limits
<int>::min();
69 EXPECT_EQ(int_max
, ToCeiledInt(infinity
));
70 EXPECT_EQ(int_max
, ToCeiledInt(max
));
71 EXPECT_EQ(int_max
, ToCeiledInt(max
+ 100));
73 EXPECT_EQ(-100, ToCeiledInt(-100.5f
));
74 EXPECT_EQ(0, ToCeiledInt(0));
75 EXPECT_EQ(101, ToCeiledInt(100.5f
));
77 EXPECT_EQ(int_min
, ToCeiledInt(-infinity
));
78 EXPECT_EQ(int_min
, ToCeiledInt(min
));
79 EXPECT_EQ(int_min
, ToCeiledInt(min
- 100));
82 TEST(SafeIntegerConversions
, ToRoundedInt
) {
83 EXPECT_EQ(0, ToRoundedInt(std::numeric_limits
<float>::quiet_NaN()));
85 float max
= std::numeric_limits
<int>::max();
86 float min
= std::numeric_limits
<int>::min();
87 float infinity
= std::numeric_limits
<float>::infinity();
89 int int_max
= std::numeric_limits
<int>::max();
90 int int_min
= std::numeric_limits
<int>::min();
92 EXPECT_EQ(int_max
, ToRoundedInt(infinity
));
93 EXPECT_EQ(int_max
, ToRoundedInt(max
));
94 EXPECT_EQ(int_max
, ToRoundedInt(max
+ 100));
96 EXPECT_EQ(-100, ToRoundedInt(-100.1f
));
97 EXPECT_EQ(-101, ToRoundedInt(-100.5f
));
98 EXPECT_EQ(-101, ToRoundedInt(-100.9f
));
99 EXPECT_EQ(0, ToRoundedInt(0));
100 EXPECT_EQ(100, ToRoundedInt(100.1f
));
101 EXPECT_EQ(101, ToRoundedInt(100.5f
));
102 EXPECT_EQ(101, ToRoundedInt(100.9f
));
104 EXPECT_EQ(int_min
, ToRoundedInt(-infinity
));
105 EXPECT_EQ(int_min
, ToRoundedInt(min
));
106 EXPECT_EQ(int_min
, ToRoundedInt(min
- 100));