OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <map> |
| 6 #include <sstream> |
| 7 #include <string> |
| 8 #include <type_traits> |
| 9 #include <unordered_map> |
| 10 |
| 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/id_type.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace base { |
| 16 |
| 17 namespace { |
| 18 |
| 19 class Foo; |
| 20 using FooId = base::IdType<Foo, int, 0>; |
| 21 |
| 22 class Bar; |
| 23 using BarId = base::IdType<Bar, int, 0>; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 TEST(IdType, DefaultValueIsInvalid) { |
| 28 FooId foo_id; |
| 29 EXPECT_TRUE(foo_id.is_null()); |
| 30 EXPECT_FALSE(foo_id.is_valid()); |
| 31 } |
| 32 |
| 33 TEST(IdType, NormalValueIsValid) { |
| 34 FooId foo_id = FooId::FromUnsafeValue(123); |
| 35 EXPECT_FALSE(foo_id.is_null()); |
| 36 EXPECT_TRUE(foo_id.is_valid()); |
| 37 } |
| 38 |
| 39 TEST(IdType, OutputStreamTest) { |
| 40 FooId foo_id = FooId::FromUnsafeValue(123); |
| 41 |
| 42 std::ostringstream ss; |
| 43 ss << foo_id; |
| 44 EXPECT_EQ(std::string("123"), ss.str()); |
| 45 } |
| 46 |
| 47 TEST(IdType, StaticAsserts) { |
| 48 static_assert(!std::is_constructible<FooId, int>::value, |
| 49 "Should be impossible to construct FooId from a raw integer."); |
| 50 static_assert(!std::is_convertible<int, FooId>::value, |
| 51 "Should be impossible to convert a raw integer into FooId."); |
| 52 |
| 53 static_assert(!std::is_constructible<FooId, BarId>::value, |
| 54 "Should be impossible to construct FooId from a BarId."); |
| 55 static_assert(!std::is_convertible<BarId, FooId>::value, |
| 56 "Should be impossible to convert a BarId into FooId."); |
| 57 |
| 58 // TODO(lukasza): Enable these once <type_traits> supports all the standard |
| 59 // C++11 equivalents (i.e. std::is_trivially_copyable instead of the |
| 60 // non-standard std::has_trivial_copy_assign). |
| 61 // static_assert(std::has_trivial_copy_constructor<FooId>::value, |
| 62 // "FooId should have a trivial copy constructor."); |
| 63 // static_assert(std::has_trivial_copy_assign<FooId>::value, |
| 64 // "FooId should have a trivial copy assignment operator."); |
| 65 // static_assert(std::has_trivial_destructor<FooId>::value, |
| 66 // "FooId should have a trivial destructor."); |
| 67 static_assert(std::is_standard_layout<FooId>::value, |
| 68 "FooId should have standard layout. " |
| 69 "See http://stackoverflow.com/a/7189821 for more info."); |
| 70 static_assert(sizeof(FooId) == sizeof(int), |
| 71 "FooId should be the same size as the raw integer it wraps."); |
| 72 } |
| 73 |
| 74 class IdTypeSpecificValueTest : public ::testing::TestWithParam<int> { |
| 75 protected: |
| 76 FooId test_id() { return FooId::FromUnsafeValue(GetParam()); } |
| 77 FooId other_id() { return FooId::FromUnsafeValue(GetParam() + 1); } |
| 78 }; |
| 79 |
| 80 TEST_P(IdTypeSpecificValueTest, ComparisonToSelf) { |
| 81 EXPECT_TRUE(test_id() == test_id()); |
| 82 EXPECT_FALSE(test_id() != test_id()); |
| 83 EXPECT_FALSE(test_id() < test_id()); |
| 84 } |
| 85 |
| 86 TEST_P(IdTypeSpecificValueTest, ComparisonToOther) { |
| 87 EXPECT_FALSE(test_id() == other_id()); |
| 88 EXPECT_TRUE(test_id() != other_id()); |
| 89 } |
| 90 |
| 91 TEST_P(IdTypeSpecificValueTest, UnsafeValueRoundtrips) { |
| 92 int original_value = GetParam(); |
| 93 FooId id = FooId::FromUnsafeValue(original_value); |
| 94 int final_value = id.GetUnsafeValue(); |
| 95 EXPECT_EQ(original_value, final_value); |
| 96 } |
| 97 |
| 98 TEST_P(IdTypeSpecificValueTest, Copying) { |
| 99 FooId original = test_id(); |
| 100 |
| 101 FooId copy_via_constructor(original); |
| 102 EXPECT_EQ(original, copy_via_constructor); |
| 103 |
| 104 FooId copy_via_assignment; |
| 105 copy_via_assignment = original; |
| 106 EXPECT_EQ(original, copy_via_assignment); |
| 107 } |
| 108 |
| 109 TEST_P(IdTypeSpecificValueTest, BaseHashmap) { |
| 110 base::hash_map<FooId, std::string> map; |
| 111 |
| 112 map[test_id()] = "test_id"; |
| 113 map[other_id()] = "other_id"; |
| 114 |
| 115 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 116 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 117 } |
| 118 |
| 119 TEST_P(IdTypeSpecificValueTest, StdMap) { |
| 120 std::map<FooId, std::string> map; |
| 121 |
| 122 map[test_id()] = "test_id"; |
| 123 map[other_id()] = "other_id"; |
| 124 |
| 125 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 126 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 127 } |
| 128 |
| 129 TEST_P(IdTypeSpecificValueTest, StdUnorderedMap) { |
| 130 std::unordered_map<FooId, std::string> map; |
| 131 |
| 132 map[test_id()] = "test_id"; |
| 133 map[other_id()] = "other_id"; |
| 134 |
| 135 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 136 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 137 } |
| 138 |
| 139 INSTANTIATE_TEST_CASE_P(, |
| 140 IdTypeSpecificValueTest, |
| 141 ::testing::Values(-1, 0, 1, 123)); |
| 142 |
| 143 } // namespace base |
OLD | NEW |