1 #include <sdbusplus/utility/dedup_variant.hpp>
2 #include <sdbusplus/utility/merge_variants.hpp>
3 #include <sdbusplus/utility/type_traits.hpp>
4
5 #include <type_traits>
6
7 #include <gmock/gmock.h>
8
9 namespace
10 {
11
12 using sdbusplus::utility::array_to_ptr_t;
13 using std::is_same_v;
14
TEST(TypeTraits,Basic)15 TEST(TypeTraits, Basic)
16 {
17 static_assert(is_same_v<char, array_to_ptr_t<char, char>>,
18 "array_to_ptr_t<char, char> != char");
19
20 static_assert(is_same_v<char*, array_to_ptr_t<char, char*>>,
21 "array_to_ptr_t<char, char*> != char*");
22
23 static_assert(is_same_v<char*, array_to_ptr_t<char, char[100]>>,
24 "array_to_ptr_t<char, char[100]> != char*");
25
26 static_assert(is_same_v<char[100], array_to_ptr_t<int, char[100]>>,
27 "array_to_ptr_t<int, char[100]> != char[100]");
28 }
29
TEST(TypeTraits,HasMemberFind)30 TEST(TypeTraits, HasMemberFind)
31 {
32 using sdbusplus::utility::has_member_find_v;
33 using namespace testing;
34
35 ASSERT_THAT((has_member_find_v<std::map<std::string, int>>), Eq(true));
36 ASSERT_THAT((has_member_find_v<std::vector<std::pair<std::string, int>>>),
37 Eq(false));
38
39 struct Foo
40 {
41 using value_type = std::pair<int, int>;
42
43 void find(std::tuple_element_t<0, value_type>) {}
44 };
45
46 struct Bar
47 {};
48
49 ASSERT_THAT(has_member_find_v<Foo>, Eq(true));
50 ASSERT_THAT(has_member_find_v<Foo&>, Eq(true));
51 ASSERT_THAT(has_member_find_v<const Foo&>, Eq(true));
52
53 ASSERT_THAT(has_member_find_v<Bar>, Eq(false));
54 }
55
TEST(TypeTraits,HasMemberContains)56 TEST(TypeTraits, HasMemberContains)
57 {
58 using sdbusplus::utility::has_member_contains_v;
59 using namespace testing;
60
61 // std::map has member_contains from c++20
62 ASSERT_THAT((has_member_contains_v<std::map<std::string, int>>), Eq(true));
63 ASSERT_THAT(
64 (has_member_contains_v<std::vector<std::pair<std::string, int>>>),
65 Eq(false));
66
67 struct Foo
68 {
69 using value_type = std::pair<int, int>;
70
71 void contains(std::tuple_element_t<0, value_type>) {}
72 };
73
74 struct Bar
75 {};
76
77 ASSERT_THAT(has_member_contains_v<Foo>, Eq(true));
78 ASSERT_THAT(has_member_contains_v<Foo&>, Eq(true));
79 ASSERT_THAT(has_member_contains_v<const Foo&>, Eq(true));
80
81 ASSERT_THAT(has_member_contains_v<Bar>, Eq(false));
82 }
83
TEST(TypeTraits,IsOptional)84 TEST(TypeTraits, IsOptional)
85 {
86 using sdbusplus::utility::is_optional;
87 using sdbusplus::utility::is_optional_v;
88
89 ASSERT_TRUE(is_optional<std::optional<int>>::value);
90 ASSERT_TRUE(is_optional<std::optional<int>&>::value);
91 ASSERT_FALSE(is_optional<int>::value);
92
93 ASSERT_TRUE(is_optional_v<std::optional<int>>);
94 ASSERT_TRUE(is_optional_v<std::optional<int>&>);
95 ASSERT_FALSE(is_optional_v<int>);
96 }
97
98 // Tests for dedup_variant.
99 static_assert(std::is_same_v<std::variant<size_t>,
100 sdbusplus::utility::dedup_variant_t<size_t>>);
101 static_assert(
102 std::is_same_v<std::variant<char, size_t>,
103 sdbusplus::utility::dedup_variant_t<char, size_t>>);
104 static_assert(std::is_same_v<
105 std::variant<uint32_t, uint64_t>,
106 sdbusplus::utility::dedup_variant_t<uint32_t, uint64_t, size_t>>);
107
108 // Tests for merge_variants.
109 static_assert(
110 std::is_same_v<std::variant<size_t>,
111 sdbusplus::utility::merge_variants_t<std::variant<size_t>>>);
112 static_assert(std::is_same_v<std::variant<char, uint32_t, uint64_t>,
113 sdbusplus::utility::merge_variants_t<
114 std::variant<char, uint32_t>,
115 std::variant<uint32_t, uint64_t, size_t>>>);
116
117 } // namespace
118