Skip to content

Commit 2bec84a

Browse files
committed
Add testcase for size and resize
1 parent bb97f12 commit 2bec84a

File tree

1 file changed

+84
-14
lines changed

1 file changed

+84
-14
lines changed

tests/beman/inplace_vector/spec.test.cpp

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <ranges>
99
#include <type_traits>
1010

11+
// TODO: Not tested in this test suite:
12+
// constexpr, emplace (ensure only one copy made), exception safety on mutation
13+
1114
namespace {
1215

1316
// We run the tests on various element types with the help of GoogleTest's
@@ -267,7 +270,7 @@ struct NonTrivial {
267270
static std::size_t num_objects;
268271
int value;
269272

270-
constexpr NonTrivial() noexcept : NonTrivial(int{}) {}
273+
constexpr NonTrivial() noexcept : NonTrivial(0) {}
271274
constexpr NonTrivial(int v) noexcept : value(v) {
272275
if (not std::is_constant_evaluated()) {
273276
++num_objects;
@@ -1117,7 +1120,86 @@ TYPED_TEST(Constructors, CopyRanges) {
11171120
}
11181121

11191122
// 23.3.14.3 Size and capacity [inplace.vector.capacity]
1120-
// TODO
1123+
1124+
template <typename Param> class SizeNCapacity : public BasicTest<Param> {};
1125+
TYPED_TEST_SUITE(SizeNCapacity, AllTypes);
1126+
1127+
TYPED_TEST(SizeNCapacity, Capacity) {
1128+
// static constexpr size_type capacity() noexcept;
1129+
// static constexpr size_type max_size() noexcept;
1130+
// Returns: N.
1131+
1132+
using IV = TestFixture::IV;
1133+
constexpr auto N = TestFixture::N;
1134+
1135+
EXPECT_EQ(IV::capacity(), N);
1136+
IV device;
1137+
EXPECT_EQ(device.max_size(), N);
1138+
}
1139+
1140+
TYPED_TEST(SizeNCapacity, ResizeDown) {
1141+
// constexpr void resize(size_type sz);
1142+
// Preconditions: T is Cpp17DefaultInsertable into inplace_vector.
1143+
// Effects: If sz < size(), erases the last size() - sz elements from the
1144+
// sequence. Otherwise, appends sz - size() default-inserted elements to the
1145+
// sequence. Remarks: If an exception is thrown, there are no effects on
1146+
// *this.
1147+
1148+
using IV = TestFixture::IV;
1149+
using T = TestFixture::T;
1150+
1151+
auto device = this->unique();
1152+
1153+
auto mid_size = std::midpoint(0ul, device.size());
1154+
device.resize(mid_size);
1155+
EXPECT_EQ(device, IV(device.begin(), device.begin() + mid_size));
1156+
1157+
device.resize(0);
1158+
EXPECT_EQ(device, IV{});
1159+
}
1160+
1161+
TYPED_TEST(SizeNCapacity, ResizeUp) {
1162+
// constexpr void resize(size_type sz);
1163+
// Preconditions: T is Cpp17DefaultInsertable into inplace_vector.
1164+
// Effects: If sz < size(), erases the last size() - sz elements from the
1165+
// sequence. Otherwise, appends sz - size() default-inserted elements to the
1166+
// sequence. Remarks: If an exception is thrown, there are no effects on
1167+
// *this.
1168+
1169+
using IV = TestFixture::IV;
1170+
using T = TestFixture::T;
1171+
1172+
IV device;
1173+
1174+
EXPECT_THROW(device.resize(device.capacity() + 1), beman::bad_alloc);
1175+
EXPECT_EQ(device, IV{});
1176+
1177+
if (device.capacity() == 0)
1178+
return;
1179+
1180+
// Trying to pollute device[0]
1181+
device.push_back(T{255});
1182+
device.pop_back();
1183+
EXPECT_TRUE(device.empty());
1184+
1185+
device.resize(1);
1186+
EXPECT_EQ(device.size(), 1);
1187+
if (std::is_same_v<T, NonTriviallyDefaultConstructible> ||
1188+
std::is_same_v<T, NonTrivial>)
1189+
EXPECT_EQ(device, IV{T{0}});
1190+
1191+
T front{341};
1192+
device[0] = front;
1193+
device.resize(device.capacity());
1194+
EXPECT_EQ(device[0], front);
1195+
1196+
if (std::is_same_v<T, NonTriviallyDefaultConstructible> ||
1197+
std::is_same_v<T, NonTrivial>) {
1198+
IV expected(device.capacity(), T{0});
1199+
expected[0] = front;
1200+
EXPECT_EQ(device, expected);
1201+
}
1202+
}
11211203

11221204
// 23.3.14.4 Data [inplace.vector.data]
11231205

@@ -1857,17 +1939,11 @@ TYPED_TEST(Modifiers, ShrinkToFitEmpty) {
18571939

18581940
TYPED_TEST(Modifiers, EraseSingle) {
18591941
// constexpr iterator erase(const_iterator position);
1860-
// constexpr iterator erase(const_iterator first, const_iterator last);
1861-
// constexpr void pop_back();
18621942
//
18631943
// Effects: Invalidates iterators and references at or after the point of the
18641944
// erase.
18651945
// Throws: Nothing unless an exception is thrown by the assignment
18661946
// operator or move assignment operator of T.
1867-
// Complexity: The destructor of T is called the number of times equal to the
1868-
// number of the elements erased, but the assignment operator of T is called
1869-
// the number of times equal to the number of elements after the erased
1870-
// elements.
18711947

18721948
auto device = this->unique();
18731949

@@ -1900,17 +1976,11 @@ TYPED_TEST(Modifiers, EraseSingle) {
19001976

19011977
TYPED_TEST(Modifiers, EraseSingleConst) {
19021978
// constexpr iterator erase(const_iterator position);
1903-
// constexpr iterator erase(const_iterator first, const_iterator last);
1904-
// constexpr void pop_back();
19051979
//
19061980
// Effects: Invalidates iterators and references at or after the point of the
19071981
// erase.
19081982
// Throws: Nothing unless an exception is thrown by the assignment
19091983
// operator or move assignment operator of T.
1910-
// Complexity: The destructor of T is called the number of times equal to the
1911-
// number of the elements erased, but the assignment operator of T is called
1912-
// the number of times equal to the number of elements after the erased
1913-
// elements.
19141984

19151985
auto device = this->unique();
19161986

0 commit comments

Comments
 (0)