// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include #include #include #include "base/safe_numerics.h" namespace base { namespace internal { // This is far (far, far) too slow to run normally, but if you're refactoring // it might be useful. // #define RUN_EXHAUSTIVE_TEST #ifdef RUN_EXHAUSTIVE_TEST template void ExhaustiveCheckFromTo() { fprintf(stderr, "."); From i = std::numeric_limits::min(); for (;;) { std::ostringstream str_from, str_to; str_from << i; To to = static_cast(i); str_to << to; bool strings_equal = str_from.str() == str_to.str(); EXPECT_EQ(IsValidNumericCast(i), strings_equal); fprintf(stderr, "\r%s vs %s\x1B[K", str_from.str().c_str(), str_to.str().c_str()); ++i; // If we wrap, then we've tested everything. if (i == std::numeric_limits::min()) break; } } template void ExhaustiveCheckFrom() { ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); ExhaustiveCheckFromTo(); fprintf(stderr, "\n"); } #endif TEST(SafeNumerics, NumericCast) { int small_positive = 1; int small_negative = -1; int large_positive = INT_MAX; int large_negative = INT_MIN; size_t size_t_small = 1; size_t size_t_large = UINT_MAX; // Narrow signed destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_TRUE(IsValidNumericCast(small_negative)); EXPECT_FALSE(IsValidNumericCast(large_positive)); EXPECT_FALSE(IsValidNumericCast(large_negative)); EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_TRUE(IsValidNumericCast(small_negative)); // Narrow unsigned destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_FALSE(IsValidNumericCast(small_negative)); EXPECT_FALSE(IsValidNumericCast(large_positive)); EXPECT_FALSE(IsValidNumericCast(large_negative)); EXPECT_FALSE(IsValidNumericCast(small_negative)); EXPECT_FALSE(IsValidNumericCast(large_negative)); // Same width signed destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_TRUE(IsValidNumericCast(small_negative)); EXPECT_TRUE(IsValidNumericCast(large_positive)); EXPECT_TRUE(IsValidNumericCast(large_negative)); // Same width unsigned destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_FALSE(IsValidNumericCast(small_negative)); EXPECT_TRUE(IsValidNumericCast(large_positive)); EXPECT_FALSE(IsValidNumericCast(large_negative)); // Wider signed destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_TRUE(IsValidNumericCast(large_negative)); EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_TRUE(IsValidNumericCast(large_negative)); // Wider unsigned destination. EXPECT_TRUE(IsValidNumericCast(small_positive)); EXPECT_FALSE(IsValidNumericCast(small_negative)); EXPECT_TRUE(IsValidNumericCast(large_positive)); EXPECT_FALSE(IsValidNumericCast(large_negative)); // Negative to size_t. EXPECT_FALSE(IsValidNumericCast(small_negative)); EXPECT_FALSE(IsValidNumericCast(large_negative)); // From unsigned. // Small. EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); EXPECT_TRUE(IsValidNumericCast(size_t_small)); // Large. EXPECT_FALSE(IsValidNumericCast(size_t_large)); EXPECT_FALSE(IsValidNumericCast(size_t_large)); EXPECT_FALSE(IsValidNumericCast(size_t_large)); EXPECT_FALSE(IsValidNumericCast(size_t_large)); EXPECT_FALSE(IsValidNumericCast(size_t_large)); EXPECT_TRUE(IsValidNumericCast(size_t_large)); EXPECT_TRUE(IsValidNumericCast(size_t_large)); EXPECT_TRUE(IsValidNumericCast(size_t_large)); // Various edge cases. EXPECT_TRUE(IsValidNumericCast(static_cast(SHRT_MIN))); EXPECT_FALSE( IsValidNumericCast(static_cast(SHRT_MIN))); EXPECT_FALSE(IsValidNumericCast(SHRT_MIN)); // Confirm that checked_numeric_cast<> actually compiles. std::vector v; unsigned int checked_size = base::checked_numeric_cast(v.size()); EXPECT_EQ(0u, checked_size); #ifdef RUN_EXHAUSTIVE_TEST ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); ExhaustiveCheckFrom(); #endif } } // namespace internal } // namespace base