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..2e93388d1887d40e7cd8f9c70e28ae580a5c10d8 |
| --- /dev/null |
| +++ b/base/allocator/type_profiler_unittests.cc |
| @@ -0,0 +1,183 @@ |
| +// 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" |
| + |
| +#include <iostream> |
|
jar (doing other things)
2012/09/02 01:37:11
nit: include order
Dai Mikurube (NOT FULLTIME)
2012/09/03 08:16:30
Removed it. (I overlooked.)
|
| + |
| +#if defined(TYPE_PROFILING) |
| +static const size_t g_dummy_array_size = 10; |
| +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( |
|
jar (doing other things)
2012/09/02 01:37:11
Please put this entire test suite into the base::t
Dai Mikurube (NOT FULLTIME)
2012/09/03 08:16:30
Done.
|
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + int* dummy = new int(48); |
| + const std::type_info* type; |
| + |
| + type = base::type_profiler::LookupType(dummy); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + delete dummy; |
| + |
| + type = base::type_profiler::LookupType(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[g_dummy_array_size]; |
| + const std::type_info* type; |
| + |
| + type = base::type_profiler::LookupType(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 = base::type_profiler::LookupType(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[g_dummy_array_size]; |
| + const std::type_info* type; |
| + for (int i = 0; i < g_dummy_array_size; ++i) |
| + dummy[i] = new int(i); |
| + |
| + for (int i = 0; i < g_dummy_array_size; ++i) { |
| + type = base::type_profiler::LookupType(dummy[i]); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + } |
| + |
| + for (int i = 0; i < g_dummy_array_size; ++i) { |
| + delete dummy[i]; |
| + type = base::type_profiler::LookupType(dummy[i]); |
| + ASSERT_EQ(type, g_const_null); |
| + } |
| +} |
| + |
| +TEST(TypeProfilerTest, MultipleNewWithDroppingDelete) { |
| + ASSERT_FALSE(base::type_profiler::IsInterceptFunctionsAvailable()) << |
| + "this test must be executed separately."; |
| + |
| + static const size_t large_size = 256 * 1024; |
| + |
| + base::type_profiler::SetInterceptFunctions( |
| + base::type_profiler::NewInterceptForTCMalloc, |
| + base::type_profiler::DeleteInterceptForTCMalloc); |
| + |
| + char* dummy_char = new char[large_size]; |
| + const std::type_info* type; |
| + |
| + type = base::type_profiler::LookupType(dummy_char); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(char).name()); |
| + |
| + // Call ::operator delete directly to drop __op_delete_intercept__. |
| + ::operator delete(dummy_char); |
| + |
| + type = base::type_profiler::LookupType(dummy_char); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(char).name()); |
| + |
| + // Allocates a little different size. |
| + int* dummy_int = new int[large_size / sizeof(int) - 1]; |
| + |
| + // We expect that tcmalloc returns the same address for these large (over 32k) |
| + // allocation calls. It usually happens, but maybe probablistic. |
| + ASSERT_EQ(static_cast<void*>(dummy_int), static_cast<void*>(dummy_char)) << |
| + "two new (malloc) calls didn't return the same address; retry it."; |
| + |
| + type = base::type_profiler::LookupType(dummy_int); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| + |
| + delete dummy_int; |
| + |
| + type = base::type_profiler::LookupType(dummy_int); |
| + EXPECT_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 = base::type_profiler::LookupType(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 = base::type_profiler::LookupType(dummy); |
| + ASSERT_NE(type, g_const_null); |
| + EXPECT_STREQ(type->name(), typeid(int).name()); |
| +} |
| +#endif // defined(TYPE_PROFILING) |
| + |
| +int main(int argc, char** argv) { |
| + testing::InitGoogleTest(&argc, argv); |
| + return RUN_ALL_TESTS(); |
| +} |