2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/buffer.h"
|
2016-09-05 07:46:20 -07:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/array_view.h"
|
|
|
|
|
#include "rtc_base/gunit.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
#include <type_traits>
|
|
|
|
|
#include <utility>
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
namespace rtc {
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
namespace {
|
2014-05-13 18:00:26 +00:00
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
// clang-format off
|
|
|
|
|
const uint8_t kTestData[] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
|
|
|
|
|
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
void TestBuf(const Buffer& b1, size_t size, size_t capacity) {
|
|
|
|
|
EXPECT_EQ(b1.size(), size);
|
|
|
|
|
EXPECT_EQ(b1.capacity(), capacity);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestConstructEmpty) {
|
|
|
|
|
TestBuf(Buffer(), 0, 0);
|
|
|
|
|
TestBuf(Buffer(Buffer()), 0, 0);
|
|
|
|
|
TestBuf(Buffer(0), 0, 0);
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
TestBuf(Buffer(kTestData, 0), 0, 0);
|
|
|
|
|
TestBuf(Buffer(kTestData, 0, 20), 0, 20);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestConstructData) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
Buffer buf(kTestData, 7);
|
|
|
|
|
EXPECT_EQ(buf.size(), 7u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 7u);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 7));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestConstructDataWithCapacity) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
Buffer buf(kTestData, 7, 14);
|
|
|
|
|
EXPECT_EQ(buf.size(), 7u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 14u);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 7));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestConstructArray) {
|
|
|
|
|
Buffer buf(kTestData);
|
|
|
|
|
EXPECT_EQ(buf.size(), 16u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 16u);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 16));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestSetData) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
Buffer buf(kTestData + 4, 7);
|
|
|
|
|
buf.SetData(kTestData, 9);
|
|
|
|
|
EXPECT_EQ(buf.size(), 9u);
|
2016-06-20 04:47:39 -07:00
|
|
|
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
|
2016-09-05 07:46:20 -07:00
|
|
|
Buffer buf2;
|
|
|
|
|
buf2.SetData(buf);
|
|
|
|
|
EXPECT_EQ(buf.size(), 9u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 7u * 3 / 2);
|
|
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 9));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestAppendData) {
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
Buffer buf(kTestData + 4, 3);
|
|
|
|
|
buf.AppendData(kTestData + 10, 2);
|
|
|
|
|
const int8_t exp[] = {0x4, 0x5, 0x6, 0xa, 0xb};
|
|
|
|
|
EXPECT_EQ(buf, Buffer(exp));
|
2016-09-05 07:46:20 -07:00
|
|
|
Buffer buf2;
|
|
|
|
|
buf2.AppendData(buf);
|
|
|
|
|
buf2.AppendData(rtc::ArrayView<uint8_t>(buf));
|
|
|
|
|
const int8_t exp2[] = {0x4, 0x5, 0x6, 0xa, 0xb, 0x4, 0x5, 0x6, 0xa, 0xb};
|
|
|
|
|
EXPECT_EQ(buf2, Buffer(exp2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestSetAndAppendWithUnknownArg) {
|
|
|
|
|
struct TestDataContainer {
|
|
|
|
|
size_t size() const { return 3; }
|
|
|
|
|
const uint8_t* data() const { return kTestData; }
|
|
|
|
|
};
|
|
|
|
|
Buffer buf;
|
|
|
|
|
buf.SetData(TestDataContainer());
|
|
|
|
|
EXPECT_EQ(3u, buf.size());
|
|
|
|
|
EXPECT_EQ(Buffer(kTestData, 3), buf);
|
|
|
|
|
buf.AppendData(TestDataContainer());
|
|
|
|
|
EXPECT_EQ(6u, buf.size());
|
|
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 3));
|
|
|
|
|
EXPECT_EQ(0, memcmp(buf.data() + 3, kTestData, 3));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-24 09:19:06 +00:00
|
|
|
TEST(BufferTest, TestSetSizeSmaller) {
|
2014-05-13 18:00:26 +00:00
|
|
|
Buffer buf;
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
buf.SetData(kTestData, 15);
|
|
|
|
|
buf.SetSize(10);
|
|
|
|
|
EXPECT_EQ(buf.size(), 10u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf, Buffer(kTestData, 10));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2015-03-24 09:19:06 +00:00
|
|
|
TEST(BufferTest, TestSetSizeLarger) {
|
2014-05-13 18:00:26 +00:00
|
|
|
Buffer buf;
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
buf.SetData(kTestData, 15);
|
|
|
|
|
EXPECT_EQ(buf.size(), 15u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 15u);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
buf.SetSize(20);
|
|
|
|
|
EXPECT_EQ(buf.size(), 20u);
|
2016-06-20 04:47:39 -07:00
|
|
|
EXPECT_EQ(buf.capacity(), 15u * 3 / 2); // Has grown.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(0, memcmp(buf.data(), kTestData, 15));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
TEST(BufferTest, TestEnsureCapacitySmaller) {
|
|
|
|
|
Buffer buf(kTestData);
|
|
|
|
|
const char* data = buf.data<char>();
|
|
|
|
|
buf.EnsureCapacity(4);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 16u); // Hasn't shrunk.
|
|
|
|
|
EXPECT_EQ(buf.data<char>(), data); // No reallocation.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf, Buffer(kTestData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestEnsureCapacityLarger) {
|
|
|
|
|
Buffer buf(kTestData, 5);
|
|
|
|
|
buf.EnsureCapacity(10);
|
|
|
|
|
const int8_t* data = buf.data<int8_t>();
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 10u);
|
|
|
|
|
buf.AppendData(kTestData + 5, 5);
|
|
|
|
|
EXPECT_EQ(buf.data<int8_t>(), data); // No reallocation.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf, Buffer(kTestData, 10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestMoveConstruct) {
|
|
|
|
|
Buffer buf1(kTestData, 3, 40);
|
2015-05-04 14:54:55 +02:00
|
|
|
const uint8_t* data = buf1.data();
|
2016-03-11 14:40:52 -08:00
|
|
|
Buffer buf2(std::move(buf1));
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf2.size(), 3u);
|
|
|
|
|
EXPECT_EQ(buf2.capacity(), 40u);
|
2015-05-04 14:54:55 +02:00
|
|
|
EXPECT_EQ(buf2.data(), data);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf2.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
buf1.Clear();
|
|
|
|
|
EXPECT_EQ(buf1.size(), 0u);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), 0u);
|
|
|
|
|
EXPECT_EQ(buf1.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_TRUE(buf1.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestMoveAssign) {
|
|
|
|
|
Buffer buf1(kTestData, 3, 40);
|
2015-05-04 14:54:55 +02:00
|
|
|
const uint8_t* data = buf1.data();
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
Buffer buf2(kTestData);
|
2016-03-11 14:40:52 -08:00
|
|
|
buf2 = std::move(buf1);
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf2.size(), 3u);
|
|
|
|
|
EXPECT_EQ(buf2.capacity(), 40u);
|
2015-05-04 14:54:55 +02:00
|
|
|
EXPECT_EQ(buf2.data(), data);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf2.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
buf1.Clear();
|
|
|
|
|
EXPECT_EQ(buf1.size(), 0u);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), 0u);
|
|
|
|
|
EXPECT_EQ(buf1.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_TRUE(buf1.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestSwap) {
|
|
|
|
|
Buffer buf1(kTestData, 3);
|
|
|
|
|
Buffer buf2(kTestData, 6, 40);
|
2015-05-04 14:54:55 +02:00
|
|
|
uint8_t* data1 = buf1.data();
|
|
|
|
|
uint8_t* data2 = buf2.data();
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
using std::swap;
|
|
|
|
|
swap(buf1, buf2);
|
|
|
|
|
EXPECT_EQ(buf1.size(), 6u);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), 40u);
|
2015-05-04 14:54:55 +02:00
|
|
|
EXPECT_EQ(buf1.data(), data2);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf1.empty());
|
rtc::Buffer improvements
1. Constructors, SetData(), and AppendData() now accept uint8_t*,
int8_t*, and char*. Previously, they accepted void*, meaning that
any kind of pointer was accepted. I think requiring an explicit
cast in cases where the input array isn't already of a byte-sized
type is a better compromise between convenience and safety.
2. data() can now return a uint8_t* instead of a char*, which seems
more appropriate for a byte array, and is harder to mix up with
zero-terminated C strings. data<int8_t>() is also available so
that callers that want that type instead won't have to cast, as
is data<char>() (which remains the default until all existing
callers have been fixed).
3. Constructors, SetData(), and AppendData() now accept arrays
natively, not just decayed to pointers. The advantage of this is
that callers don't have to pass the size separately.
4. There are new constructors that allow setting size and capacity
without initializing the array. Previously, this had to be done
separately after construction.
5. Instead of TransferTo(), Buffer now supports swap(), and move
construction and assignment, and has a Pass() method that works
just like std::move(). (The Pass method is modeled after
scoped_ptr::Pass().)
R=jmarusic@webrtc.org, tommi@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/42989004
Cr-Commit-Position: refs/heads/master@{#9033}
2015-04-20 14:03:07 +02:00
|
|
|
EXPECT_EQ(buf2.size(), 3u);
|
|
|
|
|
EXPECT_EQ(buf2.capacity(), 3u);
|
2015-05-04 14:54:55 +02:00
|
|
|
EXPECT_EQ(buf2.data(), data1);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf2.empty());
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-19 02:38:32 -08:00
|
|
|
TEST(BufferTest, TestClear) {
|
|
|
|
|
Buffer buf;
|
|
|
|
|
buf.SetData(kTestData, 15);
|
|
|
|
|
EXPECT_EQ(buf.size(), 15u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 15u);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-19 02:38:32 -08:00
|
|
|
const char* data = buf.data<char>();
|
|
|
|
|
buf.Clear();
|
|
|
|
|
EXPECT_EQ(buf.size(), 0u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 15u); // Hasn't shrunk.
|
|
|
|
|
EXPECT_EQ(buf.data<char>(), data); // No reallocation.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_TRUE(buf.empty());
|
2016-02-19 02:38:32 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-24 01:05:56 -08:00
|
|
|
TEST(BufferTest, TestLambdaSetAppend) {
|
|
|
|
|
auto setter = [](rtc::ArrayView<uint8_t> av) {
|
|
|
|
|
for (int i = 0; i != 15; ++i)
|
|
|
|
|
av[i] = kTestData[i];
|
|
|
|
|
return 15;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Buffer buf1;
|
|
|
|
|
buf1.SetData(kTestData, 15);
|
|
|
|
|
buf1.AppendData(kTestData, 15);
|
|
|
|
|
|
|
|
|
|
Buffer buf2;
|
|
|
|
|
EXPECT_EQ(buf2.SetData(15, setter), 15u);
|
|
|
|
|
EXPECT_EQ(buf2.AppendData(15, setter), 15u);
|
|
|
|
|
EXPECT_EQ(buf1, buf2);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), buf2.capacity());
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf1.empty());
|
|
|
|
|
EXPECT_FALSE(buf2.empty());
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestLambdaSetAppendSigned) {
|
|
|
|
|
auto setter = [](rtc::ArrayView<int8_t> av) {
|
|
|
|
|
for (int i = 0; i != 15; ++i)
|
|
|
|
|
av[i] = kTestData[i];
|
|
|
|
|
return 15;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Buffer buf1;
|
|
|
|
|
buf1.SetData(kTestData, 15);
|
|
|
|
|
buf1.AppendData(kTestData, 15);
|
|
|
|
|
|
|
|
|
|
Buffer buf2;
|
|
|
|
|
EXPECT_EQ(buf2.SetData<int8_t>(15, setter), 15u);
|
|
|
|
|
EXPECT_EQ(buf2.AppendData<int8_t>(15, setter), 15u);
|
|
|
|
|
EXPECT_EQ(buf1, buf2);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), buf2.capacity());
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf1.empty());
|
|
|
|
|
EXPECT_FALSE(buf2.empty());
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestLambdaAppendEmpty) {
|
|
|
|
|
auto setter = [](rtc::ArrayView<uint8_t> av) {
|
|
|
|
|
for (int i = 0; i != 15; ++i)
|
|
|
|
|
av[i] = kTestData[i];
|
|
|
|
|
return 15;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Buffer buf1;
|
|
|
|
|
buf1.SetData(kTestData, 15);
|
|
|
|
|
|
|
|
|
|
Buffer buf2;
|
|
|
|
|
EXPECT_EQ(buf2.AppendData(15, setter), 15u);
|
|
|
|
|
EXPECT_EQ(buf1, buf2);
|
|
|
|
|
EXPECT_EQ(buf1.capacity(), buf2.capacity());
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf1.empty());
|
|
|
|
|
EXPECT_FALSE(buf2.empty());
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-24 01:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-24 01:05:56 -08:00
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i != buf.size(); ++i) {
|
|
|
|
|
EXPECT_EQ(buf.data()[i], magic_number + i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 09:36:37 -08:00
|
|
|
TEST(BufferTest, TestBracketRead) {
|
|
|
|
|
Buffer buf(kTestData, 7);
|
|
|
|
|
EXPECT_EQ(buf.size(), 7u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 7u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-29 09:36:37 -08:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i != 7u; ++i) {
|
|
|
|
|
EXPECT_EQ(buf[i], kTestData[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestBracketReadConst) {
|
|
|
|
|
Buffer buf(kTestData, 7);
|
|
|
|
|
EXPECT_EQ(buf.size(), 7u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 7u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-29 09:36:37 -08:00
|
|
|
|
|
|
|
|
const Buffer& cbuf = buf;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i != 7u; ++i) {
|
|
|
|
|
EXPECT_EQ(cbuf[i], kTestData[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestBracketWrite) {
|
|
|
|
|
Buffer buf(7);
|
|
|
|
|
EXPECT_EQ(buf.size(), 7u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 7u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-02-29 09:36:37 -08:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i != 7u; ++i) {
|
|
|
|
|
buf[i] = kTestData[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i != 7u; ++i) {
|
|
|
|
|
EXPECT_EQ(buf[i], kTestData[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-05 07:38:06 -07:00
|
|
|
TEST(BufferTest, TestBeginEnd) {
|
|
|
|
|
const Buffer cbuf(kTestData);
|
|
|
|
|
Buffer buf(kTestData);
|
2017-10-24 10:08:26 -07:00
|
|
|
auto* b1 = cbuf.begin();
|
2017-04-05 07:38:06 -07:00
|
|
|
for (auto& x : buf) {
|
|
|
|
|
EXPECT_EQ(*b1, x);
|
|
|
|
|
++b1;
|
|
|
|
|
++x;
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(cbuf.end(), b1);
|
2017-10-24 10:08:26 -07:00
|
|
|
auto* b2 = buf.begin();
|
2017-04-05 07:38:06 -07:00
|
|
|
for (auto& y : cbuf) {
|
|
|
|
|
EXPECT_EQ(*b2, y + 1);
|
|
|
|
|
++b2;
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(buf.end(), b2);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 08:00:22 -07:00
|
|
|
TEST(BufferTest, TestInt16) {
|
|
|
|
|
static constexpr int16_t test_data[] = {14, 15, 16, 17, 18};
|
|
|
|
|
BufferT<int16_t> buf(test_data);
|
|
|
|
|
EXPECT_EQ(buf.size(), 5u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 5u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-04-29 08:00:22 -07:00
|
|
|
for (size_t i = 0; i != buf.size(); ++i) {
|
|
|
|
|
EXPECT_EQ(test_data[i], buf[i]);
|
|
|
|
|
}
|
|
|
|
|
BufferT<int16_t> buf2(test_data);
|
|
|
|
|
EXPECT_EQ(buf, buf2);
|
|
|
|
|
buf2[0] = 9;
|
|
|
|
|
EXPECT_NE(buf, buf2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestFloat) {
|
|
|
|
|
static constexpr float test_data[] = {14, 15, 16, 17, 18};
|
|
|
|
|
BufferT<float> buf;
|
|
|
|
|
EXPECT_EQ(buf.size(), 0u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 0u);
|
|
|
|
|
EXPECT_EQ(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_TRUE(buf.empty());
|
2016-04-29 08:00:22 -07:00
|
|
|
buf.SetData(test_data);
|
|
|
|
|
EXPECT_EQ(buf.size(), 5u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 5u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-04-29 08:00:22 -07:00
|
|
|
float* p1 = buf.data();
|
|
|
|
|
while (buf.data() == p1) {
|
|
|
|
|
buf.AppendData(test_data);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(buf.size(), buf.capacity());
|
|
|
|
|
EXPECT_GT(buf.size(), 5u);
|
|
|
|
|
EXPECT_EQ(buf.size() % 5, 0u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
|
|
|
|
for (size_t i = 0; i != buf.size(); ++i) {
|
|
|
|
|
EXPECT_EQ(test_data[i % 5], buf[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(BufferTest, TestStruct) {
|
|
|
|
|
struct BloodStone {
|
|
|
|
|
bool blood;
|
|
|
|
|
const char* stone;
|
|
|
|
|
};
|
|
|
|
|
BufferT<BloodStone> buf(4);
|
|
|
|
|
EXPECT_EQ(buf.size(), 4u);
|
|
|
|
|
EXPECT_EQ(buf.capacity(), 4u);
|
|
|
|
|
EXPECT_NE(buf.data(), nullptr);
|
2016-08-31 08:40:04 -07:00
|
|
|
EXPECT_FALSE(buf.empty());
|
2016-04-29 08:00:22 -07:00
|
|
|
BufferT<BloodStone*> buf2(4);
|
|
|
|
|
for (size_t i = 0; i < buf2.size(); ++i) {
|
|
|
|
|
buf2[i] = &buf[i];
|
|
|
|
|
}
|
|
|
|
|
static const char kObsidian[] = "obsidian";
|
|
|
|
|
buf2[2]->stone = kObsidian;
|
|
|
|
|
EXPECT_EQ(kObsidian, buf[2].stone);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-07 20:02:26 +01:00
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ZeroOnFreeBufferTest, TestZeroOnSetDataFromSetter) {
|
|
|
|
|
static constexpr size_t offset = 1;
|
|
|
|
|
const auto setter = [](rtc::ArrayView<uint8_t> av) {
|
|
|
|
|
for (int i = 0; i != 2; ++i)
|
|
|
|
|
av[i] = kTestData[offset + i];
|
|
|
|
|
return 2;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
} // namespace rtc
|