Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2018 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "absl/container/internal/compressed_tuple.h" |
| 16 | |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "gmock/gmock.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | #include "absl/container/internal/test_instance_tracker.h" |
| 23 | #include "absl/memory/memory.h" |
| 24 | #include "absl/types/any.h" |
| 25 | #include "absl/types/optional.h" |
| 26 | #include "absl/utility/utility.h" |
| 27 | |
| 28 | // These are declared at global scope purely so that error messages |
| 29 | // are smaller and easier to understand. |
| 30 | enum class CallType { kConstRef, kConstMove }; |
| 31 | |
| 32 | template <int> |
| 33 | struct Empty { |
| 34 | constexpr CallType value() const& { return CallType::kConstRef; } |
| 35 | constexpr CallType value() const&& { return CallType::kConstMove; } |
| 36 | }; |
| 37 | |
| 38 | template <typename T> |
| 39 | struct NotEmpty { |
| 40 | T value; |
| 41 | }; |
| 42 | |
| 43 | template <typename T, typename U> |
| 44 | struct TwoValues { |
| 45 | T value1; |
| 46 | U value2; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | namespace absl { |
| 51 | namespace container_internal { |
| 52 | namespace { |
| 53 | |
| 54 | using absl::test_internal::CopyableMovableInstance; |
| 55 | using absl::test_internal::InstanceTracker; |
| 56 | |
| 57 | TEST(CompressedTupleTest, Sizeof) { |
| 58 | EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int>)); |
| 59 | EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>>)); |
| 60 | EXPECT_EQ(sizeof(int), sizeof(CompressedTuple<int, Empty<0>, Empty<1>>)); |
| 61 | EXPECT_EQ(sizeof(int), |
| 62 | sizeof(CompressedTuple<int, Empty<0>, Empty<1>, Empty<2>>)); |
| 63 | |
| 64 | EXPECT_EQ(sizeof(TwoValues<int, double>), |
| 65 | sizeof(CompressedTuple<int, NotEmpty<double>>)); |
| 66 | EXPECT_EQ(sizeof(TwoValues<int, double>), |
| 67 | sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>>)); |
| 68 | EXPECT_EQ(sizeof(TwoValues<int, double>), |
| 69 | sizeof(CompressedTuple<int, Empty<0>, NotEmpty<double>, Empty<1>>)); |
| 70 | } |
| 71 | |
| 72 | TEST(CompressedTupleTest, OneMoveOnRValueConstructionTemp) { |
| 73 | InstanceTracker tracker; |
| 74 | CompressedTuple<CopyableMovableInstance> x1(CopyableMovableInstance(1)); |
| 75 | EXPECT_EQ(tracker.instances(), 1); |
| 76 | EXPECT_EQ(tracker.copies(), 0); |
| 77 | EXPECT_LE(tracker.moves(), 1); |
| 78 | EXPECT_EQ(x1.get<0>().value(), 1); |
| 79 | } |
| 80 | |
| 81 | TEST(CompressedTupleTest, OneMoveOnRValueConstructionMove) { |
| 82 | InstanceTracker tracker; |
| 83 | |
| 84 | CopyableMovableInstance i1(1); |
| 85 | CompressedTuple<CopyableMovableInstance> x1(std::move(i1)); |
| 86 | EXPECT_EQ(tracker.instances(), 2); |
| 87 | EXPECT_EQ(tracker.copies(), 0); |
| 88 | EXPECT_LE(tracker.moves(), 1); |
| 89 | EXPECT_EQ(x1.get<0>().value(), 1); |
| 90 | } |
| 91 | |
| 92 | TEST(CompressedTupleTest, OneMoveOnRValueConstructionMixedTypes) { |
| 93 | InstanceTracker tracker; |
| 94 | CopyableMovableInstance i1(1); |
| 95 | CopyableMovableInstance i2(2); |
| 96 | Empty<0> empty; |
| 97 | CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>> |
| 98 | x1(std::move(i1), i2, empty); |
| 99 | EXPECT_EQ(x1.get<0>().value(), 1); |
| 100 | EXPECT_EQ(x1.get<1>().value(), 2); |
| 101 | EXPECT_EQ(tracker.copies(), 0); |
| 102 | EXPECT_EQ(tracker.moves(), 1); |
| 103 | } |
| 104 | |
| 105 | struct IncompleteType; |
| 106 | CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>> |
| 107 | MakeWithIncomplete(CopyableMovableInstance i1, |
| 108 | IncompleteType& t, // NOLINT |
| 109 | Empty<0> empty) { |
| 110 | return CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>>{ |
| 111 | std::move(i1), t, empty}; |
| 112 | } |
| 113 | |
| 114 | struct IncompleteType {}; |
| 115 | TEST(CompressedTupleTest, OneMoveOnRValueConstructionWithIncompleteType) { |
| 116 | InstanceTracker tracker; |
| 117 | CopyableMovableInstance i1(1); |
| 118 | Empty<0> empty; |
| 119 | struct DerivedType : IncompleteType {int value = 0;}; |
| 120 | DerivedType fd; |
| 121 | fd.value = 7; |
| 122 | |
| 123 | CompressedTuple<CopyableMovableInstance, IncompleteType&, Empty<0>> x1 = |
| 124 | MakeWithIncomplete(std::move(i1), fd, empty); |
| 125 | |
| 126 | EXPECT_EQ(x1.get<0>().value(), 1); |
| 127 | EXPECT_EQ(static_cast<DerivedType&>(x1.get<1>()).value, 7); |
| 128 | |
| 129 | EXPECT_EQ(tracker.copies(), 0); |
| 130 | EXPECT_EQ(tracker.moves(), 2); |
| 131 | } |
| 132 | |
| 133 | TEST(CompressedTupleTest, |
| 134 | OneMoveOnRValueConstructionMixedTypes_BraceInitPoisonPillExpected) { |
| 135 | InstanceTracker tracker; |
| 136 | CopyableMovableInstance i1(1); |
| 137 | CopyableMovableInstance i2(2); |
| 138 | CompressedTuple<CopyableMovableInstance, CopyableMovableInstance&, Empty<0>> |
| 139 | x1(std::move(i1), i2, {}); // NOLINT |
| 140 | EXPECT_EQ(x1.get<0>().value(), 1); |
| 141 | EXPECT_EQ(x1.get<1>().value(), 2); |
| 142 | EXPECT_EQ(tracker.instances(), 3); |
| 143 | // We are forced into the `const Ts&...` constructor (invoking copies) |
| 144 | // because we need it to deduce the type of `{}`. |
| 145 | // std::tuple also has this behavior. |
| 146 | // Note, this test is proof that this is expected behavior, but it is not |
| 147 | // _desired_ behavior. |
| 148 | EXPECT_EQ(tracker.copies(), 1); |
| 149 | EXPECT_EQ(tracker.moves(), 0); |
| 150 | } |
| 151 | |
| 152 | TEST(CompressedTupleTest, OneCopyOnLValueConstruction) { |
| 153 | InstanceTracker tracker; |
| 154 | CopyableMovableInstance i1(1); |
| 155 | |
| 156 | CompressedTuple<CopyableMovableInstance> x1(i1); |
| 157 | EXPECT_EQ(tracker.copies(), 1); |
| 158 | EXPECT_EQ(tracker.moves(), 0); |
| 159 | |
| 160 | tracker.ResetCopiesMovesSwaps(); |
| 161 | |
| 162 | CopyableMovableInstance i2(2); |
| 163 | const CopyableMovableInstance& i2_ref = i2; |
| 164 | CompressedTuple<CopyableMovableInstance> x2(i2_ref); |
| 165 | EXPECT_EQ(tracker.copies(), 1); |
| 166 | EXPECT_EQ(tracker.moves(), 0); |
| 167 | } |
| 168 | |
| 169 | TEST(CompressedTupleTest, OneMoveOnRValueAccess) { |
| 170 | InstanceTracker tracker; |
| 171 | CopyableMovableInstance i1(1); |
| 172 | CompressedTuple<CopyableMovableInstance> x(std::move(i1)); |
| 173 | tracker.ResetCopiesMovesSwaps(); |
| 174 | |
| 175 | CopyableMovableInstance i2 = std::move(x).get<0>(); |
| 176 | EXPECT_EQ(tracker.copies(), 0); |
| 177 | EXPECT_EQ(tracker.moves(), 1); |
| 178 | } |
| 179 | |
| 180 | TEST(CompressedTupleTest, OneCopyOnLValueAccess) { |
| 181 | InstanceTracker tracker; |
| 182 | |
| 183 | CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0)); |
| 184 | EXPECT_EQ(tracker.copies(), 0); |
| 185 | EXPECT_EQ(tracker.moves(), 1); |
| 186 | |
| 187 | CopyableMovableInstance t = x.get<0>(); |
| 188 | EXPECT_EQ(tracker.copies(), 1); |
| 189 | EXPECT_EQ(tracker.moves(), 1); |
| 190 | } |
| 191 | |
| 192 | TEST(CompressedTupleTest, ZeroCopyOnRefAccess) { |
| 193 | InstanceTracker tracker; |
| 194 | |
| 195 | CompressedTuple<CopyableMovableInstance> x(CopyableMovableInstance(0)); |
| 196 | EXPECT_EQ(tracker.copies(), 0); |
| 197 | EXPECT_EQ(tracker.moves(), 1); |
| 198 | |
| 199 | CopyableMovableInstance& t1 = x.get<0>(); |
| 200 | const CopyableMovableInstance& t2 = x.get<0>(); |
| 201 | EXPECT_EQ(tracker.copies(), 0); |
| 202 | EXPECT_EQ(tracker.moves(), 1); |
| 203 | EXPECT_EQ(t1.value(), 0); |
| 204 | EXPECT_EQ(t2.value(), 0); |
| 205 | } |
| 206 | |
| 207 | TEST(CompressedTupleTest, Access) { |
| 208 | struct S { |
| 209 | std::string x; |
| 210 | }; |
| 211 | CompressedTuple<int, Empty<0>, S> x(7, {}, S{"ABC"}); |
| 212 | EXPECT_EQ(sizeof(x), sizeof(TwoValues<int, S>)); |
| 213 | EXPECT_EQ(7, x.get<0>()); |
| 214 | EXPECT_EQ("ABC", x.get<2>().x); |
| 215 | } |
| 216 | |
| 217 | TEST(CompressedTupleTest, NonClasses) { |
| 218 | CompressedTuple<int, const char*> x(7, "ABC"); |
| 219 | EXPECT_EQ(7, x.get<0>()); |
| 220 | EXPECT_STREQ("ABC", x.get<1>()); |
| 221 | } |
| 222 | |
| 223 | TEST(CompressedTupleTest, MixClassAndNonClass) { |
| 224 | CompressedTuple<int, const char*, Empty<0>, NotEmpty<double>> x(7, "ABC", {}, |
| 225 | {1.25}); |
| 226 | struct Mock { |
| 227 | int v; |
| 228 | const char* p; |
| 229 | double d; |
| 230 | }; |
| 231 | EXPECT_EQ(sizeof(x), sizeof(Mock)); |
| 232 | EXPECT_EQ(7, x.get<0>()); |
| 233 | EXPECT_STREQ("ABC", x.get<1>()); |
| 234 | EXPECT_EQ(1.25, x.get<3>().value); |
| 235 | } |
| 236 | |
| 237 | TEST(CompressedTupleTest, Nested) { |
| 238 | CompressedTuple<int, CompressedTuple<int>, |
| 239 | CompressedTuple<int, CompressedTuple<int>>> |
| 240 | x(1, CompressedTuple<int>(2), |
| 241 | CompressedTuple<int, CompressedTuple<int>>(3, CompressedTuple<int>(4))); |
| 242 | EXPECT_EQ(1, x.get<0>()); |
| 243 | EXPECT_EQ(2, x.get<1>().get<0>()); |
| 244 | EXPECT_EQ(3, x.get<2>().get<0>()); |
| 245 | EXPECT_EQ(4, x.get<2>().get<1>().get<0>()); |
| 246 | |
| 247 | CompressedTuple<Empty<0>, Empty<0>, |
| 248 | CompressedTuple<Empty<0>, CompressedTuple<Empty<0>>>> |
| 249 | y; |
| 250 | std::set<Empty<0>*> empties{&y.get<0>(), &y.get<1>(), &y.get<2>().get<0>(), |
| 251 | &y.get<2>().get<1>().get<0>()}; |
| 252 | #ifdef _MSC_VER |
| 253 | // MSVC has a bug where many instances of the same base class are layed out in |
| 254 | // the same address when using __declspec(empty_bases). |
| 255 | // This will be fixed in a future version of MSVC. |
| 256 | int expected = 1; |
| 257 | #else |
| 258 | int expected = 4; |
| 259 | #endif |
| 260 | EXPECT_EQ(expected, sizeof(y)); |
| 261 | EXPECT_EQ(expected, empties.size()); |
| 262 | EXPECT_EQ(sizeof(y), sizeof(Empty<0>) * empties.size()); |
| 263 | |
| 264 | EXPECT_EQ(4 * sizeof(char), |
| 265 | sizeof(CompressedTuple<CompressedTuple<char, char>, |
| 266 | CompressedTuple<char, char>>)); |
| 267 | EXPECT_TRUE((std::is_empty<CompressedTuple<Empty<0>, Empty<1>>>::value)); |
| 268 | |
| 269 | // Make sure everything still works when things are nested. |
| 270 | struct CT_Empty : CompressedTuple<Empty<0>> {}; |
| 271 | CompressedTuple<Empty<0>, CT_Empty> nested_empty; |
| 272 | auto contained = nested_empty.get<0>(); |
| 273 | auto nested = nested_empty.get<1>().get<0>(); |
| 274 | EXPECT_TRUE((std::is_same<decltype(contained), decltype(nested)>::value)); |
| 275 | } |
| 276 | |
| 277 | TEST(CompressedTupleTest, Reference) { |
| 278 | int i = 7; |
| 279 | std::string s = "Very long std::string that goes in the heap"; |
| 280 | CompressedTuple<int, int&, std::string, std::string&> x(i, i, s, s); |
| 281 | |
| 282 | // Sanity check. We should have not moved from `s` |
| 283 | EXPECT_EQ(s, "Very long std::string that goes in the heap"); |
| 284 | |
| 285 | EXPECT_EQ(x.get<0>(), x.get<1>()); |
| 286 | EXPECT_NE(&x.get<0>(), &x.get<1>()); |
| 287 | EXPECT_EQ(&x.get<1>(), &i); |
| 288 | |
| 289 | EXPECT_EQ(x.get<2>(), x.get<3>()); |
| 290 | EXPECT_NE(&x.get<2>(), &x.get<3>()); |
| 291 | EXPECT_EQ(&x.get<3>(), &s); |
| 292 | } |
| 293 | |
| 294 | TEST(CompressedTupleTest, NoElements) { |
| 295 | CompressedTuple<> x; |
| 296 | static_cast<void>(x); // Silence -Wunused-variable. |
| 297 | EXPECT_TRUE(std::is_empty<CompressedTuple<>>::value); |
| 298 | } |
| 299 | |
| 300 | TEST(CompressedTupleTest, MoveOnlyElements) { |
| 301 | CompressedTuple<std::unique_ptr<std::string>> str_tup( |
| 302 | absl::make_unique<std::string>("str")); |
| 303 | |
| 304 | CompressedTuple<CompressedTuple<std::unique_ptr<std::string>>, |
| 305 | std::unique_ptr<int>> |
| 306 | x(std::move(str_tup), absl::make_unique<int>(5)); |
| 307 | |
| 308 | EXPECT_EQ(*x.get<0>().get<0>(), "str"); |
| 309 | EXPECT_EQ(*x.get<1>(), 5); |
| 310 | |
| 311 | std::unique_ptr<std::string> x0 = std::move(x.get<0>()).get<0>(); |
| 312 | std::unique_ptr<int> x1 = std::move(x).get<1>(); |
| 313 | |
| 314 | EXPECT_EQ(*x0, "str"); |
| 315 | EXPECT_EQ(*x1, 5); |
| 316 | } |
| 317 | |
| 318 | TEST(CompressedTupleTest, MoveConstructionMoveOnlyElements) { |
| 319 | CompressedTuple<std::unique_ptr<std::string>> base( |
| 320 | absl::make_unique<std::string>("str")); |
| 321 | EXPECT_EQ(*base.get<0>(), "str"); |
| 322 | |
| 323 | CompressedTuple<std::unique_ptr<std::string>> copy(std::move(base)); |
| 324 | EXPECT_EQ(*copy.get<0>(), "str"); |
| 325 | } |
| 326 | |
| 327 | TEST(CompressedTupleTest, AnyElements) { |
| 328 | any a(std::string("str")); |
| 329 | CompressedTuple<any, any&> x(any(5), a); |
| 330 | EXPECT_EQ(absl::any_cast<int>(x.get<0>()), 5); |
| 331 | EXPECT_EQ(absl::any_cast<std::string>(x.get<1>()), "str"); |
| 332 | |
| 333 | a = 0.5f; |
| 334 | EXPECT_EQ(absl::any_cast<float>(x.get<1>()), 0.5); |
| 335 | |
| 336 | // Ensure copy construction work in the face of a type with a universal |
| 337 | // implicit constructor; |
| 338 | CompressedTuple<absl::any> c{}, d(c); // NOLINT |
| 339 | } |
| 340 | |
| 341 | TEST(CompressedTupleTest, Constexpr) { |
| 342 | struct NonTrivialStruct { |
| 343 | constexpr NonTrivialStruct() = default; |
| 344 | constexpr int value() const { return v; } |
| 345 | int v = 5; |
| 346 | }; |
| 347 | struct TrivialStruct { |
| 348 | TrivialStruct() = default; |
| 349 | constexpr int value() const { return v; } |
| 350 | int v; |
| 351 | }; |
| 352 | constexpr CompressedTuple<int, double, CompressedTuple<int>, Empty<0>> x( |
| 353 | 7, 1.25, CompressedTuple<int>(5), {}); |
| 354 | constexpr int x0 = x.get<0>(); |
| 355 | constexpr double x1 = x.get<1>(); |
| 356 | constexpr int x2 = x.get<2>().get<0>(); |
| 357 | constexpr CallType x3 = x.get<3>().value(); |
| 358 | |
| 359 | EXPECT_EQ(x0, 7); |
| 360 | EXPECT_EQ(x1, 1.25); |
| 361 | EXPECT_EQ(x2, 5); |
| 362 | EXPECT_EQ(x3, CallType::kConstRef); |
| 363 | |
| 364 | #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ > 4 |
| 365 | constexpr CompressedTuple<Empty<0>, TrivialStruct, int> trivial = {}; |
| 366 | constexpr CallType trivial0 = trivial.get<0>().value(); |
| 367 | constexpr int trivial1 = trivial.get<1>().value(); |
| 368 | constexpr int trivial2 = trivial.get<2>(); |
| 369 | |
| 370 | EXPECT_EQ(trivial0, CallType::kConstRef); |
| 371 | EXPECT_EQ(trivial1, 0); |
| 372 | EXPECT_EQ(trivial2, 0); |
| 373 | #endif |
| 374 | |
| 375 | constexpr CompressedTuple<Empty<0>, NonTrivialStruct, absl::optional<int>> |
| 376 | non_trivial = {}; |
| 377 | constexpr CallType non_trivial0 = non_trivial.get<0>().value(); |
| 378 | constexpr int non_trivial1 = non_trivial.get<1>().value(); |
| 379 | constexpr absl::optional<int> non_trivial2 = non_trivial.get<2>(); |
| 380 | |
| 381 | EXPECT_EQ(non_trivial0, CallType::kConstRef); |
| 382 | EXPECT_EQ(non_trivial1, 5); |
| 383 | EXPECT_EQ(non_trivial2, absl::nullopt); |
| 384 | |
| 385 | static constexpr char data[] = "DEF"; |
| 386 | constexpr CompressedTuple<const char*> z(data); |
| 387 | constexpr const char* z1 = z.get<0>(); |
| 388 | EXPECT_EQ(std::string(z1), std::string(data)); |
| 389 | |
| 390 | #if defined(__clang__) |
| 391 | // An apparent bug in earlier versions of gcc claims these are ambiguous. |
| 392 | constexpr int x2m = absl::move(x.get<2>()).get<0>(); |
| 393 | constexpr CallType x3m = absl::move(x).get<3>().value(); |
| 394 | EXPECT_EQ(x2m, 5); |
| 395 | EXPECT_EQ(x3m, CallType::kConstMove); |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | #if defined(__clang__) || defined(__GNUC__) |
| 400 | TEST(CompressedTupleTest, EmptyFinalClass) { |
| 401 | struct S final { |
| 402 | int f() const { return 5; } |
| 403 | }; |
| 404 | CompressedTuple<S> x; |
| 405 | EXPECT_EQ(x.get<0>().f(), 5); |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | } // namespace |
| 410 | } // namespace container_internal |
| 411 | } // namespace absl |