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