From 397e25a6abe38b0d6f7b29135f3f6aef136860a4 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sat, 6 Jan 2024 17:53:57 +0800 Subject: [PATCH] Refactor test cases for pmr_memory_resource, pmr_new, pmr_allocator, pmr_block_pool, and pmr_monotonic_buffer_resource --- test/pmr/test_pmr_allocator.cpp | 12 +++++------ test/pmr/test_pmr_block_pool.cpp | 12 +++++------ test/pmr/test_pmr_memory_resource.cpp | 4 ++-- .../test_pmr_monotonic_buffer_resource.cpp | 12 +++++------ test/pmr/test_pmr_new.cpp | 21 ++++++++++++++++++- test/pmr/test_pmr_small_storage.cpp | 18 ++++++++-------- 6 files changed, 49 insertions(+), 30 deletions(-) diff --git a/test/pmr/test_pmr_allocator.cpp b/test/pmr/test_pmr_allocator.cpp index 8173c82..f55a264 100644 --- a/test/pmr/test_pmr_allocator.cpp +++ b/test/pmr/test_pmr_allocator.cpp @@ -6,12 +6,12 @@ #include "libpmr/allocator.h" -TEST(allocator, construct) { +TEST(pmr_allocator, construct) { pmr::allocator alc; SUCCEED(); } -TEST(allocator, construct_value_initialization) { +TEST(pmr_allocator, construct_value_initialization) { pmr::allocator alc{}; auto p = alc.allocate(128); EXPECT_NE(p, nullptr); @@ -32,7 +32,7 @@ public: } // namespace -TEST(allocator, construct_copy_move) { +TEST(pmr_allocator, construct_copy_move) { pmr::new_delete_resource mem_res; dummy_resource dummy_res; pmr::allocator alc1{&mem_res}, alc2{&dummy_res}; @@ -54,7 +54,7 @@ TEST(allocator, construct_copy_move) { ASSERT_NO_THROW(alc5.deallocate(p, 128)); } -TEST(allocator, swap) { +TEST(pmr_allocator, swap) { pmr::new_delete_resource mem_res; dummy_resource dummy_res; pmr::allocator alc1{&mem_res}, alc2{&dummy_res}; @@ -65,7 +65,7 @@ TEST(allocator, swap) { ASSERT_EQ(alc1.allocate(128), nullptr); } -TEST(allocator, invalid_alloc_free) { +TEST(pmr_allocator, invalid_alloc_free) { pmr::allocator alc1; EXPECT_EQ(alc1.allocate(0), nullptr); EXPECT_NO_THROW(alc1.deallocate(nullptr, 128)); @@ -73,6 +73,6 @@ TEST(allocator, invalid_alloc_free) { EXPECT_NO_THROW(alc1.deallocate(&alc1, 0)); } -TEST(allocator, sizeof) { +TEST(pmr_allocator, sizeof) { EXPECT_EQ(sizeof(pmr::allocator), sizeof(void *) * 2); } diff --git a/test/pmr/test_pmr_block_pool.cpp b/test/pmr/test_pmr_block_pool.cpp index 5f66684..f98f89f 100644 --- a/test/pmr/test_pmr_block_pool.cpp +++ b/test/pmr/test_pmr_block_pool.cpp @@ -6,7 +6,7 @@ #include "libpmr/block_pool.h" -TEST(block_pool, central_cache_allocator) { +TEST(pmr_block_pool, central_cache_allocator) { auto &a = pmr::central_cache_allocator(); ASSERT_FALSE(nullptr == a.allocate(1)); ASSERT_FALSE(nullptr == a.allocate(10)); @@ -15,7 +15,7 @@ TEST(block_pool, central_cache_allocator) { ASSERT_FALSE(nullptr == a.allocate(10000)); } -TEST(block_pool, block) { +TEST(pmr_block_pool, block) { pmr::block<1> b1; EXPECT_EQ(sizeof(b1), (std::max)(alignof(std::max_align_t), sizeof(void *))); pmr::block b2; @@ -24,7 +24,7 @@ TEST(block_pool, block) { EXPECT_EQ(sizeof(b3), (std::max)(alignof(std::max_align_t), sizeof(void *) * 2)); } -TEST(block_pool, central_cache_pool_ctor) { +TEST(pmr_block_pool, central_cache_pool_ctor) { ASSERT_FALSE((std::is_default_constructible, 1>>::value)); ASSERT_FALSE((std::is_copy_constructible, 1>>::value)); ASSERT_FALSE((std::is_move_constructible, 1>>::value)); @@ -62,15 +62,15 @@ TEST(block_pool, central_cache_pool_ctor) { } } -TEST(block_pool, ctor) { +TEST(pmr_block_pool, ctor) { ASSERT_TRUE ((std::is_default_constructible>::value)); ASSERT_FALSE((std::is_copy_constructible>::value)); - ASSERT_FALSE((std::is_move_constructible>::value)); + ASSERT_TRUE ((std::is_move_constructible>::value)); ASSERT_FALSE((std::is_copy_assignable>::value)); ASSERT_FALSE((std::is_move_assignable>::value)); } -TEST(block_pool, allocate) { +TEST(pmr_block_pool, allocate) { std::vector v; pmr::block_pool<1, 1> pool; for (int i = 0; i < 100; ++i) { diff --git a/test/pmr/test_pmr_memory_resource.cpp b/test/pmr/test_pmr_memory_resource.cpp index b624135..e322c32 100644 --- a/test/pmr/test_pmr_memory_resource.cpp +++ b/test/pmr/test_pmr_memory_resource.cpp @@ -24,7 +24,7 @@ void *test_mr(T &&mr, std::size_t bytes, std::size_t alignment) { } // namespace -TEST(memory_resource, traits) { +TEST(pmr_memory_resource, traits) { EXPECT_FALSE(pmr::has_allocate::value); EXPECT_FALSE(pmr::has_allocate::value); EXPECT_FALSE(pmr::has_allocate>::value); @@ -44,7 +44,7 @@ TEST(memory_resource, traits) { #endif } -TEST(memory_resource, new_delete_resource) { +TEST(pmr_memory_resource, new_delete_resource) { pmr::new_delete_resource mem_res; EXPECT_EQ(test_mr(mem_res, 0, 0), nullptr); diff --git a/test/pmr/test_pmr_monotonic_buffer_resource.cpp b/test/pmr/test_pmr_monotonic_buffer_resource.cpp index a4fe77a..6703121 100644 --- a/test/pmr/test_pmr_monotonic_buffer_resource.cpp +++ b/test/pmr/test_pmr_monotonic_buffer_resource.cpp @@ -7,7 +7,7 @@ #include "libpmr/monotonic_buffer_resource.h" -TEST(monotonic_buffer_resource, construct) { +TEST(pmr_monotonic_buffer_resource, construct) { { pmr::monotonic_buffer_resource tmp; } pmr::monotonic_buffer_resource{}; pmr::monotonic_buffer_resource{pmr::allocator{}}; @@ -18,14 +18,14 @@ TEST(monotonic_buffer_resource, construct) { SUCCEED(); } -TEST(monotonic_buffer_resource, no_copy) { +TEST(pmr_monotonic_buffer_resource, no_copy) { EXPECT_FALSE(std::is_copy_constructible::value); EXPECT_FALSE(std::is_copy_assignable::value); EXPECT_FALSE(std::is_move_constructible::value); EXPECT_FALSE(std::is_move_assignable::value); } -TEST(monotonic_buffer_resource, upstream_resource) { +TEST(pmr_monotonic_buffer_resource, upstream_resource) { struct dummy_allocator { bool allocated = false; void *allocate(std::size_t, std::size_t) noexcept { allocated = true; return nullptr; } @@ -52,7 +52,7 @@ struct dummy_allocator { } // namespace -TEST(monotonic_buffer_resource, allocate) { +TEST(pmr_monotonic_buffer_resource, allocate) { dummy_allocator dummy; { pmr::monotonic_buffer_resource tmp{&dummy}; @@ -76,7 +76,7 @@ TEST(monotonic_buffer_resource, allocate) { ASSERT_EQ(dummy.allocated, 0); } -TEST(monotonic_buffer_resource, allocate_by_buffer) { +TEST(pmr_monotonic_buffer_resource, allocate_by_buffer) { dummy_allocator dummy; std::array buffer; { @@ -95,7 +95,7 @@ TEST(monotonic_buffer_resource, allocate_by_buffer) { ASSERT_EQ(dummy.allocated, 0); } -TEST(monotonic_buffer_resource, release) { +TEST(pmr_monotonic_buffer_resource, release) { dummy_allocator dummy; { pmr::monotonic_buffer_resource tmp{&dummy}; diff --git a/test/pmr/test_pmr_new.cpp b/test/pmr/test_pmr_new.cpp index efa1182..dff6663 100644 --- a/test/pmr/test_pmr_new.cpp +++ b/test/pmr/test_pmr_new.cpp @@ -68,6 +68,8 @@ TEST(pmr_new, new$array) { namespace { +int construct_count__ = 0; + class Base { public: virtual ~Base() = default; @@ -76,7 +78,14 @@ public: class Derived : public Base { public: - Derived(int value) : value_(value) {} + Derived(int value) : value_(value) { + construct_count__ = value_; + } + + ~Derived() override { + construct_count__ = 0; + } + int get() const override { return value_; } private: @@ -89,9 +98,19 @@ TEST(pmr_new, delete$poly) { Base *p = pmr::new$(-1); ASSERT_NE(p, nullptr); ASSERT_EQ(p->get(), -1); + ASSERT_EQ(construct_count__, -1); pmr::delete$(p); + ASSERT_EQ(construct_count__, 0); ASSERT_EQ(p, pmr::new$((std::numeric_limits::max)())); ASSERT_EQ(p->get(), (std::numeric_limits::max)()); + ASSERT_EQ(construct_count__, (std::numeric_limits::max)()); pmr::delete$(p); + ASSERT_EQ(construct_count__, 0); +} + +TEST(pmr_new, delete$null) { + Base *p = nullptr; + pmr::delete$(p); + SUCCEED(); } diff --git a/test/pmr/test_pmr_small_storage.cpp b/test/pmr/test_pmr_small_storage.cpp index 0f57513..c2da3f9 100644 --- a/test/pmr/test_pmr_small_storage.cpp +++ b/test/pmr/test_pmr_small_storage.cpp @@ -6,7 +6,7 @@ #include "libpmr/small_storage.h" -TEST(small_storage, holder_construct) { +TEST(pmr_small_storage, holder_construct) { pmr::holder_null(); pmr::holder(); pmr::holder(); @@ -15,7 +15,7 @@ TEST(small_storage, holder_construct) { SUCCEED(); } -TEST(small_storage, holder_copy_move_construct) { +TEST(pmr_small_storage, holder_copy_move_construct) { EXPECT_FALSE(std::is_copy_constructible::value); EXPECT_FALSE((std::is_copy_constructible>::value)); EXPECT_FALSE((std::is_copy_constructible>::value)); @@ -52,7 +52,7 @@ struct foo { } // namespace -TEST(small_storage, holder_copy_move_object_stack) { +TEST(pmr_small_storage, holder_copy_move_object_stack) { pmr::allocator alc; pmr::holder h1(alc, 1); pmr::holder h2, h3; @@ -67,7 +67,7 @@ TEST(small_storage, holder_copy_move_object_stack) { h3.destroy(alc); } -TEST(small_storage, holder_copy_move_object_heap) { +TEST(pmr_small_storage, holder_copy_move_object_heap) { pmr::allocator alc; pmr::holder h4(alc, 1); pmr::holder h5, h6; @@ -82,7 +82,7 @@ TEST(small_storage, holder_copy_move_object_heap) { h6.destroy(alc); } -TEST(small_storage, holder_copy_move_array_stack) { +TEST(pmr_small_storage, holder_copy_move_array_stack) { pmr::allocator alc; void *ph1 = std::malloc(pmr::holder::full_sizeof(10)); void *ph2 = std::malloc(pmr::holder::full_sizeof(10)); @@ -104,7 +104,7 @@ TEST(small_storage, holder_copy_move_array_stack) { std::free(ph3); } -TEST(small_storage, holder_copy_move_array_heap) { +TEST(pmr_small_storage, holder_copy_move_array_heap) { pmr::allocator alc; pmr::holder h10(alc, ::LIBIMP::types{}, 10); pmr::holder h11, h12; @@ -119,7 +119,7 @@ TEST(small_storage, holder_copy_move_array_heap) { h12.destroy(alc); } -TEST(small_storage, sizeof) { +TEST(pmr_small_storage, sizeof) { EXPECT_EQ(sizeof(pmr::holder_null), sizeof(void *)); EXPECT_EQ(sizeof(pmr::holder), sizeof(void *) + imp::round_up(sizeof(int), alignof(void *))); EXPECT_EQ(sizeof(pmr::holder), sizeof(void *) + sizeof(void *)); @@ -133,12 +133,12 @@ TEST(small_storage, sizeof) { EXPECT_GT(sizeof(pmr::small_storage<4096>), 4096); } -TEST(small_storage, construct) { +TEST(pmr_small_storage, construct) { pmr::small_storage<64> ss; SUCCEED(); } -TEST(small_storage, acquire) { +TEST(pmr_small_storage, acquire) { pmr::small_storage<128> ss; pmr::allocator alc; ASSERT_FALSE(ss.valid());