Skip to content

Commit 92c1e30

Browse files
committed
Tests for GenericAppendOnlyBuffer move operations added.
1 parent 7dccded commit 92c1e30

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/app/data-model-provider/tests/TestMetadataList.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
using namespace chip;
3434
using namespace chip::app::DataModel;
35+
using namespace chip::app::DataModel::detail;
3536

3637
namespace {
3738

@@ -204,4 +205,65 @@ TEST_F(TestMetadataList, ListBuilderConvertersWorks)
204205
}
205206
}
206207

208+
TEST_F(TestMetadataList, BufferMoveOperationsWork)
209+
{
210+
211+
{
212+
ListBuilder<int> list;
213+
214+
ASSERT_EQ(list.EnsureAppendCapacity(3), CHIP_NO_ERROR);
215+
216+
list.Append(10);
217+
list.Append(11);
218+
list.Append(12);
219+
220+
// Get a ListBuilder base class object
221+
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list)) };
222+
223+
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 });
224+
ASSERT_FALSE(originalBuffer.IsEmpty());
225+
226+
/// move constructor called for the second time here
227+
GenericAppendOnlyBuffer newBuffer{ std::move(originalBuffer) };
228+
229+
ASSERT_EQ(originalBuffer.Size(), size_t{ 0 });
230+
ASSERT_TRUE(originalBuffer.IsEmpty());
231+
232+
ASSERT_EQ(newBuffer.Size(), size_t{ 3 });
233+
ASSERT_FALSE(newBuffer.IsEmpty());
234+
}
235+
236+
{
237+
ListBuilder<int> list1;
238+
239+
ASSERT_EQ(list1.EnsureAppendCapacity(3), CHIP_NO_ERROR);
240+
241+
list1.Append(10);
242+
list1.Append(11);
243+
list1.Append(12);
244+
245+
ListBuilder<int> list2;
246+
247+
ASSERT_EQ(list2.EnsureAppendCapacity(2), CHIP_NO_ERROR);
248+
249+
list2.Append(20);
250+
list2.Append(21);
251+
252+
// Get a ListBuilder base class object
253+
GenericAppendOnlyBuffer originalBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list1)) };
254+
255+
// Get another ListBuilder base class object
256+
GenericAppendOnlyBuffer anotherBuffer{ static_cast<GenericAppendOnlyBuffer &&>(std::move(list2)) };
257+
258+
ASSERT_EQ(originalBuffer.Size(), size_t{ 3 });
259+
ASSERT_EQ(anotherBuffer.Size(), size_t{ 2 });
260+
261+
// move assignemnt operator called here
262+
originalBuffer = std::move(anotherBuffer);
263+
264+
ASSERT_EQ(originalBuffer.Size(), size_t{ 2 });
265+
ASSERT_EQ(anotherBuffer.Size(), size_t{ 0 });
266+
ASSERT_TRUE(anotherBuffer.IsEmpty());
267+
}
268+
}
207269
} // namespace

0 commit comments

Comments
 (0)