| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012, Google Inc. All rights reserved. | 2 * Copyright (c) 2012, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 EXPECT_EQ(0, saturatedSubtraction(INT_MAX, INT_MAX)); | 103 EXPECT_EQ(0, saturatedSubtraction(INT_MAX, INT_MAX)); |
| 104 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX, INT_MIN)); | 104 EXPECT_EQ(INT_MAX, saturatedSubtraction(INT_MAX, INT_MIN)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST(SaturatedArithmeticTest, SetSigned) | 107 TEST(SaturatedArithmeticTest, SetSigned) |
| 108 { | 108 { |
| 109 const int kFractionBits = 6; | 109 const int kFractionBits = 6; |
| 110 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; | 110 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; |
| 111 const int intMinForLayoutUnit = INT_MIN >> kFractionBits; | 111 const int intMinForLayoutUnit = INT_MIN >> kFractionBits; |
| 112 | 112 |
| 113 EXPECT_EQ(0, saturatedSet(0, kFractionBits)); | 113 EXPECT_EQ(0, saturatedSet<kFractionBits>(0)); |
| 114 | 114 |
| 115 // Internally the max number we can represent (without saturating) | 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 | 116 // is all the (non-sign) bits set except for the bottom n fraction bits |
| 117 const int maxInternalRepresentation = INT_MAX ^ ((1 << kFractionBits)-1); | 117 const int maxInternalRepresentation = INT_MAX ^ ((1 << kFractionBits)-1); |
| 118 EXPECT_EQ(maxInternalRepresentation, | 118 EXPECT_EQ(maxInternalRepresentation, |
| 119 saturatedSet(intMaxForLayoutUnit, kFractionBits)); | 119 saturatedSet<kFractionBits>(intMaxForLayoutUnit)); |
| 120 | 120 |
| 121 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 121 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), |
| 122 saturatedSet(intMaxForLayoutUnit + 100, kFractionBits)); | 122 saturatedSet<kFractionBits>(intMaxForLayoutUnit + 100)); |
| 123 | 123 |
| 124 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | 124 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, |
| 125 saturatedSet(intMaxForLayoutUnit - 100, kFractionBits)); | 125 saturatedSet<kFractionBits>(intMaxForLayoutUnit - 100)); |
| 126 | 126 |
| 127 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | 127 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), |
| 128 saturatedSet(intMinForLayoutUnit, kFractionBits)); | 128 saturatedSet<kFractionBits>(intMinForLayoutUnit)); |
| 129 | 129 |
| 130 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | 130 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), |
| 131 saturatedSet(intMinForLayoutUnit - 100, kFractionBits)); | 131 saturatedSet<kFractionBits>(intMinForLayoutUnit - 100)); |
| 132 | 132 |
| 133 // Shifting negative numbers left has undefined behavior, so use | 133 // Shifting negative numbers left has undefined behavior, so use |
| 134 // multiplication instead of direct shifting here. | 134 // multiplication instead of direct shifting here. |
| 135 EXPECT_EQ((intMinForLayoutUnit + 100) * (1 << kFractionBits), | 135 EXPECT_EQ((intMinForLayoutUnit + 100) * (1 << kFractionBits), |
| 136 saturatedSet(intMinForLayoutUnit + 100, kFractionBits)); | 136 saturatedSet<kFractionBits>(intMinForLayoutUnit + 100)); |
| 137 } | 137 } |
| 138 | 138 |
| 139 TEST(SaturatedArithmeticTest, SetUnsigned) | 139 TEST(SaturatedArithmeticTest, SetUnsigned) |
| 140 { | 140 { |
| 141 const int kFractionBits = 6; | 141 const int kFractionBits = 6; |
| 142 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; | 142 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; |
| 143 | 143 |
| 144 EXPECT_EQ(0, saturatedSet((unsigned)0, kFractionBits)); | 144 EXPECT_EQ(0, saturatedSet<kFractionBits>((unsigned)0)); |
| 145 | 145 |
| 146 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 146 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), |
| 147 saturatedSet((unsigned)intMaxForLayoutUnit, kFractionBits)); | 147 saturatedSet<kFractionBits>((unsigned)intMaxForLayoutUnit)); |
| 148 | 148 |
| 149 const unsigned kOverflowed = intMaxForLayoutUnit + 100; | 149 const unsigned kOverflowed = intMaxForLayoutUnit + 100; |
| 150 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 150 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), |
| 151 saturatedSet(kOverflowed, kFractionBits)); | 151 saturatedSet<kFractionBits>(kOverflowed)); |
| 152 | 152 |
| 153 const unsigned kNotOverflowed = intMaxForLayoutUnit - 100; | 153 const unsigned kNotOverflowed = intMaxForLayoutUnit - 100; |
| 154 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | 154 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, |
| 155 saturatedSet(kNotOverflowed, kFractionBits)); | 155 saturatedSet<kFractionBits>(kNotOverflowed)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 } // namespace WTF | 158 } // namespace WTF |
| OLD | NEW |