songgaoye commented on code in PR #3724:
URL: https://github.com/apache/brpc/pull/3724#discussion_r4570768623
##########
test/optional_unittest.cpp:
##########
@@ -105,6 +139,102 @@ TEST(OptionalTest, move) {
butil::optional<int> empty_move_assign;
empty_move_assign = std::move(empty);
ASSERT_FALSE(empty_move_assign);
+
+ butil::optional<std::string> string_source(std::string(128, 'x'));
+ butil::optional<std::string> string_moved(std::move(string_source));
+ ASSERT_TRUE(string_source);
+ ASSERT_EQ(std::string(128, 'x'), *string_moved);
+
+ butil::optional<std::string> string_move_source(std::string(128, 'y'));
+ string_moved = std::move(string_move_source);
+ ASSERT_TRUE(string_move_source);
+ ASSERT_TRUE(string_moved);
+ ASSERT_EQ(std::string(128, 'y'), *string_moved);
+}
+
+TEST(OptionalTest, nontrivial_lifetime) {
+ LifetimeTracker::Reset();
+ {
+ butil::optional<LifetimeTracker> source(butil::in_place);
+ butil::optional<LifetimeTracker> copied(source);
+ butil::optional<LifetimeTracker> moved(std::move(source));
+ butil::optional<LifetimeTracker> assigned;
+ assigned = copied;
+ assigned = butil::nullopt;
+ }
+ ASSERT_EQ(0, LifetimeTracker::live);
+ ASSERT_EQ(LifetimeTracker::constructed, LifetimeTracker::destroyed);
+}
+
+TEST(OptionalTest, nontrivial_assignment_states) {
+ const std::string value(128, 'x');
+ butil::optional<std::string> source(value);
+ butil::optional<std::string> empty;
+ butil::optional<std::string> target;
+
+ target = empty;
+ ASSERT_FALSE(target);
+ target = source;
+ ASSERT_EQ(value, *target);
+ target = source;
+ ASSERT_EQ(value, *target);
+ target = empty;
+ ASSERT_FALSE(target);
+
+ target = std::move(empty);
+ ASSERT_FALSE(target);
+ target = std::move(source);
+ ASSERT_EQ(value, *target);
+ source = value;
+ target = std::move(source);
+ ASSERT_EQ(value, *target);
+ target = std::move(empty);
+ ASSERT_FALSE(target);
+}
+
+TEST(OptionalTest, move_only_value) {
+ butil::optional<std::unique_ptr<int>> source(std::unique_ptr<int>(new
int(42)));
+ butil::optional<std::unique_ptr<int>> moved(std::move(source));
+ ASSERT_EQ(42, **moved);
+ butil::optional<std::unique_ptr<int>> assigned;
+ assigned = std::move(moved);
+ ASSERT_EQ(42, **assigned);
+}
+
+struct CopyConstructOnly {
+ CopyConstructOnly() = default;
+ CopyConstructOnly(const CopyConstructOnly&) = default;
+ CopyConstructOnly(CopyConstructOnly&&) = delete;
+ CopyConstructOnly& operator=(const CopyConstructOnly&) = delete;
+ CopyConstructOnly& operator=(CopyConstructOnly&&) = delete;
+};
+
+struct AssignOnly {
+ AssignOnly() = default;
+ AssignOnly(const AssignOnly&) = delete;
+ AssignOnly(AssignOnly&&) = delete;
+ AssignOnly& operator=(const AssignOnly&) = default;
+ AssignOnly& operator=(AssignOnly&&) = default;
+};
+
+TEST(OptionalTest, copy_move_traits_follow_value_type) {
+ using MoveOnly = butil::optional<std::unique_ptr<int>>;
+ static_assert(!std::is_copy_constructible<MoveOnly>::value, "");
+ static_assert(!std::is_copy_assignable<MoveOnly>::value, "");
+ static_assert(std::is_move_constructible<MoveOnly>::value, "");
+ static_assert(std::is_move_assignable<MoveOnly>::value, "");
+
+ using CopyOnly = butil::optional<CopyConstructOnly>;
+ static_assert(std::is_copy_constructible<CopyOnly>::value, "");
+ static_assert(std::is_move_constructible<CopyOnly>::value, "");
Review Comment:
This is intentional. CopyOnly can be constructed from an rvalue without
requiring CopyConstructOnly itself to be move-constructible. Verified that this
assertion compiles under C++14.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]