1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- #include <array>
- #include <cstdint>
- #include <functional>
- #include <memory>
- #include <string>
- #include <type_traits>
- #include <gtest/gtest.h>
- #include <pdx/rpc/variant.h>
- using namespace android::pdx;
- using namespace android::pdx::rpc;
- namespace {
- struct BaseType {
- // NOLINTNEXTLINE(google-explicit-constructor)
- BaseType(int value) : value(value) {}
- int value;
- };
- struct DerivedType : BaseType {
- // NOLINTNEXTLINE(google-explicit-constructor)
- DerivedType(int value) : BaseType{value} {};
- };
- template <typename T>
- class TestType {
- public:
- // NOLINTNEXTLINE(google-explicit-constructor)
- TestType(const T& value) : value_(value) {}
- // NOLINTNEXTLINE(google-explicit-constructor)
- TestType(T&& value) : value_(std::move(value)) {}
- TestType(const TestType&) = default;
- TestType(TestType&&) = default;
- TestType& operator=(const TestType&) = default;
- TestType& operator=(TestType&&) = default;
- const T& get() const { return value_; }
- T&& take() { return std::move(value_); }
- private:
- T value_;
- };
- template <typename T>
- class InstrumentType {
- public:
- // NOLINTNEXTLINE(google-explicit-constructor)
- InstrumentType(const T& value) : value_(value) { constructor_count_++; }
- // NOLINTNEXTLINE(google-explicit-constructor)
- InstrumentType(T&& value) : value_(std::move(value)) { constructor_count_++; }
- InstrumentType(const InstrumentType& other) : value_(other.value_) {
- constructor_count_++;
- }
- InstrumentType(InstrumentType&& other) : value_(std::move(other.value_)) {
- constructor_count_++;
- }
- // NOLINTNEXTLINE(google-explicit-constructor)
- InstrumentType(const TestType<T>& other) : value_(other.get()) {
- constructor_count_++;
- }
- // NOLINTNEXTLINE(google-explicit-constructor)
- InstrumentType(TestType<T>&& other) : value_(other.take()) {
- constructor_count_++;
- }
- ~InstrumentType() { destructor_count_++; }
- InstrumentType& operator=(const InstrumentType& other) {
- copy_assignment_count_++;
- value_ = other.value_;
- return *this;
- }
- InstrumentType& operator=(InstrumentType&& other) {
- move_assignment_count_++;
- value_ = std::move(other.value_);
- return *this;
- }
- InstrumentType& operator=(const TestType<T>& other) {
- copy_assignment_count_++;
- value_ = other.get();
- return *this;
- }
- InstrumentType& operator=(TestType<T>&& other) {
- move_assignment_count_++;
- value_ = other.take();
- return *this;
- }
- static std::size_t constructor_count() { return constructor_count_; }
- static std::size_t destructor_count() { return destructor_count_; }
- static std::size_t move_assignment_count() { return move_assignment_count_; }
- static std::size_t copy_assignment_count() { return copy_assignment_count_; }
- const T& get() const { return value_; }
- T&& take() { return std::move(value_); }
- static void clear() {
- constructor_count_ = 0;
- destructor_count_ = 0;
- move_assignment_count_ = 0;
- copy_assignment_count_ = 0;
- }
- private:
- T value_;
- static std::size_t constructor_count_;
- static std::size_t destructor_count_;
- static std::size_t move_assignment_count_;
- static std::size_t copy_assignment_count_;
- };
- template <typename T>
- std::size_t InstrumentType<T>::constructor_count_ = 0;
- template <typename T>
- std::size_t InstrumentType<T>::destructor_count_ = 0;
- template <typename T>
- std::size_t InstrumentType<T>::move_assignment_count_ = 0;
- template <typename T>
- std::size_t InstrumentType<T>::copy_assignment_count_ = 0;
- } // anonymous namespace
- TEST(Variant, Assignment) {
- // Assert basic type properties.
- {
- Variant<int, bool, float> v;
- ASSERT_EQ(-1, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_FALSE(v.is<bool>());
- ASSERT_FALSE(v.is<float>());
- }
- {
- Variant<int, bool, float> v;
- v = 10;
- ASSERT_EQ(0, v.index());
- ASSERT_TRUE(v.is<int>());
- ASSERT_FALSE(v.is<bool>());
- ASSERT_FALSE(v.is<float>());
- EXPECT_EQ(10, std::get<int>(v));
- }
- {
- Variant<int, bool, float> v;
- v = false;
- ASSERT_EQ(1, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_TRUE(v.is<bool>());
- ASSERT_FALSE(v.is<float>());
- EXPECT_EQ(false, std::get<bool>(v));
- }
- {
- Variant<int, bool, float> v;
- v = 1.0f;
- ASSERT_EQ(2, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_FALSE(v.is<bool>());
- ASSERT_TRUE(v.is<float>());
- EXPECT_FLOAT_EQ(1.0f, std::get<float>(v));
- }
- {
- Variant<int, bool, float> v;
- // ERROR: More than one type is implicitly convertible from double.
- // v = 1.0;
- v = static_cast<float>(1.0);
- }
- {
- Variant<int, bool, float> v;
- double x = 1.1;
- v = static_cast<float>(x);
- ASSERT_EQ(2, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_FALSE(v.is<bool>());
- ASSERT_TRUE(v.is<float>());
- EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
- }
- {
- Variant<int, std::string> v;
- ASSERT_EQ(-1, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_FALSE(v.is<std::string>());
- }
- {
- Variant<int, std::string> v;
- v = 20;
- ASSERT_EQ(0, v.index());
- ASSERT_TRUE(v.is<int>());
- ASSERT_FALSE(v.is<std::string>());
- EXPECT_EQ(20, std::get<int>(v));
- }
- {
- Variant<int, std::string> v;
- v = std::string("test");
- ASSERT_EQ(1, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(v));
- }
- {
- Variant<int, std::string> v;
- v = "test";
- ASSERT_EQ(1, v.index());
- ASSERT_FALSE(v.is<int>());
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(v));
- }
- {
- Variant<const char*> v1;
- Variant<std::string> v2;
- v1 = "test";
- ASSERT_TRUE(v1.is<const char*>());
- v2 = v1;
- ASSERT_TRUE(v2.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(v2));
- }
- {
- Variant<int> a(1);
- Variant<int> b;
- ASSERT_TRUE(!a.empty());
- ASSERT_TRUE(b.empty());
- a = b;
- ASSERT_TRUE(a.empty());
- ASSERT_TRUE(b.empty());
- }
- {
- Variant<int*, char*> v;
- // ERROR: More than one type is implicitly convertible from nullptr.
- // v = nullptr;
- v = static_cast<int*>(nullptr);
- EXPECT_TRUE(v.is<int*>());
- v = static_cast<char*>(nullptr);
- EXPECT_TRUE(v.is<char*>());
- }
- {
- Variant<int*, char*> v;
- int a = 10;
- char b = 20;
- v = &b;
- ASSERT_TRUE(v.is<char*>());
- EXPECT_EQ(&b, std::get<char*>(v));
- EXPECT_EQ(b, *std::get<char*>(v));
- v = &a;
- ASSERT_TRUE(v.is<int*>());
- EXPECT_EQ(&a, std::get<int*>(v));
- EXPECT_EQ(a, *std::get<int*>(v));
- }
- {
- using IntRef = std::reference_wrapper<int>;
- Variant<IntRef> v;
- int a = 10;
- v = a;
- ASSERT_TRUE(v.is<IntRef>());
- EXPECT_EQ(a, std::get<IntRef>(v));
- a = 20;
- EXPECT_EQ(a, std::get<IntRef>(v));
- }
- }
- TEST(Variant, MoveAssignment) {
- {
- Variant<std::string> v;
- std::string s = "test";
- v = std::move(s);
- EXPECT_TRUE(s.empty());
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(v));
- }
- {
- Variant<std::string> v("test");
- std::string s = "fizz";
- s = std::move(std::get<std::string>(v));
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(v).empty());
- EXPECT_EQ("test", s);
- }
- {
- Variant<std::string> a("test");
- Variant<std::string> b;
- b = std::move(a);
- ASSERT_TRUE(a.is<std::string>());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(a).empty());
- EXPECT_EQ("test", std::get<std::string>(b));
- }
- {
- Variant<std::string> a("test");
- Variant<std::string> b("fizz");
- b = std::move(a);
- ASSERT_TRUE(a.is<std::string>());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(a).empty());
- EXPECT_EQ("test", std::get<std::string>(b));
- }
- {
- Variant<int, std::string> a("test");
- Variant<int, std::string> b(10);
- b = std::move(a);
- ASSERT_TRUE(a.is<std::string>());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(a).empty());
- EXPECT_EQ("test", std::get<std::string>(b));
- }
- {
- Variant<int, std::string> a(10);
- Variant<int, std::string> b("test");
- b = std::move(a);
- ASSERT_TRUE(a.is<int>());
- ASSERT_TRUE(b.is<int>());
- EXPECT_EQ(10, std::get<int>(a));
- EXPECT_EQ(10, std::get<int>(b));
- }
- }
- TEST(Variant, Constructor) {
- {
- Variant<int, bool, float> v(true);
- EXPECT_TRUE(v.is<bool>());
- }
- {
- Variant<int, bool, float> v(10);
- EXPECT_TRUE(v.is<int>());
- }
- {
- Variant<int, bool, float> v(10.1f);
- EXPECT_TRUE(v.is<float>());
- }
- {
- Variant<float, std::string> v(10.);
- EXPECT_TRUE(v.is<float>());
- }
- {
- TestType<int> i(1);
- Variant<int, bool, float> v(i.take());
- ASSERT_TRUE(v.is<int>());
- EXPECT_EQ(1, std::get<int>(v));
- }
- {
- TestType<int> i(1);
- Variant<int, bool, float> v(i.get());
- ASSERT_TRUE(v.is<int>());
- EXPECT_EQ(1, std::get<int>(v));
- }
- {
- TestType<bool> b(true);
- Variant<int, bool, float> v(b.take());
- ASSERT_TRUE(v.is<bool>());
- EXPECT_EQ(true, std::get<bool>(v));
- }
- {
- TestType<bool> b(true);
- Variant<int, bool, float> v(b.get());
- ASSERT_TRUE(v.is<bool>());
- EXPECT_EQ(true, std::get<bool>(v));
- }
- {
- Variant<const char*> c("test");
- Variant<std::string> s(c);
- ASSERT_TRUE(s.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(s));
- }
- {
- Variant<int, bool, float> a(true);
- Variant<int, bool, float> b(a);
- ASSERT_TRUE(b.is<bool>());
- }
- {
- using IntRef = std::reference_wrapper<int>;
- int a = 10;
- Variant<IntRef> v(a);
- TestType<IntRef> t(a);
- ASSERT_TRUE(v.is<IntRef>());
- EXPECT_EQ(a, std::get<IntRef>(v));
- EXPECT_EQ(a, t.get());
- a = 20;
- EXPECT_EQ(a, std::get<IntRef>(v));
- EXPECT_EQ(a, t.get());
- }
- }
- // Verify correct ctor/dtor and assignment behavior used an instrumented type.
- TEST(Variant, CopyMoveConstructAssign) {
- {
- InstrumentType<int>::clear();
- // Default construct to empty, no InstrumentType activity.
- Variant<int, InstrumentType<int>> v;
- ASSERT_EQ(0u, InstrumentType<int>::constructor_count());
- ASSERT_EQ(0u, InstrumentType<int>::destructor_count());
- ASSERT_EQ(0u, InstrumentType<int>::move_assignment_count());
- ASSERT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from int type, no InstrumentType activity.
- Variant<int, InstrumentType<int>> v;
- v = 10;
- EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from int type, no InstrumentType activity.
- Variant<int, InstrumentType<int>> v(10);
- EXPECT_EQ(0u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v;
- v = InstrumentType<int>(25);
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
- // Assign from temporary, temporary ctor/dtor.
- v = InstrumentType<int>(35);
- EXPECT_EQ(3u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
- // dtor.
- v = 10;
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- {
- InstrumentType<int>::clear();
- // Construct from other temporary.
- Variant<int, InstrumentType<int>> v(TestType<int>(10));
- EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from other temporary.
- Variant<int, InstrumentType<int>> v(TestType<int>(10));
- // Assign from other temporary.
- v = TestType<int>(11);
- EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from other temporary.
- Variant<int, InstrumentType<int>> v(TestType<int>(10));
- // Assign from empty Variant.
- v = Variant<int, InstrumentType<int>>();
- EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(1u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- TestType<int> other(10);
- // Construct from other.
- Variant<int, InstrumentType<int>> v(other);
- EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from other temporary.
- Variant<int, InstrumentType<int>> v(TestType<int>(0));
- TestType<int> other(10);
- // Assign from other.
- v = other;
- EXPECT_EQ(1u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(1u, InstrumentType<int>::copy_assignment_count());
- }
- {
- InstrumentType<int>::clear();
- // Construct from temporary, temporary ctor/dtor.
- Variant<int, InstrumentType<int>> v(InstrumentType<int>(25));
- // Assign EmptyVariant.
- v = EmptyVariant{};
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- EXPECT_EQ(2u, InstrumentType<int>::constructor_count());
- EXPECT_EQ(2u, InstrumentType<int>::destructor_count());
- EXPECT_EQ(0u, InstrumentType<int>::move_assignment_count());
- EXPECT_EQ(0u, InstrumentType<int>::copy_assignment_count());
- }
- TEST(Variant, MoveConstructor) {
- {
- std::unique_ptr<int> pointer = std::make_unique<int>(10);
- Variant<std::unique_ptr<int>> v(std::move(pointer));
- ASSERT_TRUE(v.is<std::unique_ptr<int>>());
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) != nullptr);
- EXPECT_TRUE(pointer == nullptr);
- }
- {
- Variant<std::unique_ptr<int>> a(std::make_unique<int>(10));
- Variant<std::unique_ptr<int>> b(std::move(a));
- ASSERT_TRUE(a.is<std::unique_ptr<int>>());
- ASSERT_TRUE(b.is<std::unique_ptr<int>>());
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(a) == nullptr);
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(b) != nullptr);
- }
- }
- TEST(Variant, IndexOf) {
- Variant<int, bool, float> v1;
- EXPECT_EQ(0, v1.index_of<int>());
- EXPECT_EQ(1, v1.index_of<bool>());
- EXPECT_EQ(2, v1.index_of<float>());
- Variant<int, bool, float, int> v2;
- EXPECT_EQ(0, v2.index_of<int>());
- EXPECT_EQ(1, v2.index_of<bool>());
- EXPECT_EQ(2, v2.index_of<float>());
- }
- struct Visitor {
- int int_value = 0;
- bool bool_value = false;
- float float_value = 0.0;
- bool empty_value = false;
- void Visit(int value) { int_value = value; }
- void Visit(bool value) { bool_value = value; }
- void Visit(float value) { float_value = value; }
- void Visit(EmptyVariant) { empty_value = true; }
- };
- TEST(Variant, Visit) {
- {
- Variant<int, bool, float> v(10);
- EXPECT_TRUE(v.is<int>());
- Visitor visitor;
- v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
- EXPECT_EQ(10, visitor.int_value);
- visitor = {};
- v = true;
- v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
- EXPECT_EQ(true, visitor.bool_value);
- }
- {
- Variant<int, bool, float> v;
- EXPECT_EQ(-1, v.index());
- Visitor visitor;
- v.Visit([&visitor](const auto& value) { visitor.Visit(value); });
- EXPECT_TRUE(visitor.empty_value);
- }
- {
- Variant<std::string> v("test");
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_FALSE(std::get<std::string>(v).empty());
- v.Visit([](auto&& value) {
- std::remove_reference_t<decltype(value)> empty;
- std::swap(empty, value);
- });
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(v).empty());
- }
- }
- TEST(Variant, Become) {
- {
- Variant<int, bool, float> v;
- v.Become(0);
- EXPECT_TRUE(v.is<int>());
- v.Become(1);
- EXPECT_TRUE(v.is<bool>());
- v.Become(2);
- EXPECT_TRUE(v.is<float>());
- v.Become(3);
- EXPECT_TRUE(v.empty());
- v.Become(-1);
- EXPECT_TRUE(v.empty());
- v.Become(-2);
- EXPECT_TRUE(v.empty());
- }
- {
- Variant<int, bool, float> v;
- v.Become(0, 10);
- ASSERT_TRUE(v.is<int>());
- EXPECT_EQ(10, std::get<int>(v));
- v.Become(1, true);
- ASSERT_TRUE(v.is<bool>());
- EXPECT_EQ(true, std::get<bool>(v));
- v.Become(2, 2.0f);
- ASSERT_TRUE(v.is<float>());
- EXPECT_FLOAT_EQ(2.0f, std::get<float>(v));
- v.Become(3, 10);
- EXPECT_TRUE(v.empty());
- v.Become(-1, 10);
- EXPECT_TRUE(v.empty());
- v.Become(-2, 20);
- EXPECT_TRUE(v.empty());
- }
- {
- Variant<std::string> v;
- v.Become(0);
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_TRUE(std::get<std::string>(v).empty());
- }
- {
- Variant<std::string> v;
- v.Become(0, "test");
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("test", std::get<std::string>(v));
- }
- {
- Variant<std::string> v("foo");
- v.Become(0, "bar");
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("foo", std::get<std::string>(v));
- }
- }
- TEST(Variant, Swap) {
- {
- Variant<std::string> a;
- Variant<std::string> b;
- std::swap(a, b);
- EXPECT_TRUE(a.empty());
- EXPECT_TRUE(b.empty());
- }
- {
- Variant<std::string> a("1");
- Variant<std::string> b;
- std::swap(a, b);
- EXPECT_TRUE(a.empty());
- EXPECT_TRUE(!b.empty());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_EQ("1", std::get<std::string>(b));
- }
- {
- Variant<std::string> a;
- Variant<std::string> b("1");
- std::swap(a, b);
- EXPECT_TRUE(!a.empty());
- EXPECT_TRUE(b.empty());
- ASSERT_TRUE(a.is<std::string>());
- EXPECT_EQ("1", std::get<std::string>(a));
- }
- {
- Variant<std::string> a("1");
- Variant<std::string> b("2");
- std::swap(a, b);
- ASSERT_TRUE(a.is<std::string>());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_EQ("2", std::get<std::string>(a));
- EXPECT_EQ("1", std::get<std::string>(b));
- }
- {
- Variant<int, std::string> a(10);
- Variant<int, std::string> b("1");
- std::swap(a, b);
- ASSERT_TRUE(a.is<std::string>());
- ASSERT_TRUE(b.is<int>());
- EXPECT_EQ("1", std::get<std::string>(a));
- EXPECT_EQ(10, std::get<int>(b));
- }
- {
- Variant<int, std::string> a("1");
- Variant<int, std::string> b(10);
- std::swap(a, b);
- ASSERT_TRUE(a.is<int>());
- ASSERT_TRUE(b.is<std::string>());
- EXPECT_EQ(10, std::get<int>(a));
- EXPECT_EQ("1", std::get<std::string>(b));
- }
- }
- TEST(Variant, Get) {
- {
- Variant<int, bool, float, int> v;
- EXPECT_EQ(nullptr, &std::get<int>(v));
- EXPECT_EQ(nullptr, &std::get<bool>(v));
- EXPECT_EQ(nullptr, &std::get<float>(v));
- EXPECT_EQ(nullptr, &std::get<0>(v));
- EXPECT_EQ(nullptr, &std::get<1>(v));
- EXPECT_EQ(nullptr, &std::get<2>(v));
- EXPECT_EQ(nullptr, &std::get<3>(v));
- }
- {
- Variant<int, bool, float, int> v;
- v = 9;
- ASSERT_TRUE(v.is<int>())
- << "Expected type " << v.index_of<int>() << " got type " << v.index();
- EXPECT_EQ(9, std::get<int>(v));
- EXPECT_EQ(9, std::get<0>(v));
- std::get<int>(v) = 10;
- EXPECT_EQ(10, std::get<int>(v));
- EXPECT_EQ(10, std::get<0>(v));
- std::get<0>(v) = 11;
- EXPECT_EQ(11, std::get<int>(v));
- EXPECT_EQ(11, std::get<0>(v));
- std::get<3>(v) = 12;
- EXPECT_EQ(12, std::get<int>(v));
- EXPECT_EQ(12, std::get<3>(v));
- }
- {
- Variant<int, bool, float, int> v;
- v = false;
- ASSERT_TRUE(v.is<bool>())
- << "Expected type " << v.index_of<bool>() << " got type " << v.index();
- EXPECT_EQ(false, std::get<bool>(v));
- EXPECT_EQ(false, std::get<1>(v));
- std::get<bool>(v) = true;
- EXPECT_EQ(true, std::get<bool>(v));
- EXPECT_EQ(true, std::get<1>(v));
- std::get<bool>(v) = false;
- EXPECT_EQ(false, std::get<bool>(v));
- EXPECT_EQ(false, std::get<1>(v));
- std::get<1>(v) = true;
- EXPECT_EQ(true, std::get<bool>(v));
- EXPECT_EQ(true, std::get<1>(v));
- std::get<1>(v) = false;
- EXPECT_EQ(false, std::get<bool>(v));
- EXPECT_EQ(false, std::get<1>(v));
- }
- {
- Variant<int, bool, float, int> v;
- v = 1.0f;
- ASSERT_TRUE(v.is<float>())
- << "Expected type " << v.index_of<float>() << " got type " << v.index();
- EXPECT_EQ(2, v.index());
- EXPECT_FLOAT_EQ(1.0, std::get<float>(v));
- EXPECT_FLOAT_EQ(1.0, std::get<2>(v));
- std::get<float>(v) = 1.1;
- EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
- EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
- std::get<float>(v) = -3.0;
- EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
- EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
- std::get<2>(v) = 1.1;
- EXPECT_FLOAT_EQ(1.1, std::get<float>(v));
- EXPECT_FLOAT_EQ(1.1, std::get<2>(v));
- std::get<2>(v) = -3.0;
- EXPECT_FLOAT_EQ(-3.0, std::get<float>(v));
- EXPECT_FLOAT_EQ(-3.0, std::get<2>(v));
- }
- {
- Variant<std::unique_ptr<int>> v(std::make_unique<int>(10));
- std::unique_ptr<int> pointer = std::move(std::get<std::unique_ptr<int>>(v));
- ASSERT_FALSE(v.empty());
- EXPECT_TRUE(pointer != nullptr);
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
- }
- {
- Variant<std::string> v("test");
- std::string s = std::get<std::string>(std::move(v));
- EXPECT_EQ("test", s);
- }
- }
- TEST(Variant, IfAnyOf) {
- {
- Variant<int, float> v(10);
- ASSERT_TRUE(v.is<int>());
- bool b = false;
- EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
- EXPECT_TRUE(b);
- float f = 0.0f;
- EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
- EXPECT_FLOAT_EQ(10.f, f);
- }
- {
- const Variant<int, float> v(10);
- ASSERT_TRUE(v.is<int>());
- bool b = false;
- EXPECT_TRUE(IfAnyOf<int>::Get(&v, &b));
- EXPECT_TRUE(b);
- float f = 0.0f;
- EXPECT_TRUE((IfAnyOf<int, float>::Get(&v, &f)));
- EXPECT_FLOAT_EQ(10.f, f);
- }
- {
- Variant<int, float> v(10);
- ASSERT_TRUE(v.is<int>());
- bool b = false;
- EXPECT_TRUE(IfAnyOf<int>::Call(&v, [&b](const auto& value) { b = value; }));
- EXPECT_TRUE(b);
- float f = 0.0f;
- EXPECT_TRUE((
- IfAnyOf<int, float>::Call(&v, [&f](const auto& value) { f = value; })));
- EXPECT_FLOAT_EQ(10.f, f);
- }
- {
- Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
- ASSERT_TRUE(v.is<std::unique_ptr<int>>());
- const int* original_v = std::get<std::unique_ptr<int>>(v).get();
- std::unique_ptr<int> u(std::make_unique<int>(20));
- EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Take(&v, &u));
- ASSERT_TRUE(v.is<std::unique_ptr<int>>());
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
- EXPECT_EQ(u.get(), original_v);
- }
- {
- Variant<std::unique_ptr<DerivedType>, int> v(
- std::make_unique<DerivedType>(10));
- ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
- const DerivedType* original_v =
- std::get<std::unique_ptr<DerivedType>>(v).get();
- std::unique_ptr<BaseType> u(std::make_unique<BaseType>(20));
- EXPECT_TRUE(IfAnyOf<std::unique_ptr<DerivedType>>::Take(&v, &u));
- ASSERT_TRUE(v.is<std::unique_ptr<DerivedType>>());
- EXPECT_TRUE(std::get<std::unique_ptr<DerivedType>>(v) == nullptr);
- EXPECT_EQ(u.get(), original_v);
- }
- {
- Variant<std::unique_ptr<int>, int> v(std::make_unique<int>(10));
- ASSERT_TRUE(v.is<std::unique_ptr<int>>());
- const int* original_v = std::get<std::unique_ptr<int>>(v).get();
- std::unique_ptr<int> u(std::make_unique<int>(20));
- EXPECT_TRUE(IfAnyOf<std::unique_ptr<int>>::Call(
- &v, [&u](auto&& value) { u = std::move(value); }));
- ASSERT_TRUE(v.is<std::unique_ptr<int>>());
- EXPECT_TRUE(std::get<std::unique_ptr<int>>(v) == nullptr);
- EXPECT_EQ(u.get(), original_v);
- }
- {
- Variant<int, bool, float> v(true);
- ASSERT_TRUE(v.is<bool>());
- float f = 0.f;
- EXPECT_FALSE((IfAnyOf<int, float>::Get(&v, &f)));
- EXPECT_FLOAT_EQ(0.f, f);
- }
- {
- Variant<std::string, int> v("foo");
- ASSERT_TRUE(v.is<std::string>());
- std::string s = "bar";
- EXPECT_TRUE(IfAnyOf<std::string>::Swap(&v, &s));
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("bar", std::get<std::string>(v));
- EXPECT_EQ("foo", s);
- }
- {
- Variant<std::string, const char*> v(static_cast<const char*>("foo"));
- ASSERT_TRUE(v.is<const char*>());
- std::string s = "bar";
- EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
- ASSERT_TRUE(v.is<const char*>());
- EXPECT_EQ("foo", std::get<const char*>(v));
- EXPECT_EQ("foo", s);
- v = std::string("bar");
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_TRUE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
- ASSERT_TRUE(v.is<std::string>());
- EXPECT_EQ("bar", s);
- }
- {
- Variant<std::string, const char*> v;
- ASSERT_TRUE(v.empty());
- std::string s = "bar";
- EXPECT_FALSE((IfAnyOf<std::string, const char*>::Take(&v, &s)));
- EXPECT_EQ("bar", s);
- }
- {
- Variant<std::string, const char*> v(static_cast<const char*>("test"));
- ASSERT_TRUE(v.is<const char*>());
- std::string s;
- EXPECT_FALSE(IfAnyOf<>::Take(&v, &s));
- EXPECT_TRUE(s.empty());
- }
- }
- TEST(Variant, ConstVolatile) {
- {
- Variant<const int> v(10);
- ASSERT_TRUE(v.is<const int>());
- EXPECT_EQ(10, std::get<const int>(v));
- }
- {
- Variant<const std::string> v("test");
- ASSERT_TRUE(v.is<const std::string>());
- EXPECT_EQ("test", std::get<const std::string>(v));
- }
- {
- Variant<volatile int, std::string> v(10);
- ASSERT_TRUE(v.is<volatile int>());
- EXPECT_EQ(10, std::get<volatile int>(v));
- }
- }
- TEST(Variant, HasType) {
- EXPECT_TRUE((detail::HasType<int, int, float, bool>::value));
- EXPECT_FALSE((detail::HasType<char, int, float, bool>::value));
- EXPECT_FALSE(detail::HasType<>::value);
- EXPECT_TRUE((detail::HasType<int&, int, float, bool>::value));
- EXPECT_FALSE((detail::HasType<char&, int, float, bool>::value));
- }
- TEST(Variant, IsConstructible) {
- using ArrayType = const float[3];
- struct ImplicitBool {
- // NOLINTNEXTLINE(google-explicit-constructor)
- operator bool() const { return true; }
- };
- struct ExplicitBool {
- explicit operator bool() const { return true; }
- };
- struct NonBool {};
- struct TwoArgs {
- TwoArgs(int, bool) {}
- };
- EXPECT_FALSE((detail::IsConstructible<bool, ArrayType>::value));
- EXPECT_TRUE((detail::IsConstructible<bool, int>::value));
- EXPECT_TRUE((detail::IsConstructible<bool, ImplicitBool>::value));
- EXPECT_TRUE((detail::IsConstructible<bool, ExplicitBool>::value));
- EXPECT_FALSE((detail::IsConstructible<bool, NonBool>::value));
- EXPECT_TRUE((detail::IsConstructible<TwoArgs, int, bool>::value));
- EXPECT_FALSE((detail::IsConstructible<TwoArgs, int, std::string>::value));
- EXPECT_FALSE((detail::IsConstructible<TwoArgs, int>::value));
- }
- TEST(Variant, Set) {
- EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<int, bool,
- float>::value));
- EXPECT_TRUE(
- (detail::Set<int, bool, float>::template IsSubset<bool, float>::value));
- EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<float>::value));
- EXPECT_TRUE((detail::Set<int, bool, float>::template IsSubset<>::value));
- EXPECT_FALSE(
- (detail::Set<int, bool, float>::template IsSubset<int, bool, float,
- char>::value));
- EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<bool, float,
- char>::value));
- EXPECT_FALSE(
- (detail::Set<int, bool, float>::template IsSubset<float, char>::value));
- EXPECT_FALSE((detail::Set<int, bool, float>::template IsSubset<char>::value));
- EXPECT_TRUE(detail::Set<>::template IsSubset<>::value);
- EXPECT_FALSE(detail::Set<>::template IsSubset<int>::value);
- EXPECT_FALSE((detail::Set<>::template IsSubset<int, float>::value));
- }
|