// Copyright 2014 The Chromium OS Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include #include #include #include #include #include #include using brillo::Any; TEST(Any, Empty) { Any val; EXPECT_TRUE(val.IsEmpty()); Any val2 = val; EXPECT_TRUE(val.IsEmpty()); EXPECT_TRUE(val2.IsEmpty()); Any val3 = std::move(val); EXPECT_TRUE(val.IsEmpty()); EXPECT_TRUE(val3.IsEmpty()); } TEST(Any, SimpleTypes) { Any val(20); EXPECT_FALSE(val.IsEmpty()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_EQ(20, val.Get()); Any val2(3.1415926); EXPECT_FALSE(val2.IsEmpty()); EXPECT_TRUE(val2.IsTypeCompatible()); EXPECT_FALSE(val2.IsTypeCompatible()); EXPECT_DOUBLE_EQ(3.1415926, val2.Get()); Any val3(std::string("blah")); EXPECT_TRUE(val3.IsTypeCompatible()); EXPECT_EQ("blah", val3.Get()); } TEST(Any, Clear) { Any val('x'); EXPECT_FALSE(val.IsEmpty()); EXPECT_EQ('x', val.Get()); val.Clear(); EXPECT_TRUE(val.IsEmpty()); } TEST(Any, Assignments) { Any val(20); EXPECT_EQ(20, val.Get()); val = 3.1415926; EXPECT_FALSE(val.IsEmpty()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_DOUBLE_EQ(3.1415926, val.Get()); val = std::string("blah"); EXPECT_EQ("blah", val.Get()); Any val2; EXPECT_TRUE(val2.IsEmpty()); val2 = val; EXPECT_FALSE(val.IsEmpty()); EXPECT_FALSE(val2.IsEmpty()); EXPECT_EQ("blah", val.Get()); EXPECT_EQ("blah", val2.Get()); val.Clear(); EXPECT_TRUE(val.IsEmpty()); EXPECT_EQ("blah", val2.Get()); val2.Clear(); EXPECT_TRUE(val2.IsEmpty()); val = std::vector{100, 20, 3}; auto v = val.Get>(); EXPECT_EQ(100, v[0]); EXPECT_EQ(20, v[1]); EXPECT_EQ(3, v[2]); val2 = std::move(val); EXPECT_TRUE(val.IsEmpty()); EXPECT_TRUE(val2.IsTypeCompatible>()); EXPECT_EQ(3, val2.Get>().size()); val = val2; EXPECT_TRUE(val.IsTypeCompatible>()); EXPECT_TRUE(val2.IsTypeCompatible>()); EXPECT_EQ(3, val.Get>().size()); EXPECT_EQ(3, val2.Get>().size()); } TEST(Any, Enums) { enum class Dummy { foo, bar, baz }; Any val(Dummy::bar); EXPECT_FALSE(val.IsEmpty()); EXPECT_TRUE(val.IsConvertibleToInteger()); EXPECT_EQ(Dummy::bar, val.Get()); EXPECT_EQ(1, val.GetAsInteger()); val = Dummy::baz; EXPECT_EQ(2, val.GetAsInteger()); val = Dummy::foo; EXPECT_EQ(0, val.GetAsInteger()); } TEST(Any, Integers) { Any val(14); EXPECT_TRUE(val.IsConvertibleToInteger()); EXPECT_EQ(14, val.Get()); EXPECT_EQ(14, val.GetAsInteger()); val = '\x40'; EXPECT_TRUE(val.IsConvertibleToInteger()); EXPECT_EQ(64, val.Get()); EXPECT_EQ(64, val.GetAsInteger()); val = static_cast(65535); EXPECT_TRUE(val.IsConvertibleToInteger()); EXPECT_EQ(65535, val.Get()); EXPECT_EQ(65535, val.GetAsInteger()); val = static_cast(0xFFFFFFFFFFFFFFFFULL); EXPECT_TRUE(val.IsConvertibleToInteger()); EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, val.Get()); EXPECT_EQ(-1, val.GetAsInteger()); val = "abc"; EXPECT_FALSE(val.IsConvertibleToInteger()); int a = 5; val = &a; EXPECT_FALSE(val.IsConvertibleToInteger()); } TEST(Any, Pointers) { Any val("abc"); // const char* EXPECT_FALSE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_FALSE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_STREQ("abc", val.Get()); int a = 10; val = &a; EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_EQ(10, *val.Get()); *val.Get() = 3; EXPECT_EQ(3, a); } TEST(Any, Arrays) { // The following test are here to validate the array-to-pointer decay rules. // Since Any does not store the contents of a C-style array, just a pointer // to the data, putting array data into Any could be dangerous. // Make sure the array's lifetime exceeds that of an Any containing the // pointer to the array data. // If you want to store the array with data, use corresponding value types // such as std::vector or a struct containing C-style array as a member. int int_array[] = {1, 2, 3}; // int* Any val = int_array; EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_EQ(3, val.Get()[2]); const int const_int_array[] = {10, 20, 30}; // const int* val = const_int_array; EXPECT_FALSE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_FALSE(val.IsTypeCompatible()); EXPECT_TRUE(val.IsTypeCompatible()); EXPECT_EQ(30, val.Get()[2]); } TEST(Any, References) { // Passing references to object via Any might be error-prone or the // semantics could be unfamiliar to other developers. In many cases, // using pointers instead of references are more conventional and easier // to understand. Even though the cases of passing references are quite // explicit on both storing and retrieving ends, you might want to // use pointers instead anyway. int a = 5; Any val(std::ref(a)); // int& EXPECT_EQ(5, val.Get>().get()); val.Get>().get() = 7; EXPECT_EQ(7, val.Get>().get()); EXPECT_EQ(7, a); Any val2(std::cref(a)); // const int& EXPECT_EQ(7, val2.Get>().get()); a = 10; EXPECT_EQ(10, val.Get>().get()); EXPECT_EQ(10, val2.Get>().get()); } TEST(Any, CustomTypes) { struct Person { std::string name; int age; }; Any val(Person{"Jack", 40}); Any val2 = val; EXPECT_EQ("Jack", val.Get().name); val.GetPtr()->name = "Joe"; val.GetPtr()->age /= 2; EXPECT_EQ("Joe", val.Get().name); EXPECT_EQ(20, val.Get().age); EXPECT_EQ("Jack", val2.Get().name); EXPECT_EQ(40, val2.Get().age); } TEST(Any, Swap) { Any val(12); Any val2(2.7); EXPECT_EQ(12, val.Get()); EXPECT_EQ(2.7, val2.Get()); val.Swap(val2); EXPECT_EQ(2.7, val.Get()); EXPECT_EQ(12, val2.Get()); std::swap(val, val2); EXPECT_EQ(12, val.Get()); EXPECT_EQ(2.7, val2.Get()); } TEST(Any, TypeMismatch) { Any val(12); EXPECT_DEATH(val.Get(), "Requesting value of type 'double' from variant containing " "'int'"); val = std::string("123"); EXPECT_DEATH(val.GetAsInteger(), "Unable to convert value of type 'std::.*' to integer"); Any empty; EXPECT_DEATH(empty.GetAsInteger(), "Must not be called on an empty Any"); } TEST(Any, TryGet) { Any val(12); Any empty; EXPECT_EQ("dummy", val.TryGet("dummy")); EXPECT_EQ(12, val.TryGet(17)); EXPECT_EQ(17, empty.TryGet(17)); } TEST(Any, Compare_Int) { Any int1{12}; Any int2{12}; Any int3{20}; EXPECT_EQ(int1, int2); EXPECT_NE(int2, int3); } TEST(Any, Compare_String) { Any str1{std::string{"foo"}}; Any str2{std::string{"foo"}}; Any str3{std::string{"bar"}}; EXPECT_EQ(str1, str2); EXPECT_NE(str2, str3); } TEST(Any, Compare_Array) { Any vec1{std::vector{1, 2}}; Any vec2{std::vector{1, 2}}; Any vec3{std::vector{1, 2, 3}}; EXPECT_EQ(vec1, vec2); EXPECT_NE(vec2, vec3); } TEST(Any, Compare_Empty) { Any empty1; Any empty2; Any int1{1}; EXPECT_EQ(empty1, empty2); EXPECT_NE(int1, empty1); EXPECT_NE(empty2, int1); } TEST(Any, Compare_NonComparable) { struct Person { std::string name; int age; }; Any person1(Person{"Jack", 40}); Any person2 = person1; Any person3(Person{"Jill", 20}); EXPECT_NE(person1, person2); EXPECT_NE(person1, person3); EXPECT_NE(person2, person3); } TEST(Any, GetUndecoratedTypeName) { Any val; EXPECT_TRUE(val.GetUndecoratedTypeName().empty()); val = 1; EXPECT_EQ(brillo::GetUndecoratedTypeName(), val.GetUndecoratedTypeName()); val = 3.1415926; EXPECT_EQ(brillo::GetUndecoratedTypeName(), val.GetUndecoratedTypeName()); val = std::string("blah"); EXPECT_EQ(brillo::GetUndecoratedTypeName(), val.GetUndecoratedTypeName()); }