From d4dcd2129762d6caa51a0fd81ce3e23d9ffca110 Mon Sep 17 00:00:00 2001 From: Jan Babst Date: Thu, 30 Jan 2025 16:34:05 +0100 Subject: [PATCH] Improved constructor tests --- .../inplace_vector/constructors.test.cpp | 52 +++++++++---------- tests/beman/inplace_vector/gtest_setup.hpp | 9 +++- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/tests/beman/inplace_vector/constructors.test.cpp b/tests/beman/inplace_vector/constructors.test.cpp index 00c21a4..6e018f8 100644 --- a/tests/beman/inplace_vector/constructors.test.cpp +++ b/tests/beman/inplace_vector/constructors.test.cpp @@ -26,26 +26,20 @@ TYPED_TEST(Constructors, SizedDefault) { constexpr auto mid_size = std::midpoint(0ul, IV::capacity()); IV mid(mid_size); EXPECT_EQ(mid.size(), mid_size); - if (std::is_same_v || - std::is_same_v) { + if constexpr (std::is_scalar_v || std::is_aggregate_v || + !std::is_trivially_default_constructible_v) { - IV mid_correct; - for (auto i = 0ul; i < mid_size; ++i) - mid_correct.emplace_back(); - - EXPECT_EQ(mid, mid_correct); + // all elements are value-initialized + EXPECT_EQ(std::ranges::count(mid, T{}), mid.size()); } IV full((IV::capacity())); EXPECT_EQ(full.size(), IV::capacity()); - if (std::is_same_v || - std::is_same_v) { - - IV full_correct; - for (auto i = 0ul; i < full.size(); ++i) - full_correct.emplace_back(); + if constexpr (std::is_scalar_v || std::is_aggregate_v || + !std::is_trivially_default_constructible_v) { - EXPECT_EQ(full, full_correct); + // all elements are value-initialized + EXPECT_EQ(std::ranges::count(full, T{}), full.size()); } } @@ -66,7 +60,7 @@ TYPED_TEST(Constructors, SizedValue) { EXPECT_THROW(IV(IV::capacity() + 1, value), std::bad_alloc); } - if (IV::capacity() < 1) + if constexpr (IV::capacity() < 1u) return; { @@ -79,11 +73,7 @@ TYPED_TEST(Constructors, SizedValue) { T value{8194}; IV device(IV::capacity(), value); - IV correct; - for (auto i = 0ul; i < device.size(); ++i) - correct.push_back(value); - - EXPECT_EQ(std::count(device.begin(), device.end(), value), IV::capacity()); + EXPECT_EQ(std::ranges::count(device, value), device.size()); } } @@ -94,16 +84,24 @@ TYPED_TEST(Constructors, CopyIter) { // Complexity: Linear in distance(first, last). using IV = TestFixture::IV; + using T = TestFixture::T; using InputIterator = TestFixture::InputIterator; - IV a(InputIterator{0}, InputIterator{IV::max_size() / 2}); - EXPECT_EQ(a.size(), IV::max_size() / 2); - if (!a.empty()) { - EXPECT_EQ(a.back().value, IV::max_size() / 2 - 1); + for (std::size_t n : {std::size_t(0), IV::max_size() / 2u, IV::max_size()}) { + // InputIterator + InputIterator::num_deref = 0u; + IV a(InputIterator{}, InputIterator{static_cast(n)}); + EXPECT_EQ(a.size(), n); + for (int i = 0; i < static_cast(n); ++i) { + EXPECT_EQ(a[i], T{i}); + } + // Only single-pass through input range + EXPECT_EQ(InputIterator::num_deref, n); + + // RandomAccessIterator + IV b(a.begin(), a.end()); + EXPECT_EQ(b, a); } - - IV b(a.begin(), a.end()); - EXPECT_EQ(b, a); } TYPED_TEST(Constructors, CopyRanges) { diff --git a/tests/beman/inplace_vector/gtest_setup.hpp b/tests/beman/inplace_vector/gtest_setup.hpp index b09c90d..4c8a868 100644 --- a/tests/beman/inplace_vector/gtest_setup.hpp +++ b/tests/beman/inplace_vector/gtest_setup.hpp @@ -364,6 +364,7 @@ template class IVBasicTest : public ::testing::Test { } struct InputIterator { + static std::size_t num_deref; T value; using difference_type = std::ptrdiff_t; using value_type = T; @@ -371,7 +372,10 @@ template class IVBasicTest : public ::testing::Test { constexpr InputIterator() noexcept : value{0} {} constexpr InputIterator(int i) noexcept : value{i} {} - T operator*() const { return value; }; + T operator*() const { + ++num_deref; + return value; + }; InputIterator &operator++() { ++value.value; @@ -385,3 +389,6 @@ template class IVBasicTest : public ::testing::Test { }; static_assert(std::input_iterator); }; + +template +std::size_t IVBasicTest::InputIterator::num_deref;