Add tests for new ByteBufferWriter accessors (and fix bug)

Bug: webrtc:15665
Change-Id: Iedf39afcec52861b501b016a7abcf7b1bcfb770c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/331060
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Sergey Sukhanov <sergeysu@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41361}
This commit is contained in:
Harald Alvestrand 2023-12-11 21:38:51 +00:00 committed by WebRTC LUCI CQ
parent 601ac2eea8
commit 776fe6d923
2 changed files with 20 additions and 2 deletions

View file

@ -51,7 +51,9 @@ class ByteBufferWriterT {
absl::string_view DataAsStringView() const {
return absl::string_view(reinterpret_cast<const char*>(Data()), Length());
}
char* DataAsCharPointer() const { return reinterpret_cast<char*>(Data()); }
const char* DataAsCharPointer() const {
return reinterpret_cast<const char*>(Data());
}
// Write value to the buffer. Resizes the buffer when it is
// neccessary.
@ -91,7 +93,7 @@ class ByteBufferWriterT {
WriteBytes(&last_byte, 1);
}
void WriteString(absl::string_view val) {
WriteBytes(val.data(), val.size());
WriteBytes(reinterpret_cast<const value_type*>(val.data()), val.size());
}
void WriteBytes(const value_type* val, size_t len) {
buffer_.AppendData(val, len);

View file

@ -16,10 +16,26 @@
#include "rtc_base/arraysize.h"
#include "rtc_base/byte_order.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace rtc {
using ::testing::ElementsAre;
TEST(ByteBufferTest, WriterAccessors) {
// To be changed into ByteBufferWriter when base type is converted.
ByteBufferWriterT<BufferT<uint8_t>> buffer;
buffer.WriteString("abc");
EXPECT_EQ(buffer.Length(), 3U);
EXPECT_THAT(buffer.DataView(), ElementsAre('a', 'b', 'c'));
EXPECT_EQ(absl::string_view("abc"), buffer.DataAsStringView());
buffer.WriteUInt8(0);
EXPECT_STREQ(buffer.DataAsCharPointer(), "abc");
EXPECT_STREQ(reinterpret_cast<const char*>(buffer.Data()), "abc");
}
TEST(ByteBufferTest, TestByteOrder) {
uint16_t n16 = 1;
uint32_t n32 = 1;