| Index: base/id_type_unittest.cc
|
| diff --git a/base/id_type_unittest.cc b/base/id_type_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4403cd43f7d1c87e63544849ae77e1db1c4a3549
|
| --- /dev/null
|
| +++ b/base/id_type_unittest.cc
|
| @@ -0,0 +1,143 @@
|
| +// Copyright 2015 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 <map>
|
| +#include <sstream>
|
| +#include <string>
|
| +#include <type_traits>
|
| +#include <unordered_map>
|
| +
|
| +#include "base/containers/hash_tables.h"
|
| +#include "base/id_type.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace base {
|
| +
|
| +namespace {
|
| +
|
| +class Foo;
|
| +using FooId = base::IdType<Foo, int, 0>;
|
| +
|
| +class Bar;
|
| +using BarId = base::IdType<Bar, int, 0>;
|
| +
|
| +} // namespace
|
| +
|
| +TEST(IdType, DefaultValueIsInvalid) {
|
| + FooId foo_id;
|
| + EXPECT_TRUE(foo_id.is_null());
|
| + EXPECT_FALSE(foo_id.is_valid());
|
| +}
|
| +
|
| +TEST(IdType, NormalValueIsValid) {
|
| + FooId foo_id = FooId::FromUnsafeValue(123);
|
| + EXPECT_FALSE(foo_id.is_null());
|
| + EXPECT_TRUE(foo_id.is_valid());
|
| +}
|
| +
|
| +TEST(IdType, OutputStreamTest) {
|
| + FooId foo_id = FooId::FromUnsafeValue(123);
|
| +
|
| + std::ostringstream ss;
|
| + ss << foo_id;
|
| + EXPECT_EQ(std::string("123"), ss.str());
|
| +}
|
| +
|
| +TEST(IdType, StaticAsserts) {
|
| + static_assert(!std::is_constructible<FooId, int>::value,
|
| + "Should be impossible to construct FooId from a raw integer.");
|
| + static_assert(!std::is_convertible<int, FooId>::value,
|
| + "Should be impossible to convert a raw integer into FooId.");
|
| +
|
| + static_assert(!std::is_constructible<FooId, BarId>::value,
|
| + "Should be impossible to construct FooId from a BarId.");
|
| + static_assert(!std::is_convertible<BarId, FooId>::value,
|
| + "Should be impossible to convert a BarId into FooId.");
|
| +
|
| + // TODO(lukasza): Enable these once <type_traits> supports all the standard
|
| + // C++11 equivalents (i.e. std::is_trivially_copyable instead of the
|
| + // non-standard std::has_trivial_copy_assign).
|
| + // static_assert(std::has_trivial_copy_constructor<FooId>::value,
|
| + // "FooId should have a trivial copy constructor.");
|
| + // static_assert(std::has_trivial_copy_assign<FooId>::value,
|
| + // "FooId should have a trivial copy assignment operator.");
|
| + // static_assert(std::has_trivial_destructor<FooId>::value,
|
| + // "FooId should have a trivial destructor.");
|
| + static_assert(std::is_standard_layout<FooId>::value,
|
| + "FooId should have standard layout. "
|
| + "See http://stackoverflow.com/a/7189821 for more info.");
|
| + static_assert(sizeof(FooId) == sizeof(int),
|
| + "FooId should be the same size as the raw integer it wraps.");
|
| +}
|
| +
|
| +class IdTypeSpecificValueTest : public ::testing::TestWithParam<int> {
|
| + protected:
|
| + FooId test_id() { return FooId::FromUnsafeValue(GetParam()); }
|
| + FooId other_id() { return FooId::FromUnsafeValue(GetParam() + 1); }
|
| +};
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, ComparisonToSelf) {
|
| + EXPECT_TRUE(test_id() == test_id());
|
| + EXPECT_FALSE(test_id() != test_id());
|
| + EXPECT_FALSE(test_id() < test_id());
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, ComparisonToOther) {
|
| + EXPECT_FALSE(test_id() == other_id());
|
| + EXPECT_TRUE(test_id() != other_id());
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, UnsafeValueRoundtrips) {
|
| + int original_value = GetParam();
|
| + FooId id = FooId::FromUnsafeValue(original_value);
|
| + int final_value = id.GetUnsafeValue();
|
| + EXPECT_EQ(original_value, final_value);
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, Copying) {
|
| + FooId original = test_id();
|
| +
|
| + FooId copy_via_constructor(original);
|
| + EXPECT_EQ(original, copy_via_constructor);
|
| +
|
| + FooId copy_via_assignment;
|
| + copy_via_assignment = original;
|
| + EXPECT_EQ(original, copy_via_assignment);
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, BaseHashmap) {
|
| + base::hash_map<FooId, std::string> map;
|
| +
|
| + map[test_id()] = "test_id";
|
| + map[other_id()] = "other_id";
|
| +
|
| + EXPECT_EQ(map[test_id()], std::string("test_id"));
|
| + EXPECT_EQ(map[other_id()], std::string("other_id"));
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, StdMap) {
|
| + std::map<FooId, std::string> map;
|
| +
|
| + map[test_id()] = "test_id";
|
| + map[other_id()] = "other_id";
|
| +
|
| + EXPECT_EQ(map[test_id()], std::string("test_id"));
|
| + EXPECT_EQ(map[other_id()], std::string("other_id"));
|
| +}
|
| +
|
| +TEST_P(IdTypeSpecificValueTest, StdUnorderedMap) {
|
| + std::unordered_map<FooId, std::string> map;
|
| +
|
| + map[test_id()] = "test_id";
|
| + map[other_id()] = "other_id";
|
| +
|
| + EXPECT_EQ(map[test_id()], std::string("test_id"));
|
| + EXPECT_EQ(map[other_id()], std::string("other_id"));
|
| +}
|
| +
|
| +INSTANTIATE_TEST_CASE_P(,
|
| + IdTypeSpecificValueTest,
|
| + ::testing::Values(-1, 0, 1, 123));
|
| +
|
| +} // namespace base
|
|
|