| Index: base/numerics/safe_numerics_unittest.cc
|
| ===================================================================
|
| --- base/numerics/safe_numerics_unittest.cc (revision 248433)
|
| +++ base/numerics/safe_numerics_unittest.cc (working copy)
|
| @@ -2,18 +2,15 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "base/numerics/safe_conversions.h"
|
| -
|
| #include <stdint.h>
|
|
|
| #include <limits>
|
|
|
| #include "base/compiler_specific.h"
|
| +#include "base/numerics/safe_conversions.h"
|
| +#include "base/numerics/safe_math.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| -namespace base {
|
| -namespace internal {
|
| -
|
| // Enumerates the five different conversions types we need to test.
|
| enum NumericConversionType {
|
| SIGN_PRESERVING_VALUE_PRESERVING,
|
| @@ -23,21 +20,36 @@
|
| UNSIGN_TO_SIGN_NARROW_OR_EQUAL,
|
| };
|
|
|
| +using std::numeric_limits;
|
| +using base::CheckedNumeric;
|
| +using base::saturated_cast;
|
| +using base::checked_cast;
|
| +using base::internal::MaxExponent;
|
| +using base::internal::TYPE_VALID;
|
| +using base::internal::TYPE_INVALID;
|
| +using base::internal::TYPE_OVERFLOW;
|
| +using base::internal::TYPE_UNDERFLOW;
|
| +
|
| // Template covering the different conversion tests.
|
| template <typename Dst, typename Src, NumericConversionType conversion>
|
| -struct TestNumericConversion {};
|
| +struct TestNumericOperations {};
|
|
|
| -// EXPECT_EQ wrapper providing specific detail on test failures.
|
| -#define TEST_EXPECTED_RANGE(expected, actual) \
|
| - EXPECT_EQ(expected, RangeCheck<Dst>(actual)) << \
|
| - "Conversion test: " << src << " value " << actual << \
|
| +// EXPECT_EQ wrappers providing specific detail on test failures.
|
| +#define TEST_EXPECTED_RANGE(expected, actual) \
|
| + EXPECT_EQ(expected, base::internal::RangeCheck<Dst>(actual)) << \
|
| + "Conversion test: " << src << " value " << actual << \
|
| " to " << dst << " on line " << line;
|
|
|
| +#define TEST_EXPECTED_RESULT(expected, actual) \
|
| + EXPECT_EQ(expected, actual) << \
|
| + "Result test: " << src << " value " << actual << \
|
| + " to " << dst << " on line " << line;
|
| +
|
| template <typename Dst, typename Src>
|
| -struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> {
|
| +struct TestNumericOperations<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> {
|
| static void Test(const char *dst, const char *src, int line) {
|
| - typedef std::numeric_limits<Src> SrcLimits;
|
| - typedef std::numeric_limits<Dst> DstLimits;
|
| + typedef numeric_limits<Src> SrcLimits;
|
| + typedef numeric_limits<Dst> DstLimits;
|
| // Integral to floating.
|
| COMPILE_ASSERT((DstLimits::is_iec559 && SrcLimits::is_integer) ||
|
| // Not floating to integral and...
|
| @@ -49,6 +61,18 @@
|
| (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))),
|
| comparison_must_be_sign_preserving_and_value_preserving);
|
|
|
| + CheckedNumeric<Dst> checked_result;
|
| + CheckedNumeric<Src> checked_src = SrcLimits::max();
|
| + checked_result = checked_src;
|
| + TEST_EXPECTED_RESULT(TYPE_VALID, checked_result.validity());
|
| + if (MaxExponent<Dst>::value > MaxExponent<Src>::value) {
|
| + checked_result = checked_result + 1;
|
| + TEST_EXPECTED_RESULT(TYPE_VALID, checked_result.validity());
|
| + } else {
|
| + checked_result = checked_result + 1;
|
| + TEST_EXPECTED_RESULT(TYPE_OVERFLOW, checked_result.validity());
|
| + }
|
| +
|
| TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max());
|
| TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1));
|
| if (SrcLimits::is_iec559) {
|
| @@ -56,7 +80,7 @@
|
| TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity());
|
| TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1);
|
| TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN());
|
| - } else if (std::numeric_limits<Src>::is_signed) {
|
| + } else if (numeric_limits<Src>::is_signed) {
|
| TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1));
|
| TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min());
|
| }
|
| @@ -64,10 +88,10 @@
|
| };
|
|
|
| template <typename Dst, typename Src>
|
| -struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> {
|
| +struct TestNumericOperations<Dst, Src, SIGN_PRESERVING_NARROW> {
|
| static void Test(const char *dst, const char *src, int line) {
|
| - typedef std::numeric_limits<Src> SrcLimits;
|
| - typedef std::numeric_limits<Dst> DstLimits;
|
| + typedef numeric_limits<Src> SrcLimits;
|
| + typedef numeric_limits<Dst> DstLimits;
|
| COMPILE_ASSERT(SrcLimits::is_signed == DstLimits::is_signed,
|
| destination_and_source_sign_must_be_the_same);
|
| COMPILE_ASSERT(sizeof(Dst) < sizeof(Src) ||
|
| @@ -92,10 +116,10 @@
|
| };
|
|
|
| template <typename Dst, typename Src>
|
| -struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> {
|
| +struct TestNumericOperations<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> {
|
| static void Test(const char *dst, const char *src, int line) {
|
| - typedef std::numeric_limits<Src> SrcLimits;
|
| - typedef std::numeric_limits<Dst> DstLimits;
|
| + typedef numeric_limits<Src> SrcLimits;
|
| + typedef numeric_limits<Dst> DstLimits;
|
| COMPILE_ASSERT(sizeof(Dst) >= sizeof(Src),
|
| destination_must_be_equal_or_wider_than_source);
|
| COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed);
|
| @@ -109,10 +133,10 @@
|
| };
|
|
|
| template <typename Dst, typename Src>
|
| -struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> {
|
| +struct TestNumericOperations<Dst, Src, SIGN_TO_UNSIGN_NARROW> {
|
| static void Test(const char *dst, const char *src, int line) {
|
| - typedef std::numeric_limits<Src> SrcLimits;
|
| - typedef std::numeric_limits<Dst> DstLimits;
|
| + typedef numeric_limits<Src> SrcLimits;
|
| + typedef numeric_limits<Dst> DstLimits;
|
| COMPILE_ASSERT((DstLimits::is_integer && SrcLimits::is_iec559) ||
|
| (sizeof(Dst) < sizeof(Src)),
|
| destination_must_be_narrower_than_source);
|
| @@ -134,10 +158,10 @@
|
| };
|
|
|
| template <typename Dst, typename Src>
|
| -struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> {
|
| +struct TestNumericOperations<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> {
|
| static void Test(const char *dst, const char *src, int line) {
|
| - typedef std::numeric_limits<Src> SrcLimits;
|
| - typedef std::numeric_limits<Dst> DstLimits;
|
| + typedef numeric_limits<Src> SrcLimits;
|
| + typedef numeric_limits<Dst> DstLimits;
|
| COMPILE_ASSERT(sizeof(Dst) <= sizeof(Src),
|
| destination_must_be_narrower_or_equal_to_source);
|
| COMPILE_ASSERT(!SrcLimits::is_signed, source_must_be_unsigned);
|
| @@ -150,98 +174,98 @@
|
| };
|
|
|
| // Helper macro to wrap displaying the conversion types and line numbers
|
| -#define TEST_NUMERIC_CONVERSION(d, s, t) \
|
| - TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__)
|
| +#define TEST_NUMERIC_OPERATIONS(d, s, t) \
|
| + TestNumericOperations<d, s, t>::Test(#d, #s, __LINE__)
|
|
|
| -TEST(SafeNumerics, IntMinConversions) {
|
| - TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| +TEST(SafeNumerics, IntMinOperations) {
|
| + TEST_NUMERIC_OPERATIONS(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
|
|
| - TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(int8_t, int, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, unsigned int, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(int8_t, float, SIGN_PRESERVING_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
|
|
| - TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW);
|
| - TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW);
|
| - TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, int, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uint8_t, float, SIGN_TO_UNSIGN_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| - TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| }
|
|
|
| -TEST(SafeNumerics, IntConversions) {
|
| - TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, unsigned int,
|
| +TEST(SafeNumerics, IntOperations) {
|
| + TEST_NUMERIC_OPERATIONS(int, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, unsigned int,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, uint8_t,
|
| + TEST_NUMERIC_OPERATIONS(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, uint8_t,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
|
|
| - TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(int, intmax_t, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(int, float, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(int, double, SIGN_PRESERVING_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
|
|
| - TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW);
|
| - TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, float, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(unsigned int, double, SIGN_TO_UNSIGN_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| - TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| }
|
|
|
| -TEST(SafeNumerics, IntMaxConversions) {
|
| - TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t,
|
| +TEST(SafeNumerics, IntMaxOperations) {
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, uintmax_t,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int,
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, unsigned int,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(intmax_t, unsigned int,
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, unsigned int,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
|
|
| - TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW);
|
| - TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, float, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, double, SIGN_PRESERVING_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
|
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW);
|
| - TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, float, SIGN_TO_UNSIGN_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(uintmax_t, double, SIGN_TO_UNSIGN_NARROW);
|
|
|
| - TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| }
|
|
|
| -TEST(SafeNumerics, FloatConversions) {
|
| - TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(float, uintmax_t,
|
| +TEST(SafeNumerics, FloatOperations) {
|
| + TEST_NUMERIC_OPERATIONS(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(float, uintmax_t,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(float, unsigned int,
|
| + TEST_NUMERIC_OPERATIONS(float, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(float, unsigned int,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
|
|
| - TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW);
|
| + TEST_NUMERIC_OPERATIONS(float, double, SIGN_PRESERVING_NARROW);
|
| }
|
|
|
| -TEST(SafeNumerics, DoubleConversions) {
|
| - TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(double, uintmax_t,
|
| +TEST(SafeNumerics, DoubleOperations) {
|
| + TEST_NUMERIC_OPERATIONS(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(double, uintmax_t,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| - TEST_NUMERIC_CONVERSION(double, unsigned int,
|
| + TEST_NUMERIC_OPERATIONS(double, int, SIGN_PRESERVING_VALUE_PRESERVING);
|
| + TEST_NUMERIC_OPERATIONS(double, unsigned int,
|
| SIGN_PRESERVING_VALUE_PRESERVING);
|
| }
|
|
|
| -TEST(SafeNumerics, SizeTConversions) {
|
| - TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| - TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| +TEST(SafeNumerics, SizeTOperations) {
|
| + TEST_NUMERIC_OPERATIONS(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
|
| + TEST_NUMERIC_OPERATIONS(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
|
| }
|
|
|
| TEST(SafeNumerics, CastTests) {
|
| @@ -254,11 +278,11 @@
|
| int small_positive = 1;
|
| int small_negative = -1;
|
| double double_small = 1.0;
|
| - double double_large = std::numeric_limits<double>::max();
|
| - double double_infinity = std::numeric_limits<float>::infinity();
|
| + double double_large = numeric_limits<double>::max();
|
| + double double_infinity = numeric_limits<float>::infinity();
|
|
|
| // Just test that the cast compiles, since the other tests cover logic.
|
| - EXPECT_EQ(0, base::checked_cast<int>(static_cast<size_t>(0)));
|
| + EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0)));
|
|
|
| // Test various saturation corner cases.
|
| EXPECT_EQ(saturated_cast<int>(small_negative),
|
| @@ -270,11 +294,49 @@
|
| EXPECT_EQ(saturated_cast<int>(double_small),
|
| static_cast<int>(double_small));
|
| EXPECT_EQ(saturated_cast<int>(double_large),
|
| - std::numeric_limits<int>::max());
|
| + numeric_limits<int>::max());
|
| EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
|
| EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);
|
| }
|
|
|
| -} // namespace internal
|
| -} // namespace base
|
| +TEST(SafeNumerics, MathTests) {
|
| + CheckedNumeric<int> checked_int = CheckedNumeric<size_t>(1);
|
| + CheckedNumeric<size_t> checked_sizet = 1;
|
|
|
| + checked_int = checked_int + checked_sizet;
|
| + EXPECT_EQ(2, checked_int.ValueUnsafe());
|
| +
|
| + checked_int = checked_int + numeric_limits<int>::max();
|
| + EXPECT_TRUE(checked_int.IsOverflow());
|
| +
|
| + checked_int = -1;
|
| + checked_int = numeric_limits<int>::min() + checked_int;
|
| + EXPECT_TRUE(checked_int.IsUnderflow());
|
| +
|
| + checked_int = -1;
|
| + checked_int -= numeric_limits<int>::max();
|
| + EXPECT_TRUE(checked_int.IsValid());
|
| +
|
| + checked_int = -2;
|
| + checked_int = checked_int - numeric_limits<int>::max();
|
| + EXPECT_TRUE(checked_int.IsUnderflow());
|
| +
|
| + checked_int = 1;
|
| + checked_int = checked_int - numeric_limits<int>::min();
|
| + EXPECT_TRUE(checked_int.IsOverflow());
|
| +
|
| + checked_int = 1;
|
| + checked_int = numeric_limits<int>::min() - checked_int;
|
| + EXPECT_TRUE(checked_int.IsUnderflow());
|
| +
|
| + checked_int = -1;
|
| + checked_int *= 2;
|
| + EXPECT_TRUE(checked_int.IsValid());
|
| +
|
| + checked_int /= 2;
|
| + EXPECT_TRUE(checked_int.IsValid());
|
| +
|
| + checked_int %= 2;
|
| + EXPECT_TRUE(checked_int.IsValid());
|
| +}
|
| +
|
|
|