Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / SaturatedArithmeticTest.cpp
blob360244a028eb8ecaee738d35c910f31e73eaae65
1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
33 #include "wtf/SaturatedArithmetic.h"
34 #include <gtest/gtest.h>
35 #include <limits.h>
37 namespace WTF {
39 TEST(SaturatedArithmeticTest, Addition)
41 EXPECT_EQ(0, saturatedAddition(0, 0));
42 EXPECT_EQ(1, saturatedAddition(0, 1));
43 EXPECT_EQ(100, saturatedAddition(0, 100));
44 EXPECT_EQ(150, saturatedAddition(100, 50));
46 EXPECT_EQ(-1, saturatedAddition(0, -1));
47 EXPECT_EQ(0, saturatedAddition(1, -1));
48 EXPECT_EQ(50, saturatedAddition(100, -50));
49 EXPECT_EQ(-50, saturatedAddition(50, -100));
51 EXPECT_EQ(INT_MAX - 1, saturatedAddition(INT_MAX - 1, 0));
52 EXPECT_EQ(INT_MAX, saturatedAddition(INT_MAX - 1, 1));
53 EXPECT_EQ(INT_MAX, saturatedAddition(INT_MAX - 1, 2));
54 EXPECT_EQ(INT_MAX - 1, saturatedAddition(0, INT_MAX - 1));
55 EXPECT_EQ(INT_MAX, saturatedAddition(1, INT_MAX - 1));
56 EXPECT_EQ(INT_MAX, saturatedAddition(2, INT_MAX - 1));
57 EXPECT_EQ(INT_MAX, saturatedAddition(INT_MAX - 1, INT_MAX - 1));
58 EXPECT_EQ(INT_MAX, saturatedAddition(INT_MAX, INT_MAX));
60 EXPECT_EQ(INT_MIN, saturatedAddition(INT_MIN, 0));
61 EXPECT_EQ(INT_MIN + 1, saturatedAddition(INT_MIN + 1, 0));
62 EXPECT_EQ(INT_MIN + 2, saturatedAddition(INT_MIN + 1, 1));
63 EXPECT_EQ(INT_MIN + 3, saturatedAddition(INT_MIN + 1, 2));
64 EXPECT_EQ(INT_MIN, saturatedAddition(INT_MIN + 1, -1));
65 EXPECT_EQ(INT_MIN, saturatedAddition(INT_MIN + 1, -2));
66 EXPECT_EQ(INT_MIN + 1, saturatedAddition(0, INT_MIN + 1));
67 EXPECT_EQ(INT_MIN, saturatedAddition(-1, INT_MIN + 1));
68 EXPECT_EQ(INT_MIN, saturatedAddition(-2, INT_MIN + 1));
70 EXPECT_EQ(INT_MAX / 2 + 10000, saturatedAddition(INT_MAX / 2, 10000));
71 EXPECT_EQ(INT_MAX, saturatedAddition(INT_MAX / 2 + 1, INT_MAX / 2 + 1));
72 EXPECT_EQ(-1, saturatedAddition(INT_MIN, INT_MAX));
75 TEST(SaturatedArithmeticTest, Subtraction)
77 EXPECT_EQ(0, saturatedSubtraction(0, 0));
78 EXPECT_EQ(-1, saturatedSubtraction(0, 1));
79 EXPECT_EQ(-100, saturatedSubtraction(0, 100));
80 EXPECT_EQ(50, saturatedSubtraction(100, 50));
82 EXPECT_EQ(1, saturatedSubtraction(0, -1));
83 EXPECT_EQ(2, saturatedSubtraction(1, -1));
84 EXPECT_EQ(150, saturatedSubtraction(100, -50));
85 EXPECT_EQ(150, saturatedSubtraction(50, -100));
87 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX, 0));
88 EXPECT_EQ(INT_MAX - 1, saturatedSubtraction(INT_MAX, 1));
89 EXPECT_EQ(INT_MAX - 1, saturatedSubtraction(INT_MAX - 1, 0));
90 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX - 1, -1));
91 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX - 1, -2));
92 EXPECT_EQ(-INT_MAX + 1, saturatedSubtraction(0, INT_MAX - 1));
93 EXPECT_EQ(-INT_MAX, saturatedSubtraction(-1, INT_MAX - 1));
94 EXPECT_EQ(-INT_MAX - 1, saturatedSubtraction(-2, INT_MAX - 1));
95 EXPECT_EQ(-INT_MAX - 1, saturatedSubtraction(-3, INT_MAX - 1));
97 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN, 0));
98 EXPECT_EQ(INT_MIN + 1, saturatedSubtraction(INT_MIN + 1, 0));
99 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 1));
100 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 2));
102 EXPECT_EQ(0, saturatedSubtraction(INT_MIN, INT_MIN));
103 EXPECT_EQ(0, saturatedSubtraction(INT_MAX, INT_MAX));
104 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX, INT_MIN));
107 TEST(SaturatedArithmeticTest, SetSigned)
109 const int kFractionBits = 6;
110 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits;
111 const int intMinForLayoutUnit = INT_MIN >> kFractionBits;
113 EXPECT_EQ(0, saturatedSet(0, kFractionBits));
115 // Internally the max number we can represent (without saturating)
116 // is all the (non-sign) bits set except for the bottom n fraction bits
117 const int maxInternalRepresentation = INT_MAX ^ ((1 << kFractionBits)-1);
118 EXPECT_EQ(maxInternalRepresentation,
119 saturatedSet(intMaxForLayoutUnit, kFractionBits));
121 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits),
122 saturatedSet(intMaxForLayoutUnit + 100, kFractionBits));
124 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits,
125 saturatedSet(intMaxForLayoutUnit - 100, kFractionBits));
127 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits),
128 saturatedSet(intMinForLayoutUnit, kFractionBits));
130 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits),
131 saturatedSet(intMinForLayoutUnit - 100, kFractionBits));
133 EXPECT_EQ((intMinForLayoutUnit + 100) << kFractionBits,
134 saturatedSet(intMinForLayoutUnit + 100, kFractionBits));
137 TEST(SaturatedArithmeticTest, SetUnsigned)
139 const int kFractionBits = 6;
140 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits;
142 EXPECT_EQ(0, saturatedSet((unsigned)0, kFractionBits));
144 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits),
145 saturatedSet((unsigned)intMaxForLayoutUnit, kFractionBits));
147 const unsigned kOverflowed = intMaxForLayoutUnit + 100;
148 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits),
149 saturatedSet(kOverflowed, kFractionBits));
151 const unsigned kNotOverflowed = intMaxForLayoutUnit - 100;
152 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits,
153 saturatedSet(kNotOverflowed, kFractionBits));
156 } // namespace WTF