Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: content/common/partial_circular_buffer_unittest.cc

Issue 13473005: Adding partially circular memory buffer wrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Code review fixes. Removed packing of struct. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/partial_circular_buffer.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 // The test buffer data is 52 bytes, wrap position is set to 20. The total
6 // buffer size is allocated dynamically based on the actual header size. This
7 // 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.
14
15 #include "base/memory/scoped_ptr.h"
16 #include "content/common/partial_circular_buffer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace content {
20
21 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.
22 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.
23 const uint8 kInputData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
24 const uint8 kOutputRefDataWrap[] =
25 // The 20 bytes in the non-wrapping part.
26 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6,
27 // The 32 bytes in wrapping part.
28 11, 12, 13, 14,
29 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
30 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
31
32 class PartialCircularBufferTest : public testing::Test {
33 public:
34 PartialCircularBufferTest() {
35 PartialCircularBuffer::BufferData test_struct;
36 buffer_header_size_ =
37 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.
38
39 buffer_.reset(new uint8[buffer_header_size_ + kBufferDataSize]);
40 pcb_write_.reset(
41 new PartialCircularBuffer(buffer_.get(),
42 buffer_header_size_ + kBufferDataSize,
43 kWrapPosition));
44 }
45
46 void WriteToBuffer(int num) {
47 for (int i = 0; i < num; ++i)
48 pcb_write_->Write(kInputData, sizeof(kInputData));
49 }
50
51 void InitReadBuffer() {
52 pcb_read_.reset(new PartialCircularBuffer(
53 buffer_.get(), buffer_header_size_ + kBufferDataSize));
54 }
55
56 protected:
57 scoped_ptr<PartialCircularBuffer> pcb_write_;
58 scoped_ptr<PartialCircularBuffer> pcb_read_;
59 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.
60 size_t buffer_header_size_;
61
62 DISALLOW_COPY_AND_ASSIGN(PartialCircularBufferTest);
63 };
64
65 TEST_F(PartialCircularBufferTest, NoWrapBeginningPartOnly) {
66 WriteToBuffer(1);
67 InitReadBuffer();
68
69 uint8 output_data[sizeof(kInputData)] = {0};
70 EXPECT_EQ(sizeof(output_data),
71 pcb_read_->Read(output_data, sizeof(output_data)));
72
73 EXPECT_EQ(0, memcmp(kInputData, output_data, sizeof(kInputData)));
74
75 EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data)));
76 }
77
78 TEST_F(PartialCircularBufferTest, NoWrapBeginningAndEndParts) {
79 WriteToBuffer(2);
80 InitReadBuffer();
81
82 uint8 output_data[2 * sizeof(kInputData)] = {0};
83 EXPECT_EQ(sizeof(output_data),
84 pcb_read_->Read(output_data, sizeof(output_data)));
85
86 const uint8 output_ref_data[2 * sizeof(kInputData)] =
87 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
88 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
89 EXPECT_EQ(0, memcmp(output_ref_data, output_data, sizeof(output_data)));
90
91 EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data)));
92 }
93
94 TEST_F(PartialCircularBufferTest, WrapOnce) {
95 WriteToBuffer(4);
96 InitReadBuffer();
97
98 uint8 output_data[sizeof(kOutputRefDataWrap)] = {0};
99 EXPECT_EQ(sizeof(output_data),
100 pcb_read_->Read(output_data, sizeof(output_data)));
101
102 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data)));
103
104 EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data)));
105 }
106
107 TEST_F(PartialCircularBufferTest, WrapTwice) {
108 WriteToBuffer(7);
109 InitReadBuffer();
110
111 uint8 output_data[sizeof(kOutputRefDataWrap)] = {0};
112 EXPECT_EQ(sizeof(output_data),
113 pcb_read_->Read(output_data, sizeof(output_data)));
114
115 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data)));
116
117 EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data)));
118 }
119
120 TEST_F(PartialCircularBufferTest, WrapOnceSmallerOutputBuffer) {
121 WriteToBuffer(4);
122 InitReadBuffer();
123
124 uint8 output_data[sizeof(kOutputRefDataWrap)] = {0};
125 const size_t size_per_read = 16;
126 size_t read = 0;
127 for (; read + size_per_read <= sizeof(output_data); read += size_per_read) {
128 EXPECT_EQ(size_per_read,
129 pcb_read_->Read(output_data + read, size_per_read));
130 }
131 EXPECT_EQ(sizeof(output_data) - read,
132 pcb_read_->Read(output_data + read, size_per_read));
133
134 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, sizeof(output_data)));
135
136 EXPECT_EQ(0u, pcb_read_->Read(output_data, sizeof(output_data)));
137 }
138
139 } // namespace content
OLDNEW
« no previous file with comments | « content/common/partial_circular_buffer.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698