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

Side by Side Diff: base/trace_event/heap_profiler_string_deduplicator_unittest.cc

Issue 2650863003: [tracing] Switch to new heap dump format. (Closed)
Patch Set: Fix rebase damage Created 3 years, 7 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
(Empty)
1 // Copyright 2017 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 "base/trace_event/heap_profiler_string_deduplicator.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "base/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace trace_event {
17
18 namespace {
19
20 // Calls StringDeduplicator::SerializeIncrementally() and returns ListValue
21 // with serialized entries.
22 std::unique_ptr<ListValue> SerializeEntriesIncrementally(
23 StringDeduplicator* dedup) {
24 TracedValue traced_value;
25 traced_value.BeginArray("");
26 dedup->SerializeIncrementally(&traced_value);
27 traced_value.EndArray();
28
29 auto base_value = traced_value.ToBaseValue();
30 DictionaryValue* dictionary;
31 std::unique_ptr<Value> entries;
32 if (!base_value->GetAsDictionary(&dictionary) ||
33 !dictionary->Remove("", &entries)) {
34 return nullptr;
35 }
36 return ListValue::From(std::move(entries));
37 }
38
39 struct StringMapping {
40 const int id;
41 const char* const string;
42 };
43
44 std::unique_ptr<ListValue> SerializeMappingsAsEntries(
45 std::initializer_list<StringMapping> mappings) {
46 auto entries = MakeUnique<ListValue>();
47 for (const auto& mapping : mappings) {
48 auto entry = MakeUnique<DictionaryValue>();
49 entry->SetInteger("id", mapping.id);
50 entry->SetString("string", mapping.string);
51 entries->Append(std::move(entry));
52 }
53 return entries;
54 }
55
56 void ExpectIncrementalEntries(StringDeduplicator* dedup,
57 std::initializer_list<StringMapping> mappings) {
58 auto entries = SerializeEntriesIncrementally(dedup);
59 ASSERT_TRUE(entries);
60
61 auto expected_entries = SerializeMappingsAsEntries(mappings);
62 ASSERT_TRUE(expected_entries->Equals(entries.get()))
63 << "expected_entries = " << *expected_entries << "entries = " << *entries;
64 }
65
66 } // namespace
67
68 TEST(StringDeduplicatorTest, ImplicitId0) {
69 StringDeduplicator dedup;
70
71 // NULL string is mapped to an implicitly added ID #0.
72 ExpectIncrementalEntries(&dedup, {{0, "[null]"}});
73 ASSERT_EQ(0, dedup.Insert(nullptr));
74
75 // Even though ID #0 is serialized as "[null]", it's distinct from
76 // explicitly added "[null]" string.
77 ASSERT_EQ(1, dedup.Insert("[null]"));
78 ExpectIncrementalEntries(&dedup, {{1, "[null]"}});
79 }
80
81 TEST(StringDeduplicatorTest, Deduplicate) {
82 StringDeduplicator dedup;
83
84 ASSERT_EQ(1, dedup.Insert("foo"));
85 ASSERT_EQ(2, dedup.Insert("bar"));
86 ASSERT_EQ(3, dedup.Insert("baz"));
87
88 // Inserting again should return the same IDs.
89 ASSERT_EQ(2, dedup.Insert("bar"));
90 ASSERT_EQ(1, dedup.Insert("foo"));
91 ASSERT_EQ(3, dedup.Insert("baz"));
92 }
93
94 TEST(StringDeduplicatorTest, InsertCopies) {
95 StringDeduplicator dedup;
96
97 std::string string = "foo";
98 ASSERT_EQ(1, dedup.Insert(string));
99
100 // StringDeduplicatorTest::Insert() takes StringPiece, which implicitly
101 // constructs from both const char* and std::string. Check that Insert()
102 // actually copies string data, and doesn't simply copy StringPieces.
103 string = "???";
104 ASSERT_EQ(1, dedup.Insert("foo"));
105 }
106
107 TEST(StringDeduplicatorTest, SerializeIncrementally) {
108 StringDeduplicator dedup;
109
110 ASSERT_EQ(1, dedup.Insert("foo"));
111 ASSERT_EQ(2, dedup.Insert("bar"));
112
113 ExpectIncrementalEntries(&dedup, {{0, "[null]"}, {1, "foo"}, {2, "bar"}});
114
115 ASSERT_EQ(2, dedup.Insert("bar"));
116 ASSERT_EQ(3, dedup.Insert("baz"));
117
118 ExpectIncrementalEntries(&dedup, {{3, "baz"}});
119
120 ExpectIncrementalEntries(&dedup, {});
121 }
122
123 } // namespace trace_event
124 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/heap_profiler_string_deduplicator.cc ('k') | base/trace_event/heap_profiler_type_name_deduplicator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698