Skip to content

Commit 97b10de

Browse files
authored
Merge pull request #60 from bemanproject/spec-test
Improved constructor tests
2 parents a739778 + d4dcd21 commit 97b10de

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

tests/beman/inplace_vector/constructors.test.cpp

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,20 @@ TYPED_TEST(Constructors, SizedDefault) {
2626
constexpr auto mid_size = std::midpoint(0ul, IV::capacity());
2727
IV mid(mid_size);
2828
EXPECT_EQ(mid.size(), mid_size);
29-
if (std::is_same_v<T, NonTriviallyDefaultConstructible> ||
30-
std::is_same_v<T, NonTrivial>) {
29+
if constexpr (std::is_scalar_v<T> || std::is_aggregate_v<T> ||
30+
!std::is_trivially_default_constructible_v<T>) {
3131

32-
IV mid_correct;
33-
for (auto i = 0ul; i < mid_size; ++i)
34-
mid_correct.emplace_back();
35-
36-
EXPECT_EQ(mid, mid_correct);
32+
// all elements are value-initialized
33+
EXPECT_EQ(std::ranges::count(mid, T{}), mid.size());
3734
}
3835

3936
IV full((IV::capacity()));
4037
EXPECT_EQ(full.size(), IV::capacity());
41-
if (std::is_same_v<T, NonTriviallyDefaultConstructible> ||
42-
std::is_same_v<T, NonTrivial>) {
43-
44-
IV full_correct;
45-
for (auto i = 0ul; i < full.size(); ++i)
46-
full_correct.emplace_back();
38+
if constexpr (std::is_scalar_v<T> || std::is_aggregate_v<T> ||
39+
!std::is_trivially_default_constructible_v<T>) {
4740

48-
EXPECT_EQ(full, full_correct);
41+
// all elements are value-initialized
42+
EXPECT_EQ(std::ranges::count(full, T{}), full.size());
4943
}
5044
}
5145

@@ -66,7 +60,7 @@ TYPED_TEST(Constructors, SizedValue) {
6660
EXPECT_THROW(IV(IV::capacity() + 1, value), std::bad_alloc);
6761
}
6862

69-
if (IV::capacity() < 1)
63+
if constexpr (IV::capacity() < 1u)
7064
return;
7165

7266
{
@@ -79,11 +73,7 @@ TYPED_TEST(Constructors, SizedValue) {
7973
T value{8194};
8074
IV device(IV::capacity(), value);
8175

82-
IV correct;
83-
for (auto i = 0ul; i < device.size(); ++i)
84-
correct.push_back(value);
85-
86-
EXPECT_EQ(std::count(device.begin(), device.end(), value), IV::capacity());
76+
EXPECT_EQ(std::ranges::count(device, value), device.size());
8777
}
8878
}
8979

@@ -94,16 +84,24 @@ TYPED_TEST(Constructors, CopyIter) {
9484
// Complexity: Linear in distance(first, last).
9585

9686
using IV = TestFixture::IV;
87+
using T = TestFixture::T;
9788
using InputIterator = TestFixture::InputIterator;
9889

99-
IV a(InputIterator{0}, InputIterator{IV::max_size() / 2});
100-
EXPECT_EQ(a.size(), IV::max_size() / 2);
101-
if (!a.empty()) {
102-
EXPECT_EQ(a.back().value, IV::max_size() / 2 - 1);
90+
for (std::size_t n : {std::size_t(0), IV::max_size() / 2u, IV::max_size()}) {
91+
// InputIterator
92+
InputIterator::num_deref = 0u;
93+
IV a(InputIterator{}, InputIterator{static_cast<int>(n)});
94+
EXPECT_EQ(a.size(), n);
95+
for (int i = 0; i < static_cast<int>(n); ++i) {
96+
EXPECT_EQ(a[i], T{i});
97+
}
98+
// Only single-pass through input range
99+
EXPECT_EQ(InputIterator::num_deref, n);
100+
101+
// RandomAccessIterator
102+
IV b(a.begin(), a.end());
103+
EXPECT_EQ(b, a);
103104
}
104-
105-
IV b(a.begin(), a.end());
106-
EXPECT_EQ(b, a);
107105
}
108106

109107
TYPED_TEST(Constructors, CopyRanges) {

tests/beman/inplace_vector/gtest_setup.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,18 @@ template <typename Param> class IVBasicTest : public ::testing::Test {
364364
}
365365

366366
struct InputIterator {
367+
static std::size_t num_deref;
367368
T value;
368369
using difference_type = std::ptrdiff_t;
369370
using value_type = T;
370371

371372
constexpr InputIterator() noexcept : value{0} {}
372373
constexpr InputIterator(int i) noexcept : value{i} {}
373374

374-
T operator*() const { return value; };
375+
T operator*() const {
376+
++num_deref;
377+
return value;
378+
};
375379

376380
InputIterator &operator++() {
377381
++value.value;
@@ -385,3 +389,6 @@ template <typename Param> class IVBasicTest : public ::testing::Test {
385389
};
386390
static_assert(std::input_iterator<InputIterator>);
387391
};
392+
393+
template <typename Param>
394+
std::size_t IVBasicTest<Param>::InputIterator::num_deref;

0 commit comments

Comments
 (0)