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

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: Fixed compilation error on some setups. 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
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 is 64 bytes, wrap position is set to 32 - header, which
6 // gives:
7 // Header 12 bytes, non-wrapping part 20 bytes, wrapping part 32 bytes.
8 // As input data, a 14 byte array is used and repeatedly written. It's chosen
9 // not to be an integer factor smaller than the wrapping part. This ensures that
10 // the wrapped data isn't repeated at the same position.
11 // Note that desipte the number of wraps (if one or more), the reference output
12 // data is the same since the offset at each wrap is always the same.
13
14 #include "base/memory/scoped_ptr.h"
15 #include "content/common/partial_circular_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace content {
19
20 const size_t kExpectedHeaderSize = 12;
21 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.
22 const uint8 kOutputRefDataWrap[52] =
23 // The 20 bytes in the non-wrapping part.
24 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6,
25 // The 32 bytes in wrapping part.
26 11, 12, 13, 14,
27 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
28 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
29
30 // The test buffer is 64 by
31 class PartialCircularBufferTest : public testing::Test {
32 public:
33 PartialCircularBufferTest()
34 : buffer_size_(sizeof(buffer_)),
35 input_data_size_(sizeof(kInputData)) {
36 memset(buffer_, 0, sizeof(buffer_));
37 pcb_write_.reset(
38 new PartialCircularBuffer(buffer_, buffer_size_,
39 buffer_size_ / 2 - kExpectedHeaderSize));
40 }
41
42 void WriteToBuffer(int num) {
43 for (int i = 0; i < num; ++i)
44 pcb_write_->Write(kInputData, input_data_size_);
45 }
46
47 protected:
48 scoped_ptr<PartialCircularBuffer> pcb_write_;
49 uint8 buffer_[64];
50 size_t buffer_size_;
51 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.
52
53 DISALLOW_COPY_AND_ASSIGN(PartialCircularBufferTest);
54 };
55
56 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
57 EXPECT_EQ(kExpectedHeaderSize, buffer_size_ - pcb_write_->data_size_);
58 }
59
60 TEST_F(PartialCircularBufferTest, NoWrapBeginningPartOnly) {
61 WriteToBuffer(1);
62
63 scoped_ptr<PartialCircularBuffer> pcb_read(
64 new PartialCircularBuffer(buffer_, buffer_size_));
65 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.
66 size_t output_data_size = sizeof(output_data);
67 memset(output_data, 0, sizeof(output_data));
68 EXPECT_EQ(input_data_size_, output_data_size);
69 EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size));
70
71 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
72 }
73
74 TEST_F(PartialCircularBufferTest, NoWrapBeginningAndEndParts) {
75 WriteToBuffer(2);
76
77 scoped_ptr<PartialCircularBuffer> pcb_read(
78 new PartialCircularBuffer(buffer_, buffer_size_));
79 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.
80 size_t output_data_size = sizeof(output_data);
81 memset(output_data, 0, output_data_size);
82 EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size));
83
84 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.
85 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
86 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
87 EXPECT_EQ(0, memcmp(output_ref_data, output_data, output_data_size));
88 }
89
90 TEST_F(PartialCircularBufferTest, WrapOnce) {
91 WriteToBuffer(4);
92
93 scoped_ptr<PartialCircularBuffer> pcb_read(
94 new PartialCircularBuffer(buffer_, buffer_size_));
95 uint8 output_data[52];
96 size_t output_data_size = sizeof(output_data);
97 memset(output_data, 0, output_data_size);
98 EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size));
99
100 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size));
101 }
102
103 TEST_F(PartialCircularBufferTest, WrapTwice) {
104 WriteToBuffer(7);
105
106 scoped_ptr<PartialCircularBuffer> pcb_read(
107 new PartialCircularBuffer(buffer_, buffer_size_));
108 uint8 output_data[52];
109 size_t output_data_size = sizeof(output_data);
110 memset(output_data, 0, output_data_size);
111 EXPECT_EQ(output_data_size, pcb_read->Read(output_data, output_data_size));
112
113 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size));
114 }
115
116 TEST_F(PartialCircularBufferTest, WrapOnceSmallerOutputBuffer) {
117 WriteToBuffer(4);
118
119 scoped_ptr<PartialCircularBuffer> pcb_read(
120 new PartialCircularBuffer(buffer_, buffer_size_));
121 uint8 output_data[52];
122 size_t output_data_size = sizeof(output_data);
123 memset(output_data, 0, output_data_size);
124 const size_t size_per_read = 16;
125 EXPECT_EQ(size_per_read, pcb_read->Read(output_data, size_per_read));
126 EXPECT_EQ(size_per_read,
127 pcb_read->Read(output_data + size_per_read, size_per_read));
128 EXPECT_EQ(size_per_read,
129 pcb_read->Read(output_data + 2 * size_per_read, size_per_read));
130 EXPECT_EQ(output_data_size - 3 * size_per_read,
131 pcb_read->Read(output_data + 3 * size_per_read, size_per_read));
132
133 EXPECT_EQ(0, memcmp(kOutputRefDataWrap, output_data, output_data_size));
134 }
135
136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698