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