1 // Copyright 2023 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/functional/overload.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <type_traits>
20
21 #include "absl/base/config.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/types/variant.h"
25
26 #if defined(ABSL_INTERNAL_CPLUSPLUS_LANG) && \
27 ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
28
29 #include "gtest/gtest.h"
30
31 namespace {
32
TEST(OverloadTest,DispatchConsidersTypeWithAutoFallback)33 TEST(OverloadTest, DispatchConsidersTypeWithAutoFallback) {
34 auto overloaded = absl::Overload{
35 [](int v) { return absl::StrCat("int ", v); },
36 [](double v) { return absl::StrCat("double ", v); },
37 [](const char* v) { return absl::StrCat("const char* ", v); },
38 [](auto v) { return absl::StrCat("auto ", v); },
39 };
40
41 EXPECT_EQ("int 1", overloaded(1));
42 EXPECT_EQ("double 2.5", overloaded(2.5));
43 EXPECT_EQ("const char* hello", overloaded("hello"));
44 EXPECT_EQ("auto 1.5", overloaded(1.5f));
45 }
46
TEST(OverloadTest,DispatchConsidersNumberOfArguments)47 TEST(OverloadTest, DispatchConsidersNumberOfArguments) {
48 auto overloaded = absl::Overload{
49 [](int a) { return a + 1; },
50 [](int a, int b) { return a * b; },
51 []() -> absl::string_view { return "none"; },
52 };
53
54 EXPECT_EQ(3, overloaded(2));
55 EXPECT_EQ(21, overloaded(3, 7));
56 EXPECT_EQ("none", overloaded());
57 }
58
TEST(OverloadTest,SupportsConstantEvaluation)59 TEST(OverloadTest, SupportsConstantEvaluation) {
60 auto overloaded = absl::Overload{
61 [](int a) { return a + 1; },
62 [](int a, int b) { return a * b; },
63 []() -> absl::string_view { return "none"; },
64 };
65
66 static_assert(overloaded() == "none");
67 static_assert(overloaded(2) == 3);
68 static_assert(overloaded(3, 7) == 21);
69 }
70
TEST(OverloadTest,PropogatesDefaults)71 TEST(OverloadTest, PropogatesDefaults) {
72 auto overloaded = absl::Overload{
73 [](int a, int b = 5) { return a * b; },
74 [](double c) { return c; },
75 };
76
77 EXPECT_EQ(21, overloaded(3, 7));
78 EXPECT_EQ(35, overloaded(7));
79 EXPECT_EQ(2.5, overloaded(2.5));
80 }
81
TEST(OverloadTest,AmbiguousWithDefaultsNotInvocable)82 TEST(OverloadTest, AmbiguousWithDefaultsNotInvocable) {
83 auto overloaded = absl::Overload{
84 [](int a, int b = 5) { return a * b; },
85 [](int c) { return c; },
86 };
87
88 static_assert(!std::is_invocable_v<decltype(overloaded), int>);
89 static_assert(std::is_invocable_v<decltype(overloaded), int, int>);
90 }
91
TEST(OverloadTest,AmbiguousDuplicatesNotInvocable)92 TEST(OverloadTest, AmbiguousDuplicatesNotInvocable) {
93 auto overloaded = absl::Overload{
94 [](int a) { return a; },
95 [](int c) { return c; },
96 };
97
98 static_assert(!std::is_invocable_v<decltype(overloaded), int>);
99 }
100
TEST(OverloadTest,AmbiguousConversionNotInvocable)101 TEST(OverloadTest, AmbiguousConversionNotInvocable) {
102 auto overloaded = absl::Overload{
103 [](uint16_t a) { return a; },
104 [](uint64_t c) { return c; },
105 };
106
107 static_assert(!std::is_invocable_v<decltype(overloaded), int>);
108 }
109
TEST(OverloadTest,AmbiguousConversionWithAutoNotInvocable)110 TEST(OverloadTest, AmbiguousConversionWithAutoNotInvocable) {
111 auto overloaded = absl::Overload{
112 [](auto a) { return a; },
113 [](auto c) { return c; },
114 };
115
116 static_assert(!std::is_invocable_v<decltype(overloaded), int>);
117 }
118
119 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
120
TEST(OverloadTest,AmbiguousConversionWithAutoAndTemplateNotInvocable)121 TEST(OverloadTest, AmbiguousConversionWithAutoAndTemplateNotInvocable) {
122 auto overloaded = absl::Overload{
123 [](auto a) { return a; },
124 []<class T>(T c) { return c; },
125 };
126
127 static_assert(!std::is_invocable_v<decltype(overloaded), int>);
128 }
129
TEST(OverloadTest,DispatchConsidersTypeWithTemplateFallback)130 TEST(OverloadTest, DispatchConsidersTypeWithTemplateFallback) {
131 auto overloaded = absl::Overload{
132 [](int a) { return a; },
133 []<class T>(T c) { return c * 2; },
134 };
135
136 EXPECT_EQ(7, overloaded(7));
137 EXPECT_EQ(14.0, overloaded(7.0));
138 }
139
140 #endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
141
TEST(OverloadTest,DispatchConsidersSfinae)142 TEST(OverloadTest, DispatchConsidersSfinae) {
143 auto overloaded = absl::Overload{
144 [](auto a) -> decltype(a + 1) { return a + 1; },
145 };
146
147 static_assert(std::is_invocable_v<decltype(overloaded), int>);
148 static_assert(!std::is_invocable_v<decltype(overloaded), std::string>);
149 }
150
TEST(OverloadTest,VariantVisitDispatchesCorrectly)151 TEST(OverloadTest, VariantVisitDispatchesCorrectly) {
152 absl::variant<int, double, std::string> v(1);
153 auto overloaded = absl::Overload{
154 [](int) -> absl::string_view { return "int"; },
155 [](double) -> absl::string_view { return "double"; },
156 [](const std::string&) -> absl::string_view { return "string"; },
157 };
158
159 EXPECT_EQ("int", absl::visit(overloaded, v));
160 v = 1.1;
161 EXPECT_EQ("double", absl::visit(overloaded, v));
162 v = "hello";
163 EXPECT_EQ("string", absl::visit(overloaded, v));
164 }
165
TEST(OverloadTest,VariantVisitWithAutoFallbackDispatchesCorrectly)166 TEST(OverloadTest, VariantVisitWithAutoFallbackDispatchesCorrectly) {
167 absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
168 auto overloaded = absl::Overload{
169 [](const std::string& s) { return s.size(); },
170 [](const auto& s) { return sizeof(s); },
171 };
172
173 EXPECT_EQ(4, absl::visit(overloaded, v));
174 v = int64_t{1};
175 EXPECT_EQ(8, absl::visit(overloaded, v));
176 v = std::string("hello");
177 EXPECT_EQ(5, absl::visit(overloaded, v));
178 }
179
180 // This API used to be exported as a function, so it should also work fine to
181 // use parantheses when initializing it.
TEST(OverloadTest,UseWithParentheses)182 TEST(OverloadTest, UseWithParentheses) {
183 const auto overloaded =
184 absl::Overload([](const std::string& s) { return s.size(); },
185 [](const auto& s) { return sizeof(s); });
186
187 absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
188 EXPECT_EQ(4, absl::visit(overloaded, v));
189
190 v = int64_t{1};
191 EXPECT_EQ(8, absl::visit(overloaded, v));
192
193 v = std::string("hello");
194 EXPECT_EQ(5, absl::visit(overloaded, v));
195 }
196
TEST(OverloadTest,HasConstexprConstructor)197 TEST(OverloadTest, HasConstexprConstructor) {
198 constexpr auto overloaded = absl::Overload{
199 [](int v) { return absl::StrCat("int ", v); },
200 [](double v) { return absl::StrCat("double ", v); },
201 [](const char* v) { return absl::StrCat("const char* ", v); },
202 [](auto v) { return absl::StrCat("auto ", v); },
203 };
204
205 EXPECT_EQ("int 1", overloaded(1));
206 EXPECT_EQ("double 2.5", overloaded(2.5));
207 EXPECT_EQ("const char* hello", overloaded("hello"));
208 EXPECT_EQ("auto 1.5", overloaded(1.5f));
209 }
210
211 } // namespace
212
213 #endif
214