Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: third_party/WebKit/Source/wtf/MathExtrasTest.cpp

Issue 2771783003: Move wtf_unittests to platform/wtf/. (Closed)
Patch Set: Rebase. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "wtf/MathExtras.h"
27
28 #include "testing/gtest/include/gtest/gtest.h"
29
30 namespace WTF {
31
32 TEST(MathExtrasTest, Lrint) {
33 EXPECT_EQ(-8, lrint(-7.5));
34 EXPECT_EQ(-8, lrint(-8.5));
35 EXPECT_EQ(0, lrint(-0.5));
36 EXPECT_EQ(0, lrint(0.5));
37 EXPECT_EQ(0, lrint(-0.5));
38 EXPECT_EQ(1, lrint(1.3));
39 EXPECT_EQ(2, lrint(1.7));
40 EXPECT_EQ(0, lrint(0));
41 EXPECT_EQ(0, lrint(-0));
42 if (sizeof(long int) == 8) {
43 // Largest double number with 0.5 precision and one halfway rounding case
44 // below.
45 EXPECT_EQ(pow(2.0, 52), lrint(pow(2.0, 52) - 0.5));
46 EXPECT_EQ(pow(2.0, 52) - 2, lrint(pow(2.0, 52) - 1.5));
47 // Smallest double number with 0.5 precision and one halfway rounding case
48 // 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
54 TEST(MathExtrasTest, clampToIntLong) {
55 if (sizeof(long) == sizeof(int))
56 return;
57
58 long maxInt = std::numeric_limits<int>::max();
59 long minInt = std::numeric_limits<int>::min();
60 long overflowInt = maxInt + 1;
61 long underflowInt = minInt - 1;
62
63 EXPECT_GT(overflowInt, maxInt);
64 EXPECT_LT(underflowInt, minInt);
65
66 EXPECT_EQ(maxInt, clampTo<int>(maxInt));
67 EXPECT_EQ(minInt, clampTo<int>(minInt));
68
69 EXPECT_EQ(maxInt, clampTo<int>(overflowInt));
70 EXPECT_EQ(minInt, clampTo<int>(underflowInt));
71 }
72
73 TEST(MathExtrasTest, clampToIntLongLong) {
74 long long maxInt = std::numeric_limits<int>::max();
75 long long minInt = std::numeric_limits<int>::min();
76 long long overflowInt = maxInt + 1;
77 long long underflowInt = minInt - 1;
78
79 EXPECT_GT(overflowInt, maxInt);
80 EXPECT_LT(underflowInt, minInt);
81
82 EXPECT_EQ(maxInt, clampTo<int>(maxInt));
83 EXPECT_EQ(minInt, clampTo<int>(minInt));
84
85 EXPECT_EQ(maxInt, clampTo<int>(overflowInt));
86 EXPECT_EQ(minInt, clampTo<int>(underflowInt));
87 }
88
89 TEST(MathExtrasTest, clampToIntFloat) {
90 float maxInt = static_cast<float>(std::numeric_limits<int>::max());
91 float minInt = static_cast<float>(std::numeric_limits<int>::min());
92 float overflowInt = maxInt * 1.1f;
93 float underflowInt = minInt * 1.1f;
94
95 EXPECT_GT(overflowInt, maxInt);
96 EXPECT_LT(underflowInt, minInt);
97
98 EXPECT_EQ(maxInt, clampTo<int>(maxInt));
99 EXPECT_EQ(minInt, clampTo<int>(minInt));
100
101 EXPECT_EQ(maxInt, clampTo<int>(overflowInt));
102 EXPECT_EQ(minInt, clampTo<int>(underflowInt));
103
104 // This value and the value one greater are typically represented the same
105 // way when stored in a 32-bit float. Make sure clamping does not cause us
106 // to erroneously jump to the larger value.
107 int nearFloatPrecisionLimit = 2147483520;
108 EXPECT_EQ(nearFloatPrecisionLimit,
109 clampTo<int>(static_cast<float>(nearFloatPrecisionLimit), 0,
110 nearFloatPrecisionLimit + 1));
111 EXPECT_EQ(-nearFloatPrecisionLimit,
112 clampTo<int>(static_cast<float>(-nearFloatPrecisionLimit),
113 -nearFloatPrecisionLimit - 1, 0));
114 }
115
116 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;
123
124 EXPECT_LT(almostOverflowInt, maxInt);
125 EXPECT_GT(overflowInt, maxInt);
126 EXPECT_GT(almostUnderflowInt, minInt);
127 EXPECT_LT(underflowInt, minInt);
128
129 EXPECT_EQ(maxInt, clampTo<int>(static_cast<double>(maxInt)));
130 EXPECT_EQ(minInt, clampTo<int>(static_cast<double>(minInt)));
131
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));
136 }
137
138 TEST(MathExtrasTest, clampToFloatDouble) {
139 double maxFloat = std::numeric_limits<float>::max();
140 double minFloat = -maxFloat;
141 double overflowFloat = maxFloat * 1.1;
142 double underflowFloat = minFloat * 1.1;
143
144 EXPECT_GT(overflowFloat, maxFloat);
145 EXPECT_LT(underflowFloat, minFloat);
146
147 EXPECT_EQ(maxFloat, clampTo<float>(maxFloat));
148 EXPECT_EQ(minFloat, clampTo<float>(minFloat));
149
150 EXPECT_EQ(maxFloat, clampTo<float>(overflowFloat));
151 EXPECT_EQ(minFloat, clampTo<float>(underflowFloat));
152
153 EXPECT_EQ(maxFloat, clampTo<float>(std::numeric_limits<float>::infinity()));
154 EXPECT_EQ(minFloat, clampTo<float>(-std::numeric_limits<float>::infinity()));
155 }
156
157 TEST(MathExtrasTest, clampToDouble) {
158 EXPECT_EQ(0.0, clampTo<double>(0));
159 EXPECT_EQ(0.0, clampTo<double>(0.0f));
160 EXPECT_EQ(0.0, clampTo<double>(0ULL));
161 EXPECT_EQ(3.5, clampTo<double>(std::numeric_limits<unsigned long long>::max(),
162 0.0, 3.5));
163 }
164
165 TEST(MathExtrasText, clampToLongLongDouble) {
166 double overflowLL =
167 static_cast<double>(std::numeric_limits<long long>::max()) * 2;
168 EXPECT_EQ(std::numeric_limits<long long>::max(),
169 clampTo<long long>(overflowLL));
170 EXPECT_EQ(std::numeric_limits<long long>::min(),
171 clampTo<long long>(-overflowLL));
172 }
173
174 TEST(MathExtrasText, clampToUnsignedLongLongDouble) {
175 double overflowULL =
176 static_cast<double>(std::numeric_limits<unsigned long long>::max()) * 2;
177 EXPECT_EQ(std::numeric_limits<unsigned long long>::max(),
178 clampTo<unsigned long long>(overflowULL));
179 EXPECT_EQ(std::numeric_limits<unsigned long long>::min(),
180 clampTo<unsigned long long>(-overflowULL));
181 }
182
183 TEST(MathExtrasTest, clampToUnsignedUnsignedLong) {
184 if (sizeof(unsigned long) == sizeof(unsigned))
185 return;
186
187 unsigned long maxUnsigned = std::numeric_limits<unsigned>::max();
188 unsigned long overflowUnsigned = maxUnsigned + 1;
189
190 EXPECT_GT(overflowUnsigned, maxUnsigned);
191
192 EXPECT_EQ(maxUnsigned, clampTo<unsigned>(maxUnsigned));
193
194 EXPECT_EQ(maxUnsigned, clampTo<unsigned>(overflowUnsigned));
195 EXPECT_EQ(0u, clampTo<unsigned>(-1));
196 }
197
198 TEST(MathExtrasTest, clampToUnsignedUnsignedLongLong) {
199 unsigned long long maxUnsigned = std::numeric_limits<unsigned>::max();
200 unsigned long long overflowUnsigned = maxUnsigned + 1;
201
202 EXPECT_GT(overflowUnsigned, maxUnsigned);
203
204 EXPECT_EQ(maxUnsigned, clampTo<unsigned>(maxUnsigned));
205
206 EXPECT_EQ(maxUnsigned, clampTo<unsigned>(overflowUnsigned));
207 EXPECT_EQ(0u, clampTo<unsigned>(-1));
208 }
209
210 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;
214
215 EXPECT_GT(overflowLongLong, maxLongLongULL);
216
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));
220
221 EXPECT_EQ(-3LL, clampTo<long long>(2ULL, -5LL, -3LL));
222 }
223
224 TEST(MathExtrasTest, clampToUnsignedLongLongInt) {
225 EXPECT_EQ(0ULL, clampTo<unsigned long long>(-1));
226 EXPECT_EQ(0ULL, clampTo<unsigned long long>(0));
227 EXPECT_EQ(1ULL, clampTo<unsigned long long>(1));
228 }
229
230 TEST(MathExtrasTest, clampToUnsignedLongLongUnsignedLongLong) {
231 EXPECT_EQ(0ULL, clampTo<unsigned long long>(0ULL));
232 EXPECT_EQ(1ULL, clampTo<unsigned long long>(0ULL, 1ULL, 2ULL));
233 EXPECT_EQ(2ULL, clampTo<unsigned long long>(3ULL, 1ULL, 2ULL));
234 EXPECT_EQ(0xFFFFFFFFFFFFFFF5ULL,
235 clampTo<unsigned long long>(0xFFFFFFFFFFFFFFF5ULL));
236 }
237
238 // Make sure that various +-inf cases are handled properly (they aren't
239 // by default on VS).
240 TEST(MathExtrasTest, infinityMath) {
241 double posInf = std::numeric_limits<double>::infinity();
242 double negInf = -std::numeric_limits<double>::infinity();
243 double nan = std::numeric_limits<double>::quiet_NaN();
244
245 EXPECT_EQ(M_PI_4, atan2(posInf, posInf));
246 EXPECT_EQ(3.0 * M_PI_4, atan2(posInf, negInf));
247 EXPECT_EQ(-M_PI_4, atan2(negInf, posInf));
248 EXPECT_EQ(-3.0 * M_PI_4, atan2(negInf, negInf));
249
250 EXPECT_EQ(0.0, fmod(0.0, posInf));
251 EXPECT_EQ(7.0, fmod(7.0, posInf));
252 EXPECT_EQ(-7.0, fmod(-7.0, posInf));
253 EXPECT_EQ(0.0, fmod(0.0, negInf));
254 EXPECT_EQ(7.0, fmod(7.0, negInf));
255 EXPECT_EQ(-7.0, fmod(-7.0, negInf));
256
257 EXPECT_EQ(1.0, pow(5.0, 0.0));
258 EXPECT_EQ(1.0, pow(-5.0, 0.0));
259 EXPECT_EQ(1.0, pow(nan, 0.0));
260 }
261
262 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/ListHashSetTest.cpp ('k') | third_party/WebKit/Source/wtf/OptionalTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698