Bug 768538 - 2/2 - Fix undefined behavior in the CheckedInt unit test - r=jwalden

This commit is contained in:
Benoit Jacob 2012-07-05 10:13:23 -04:00
parent 457f2bf3b9
commit 184c216c48

View File

@ -99,17 +99,19 @@ void test()
const CheckedInt<T> max(detail::MaxValue<T>::value); const CheckedInt<T> max(detail::MaxValue<T>::value);
const CheckedInt<T> min(detail::MinValue<T>::value); const CheckedInt<T> min(detail::MinValue<T>::value);
// Check min() and max(), since they are custom implementations and a mistake there // Check MinValue and MaxValue, since they are custom implementations and a mistake there
// could potentially NOT be caught by any other tests... while making everything wrong! // could potentially NOT be caught by any other tests... while making everything wrong!
T bit = 1; unsignedT bit = 1;
unsignedT unsignedMinValue(min.value());
unsignedT unsignedMaxValue(max.value());
for (size_t i = 0; i < sizeof(T) * CHAR_BIT - 1; i++) for (size_t i = 0; i < sizeof(T) * CHAR_BIT - 1; i++)
{ {
VERIFY((min.value() & bit) == 0); VERIFY((unsignedMinValue & bit) == 0);
bit <<= 1; bit <<= 1;
} }
VERIFY((min.value() & bit) == (isTSigned ? bit : T(0))); VERIFY((unsignedMinValue & bit) == (isTSigned ? bit : unsignedT(0)));
VERIFY(max.value() == T(~(min.value()))); VERIFY(unsignedMaxValue == unsignedT(~unsignedMinValue));
const CheckedInt<T> zero(0); const CheckedInt<T> zero(0);
const CheckedInt<T> one(1); const CheckedInt<T> one(1);
@ -117,7 +119,7 @@ void test()
const CheckedInt<T> three(3); const CheckedInt<T> three(3);
const CheckedInt<T> four(4); const CheckedInt<T> four(4);
/* Addition / substraction checks */ /* Addition / subtraction checks */
VERIFY_IS_VALID(zero + zero); VERIFY_IS_VALID(zero + zero);
VERIFY(zero + zero == zero); VERIFY(zero + zero == zero);