OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/gfx/safe_integer_conversions.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace gfx { | |
12 | |
13 TEST(SafeIntegerConversions, ClampToInt) { | |
14 EXPECT_EQ(0, ClampToInt(std::numeric_limits<float>::quiet_NaN())); | |
15 | |
16 float max = std::numeric_limits<int>::max(); | |
17 float min = std::numeric_limits<int>::min(); | |
18 float infinity = std::numeric_limits<float>::infinity(); | |
19 | |
20 int int_max = std::numeric_limits<int>::max(); | |
21 int int_min = std::numeric_limits<int>::min(); | |
22 | |
23 EXPECT_EQ(int_max, ClampToInt(infinity)); | |
24 EXPECT_EQ(int_max, ClampToInt(max)); | |
25 EXPECT_EQ(int_max, ClampToInt(max + 100)); | |
26 | |
27 EXPECT_EQ(-100, ClampToInt(-100.5f)); | |
28 EXPECT_EQ(0, ClampToInt(0)); | |
29 EXPECT_EQ(100, ClampToInt(100.5f)); | |
30 | |
31 EXPECT_EQ(int_min, ClampToInt(-infinity)); | |
32 EXPECT_EQ(int_min, ClampToInt(min)); | |
33 EXPECT_EQ(int_min, ClampToInt(min - 100)); | |
34 } | |
35 | |
36 TEST(SafeIntegerConversions, ToFlooredInt) { | |
37 EXPECT_EQ(0, ToFlooredInt(std::numeric_limits<float>::quiet_NaN())); | |
38 | |
39 float max = std::numeric_limits<int>::max(); | |
40 float min = std::numeric_limits<int>::min(); | |
41 float infinity = std::numeric_limits<float>::infinity(); | |
42 | |
43 int int_max = std::numeric_limits<int>::max(); | |
44 int int_min = std::numeric_limits<int>::min(); | |
45 | |
46 EXPECT_EQ(int_max, ToFlooredInt(infinity)); | |
47 EXPECT_EQ(int_max, ToFlooredInt(max)); | |
48 EXPECT_EQ(int_max, ToFlooredInt(max + 100)); | |
49 | |
50 EXPECT_EQ(-101, ToFlooredInt(-100.5f)); | |
51 EXPECT_EQ(0, ToFlooredInt(0)); | |
52 EXPECT_EQ(100, ToFlooredInt(100.5f)); | |
53 | |
54 EXPECT_EQ(int_min, ToFlooredInt(-infinity)); | |
55 EXPECT_EQ(int_min, ToFlooredInt(min)); | |
56 EXPECT_EQ(int_min, ToFlooredInt(min - 100)); | |
57 } | |
58 | |
59 TEST(SafeIntegerConversions, ToCeiledInt) { | |
60 EXPECT_EQ(0, ToCeiledInt(std::numeric_limits<float>::quiet_NaN())); | |
61 | |
62 float max = std::numeric_limits<int>::max(); | |
63 float min = std::numeric_limits<int>::min(); | |
64 float infinity = std::numeric_limits<float>::infinity(); | |
65 | |
66 int int_max = std::numeric_limits<int>::max(); | |
67 int int_min = std::numeric_limits<int>::min(); | |
68 | |
69 EXPECT_EQ(int_max, ToCeiledInt(infinity)); | |
70 EXPECT_EQ(int_max, ToCeiledInt(max)); | |
71 EXPECT_EQ(int_max, ToCeiledInt(max + 100)); | |
72 | |
73 EXPECT_EQ(-100, ToCeiledInt(-100.5f)); | |
74 EXPECT_EQ(0, ToCeiledInt(0)); | |
75 EXPECT_EQ(101, ToCeiledInt(100.5f)); | |
76 | |
77 EXPECT_EQ(int_min, ToCeiledInt(-infinity)); | |
78 EXPECT_EQ(int_min, ToCeiledInt(min)); | |
79 EXPECT_EQ(int_min, ToCeiledInt(min - 100)); | |
80 } | |
81 | |
82 TEST(SafeIntegerConversions, ToRoundedInt) { | |
83 EXPECT_EQ(0, ToRoundedInt(std::numeric_limits<float>::quiet_NaN())); | |
84 | |
85 float max = std::numeric_limits<int>::max(); | |
86 float min = std::numeric_limits<int>::min(); | |
87 float infinity = std::numeric_limits<float>::infinity(); | |
88 | |
89 int int_max = std::numeric_limits<int>::max(); | |
90 int int_min = std::numeric_limits<int>::min(); | |
91 | |
92 EXPECT_EQ(int_max, ToRoundedInt(infinity)); | |
93 EXPECT_EQ(int_max, ToRoundedInt(max)); | |
94 EXPECT_EQ(int_max, ToRoundedInt(max + 100)); | |
95 | |
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)); | |
103 | |
104 EXPECT_EQ(int_min, ToRoundedInt(-infinity)); | |
105 EXPECT_EQ(int_min, ToRoundedInt(min)); | |
106 EXPECT_EQ(int_min, ToRoundedInt(min - 100)); | |
107 } | |
108 | |
109 } // namespace gfx | |
OLD | NEW |