/* * Copyright 2004 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
// We can't use a literal 0 for the first argument, because C++ will allow // that to be considered a null pointer, which makes the call ambiguous.
TestBuf(Buffer(0 + 0, 10), 0, 10);
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, 3, 40);
Buffer* buf_ptr = &buf;
buf = std::move(*buf_ptr);
}
TEST(BufferTest, TestLambdaAppendPartial) { auto setter = [](rtc::ArrayView<uint8_t> av) { for (int i = 0; i != 7; ++i)
av[i] = kTestData[i]; return 7;
};
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](rtc::ArrayView<uint8_t> av) mutable { for (int i = 0; i != 15; ++i) {
av[i] = magic_number;
++magic_number;
} return 15;
};
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.data()[i], magic_number + i);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetData) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity(); const size_t old_size = buf.size();
constexpr size_t offset = 1;
buf.SetData(kTestData + 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]); for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity(); const size_t old_size = buf.size();
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]); for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnSetSize) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity(); const size_t old_size = buf.size();
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]); for (size_t i = 2; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
TEST(ZeroOnFreeBufferTest, TestZeroOnClear) {
ZeroOnFreeBuffer<uint8_t> buf(kTestData, 7); const uint8_t* old_data = buf.data(); const size_t old_capacity = buf.capacity(); const size_t old_size = buf.size();
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. for (size_t i = 0; i < old_size; i++) {
EXPECT_EQ(0, old_data[i]);
}
}
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.