| Index: base/numeric_cast_unittest.cc
|
| ===================================================================
|
| --- base/numeric_cast_unittest.cc (revision 0)
|
| +++ base/numeric_cast_unittest.cc (revision 0)
|
| @@ -0,0 +1,66 @@
|
| +// 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 <gtest/gtest.h>
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/numeric_cast.h"
|
| +
|
| +TEST(NumericCast, Test) {
|
| + int small_positive = 1;
|
| + int small_negative = -1;
|
| + int large_positive = INT_MAX;
|
| + int large_negative = INT_MIN;
|
| +
|
| + // Narrow signed destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_positive));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed char>(small_negative));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<signed char>(large_negative));
|
| +
|
| + // Narrow unsigned destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<unsigned char>(small_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(small_negative));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned char>(large_negative));
|
| +
|
| + // Same width signed destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_positive));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed int>(small_negative));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_positive));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<signed int>(large_negative));
|
| +
|
| + // Same width unsigned destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(small_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(small_negative));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<unsigned int>(large_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned int>(large_negative));
|
| +
|
| + // Wider signed destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<long long>(small_positive));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<long long>(large_negative));
|
| +
|
| + // Wider unsigned destination.
|
| + EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(small_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(small_negative));
|
| + EXPECT_TRUE(base::IsNumericCastableTo<unsigned long long>(large_positive));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<unsigned long long>(large_negative));
|
| +
|
| + // Negative to size_t.
|
| + EXPECT_FALSE(base::IsNumericCastableTo<size_t>(small_negative));
|
| + EXPECT_FALSE(base::IsNumericCastableTo<size_t>(large_negative));
|
| +
|
| + // These shouldn't compile.
|
| + // EXPECT_TRUE(base::IsNumericCastableTo<float>(0.0));
|
| + // EXPECT_TRUE(base::IsNumericCastableTo<double>(0.0f));
|
| + // EXPECT_FALSE(base::IsNumericCastableTo<int>(DBL_MAX));
|
| +
|
| + // Confirm that numeric_cast<> actually compiles.
|
| + std::vector<int> v;
|
| + unsigned int checked_size = base::numeric_cast<unsigned int>(v.size());
|
| + EXPECT_EQ(0, checked_size);
|
| +}
|
|
|