TEST(BufferTest, TestMoveAssignSelf) { // Move self-assignment isn't required to produce a meaningful state, but // should not leave the object in an inconsistent state. (Such inconsistent // state could be caught by the DCHECKs and/or by the leak checker.) We need // to be sneaky when testing this; if we're doing a too-obvious // move-assign-to-self, clang's -Wself-move triggers at compile time.
Buffer buf(kTestData.data(), 3, 40);
Buffer* buf_ptr = &buf;
buf = std::move(*buf_ptr);
}
TEST(BufferTest, TestLambdaAppendPartial) { auto setter = [](std::span<uint8_t> av) {
for (int i = 0; i != 7; ++i)
av[i] = kTestData[i]; return7;
};
Buffer buf;
EXPECT_EQ(buf.AppendData(15, setter), 7u);
EXPECT_EQ(buf.size(), 7u); // Size is exactly what we wrote.
EXPECT_GE(buf.capacity(), 7u); // Capacity is valid.
EXPECT_NE(buf.data<char>(), nullptr); // Data is actually stored.
EXPECT_FALSE(buf.empty());
}
TEST(BufferTest, TestMutableLambdaSetAppend) {
uint8_t magic_number = 17; auto setter = [magic_number](std::span<uint8_t> av) mutable {
for (int i = 0; i != 15; ++i) {
av[i] = magic_number;
++magic_number;
} return15;
};
EXPECT_EQ(magic_number, 17);
Buffer buf;
EXPECT_EQ(buf.SetData(15, setter), 15u);
EXPECT_EQ(buf.AppendData(15, setter), 15u);
EXPECT_EQ(buf.size(), 30u); // Size is exactly what we wrote.
EXPECT_GE(buf.capacity(), 30u); // Capacity is valid.
EXPECT_NE(buf.data<char>(), nullptr); // Data is actually stored.
EXPECT_FALSE(buf.empty());
for (uint8_t i = 0; i != buf.size(); ++i) {
EXPECT_EQ(buf[i], magic_number + i);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetData) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData.data(), 7); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity();
constexpr size_t offset = 1; // Pointer to the last five bytes of the underlying buffer. auto to_be_zeroed = std::span<const uint8_t>(buf).subspan(2);
buf.SetData(TestDataAtIndex(offset), 2); // Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity()); // The first two elements have been overwritten, and the remaining five have // been zeroed.
EXPECT_EQ(kTestData[offset], buf[0]);
EXPECT_EQ(kTestData[offset + 1], buf[1]);
EXPECT_THAT(to_be_zeroed, Each(Eq(0)));
}
ZeroOnFreeBuffer<uint8_t> buf(kTestData.data(), 7); const uint8_t* old_data = buf.data(); auto to_be_zeroed = std::span<const uint8_t>(buf).subspan(2); const size_t old_capacity = buf.capacity();
buf.SetData(2, setter); // Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity()); // The first two elements have been overwritten, and the remaining five have // been zeroed.
EXPECT_EQ(kTestData[offset], buf[0]);
EXPECT_EQ(kTestData[offset + 1], buf[1]);
EXPECT_THAT(to_be_zeroed, Each(Eq(0)));
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetSize) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData.data(), 7); auto to_be_zeroed = std::span<const uint8_t>(buf).subspan(2); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity();
buf.SetSize(2); // Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity()); // The first two elements have not been modified and the remaining five have // been zeroed.
EXPECT_EQ(kTestData[0], buf[0]);
EXPECT_EQ(kTestData[1], buf[1]);
EXPECT_THAT(to_be_zeroed, Each(Eq(0)));
}
TEST(ZeroOnFreeBufferTest, TestZeroOnClear) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData.data(), 7); auto to_be_zeroed = std::span<const uint8_t>(buf); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity();
buf.Clear(); // Sanity checks to make sure the underlying heap memory was not reallocated.
EXPECT_EQ(old_data, buf.data());
EXPECT_EQ(old_capacity, buf.capacity()); // The underlying memory was not released but cleared.
EXPECT_THAT(to_be_zeroed, Each(Eq(0)));
}
} // namespace webrtc
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-08-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.