Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved constructor tests #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions tests/beman/inplace_vector/constructors.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, NonTriviallyDefaultConstructible> ||
std::is_same_v<T, NonTrivial>) {
if constexpr (std::is_scalar_v<T> || std::is_aggregate_v<T> ||
!std::is_trivially_default_constructible_v<T>) {

This comment was marked as resolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ops, didnt see the negation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is actually better than std::is_same_v<T, NonTriviallyDefaultConstructible> || std::is_same_v<T, NonTrivial>, we can do the comparison here not because the underlying is a scalar or an aggregate, it is because we know it has a well defined value-initialization.


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<T, NonTriviallyDefaultConstructible> ||
std::is_same_v<T, NonTrivial>) {

IV full_correct;
for (auto i = 0ul; i < full.size(); ++i)
full_correct.emplace_back();
if constexpr (std::is_scalar_v<T> || std::is_aggregate_v<T> ||
!std::is_trivially_default_constructible_v<T>) {

EXPECT_EQ(full, full_correct);
// all elements are value-initialized
EXPECT_EQ(std::ranges::count(full, T{}), full.size());
}
}

Expand All @@ -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;

{
Expand All @@ -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());
}
}

Expand All @@ -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<int>(n)});
EXPECT_EQ(a.size(), n);
for (int i = 0; i < static_cast<int>(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);
Comment on lines +87 to +103
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really helpful, thanks!

}

IV b(a.begin(), a.end());
EXPECT_EQ(b, a);
}

TYPED_TEST(Constructors, CopyRanges) {
Expand Down
9 changes: 8 additions & 1 deletion tests/beman/inplace_vector/gtest_setup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,18 @@ template <typename Param> 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;

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;
Expand All @@ -385,3 +389,6 @@ template <typename Param> class IVBasicTest : public ::testing::Test {
};
static_assert(std::input_iterator<InputIterator>);
};

template <typename Param>
std::size_t IVBasicTest<Param>::InputIterator::num_deref;