| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN, 0)); | 97 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN, 0)); |
| 98 EXPECT_EQ(INT_MIN + 1, saturatedSubtraction(INT_MIN + 1, 0)); | 98 EXPECT_EQ(INT_MIN + 1, saturatedSubtraction(INT_MIN + 1, 0)); |
| 99 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 1)); | 99 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 1)); |
| 100 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 2)); | 100 EXPECT_EQ(INT_MIN, saturatedSubtraction(INT_MIN + 1, 2)); |
| 101 | 101 |
| 102 EXPECT_EQ(0, saturatedSubtraction(INT_MIN, INT_MIN)); | 102 EXPECT_EQ(0, saturatedSubtraction(INT_MIN, INT_MIN)); |
| 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) | |
| 108 { | |
| 109 const int kFractionBits = 6; | |
| 110 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; | |
| 111 const int intMinForLayoutUnit = INT_MIN >> kFractionBits; | |
| 112 | |
| 113 EXPECT_EQ(0, saturatedSet(0, kFractionBits)); | |
| 114 | |
| 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)); | |
| 120 | |
| 121 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | |
| 122 saturatedSet(intMaxForLayoutUnit + 100, kFractionBits)); | |
| 123 | |
| 124 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | |
| 125 saturatedSet(intMaxForLayoutUnit - 100, kFractionBits)); | |
| 126 | |
| 127 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | |
| 128 saturatedSet(intMinForLayoutUnit, kFractionBits)); | |
| 129 | |
| 130 EXPECT_EQ(getMinSaturatedSetResultForTesting(kFractionBits), | |
| 131 saturatedSet(intMinForLayoutUnit - 100, kFractionBits)); | |
| 132 | |
| 133 EXPECT_EQ((intMinForLayoutUnit + 100) << kFractionBits, | |
| 134 saturatedSet(intMinForLayoutUnit + 100, kFractionBits)); | |
| 135 } | |
| 136 | |
| 137 TEST(SaturatedArithmeticTest, SetUnsigned) | |
| 138 { | |
| 139 const int kFractionBits = 6; | |
| 140 const int intMaxForLayoutUnit = INT_MAX >> kFractionBits; | |
| 141 | |
| 142 EXPECT_EQ(0, saturatedSet((unsigned)0, kFractionBits)); | |
| 143 | |
| 144 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | |
| 145 saturatedSet((unsigned)intMaxForLayoutUnit, kFractionBits)); | |
| 146 | |
| 147 EXPECT_EQ(getMaxSaturatedSetResultForTesting(kFractionBits), | |
| 148 saturatedSet((unsigned)(intMaxForLayoutUnit+100), kFractionBits)); | |
| 149 | |
| 150 EXPECT_EQ((intMaxForLayoutUnit - 100) << kFractionBits, | |
| 151 saturatedSet((unsigned)(intMaxForLayoutUnit - 100), kFractionBits)); | |
| 152 } | |
| 153 | |
| 154 | |
| 155 } // namespace | 107 } // namespace |
| OLD | NEW |