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

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

Issue 2303343002: tracing v2: building blocks for reading-back trace protobufs (Closed)
Patch Set: Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/tracing/core/proto_utils.h" 5 #include "components/tracing/core/proto_utils.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 namespace tracing { 13 namespace tracing {
13 namespace v2 { 14 namespace v2 {
14 namespace proto { 15 namespace proto {
15 namespace { 16 namespace {
16 17
17 template <typename T> 18 struct VarIntExpectation {
18 bool CheckWriteVarInt(const char* expected, size_t length, T value) { 19 const char* encoded;
19 uint8_t buf[32]; 20 size_t encoded_size;
20 uint8_t* res = WriteVarInt<T>(value, buf); 21 uint64_t int_value;
21 if (static_cast<size_t>(res - buf) != length) 22 };
22 return false;
23 return memcmp(expected, buf, length) == 0;
24 }
25 23
26 #define EXPECT_VARINT32_EQ(expected, expected_length, value) \ 24 const VarIntExpectation kVarIntExpectations[] = {
27 EXPECT_PRED3(CheckWriteVarInt<uint32_t>, expected, expected_length, value) 25 {"\x00", 1, 0},
28 26 {"\x01", 1, 0x1},
29 #define EXPECT_VARINT64_EQ(expected, expected_length, value) \ 27 {"\x7f", 1, 0x7F},
30 EXPECT_PRED3(CheckWriteVarInt<uint64_t>, expected, expected_length, value) 28 {"\xFF\x01", 2, 0xFF},
29 {"\xFF\x7F", 2, 0x3FFF},
30 {"\x80\x80\x01", 3, 0x4000},
31 {"\xFF\xFF\x7F", 3, 0x1FFFFF},
32 {"\x80\x80\x80\x01", 4, 0x200000},
33 {"\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF},
34 {"\x80\x80\x80\x80\x01", 5, 0x10000000},
35 {"\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF},
36 {"\x80\x80\x80\x80\x10", 5, 0x100000000},
37 {"\xFF\xFF\xFF\xFF\x7F", 5, 0x7FFFFFFFF},
38 {"\x80\x80\x80\x80\x80\x01", 6, 0x800000000},
39 {"\xFF\xFF\xFF\xFF\xFF\x7F", 6, 0x3FFFFFFFFFF},
40 {"\x80\x80\x80\x80\x80\x80\x01", 7, 0x40000000000},
41 {"\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 7, 0x1FFFFFFFFFFFF},
42 {"\x80\x80\x80\x80\x80\x80\x80\x01", 8, 0x2000000000000},
43 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 8, 0xFFFFFFFFFFFFFF},
44 {"\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9, 0x100000000000000},
45 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 9, 0x7FFFFFFFFFFFFFFF},
46 {"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10, 0x8000000000000000},
47 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01", 10, 0xFFFFFFFFFFFFFFFF},
48 };
31 49
32 TEST(ProtoUtilsTest, Serialization) { 50 TEST(ProtoUtilsTest, Serialization) {
33 // According to C++ standard, right shift of negative value has 51 // According to C++ standard, right shift of negative value has
34 // implementation-defined resulting value. 52 // implementation-defined resulting value.
35 if ((static_cast<int32_t>(0x80000000u) >> 31) != -1) 53 if ((static_cast<int32_t>(0x80000000u) >> 31) != -1)
36 FAIL() << "Platform has unsupported negative number format or arithmetic"; 54 FAIL() << "Platform has unsupported negative number format or arithmetic";
37 55
38 EXPECT_EQ(0x08u, MakeTagVarInt(1)); 56 EXPECT_EQ(0x08u, MakeTagVarInt(1));
39 EXPECT_EQ(0x09u, MakeTagFixed<uint64_t>(1)); 57 EXPECT_EQ(0x09u, MakeTagFixed<uint64_t>(1));
40 EXPECT_EQ(0x0Au, MakeTagLengthDelimited(1)); 58 EXPECT_EQ(0x0Au, MakeTagLengthDelimited(1));
(...skipping 23 matching lines...) Expand all
64 EXPECT_EQ(1u, ZigZagEncode(-1)); 82 EXPECT_EQ(1u, ZigZagEncode(-1));
65 EXPECT_EQ(2u, ZigZagEncode(1)); 83 EXPECT_EQ(2u, ZigZagEncode(1));
66 EXPECT_EQ(3u, ZigZagEncode(-2)); 84 EXPECT_EQ(3u, ZigZagEncode(-2));
67 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647)); 85 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647));
68 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647)); 86 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647));
69 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), 87 EXPECT_EQ(std::numeric_limits<uint32_t>::max(),
70 ZigZagEncode(std::numeric_limits<int32_t>::min())); 88 ZigZagEncode(std::numeric_limits<int32_t>::min()));
71 EXPECT_EQ(std::numeric_limits<uint64_t>::max(), 89 EXPECT_EQ(std::numeric_limits<uint64_t>::max(),
72 ZigZagEncode(std::numeric_limits<int64_t>::min())); 90 ZigZagEncode(std::numeric_limits<int64_t>::min()));
73 91
74 EXPECT_VARINT32_EQ("\x00", 1, 0); 92 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) {
75 EXPECT_VARINT32_EQ("\x00", 1, 0); 93 const VarIntExpectation& exp = kVarIntExpectations[i];
76 EXPECT_VARINT32_EQ("\x01", 1, 0x1); 94 uint8_t buf[32];
77 EXPECT_VARINT32_EQ("\x7f", 1, 0x7F); 95 uint8_t* res = WriteVarInt<uint64_t>(exp.int_value, buf);
78 EXPECT_VARINT32_EQ("\xFF\x01", 2, 0xFF); 96 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf));
79 EXPECT_VARINT32_EQ("\xFF\x7F", 2, 0x3FFF); 97 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size));
80 EXPECT_VARINT32_EQ("\x80\x80\x01", 3, 0x4000);
81 EXPECT_VARINT32_EQ("\xFF\xFF\x7F", 3, 0x1FFFFF);
82 EXPECT_VARINT32_EQ("\x80\x80\x80\x01", 4, 0x200000);
83 EXPECT_VARINT32_EQ("\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF);
84 EXPECT_VARINT32_EQ("\x80\x80\x80\x80\x01", 5, 0x10000000);
85 EXPECT_VARINT32_EQ("\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF);
86 98
87 EXPECT_VARINT64_EQ("\x00", 1, 0); 99 if (exp.int_value <= std::numeric_limits<uint32_t>::max()) {
88 EXPECT_VARINT64_EQ("\x01", 1, 0x1); 100 uint8_t* res = WriteVarInt<uint32_t>(exp.int_value, buf);
89 EXPECT_VARINT64_EQ("\x7f", 1, 0x7F); 101 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf));
90 EXPECT_VARINT64_EQ("\xFF\x01", 2, 0xFF); 102 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size));
91 EXPECT_VARINT64_EQ("\xFF\x7F", 2, 0x3FFF); 103 }
92 EXPECT_VARINT64_EQ("\x80\x80\x01", 3, 0x4000); 104 }
93 EXPECT_VARINT64_EQ("\xFF\xFF\x7F", 3, 0x1FFFFF);
94 EXPECT_VARINT64_EQ("\x80\x80\x80\x01", 4, 0x200000);
95 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF);
96 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x01", 5, 0x10000000);
97 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF);
98 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x10", 5, 0x100000000);
99 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\x7F", 5, 0x7FFFFFFFF);
100 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x01", 6, 0x800000000);
101 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\x7F", 6, 0x3FFFFFFFFFF);
102 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x01", 7, 0x40000000000);
103 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 7, 0x1FFFFFFFFFFFF);
104 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x01", 8, 0x2000000000000);
105 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 8, 0xFFFFFFFFFFFFFF);
106 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9,
107 0x100000000000000);
108 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 9,
109 0x7FFFFFFFFFFFFFFF);
110 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10,
111 0x8000000000000000);
112 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01", 10,
113 0xFFFFFFFFFFFFFFFF);
114 105
115 uint8_t buf[kMessageLengthFieldSize]; 106 uint8_t buf[kMessageLengthFieldSize];
116 107
117 WriteRedundantVarInt(0, buf); 108 WriteRedundantVarInt(0, buf);
118 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf))); 109 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf)));
119 110
120 WriteRedundantVarInt(1, buf); 111 WriteRedundantVarInt(1, buf);
121 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf))); 112 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf)));
122 113
123 WriteRedundantVarInt(0x80, buf); 114 WriteRedundantVarInt(0x80, buf);
124 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf))); 115 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf)));
125 116
126 WriteRedundantVarInt(0x332211, buf); 117 WriteRedundantVarInt(0x332211, buf);
127 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf))); 118 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf)));
128 119
129 // Largest allowed length. 120 // Largest allowed length.
130 WriteRedundantVarInt(0x0FFFFFFF, buf); 121 WriteRedundantVarInt(0x0FFFFFFF, buf);
131 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf))); 122 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf)));
132 } 123 }
133 124
125 TEST(ProtoUtilsTest, Deserialization) {
126 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) {
127 const VarIntExpectation& exp = kVarIntExpectations[i];
128 uint64_t value = 0;
129 const uint8_t* res = ParseVarInt(
130 reinterpret_cast<const uint8_t*>(exp.encoded),
131 reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size),
132 &value);
133 ASSERT_EQ(reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size),
134 res);
135 ASSERT_EQ(exp.int_value, value);
136 }
137
138 // TODO(primiano): add tests for ParseField.
139 }
140
134 } // namespace 141 } // namespace
135 } // namespace proto 142 } // namespace proto
136 } // namespace v2 143 } // namespace v2
137 } // namespace tracing 144 } // namespace tracing
OLDNEW
« components/tracing/core/proto_utils.cc ('K') | « components/tracing/core/proto_utils.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698