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

Unified Diff: base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc

Issue 1467453003: [Tracing] Make heap profiler type info a string (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc
diff --git a/base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc b/base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..79cacc23e210fa9318d74c22f35001975195e2fd
--- /dev/null
+++ b/base/trace_event/heap_profiler_type_name_deduplicator_unittest.cc
@@ -0,0 +1,76 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "base/json/json_reader.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/trace_event/heap_profiler_type_name_deduplicator.h"
+#include "base/values.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace trace_event {
+
+// Define all strings once, because the deduplicator requires pointer equality,
+// and string interning is unreliable.
+const char kInt[] = "int";
+const char kBool[] = "bool";
+const char kString[] = "string";
+const char kCxxAbiMangled[] = "N3WTF10RefCountedIN5blink11WebViewImplEEE";
+
+scoped_ptr<Value> DumpAndReadBack(const ConvertableToTraceFormat& convertable) {
+ std::string json;
+ convertable.AppendAsTraceFormat(&json);
+ return JSONReader::Read(json);
+}
+
+TEST(TypeNameDeduplicatorTest, Deduplication) {
+ // The type IDs should be like this:
+ // 0: [unknown]
+ // 1: int
+ // 2: bool
+ // 3: string
+
+ scoped_refptr<TypeNameDeduplicator> dedup = new TypeNameDeduplicator;
+ ASSERT_EQ(1, dedup->Insert(kInt));
+ ASSERT_EQ(2, dedup->Insert(kBool));
+ ASSERT_EQ(3, dedup->Insert(kString));
+
+ // Inserting again should return the same IDs.
+ ASSERT_EQ(2, dedup->Insert(kBool));
+ ASSERT_EQ(1, dedup->Insert(kInt));
+ ASSERT_EQ(3, dedup->Insert(kString));
+
+ // A null pointer should yield type ID 0.
+ ASSERT_EQ(0, dedup->Insert(nullptr));
+}
+
+#if !defined(COMPILER_MSVC) // Type names are not mangled on MSVC.
+
+TEST(TypeNameDeduplicatorTest, DemangleTypeNames) {
+ scoped_refptr<TypeNameDeduplicator> dedup = new TypeNameDeduplicator;
+ ASSERT_EQ(1, dedup->Insert(kCxxAbiMangled));
+ ASSERT_EQ(2, dedup->Insert(kString));
+
+ scoped_ptr<Value> type_names = DumpAndReadBack(*dedup);
+ const DictionaryValue* dictionary;
+ ASSERT_TRUE(type_names->GetAsDictionary(&dictionary));
+
+ // When the mangled name was inserted, it got ID 1. The exported key "1"
+ // should contain the demangled name.
+ std::string type_name;
+ ASSERT_TRUE(dictionary->GetString("1", &type_name));
+ ASSERT_EQ("WTF::RefCounted<blink::WebViewImpl>", type_name);
+
+ // Verify that the unmangled name at ID 2 was not modified.
+ ASSERT_TRUE(dictionary->GetString("2", &type_name));
+ ASSERT_EQ(kString, type_name);
+}
+
+#endif
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698