From f7c8b45dcdb9d21e1a43e5631603d22684fa52e6 Mon Sep 17 00:00:00 2001 From: Aliaksey Kandratsenka Date: Sun, 17 Mar 2024 13:38:14 -0400 Subject: [PATCH] gtestify realloc_unittest --- CMakeLists.txt | 4 +-- Makefile.am | 6 ++-- src/tests/realloc_unittest.cc | 29 +++++++++---------- .../realloc_unittest/realloc_unittest.vcxproj | 3 ++ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c4761f..216a75e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Makefile.am b/Makefile.am index a9926c5..dd72dfa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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. diff --git a/src/tests/realloc_unittest.cc b/src/tests/realloc_unittest.cc index a4ea17c..02ebb88 100644 --- a/src/tests/realloc_unittest.cc +++ b/src/tests/realloc_unittest.cc @@ -34,15 +34,15 @@ // Test realloc() functionality #include "config_for_unittests.h" -#include // for assert + +#include #include -#include // for size_t, NULL -#include // for free, malloc, realloc -#include // for min -#include "base/logging.h" - -using std::min; +#include +#include +#include +#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; } diff --git a/vsprojects/realloc_unittest/realloc_unittest.vcxproj b/vsprojects/realloc_unittest/realloc_unittest.vcxproj index 4ec8e47..76a553e 100644 --- a/vsprojects/realloc_unittest/realloc_unittest.vcxproj +++ b/vsprojects/realloc_unittest/realloc_unittest.vcxproj @@ -214,6 +214,9 @@ {55e2b3ae-3ca1-4db6-97f7-0a044d6f446f} false + + {0496df42-d7ad-46b6-b10c-c57a07e89b0d} +