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

Side by Side Diff: components/tracing/core/scattered_stream_writer_unittest.cc

Issue 2047273002: tracing v2: Add ScatteredStreamWriter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tv_ups1
Patch Set: Typo Created 4 years, 5 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
« no previous file with comments | « components/tracing/core/scattered_stream_writer.cc ('k') | no next file » | 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 2016 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 #include "components/tracing/core/scattered_stream_writer.h"
6
7 #include <string.h>
8
9 #include <memory>
10 #include <vector>
11
12 #include "base/strings/string_number_conversions.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace tracing {
16 namespace v2 {
17 namespace {
18
19 class MockDelegate : public ScatteredStreamWriter::Delegate {
20 public:
21 static const size_t kChunkSize = 8;
22
23 ContiguousMemoryRange GetNewBuffer() override {
24 std::unique_ptr<uint8_t[]> chunk(new uint8_t[kChunkSize]);
25 uint8_t* begin = chunk.get();
26 memset(begin, 0, kChunkSize);
27 chunks.push_back(std::move(chunk));
28 return {begin, begin + kChunkSize};
29 }
30
31 std::string GetChunkAsString(int chunk_index) {
32 return base::HexEncode(chunks[chunk_index].get(), kChunkSize);
33 }
34
35 std::vector<std::unique_ptr<uint8_t[]>> chunks;
36 };
37
38 TEST(ScatteredStreamWriterTest, ScatteredWrites) {
39 MockDelegate delegate;
40 ScatteredStreamWriter ssw(&delegate);
41
42 const uint8_t kOneByteBuf[] = {0x40};
43 const uint8_t kThreeByteBuf[] = {0x50, 0x51, 0x52};
44 const uint8_t kFourByteBuf[] = {0x60, 0x61, 0x62, 0x63};
45 uint8_t kTwentyByteBuf[20];
46 for (uint8_t i = 0; i < sizeof(kTwentyByteBuf); ++i)
47 kTwentyByteBuf[i] = 0xA0 + i;
48
49 // Writing up to the chunk size should cause only the initial extension.
50 for (uint8_t i = 0; i < MockDelegate::kChunkSize; ++i) {
51 ssw.WriteByte(i);
52 EXPECT_EQ(MockDelegate::kChunkSize - i - 1, ssw.bytes_available());
53 }
54 EXPECT_EQ(1u, delegate.chunks.size());
55 EXPECT_EQ(0u, ssw.bytes_available());
56
57 // This extra write will cause the first extension.
58 ssw.WriteBytes(kOneByteBuf, sizeof(kOneByteBuf));
59 EXPECT_EQ(2u, delegate.chunks.size());
60 EXPECT_EQ(7u, ssw.bytes_available());
61
62 // This starts at offset 1, to make sure we don't hardcode any assumption
63 // about alignment.
64 ContiguousMemoryRange reserved_range_1 = ssw.ReserveBytes(4);
65 EXPECT_EQ(2u, delegate.chunks.size());
66 EXPECT_EQ(3u, ssw.bytes_available());
67
68 ssw.WriteByte(0xFF);
69 ssw.WriteBytes(kThreeByteBuf, sizeof(kThreeByteBuf));
70 EXPECT_EQ(3u, delegate.chunks.size());
71 EXPECT_EQ(7u, ssw.bytes_available());
72
73 ContiguousMemoryRange reserved_range_2 = ssw.ReserveBytes(4);
74 ssw.WriteBytes(kTwentyByteBuf, sizeof(kTwentyByteBuf));
75 EXPECT_EQ(6u, delegate.chunks.size());
76 EXPECT_EQ(7u, ssw.bytes_available());
77
78 // Writing reserved bytes should not change the bytes_available().
79 memcpy(reserved_range_1.begin, kFourByteBuf, sizeof(kFourByteBuf));
80 memcpy(reserved_range_2.begin, kFourByteBuf, sizeof(kFourByteBuf));
81 EXPECT_EQ(6u, delegate.chunks.size());
82 EXPECT_EQ(7u, ssw.bytes_available());
83
84 // Check that reserving more bytes than what left creates a brand new chunk
85 // even if the previous one is not exhausted
86 for (uint8_t i = 0; i < 5; ++i)
87 ssw.WriteByte(0xFF);
88 memcpy(ssw.ReserveBytes(4).begin, kFourByteBuf, sizeof(kFourByteBuf));
89 EXPECT_EQ(7u, delegate.chunks.size());
90 EXPECT_EQ(4u, ssw.bytes_available());
91
92 EXPECT_EQ("0001020304050607", delegate.GetChunkAsString(0));
93 EXPECT_EQ("4060616263FF5051", delegate.GetChunkAsString(1));
94 EXPECT_EQ("5260616263A0A1A2", delegate.GetChunkAsString(2));
95 EXPECT_EQ("A3A4A5A6A7A8A9AA", delegate.GetChunkAsString(3));
96 EXPECT_EQ("ABACADAEAFB0B1B2", delegate.GetChunkAsString(4));
97 EXPECT_EQ("B3FFFFFFFFFF0000", delegate.GetChunkAsString(5));
98 EXPECT_EQ("6061626300000000", delegate.GetChunkAsString(6));
99
100 // Finally reset the writer to a new buffer.
101 uint8_t other_buffer[8] = {0};
102 ssw.Reset({other_buffer, other_buffer + sizeof(other_buffer)});
103 EXPECT_EQ(other_buffer, ssw.write_ptr());
104 ssw.WriteByte(1);
105 ssw.WriteBytes(kThreeByteBuf, sizeof(kThreeByteBuf));
106 EXPECT_EQ(1u, other_buffer[0]);
107 EXPECT_EQ(0x52u, other_buffer[3]);
108 }
109
110 } // namespace
111 } // namespace v2
112 } // namespace tracing
OLDNEW
« no previous file with comments | « components/tracing/core/scattered_stream_writer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698