diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-18 01:54:55 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-18 01:54:55 +0000 |
commit | e609bb94b0cf64a4373261854cfa45cfa87f6004 (patch) | |
tree | c8c18ff14831d9a2cf0ea1c7f7993c3bd35dc996 /remoting/base | |
parent | 7ed9caae9a7f7117d8d20bc2057d434ccf192908 (diff) | |
download | chromium_src-e609bb94b0cf64a4373261854cfa45cfa87f6004.zip chromium_src-e609bb94b0cf64a4373261854cfa45cfa87f6004.tar.gz chromium_src-e609bb94b0cf64a4373261854cfa45cfa87f6004.tar.bz2 |
Use CompoundBuffer in MessageDecoder.
TEST=Unittests
BUG=None
Review URL: http://codereview.chromium.org/5067001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66574 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/base')
-rw-r--r-- | remoting/base/compound_buffer.cc | 43 | ||||
-rw-r--r-- | remoting/base/compound_buffer.h | 4 | ||||
-rw-r--r-- | remoting/base/compound_buffer_unittest.cc | 37 |
3 files changed, 84 insertions, 0 deletions
diff --git a/remoting/base/compound_buffer.cc b/remoting/base/compound_buffer.cc index 9e8b8c3..2808f6d 100644 --- a/remoting/base/compound_buffer.cc +++ b/remoting/base/compound_buffer.cc @@ -88,6 +88,49 @@ void CompoundBuffer::PrependCopyOf(const char* data, int size) { Prepend(buffer, size); } +void CompoundBuffer::CropFront(int bytes) { + CHECK(!locked_); + + if (total_bytes_ <= bytes) { + Clear(); + return; + } + + total_bytes_ -= bytes; + while (chunks_.size() > 0 && chunks_.front().size <= bytes) { + bytes -= chunks_.front().size; + chunks_.pop_front(); + } + if (chunks_.size() > 0 && bytes > 0) { + chunks_.front().start += bytes; + chunks_.front().size -= bytes; + DCHECK_GT(chunks_.front().size, 0); + bytes = 0; + } + DCHECK_EQ(bytes, 0); +} + +void CompoundBuffer::CropBack(int bytes) { + CHECK(!locked_); + + if (total_bytes_ <= bytes) { + Clear(); + return; + } + + total_bytes_ -= bytes; + while (chunks_.size() > 0 && chunks_.back().size <= bytes) { + bytes -= chunks_.back().size; + chunks_.pop_back(); + } + if (chunks_.size() > 0 && bytes > 0) { + chunks_.back().size -= bytes; + DCHECK_GT(chunks_.back().size, 0); + bytes = 0; + } + DCHECK_EQ(bytes, 0); +} + void CompoundBuffer::Lock() { locked_ = true; } diff --git a/remoting/base/compound_buffer.h b/remoting/base/compound_buffer.h index 050182a..1b0a546 100644 --- a/remoting/base/compound_buffer.h +++ b/remoting/base/compound_buffer.h @@ -52,6 +52,10 @@ class CompoundBuffer { void AppendCopyOf(const char* data, int data_size); void PrependCopyOf(const char* data, int data_size); + // Drop |bytes| bytes from the beginning or the end of the buffer. + void CropFront(int bytes); + void CropBack(int bytes); + // Current size of the buffer. int total_bytes() const { return total_bytes_; } diff --git a/remoting/base/compound_buffer_unittest.cc b/remoting/base/compound_buffer_unittest.cc index 63ede5a..2b90bfe 100644 --- a/remoting/base/compound_buffer_unittest.cc +++ b/remoting/base/compound_buffer_unittest.cc @@ -24,6 +24,9 @@ const int kChunkSizes1[] = {1, 10, 20, -1}; // Chunk sizes used to test CopyFrom(). const int kCopySizes0[] = {10, 3, -1}; const int kCopySizes1[] = {20, -1}; + +const int kCropSizes[] = {1, -1}; + } // namespace class CompoundBufferTest : public testing::Test { @@ -52,6 +55,22 @@ class CompoundBufferTest : public testing::Test { EXPECT_TRUE(CompareData(copy, data_->data() + pos, size)); } + void TestCropFront(int pos, int size) { + CompoundBuffer cropped; + cropped.CopyFrom(target_, 0, target_.total_bytes()); + cropped.CropFront(pos); + EXPECT_TRUE(CompareData(cropped, data_->data() + pos, + target_.total_bytes() - pos)); + } + + void TestCropBack(int pos, int size) { + CompoundBuffer cropped; + cropped.CopyFrom(target_, 0, target_.total_bytes()); + cropped.CropBack(pos); + EXPECT_TRUE(CompareData(cropped, data_->data(), + target_.total_bytes() - pos)); + } + protected: virtual void SetUp() { data_ = new IOBuffer(kDataSize); @@ -210,6 +229,24 @@ TEST_F(CompoundBufferTest, PrependCopyOf) { EXPECT_TRUE(CompareData(target_, data_->data(), kDataSize)); } +TEST_F(CompoundBufferTest, CropFront) { + target_.Clear(); + IterateOverPieces(kChunkSizes1, NewCallback( + static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); + IterateOverPieces(kCropSizes, NewCallback( + static_cast<CompoundBufferTest*>(this), + &CompoundBufferTest::TestCropFront)); +} + +TEST_F(CompoundBufferTest, CropBack) { + target_.Clear(); + IterateOverPieces(kChunkSizes1, NewCallback( + static_cast<CompoundBufferTest*>(this), &CompoundBufferTest::Append)); + IterateOverPieces(kCropSizes, NewCallback( + static_cast<CompoundBufferTest*>(this), + &CompoundBufferTest::TestCropBack)); +} + TEST_F(CompoundBufferTest, CopyFrom) { target_.Clear(); IterateOverPieces(kChunkSizes1, NewCallback( |