1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // The test buffer data is 52 bytes, wrap position is set to 20 (this is
6 // arbitrarily chosen). The total buffer size is allocated dynamically based on
7 // the actual header size. This gives:
8 // Header of some size, non-wrapping part 20 bytes, wrapping part 32 bytes.
9 // As input data, a 14 byte array is used and repeatedly written. It's chosen
10 // not to be an integer factor smaller than the wrapping part. This ensures that
11 // the wrapped data isn't repeated at the same position.
12 // Note that desipte the number of wraps (if one or more), the reference output
13 // data is the same since the offset at each wrap is always the same.
15 #include "base/memory/scoped_ptr.h"
16 #include "chrome/common/partial_circular_buffer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 const uint32 kWrapPosition
= 20;
20 const uint8 kInputData
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
21 const uint8 kOutputRefDataWrap
[] =
22 // The 20 bytes in the non-wrapping part.
23 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6,
24 // The 32 bytes in wrapping part.
26 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
27 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
29 class PartialCircularBufferTest
: public testing::Test
{
31 PartialCircularBufferTest() {
32 PartialCircularBuffer::BufferData test_struct
;
34 &test_struct
.data
[0] - reinterpret_cast<uint8
*>(&test_struct
);
36 buffer_
.reset(new uint8
[buffer_header_size_
+ sizeof(kOutputRefDataWrap
)]);
39 void InitWriteBuffer(bool append
) {
40 pcb_write_
.reset(new PartialCircularBuffer(
42 buffer_header_size_
+ sizeof(kOutputRefDataWrap
),
47 void WriteToBuffer(int num
) {
48 for (int i
= 0; i
< num
; ++i
)
49 pcb_write_
->Write(kInputData
, sizeof(kInputData
));
52 void InitReadBuffer() {
53 pcb_read_
.reset(new PartialCircularBuffer(
54 buffer_
.get(), buffer_header_size_
+ sizeof(kOutputRefDataWrap
)));
58 scoped_ptr
<PartialCircularBuffer
> pcb_write_
;
59 scoped_ptr
<PartialCircularBuffer
> pcb_read_
;
60 scoped_ptr
<uint8
[]> buffer_
;
61 uint32 buffer_header_size_
;
63 DISALLOW_COPY_AND_ASSIGN(PartialCircularBufferTest
);
66 TEST_F(PartialCircularBufferTest
, NoWrapBeginningPartOnly
) {
67 InitWriteBuffer(false);
71 uint8 output_data
[sizeof(kInputData
)] = {0};
72 EXPECT_EQ(sizeof(output_data
),
73 pcb_read_
->Read(output_data
, sizeof(output_data
)));
75 EXPECT_EQ(0, memcmp(kInputData
, output_data
, sizeof(kInputData
)));
77 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
80 TEST_F(PartialCircularBufferTest
, NoWrapBeginningAndEndParts
) {
81 InitWriteBuffer(false);
85 uint8 output_data
[2 * sizeof(kInputData
)] = {0};
86 EXPECT_EQ(sizeof(output_data
),
87 pcb_read_
->Read(output_data
, sizeof(output_data
)));
89 const uint8 output_ref_data
[2 * sizeof(kInputData
)] =
90 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
91 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
92 EXPECT_EQ(0, memcmp(output_ref_data
, output_data
, sizeof(output_data
)));
94 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
97 TEST_F(PartialCircularBufferTest
, WrapOnce
) {
98 InitWriteBuffer(false);
102 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
103 EXPECT_EQ(sizeof(output_data
),
104 pcb_read_
->Read(output_data
, sizeof(output_data
)));
106 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
108 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
111 TEST_F(PartialCircularBufferTest
, WrapTwice
) {
112 InitWriteBuffer(false);
116 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
117 EXPECT_EQ(sizeof(output_data
),
118 pcb_read_
->Read(output_data
, sizeof(output_data
)));
120 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
122 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
125 TEST_F(PartialCircularBufferTest
, WrapOnceSmallerOutputBuffer
) {
126 InitWriteBuffer(false);
130 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
131 const uint32 size_per_read
= 16;
133 for (; read
+ size_per_read
<= sizeof(output_data
); read
+= size_per_read
) {
134 EXPECT_EQ(size_per_read
,
135 pcb_read_
->Read(output_data
+ read
, size_per_read
));
137 EXPECT_EQ(sizeof(output_data
) - read
,
138 pcb_read_
->Read(output_data
+ read
, size_per_read
));
140 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
142 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
145 TEST_F(PartialCircularBufferTest
, WrapOnceWithAppend
) {
146 InitWriteBuffer(false);
148 InitWriteBuffer(true);
152 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
153 EXPECT_EQ(sizeof(output_data
),
154 pcb_read_
->Read(output_data
, sizeof(output_data
)));
156 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
158 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
161 TEST_F(PartialCircularBufferTest
, WrapTwiceWithAppend
) {
162 InitWriteBuffer(false);
164 InitWriteBuffer(true);
168 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
169 EXPECT_EQ(sizeof(output_data
),
170 pcb_read_
->Read(output_data
, sizeof(output_data
)));
172 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
174 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
177 TEST_F(PartialCircularBufferTest
, WrapOnceThenOverwriteWithNoWrap
) {
178 InitWriteBuffer(false);
180 InitWriteBuffer(false);
184 uint8 output_data
[sizeof(kInputData
)] = {0};
185 EXPECT_EQ(sizeof(output_data
),
186 pcb_read_
->Read(output_data
, sizeof(output_data
)));
188 EXPECT_EQ(0, memcmp(kInputData
, output_data
, sizeof(kInputData
)));
190 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));
193 TEST_F(PartialCircularBufferTest
, WrapTwiceWithSingleWrite
) {
194 const size_t kInputSize
= sizeof(kInputData
);
195 const size_t kLargeSize
= kInputSize
* 7;
196 uint8 large_input
[kLargeSize
] = {0};
197 for (size_t offset
= 0; offset
< kLargeSize
; offset
+= kInputSize
)
198 memcpy(large_input
+ offset
, kInputData
, kInputSize
);
200 InitWriteBuffer(false);
201 pcb_write_
->Write(large_input
, kLargeSize
);
204 uint8 output_data
[sizeof(kOutputRefDataWrap
)] = {0};
205 EXPECT_EQ(sizeof(output_data
),
206 pcb_read_
->Read(output_data
, sizeof(output_data
)));
208 EXPECT_EQ(0, memcmp(kOutputRefDataWrap
, output_data
, sizeof(output_data
)));
210 EXPECT_EQ(0u, pcb_read_
->Read(output_data
, sizeof(output_data
)));