Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / common / partial_circular_buffer_unittest.cc
blob701aefab374aa3b24c8ec265e4ee03559af870ba
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.
25 11, 12, 13, 14,
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 {
30 public:
31 PartialCircularBufferTest() {
32 PartialCircularBuffer::BufferData test_struct;
33 buffer_header_size_ =
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(
41 buffer_.get(),
42 buffer_header_size_ + sizeof(kOutputRefDataWrap),
43 kWrapPosition,
44 append));
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)));
57 protected:
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);
68 WriteToBuffer(1);
69 InitReadBuffer();
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);
82 WriteToBuffer(2);
83 InitReadBuffer();
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);
99 WriteToBuffer(4);
100 InitReadBuffer();
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);
113 WriteToBuffer(7);
114 InitReadBuffer();
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);
127 WriteToBuffer(4);
128 InitReadBuffer();
130 uint8 output_data[sizeof(kOutputRefDataWrap)] = {0};
131 const uint32 size_per_read = 16;
132 uint32 read = 0;
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);
147 WriteToBuffer(2);
148 InitWriteBuffer(true);
149 WriteToBuffer(2);
150 InitReadBuffer();
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);
163 WriteToBuffer(4);
164 InitWriteBuffer(true);
165 WriteToBuffer(3);
166 InitReadBuffer();
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);
179 WriteToBuffer(4);
180 InitWriteBuffer(false);
181 WriteToBuffer(1);
182 InitReadBuffer();
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);
202 InitReadBuffer();
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)));