Chromium Code Reviews| 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 int v0 = saturatedSetSigned<32-kFractionBits, kFractionBits>(0); |
|
Sami
2014/07/09 13:59:26
Ditto. Same for the tests below.
picksi
2014/07/09 14:27:52
Done.
| |
| 114 EXPECT_EQ(0, v0); | |
| 114 | 115 |
| 115 // Internally the max number we can represent (without saturating) | 116 // 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 // is all the (non-sign) bits set except for the bottom n fraction bits |
| 117 const int maxInternalRepresentation = INT_MAX ^ ((1 << kFractionBits)-1); | 118 const int maxInternalRepresentation = INT_MAX ^ ((1 << kFractionBits)-1); |
| 118 EXPECT_EQ(maxInternalRepresentation, | |
| 119 saturatedSet(intMaxForLayoutUnit, kFractionBits)); | |
| 120 | 119 |
| 121 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 120 int v1 = saturatedSetSigned< |
| 122 saturatedSet(intMaxForLayoutUnit + 100, kFractionBits)); | 121 32-kFractionBits, |
| 122 kFractionBits>(intMaxForLayoutUnit); | |
| 123 EXPECT_EQ(maxInternalRepresentation, v1); | |
| 123 | 124 |
| 124 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | 125 int v2 = saturatedSetSigned< |
| 125 saturatedSet(intMaxForLayoutUnit - 100, kFractionBits)); | 126 32-kFractionBits, |
| 127 kFractionBits>(intMaxForLayoutUnit + 100); | |
| 128 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), v2); | |
| 126 | 129 |
| 127 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | 130 int v3 = saturatedSetSigned< |
| 128 saturatedSet(intMinForLayoutUnit, kFractionBits)); | 131 32-kFractionBits, |
| 132 kFractionBits>(intMaxForLayoutUnit - 100); | |
| 133 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, v3); | |
| 129 | 134 |
| 130 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | 135 int v4 = saturatedSetSigned< |
| 131 saturatedSet(intMinForLayoutUnit - 100, kFractionBits)); | 136 32-kFractionBits, |
| 137 kFractionBits>(intMinForLayoutUnit); | |
| 138 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), v4); | |
| 132 | 139 |
| 133 EXPECT_EQ((intMinForLayoutUnit + 100) << kFractionBits, | 140 int v5 = saturatedSetSigned< |
| 134 saturatedSet(intMinForLayoutUnit + 100, kFractionBits)); | 141 32-kFractionBits, |
| 142 kFractionBits>(intMinForLayoutUnit - 100); | |
| 143 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), v5); | |
| 144 | |
| 145 int v6 = saturatedSetSigned< | |
| 146 32-kFractionBits, | |
| 147 kFractionBits>(intMinForLayoutUnit + 100); | |
| 148 EXPECT_EQ((intMinForLayoutUnit + 100) << kFractionBits, v6); | |
| 135 } | 149 } |
| 136 | 150 |
| 137 TEST(SaturatedArithmeticTest, SetUnsigned) | 151 TEST(SaturatedArithmeticTest, SetUnsigned) |
| 138 { | 152 { |
| 139 const int kFractionBits = 6; | 153 const int kFractionBits = 6; |
| 140 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; | 154 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; |
| 141 | 155 |
| 142 EXPECT_EQ(0, saturatedSet((unsigned)0, kFractionBits)); | 156 int v1 = saturatedSetUnsigned<31-kFractionBits, kFractionBits>(0); |
| 157 EXPECT_EQ(0, v1); | |
| 143 | 158 |
| 144 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 159 int v2 = saturatedSetUnsigned< |
| 145 saturatedSet((unsigned)intMaxForLayoutUnit, kFractionBits)); | 160 31-kFractionBits, |
| 161 kFractionBits>((unsigned)intMaxForLayoutUnit); | |
| 162 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), v2); | |
| 146 | 163 |
| 147 const unsigned kOverflowed = intMaxForLayoutUnit + 100; | 164 const unsigned kOverflowed = intMaxForLayoutUnit + 100; |
| 148 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | 165 int v4 = saturatedSetUnsigned< |
| 149 saturatedSet(kOverflowed, kFractionBits)); | 166 31-kFractionBits, |
| 167 kFractionBits>(kOverflowed); | |
| 168 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), v4); | |
| 150 | 169 |
| 151 const unsigned kNotOverflowed = intMaxForLayoutUnit - 100; | 170 const unsigned kNotOverflowed = intMaxForLayoutUnit - 100; |
| 152 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | 171 int v5 = saturatedSetUnsigned< |
| 153 saturatedSet(kNotOverflowed, kFractionBits)); | 172 31-kFractionBits, |
| 173 kFractionBits>(kNotOverflowed); | |
| 174 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, v5); | |
| 154 } | 175 } |
| 155 | 176 |
| 156 | 177 |
| 157 } // namespace | 178 } // namespace |
| OLD | NEW |