• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 
28 #include "wtf/MathExtras.h"
29 #include <gtest/gtest.h>
30 
31 namespace {
32 
TEST(MathExtrasTest,Lrint)33 TEST(MathExtrasTest, Lrint)
34 {
35     EXPECT_EQ(-8, lrint(-7.5));
36     EXPECT_EQ(-8, lrint(-8.5));
37     EXPECT_EQ(0, lrint(-0.5));
38     EXPECT_EQ(0, lrint(0.5));
39     EXPECT_EQ(0, lrint(-0.5));
40     EXPECT_EQ(1, lrint(1.3));
41     EXPECT_EQ(2, lrint(1.7));
42     EXPECT_EQ(0, lrint(0));
43     EXPECT_EQ(0, lrint(-0));
44     if (sizeof(long int) == 8) {
45         // Largest double number with 0.5 precision and one halfway rounding case below.
46         EXPECT_EQ(pow(2.0, 52), lrint(pow(2.0, 52) - 0.5));
47         EXPECT_EQ(pow(2.0, 52) - 2, lrint(pow(2.0, 52) - 1.5));
48         // Smallest double number with 0.5 precision and one halfway rounding case above.
49         EXPECT_EQ(-pow(2.0, 52), lrint(-pow(2.0, 52) + 0.5));
50         EXPECT_EQ(-pow(2.0, 52) + 2, lrint(-pow(2.0, 52) + 1.5));
51     }
52 }
53 
TEST(MathExtrasTest,clampToIntLong)54 TEST(MathExtrasTest, clampToIntLong)
55 {
56     if (sizeof(long) == sizeof(int))
57         return;
58 
59     long maxInt = std::numeric_limits<int>::max();
60     long minInt = std::numeric_limits<int>::min();
61     long overflowInt = maxInt + 1;
62     long underflowInt = minInt - 1;
63 
64     EXPECT_GT(overflowInt, maxInt);
65     EXPECT_LT(underflowInt, minInt);
66 
67     EXPECT_EQ(maxInt, clampTo<int>(maxInt));
68     EXPECT_EQ(minInt, clampTo<int>(minInt));
69 
70     EXPECT_EQ(maxInt, clampTo<int>(overflowInt));
71     EXPECT_EQ(minInt, clampTo<int>(underflowInt));
72 }
73 
TEST(MathExtrasTest,clampToIntLongLong)74 TEST(MathExtrasTest, clampToIntLongLong)
75 {
76     long long maxInt = std::numeric_limits<int>::max();
77     long long minInt = std::numeric_limits<int>::min();
78     long long overflowInt = maxInt + 1;
79     long long underflowInt = minInt - 1;
80 
81     EXPECT_GT(overflowInt, maxInt);
82     EXPECT_LT(underflowInt, minInt);
83 
84     EXPECT_EQ(maxInt, clampTo<int>(maxInt));
85     EXPECT_EQ(minInt, clampTo<int>(minInt));
86 
87     EXPECT_EQ(maxInt, clampTo<int>(overflowInt));
88     EXPECT_EQ(minInt, clampTo<int>(underflowInt));
89 }
90 
TEST(MathExtrasTest,clampToIntegerFloat)91 TEST(MathExtrasTest, clampToIntegerFloat)
92 {
93     // This test is inaccurate as floats will round the min / max integer
94     // due to the narrow mantissa. However it will properly checks within
95     // (close to the extreme) and outside the integer range.
96     float maxInt = std::numeric_limits<int>::max();
97     float minInt = std::numeric_limits<int>::min();
98     float overflowInt = maxInt * 1.1;
99     float underflowInt = minInt * 1.1;
100 
101     EXPECT_GT(overflowInt, maxInt);
102     EXPECT_LT(underflowInt, minInt);
103 
104     // If maxInt == 2^31 - 1 (ie on I32 architecture), the closest float used to represent it is 2^31.
105     EXPECT_NEAR(clampToInteger(maxInt), maxInt, 1);
106     EXPECT_EQ(minInt, clampToInteger(minInt));
107 
108     EXPECT_NEAR(clampToInteger(overflowInt), maxInt, 1);
109     EXPECT_EQ(minInt, clampToInteger(underflowInt));
110 }
111 
TEST(MathExtrasTest,clampToIntegerDouble)112 TEST(MathExtrasTest, clampToIntegerDouble)
113 {
114     double maxInt = std::numeric_limits<int>::max();
115     double minInt = std::numeric_limits<int>::min();
116     double overflowInt = maxInt + 1;
117     double underflowInt = minInt - 1;
118 
119     EXPECT_GT(overflowInt, maxInt);
120     EXPECT_LT(underflowInt, minInt);
121 
122     EXPECT_EQ(maxInt, clampToInteger(maxInt));
123     EXPECT_EQ(minInt, clampToInteger(minInt));
124 
125     EXPECT_EQ(clampToInteger(overflowInt), maxInt);
126     EXPECT_EQ(clampToInteger(underflowInt), minInt);
127 }
128 
TEST(MathExtrasTest,clampToFloat)129 TEST(MathExtrasTest, clampToFloat)
130 {
131     double maxFloat = std::numeric_limits<float>::max();
132     double minFloat = -maxFloat;
133     double overflowFloat = maxFloat * 1.1;
134     double underflowFloat = minFloat * 1.1;
135 
136     EXPECT_GT(overflowFloat, maxFloat);
137     EXPECT_LT(underflowFloat, minFloat);
138 
139     EXPECT_EQ(maxFloat, clampToFloat(maxFloat));
140     EXPECT_EQ(minFloat, clampToFloat(minFloat));
141 
142     EXPECT_EQ(maxFloat, clampToFloat(overflowFloat));
143     EXPECT_EQ(minFloat, clampToFloat(underflowFloat));
144 
145     EXPECT_EQ(maxFloat, clampToFloat(std::numeric_limits<float>::infinity()));
146     EXPECT_EQ(minFloat, clampToFloat(-std::numeric_limits<float>::infinity()));
147 }
148 
TEST(MathExtrasTest,clampToUnsignedLong)149 TEST(MathExtrasTest, clampToUnsignedLong)
150 {
151     if (sizeof(unsigned long) == sizeof(unsigned))
152         return;
153 
154     unsigned long maxUnsigned = std::numeric_limits<unsigned>::max();
155     unsigned long overflowUnsigned = maxUnsigned + 1;
156 
157     EXPECT_GT(overflowUnsigned, maxUnsigned);
158 
159     EXPECT_EQ(maxUnsigned, clampTo<unsigned>(maxUnsigned));
160 
161     EXPECT_EQ(maxUnsigned, clampTo<unsigned>(overflowUnsigned));
162     EXPECT_EQ(0u, clampTo<unsigned>(-1));
163 }
164 
TEST(MathExtrasTest,clampToUnsignedLongLong)165 TEST(MathExtrasTest, clampToUnsignedLongLong)
166 {
167     unsigned long long maxUnsigned = std::numeric_limits<unsigned>::max();
168     unsigned long long overflowUnsigned = maxUnsigned + 1;
169 
170     EXPECT_GT(overflowUnsigned, maxUnsigned);
171 
172     EXPECT_EQ(maxUnsigned, clampTo<unsigned>(maxUnsigned));
173 
174     EXPECT_EQ(maxUnsigned, clampTo<unsigned>(overflowUnsigned));
175     EXPECT_EQ(0u, clampTo<unsigned>(-1));
176 }
177 
178 // Make sure that various +-inf cases are handled properly (they aren't
179 // by default on VS).
TEST(MathExtrasTest,infinityMath)180 TEST(MathExtrasTest, infinityMath)
181 {
182     double posInf = std::numeric_limits<double>::infinity();
183     double negInf = -std::numeric_limits<double>::infinity();
184     double nan = std::numeric_limits<double>::quiet_NaN();
185 
186     EXPECT_EQ(M_PI_4, atan2(posInf, posInf));
187     EXPECT_EQ(3.0 * M_PI_4, atan2(posInf, negInf));
188     EXPECT_EQ(-M_PI_4, atan2(negInf, posInf));
189     EXPECT_EQ(-3.0 * M_PI_4, atan2(negInf, negInf));
190 
191     EXPECT_EQ(0.0, fmod(0.0, posInf));
192     EXPECT_EQ(7.0, fmod(7.0, posInf));
193     EXPECT_EQ(-7.0, fmod(-7.0, posInf));
194     EXPECT_EQ(0.0, fmod(0.0, negInf));
195     EXPECT_EQ(7.0, fmod(7.0, negInf));
196     EXPECT_EQ(-7.0, fmod(-7.0, negInf));
197 
198     EXPECT_EQ(1.0, pow(5.0, 0.0));
199     EXPECT_EQ(1.0, pow(-5.0, 0.0));
200     EXPECT_EQ(1.0, pow(nan, 0.0));
201 }
202 
203 } // namespace
204