gtestify low_level_alloc_unittest
This commit is contained in:
parent
6c24a59682
commit
a46a391b14
|
@ -569,7 +569,7 @@ if(BUILD_TESTING)
|
|||
# By default, MallocHook takes stack traces for use by the heap-checker.
|
||||
# We don't need that functionality here, so we turn it off to reduce deps.
|
||||
target_compile_definitions(low_level_alloc_unittest PRIVATE NO_TCMALLOC_SAMPLES)
|
||||
target_link_libraries(low_level_alloc_unittest spinlock sysinfo logging)
|
||||
target_link_libraries(low_level_alloc_unittest spinlock sysinfo logging gtest)
|
||||
add_test(low_level_alloc_unittest low_level_alloc_unittest)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -195,7 +195,8 @@ low_level_alloc_unittest_SOURCES = src/base/low_level_alloc.cc \
|
|||
# By default, MallocHook takes stack traces for use by the heap-checker.
|
||||
# We don't need that functionality here, so we turn it off to reduce deps.
|
||||
low_level_alloc_unittest_CXXFLAGS = -DNO_TCMALLOC_SAMPLES $(AM_CXXFLAGS)
|
||||
low_level_alloc_unittest_LDADD = libcommon.la
|
||||
low_level_alloc_unittest_CPPFLAGS = $(gtest_CPPFLAGS)
|
||||
low_level_alloc_unittest_LDADD = libcommon.la libgtest.la
|
||||
endif WITH_HEAP_PROFILER_OR_CHECKER
|
||||
|
||||
### ------- stack trace
|
||||
|
|
|
@ -31,13 +31,13 @@
|
|||
|
||||
// A test for low_level_alloc.cc
|
||||
|
||||
#include "base/low_level_alloc.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <map>
|
||||
#include "base/low_level_alloc.h"
|
||||
#include "base/logging.h"
|
||||
#include <gperftools/malloc_hook.h>
|
||||
|
||||
using std::map;
|
||||
#include <gperftools/malloc_hook.h>
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
// a block of memory obtained from the allocator
|
||||
struct BlockDesc {
|
||||
|
@ -50,7 +50,7 @@ struct BlockDesc {
|
|||
// by RandomizeBlockDesc is still there.
|
||||
static void CheckBlockDesc(const BlockDesc &d) {
|
||||
for (int i = 0; i != d.len; i++) {
|
||||
CHECK((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
|
||||
ASSERT_TRUE((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -79,8 +79,8 @@ static bool using_low_level_alloc = false;
|
|||
// If call_malloc_hook is true and user_arena is true,
|
||||
// allocations and deallocations are reported via the MallocHook
|
||||
// interface.
|
||||
static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
|
||||
typedef map<int, BlockDesc> AllocMap;
|
||||
static void ExerciseAllocator(bool use_new_arena, bool call_malloc_hook, int n) {
|
||||
typedef std::map<int, BlockDesc> AllocMap;
|
||||
AllocMap allocated;
|
||||
AllocMap::iterator it;
|
||||
BlockDesc block_desc;
|
||||
|
@ -140,7 +140,7 @@ static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
|
|||
allocated.erase(it);
|
||||
}
|
||||
if (use_new_arena) {
|
||||
CHECK(LowLevelAlloc::DeleteArena(arena));
|
||||
ASSERT_TRUE(LowLevelAlloc::DeleteArena(arena));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -162,34 +162,29 @@ static void FreeHook(const void *p) {
|
|||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "USAGE: %s\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
TEST(LowLevelAllocTest, Basic) {
|
||||
ASSERT_TRUE(MallocHook::AddNewHook(&AllocHook));
|
||||
ASSERT_TRUE(MallocHook::AddDeleteHook(&FreeHook));
|
||||
ASSERT_EQ(allocates, 0);
|
||||
ASSERT_EQ(frees, 0);
|
||||
|
||||
CHECK(MallocHook::AddNewHook(&AllocHook));
|
||||
CHECK(MallocHook::AddDeleteHook(&FreeHook));
|
||||
CHECK_EQ(allocates, 0);
|
||||
CHECK_EQ(frees, 0);
|
||||
Test(false, false, 50000);
|
||||
CHECK_NE(allocates, 0); // default arena calls hooks
|
||||
CHECK_NE(frees, 0);
|
||||
ExerciseAllocator(false, false, 50000);
|
||||
ASSERT_NE(allocates, 0); // default arena calls hooks
|
||||
ASSERT_NE(frees, 0);
|
||||
for (int i = 0; i != 16; i++) {
|
||||
bool call_hooks = ((i & 1) == 1);
|
||||
allocates = 0;
|
||||
frees = 0;
|
||||
Test(true, call_hooks, 15000);
|
||||
ExerciseAllocator(true, call_hooks, 15000);
|
||||
if (call_hooks) {
|
||||
CHECK_GT(allocates, 5000); // arena calls hooks
|
||||
CHECK_GT(frees, 5000);
|
||||
ASSERT_GT(allocates, 5000); // arena calls hooks
|
||||
ASSERT_GT(frees, 5000);
|
||||
} else {
|
||||
CHECK_EQ(allocates, 0); // arena doesn't call hooks
|
||||
CHECK_EQ(frees, 0);
|
||||
ASSERT_EQ(allocates, 0); // arena doesn't call hooks
|
||||
ASSERT_EQ(frees, 0);
|
||||
}
|
||||
}
|
||||
printf("\nPASS\n");
|
||||
CHECK(MallocHook::RemoveNewHook(&AllocHook));
|
||||
CHECK(MallocHook::RemoveDeleteHook(&FreeHook));
|
||||
return 0;
|
||||
|
||||
ASSERT_TRUE(MallocHook::RemoveNewHook(&AllocHook));
|
||||
ASSERT_TRUE(MallocHook::RemoveDeleteHook(&FreeHook));
|
||||
}
|
||||
|
|
|
@ -236,6 +236,11 @@
|
|||
<ClInclude Include="..\config.h" />
|
||||
<ClInclude Include="..\..\src\windows\port.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\gtest\gtest.vcxproj">
|
||||
<Project>{0496df42-d7ad-46b6-b10c-c57a07e89b0d}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
|
Loading…
Reference in New Issue