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 <limits> |
| 6 #include <map> |
| 7 #include <sstream> |
| 8 #include <string> |
| 9 #include <type_traits> |
| 10 #include <unordered_map> |
| 11 |
| 12 #include "base/containers/hash_tables.h" |
| 13 #include "content/common/id_type.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 class Foo; |
| 21 using FooId = IdType<Foo, int, 0>; |
| 22 |
| 23 class Bar; |
| 24 using BarId = IdType<Bar, int, 0>; |
| 25 |
| 26 class AnotherIdMarker; |
| 27 class DerivedId : public IdType<AnotherIdMarker, int, 0> { |
| 28 public: |
| 29 explicit DerivedId(int unsafe_value) |
| 30 : IdType<AnotherIdMarker, int, 0>(unsafe_value) {} |
| 31 }; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 TEST(IdType, DefaultValueIsInvalid) { |
| 36 FooId foo_id; |
| 37 EXPECT_TRUE(foo_id.is_null()); |
| 38 EXPECT_FALSE(foo_id.is_valid()); |
| 39 } |
| 40 |
| 41 TEST(IdType, NormalValueIsValid) { |
| 42 FooId foo_id = FooId::FromUnsafeValue(123); |
| 43 EXPECT_FALSE(foo_id.is_null()); |
| 44 EXPECT_TRUE(foo_id.is_valid()); |
| 45 } |
| 46 |
| 47 TEST(IdType, OutputStreamTest) { |
| 48 FooId foo_id = FooId::FromUnsafeValue(123); |
| 49 |
| 50 std::ostringstream ss; |
| 51 ss << foo_id; |
| 52 EXPECT_EQ(std::string("123"), ss.str()); |
| 53 } |
| 54 |
| 55 TEST(IdType, IdType32) { |
| 56 IdType32<Foo> id; |
| 57 |
| 58 EXPECT_EQ(0u, id.GetUnsafeValue()); |
| 59 EXPECT_EQ(sizeof(uint32_t), sizeof(id)); |
| 60 } |
| 61 |
| 62 TEST(IdType, IdType64) { |
| 63 IdType64<Foo> id; |
| 64 |
| 65 EXPECT_EQ(0u, id.GetUnsafeValue()); |
| 66 EXPECT_EQ(sizeof(uint64_t), sizeof(id)); |
| 67 } |
| 68 |
| 69 TEST(IdType, DerivedClasses) { |
| 70 DerivedId derived_id(456); |
| 71 |
| 72 std::ostringstream ss; |
| 73 ss << derived_id; |
| 74 EXPECT_EQ(std::string("456"), ss.str()); |
| 75 |
| 76 std::map<DerivedId, std::string> ordered_map; |
| 77 ordered_map[derived_id] = "blah"; |
| 78 EXPECT_EQ(ordered_map[derived_id], std::string("blah")); |
| 79 |
| 80 // TODO(lukasza): Enable std::unordered_map and base::hash_map for DerivedId. |
| 81 // Ideally this should be possible without having to repeat std::hash<...> |
| 82 // specialization for each derived class (but then SFINAE + std::enable_if + |
| 83 // std::is_base_of doesn't seem to work for std::hash because std::hash only |
| 84 // has a single template parameter?). |
| 85 // std::unordered_map<DerivedId, std::string> unordered_map; |
| 86 // unordered_map[derived_id] = "blah2"; |
| 87 // EXPECT_EQ(unordered_map[derived_id], std::string("blah2")); |
| 88 } |
| 89 |
| 90 TEST(IdType, StaticAsserts) { |
| 91 static_assert(!std::is_constructible<FooId, int>::value, |
| 92 "Should be impossible to construct FooId from a raw integer."); |
| 93 static_assert(!std::is_convertible<int, FooId>::value, |
| 94 "Should be impossible to convert a raw integer into FooId."); |
| 95 |
| 96 static_assert(!std::is_constructible<FooId, BarId>::value, |
| 97 "Should be impossible to construct FooId from a BarId."); |
| 98 static_assert(!std::is_convertible<BarId, FooId>::value, |
| 99 "Should be impossible to convert a BarId into FooId."); |
| 100 |
| 101 // TODO(lukasza): Enable these once <type_traits> supports all the standard |
| 102 // C++11 equivalents (i.e. std::is_trivially_copyable instead of the |
| 103 // non-standard std::has_trivial_copy_assign). |
| 104 // static_assert(std::has_trivial_copy_constructor<FooId>::value, |
| 105 // "FooId should have a trivial copy constructor."); |
| 106 // static_assert(std::has_trivial_copy_assign<FooId>::value, |
| 107 // "FooId should have a trivial copy assignment operator."); |
| 108 // static_assert(std::has_trivial_destructor<FooId>::value, |
| 109 // "FooId should have a trivial destructor."); |
| 110 static_assert(std::is_standard_layout<FooId>::value, |
| 111 "FooId should have standard layout. " |
| 112 "See http://stackoverflow.com/a/7189821 for more info."); |
| 113 static_assert(sizeof(FooId) == sizeof(int), |
| 114 "FooId should be the same size as the raw integer it wraps."); |
| 115 } |
| 116 |
| 117 class IdTypeSpecificValueTest : public ::testing::TestWithParam<int> { |
| 118 protected: |
| 119 FooId test_id() { return FooId::FromUnsafeValue(GetParam()); } |
| 120 |
| 121 FooId other_id() { |
| 122 if (GetParam() != std::numeric_limits<int>::max()) |
| 123 return FooId::FromUnsafeValue(GetParam() + 1); |
| 124 else |
| 125 return FooId::FromUnsafeValue(std::numeric_limits<int>::min()); |
| 126 } |
| 127 }; |
| 128 |
| 129 TEST_P(IdTypeSpecificValueTest, ComparisonToSelf) { |
| 130 EXPECT_TRUE(test_id() == test_id()); |
| 131 EXPECT_FALSE(test_id() != test_id()); |
| 132 EXPECT_FALSE(test_id() < test_id()); |
| 133 } |
| 134 |
| 135 TEST_P(IdTypeSpecificValueTest, ComparisonToOther) { |
| 136 EXPECT_FALSE(test_id() == other_id()); |
| 137 EXPECT_TRUE(test_id() != other_id()); |
| 138 } |
| 139 |
| 140 TEST_P(IdTypeSpecificValueTest, UnsafeValueRoundtrips) { |
| 141 int original_value = GetParam(); |
| 142 FooId id = FooId::FromUnsafeValue(original_value); |
| 143 int final_value = id.GetUnsafeValue(); |
| 144 EXPECT_EQ(original_value, final_value); |
| 145 } |
| 146 |
| 147 TEST_P(IdTypeSpecificValueTest, Copying) { |
| 148 FooId original = test_id(); |
| 149 |
| 150 FooId copy_via_constructor(original); |
| 151 EXPECT_EQ(original, copy_via_constructor); |
| 152 |
| 153 FooId copy_via_assignment; |
| 154 copy_via_assignment = original; |
| 155 EXPECT_EQ(original, copy_via_assignment); |
| 156 } |
| 157 |
| 158 TEST_P(IdTypeSpecificValueTest, BaseHashmap) { |
| 159 base::hash_map<FooId, std::string> map; |
| 160 |
| 161 map[test_id()] = "test_id"; |
| 162 map[other_id()] = "other_id"; |
| 163 |
| 164 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 165 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 166 } |
| 167 |
| 168 TEST_P(IdTypeSpecificValueTest, StdMap) { |
| 169 std::map<FooId, std::string> map; |
| 170 |
| 171 map[test_id()] = "test_id"; |
| 172 map[other_id()] = "other_id"; |
| 173 |
| 174 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 175 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 176 } |
| 177 |
| 178 TEST_P(IdTypeSpecificValueTest, StdUnorderedMap) { |
| 179 std::unordered_map<FooId, std::string> map; |
| 180 |
| 181 map[test_id()] = "test_id"; |
| 182 map[other_id()] = "other_id"; |
| 183 |
| 184 EXPECT_EQ(map[test_id()], std::string("test_id")); |
| 185 EXPECT_EQ(map[other_id()], std::string("other_id")); |
| 186 } |
| 187 |
| 188 INSTANTIATE_TEST_CASE_P(, |
| 189 IdTypeSpecificValueTest, |
| 190 ::testing::Values(std::numeric_limits<int>::min(), |
| 191 -1, |
| 192 0, |
| 193 1, |
| 194 123, |
| 195 std::numeric_limits<int>::max())); |
| 196 |
| 197 } // namespace content |
OLD | NEW |