gtestify realloc_unittest

This commit is contained in:
Aliaksey Kandratsenka 2024-03-17 13:38:14 -04:00
parent 0dddc476b3
commit f7c8b45dcd
4 changed files with 22 additions and 20 deletions

View File

@ -675,7 +675,7 @@ if(BUILD_TESTING)
add_test(proc_maps_iterator_test proc_maps_iterator_test)
add_executable(realloc_unittest src/tests/realloc_unittest.cc)
target_link_libraries(realloc_unittest tcmalloc_minimal)
target_link_libraries(realloc_unittest tcmalloc_minimal gtest)
add_test(realloc_unittest realloc_unittest)
add_executable(stack_trace_table_test
@ -720,7 +720,7 @@ if(GPERFTOOLS_BUILD_DEBUGALLOC)
endif()
add_executable(realloc_debug_unittest src/tests/realloc_unittest.cc)
target_link_libraries(realloc_debug_unittest PUBLIC tcmalloc_minimal_debug)
target_link_libraries(realloc_debug_unittest PUBLIC tcmalloc_minimal_debug gtest)
add_test(realloc_debug_unittest realloc_debug_unittest)
if(WITH_STACK_TRACE)

View File

@ -458,7 +458,8 @@ endif !MINGW
TESTS += realloc_unittest
realloc_unittest_SOURCES = src/tests/realloc_unittest.cc
realloc_unittest_LDFLAGS = $(TCMALLOC_FLAGS) $(AM_LDFLAGS)
realloc_unittest_LDADD = libtcmalloc_minimal.la
realloc_unittest_CPPFLAGS = $(gtest_CPPFLAGS)
realloc_unittest_LDADD = libtcmalloc_minimal.la libgtest.la
TESTS += thread_dealloc_unittest
thread_dealloc_unittest_SOURCES = src/tests/thread_dealloc_unittest.cc \
@ -545,7 +546,8 @@ TESTS += realloc_debug_unittest
realloc_debug_unittest_SOURCES = $(realloc_unittest_SOURCES)
realloc_debug_unittest_CXXFLAGS = $(realloc_unittest_CXXFLAGS)
realloc_debug_unittest_LDFLAGS = $(realloc_unittest_LDFLAGS)
realloc_debug_unittest_LDADD = libtcmalloc_minimal_debug.la
realloc_debug_unittest_CPPFLAGS = $(gtest_CPPFLAGS)
realloc_debug_unittest_LDADD = libtcmalloc_minimal_debug.la libgtest.la
# debugallocation_test checks that we print a proper stacktrace when
# debug-allocs fail, so we can't run it if we don't have stacktrace info.

View File

@ -34,15 +34,15 @@
// Test realloc() functionality
#include "config_for_unittests.h"
#include <assert.h> // for assert
#include <assert.h>
#include <stdio.h>
#include <stddef.h> // for size_t, NULL
#include <stdlib.h> // for free, malloc, realloc
#include <algorithm> // for min
#include "base/logging.h"
using std::min;
#include <stddef.h>
#include <stdlib.h>
#include <algorithm>
#include "tests/testutil.h"
#include "gtest/gtest.h"
// Fill a buffer of the specified size with a predetermined pattern
static void Fill(unsigned char* buffer, int n) {
@ -87,15 +87,15 @@ static int NextSize(int size) {
}
}
int main(int argc, char** argv) {
TEST(ReallocUnittest, Basics) {
for (int src_size = 0; src_size >= 0; src_size = NextSize(src_size)) {
for (int dst_size = 0; dst_size >= 0; dst_size = NextSize(dst_size)) {
unsigned char* src = (unsigned char*) malloc(src_size);
Fill(src, src_size);
unsigned char* dst = (unsigned char*) realloc(src, dst_size);
CHECK(Valid(dst, min(src_size, dst_size)));
ASSERT_TRUE(Valid(dst, std::min(src_size, dst_size)));
Fill(dst, dst_size);
CHECK(Valid(dst, dst_size));
ASSERT_TRUE(Valid(dst, dst_size));
if (dst != NULL) free(dst);
}
}
@ -104,22 +104,19 @@ int main(int argc, char** argv) {
// packed cache, so some entries are evicted from the cache.
// The cache has 2^12 entries, keyed by page number.
const int kNumEntries = 1 << 14;
int** p = (int**)malloc(sizeof(*p) * kNumEntries);
int** p = (int**)noopt(malloc(sizeof(*p) * kNumEntries));
int sum = 0;
for (int i = 0; i < kNumEntries; i++) {
p[i] = (int*)malloc(8192); // no page size is likely to be bigger
p[i][1000] = i; // use memory deep in the heart of p
}
for (int i = 0; i < kNumEntries; i++) {
p[i] = (int*)realloc(p[i], 9000);
p[i] = (int*)noopt(realloc(p[i], 9000));
}
for (int i = 0; i < kNumEntries; i++) {
sum += p[i][1000];
free(p[i]);
}
CHECK_EQ(kNumEntries/2 * (kNumEntries - 1), sum); // assume kNE is even
ASSERT_EQ(kNumEntries/2 * (kNumEntries - 1), sum); // assume kNE is even
free(p);
printf("PASS\n");
return 0;
}

View File

@ -214,6 +214,9 @@
<Project>{55e2b3ae-3ca1-4db6-97f7-0a044d6f446f}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\gtest\gtest.vcxproj">
<Project>{0496df42-d7ad-46b6-b10c-c57a07e89b0d}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">