Chromium Code Reviews| Index: base/allocator/type_profiler_unittests.cc |
| diff --git a/base/allocator/type_profiler_unittests.cc b/base/allocator/type_profiler_unittests.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..87756844e0b7e1266d5d3d6e3f8574352009dc3c |
| --- /dev/null |
| +++ b/base/allocator/type_profiler_unittests.cc |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2012 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. |
| + |
| +// This is a unittest set for type_profiler. It is independent from other |
| +// tests and executed manually like allocator_unittests because type_profiler |
| +// is not able to turn on/off many times. |
| +// |
| +// It tests that the profiler doesn't fail in suspicous cases. For example, |
| +// 'new' is not profiled, but 'delete' for the created object is profiled. |
| +// |
| +// NOTE: This test must be run with an environment variable "HEAPPROFILE". |
| +// |
| +// NOTE: Each test in TypeProfilerTest must be executed separately with using |
| +// --gtest_filter like following : |
| +// --gtest_filter="TypeProfilerTest.NormalProfiling" |
| +// because type_profiler provides only an initializer and a stopper for safety. |
| +// It can be initialized once, and stopped once. |
| + |
| +#include "base/allocator/type_profiler_control.h" |
| +#include "base/allocator/type_profiler_tcmalloc.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/tcmalloc/chromium/src/gperftools/type_profiler_map.h" |
| + |
| +#define DUMMY_ARRAY_SIZE 10 |
|
jar (doing other things)
2012/08/30 16:02:10
nit: In C++, we prefer using of static const size_
Dai Mikurube (NOT FULLTIME)
2012/08/31 06:10:30
Done.
|
| + |
| +#if defined(PROFILING_TYPE) |
| +static const void* const g_const_null = static_cast<const void*>(NULL); |
| + |
| +TEST(TypeProfilerTest, NormalProfiling) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + type = TypeProfilerLookupType(dummy); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + delete dummy; |
| + |
| + type = TypeProfilerLookupType(dummy); |
| + EXPECT_EQ(type, g_const_null); |
| +} |
| + |
| +TEST(TypeProfilerTest, NormalArrayProfiling) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + int* dummy = new int[DUMMY_ARRAY_SIZE]; |
| + const std::type_info* type; |
| + |
| + type = TypeProfilerLookupType(dummy); |
| + ASSERT_NE(type, g_const_null); |
| + // For an array, the profiler remembers its base type. |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + delete[] dummy; |
| + |
| + type = TypeProfilerLookupType(dummy); |
| + EXPECT_EQ(type, g_const_null); |
| +} |
| + |
| +TEST(TypeProfilerTest, RepeatedNewAndDelete) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + int *dummy[DUMMY_ARRAY_SIZE]; |
| + const std::type_info* type; |
| + for (int i = 0; i < DUMMY_ARRAY_SIZE; ++i) |
| + dummy[i] = new int(i); |
| + |
| + for (int i = 0; i < DUMMY_ARRAY_SIZE; ++i) { |
| + type = TypeProfilerLookupType(dummy[i]); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + } |
| + |
| + for (int i = 0; i < DUMMY_ARRAY_SIZE; ++i) { |
| + delete dummy[i]; |
| + type = TypeProfilerLookupType(dummy[i]); |
| + ASSERT_EQ(type, g_const_null); |
| + } |
| +} |
| + |
| +TEST(TypeProfilerTest, ProfileDeleteWithoutProfiledNew) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + delete dummy; |
| + |
| + type = TypeProfilerLookupType(dummy); |
| + EXPECT_EQ(type, g_const_null); |
| +} |
| + |
| +TEST(TypeProfilerTest, ProfileNewWithoutProfiledDelete) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + base::type_profiler::StopProfiling(); |
| + |
| + delete dummy; |
| + |
| + // NOTE: We accept that a profile entry remains when a profiled object is |
| + // deleted after StopProfiling(). |
| + type = TypeProfilerLookupType(dummy); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| +} |
| +#endif // defined(PROFILING_TYPE) |
| + |
| +int main(int argc, char** argv) { |
| + testing::InitGoogleTest(&argc, argv); |
| + return RUN_ALL_TESTS(); |
| +} |