#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include <stdint.h>
#include <limits>
#include <type_traits>
#include "base/compiler_specific.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_WIN)
#pragma warning(disable : 4756)
#pragma warning(disable : 4293)
#endif
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winteger-overflow"
#endif
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/numerics/safe_math.h"
#include "base/numerics/wrapping_math.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
#include <mmintrin.h>
#endif
namespace base {
namespace internal {
numeric_limits;
template <typename Dst, typename Src>
Dst GetMaxConvertibleToFloat() { … }
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
constexpr int32_t kIntOne = …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
constexpr float kFloatOne = …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
static_assert …;
template <typename U>
U GetNumericValueForTest(const CheckedNumeric<U>& src) { … }
template <typename U>
U GetNumericValueForTest(const ClampedNumeric<U>& src) { … }
template <typename U>
U GetNumericValueForTest(const U& src) { … }
struct LogOnFailure { … };
template <typename T>
constexpr T GetValue(const T& src) { … }
template <typename T, typename U>
constexpr T GetValueAsDest(const U& src) { … }
template <typename T>
constexpr T GetValue(const CheckedNumeric<T>& src) { … }
template <typename T, typename U>
constexpr T GetValueAsDest(const CheckedNumeric<U>& src) { … }
template <typename T>
constexpr T GetValue(const ClampedNumeric<T>& src) { … }
template <typename T, typename U>
constexpr T GetValueAsDest(const ClampedNumeric<U>& src) { … }
#define TEST_EXPECTED_VALIDITY(expected, actual) …
#define TEST_EXPECTED_SUCCESS(actual) …
#define TEST_EXPECTED_FAILURE(actual) …
#define TEST_EXPECTED_VALUE(expected, actual) …
template <typename Dst>
void TestStrictPointerMath() { … }
template <typename Dst>
static void TestSpecializedArithmetic(
const char* dst,
int line,
std::enable_if_t<numeric_limits<Dst>::is_integer &&
numeric_limits<Dst>::is_signed,
int> = 0) { … }
template <typename Dst>
static void TestSpecializedArithmetic(
const char* dst,
int line,
std::enable_if_t<numeric_limits<Dst>::is_integer &&
!numeric_limits<Dst>::is_signed,
int> = 0) { … }
template <typename Dst>
void TestSpecializedArithmetic(
const char* dst,
int line,
std::enable_if_t<numeric_limits<Dst>::is_iec559, int> = 0) { … }
template <typename Dst>
static void TestArithmetic(const char* dst, int line) { … }
#define TEST_ARITHMETIC(Dst) …
TEST(SafeNumerics, SignedIntegerMath) { … }
TEST(SafeNumerics, UnsignedIntegerMath) { … }
TEST(SafeNumerics, FloatingPointMath) { … }
enum NumericConversionType { … };
template <typename Dst, typename Src, NumericConversionType conversion>
struct TestNumericConversion { … };
enum RangeConstraint { … };
constexpr RangeConstraint RangeCheckToEnum(const RangeCheck constraint) { … }
#define TEST_EXPECTED_RANGE(expected, actual) …
template <typename Dst, typename Src>
void TestStrictComparison(const char* dst, const char* src, int line) { … }
TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING>;
TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW>;
TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL>;
TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW>;
TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL>;
#define TEST_NUMERIC_CONVERSION(d, s, t) …
TEST(SafeNumerics, IntMinOperations) { … }
TEST(SafeNumerics, Int16Operations) { … }
TEST(SafeNumerics, IntOperations) { … }
TEST(SafeNumerics, IntMaxOperations) { … }
TEST(SafeNumerics, FloatOperations) { … }
TEST(SafeNumerics, DoubleOperations) { … }
TEST(SafeNumerics, SizeTOperations) { … }
int TestOverload(int) { … }
uint8_t TestOverload(uint8_t) { … }
size_t TestOverload(size_t) { … }
static_assert …;
static_assert …;
template <typename T>
struct CastTest1 { … };
template <typename T>
struct CastTest2 { … };
TEST(SafeNumerics, CastTests) { … }
TEST(SafeNumerics, IsValueInRangeForNumericType) { … }
TEST(SafeNumerics, CompoundNumericOperations) { … }
TEST(SafeNumerics, TemplatedSafeMath) { … }
TEST(SafeNumerics, VariadicNumericOperations) { … }
TEST(SafeNumerics, CeilInt) { … }
TEST(SafeNumerics, FloorInt) { … }
TEST(SafeNumerics, RoundInt) { … }
TEST(SafeNumerics, Int64) { … }
template <typename T>
void TestWrappingMathSigned() { … }
template <typename T>
void TestWrappingMathUnsigned() { … }
TEST(SafeNumerics, WrappingMath) { … }
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
}
}