c74e3c9e5474f69897e94612595475c2d6e3bd2e
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WTF / MathExtras.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation
3  * Copyright (C) 2019 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28
29 #include <wtf/MathExtras.h>
30
31 namespace TestWebKitAPI {
32
33 TEST(WTF, Lrint)
34 {
35     EXPECT_EQ(lrint(-7.5), -8);
36     EXPECT_EQ(lrint(-8.5), -8);
37     EXPECT_EQ(lrint(-0.5), 0);
38     EXPECT_EQ(lrint(0.5), 0);
39     EXPECT_EQ(lrint(-0.5), 0);
40     EXPECT_EQ(lrint(1.3), 1);
41     EXPECT_EQ(lrint(1.7), 2);
42     EXPECT_EQ(lrint(0), 0);
43     EXPECT_EQ(lrint(-0), 0);
44     if (sizeof(long int) == 8) {
45         // Largest double number with 0.5 precision and one halfway rounding case below.
46         EXPECT_EQ(lrint(pow(2.0, 52) - 0.5), pow(2.0, 52));
47         EXPECT_EQ(lrint(pow(2.0, 52) - 1.5), pow(2.0, 52) - 2);
48         // Smallest double number with 0.5 precision and one halfway rounding case above.
49         EXPECT_EQ(lrint(-pow(2.0, 52) + 0.5), -pow(2.0, 52));
50         EXPECT_EQ(lrint(-pow(2.0, 52) + 1.5), -pow(2.0, 52) + 2);
51     }
52 }
53
54 TEST(WTF, 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(clampTo<int>(maxInt), maxInt);
68     EXPECT_EQ(clampTo<int>(minInt), minInt);
69
70     EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
71     EXPECT_EQ(clampTo<int>(underflowInt), minInt);
72 }
73
74 TEST(WTF, 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(clampTo<int>(maxInt), maxInt);
85     EXPECT_EQ(clampTo<int>(minInt), minInt);
86
87     EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
88     EXPECT_EQ(clampTo<int>(underflowInt), minInt);
89 }
90
91 TEST(WTF, 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(clampToInteger(minInt), minInt);
107
108     EXPECT_NEAR(clampToInteger(overflowInt), maxInt, 1);
109     EXPECT_EQ(clampToInteger(underflowInt), minInt);
110 }
111
112 TEST(WTF, 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(clampToInteger(maxInt), maxInt);
123     EXPECT_EQ(clampToInteger(minInt), minInt);
124
125     EXPECT_EQ(clampToInteger(overflowInt), maxInt);
126     EXPECT_EQ(clampToInteger(underflowInt), minInt);
127 }
128
129 TEST(WTF, 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(clampToFloat(maxFloat), maxFloat);
140     EXPECT_EQ(clampToFloat(minFloat), minFloat);
141
142     EXPECT_EQ(clampToFloat(overflowFloat), maxFloat);
143     EXPECT_EQ(clampToFloat(underflowFloat), minFloat);
144
145     EXPECT_EQ(clampToFloat(std::numeric_limits<float>::infinity()), maxFloat);
146     EXPECT_EQ(clampToFloat(-std::numeric_limits<float>::infinity()), minFloat);
147 }
148
149 TEST(WTF, 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(clampTo<unsigned>(maxUnsigned), maxUnsigned);
160
161     EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
162     EXPECT_EQ(clampTo<unsigned>(-1), 0u);
163 }
164
165 TEST(WTF, 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(clampTo<unsigned>(maxUnsigned), maxUnsigned);
173
174     EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
175     EXPECT_EQ(clampTo<unsigned>(-1), 0u);
176 }
177
178 #if !COMPILER(MSVC)
179 template<typename TargetType, typename SourceType>
180 static void testClampFloatingPointToFloatingPoint()
181 {
182     // No clamping.
183     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(0)), static_cast<TargetType>(0));
184     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(1)), static_cast<TargetType>(1));
185     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(1.5)), static_cast<TargetType>(1.5));
186     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-1)), static_cast<TargetType>(-1));
187     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-1.5)), static_cast<TargetType>(-1.5));
188
189     // Explicit boundaries, clamped or not.
190     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-42), static_cast<SourceType>(-42.5)), static_cast<TargetType>(-42));
191     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-43), static_cast<SourceType>(-42.5)), static_cast<TargetType>(static_cast<SourceType>(-42.5)));
192     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(42), static_cast<SourceType>(41), static_cast<SourceType>(42.5)), static_cast<TargetType>(42));
193     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(43), static_cast<SourceType>(41), static_cast<SourceType>(42.5)), static_cast<TargetType>(static_cast<SourceType>(42.5)));
194
195     // Integer bounds.
196     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::max()) + 1), static_cast<TargetType>(std::numeric_limits<int32_t>::max()) + 1);
197     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int64_t>::max())), static_cast<TargetType>(std::numeric_limits<int64_t>::max()));
198     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::max()) + 1), static_cast<TargetType>(std::numeric_limits<int32_t>::max()) + 1);
199
200     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min())), static_cast<TargetType>(std::numeric_limits<int32_t>::min()));
201     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int64_t>::min())), static_cast<TargetType>(std::numeric_limits<int64_t>::min()));
202
203     if (std::is_same<TargetType, double>::value && std::is_same<SourceType, float>::value) {
204         // If the source is float and target is double, the input of those cases has lost bits in float.
205         // In that case, we also round the expectation to float.
206         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::max())), static_cast<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::max())));
207         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1), static_cast<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1));
208         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1), static_cast<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1));
209     } else {
210         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::max())), static_cast<TargetType>(std::numeric_limits<int32_t>::max()));
211         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1), static_cast<TargetType>(std::numeric_limits<int32_t>::min()) - 1);
212         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<int32_t>::min()) - 1), static_cast<TargetType>(std::numeric_limits<int32_t>::min()) - 1);
213     }
214
215     // At the limit.
216     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<TargetType>::max()), std::numeric_limits<TargetType>::max());
217     EXPECT_EQ(clampTo<TargetType>(-std::numeric_limits<TargetType>::max()), -std::numeric_limits<TargetType>::max());
218
219     // At Epsilon from the limit.
220     TargetType epsilon = std::numeric_limits<TargetType>::epsilon();
221     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<TargetType>::max() - epsilon), std::numeric_limits<TargetType>::max() - epsilon);
222     EXPECT_EQ(clampTo<TargetType>(-std::numeric_limits<TargetType>::max() + epsilon), -std::numeric_limits<TargetType>::max() + epsilon);
223
224     // Infinity.
225     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::infinity()), std::numeric_limits<TargetType>::max());
226     EXPECT_EQ(clampTo<TargetType>(-std::numeric_limits<SourceType>::infinity()), -std::numeric_limits<TargetType>::max());
227 }
228
229 TEST(WTF, clampFloatingPointToFloatingPoint)
230 {
231     testClampFloatingPointToFloatingPoint<float, float>();
232     testClampFloatingPointToFloatingPoint<double, double>();
233
234     testClampFloatingPointToFloatingPoint<double, float>();
235     testClampFloatingPointToFloatingPoint<float, double>();
236
237     // Large double into smaller float.
238     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max())), std::numeric_limits<float>::max());
239     EXPECT_EQ(clampTo<float>(-static_cast<double>(std::numeric_limits<float>::max())), -std::numeric_limits<float>::max());
240     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max()) + 1), std::numeric_limits<float>::max());
241     EXPECT_EQ(clampTo<float>(-static_cast<double>(std::numeric_limits<float>::max()) - 1), -std::numeric_limits<float>::max());
242     EXPECT_EQ(clampTo<float>(std::numeric_limits<double>::max()), std::numeric_limits<float>::max());
243     EXPECT_EQ(clampTo<float>(-std::numeric_limits<double>::max()), -std::numeric_limits<float>::max());
244
245     float floatEspilon = std::numeric_limits<float>::epsilon();
246     double doubleEspilon = std::numeric_limits<double>::epsilon();
247     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max()) + doubleEspilon), std::numeric_limits<float>::max());
248     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max()) - doubleEspilon), std::numeric_limits<float>::max());
249     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max()) + floatEspilon), std::numeric_limits<float>::max());
250     EXPECT_EQ(clampTo<float>(static_cast<double>(std::numeric_limits<float>::max()) - floatEspilon), std::numeric_limits<float>::max() - floatEspilon);
251 }
252 #endif // !COMPILER(MSVC)
253
254 template<typename FloatingPointType>
255 static void testClampFloatingPointToInteger()
256 {
257     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(0)), 0);
258     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(1)), 1);
259     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(-1)), -1);
260     if (std::is_same<FloatingPointType, double>::value)
261         EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max()) - 1.f), std::numeric_limits<int32_t>::max() - 1);
262     else
263         EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max()) - 1.f), std::numeric_limits<int32_t>::max());
264     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max())), std::numeric_limits<int32_t>::max());
265     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max()) + 1.f), std::numeric_limits<int32_t>::max());
266
267     if (std::is_same<FloatingPointType, double>::value)
268         EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min()) + 1.f), std::numeric_limits<int32_t>::min() + 1);
269     else
270         EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min()) + 1.f), std::numeric_limits<int32_t>::min());
271     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min())), std::numeric_limits<int32_t>::min());
272     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min()) - 1.f), std::numeric_limits<int32_t>::min());
273
274     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint32_t>::max())), std::numeric_limits<int32_t>::max());
275     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint32_t>::max()) + 1.f), std::numeric_limits<int32_t>::max());
276     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint32_t>::min())), 0.f);
277
278     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int64_t>::max())), std::numeric_limits<int32_t>::max());
279     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int64_t>::max()) + 1.f), std::numeric_limits<int32_t>::max());
280     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int64_t>::min())), std::numeric_limits<int32_t>::min());
281     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int64_t>::min()) - 1.f), std::numeric_limits<int32_t>::min());
282
283     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint64_t>::max())), std::numeric_limits<int32_t>::max());
284     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint64_t>::max()) + 1.f), std::numeric_limits<int32_t>::max());
285     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<uint64_t>::min())), 0.f);
286
287     FloatingPointType epsilon = std::numeric_limits<FloatingPointType>::epsilon();
288     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max()) - epsilon), std::numeric_limits<int32_t>::max());
289     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::max()) + epsilon), std::numeric_limits<int32_t>::max());
290     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min()) - epsilon), std::numeric_limits<int32_t>::min());
291     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<int32_t>::min()) + epsilon), std::numeric_limits<int32_t>::min());
292
293     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(std::numeric_limits<FloatingPointType>::infinity())), std::numeric_limits<int32_t>::max());
294     EXPECT_EQ(clampTo<int32_t>(static_cast<FloatingPointType>(-std::numeric_limits<FloatingPointType>::infinity())), std::numeric_limits<int32_t>::min());
295 }
296
297 TEST(WTF, clampFloatToInt)
298 {
299     testClampFloatingPointToInteger<float>();
300     testClampFloatingPointToInteger<double>();
301
302     // 2**24 = 16777216, the largest integer representable exactly as float.
303     EXPECT_EQ(clampTo<int32_t>(static_cast<float>(16777215)), 16777215);
304     EXPECT_EQ(clampTo<int32_t>(static_cast<float>(16777216)), 16777216);
305     EXPECT_EQ(clampTo<int32_t>(static_cast<float>(16777217)), 16777216);
306     EXPECT_EQ(clampTo<int32_t>(static_cast<double>(16777216)), 16777216);
307     EXPECT_EQ(clampTo<int32_t>(static_cast<double>(16777217)), 16777217);
308
309     EXPECT_EQ(clampTo<int16_t>(static_cast<float>(16777215)), std::numeric_limits<int16_t>::max());
310     EXPECT_EQ(clampTo<int16_t>(static_cast<float>(16777216)), std::numeric_limits<int16_t>::max());
311     EXPECT_EQ(clampTo<int16_t>(static_cast<float>(16777217)), std::numeric_limits<int16_t>::max());
312
313     // 2**53 = 9007199254740992, the largest integer representable exactly as double.
314     EXPECT_EQ(clampTo<uint64_t>(static_cast<double>(9007199254740991)), static_cast<uint64_t>(9007199254740991));
315     EXPECT_EQ(clampTo<uint64_t>(static_cast<double>(9007199254740992)), static_cast<uint64_t>(9007199254740992));
316     EXPECT_EQ(clampTo<uint64_t>(static_cast<double>(9007199254740993)), static_cast<uint64_t>(9007199254740992));
317
318     EXPECT_EQ(clampTo<int32_t>(static_cast<double>(9007199254740991)), std::numeric_limits<int32_t>::max());
319     EXPECT_EQ(clampTo<int32_t>(static_cast<double>(9007199254740992)), std::numeric_limits<int32_t>::max());
320     EXPECT_EQ(clampTo<int32_t>(static_cast<double>(9007199254740993)), std::numeric_limits<int32_t>::max());
321 }
322
323 template<typename TargetType, typename SourceType>
324 static void testClampSameSignIntegers()
325 {
326     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(0)), static_cast<TargetType>(0));
327     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(1)), static_cast<TargetType>(1));
328     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-1)), std::numeric_limits<TargetType>::is_signed ? static_cast<TargetType>(-1) : std::numeric_limits<TargetType>::max());
329
330     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::min())), std::numeric_limits<TargetType>::min());
331     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max())), std::numeric_limits<TargetType>::max());
332
333     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::min()), std::numeric_limits<TargetType>::min());
334     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::max()), std::numeric_limits<TargetType>::max());
335 }
336
337 TEST(WTF, clampSameSignIntegers)
338 {
339     testClampSameSignIntegers<char, char>();
340     testClampSameSignIntegers<unsigned char, unsigned char>();
341     testClampSameSignIntegers<char, int32_t>();
342     testClampSameSignIntegers<unsigned char, uint32_t>();
343     testClampSameSignIntegers<char, int64_t>();
344     testClampSameSignIntegers<unsigned char, uint64_t>();
345
346     testClampSameSignIntegers<int32_t, int32_t>();
347     testClampSameSignIntegers<uint32_t, uint32_t>();
348     testClampSameSignIntegers<int32_t, int64_t>();
349     testClampSameSignIntegers<uint32_t, uint64_t>();
350     testClampSameSignIntegers<int16_t, int64_t>();
351     testClampSameSignIntegers<uint16_t, uint64_t>();
352 }
353
354 template<typename TargetType, typename SourceType>
355 static void testClampUnsignedToSigned()
356 {
357     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(0)), static_cast<TargetType>(0));
358     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(1)), static_cast<TargetType>(1));
359
360     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max()) - 1), std::numeric_limits<TargetType>::max() - 1);
361     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max())), std::numeric_limits<TargetType>::max());
362     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max()) + 1), std::numeric_limits<TargetType>::max());
363     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::max()), std::numeric_limits<TargetType>::max());
364     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::max() - 1), std::numeric_limits<TargetType>::max());
365 }
366
367 TEST(WTF, clampUnsignedToSigned)
368 {
369     testClampUnsignedToSigned<char, unsigned char>();
370     testClampUnsignedToSigned<char, uint32_t>();
371     testClampUnsignedToSigned<int32_t, uint32_t>();
372     testClampUnsignedToSigned<int64_t, uint64_t>();
373     testClampUnsignedToSigned<int32_t, uint64_t>();
374     testClampUnsignedToSigned<int16_t, uint32_t>();
375     testClampUnsignedToSigned<int16_t, uint64_t>();
376 }
377
378 template<typename TargetType, typename SourceType>
379 static void testClampSignedToUnsigned()
380 {
381     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(0)), static_cast<TargetType>(0));
382     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(1)), static_cast<TargetType>(1));
383     EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(-1)), static_cast<TargetType>(0));
384
385     if (sizeof(TargetType) < sizeof(SourceType)) {
386         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::min())), static_cast<TargetType>(0));
387         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max()) - 1), std::numeric_limits<TargetType>::max() - 1);
388         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max())), std::numeric_limits<TargetType>::max());
389         EXPECT_EQ(clampTo<TargetType>(static_cast<SourceType>(std::numeric_limits<TargetType>::max()) + 1), std::numeric_limits<TargetType>::max());
390     }
391
392     EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::min()), static_cast<TargetType>(0));
393     if (sizeof(TargetType) < sizeof(SourceType))
394         EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::max()), std::numeric_limits<TargetType>::max());
395     else
396         EXPECT_EQ(clampTo<TargetType>(std::numeric_limits<SourceType>::max()), static_cast<TargetType>(std::numeric_limits<SourceType>::max()));
397 }
398
399 TEST(WTF, clampSignedToUnsigned)
400 {
401     testClampSignedToUnsigned<unsigned char, char>();
402     testClampSignedToUnsigned<unsigned char, int32_t>();
403     testClampSignedToUnsigned<uint32_t, int32_t>();
404     testClampSignedToUnsigned<uint64_t, int64_t>();
405     testClampSignedToUnsigned<uint32_t, int64_t>();
406     testClampSignedToUnsigned<uint16_t, int32_t>();
407     testClampSignedToUnsigned<uint16_t, int64_t>();
408 }
409
410 TEST(WTF, roundUpToPowerOfTwo)
411 {
412     EXPECT_EQ(WTF::roundUpToPowerOfTwo(UINT32_MAX), 0U);
413     EXPECT_EQ(WTF::roundUpToPowerOfTwo(1U << 31), (1U << 31));
414     EXPECT_EQ(WTF::roundUpToPowerOfTwo((1U << 31) + 1), 0U);
415 }
416
417 } // namespace TestWebKitAPI