aboutsummaryrefslogtreecommitdiff
path: root/absl/functional/overload_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/functional/overload_test.cc')
-rw-r--r--absl/functional/overload_test.cc145
1 files changed, 87 insertions, 58 deletions
diff --git a/absl/functional/overload_test.cc b/absl/functional/overload_test.cc
index 040bf83f..a2e87674 100644
--- a/absl/functional/overload_test.cc
+++ b/absl/functional/overload_test.cc
@@ -31,14 +31,15 @@
namespace {
TEST(OverloadTest, DispatchConsidersTypeWithAutoFallback) {
- auto overloaded = absl::Overload(
- [](int v) -> std::string { return absl::StrCat("int ", v); }, //
- [](double v) -> std::string { return absl::StrCat("double ", v); }, //
- [](const char* v) -> std::string { //
- return absl::StrCat("const char* ", v); //
- }, //
- [](auto v) -> std::string { return absl::StrCat("auto ", v); } //
- );
+ auto overloaded = absl::Overload{
+ [](int v) -> std::string { return absl::StrCat("int ", v); },
+ [](double v) -> std::string { return absl::StrCat("double ", v); },
+ [](const char* v) -> std::string {
+ return absl::StrCat("const char* ", v);
+ },
+ [](auto v) -> std::string { return absl::StrCat("auto ", v); },
+ };
+
EXPECT_EQ("int 1", overloaded(1));
EXPECT_EQ("double 2.5", overloaded(2.5));
EXPECT_EQ("const char* hello", overloaded("hello"));
@@ -46,32 +47,34 @@ TEST(OverloadTest, DispatchConsidersTypeWithAutoFallback) {
}
TEST(OverloadTest, DispatchConsidersNumberOfArguments) {
- auto overloaded = absl::Overload( //
- [](int a) { return a + 1; }, //
- [](int a, int b) { return a * b; }, //
- []() -> absl::string_view { return "none"; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a) { return a + 1; },
+ [](int a, int b) { return a * b; },
+ []() -> absl::string_view { return "none"; },
+ };
+
EXPECT_EQ(3, overloaded(2));
EXPECT_EQ(21, overloaded(3, 7));
EXPECT_EQ("none", overloaded());
}
TEST(OverloadTest, SupportsConstantEvaluation) {
- auto overloaded = absl::Overload( //
- [](int a) { return a + 1; }, //
- [](int a, int b) { return a * b; }, //
- []() -> absl::string_view { return "none"; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a) { return a + 1; },
+ [](int a, int b) { return a * b; },
+ []() -> absl::string_view { return "none"; },
+ };
+
static_assert(overloaded() == "none");
static_assert(overloaded(2) == 3);
static_assert(overloaded(3, 7) == 21);
}
TEST(OverloadTest, PropogatesDefaults) {
- auto overloaded = absl::Overload( //
- [](int a, int b = 5) { return a * b; }, //
- [](double c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a, int b = 5) { return a * b; },
+ [](double c) { return c; },
+ };
EXPECT_EQ(21, overloaded(3, 7));
EXPECT_EQ(35, overloaded(7));
@@ -79,53 +82,59 @@ TEST(OverloadTest, PropogatesDefaults) {
}
TEST(OverloadTest, AmbiguousWithDefaultsNotInvocable) {
- auto overloaded = absl::Overload( //
- [](int a, int b = 5) { return a * b; }, //
- [](int c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a, int b = 5) { return a * b; },
+ [](int c) { return c; },
+ };
+
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
static_assert(std::is_invocable_v<decltype(overloaded), int, int>);
}
TEST(OverloadTest, AmbiguousDuplicatesNotInvocable) {
- auto overloaded = absl::Overload( //
- [](int a) { return a; }, //
- [](int c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a) { return a; },
+ [](int c) { return c; },
+ };
+
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
}
TEST(OverloadTest, AmbiguousConversionNotInvocable) {
- auto overloaded = absl::Overload( //
- [](uint16_t a) { return a; }, //
- [](uint64_t c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](uint16_t a) { return a; },
+ [](uint64_t c) { return c; },
+ };
+
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
}
TEST(OverloadTest, AmbiguousConversionWithAutoNotInvocable) {
- auto overloaded = absl::Overload( //
- [](auto a) { return a; }, //
- [](auto c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](auto a) { return a; },
+ [](auto c) { return c; },
+ };
+
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
}
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
TEST(OverloadTest, AmbiguousConversionWithAutoAndTemplateNotInvocable) {
- auto overloaded = absl::Overload( //
- [](auto a) { return a; }, //
- []<class T>(T c) { return c; } //
- );
+ auto overloaded = absl::Overload{
+ [](auto a) { return a; },
+ []<class T>(T c) { return c; },
+ };
+
static_assert(!std::is_invocable_v<decltype(overloaded), int>);
}
TEST(OverloadTest, DispatchConsidersTypeWithTemplateFallback) {
- auto overloaded = absl::Overload( //
- [](int a) { return a; }, //
- []<class T>(T c) { return c * 2; } //
- );
+ auto overloaded = absl::Overload{
+ [](int a) { return a; },
+ []<class T>(T c) { return c * 2; },
+ };
+
EXPECT_EQ(7, overloaded(7));
EXPECT_EQ(14.0, overloaded(7.0));
}
@@ -133,20 +142,22 @@ TEST(OverloadTest, DispatchConsidersTypeWithTemplateFallback) {
#endif // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
TEST(OverloadTest, DispatchConsidersSfinae) {
- auto overloaded = absl::Overload( //
- [](auto a) -> decltype(a + 1) { return a + 1; } //
- );
+ auto overloaded = absl::Overload{
+ [](auto a) -> decltype(a + 1) { return a + 1; },
+ };
+
static_assert(std::is_invocable_v<decltype(overloaded), int>);
static_assert(!std::is_invocable_v<decltype(overloaded), std::string>);
}
TEST(OverloadTest, VariantVisitDispatchesCorrectly) {
absl::variant<int, double, std::string> v(1);
- auto overloaded = absl::Overload(
- [](int) -> absl::string_view { return "int"; }, //
- [](double) -> absl::string_view { return "double"; }, //
- [](const std::string&) -> absl::string_view { return "string"; } //
- );
+ auto overloaded = absl::Overload{
+ [](int) -> absl::string_view { return "int"; },
+ [](double) -> absl::string_view { return "double"; },
+ [](const std::string&) -> absl::string_view { return "string"; },
+ };
+
EXPECT_EQ("int", absl::visit(overloaded, v));
v = 1.1;
EXPECT_EQ("double", absl::visit(overloaded, v));
@@ -156,10 +167,11 @@ TEST(OverloadTest, VariantVisitDispatchesCorrectly) {
TEST(OverloadTest, VariantVisitWithAutoFallbackDispatchesCorrectly) {
absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
- auto overloaded = absl::Overload(
- [](const std::string& s) { return s.size(); }, //
- [](const auto& s) { return sizeof(s); } //
- );
+ auto overloaded = absl::Overload{
+ [](const std::string& s) { return s.size(); },
+ [](const auto& s) { return sizeof(s); },
+ };
+
EXPECT_EQ(4, absl::visit(overloaded, v));
v = int64_t{1};
EXPECT_EQ(8, absl::visit(overloaded, v));
@@ -167,6 +179,23 @@ TEST(OverloadTest, VariantVisitWithAutoFallbackDispatchesCorrectly) {
EXPECT_EQ(5, absl::visit(overloaded, v));
}
+// This API used to be exported as a function, so it should also work fine to
+// use parantheses when initializing it.
+TEST(OverloadTest, UseWithParentheses) {
+ const auto overloaded =
+ absl::Overload([](const std::string& s) { return s.size(); },
+ [](const auto& s) { return sizeof(s); });
+
+ absl::variant<std::string, int32_t, int64_t> v(int32_t{1});
+ EXPECT_EQ(4, absl::visit(overloaded, v));
+
+ v = int64_t{1};
+ EXPECT_EQ(8, absl::visit(overloaded, v));
+
+ v = std::string("hello");
+ EXPECT_EQ(5, absl::visit(overloaded, v));
+}
+
} // namespace
#endif