- add sources.
[platform/framework/web/crosswalk.git] / src / base / allocator / type_profiler_map_unittests.cc
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.
4
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.
9
10 #if defined(TYPE_PROFILING)
11
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"
15
16 namespace base {
17 namespace type_profiler {
18
19 static const void* const g_const_null = static_cast<const void*>(NULL);
20
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;
26
27   type = LookupType(dummy.get());
28   EXPECT_EQ(g_const_null, type);
29
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());
34
35   EraseType(dummy.get());
36   type = LookupType(dummy.get());
37   EXPECT_EQ(g_const_null, type);
38 }
39
40 TEST(TypeProfilerMapTest, EraseWithoutInsert) {
41   scoped_ptr<int> dummy(new int(48));
42   const std::type_info* type;
43
44   for (int i = 0; i < 10; ++i) {
45     EraseType(dummy.get());
46     type = LookupType(dummy.get());
47     EXPECT_EQ(g_const_null, type);
48   }
49 }
50
51 TEST(TypeProfilerMapTest, InsertThenMultipleErase) {
52   scoped_ptr<int> dummy(new int(48));
53   const std::type_info* type;
54
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());
59
60   for (int i = 0; i < 10; ++i) {
61     EraseType(dummy.get());
62     type = LookupType(dummy.get());
63     EXPECT_EQ(g_const_null, type);
64   }
65 }
66
67 TEST(TypeProfilerMapTest, MultipleInsertWithoutErase) {
68   scoped_ptr<int> dummy(new int(48));
69   const std::type_info* type;
70
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());
75
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());
80
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());
85
86   EraseType(dummy.get());
87   type = LookupType(dummy.get());
88   EXPECT_EQ(g_const_null, type);
89 }
90
91 }  // namespace type_profiler
92 }  // namespace base
93
94 #endif  // defined(TYPE_PROFILING)
95
96 int main(int argc, char** argv) {
97   testing::InitGoogleTest(&argc, argv);
98   return RUN_ALL_TESTS();
99 }