summaryrefslogtreecommitdiff
path: root/absl/flags/flag_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/flag_test.cc')
-rw-r--r--absl/flags/flag_test.cc231
1 files changed, 226 insertions, 5 deletions
diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc
index 6912b546..845b4eba 100644
--- a/absl/flags/flag_test.cc
+++ b/absl/flags/flag_test.cc
@@ -61,6 +61,7 @@ void TestCallback() {}
struct UDT {
UDT() = default;
UDT(const UDT&) = default;
+ UDT& operator=(const UDT&) = default;
};
bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
std::string AbslUnparseFlag(const UDT&) { return ""; }
@@ -102,9 +103,9 @@ struct S2 {
TEST_F(FlagTest, Traits) {
EXPECT_EQ(flags::StorageKind<int>(),
- flags::FlagValueStorageKind::kOneWordAtomic);
+ flags::FlagValueStorageKind::kValueAndInitBit);
EXPECT_EQ(flags::StorageKind<bool>(),
- flags::FlagValueStorageKind::kOneWordAtomic);
+ flags::FlagValueStorageKind::kValueAndInitBit);
EXPECT_EQ(flags::StorageKind<double>(),
flags::FlagValueStorageKind::kOneWordAtomic);
EXPECT_EQ(flags::StorageKind<int64_t>(),
@@ -723,6 +724,8 @@ ABSL_FLAG(CustomUDT, test_flag_custom_udt, CustomUDT(), "test flag custom UDT");
namespace {
TEST_F(FlagTest, TestCustomUDT) {
+ EXPECT_EQ(flags::StorageKind<CustomUDT>(),
+ flags::FlagValueStorageKind::kOneWordAtomic);
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_custom_udt), CustomUDT(1, 1));
absl::SetFlag(&FLAGS_test_flag_custom_udt, CustomUDT(2, 3));
EXPECT_EQ(absl::GetFlag(FLAGS_test_flag_custom_udt), CustomUDT(2, 3));
@@ -851,7 +854,9 @@ ABSL_RETIRED_FLAG(bool, old_bool_flag, true, "old descr");
ABSL_RETIRED_FLAG(int, old_int_flag, (int)std::sqrt(10), "old descr");
ABSL_RETIRED_FLAG(std::string, old_str_flag, "", absl::StrCat("old ", "descr"));
-bool initializaion_order_fiasco_test = [] {
+namespace {
+
+bool initialization_order_fiasco_test ABSL_ATTRIBUTE_UNUSED = [] {
// Iterate over all the flags during static initialization.
// This should not trigger ASan's initialization-order-fiasco.
auto* handle1 = absl::FindCommandLineFlag("flag_on_separate_file");
@@ -862,8 +867,6 @@ bool initializaion_order_fiasco_test = [] {
return true;
}();
-namespace {
-
TEST_F(FlagTest, TestRetiredFlagRegistration) {
auto* handle = absl::FindCommandLineFlag("old_bool_flag");
EXPECT_TRUE(handle->IsOfType<bool>());
@@ -943,3 +946,221 @@ TEST_F(FlagTest, TestNonTriviallyCopyableUDT) {
}
} // namespace
+
+// --------------------------------------------------------------------
+
+namespace {
+
+enum TestE { A = 1, B = 2, C = 3 };
+
+struct EnumWrapper {
+ EnumWrapper() : e(A) {}
+
+ TestE e;
+};
+
+bool AbslParseFlag(absl::string_view, EnumWrapper*, std::string*) {
+ return true;
+}
+std::string AbslUnparseFlag(const EnumWrapper&) { return ""; }
+
+} // namespace
+
+ABSL_FLAG(EnumWrapper, test_enum_wrapper_flag, {}, "help");
+
+TEST_F(FlagTest, TesTypeWrappingEnum) {
+ EnumWrapper value = absl::GetFlag(FLAGS_test_enum_wrapper_flag);
+ EXPECT_EQ(value.e, A);
+
+ value.e = B;
+ absl::SetFlag(&FLAGS_test_enum_wrapper_flag, value);
+ value = absl::GetFlag(FLAGS_test_enum_wrapper_flag);
+ EXPECT_EQ(value.e, B);
+}
+
+// This is a compile test to ensure macros are expanded within ABSL_FLAG and
+// ABSL_DECLARE_FLAG.
+#define FLAG_NAME_MACRO(name) prefix_ ## name
+ABSL_DECLARE_FLAG(int, FLAG_NAME_MACRO(test_macro_named_flag));
+ABSL_FLAG(int, FLAG_NAME_MACRO(test_macro_named_flag), 0,
+ "Testing macro expansion within ABSL_FLAG");
+
+TEST_F(FlagTest, MacroWithinAbslFlag) {
+ EXPECT_EQ(absl::GetFlag(FLAGS_prefix_test_macro_named_flag), 0);
+ absl::SetFlag(&FLAGS_prefix_test_macro_named_flag, 1);
+ EXPECT_EQ(absl::GetFlag(FLAGS_prefix_test_macro_named_flag), 1);
+}
+
+// --------------------------------------------------------------------
+
+#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ <= 5
+#define ABSL_SKIP_OPTIONAL_BOOL_TEST_DUE_TO_GCC_BUG
+#endif
+
+#ifndef ABSL_SKIP_OPTIONAL_BOOL_TEST_DUE_TO_GCC_BUG
+ABSL_FLAG(absl::optional<bool>, optional_bool, absl::nullopt, "help");
+#endif
+ABSL_FLAG(absl::optional<int>, optional_int, {}, "help");
+ABSL_FLAG(absl::optional<double>, optional_double, 9.3, "help");
+ABSL_FLAG(absl::optional<std::string>, optional_string, absl::nullopt, "help");
+ABSL_FLAG(absl::optional<absl::Duration>, optional_duration, absl::nullopt,
+ "help");
+ABSL_FLAG(absl::optional<absl::optional<int>>, optional_optional_int,
+ absl::nullopt, "help");
+#if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
+ABSL_FLAG(std::optional<int64_t>, std_optional_int64, std::nullopt, "help");
+#endif
+
+namespace {
+
+#ifndef ABSL_SKIP_OPTIONAL_BOOL_TEST_DUE_TO_GCC_BUG
+TEST_F(FlagTest, TestOptionalBool) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_bool).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_bool), absl::nullopt);
+
+ absl::SetFlag(&FLAGS_optional_bool, false);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_bool).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_bool), false);
+
+ absl::SetFlag(&FLAGS_optional_bool, true);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_bool).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_bool), true);
+
+ absl::SetFlag(&FLAGS_optional_bool, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_bool).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_bool), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+#endif
+
+TEST_F(FlagTest, TestOptionalInt) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_int), absl::nullopt);
+
+ absl::SetFlag(&FLAGS_optional_int, 0);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_int), 0);
+
+ absl::SetFlag(&FLAGS_optional_int, 10);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_int), 10);
+
+ absl::SetFlag(&FLAGS_optional_int, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_int), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, TestOptionalDouble) {
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_double).has_value());
+ EXPECT_DOUBLE_EQ(*absl::GetFlag(FLAGS_optional_double), 9.3);
+
+ absl::SetFlag(&FLAGS_optional_double, 0.0);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_double).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_double), 0.0);
+
+ absl::SetFlag(&FLAGS_optional_double, 1.234);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_double).has_value());
+ EXPECT_DOUBLE_EQ(*absl::GetFlag(FLAGS_optional_double), 1.234);
+
+ absl::SetFlag(&FLAGS_optional_double, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_double).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_double), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, TestOptionalString) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_string).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_string), absl::nullopt);
+
+ // Setting optional string to "" leads to undefined behavior.
+
+ absl::SetFlag(&FLAGS_optional_string, " ");
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_string).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_string), " ");
+
+ absl::SetFlag(&FLAGS_optional_string, "QWERTY");
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_string).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_string), "QWERTY");
+
+ absl::SetFlag(&FLAGS_optional_string, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_string).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_string), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, TestOptionalDuration) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_duration).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_duration), absl::nullopt);
+
+ absl::SetFlag(&FLAGS_optional_duration, absl::ZeroDuration());
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_duration).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_duration), absl::Seconds(0));
+
+ absl::SetFlag(&FLAGS_optional_duration, absl::Hours(3));
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_duration).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_duration), absl::Hours(3));
+
+ absl::SetFlag(&FLAGS_optional_duration, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_duration).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_duration), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+TEST_F(FlagTest, TestOptionalOptional) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int), absl::nullopt);
+
+ absl::optional<int> nullint{absl::nullopt};
+
+ absl::SetFlag(&FLAGS_optional_optional_int, nullint);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_optional_int).has_value());
+ EXPECT_NE(absl::GetFlag(FLAGS_optional_optional_int), nullint);
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int),
+ absl::optional<absl::optional<int>>{nullint});
+
+ absl::SetFlag(&FLAGS_optional_optional_int, 0);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int), 0);
+
+ absl::SetFlag(&FLAGS_optional_optional_int, absl::optional<int>{0});
+ EXPECT_TRUE(absl::GetFlag(FLAGS_optional_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int), 0);
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int), absl::optional<int>{0});
+
+ absl::SetFlag(&FLAGS_optional_optional_int, absl::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_optional_optional_int).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_optional_optional_int), absl::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+#if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
+
+TEST_F(FlagTest, TestStdOptional) {
+ EXPECT_FALSE(absl::GetFlag(FLAGS_std_optional_int64).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_std_optional_int64), std::nullopt);
+
+ absl::SetFlag(&FLAGS_std_optional_int64, 0);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_std_optional_int64).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_std_optional_int64), 0);
+
+ absl::SetFlag(&FLAGS_std_optional_int64, 0xFFFFFFFFFF16);
+ EXPECT_TRUE(absl::GetFlag(FLAGS_std_optional_int64).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_std_optional_int64), 0xFFFFFFFFFF16);
+
+ absl::SetFlag(&FLAGS_std_optional_int64, std::nullopt);
+ EXPECT_FALSE(absl::GetFlag(FLAGS_std_optional_int64).has_value());
+ EXPECT_EQ(absl::GetFlag(FLAGS_std_optional_int64), std::nullopt);
+}
+
+// --------------------------------------------------------------------
+
+#endif
+
+} // namespace