Corey Tabaka | 7190e8a | 2017-06-22 20:39:42 -0700 | [diff] [blame] | 1 | #include <array> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 2 | #include <cstdint> |
| 3 | #include <functional> |
| 4 | #include <memory> |
| 5 | #include <string> |
| 6 | #include <type_traits> |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | #include <pdx/rpc/variant.h> |
| 10 | |
| 11 | using namespace android::pdx; |
| 12 | using namespace android::pdx::rpc; |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | struct BaseType { |
| 17 | BaseType(int value) : value(value) {} |
| 18 | int value; |
| 19 | }; |
| 20 | |
| 21 | struct DerivedType : BaseType { |
| 22 | DerivedType(int value) : BaseType{value} {}; |
| 23 | }; |
| 24 | |
| 25 | template <typename T> |
| 26 | class TestType { |
| 27 | public: |
| 28 | TestType(const T& value) : value_(value) {} |
| 29 | TestType(T&& value) : value_(std::move(value)) {} |
| 30 | TestType(const TestType&) = default; |
| 31 | TestType(TestType&&) = default; |
| 32 | |
| 33 | TestType& operator=(const TestType&) = default; |
| 34 | TestType& operator=(TestType&&) = default; |
| 35 | |
| 36 | const T& get() const { return value_; } |
| 37 | T&& take() { return std::move(value_); } |
| 38 | |
| 39 | private: |
| 40 | T value_; |
| 41 | }; |
| 42 | |
| 43 | template <typename T> |
| 44 | class InstrumentType { |
| 45 | public: |
| 46 | InstrumentType(const T& value) : value_(value) { constructor_count_++; } |
| 47 | InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; } |
| 48 | InstrumentType(const InstrumentType& other) : value_(other.value_) { |
| 49 | constructor_count_++; |
| 50 | } |
| 51 | InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) { |
| 52 | constructor_count_++; |
| 53 | } |
| 54 | InstrumentType(const TestType<T>& other) : value_(other.get()) { |
| 55 | constructor_count_++; |
| 56 | } |
| 57 | InstrumentType(TestType<T>&& other) : value_(other.take()) { |
| 58 | constructor_count_++; |
| 59 | } |
| 60 | ~InstrumentType() { destructor_count_++; } |
| 61 | |
| 62 | InstrumentType& operator=(const InstrumentType& other) { |
| 63 | copy_assignment_count_++; |
| 64 | value_ = other.value_; |
| 65 | return *this; |
| 66 | } |
| 67 | InstrumentType& operator=(InstrumentType&& other) { |
| 68 | move_assignment_count_++; |
| 69 | value_ = std::move(other.value_); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | InstrumentType& operator=(const TestType<T>& other) { |
| 74 | copy_assignment_count_++; |
| 75 | value_ = other.get(); |
| 76 | return *this; |
| 77 | } |
| 78 | InstrumentType& operator=(TestType<T>&& other) { |
| 79 | move_assignment_count_++; |
| 80 | value_ = other.take(); |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | static std::size_t constructor_count() { return constructor_count_; } |
| 85 | static std::size_t destructor_count() { return destructor_count_; } |
| 86 | static std::size_t move_assignment_count() { return move_assignment_count_; } |
| 87 | static std::size_t copy_assignment_count() { return copy_assignment_count_; } |
| 88 | |
| 89 | const T& get() const { return value_; } |
| 90 | T&& take() { return std::move(value_); } |
| 91 | |
| 92 | static void clear() { |
| 93 | constructor_count_ = 0; |
| 94 | destructor_count_ = 0; |
| 95 | move_assignment_count_ = 0; |
| 96 | copy_assignment_count_ = 0; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | T value_; |
| 101 | |
| 102 | static std::size_t constructor_count_; |
| 103 | static std::size_t destructor_count_; |
| 104 | static std::size_t move_assignment_count_; |
| 105 | static std::size_t copy_assignment_count_; |
| 106 | }; |
| 107 | |
| 108 | template <typename T> |
| 109 | std::size_t InstrumentType<T>::constructor_count_ = 0; |
| 110 | template <typename T> |
| 111 | std::size_t InstrumentType<T>::destructor_count_ = 0; |
| 112 | template <typename T> |
| 113 | std::size_t InstrumentType<T>::move_assignment_count_ = 0; |
| 114 | template <typename T> |
| 115 | std::size_t InstrumentType<T>::copy_assignment_count_ = 0; |
| 116 | |
| 117 | } // anonymous namespace |
| 118 | |
| 119 | TEST(Variant, Assignment) { |
| 120 | // Assert basic type properties. |
| 121 | { |
| 122 | Variant<int, bool, float> v; |
| 123 | ASSERT_EQ(-1, v.index()); |
| 124 | ASSERT_FALSE(v.is<int>()); |
| 125 | ASSERT_FALSE(v.is<bool>()); |
| 126 | ASSERT_FALSE(v.is<float>()); |
| 127 | } |
| 128 | |
| 129 | { |
| 130 | Variant<int, bool, float> v; |
| 131 | v = 10; |
| 132 | ASSERT_EQ(0, v.index()); |
| 133 | ASSERT_TRUE(v.is<int>()); |
| 134 | ASSERT_FALSE(v.is<bool>()); |
| 135 | ASSERT_FALSE(v.is<float>()); |
| 136 | EXPECT_EQ(10, std::get<int>(v)); |
| 137 | } |
| 138 | |
| 139 | { |
| 140 | Variant<int, bool, float> v; |
| 141 | v = false; |
| 142 | ASSERT_EQ(1, v.index()); |
| 143 | ASSERT_FALSE(v.is<int>()); |
| 144 | ASSERT_TRUE(v.is<bool>()); |
| 145 | ASSERT_FALSE(v.is<float>()); |
| 146 | EXPECT_EQ(false, std::get<bool>(v)); |
| 147 | } |
| 148 | |
| 149 | { |
| 150 | Variant<int, bool, float> v; |
| 151 | v = 1.0f; |
| 152 | ASSERT_EQ(2, v.index()); |
| 153 | ASSERT_FALSE(v.is<int>()); |
| 154 | ASSERT_FALSE(v.is<bool>()); |
| 155 | ASSERT_TRUE(v.is<float>()); |
| 156 | EXPECT_FLOAT_EQ(1.0f, std::get<float>(v)); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | Variant<int, bool, float> v; |
| 161 | // ERROR: More than one type is implicitly convertible from double. |
| 162 | // v = 1.0; |
| 163 | v = static_cast<float>(1.0); |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | Variant<int, bool, float> v; |
| 168 | |
| 169 | double x = 1.1; |
| 170 | v = static_cast<float>(x); |
| 171 | ASSERT_EQ(2, v.index()); |
| 172 | ASSERT_FALSE(v.is<int>()); |
| 173 | ASSERT_FALSE(v.is<bool>()); |
| 174 | ASSERT_TRUE(v.is<float>()); |
| 175 | EXPECT_FLOAT_EQ(1.1, std::get<float>(v)); |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | Variant<int, std::string> v; |
| 180 | ASSERT_EQ(-1, v.index()); |
| 181 | ASSERT_FALSE(v.is<int>()); |
| 182 | ASSERT_FALSE(v.is<std::string>()); |
| 183 | } |
| 184 | |
| 185 | { |
| 186 | Variant<int, std::string> v; |
| 187 | v = 20; |
| 188 | ASSERT_EQ(0, v.index()); |
| 189 | ASSERT_TRUE(v.is<int>()); |
| 190 | ASSERT_FALSE(v.is<std::string>()); |
| 191 | EXPECT_EQ(20, std::get<int>(v)); |
| 192 | } |
| 193 | |
| 194 | { |
| 195 | Variant<int, std::string> v; |
| 196 | v = std::string("test"); |
| 197 | ASSERT_EQ(1, v.index()); |
| 198 | ASSERT_FALSE(v.is<int>()); |
| 199 | ASSERT_TRUE(v.is<std::string>()); |
| 200 | EXPECT_EQ("test", std::get<std::string>(v)); |
| 201 | } |
| 202 | |
| 203 | { |
| 204 | Variant<int, std::string> v; |
| 205 | v = "test"; |
| 206 | ASSERT_EQ(1, v.index()); |
| 207 | ASSERT_FALSE(v.is<int>()); |
| 208 | ASSERT_TRUE(v.is<std::string>()); |
| 209 | EXPECT_EQ("test", std::get<std::string>(v)); |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | Variant<const char*> v1; |
| 214 | Variant<std::string> v2; |
| 215 | |
| 216 | v1 = "test"; |
| 217 | ASSERT_TRUE(v1.is<const char*>()); |
| 218 | v2 = v1; |
| 219 | ASSERT_TRUE(v2.is<std::string>()); |
| 220 | EXPECT_EQ("test", std::get<std::string>(v2)); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | Variant<int> a(1); |
| 225 | Variant<int> b; |
| 226 | ASSERT_TRUE(!a.empty()); |
| 227 | ASSERT_TRUE(b.empty()); |
| 228 | |
| 229 | a = b; |
| 230 | ASSERT_TRUE(a.empty()); |
| 231 | ASSERT_TRUE(b.empty()); |
| 232 | } |
| 233 | |
| 234 | { |
| 235 | Variant<int*, char*> v; |
| 236 | |
| 237 | // ERROR: More than one type is implicitly convertible from nullptr. |
| 238 | // v = nullptr; |
| 239 | |
| 240 | v = static_cast<int*>(nullptr); |
| 241 | EXPECT_TRUE(v.is<int*>()); |
| 242 | |
| 243 | v = static_cast<char*>(nullptr); |
| 244 | EXPECT_TRUE(v.is<char*>()); |
| 245 | } |
| 246 | |
| 247 | { |
| 248 | Variant<int*, char*> v; |
| 249 | int a = 10; |
| 250 | char b = 20; |
| 251 | |
| 252 | v = &b; |
| 253 | ASSERT_TRUE(v.is<char*>()); |
| 254 | EXPECT_EQ(&b, std::get<char*>(v)); |
| 255 | EXPECT_EQ(b, *std::get<char*>(v)); |
| 256 | |
| 257 | v = &a; |
| 258 | ASSERT_TRUE(v.is<int*>()); |
| 259 | EXPECT_EQ(&a, std::get<int*>(v)); |
| 260 | EXPECT_EQ(a, *std::get<int*>(v)); |
| 261 | } |
| 262 | |
| 263 | { |
| 264 | using IntRef = std::reference_wrapper<int>; |
| 265 | Variant<IntRef> v; |
| 266 | int a = 10; |
| 267 | |
| 268 | v = a; |
| 269 | ASSERT_TRUE(v.is<IntRef>()); |
| 270 | EXPECT_EQ(a, std::get<IntRef>(v)); |
| 271 | |
| 272 | a = 20; |
| 273 | EXPECT_EQ(a, std::get<IntRef>(v)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | TEST(Variant, MoveAssignment) { |
| 278 | { |
| 279 | Variant<std::string> v; |
| 280 | std::string s = "test"; |
| 281 | v = std::move(s); |
| 282 | |
| 283 | EXPECT_TRUE(s.empty()); |
| 284 | ASSERT_TRUE(v.is<std::string>()); |
| 285 | EXPECT_EQ("test", std::get<std::string>(v)); |
| 286 | } |
| 287 | |
| 288 | { |
| 289 | Variant<std::string> v("test"); |
| 290 | std::string s = "fizz"; |
| 291 | s = std::move(std::get<std::string>(v)); |
| 292 | |
| 293 | ASSERT_TRUE(v.is<std::string>()); |
| 294 | EXPECT_TRUE(std::get<std::string>(v).empty()); |
| 295 | EXPECT_EQ("test", s); |
| 296 | } |
| 297 | |
| 298 | { |
| 299 | Variant<std::string> a("test"); |
| 300 | Variant<std::string> b; |
| 301 | |
| 302 | b = std::move(a); |
| 303 | ASSERT_TRUE(a.is<std::string>()); |
| 304 | ASSERT_TRUE(b.is<std::string>()); |
| 305 | EXPECT_TRUE(std::get<std::string>(a).empty()); |
| 306 | EXPECT_EQ("test", std::get<std::string>(b)); |
| 307 | } |
| 308 | |
| 309 | { |
| 310 | Variant<std::string> a("test"); |
| 311 | Variant<std::string> b("fizz"); |
| 312 | |
| 313 | b = std::move(a); |
| 314 | ASSERT_TRUE(a.is<std::string>()); |
| 315 | ASSERT_TRUE(b.is<std::string>()); |
| 316 | EXPECT_TRUE(std::get<std::string>(a).empty()); |
| 317 | EXPECT_EQ("test", std::get<std::string>(b)); |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | Variant<int, std::string> a("test"); |
| 322 | Variant<int, std::string> b(10); |
| 323 | |
| 324 | b = std::move(a); |
| 325 | ASSERT_TRUE(a.is<std::string>()); |
| 326 | ASSERT_TRUE(b.is<std::string>()); |
| 327 | EXPECT_TRUE(std::get<std::string>(a).empty()); |
| 328 | EXPECT_EQ("test", std::get<std::string>(b)); |
| 329 | } |
| 330 | |
| 331 | { |
| 332 | Variant<int, std::string> a(10); |
| 333 | Variant<int, std::string> b("test"); |
| 334 | |
| 335 | b = std::move(a); |
| 336 | ASSERT_TRUE(a.is<int>()); |
| 337 | ASSERT_TRUE(b.is<int>()); |
| 338 | EXPECT_EQ(10, std::get<int>(a)); |
| 339 | EXPECT_EQ(10, std::get<int>(b)); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | TEST(Variant, Constructor) { |
| 344 | { |
| 345 | Variant<int, bool, float> v(true); |
| 346 | EXPECT_TRUE(v.is<bool>()); |
| 347 | } |
| 348 | |
| 349 | { |
| 350 | Variant<int, bool, float> v(10); |
| 351 | EXPECT_TRUE(v.is<int>()); |
| 352 | } |
| 353 | |
| 354 | { |
| 355 | Variant<int, bool, float> v(10.1f); |
| 356 | EXPECT_TRUE(v.is<float>()); |
| 357 | } |
| 358 | |
| 359 | { |
| 360 | Variant<float, std::string> v(10.); |
| 361 | EXPECT_TRUE(v.is<float>()); |
| 362 | } |
| 363 | |
| 364 | { |
| 365 | TestType<int> i(1); |
| 366 | Variant<int, bool, float> v(i.take()); |
| 367 | ASSERT_TRUE(v.is<int>()); |
| 368 | EXPECT_EQ(1, std::get<int>(v)); |
| 369 | } |
| 370 | |
| 371 | { |
Corey Tabaka | a44b9f9 | 2017-04-03 12:01:31 -0700 | [diff] [blame] | 372 | TestType<int> i(1); |
| 373 | Variant<int, bool, float> v(i.get()); |
| 374 | ASSERT_TRUE(v.is<int>()); |
| 375 | EXPECT_EQ(1, std::get<int>(v)); |
| 376 | } |
| 377 | |
| 378 | { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 379 | TestType<bool> b(true); |
| 380 | Variant<int, bool, float> v(b.take()); |
| 381 | ASSERT_TRUE(v.is<bool>()); |
| 382 | EXPECT_EQ(true, std::get<bool>(v)); |
| 383 | } |
| 384 | |
| 385 | { |
Corey Tabaka | a44b9f9 | 2017-04-03 12:01:31 -0700 | [diff] [blame] | 386 | TestType<bool> b(true); |
| 387 | Variant<int, bool, float> v(b.get()); |
| 388 | ASSERT_TRUE(v.is<bool>()); |
| 389 | EXPECT_EQ(true, std::get<bool>(v)); |
| 390 | } |
| 391 | |
| 392 | { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 393 | Variant<const char*> c("test"); |
| 394 | Variant<std::string> s(c); |
| 395 | ASSERT_TRUE(s.is<std::string>()); |
| 396 | EXPECT_EQ("test", std::get<std::string>(s)); |
| 397 | } |
| 398 | |
| 399 | { |
| 400 | Variant<int, bool, float> a(true); |
| 401 | Variant<int, bool, float> b(a); |
| 402 | |
| 403 | ASSERT_TRUE(b.is<bool>()); |
| 404 | } |
| 405 | |
| 406 | { |
| 407 | using IntRef = std::reference_wrapper<int>; |
| 408 | int a = 10; |
| 409 | Variant<IntRef> v(a); |
| 410 | TestType<IntRef> t(a); |
| 411 | |
| 412 | ASSERT_TRUE(v.is<IntRef>()); |
| 413 | EXPECT_EQ(a, std::get<IntRef>(v)); |
| 414 | EXPECT_EQ(a, t.get()); |
| 415 | |
| 416 | a = 20; |
| 417 | EXPECT_EQ(a, std::get<IntRef>(v)); |
| 418 | EXPECT_EQ(a, t.get()); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Verify correct ctor/dtor and assignment behavior used an instrumented type. |
| 423 | TEST(Variant, CopyMoveConstructAssign) { |
| 424 | { |
| 425 | InstrumentType<int>::clear(); |
| 426 | |
| 427 | // Default construct to empty, no InstrumentType activity. |
| 428 | Variant<int, InstrumentType<int>> v; |
| 429 | ASSERT_EQ(0u, InstrumentType<int>::constructor_count()); |
| 430 | ASSERT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 431 | ASSERT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 432 | ASSERT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 433 | } |
| 434 | |
| 435 | { |
| 436 | InstrumentType<int>::clear(); |
| 437 | |
| 438 | // Construct from int type, no InstrumentType activity. |
| 439 | Variant<int, InstrumentType<int>> v; |
| 440 | v = 10; |
| 441 | EXPECT_EQ(0u, InstrumentType<int>::constructor_count()); |
| 442 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 443 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 444 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 445 | } |
| 446 | |
| 447 | { |
| 448 | InstrumentType<int>::clear(); |
| 449 | |
| 450 | // Construct from int type, no InstrumentType activity. |
| 451 | Variant<int, InstrumentType<int>> v(10); |
| 452 | EXPECT_EQ(0u, InstrumentType<int>::constructor_count()); |
| 453 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 454 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 455 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 456 | } |
| 457 | |
| 458 | { |
| 459 | InstrumentType<int>::clear(); |
| 460 | |
| 461 | // Construct from temporary, temporary ctor/dtor. |
| 462 | Variant<int, InstrumentType<int>> v; |
| 463 | v = InstrumentType<int>(25); |
| 464 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 465 | EXPECT_EQ(1u, InstrumentType<int>::destructor_count()); |
| 466 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 467 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 468 | } |
| 469 | |
| 470 | { |
| 471 | InstrumentType<int>::clear(); |
| 472 | |
| 473 | // Construct from temporary, temporary ctor/dtor. |
| 474 | Variant<int, InstrumentType<int>> v(InstrumentType<int>(25)); |
| 475 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 476 | EXPECT_EQ(1u, InstrumentType<int>::destructor_count()); |
| 477 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 478 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 479 | } |
| 480 | |
| 481 | { |
| 482 | InstrumentType<int>::clear(); |
| 483 | |
| 484 | // Construct from temporary, temporary ctor/dtor. |
| 485 | Variant<int, InstrumentType<int>> v(InstrumentType<int>(25)); |
| 486 | |
| 487 | // Assign from temporary, temporary ctor/dtor. |
| 488 | v = InstrumentType<int>(35); |
| 489 | EXPECT_EQ(3u, InstrumentType<int>::constructor_count()); |
| 490 | EXPECT_EQ(2u, InstrumentType<int>::destructor_count()); |
| 491 | EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count()); |
| 492 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 493 | } |
| 494 | |
| 495 | { |
| 496 | InstrumentType<int>::clear(); |
| 497 | |
| 498 | // Construct from temporary, temporary ctor/dtor. |
| 499 | Variant<int, InstrumentType<int>> v(InstrumentType<int>(25)); |
| 500 | |
| 501 | // dtor. |
| 502 | v = 10; |
| 503 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 504 | EXPECT_EQ(2u, InstrumentType<int>::destructor_count()); |
| 505 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 506 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 507 | } |
| 508 | |
| 509 | { |
| 510 | InstrumentType<int>::clear(); |
| 511 | |
| 512 | // Construct from temporary, temporary ctor/dtor. |
| 513 | Variant<int, InstrumentType<int>> v(InstrumentType<int>(25)); |
| 514 | |
| 515 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 516 | EXPECT_EQ(1u, InstrumentType<int>::destructor_count()); |
| 517 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 518 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 519 | } |
| 520 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 521 | EXPECT_EQ(2u, InstrumentType<int>::destructor_count()); |
| 522 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 523 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 524 | |
| 525 | { |
| 526 | InstrumentType<int>::clear(); |
| 527 | |
| 528 | // Construct from other temporary. |
| 529 | Variant<int, InstrumentType<int>> v(TestType<int>(10)); |
| 530 | EXPECT_EQ(1u, InstrumentType<int>::constructor_count()); |
| 531 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 532 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 533 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 534 | } |
| 535 | |
| 536 | { |
| 537 | InstrumentType<int>::clear(); |
| 538 | |
| 539 | // Construct from other temporary. |
| 540 | Variant<int, InstrumentType<int>> v(TestType<int>(10)); |
| 541 | // Assign from other temporary. |
| 542 | v = TestType<int>(11); |
| 543 | EXPECT_EQ(1u, InstrumentType<int>::constructor_count()); |
| 544 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 545 | EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count()); |
| 546 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 547 | } |
| 548 | |
| 549 | { |
| 550 | InstrumentType<int>::clear(); |
| 551 | |
| 552 | // Construct from other temporary. |
| 553 | Variant<int, InstrumentType<int>> v(TestType<int>(10)); |
| 554 | // Assign from empty Variant. |
| 555 | v = Variant<int, InstrumentType<int>>(); |
| 556 | EXPECT_EQ(1u, InstrumentType<int>::constructor_count()); |
| 557 | EXPECT_EQ(1u, InstrumentType<int>::destructor_count()); |
| 558 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 559 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 560 | } |
| 561 | |
| 562 | { |
| 563 | InstrumentType<int>::clear(); |
| 564 | |
| 565 | TestType<int> other(10); |
| 566 | // Construct from other. |
| 567 | Variant<int, InstrumentType<int>> v(other); |
| 568 | |
| 569 | EXPECT_EQ(1u, InstrumentType<int>::constructor_count()); |
| 570 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 571 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 572 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 573 | } |
| 574 | |
| 575 | { |
| 576 | InstrumentType<int>::clear(); |
| 577 | |
| 578 | // Construct from other temporary. |
| 579 | Variant<int, InstrumentType<int>> v(TestType<int>(0)); |
| 580 | TestType<int> other(10); |
| 581 | // Assign from other. |
| 582 | v = other; |
| 583 | EXPECT_EQ(1u, InstrumentType<int>::constructor_count()); |
| 584 | EXPECT_EQ(0u, InstrumentType<int>::destructor_count()); |
| 585 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 586 | EXPECT_EQ(1u, InstrumentType<int>::copy_assignment_count()); |
| 587 | } |
Corey Tabaka | dfee6a7 | 2017-06-01 00:24:49 -0700 | [diff] [blame] | 588 | |
| 589 | { |
| 590 | InstrumentType<int>::clear(); |
| 591 | |
| 592 | // Construct from temporary, temporary ctor/dtor. |
| 593 | Variant<int, InstrumentType<int>> v(InstrumentType<int>(25)); |
| 594 | |
| 595 | // Assign EmptyVariant. |
| 596 | v = EmptyVariant{}; |
| 597 | |
| 598 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 599 | EXPECT_EQ(2u, InstrumentType<int>::destructor_count()); |
| 600 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 601 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
| 602 | } |
| 603 | EXPECT_EQ(2u, InstrumentType<int>::constructor_count()); |
| 604 | EXPECT_EQ(2u, InstrumentType<int>::destructor_count()); |
| 605 | EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count()); |
| 606 | EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | TEST(Variant, MoveConstructor) { |
| 610 | { |
| 611 | std::unique_ptr<int> pointer = std::make_unique<int>(10); |
| 612 | Variant<std::unique_ptr<int>> v(std::move(pointer)); |
| 613 | ASSERT_TRUE(v.is<std::unique_ptr<int>>()); |
| 614 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) != nullptr); |
| 615 | EXPECT_TRUE(pointer == nullptr); |
| 616 | } |
| 617 | |
| 618 | { |
| 619 | Variant<std::unique_ptr<int>> a(std::make_unique<int>(10)); |
| 620 | Variant<std::unique_ptr<int>> b(std::move(a)); |
| 621 | |
| 622 | ASSERT_TRUE(a.is<std::unique_ptr<int>>()); |
| 623 | ASSERT_TRUE(b.is<std::unique_ptr<int>>()); |
| 624 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(a) == nullptr); |
| 625 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(b) != nullptr); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | TEST(Variant, IndexOf) { |
| 630 | Variant<int, bool, float> v1; |
| 631 | |
| 632 | EXPECT_EQ(0, v1.index_of<int>()); |
| 633 | EXPECT_EQ(1, v1.index_of<bool>()); |
| 634 | EXPECT_EQ(2, v1.index_of<float>()); |
| 635 | |
| 636 | Variant<int, bool, float, int> v2; |
| 637 | |
| 638 | EXPECT_EQ(0, v2.index_of<int>()); |
| 639 | EXPECT_EQ(1, v2.index_of<bool>()); |
| 640 | EXPECT_EQ(2, v2.index_of<float>()); |
| 641 | } |
| 642 | |
| 643 | struct Visitor { |
| 644 | int int_value = 0; |
| 645 | bool bool_value = false; |
| 646 | float float_value = 0.0; |
| 647 | bool empty_value = false; |
| 648 | |
| 649 | void Visit(int value) { int_value = value; } |
| 650 | void Visit(bool value) { bool_value = value; } |
| 651 | void Visit(float value) { float_value = value; } |
| 652 | void Visit(EmptyVariant) { empty_value = true; } |
| 653 | }; |
| 654 | |
| 655 | TEST(Variant, Visit) { |
| 656 | { |
| 657 | Variant<int, bool, float> v(10); |
| 658 | EXPECT_TRUE(v.is<int>()); |
| 659 | |
| 660 | Visitor visitor; |
| 661 | v.Visit([&visitor](const auto& value) { visitor.Visit(value); }); |
| 662 | EXPECT_EQ(10, visitor.int_value); |
| 663 | |
| 664 | visitor = {}; |
| 665 | v = true; |
| 666 | v.Visit([&visitor](const auto& value) { visitor.Visit(value); }); |
| 667 | EXPECT_EQ(true, visitor.bool_value); |
| 668 | } |
| 669 | |
| 670 | { |
| 671 | Variant<int, bool, float> v; |
| 672 | EXPECT_EQ(-1, v.index()); |
| 673 | |
| 674 | Visitor visitor; |
| 675 | v.Visit([&visitor](const auto& value) { visitor.Visit(value); }); |
| 676 | EXPECT_TRUE(visitor.empty_value); |
| 677 | } |
| 678 | |
| 679 | { |
| 680 | Variant<std::string> v("test"); |
| 681 | ASSERT_TRUE(v.is<std::string>()); |
| 682 | EXPECT_FALSE(std::get<std::string>(v).empty()); |
| 683 | |
| 684 | v.Visit([](auto&& value) { |
| 685 | std::remove_reference_t<decltype(value)> empty; |
| 686 | std::swap(empty, value); |
| 687 | }); |
| 688 | ASSERT_TRUE(v.is<std::string>()); |
| 689 | EXPECT_TRUE(std::get<std::string>(v).empty()); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | TEST(Variant, Become) { |
| 694 | { |
| 695 | Variant<int, bool, float> v; |
| 696 | |
| 697 | v.Become(0); |
| 698 | EXPECT_TRUE(v.is<int>()); |
| 699 | |
| 700 | v.Become(1); |
| 701 | EXPECT_TRUE(v.is<bool>()); |
| 702 | |
| 703 | v.Become(2); |
| 704 | EXPECT_TRUE(v.is<float>()); |
| 705 | |
| 706 | v.Become(3); |
| 707 | EXPECT_TRUE(v.empty()); |
| 708 | |
| 709 | v.Become(-1); |
| 710 | EXPECT_TRUE(v.empty()); |
| 711 | |
| 712 | v.Become(-2); |
| 713 | EXPECT_TRUE(v.empty()); |
| 714 | } |
| 715 | |
| 716 | { |
| 717 | Variant<int, bool, float> v; |
| 718 | |
| 719 | v.Become(0, 10); |
| 720 | ASSERT_TRUE(v.is<int>()); |
| 721 | EXPECT_EQ(10, std::get<int>(v)); |
| 722 | |
| 723 | v.Become(1, true); |
| 724 | ASSERT_TRUE(v.is<bool>()); |
| 725 | EXPECT_EQ(true, std::get<bool>(v)); |
| 726 | |
| 727 | v.Become(2, 2.0f); |
| 728 | ASSERT_TRUE(v.is<float>()); |
| 729 | EXPECT_FLOAT_EQ(2.0f, std::get<float>(v)); |
| 730 | |
| 731 | v.Become(3, 10); |
| 732 | EXPECT_TRUE(v.empty()); |
| 733 | |
| 734 | v.Become(-1, 10); |
| 735 | EXPECT_TRUE(v.empty()); |
| 736 | |
| 737 | v.Become(-2, 20); |
| 738 | EXPECT_TRUE(v.empty()); |
| 739 | } |
| 740 | |
| 741 | { |
| 742 | Variant<std::string> v; |
| 743 | |
| 744 | v.Become(0); |
| 745 | ASSERT_TRUE(v.is<std::string>()); |
| 746 | EXPECT_TRUE(std::get<std::string>(v).empty()); |
| 747 | } |
| 748 | |
| 749 | { |
| 750 | Variant<std::string> v; |
| 751 | |
| 752 | v.Become(0, "test"); |
| 753 | ASSERT_TRUE(v.is<std::string>()); |
| 754 | EXPECT_EQ("test", std::get<std::string>(v)); |
| 755 | } |
| 756 | |
| 757 | { |
| 758 | Variant<std::string> v("foo"); |
| 759 | |
| 760 | v.Become(0, "bar"); |
| 761 | ASSERT_TRUE(v.is<std::string>()); |
| 762 | EXPECT_EQ("foo", std::get<std::string>(v)); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | TEST(Variant, Swap) { |
| 767 | { |
| 768 | Variant<std::string> a; |
| 769 | Variant<std::string> b; |
| 770 | |
| 771 | std::swap(a, b); |
| 772 | EXPECT_TRUE(a.empty()); |
| 773 | EXPECT_TRUE(b.empty()); |
| 774 | } |
| 775 | |
| 776 | { |
| 777 | Variant<std::string> a("1"); |
| 778 | Variant<std::string> b; |
| 779 | |
| 780 | std::swap(a, b); |
Corey Tabaka | dfee6a7 | 2017-06-01 00:24:49 -0700 | [diff] [blame] | 781 | EXPECT_TRUE(a.empty()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 782 | EXPECT_TRUE(!b.empty()); |
| 783 | ASSERT_TRUE(b.is<std::string>()); |
| 784 | EXPECT_EQ("1", std::get<std::string>(b)); |
| 785 | } |
| 786 | |
| 787 | { |
| 788 | Variant<std::string> a; |
| 789 | Variant<std::string> b("1"); |
| 790 | |
| 791 | std::swap(a, b); |
| 792 | EXPECT_TRUE(!a.empty()); |
Corey Tabaka | dfee6a7 | 2017-06-01 00:24:49 -0700 | [diff] [blame] | 793 | EXPECT_TRUE(b.empty()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 794 | ASSERT_TRUE(a.is<std::string>()); |
| 795 | EXPECT_EQ("1", std::get<std::string>(a)); |
| 796 | } |
| 797 | |
| 798 | { |
| 799 | Variant<std::string> a("1"); |
| 800 | Variant<std::string> b("2"); |
| 801 | |
| 802 | std::swap(a, b); |
| 803 | ASSERT_TRUE(a.is<std::string>()); |
| 804 | ASSERT_TRUE(b.is<std::string>()); |
| 805 | EXPECT_EQ("2", std::get<std::string>(a)); |
| 806 | EXPECT_EQ("1", std::get<std::string>(b)); |
| 807 | } |
| 808 | |
| 809 | { |
| 810 | Variant<int, std::string> a(10); |
| 811 | Variant<int, std::string> b("1"); |
| 812 | |
| 813 | std::swap(a, b); |
| 814 | ASSERT_TRUE(a.is<std::string>()); |
| 815 | ASSERT_TRUE(b.is<int>()); |
| 816 | EXPECT_EQ("1", std::get<std::string>(a)); |
| 817 | EXPECT_EQ(10, std::get<int>(b)); |
| 818 | } |
| 819 | |
| 820 | { |
| 821 | Variant<int, std::string> a("1"); |
| 822 | Variant<int, std::string> b(10); |
| 823 | |
| 824 | std::swap(a, b); |
| 825 | ASSERT_TRUE(a.is<int>()); |
| 826 | ASSERT_TRUE(b.is<std::string>()); |
| 827 | EXPECT_EQ(10, std::get<int>(a)); |
| 828 | EXPECT_EQ("1", std::get<std::string>(b)); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | TEST(Variant, Get) { |
| 833 | { |
| 834 | Variant<int, bool, float, int> v; |
| 835 | |
| 836 | EXPECT_EQ(nullptr, &std::get<int>(v)); |
| 837 | EXPECT_EQ(nullptr, &std::get<bool>(v)); |
| 838 | EXPECT_EQ(nullptr, &std::get<float>(v)); |
| 839 | EXPECT_EQ(nullptr, &std::get<0>(v)); |
| 840 | EXPECT_EQ(nullptr, &std::get<1>(v)); |
| 841 | EXPECT_EQ(nullptr, &std::get<2>(v)); |
| 842 | EXPECT_EQ(nullptr, &std::get<3>(v)); |
| 843 | } |
| 844 | |
| 845 | { |
| 846 | Variant<int, bool, float, int> v; |
| 847 | v = 9; |
| 848 | ASSERT_TRUE(v.is<int>()) |
| 849 | << "Expected type " << v.index_of<int>() << " got type " << v.index(); |
| 850 | EXPECT_EQ(9, std::get<int>(v)); |
| 851 | EXPECT_EQ(9, std::get<0>(v)); |
| 852 | |
| 853 | std::get<int>(v) = 10; |
| 854 | EXPECT_EQ(10, std::get<int>(v)); |
| 855 | EXPECT_EQ(10, std::get<0>(v)); |
| 856 | |
| 857 | std::get<0>(v) = 11; |
| 858 | EXPECT_EQ(11, std::get<int>(v)); |
| 859 | EXPECT_EQ(11, std::get<0>(v)); |
| 860 | |
| 861 | std::get<3>(v) = 12; |
| 862 | EXPECT_EQ(12, std::get<int>(v)); |
| 863 | EXPECT_EQ(12, std::get<3>(v)); |
| 864 | } |
| 865 | |
| 866 | { |
| 867 | Variant<int, bool, float, int> v; |
| 868 | v = false; |
| 869 | ASSERT_TRUE(v.is<bool>()) |
| 870 | << "Expected type " << v.index_of<bool>() << " got type " << v.index(); |
| 871 | EXPECT_EQ(false, std::get<bool>(v)); |
| 872 | EXPECT_EQ(false, std::get<1>(v)); |
| 873 | |
| 874 | std::get<bool>(v) = true; |
| 875 | EXPECT_EQ(true, std::get<bool>(v)); |
| 876 | EXPECT_EQ(true, std::get<1>(v)); |
| 877 | |
| 878 | std::get<bool>(v) = false; |
| 879 | EXPECT_EQ(false, std::get<bool>(v)); |
| 880 | EXPECT_EQ(false, std::get<1>(v)); |
| 881 | |
| 882 | std::get<1>(v) = true; |
| 883 | EXPECT_EQ(true, std::get<bool>(v)); |
| 884 | EXPECT_EQ(true, std::get<1>(v)); |
| 885 | |
| 886 | std::get<1>(v) = false; |
| 887 | EXPECT_EQ(false, std::get<bool>(v)); |
| 888 | EXPECT_EQ(false, std::get<1>(v)); |
| 889 | } |
| 890 | |
| 891 | { |
| 892 | Variant<int, bool, float, int> v; |
| 893 | v = 1.0f; |
| 894 | ASSERT_TRUE(v.is<float>()) |
| 895 | << "Expected type " << v.index_of<float>() << " got type " << v.index(); |
| 896 | EXPECT_EQ(2, v.index()); |
| 897 | EXPECT_FLOAT_EQ(1.0, std::get<float>(v)); |
| 898 | EXPECT_FLOAT_EQ(1.0, std::get<2>(v)); |
| 899 | |
| 900 | std::get<float>(v) = 1.1; |
| 901 | EXPECT_FLOAT_EQ(1.1, std::get<float>(v)); |
| 902 | EXPECT_FLOAT_EQ(1.1, std::get<2>(v)); |
| 903 | |
| 904 | std::get<float>(v) = -3.0; |
| 905 | EXPECT_FLOAT_EQ(-3.0, std::get<float>(v)); |
| 906 | EXPECT_FLOAT_EQ(-3.0, std::get<2>(v)); |
| 907 | |
| 908 | std::get<2>(v) = 1.1; |
| 909 | EXPECT_FLOAT_EQ(1.1, std::get<float>(v)); |
| 910 | EXPECT_FLOAT_EQ(1.1, std::get<2>(v)); |
| 911 | |
| 912 | std::get<2>(v) = -3.0; |
| 913 | EXPECT_FLOAT_EQ(-3.0, std::get<float>(v)); |
| 914 | EXPECT_FLOAT_EQ(-3.0, std::get<2>(v)); |
| 915 | } |
| 916 | |
| 917 | { |
| 918 | Variant<std::unique_ptr<int>> v(std::make_unique<int>(10)); |
| 919 | std::unique_ptr<int> pointer = std::move(std::get<std::unique_ptr<int>>(v)); |
| 920 | ASSERT_FALSE(v.empty()); |
| 921 | EXPECT_TRUE(pointer != nullptr); |
| 922 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr); |
| 923 | } |
| 924 | |
| 925 | { |
| 926 | Variant<std::string> v("test"); |
| 927 | std::string s = std::get<std::string>(std::move(v)); |
| 928 | EXPECT_EQ("test", s); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | TEST(Variant, IfAnyOf) { |
| 933 | { |
| 934 | Variant<int, float> v(10); |
| 935 | ASSERT_TRUE(v.is<int>()); |
| 936 | |
| 937 | bool b = false; |
| 938 | EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b)); |
| 939 | EXPECT_TRUE(b); |
| 940 | |
| 941 | float f = 0.0f; |
| 942 | EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f))); |
| 943 | EXPECT_FLOAT_EQ(10.f, f); |
| 944 | } |
| 945 | |
| 946 | { |
| 947 | const Variant<int, float> v(10); |
| 948 | ASSERT_TRUE(v.is<int>()); |
| 949 | |
| 950 | bool b = false; |
| 951 | EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b)); |
| 952 | EXPECT_TRUE(b); |
| 953 | |
| 954 | float f = 0.0f; |
| 955 | EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f))); |
| 956 | EXPECT_FLOAT_EQ(10.f, f); |
| 957 | } |
| 958 | |
| 959 | { |
| 960 | Variant<int, float> v(10); |
| 961 | ASSERT_TRUE(v.is<int>()); |
| 962 | |
| 963 | bool b = false; |
| 964 | EXPECT_TRUE(IfAnyOf<int>::Call(&v, [&b](const auto& value) { b = value; })); |
| 965 | EXPECT_TRUE(b); |
| 966 | |
| 967 | float f = 0.0f; |
| 968 | EXPECT_TRUE(( |
| 969 | IfAnyOf<int, float>::Call(&v, [&f](const auto& value) { f = value; }))); |
| 970 | EXPECT_FLOAT_EQ(10.f, f); |
| 971 | } |
| 972 | |
| 973 | { |
| 974 | Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10)); |
| 975 | ASSERT_TRUE(v.is<std::unique_ptr<int>>()); |
| 976 | const int* original_v = std::get<std::unique_ptr<int>>(v).get(); |
| 977 | |
| 978 | std::unique_ptr<int> u(std::make_unique<int>(20)); |
| 979 | |
| 980 | EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Take(&v, &u)); |
| 981 | ASSERT_TRUE(v.is<std::unique_ptr<int>>()); |
| 982 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr); |
| 983 | EXPECT_EQ(u.get(), original_v); |
| 984 | } |
| 985 | |
| 986 | { |
| 987 | Variant<std::unique_ptr<DerivedType>, int> v( |
| 988 | std::make_unique<DerivedType>(10)); |
| 989 | ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>()); |
| 990 | const DerivedType* original_v = |
| 991 | std::get<std::unique_ptr<DerivedType>>(v).get(); |
| 992 | |
| 993 | std::unique_ptr<BaseType> u(std::make_unique<BaseType>(20)); |
| 994 | |
| 995 | EXPECT_TRUE(IfAnyOf<std::unique_ptr<DerivedType>>::Take(&v, &u)); |
| 996 | ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>()); |
| 997 | EXPECT_TRUE(std::get<std::unique_ptr<DerivedType>>(v) == nullptr); |
| 998 | EXPECT_EQ(u.get(), original_v); |
| 999 | } |
| 1000 | |
| 1001 | { |
| 1002 | Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10)); |
| 1003 | ASSERT_TRUE(v.is<std::unique_ptr<int>>()); |
| 1004 | const int* original_v = std::get<std::unique_ptr<int>>(v).get(); |
| 1005 | |
| 1006 | std::unique_ptr<int> u(std::make_unique<int>(20)); |
| 1007 | |
| 1008 | EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Call( |
| 1009 | &v, [&u](auto&& value) { u = std::move(value); })); |
| 1010 | ASSERT_TRUE(v.is<std::unique_ptr<int>>()); |
| 1011 | EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr); |
| 1012 | EXPECT_EQ(u.get(), original_v); |
| 1013 | } |
| 1014 | |
| 1015 | { |
| 1016 | Variant<int, bool, float> v(true); |
| 1017 | ASSERT_TRUE(v.is<bool>()); |
| 1018 | |
| 1019 | float f = 0.f; |
| 1020 | EXPECT_FALSE((IfAnyOf<int, float>::Get(&v, &f))); |
| 1021 | EXPECT_FLOAT_EQ(0.f, f); |
| 1022 | } |
| 1023 | |
| 1024 | { |
| 1025 | Variant<std::string, int> v("foo"); |
| 1026 | ASSERT_TRUE(v.is<std::string>()); |
| 1027 | |
| 1028 | std::string s = "bar"; |
| 1029 | EXPECT_TRUE(IfAnyOf<std::string>::Swap(&v, &s)); |
| 1030 | ASSERT_TRUE(v.is<std::string>()); |
| 1031 | EXPECT_EQ("bar", std::get<std::string>(v)); |
| 1032 | EXPECT_EQ("foo", s); |
| 1033 | } |
| 1034 | |
| 1035 | { |
| 1036 | Variant<std::string, const char*> v(static_cast<const char*>("foo")); |
| 1037 | ASSERT_TRUE(v.is<const char*>()); |
| 1038 | |
| 1039 | std::string s = "bar"; |
| 1040 | EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s))); |
| 1041 | ASSERT_TRUE(v.is<const char*>()); |
| 1042 | EXPECT_EQ("foo", std::get<const char*>(v)); |
| 1043 | EXPECT_EQ("foo", s); |
| 1044 | |
| 1045 | v = std::string("bar"); |
| 1046 | ASSERT_TRUE(v.is<std::string>()); |
| 1047 | |
| 1048 | EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s))); |
| 1049 | ASSERT_TRUE(v.is<std::string>()); |
| 1050 | EXPECT_EQ("bar", s); |
| 1051 | } |
| 1052 | |
| 1053 | { |
| 1054 | Variant<std::string, const char*> v; |
| 1055 | ASSERT_TRUE(v.empty()); |
| 1056 | |
| 1057 | std::string s = "bar"; |
| 1058 | EXPECT_FALSE((IfAnyOf<std::string, const char*>::Take(&v, &s))); |
| 1059 | EXPECT_EQ("bar", s); |
| 1060 | } |
| 1061 | |
| 1062 | { |
| 1063 | Variant<std::string, const char*> v(static_cast<const char*>("test")); |
| 1064 | ASSERT_TRUE(v.is<const char*>()); |
| 1065 | |
| 1066 | std::string s; |
| 1067 | EXPECT_FALSE(IfAnyOf<>::Take(&v, &s)); |
| 1068 | EXPECT_TRUE(s.empty()); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | TEST(Variant, ConstVolatile) { |
| 1073 | { |
| 1074 | Variant<const int> v(10); |
| 1075 | ASSERT_TRUE(v.is<const int>()); |
| 1076 | EXPECT_EQ(10, std::get<const int>(v)); |
| 1077 | } |
| 1078 | |
| 1079 | { |
| 1080 | Variant<const std::string> v("test"); |
| 1081 | ASSERT_TRUE(v.is<const std::string>()); |
| 1082 | EXPECT_EQ("test", std::get<const std::string>(v)); |
| 1083 | } |
| 1084 | |
| 1085 | { |
| 1086 | Variant<volatile int, std::string> v(10); |
| 1087 | ASSERT_TRUE(v.is<volatile int>()); |
| 1088 | EXPECT_EQ(10, std::get<volatile int>(v)); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | TEST(Variant, HasType) { |
| 1093 | EXPECT_TRUE((detail::HasType<int, int, float, bool>::value)); |
| 1094 | EXPECT_FALSE((detail::HasType<char, int, float, bool>::value)); |
| 1095 | EXPECT_FALSE(detail::HasType<>::value); |
| 1096 | |
Corey Tabaka | a44b9f9 | 2017-04-03 12:01:31 -0700 | [diff] [blame] | 1097 | EXPECT_TRUE((detail::HasType<int&, int, float, bool>::value)); |
| 1098 | EXPECT_FALSE((detail::HasType<char&, int, float, bool>::value)); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Corey Tabaka | 7190e8a | 2017-06-22 20:39:42 -0700 | [diff] [blame] | 1101 | TEST(Variant, IsConstructible) { |
| 1102 | using ArrayType = const float[3]; |
| 1103 | struct ImplicitBool { |
| 1104 | operator bool() const { return true; } |
| 1105 | }; |
| 1106 | struct ExplicitBool { |
| 1107 | explicit operator bool() const { return true; } |
| 1108 | }; |
| 1109 | struct NonBool {}; |
| 1110 | struct TwoArgs { |
| 1111 | TwoArgs(int, bool) {} |
| 1112 | }; |
| 1113 | |
| 1114 | EXPECT_FALSE((detail::IsConstructible<bool, ArrayType>::value)); |
| 1115 | EXPECT_TRUE((detail::IsConstructible<bool, int>::value)); |
| 1116 | EXPECT_TRUE((detail::IsConstructible<bool, ImplicitBool>::value)); |
| 1117 | EXPECT_TRUE((detail::IsConstructible<bool, ExplicitBool>::value)); |
| 1118 | EXPECT_FALSE((detail::IsConstructible<bool, NonBool>::value)); |
| 1119 | EXPECT_TRUE((detail::IsConstructible<TwoArgs, int, bool>::value)); |
| 1120 | EXPECT_FALSE((detail::IsConstructible<TwoArgs, int, std::string>::value)); |
| 1121 | EXPECT_FALSE((detail::IsConstructible<TwoArgs, int>::value)); |
| 1122 | } |
| 1123 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1124 | TEST(Variant, Set) { |
| 1125 | EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<int, bool, |
| 1126 | float>::value)); |
| 1127 | EXPECT_TRUE( |
| 1128 | (detail::Set<int, bool, float>::template IsSubset<bool, float>::value)); |
| 1129 | EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<float>::value)); |
| 1130 | EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<>::value)); |
| 1131 | |
| 1132 | EXPECT_FALSE( |
| 1133 | (detail::Set<int, bool, float>::template IsSubset<int, bool, float, |
| 1134 | char>::value)); |
| 1135 | EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<bool, float, |
| 1136 | char>::value)); |
| 1137 | EXPECT_FALSE( |
| 1138 | (detail::Set<int, bool, float>::template IsSubset<float, char>::value)); |
| 1139 | EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<char>::value)); |
| 1140 | |
| 1141 | EXPECT_TRUE(detail::Set<>::template IsSubset<>::value); |
| 1142 | EXPECT_FALSE(detail::Set<>::template IsSubset<int>::value); |
| 1143 | EXPECT_FALSE((detail::Set<>::template IsSubset<int, float>::value)); |
| 1144 | } |