2 * Copyright (C) 2012 Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "wtf/MathExtras.h"
29 #include <gtest/gtest.h>
33 TEST(MathExtrasTest
, Lrint
)
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));
54 TEST(MathExtrasTest
, clampToIntLong
)
56 if (sizeof(long) == sizeof(int))
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;
64 EXPECT_GT(overflowInt
, maxInt
);
65 EXPECT_LT(underflowInt
, minInt
);
67 EXPECT_EQ(maxInt
, clampTo
<int>(maxInt
));
68 EXPECT_EQ(minInt
, clampTo
<int>(minInt
));
70 EXPECT_EQ(maxInt
, clampTo
<int>(overflowInt
));
71 EXPECT_EQ(minInt
, clampTo
<int>(underflowInt
));
74 TEST(MathExtrasTest
, clampToIntLongLong
)
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;
81 EXPECT_GT(overflowInt
, maxInt
);
82 EXPECT_LT(underflowInt
, minInt
);
84 EXPECT_EQ(maxInt
, clampTo
<int>(maxInt
));
85 EXPECT_EQ(minInt
, clampTo
<int>(minInt
));
87 EXPECT_EQ(maxInt
, clampTo
<int>(overflowInt
));
88 EXPECT_EQ(minInt
, clampTo
<int>(underflowInt
));
91 TEST(MathExtrasTest
, clampToIntFloat
)
93 float maxInt
= static_cast<float>(std::numeric_limits
<int>::max());
94 float minInt
= static_cast<float>(std::numeric_limits
<int>::min());
95 float overflowInt
= maxInt
* 1.1f
;
96 float underflowInt
= minInt
* 1.1f
;
98 EXPECT_GT(overflowInt
, maxInt
);
99 EXPECT_LT(underflowInt
, minInt
);
101 EXPECT_EQ(maxInt
, clampTo
<int>(maxInt
));
102 EXPECT_EQ(minInt
, clampTo
<int>(minInt
));
104 EXPECT_EQ(maxInt
, clampTo
<int>(overflowInt
));
105 EXPECT_EQ(minInt
, clampTo
<int>(underflowInt
));
107 // This value and the value one greater are typically represented the same
108 // way when stored in a 32-bit float. Make sure clamping does not cause us
109 // to erroneously jump to the larger value.
110 int nearFloatPrecisionLimit
= 2147483520;
111 EXPECT_EQ(nearFloatPrecisionLimit
, clampTo
<int>(static_cast<float>(nearFloatPrecisionLimit
), 0, nearFloatPrecisionLimit
+ 1));
112 EXPECT_EQ(-nearFloatPrecisionLimit
, clampTo
<int>(static_cast<float>(-nearFloatPrecisionLimit
), -nearFloatPrecisionLimit
- 1, 0));
115 TEST(MathExtrasTest
, clampToIntDouble
)
117 int maxInt
= std::numeric_limits
<int>::max();
118 int minInt
= std::numeric_limits
<int>::min();
119 double almostOverflowInt
= maxInt
- 0.5;
120 double overflowInt
= maxInt
+ 0.5;
121 double almostUnderflowInt
= minInt
+ 0.5;
122 double underflowInt
= minInt
- 0.5;
124 EXPECT_LT(almostOverflowInt
, maxInt
);
125 EXPECT_GT(overflowInt
, maxInt
);
126 EXPECT_GT(almostUnderflowInt
, minInt
);
127 EXPECT_LT(underflowInt
, minInt
);
129 EXPECT_EQ(maxInt
, clampTo
<int>(static_cast<double>(maxInt
)));
130 EXPECT_EQ(minInt
, clampTo
<int>(static_cast<double>(minInt
)));
132 EXPECT_EQ(maxInt
- 1, clampTo
<int>(almostOverflowInt
));
133 EXPECT_EQ(maxInt
, clampTo
<int>(overflowInt
));
134 EXPECT_EQ(minInt
+ 1, clampTo
<int>(almostUnderflowInt
));
135 EXPECT_EQ(minInt
, clampTo
<int>(underflowInt
));
138 TEST(MathExtrasTest
, clampToFloatDouble
)
140 double maxFloat
= std::numeric_limits
<float>::max();
141 double minFloat
= -maxFloat
;
142 double overflowFloat
= maxFloat
* 1.1;
143 double underflowFloat
= minFloat
* 1.1;
145 EXPECT_GT(overflowFloat
, maxFloat
);
146 EXPECT_LT(underflowFloat
, minFloat
);
148 EXPECT_EQ(maxFloat
, clampTo
<float>(maxFloat
));
149 EXPECT_EQ(minFloat
, clampTo
<float>(minFloat
));
151 EXPECT_EQ(maxFloat
, clampTo
<float>(overflowFloat
));
152 EXPECT_EQ(minFloat
, clampTo
<float>(underflowFloat
));
154 EXPECT_EQ(maxFloat
, clampTo
<float>(std::numeric_limits
<float>::infinity()));
155 EXPECT_EQ(minFloat
, clampTo
<float>(-std::numeric_limits
<float>::infinity()));
158 TEST(MathExtrasTest
, clampToDouble
)
160 EXPECT_EQ(0.0, clampTo
<double>(0));
161 EXPECT_EQ(0.0, clampTo
<double>(0.0f
));
162 EXPECT_EQ(0.0, clampTo
<double>(0ULL));
163 EXPECT_EQ(3.5, clampTo
<double>(std::numeric_limits
<unsigned long long>::max(), 0.0, 3.5));
166 TEST(MathExtrasText
, clampToLongLongDouble
)
168 double overflowLL
= static_cast<double>(std::numeric_limits
<long long>::max()) * 2;
169 EXPECT_EQ(std::numeric_limits
<long long>::max(), clampTo
<long long>(overflowLL
));
170 EXPECT_EQ(std::numeric_limits
<long long>::min(), clampTo
<long long>(-overflowLL
));
173 TEST(MathExtrasText
, clampToUnsignedLongLongDouble
)
175 double overflowULL
= static_cast<double>(std::numeric_limits
<unsigned long long>::max()) * 2;
176 EXPECT_EQ(std::numeric_limits
<unsigned long long>::max(), clampTo
<unsigned long long>(overflowULL
));
177 EXPECT_EQ(std::numeric_limits
<unsigned long long>::min(), clampTo
<unsigned long long>(-overflowULL
));
180 TEST(MathExtrasTest
, clampToUnsignedUnsignedLong
)
182 if (sizeof(unsigned long) == sizeof(unsigned))
185 unsigned long maxUnsigned
= std::numeric_limits
<unsigned>::max();
186 unsigned long overflowUnsigned
= maxUnsigned
+ 1;
188 EXPECT_GT(overflowUnsigned
, maxUnsigned
);
190 EXPECT_EQ(maxUnsigned
, clampTo
<unsigned>(maxUnsigned
));
192 EXPECT_EQ(maxUnsigned
, clampTo
<unsigned>(overflowUnsigned
));
193 EXPECT_EQ(0u, clampTo
<unsigned>(-1));
196 TEST(MathExtrasTest
, clampToUnsignedUnsignedLongLong
)
198 unsigned long long maxUnsigned
= std::numeric_limits
<unsigned>::max();
199 unsigned long long overflowUnsigned
= maxUnsigned
+ 1;
201 EXPECT_GT(overflowUnsigned
, maxUnsigned
);
203 EXPECT_EQ(maxUnsigned
, clampTo
<unsigned>(maxUnsigned
));
205 EXPECT_EQ(maxUnsigned
, clampTo
<unsigned>(overflowUnsigned
));
206 EXPECT_EQ(0u, clampTo
<unsigned>(-1));
209 TEST(MathExtrasTest
, clampToLongLongUnsignedLongLong
)
211 long long maxLongLongLL
= std::numeric_limits
<long long>::max();
212 unsigned long long maxLongLongULL
= maxLongLongLL
;
213 unsigned long long overflowLongLong
= maxLongLongULL
+ 1;
215 EXPECT_GT(overflowLongLong
, maxLongLongULL
);
217 EXPECT_EQ(maxLongLongLL
, clampTo
<long long>(maxLongLongULL
));
218 EXPECT_EQ(maxLongLongLL
- 1, clampTo
<long long>(maxLongLongULL
- 1));
219 EXPECT_EQ(maxLongLongLL
, clampTo
<long long>(overflowLongLong
));
221 EXPECT_EQ(-3LL, clampTo
<long long>(2ULL, -5LL, -3LL));
224 TEST(MathExtrasTest
, clampToUnsignedLongLongInt
)
226 EXPECT_EQ(0ULL, clampTo
<unsigned long long>(-1));
227 EXPECT_EQ(0ULL, clampTo
<unsigned long long>(0));
228 EXPECT_EQ(1ULL, clampTo
<unsigned long long>(1));
231 TEST(MathExtrasTest
, clampToUnsignedLongLongUnsignedLongLong
)
233 EXPECT_EQ(0ULL, clampTo
<unsigned long long>(0ULL));
234 EXPECT_EQ(1ULL, clampTo
<unsigned long long>(0ULL, 1ULL, 2ULL));
235 EXPECT_EQ(2ULL, clampTo
<unsigned long long>(3ULL, 1ULL, 2ULL));
236 EXPECT_EQ(0xFFFFFFFFFFFFFFF5ULL
, clampTo
<unsigned long long>(0xFFFFFFFFFFFFFFF5ULL
));
239 // Make sure that various +-inf cases are handled properly (they aren't
240 // by default on VS).
241 TEST(MathExtrasTest
, infinityMath
)
243 double posInf
= std::numeric_limits
<double>::infinity();
244 double negInf
= -std::numeric_limits
<double>::infinity();
245 double nan
= std::numeric_limits
<double>::quiet_NaN();
247 EXPECT_EQ(M_PI_4
, atan2(posInf
, posInf
));
248 EXPECT_EQ(3.0 * M_PI_4
, atan2(posInf
, negInf
));
249 EXPECT_EQ(-M_PI_4
, atan2(negInf
, posInf
));
250 EXPECT_EQ(-3.0 * M_PI_4
, atan2(negInf
, negInf
));
252 EXPECT_EQ(0.0, fmod(0.0, posInf
));
253 EXPECT_EQ(7.0, fmod(7.0, posInf
));
254 EXPECT_EQ(-7.0, fmod(-7.0, posInf
));
255 EXPECT_EQ(0.0, fmod(0.0, negInf
));
256 EXPECT_EQ(7.0, fmod(7.0, negInf
));
257 EXPECT_EQ(-7.0, fmod(-7.0, negInf
));
259 EXPECT_EQ(1.0, pow(5.0, 0.0));
260 EXPECT_EQ(1.0, pow(-5.0, 0.0));
261 EXPECT_EQ(1.0, pow(nan
, 0.0));