Chromium Code Reviews| OLD | NEW |
|---|---|
| (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); | |
|
petrcermak
2016/06/29 10:27:49
you could also add EXPECT_EQ(kChunkSize - i - 1, s
Primiano Tucci (use gerrit)
2016/06/30 12:17:35
Done.
| |
| 52 EXPECT_EQ(1u, delegate.chunks.size()); | |
| 53 EXPECT_EQ(0u, ssw.bytes_available()); | |
| 54 | |
| 55 // This extra write will cause the first extension. | |
| 56 ssw.WriteBytes(kOneByteBuf, sizeof(kOneByteBuf)); | |
| 57 EXPECT_EQ(2u, delegate.chunks.size()); | |
| 58 EXPECT_EQ(7u, ssw.bytes_available()); | |
| 59 | |
| 60 // This starts at offset 1 , to make sure we don't hardcode any assumption | |
|
petrcermak
2016/06/29 10:27:49
nit: remove space and comma
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 61 // about alignment. | |
| 62 ContiguousMemoryRange reserved_range_1 = ssw.ReserveBytes(4); | |
| 63 EXPECT_EQ(3u, ssw.bytes_available()); | |
|
petrcermak
2016/06/29 10:27:49
check chunk count as well
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 64 | |
| 65 ssw.WriteByte(0xFF); | |
| 66 ssw.WriteBytes(kThreeByteBuf, sizeof(kThreeByteBuf)); | |
| 67 EXPECT_EQ(3u, delegate.chunks.size()); | |
|
petrcermak
2016/06/29 10:27:49
check bytes_available as well
Primiano Tucci (use gerrit)
2016/06/30 12:17:35
Done.
| |
| 68 | |
| 69 ContiguousMemoryRange reserved_range_2 = ssw.ReserveBytes(4); | |
| 70 ssw.WriteBytes(kTwentyByteBuf, sizeof(kTwentyByteBuf)); | |
| 71 EXPECT_EQ(6u, delegate.chunks.size()); | |
| 72 EXPECT_EQ(7u, ssw.bytes_available()); | |
| 73 | |
| 74 // Writing reserved bytes should not change the bytes_available(). | |
| 75 memcpy(reserved_range_1.begin, kFourByteBuf, sizeof(kFourByteBuf)); | |
| 76 memcpy(reserved_range_2.begin, kFourByteBuf, sizeof(kFourByteBuf)); | |
| 77 EXPECT_EQ(7u, ssw.bytes_available()); | |
|
petrcermak
2016/06/29 10:27:49
check chunk count as well
Primiano Tucci (use gerrit)
2016/06/30 12:17:35
Done.
| |
| 78 | |
| 79 // Check that reserving more bytes than what left creates a brand new chunk | |
| 80 // even if the previous one is not exhausted | |
| 81 for (uint8_t i = 0; i < 5; ++i) | |
| 82 ssw.WriteByte(0xFF); | |
| 83 memcpy(ssw.ReserveBytes(4).begin, kFourByteBuf, sizeof(kFourByteBuf)); | |
| 84 EXPECT_EQ(7u, delegate.chunks.size()); | |
|
petrcermak
2016/06/29 10:27:49
check bytes_available as well
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Done.
| |
| 85 | |
| 86 EXPECT_EQ("0001020304050607", delegate.GetChunkAsString(0)); | |
| 87 EXPECT_EQ("4060616263FF5051", delegate.GetChunkAsString(1)); | |
| 88 EXPECT_EQ("5260616263A0A1A2", delegate.GetChunkAsString(2)); | |
| 89 EXPECT_EQ("A3A4A5A6A7A8A9AA", delegate.GetChunkAsString(3)); | |
| 90 EXPECT_EQ("ABACADAEAFB0B1B2", delegate.GetChunkAsString(4)); | |
| 91 EXPECT_EQ("B3FFFFFFFFFF0000", delegate.GetChunkAsString(5)); | |
| 92 EXPECT_EQ("6061626300000000", delegate.GetChunkAsString(6)); | |
| 93 | |
| 94 // Finally reset the writer to a new buffer. | |
| 95 uint8_t other_buffer[8] = {0}; | |
| 96 ssw.Reset({other_buffer, other_buffer + sizeof(other_buffer)}); | |
| 97 EXPECT_EQ(other_buffer, ssw.write_ptr()); | |
| 98 ssw.WriteByte(1); | |
| 99 ssw.WriteBytes(kThreeByteBuf, sizeof(kThreeByteBuf)); | |
| 100 EXPECT_EQ(1u, other_buffer[0]); | |
| 101 EXPECT_EQ(0x52u, other_buffer[3]); | |
| 102 } | |
| 103 | |
| 104 } // namespace | |
| 105 } // namespace v2 | |
| 106 } // namespace tracing | |
| OLD | NEW |