This source file includes following definitions.
- SetInterceptFunctions
- ResetInterceptFunctions
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- main
#if defined(TYPE_PROFILING)
#include "base/allocator/type_profiler.h"
#include "base/allocator/type_profiler_control.h"
#include "base/allocator/type_profiler_tcmalloc.h"
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/tcmalloc/chromium/src/gperftools/type_profiler_map.h"
namespace base {
namespace type_profiler {
class TypeProfilerTest : public testing::Test {
public:
TypeProfilerTest() {}
void SetInterceptFunctions() {
InterceptFunctions::SetFunctions(NewInterceptForTCMalloc,
DeleteInterceptForTCMalloc);
}
void ResetInterceptFunctions() {
InterceptFunctions::ResetFunctions();
}
void SetUp() {
SetInterceptFunctions();
}
void TearDown() {
ResetInterceptFunctions();
}
protected:
static const size_t kDummyArraySize;
static const void* const kConstNull;
private:
DISALLOW_COPY_AND_ASSIGN(TypeProfilerTest);
};
const size_t TypeProfilerTest::kDummyArraySize = 10;
const void* const TypeProfilerTest::kConstNull = static_cast<const void*>(NULL);
TEST_F(TypeProfilerTest, TestNormalProfiling) {
int* dummy = new int(48);
const std::type_info* type;
type = LookupType(dummy);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(int).name(), type->name());
delete dummy;
type = LookupType(dummy);
EXPECT_EQ(kConstNull, type);
}
TEST_F(TypeProfilerTest, TestNormalArrayProfiling) {
int* dummy = new int[kDummyArraySize];
const std::type_info* type;
type = LookupType(dummy);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(int).name(), type->name());
delete[] dummy;
type = LookupType(dummy);
EXPECT_EQ(kConstNull, type);
}
TEST_F(TypeProfilerTest, TestRepeatedNewAndDelete) {
int *dummy[kDummyArraySize];
const std::type_info* type;
for (int i = 0; i < kDummyArraySize; ++i)
dummy[i] = new int(i);
for (int i = 0; i < kDummyArraySize; ++i) {
type = LookupType(dummy[i]);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(int).name(), type->name());
}
for (int i = 0; i < kDummyArraySize; ++i) {
delete dummy[i];
type = LookupType(dummy[i]);
ASSERT_EQ(kConstNull, type);
}
}
TEST_F(TypeProfilerTest, TestMultipleNewWithDroppingDelete) {
static const size_t large_size = 256 * 1024;
char* dummy_char = new char[large_size / sizeof(*dummy_char)];
const std::type_info* type;
type = LookupType(dummy_char);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(char).name(), type->name());
::operator delete[](dummy_char);
type = LookupType(dummy_char);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(char).name(), type->name());
int* dummy_int = new int[large_size / sizeof(*dummy_int) - 1];
ASSERT_EQ(static_cast<void*>(dummy_char), static_cast<void*>(dummy_int)) <<
"two new (malloc) calls didn't return the same address; retry it.";
type = LookupType(dummy_int);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(int).name(), type->name());
delete[] dummy_int;
type = LookupType(dummy_int);
EXPECT_EQ(kConstNull, type);
}
TEST_F(TypeProfilerTest, TestProfileDeleteWithoutProfiledNew) {
ResetInterceptFunctions();
int* dummy = new int(48);
const std::type_info* type;
SetInterceptFunctions();
delete dummy;
type = LookupType(dummy);
EXPECT_EQ(kConstNull, type);
ResetInterceptFunctions();
}
TEST_F(TypeProfilerTest, TestProfileNewWithoutProfiledDelete) {
int* dummy = new int(48);
const std::type_info* type;
EXPECT_TRUE(Controller::IsProfiling());
Controller::Stop();
EXPECT_FALSE(Controller::IsProfiling());
delete dummy;
type = LookupType(dummy);
ASSERT_NE(kConstNull, type);
EXPECT_STREQ(typeid(int).name(), type->name());
Controller::Restart();
EXPECT_TRUE(Controller::IsProfiling());
EraseType(dummy);
}
}
}
#endif
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}