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..93f38793fec2a011ef03104dad0395b1c3c0b664 |
--- /dev/null |
+++ b/content/common/partial_circular_buffer_unittest.cc |
@@ -0,0 +1,136 @@ |
+// 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 is 64 bytes, wrap position is set to 32 - header, which |
+// gives: |
+// Header 12 bytes, 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 kExpectedHeaderSize = 12; |
+const uint8 kInputData[14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; |
tommi (sloooow) - chröme
2013/04/11 03:09:43
nit: you can leave the size of the buffer unspecif
Henrik Grunell
2013/04/11 09:24:18
Done.
|
+const uint8 kOutputRefDataWrap[52] = |
+ // 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}; |
+ |
+// The test buffer is 64 by |
+class PartialCircularBufferTest : public testing::Test { |
+ public: |
+ PartialCircularBufferTest() |
+ : buffer_size_(sizeof(buffer_)), |
+ input_data_size_(sizeof(kInputData)) { |
+ memset(buffer_, 0, sizeof(buffer_)); |
+ pcb_write_.reset( |
+ new PartialCircularBuffer(buffer_, buffer_size_, |
+ buffer_size_ / 2 - kExpectedHeaderSize)); |
+ } |
+ |
+ void WriteToBuffer(int num) { |
+ for (int i = 0; i < num; ++i) |
+ pcb_write_->Write(kInputData, input_data_size_); |
+ } |
+ |
+ protected: |
+ scoped_ptr<PartialCircularBuffer> pcb_write_; |
+ uint8 buffer_[64]; |
+ size_t buffer_size_; |
+ size_t input_data_size_; |
tommi (sloooow) - chröme
2013/04/11 03:09:43
I don't think you need these two member variables
Henrik Grunell
2013/04/11 09:24:18
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(PartialCircularBufferTest); |
+}; |
+ |
+TEST_F(PartialCircularBufferTest, TestHeaderSize) { |
tommi (sloooow) - chröme
2013/04/11 03:09:43
do we need a test for this? Can we instead have a
Henrik Grunell
2013/04/11 09:24:18
Actually, I think it's better to not to rely on a
tommi (sloooow) - chröme
2013/04/11 16:37:39
Agreed
|
+ EXPECT_EQ(kExpectedHeaderSize, buffer_size_ - pcb_write_->data_size_); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, NoWrapBeginningPartOnly) { |
+ WriteToBuffer(1); |
+ |
+ scoped_ptr<PartialCircularBuffer> pcb_read( |
+ new PartialCircularBuffer(buffer_, buffer_size_)); |
+ uint8 output_data[14]; |
tommi (sloooow) - chröme
2013/04/11 03:09:43
instead use sizeof(kInputData) and initialize the
Henrik Grunell
2013/04/11 09:24:18
Done.
|
+ size_t output_data_size = sizeof(output_data); |
+ memset(output_data, 0, sizeof(output_data)); |
+ EXPECT_EQ(input_data_size_, output_data_size); |
+ EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size)); |
+ |
+ EXPECT_EQ(0, memcmp(kInputData, output_data, input_data_size_)); |
tommi (sloooow) - chröme
2013/04/11 03:09:43
Is there a way to determine if more can be read?
Henrik Grunell
2013/04/11 09:24:18
Good point. Yes it returns 0 if no more can be rea
|
+} |
+ |
+TEST_F(PartialCircularBufferTest, NoWrapBeginningAndEndParts) { |
+ WriteToBuffer(2); |
+ |
+ scoped_ptr<PartialCircularBuffer> pcb_read( |
+ new PartialCircularBuffer(buffer_, buffer_size_)); |
+ uint8 output_data[28]; |
tommi (sloooow) - chröme
2013/04/11 03:09:43
same here as for output_data above.
Henrik Grunell
2013/04/11 09:24:18
Done.
|
+ size_t output_data_size = sizeof(output_data); |
+ memset(output_data, 0, output_data_size); |
+ EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size)); |
+ |
+ const uint8 output_ref_data[28] = |
tommi (sloooow) - chröme
2013/04/11 03:09:43
same comments for specifying the size of the array
Henrik Grunell
2013/04/11 09:24:18
Done throughout the code.
|
+ {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, output_data_size)); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapOnce) { |
+ WriteToBuffer(4); |
+ |
+ scoped_ptr<PartialCircularBuffer> pcb_read( |
+ new PartialCircularBuffer(buffer_, buffer_size_)); |
+ uint8 output_data[52]; |
+ size_t output_data_size = sizeof(output_data); |
+ memset(output_data, 0, output_data_size); |
+ EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size)); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size)); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapTwice) { |
+ WriteToBuffer(7); |
+ |
+ scoped_ptr<PartialCircularBuffer> pcb_read( |
+ new PartialCircularBuffer(buffer_, buffer_size_)); |
+ uint8 output_data[52]; |
+ size_t output_data_size = sizeof(output_data); |
+ memset(output_data, 0, output_data_size); |
+ EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size)); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size)); |
+} |
+ |
+TEST_F(PartialCircularBufferTest, WrapOnceSmallerOutputBuffer) { |
+ WriteToBuffer(4); |
+ |
+ scoped_ptr<PartialCircularBuffer> pcb_read( |
+ new PartialCircularBuffer(buffer_, buffer_size_)); |
+ uint8 output_data[52]; |
+ size_t output_data_size = sizeof(output_data); |
+ memset(output_data, 0, output_data_size); |
+ const size_t size_per_read = 16; |
+ EXPECT_EQ(size_per_read, pcb_read->Read(output_data, size_per_read)); |
+ EXPECT_EQ(size_per_read, |
+ pcb_read->Read(output_data + size_per_read, size_per_read)); |
+ EXPECT_EQ(size_per_read, |
+ pcb_read->Read(output_data + 2 * size_per_read, size_per_read)); |
+ EXPECT_EQ(output_data_size - 3 * size_per_read, |
+ pcb_read->Read(output_data + 3 * size_per_read, size_per_read)); |
+ |
+ EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size)); |
+} |
+ |
+} // namespace content |