-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests for GenericAppendOnlyBuffer move operations added. #37667
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
|
||
using namespace chip; | ||
using namespace chip::app::DataModel; | ||
using namespace chip::app::DataModel::detail; | ||
|
||
namespace { | ||
|
||
|
@@ -204,4 +205,65 @@ TEST_F(TestMetadataList, ListBuilderConvertersWorks) | |
} | ||
} | ||
|
||
TEST_F(TestMetadataList, BufferMoveOperationsWork) | ||
{ | ||
|
||
{ | ||
ListBuilder<int> list; | ||
|
||
ASSERT_EQ(list.EnsureAppendCapacity(3), CHIP_NO_ERROR); | ||
|
||
list.Append(10); | ||
list.Append(11); | ||
list.Append(12); | ||
|
||
// Get a ListBuilder base class object | ||
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list)) }; | ||
|
||
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 }); | ||
ASSERT_FALSE(originalBuffer.IsEmpty()); | ||
|
||
/// move constructor called for the second time here | ||
GenericAppendOnlyBuffer newBuffer{ std::move(originalBuffer) }; | ||
|
||
ASSERT_EQ(originalBuffer.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move) | ||
ASSERT_TRUE(originalBuffer.IsEmpty()); // NOLINT(bugprone-use-after-move) | ||
Comment on lines
+229
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a conceited test that does something we should not expect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I was following @andreilitvin suggestion to assert original buffer (the one instance being moved from) gets its (observable) internal state cleared. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please disregard this PR in favor of this new one: #37812 I had to recreate my environment so it was much easier to create a new branch with the existing commits. |
||
|
||
ASSERT_EQ(newBuffer.Size(), size_t{ 3 }); | ||
ASSERT_FALSE(newBuffer.IsEmpty()); | ||
} | ||
|
||
{ | ||
ListBuilder<int> list1; | ||
|
||
ASSERT_EQ(list1.EnsureAppendCapacity(3), CHIP_NO_ERROR); | ||
|
||
list1.Append(10); | ||
list1.Append(11); | ||
list1.Append(12); | ||
|
||
ListBuilder<int> list2; | ||
|
||
ASSERT_EQ(list2.EnsureAppendCapacity(2), CHIP_NO_ERROR); | ||
|
||
list2.Append(20); | ||
list2.Append(21); | ||
|
||
// Get a ListBuilder base class object | ||
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list1)) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove all static cast to && There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please disregard this PR in favor of this new one: #37812 I had to recreate my environment so it was much easier to create a new branch with the existing commits. |
||
|
||
// Get another ListBuilder base class object | ||
GenericAppendOnlyBuffer anotherBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list2)) }; | ||
|
||
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 }); | ||
ASSERT_EQ(anotherBuffer.Size(), size_t{ 2 }); | ||
|
||
// move assignemnt operator called here | ||
originalBuffer = std::move(anotherBuffer); | ||
|
||
ASSERT_EQ(originalBuffer.Size(), size_t{ 2 }); | ||
ASSERT_EQ(anotherBuffer.Size(), size_t{ 0 }); // NOLINT(bugprone-use-after-move) | ||
ASSERT_TRUE(anotherBuffer.IsEmpty()); // NOLINT(bugprone-use-after-move) | ||
} | ||
} | ||
} // namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The static cast should not be needed, as it is what
std::move
does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try removing it. But list's type is not
GenericAppendOnlyBuffer
butListBuilder<int>
which in turn derives fromGenericAppendOnlyBuffer
.if I'm not mistaken, std::move(list) will cast a
ListBuilder<int> &
into aListBuilder<int> &&
, and then I still need to go fromListBuilder<int> &&
toGenericAppendOnlyBuffer &&
, and that's why static_cast is used here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should have listbuilder support the move constructor and that would in turn move the append-only. If listbuilder has no move suport, that sounds like a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please disregard this PR in favor of this new one: #37812
I had to recreate my environment so it was much easier to create a new branch with the existing commits.