1 // Copyright 2012 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.
5 // This is a unittest set for type_profiler_map in third_party/tcmalloc. It is
6 // independent from other tests and executed manually like allocator_unittests
7 // since type_profiler_map is a singleton (like TCMalloc's heap-profiler), and
8 // it requires RTTI and different compiling/linking options from others.
10 #if defined(TYPE_PROFILING)
12 #include "base/memory/scoped_ptr.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/tcmalloc/chromium/src/gperftools/type_profiler_map.h"
17 namespace type_profiler {
19 static const void* const g_const_null = static_cast<const void*>(NULL);
21 TEST(TypeProfilerMapTest, NormalOperation) {
22 // Allocate an object just to get a valid address.
23 // This 'new' is not profiled by type_profiler.
24 scoped_ptr<int> dummy(new int(48));
25 const std::type_info* type;
27 type = LookupType(dummy.get());
28 EXPECT_EQ(g_const_null, type);
30 InsertType(dummy.get(), 12, typeid(int));
31 type = LookupType(dummy.get());
32 ASSERT_NE(g_const_null, type);
33 EXPECT_STREQ(typeid(int).name(), type->name());
35 EraseType(dummy.get());
36 type = LookupType(dummy.get());
37 EXPECT_EQ(g_const_null, type);
40 TEST(TypeProfilerMapTest, EraseWithoutInsert) {
41 scoped_ptr<int> dummy(new int(48));
42 const std::type_info* type;
44 for (int i = 0; i < 10; ++i) {
45 EraseType(dummy.get());
46 type = LookupType(dummy.get());
47 EXPECT_EQ(g_const_null, type);
51 TEST(TypeProfilerMapTest, InsertThenMultipleErase) {
52 scoped_ptr<int> dummy(new int(48));
53 const std::type_info* type;
55 InsertType(dummy.get(), 12, typeid(int));
56 type = LookupType(dummy.get());
57 ASSERT_NE(g_const_null, type);
58 EXPECT_STREQ(typeid(int).name(), type->name());
60 for (int i = 0; i < 10; ++i) {
61 EraseType(dummy.get());
62 type = LookupType(dummy.get());
63 EXPECT_EQ(g_const_null, type);
67 TEST(TypeProfilerMapTest, MultipleInsertWithoutErase) {
68 scoped_ptr<int> dummy(new int(48));
69 const std::type_info* type;
71 InsertType(dummy.get(), 12, typeid(int));
72 type = LookupType(dummy.get());
73 ASSERT_NE(g_const_null, type);
74 EXPECT_STREQ(typeid(int).name(), type->name());
76 InsertType(dummy.get(), 5, typeid(char));
77 type = LookupType(dummy.get());
78 ASSERT_NE(g_const_null, type);
79 EXPECT_STREQ(typeid(char).name(), type->name());
81 InsertType(dummy.get(), 129, typeid(long));
82 type = LookupType(dummy.get());
83 ASSERT_NE(g_const_null, type);
84 EXPECT_STREQ(typeid(long).name(), type->name());
86 EraseType(dummy.get());
87 type = LookupType(dummy.get());
88 EXPECT_EQ(g_const_null, type);
91 } // namespace type_profiler
94 #endif // defined(TYPE_PROFILING)
96 int main(int argc, char** argv) {
97 testing::InitGoogleTest(&argc, argv);
98 return RUN_ALL_TESTS();