Index: content/common/partial_circular_buffer_unittest.cc |
diff --git a/content/common/partial_circular_buffer_unittest.cc b/content/common/partial_circular_buffer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b057a7d4aec046c8342283f2c7060567e567483e |
--- /dev/null |
+++ b/content/common/partial_circular_buffer_unittest.cc |
@@ -0,0 +1,139 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// The test buffer data is 52 bytes, wrap position is set to 20. The total |
+// buffer size is allocated dynamically based on the actual header size. This |
+// gives: |
+// Header of some size, non-wrapping part 20 bytes, wrapping part 32 bytes. |
+// As input data, a 14 byte array is used and repeatedly written. It's chosen |
+// not to be an integer factor smaller than the wrapping part. This ensures that |
+// the wrapped data isn't repeated at the same position. |
+// Note that desipte the number of wraps (if one or more), the reference output |
+// data is the same since the offset at each wrap is always the same. |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "content/common/partial_circular_buffer.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace content { |
+ |
+const size_t kBufferDataSize = 52; |
tommi (sloooow) - chröme
2013/04/11 16:37:40
should this be sizeof(kOutputRefDataWrap)?
Henrik Grunell
2013/04/12 11:11:08
That could be better. Done.
|
+const size_t kWrapPosition = 20; |
tommi (sloooow) - chröme
2013/04/11 16:37:40
a comment here would be nice (I'm assuming 20 is j
Henrik Grunell
2013/04/12 11:11:08
Correct. Added comment.
|
+const uint8 kInputData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; |
+const uint8 kOutputRefDataWrap[] = |
+ // The 20 bytes in the non-wrapping part. |
+ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, |
+ // The 32 bytes in wrapping part. |
+ 11, 12, 13, 14, |
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; |
+ |
+class PartialCircularBufferTest : public testing::Test { |
+ public: |
+ PartialCircularBufferTest() { |
+ PartialCircularBuffer::BufferData test_struct; |
+ buffer_header_size_ = |
+ test_struct.data - reinterpret_cast<uint8*>(&test_struct); |
tommi (sloooow) - chröme
2013/04/11 16:37:40
absolute nit: &test_struct.data[0]
Henrik Grunell
2013/04/12 11:11:08
Done.
|
+ |
+ buffer_.reset(new uint8[buffer_header_size_ + kBufferDataSize]); |
+ pcb_write_.reset( |
+ new PartialCircularBuffer(buffer_.get(), |
+ buffer_header_size_ + kBufferDataSize, |
+ kWrapPosition)); |
+ } |
+ |
+ void WriteToBuffer(int num) { |
+ for (int i = 0; i < num; ++i) |
+ pcb_write_->Write(kInputData, sizeof(kInputData)); |
+ } |
+ |
+ void InitReadBuffer() { |
+ pcb_read_.reset(new PartialCircularBuffer( |
+ buffer_.get(), buffer_header_size_ + kBufferDataSize)); |
+ } |
+ |
+ protected: |
+ scoped_ptr<PartialCircularBuffer> pcb_write_; |
+ scoped_ptr<PartialCircularBuffer> pcb_read_; |
+ scoped_array<uint8> buffer_; |
tommi (sloooow) - chröme
2013/04/11 16:37:40
I think we now use scoped_ptr<uint8[]> instead of
Henrik Grunell
2013/04/12 11:11:08
Correct, it's deprecated. Done.
|
+ size_t buffer_header_size_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PartialCircularBufferTest); |
+}; |
+ |
+TEST_F(PartialCircularBufferTest, NoWrapBeginningPartOnly) { |
+ WriteToBuffer(1); |
+ InitReadBuffer(); |
+ |
+ uint8 output_data[sizeof(kInputData)] = {0}; |
+ EXPECT_EQ(sizeof(output_data), |
+ pcb_read_->Read(output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0, memcmp(kInputData, output_data, sizeof(kInputData))); |
+ |
+ EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data))); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, NoWrapBeginningAndEndParts) { |
+ WriteToBuffer(2); |
+ InitReadBuffer(); |
+ |
+ uint8 output_data[2 * sizeof(kInputData)] = {0}; |
+ EXPECT_EQ(sizeof(output_data), |
+ pcb_read_->Read(output_data, sizeof(output_data))); |
+ |
+ const uint8 output_ref_data[2 * sizeof(kInputData)] = |
+ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; |
+ EXPECT_EQ(0, memcmp(output_ref_data, output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data))); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapOnce) { |
+ WriteToBuffer(4); |
+ InitReadBuffer(); |
+ |
+ uint8 output_data[sizeof(kOutputRefDataWrap)] = {0}; |
+ EXPECT_EQ(sizeof(output_data), |
+ pcb_read_->Read(output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data))); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapTwice) { |
+ WriteToBuffer(7); |
+ InitReadBuffer(); |
+ |
+ uint8 output_data[sizeof(kOutputRefDataWrap)] = {0}; |
+ EXPECT_EQ(sizeof(output_data), |
+ pcb_read_->Read(output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data))); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapOnceSmallerOutputBuffer) { |
+ WriteToBuffer(4); |
+ InitReadBuffer(); |
+ |
+ uint8 output_data[sizeof(kOutputRefDataWrap)] = {0}; |
+ const size_t size_per_read = 16; |
+ size_t read = 0; |
+ for (; read + size_per_read <= sizeof(output_data); read += size_per_read) { |
+ EXPECT_EQ(size_per_read, |
+ pcb_read_->Read(output_data + read, size_per_read)); |
+ } |
+ EXPECT_EQ(sizeof(output_data) - read, |
+ pcb_read_->Read(output_data + read, size_per_read)); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data))); |
+ |
+ EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data))); |
+} |
+ |
+} // namespace content |