OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> |
5 #include <string> | 6 #include <string> |
6 | 7 |
7 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" | 9 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 namespace trace_event { | 14 namespace trace_event { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Define all strings once, because the deduplicator requires pointer equality, | 18 // Define all strings once, because the deduplicator requires pointer equality, |
19 // and string interning is unreliable. | 19 // and string interning is unreliable. |
20 const char kInt[] = "int"; | 20 const char kInt[] = "int"; |
21 const char kBool[] = "bool"; | 21 const char kBool[] = "bool"; |
22 const char kString[] = "string"; | 22 const char kString[] = "string"; |
23 const char kNeedsEscape[] = "\"quotes\""; | 23 const char kNeedsEscape[] = "\"quotes\""; |
24 | 24 |
25 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
26 const char kTaskFileName[] = "../../base/trace_event/trace_log.cc"; | 26 const char kTaskFileName[] = "../../base/trace_event/trace_log.cc"; |
27 const char kTaskPath[] = "base/trace_event"; | 27 const char kTaskPath[] = "base/trace_event"; |
28 #else | 28 #else |
29 const char kTaskFileName[] = "..\\..\\base\\memory\\memory_win.cc"; | 29 const char kTaskFileName[] = "..\\..\\base\\memory\\memory_win.cc"; |
30 const char kTaskPath[] = "base\\memory"; | 30 const char kTaskPath[] = "base\\memory"; |
31 #endif | 31 #endif |
32 | 32 |
33 scoped_ptr<Value> DumpAndReadBack(const TypeNameDeduplicator& deduplicator) { | 33 std::unique_ptr<Value> DumpAndReadBack( |
| 34 const TypeNameDeduplicator& deduplicator) { |
34 std::string json; | 35 std::string json; |
35 deduplicator.AppendAsTraceFormat(&json); | 36 deduplicator.AppendAsTraceFormat(&json); |
36 return JSONReader::Read(json); | 37 return JSONReader::Read(json); |
37 } | 38 } |
38 | 39 |
39 // Inserts a single type name into a new TypeNameDeduplicator instance and | 40 // Inserts a single type name into a new TypeNameDeduplicator instance and |
40 // checks if the value gets inserted and the exported value for |type_name| is | 41 // checks if the value gets inserted and the exported value for |type_name| is |
41 // the same as |expected_value|. | 42 // the same as |expected_value|. |
42 void TestInsertTypeAndReadback(const char* type_name, | 43 void TestInsertTypeAndReadback(const char* type_name, |
43 const char* expected_value) { | 44 const char* expected_value) { |
44 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); | 45 std::unique_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); |
45 ASSERT_EQ(1, dedup->Insert(type_name)); | 46 ASSERT_EQ(1, dedup->Insert(type_name)); |
46 | 47 |
47 scoped_ptr<Value> type_names = DumpAndReadBack(*dedup); | 48 std::unique_ptr<Value> type_names = DumpAndReadBack(*dedup); |
48 ASSERT_NE(nullptr, type_names); | 49 ASSERT_NE(nullptr, type_names); |
49 | 50 |
50 const DictionaryValue* dictionary; | 51 const DictionaryValue* dictionary; |
51 ASSERT_TRUE(type_names->GetAsDictionary(&dictionary)); | 52 ASSERT_TRUE(type_names->GetAsDictionary(&dictionary)); |
52 | 53 |
53 // When the type name was inserted, it got ID 1. The exported key "1" | 54 // When the type name was inserted, it got ID 1. The exported key "1" |
54 // should be equal to |expected_value|. | 55 // should be equal to |expected_value|. |
55 std::string value; | 56 std::string value; |
56 ASSERT_TRUE(dictionary->GetString("1", &value)); | 57 ASSERT_TRUE(dictionary->GetString("1", &value)); |
57 ASSERT_EQ(expected_value, value); | 58 ASSERT_EQ(expected_value, value); |
58 } | 59 } |
59 | 60 |
60 } // namespace | 61 } // namespace |
61 | 62 |
62 TEST(TypeNameDeduplicatorTest, Deduplication) { | 63 TEST(TypeNameDeduplicatorTest, Deduplication) { |
63 // The type IDs should be like this: | 64 // The type IDs should be like this: |
64 // 0: [unknown] | 65 // 0: [unknown] |
65 // 1: int | 66 // 1: int |
66 // 2: bool | 67 // 2: bool |
67 // 3: string | 68 // 3: string |
68 | 69 |
69 scoped_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); | 70 std::unique_ptr<TypeNameDeduplicator> dedup(new TypeNameDeduplicator); |
70 ASSERT_EQ(1, dedup->Insert(kInt)); | 71 ASSERT_EQ(1, dedup->Insert(kInt)); |
71 ASSERT_EQ(2, dedup->Insert(kBool)); | 72 ASSERT_EQ(2, dedup->Insert(kBool)); |
72 ASSERT_EQ(3, dedup->Insert(kString)); | 73 ASSERT_EQ(3, dedup->Insert(kString)); |
73 | 74 |
74 // Inserting again should return the same IDs. | 75 // Inserting again should return the same IDs. |
75 ASSERT_EQ(2, dedup->Insert(kBool)); | 76 ASSERT_EQ(2, dedup->Insert(kBool)); |
76 ASSERT_EQ(1, dedup->Insert(kInt)); | 77 ASSERT_EQ(1, dedup->Insert(kInt)); |
77 ASSERT_EQ(3, dedup->Insert(kString)); | 78 ASSERT_EQ(3, dedup->Insert(kString)); |
78 | 79 |
79 // A null pointer should yield type ID 0. | 80 // A null pointer should yield type ID 0. |
80 ASSERT_EQ(0, dedup->Insert(nullptr)); | 81 ASSERT_EQ(0, dedup->Insert(nullptr)); |
81 } | 82 } |
82 | 83 |
83 TEST(TypeNameDeduplicatorTest, EscapeTypeName) { | 84 TEST(TypeNameDeduplicatorTest, EscapeTypeName) { |
84 // Reading json should not fail, because the type name should have been | 85 // Reading json should not fail, because the type name should have been |
85 // escaped properly and exported value should contain quotes. | 86 // escaped properly and exported value should contain quotes. |
86 TestInsertTypeAndReadback(kNeedsEscape, kNeedsEscape); | 87 TestInsertTypeAndReadback(kNeedsEscape, kNeedsEscape); |
87 } | 88 } |
88 | 89 |
89 TEST(TypeNameDeduplicatorTest, TestExtractFileName) { | 90 TEST(TypeNameDeduplicatorTest, TestExtractFileName) { |
90 // The exported value for passed file name should be the folders in the path. | 91 // The exported value for passed file name should be the folders in the path. |
91 TestInsertTypeAndReadback(kTaskFileName, kTaskPath); | 92 TestInsertTypeAndReadback(kTaskFileName, kTaskPath); |
92 } | 93 } |
93 | 94 |
94 } // namespace trace_event | 95 } // namespace trace_event |
95 } // namespace base | 96 } // namespace base |
OLD | NEW |