Skip to content

Commit 9ca550e

Browse files
committed
add pushback/ emplaceback tests
1 parent e75d44b commit 9ca550e

File tree

1 file changed

+178
-11
lines changed

1 file changed

+178
-11
lines changed

tests/beman/inplace_vector/spec.test2.experimental.cpp

Lines changed: 178 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ TYPED_TEST(Modifiers, Insert) {
11481148
TYPED_TEST(Modifiers, PushBack) {
11491149
// constexpr reference push_back(const T& x);
11501150
// constexpr reference push_back(T&& x);
1151+
//
1152+
// Returns: back().
1153+
// Throws: bad_alloc or any exception thrown by the initialization of the
1154+
// inserted element.
1155+
// Complexity: Constant.
1156+
// Remarks: If an exception is thrown, there are no effects on *this.
1157+
1158+
using IV = TestFixture::IV;
1159+
1160+
const auto reference = this->unique();
1161+
1162+
IV device;
1163+
for (int i = 0; i < reference.size(); ++i) {
1164+
EXPECT_EQ(device.push_back(reference[i]), device.back());
1165+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1166+
}
1167+
}
1168+
1169+
// TODO: Check if there's extra copies
1170+
1171+
TYPED_TEST(Modifiers, EmplaceBack) {
11511172
// template<class... Args>
11521173
// constexpr reference emplace_back(Args&&... args);
11531174
//
@@ -1157,11 +1178,18 @@ TYPED_TEST(Modifiers, PushBack) {
11571178
// Complexity: Constant.
11581179
// Remarks: If an exception is thrown, there are no effects on *this.
11591180

1160-
// TODO
1161-
GTEST_SKIP();
1181+
using IV = TestFixture::IV;
1182+
1183+
const auto reference = this->unique();
1184+
1185+
IV device;
1186+
for (int i = 0; i < reference.size(); ++i) {
1187+
EXPECT_EQ(device.emplace_back(reference[i].value), device.back());
1188+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1189+
}
11621190
}
11631191

1164-
TYPED_TEST(Modifiers, TryPushBack) {
1192+
TYPED_TEST(Modifiers, TryEmplaceBack) {
11651193
// template<class... Args>
11661194
// constexpr pointer try_emplace_back(Args&&... args);
11671195
// constexpr pointer try_push_back(const T& x);
@@ -1183,8 +1211,113 @@ TYPED_TEST(Modifiers, TryPushBack) {
11831211
// Complexity: Constant.
11841212
// Remarks: If an exception is thrown, there are no effects on *this.
11851213

1186-
// TODO
1187-
GTEST_SKIP();
1214+
using IV = TestFixture::IV;
1215+
1216+
const auto reference = this->unique();
1217+
IV device;
1218+
if (!reference.empty()) {
1219+
for (int i = 0; i < reference.size(); ++i) {
1220+
EXPECT_EQ(device.try_emplace_back(reference[i].value),
1221+
std::addressof(device.back()));
1222+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1223+
}
1224+
1225+
EXPECT_EQ(device.try_emplace_back(reference[0].value), nullptr);
1226+
EXPECT_EQ(device, reference);
1227+
} else {
1228+
EXPECT_EQ(device.try_emplace_back(0), nullptr);
1229+
EXPECT_EQ(device, IV());
1230+
}
1231+
}
1232+
1233+
TYPED_TEST(Modifiers, TryPushBackConstRef) {
1234+
// template<class... Args>
1235+
// constexpr pointer try_emplace_back(Args&&... args);
1236+
// constexpr pointer try_push_back(const T& x);
1237+
// constexpr pointer try_push_back(T&& x);
1238+
//
1239+
// Let vals denote a pack:
1240+
// (8.1) std::forward<Args>(args)... for the first overload,
1241+
// (8.2) x for the second overload,
1242+
// (8.3) std::move(x) for the third overload.
1243+
//
1244+
// Preconditions: value_type is Cpp17EmplaceConstructible into inplace_vector
1245+
// from vals....
1246+
// Effects: If size() < capacity() is true, appends an object of type T
1247+
// direct-non-list-initialized with vals.... Otherwise, there are no effects.
1248+
// Returns: nullptr if size() == capacity() is true, otherwise
1249+
// addressof(back()).
1250+
// Throws: Nothing unless an exception is thrown by the initialization of the
1251+
// inserted element.
1252+
// Complexity: Constant.
1253+
// Remarks: If an exception is thrown, there are no effects on *this.
1254+
1255+
using IV = TestFixture::IV;
1256+
using T = TestFixture::T;
1257+
1258+
const auto reference = this->unique();
1259+
IV device;
1260+
1261+
if (!reference.empty()) {
1262+
for (int i = 0; i < reference.size(); ++i) {
1263+
EXPECT_EQ(device.try_push_back(reference[i]),
1264+
std::addressof(device.back()));
1265+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1266+
}
1267+
1268+
EXPECT_EQ(device.try_push_back(reference[0]), nullptr);
1269+
EXPECT_EQ(device, reference);
1270+
} else {
1271+
T val{0};
1272+
EXPECT_EQ(device.try_push_back(val), nullptr);
1273+
EXPECT_EQ(device, IV());
1274+
}
1275+
}
1276+
1277+
TYPED_TEST(Modifiers, TryPushBackRV) {
1278+
// template<class... Args>
1279+
// constexpr pointer try_emplace_back(Args&&... args);
1280+
// constexpr pointer try_push_back(const T& x);
1281+
// constexpr pointer try_push_back(T&& x);
1282+
//
1283+
// Let vals denote a pack:
1284+
// (8.1) std::forward<Args>(args)... for the first overload,
1285+
// (8.2) x for the second overload,
1286+
// (8.3) std::move(x) for the third overload.
1287+
//
1288+
// Preconditions: value_type is Cpp17EmplaceConstructible into inplace_vector
1289+
// from vals....
1290+
// Effects: If size() < capacity() is true, appends an object of type T
1291+
// direct-non-list-initialized with vals.... Otherwise, there are no effects.
1292+
// Returns: nullptr if size() == capacity() is true, otherwise
1293+
// addressof(back()).
1294+
// Throws: Nothing unless an exception is thrown by the initialization of the
1295+
// inserted element.
1296+
// Complexity: Constant.
1297+
// Remarks: If an exception is thrown, there are no effects on *this.
1298+
1299+
using IV = TestFixture::IV;
1300+
using T = TestFixture::T;
1301+
1302+
const auto reference = this->unique();
1303+
1304+
IV device;
1305+
1306+
if (!reference.empty()) {
1307+
for (int i = 0; i < reference.size(); ++i) {
1308+
T val{reference[i].value};
1309+
EXPECT_EQ(device.try_push_back(std::move(val)),
1310+
std::addressof(device.back()));
1311+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1312+
}
1313+
1314+
EXPECT_EQ(device.try_push_back(reference[0]), nullptr);
1315+
EXPECT_EQ(device, reference);
1316+
} else {
1317+
T val{0};
1318+
EXPECT_EQ(device.try_push_back(std::move(val)), nullptr);
1319+
EXPECT_EQ(device, IV());
1320+
}
11881321
}
11891322

11901323
TYPED_TEST(Modifiers, TryAppendRanges) {
@@ -1219,19 +1352,53 @@ TYPED_TEST(Modifiers, UncheckedEmplacedBack) {
12191352
// Effects: Equivalent to: return
12201353
// *try_emplace_back(std::forward<Args>(args)...);
12211354

1222-
// TODO
1223-
GTEST_SKIP();
1355+
using IV = TestFixture::IV;
1356+
1357+
const auto reference = this->unique();
1358+
1359+
IV device;
1360+
for (int i = 0; i < reference.size(); ++i) {
1361+
EXPECT_EQ(device.unchecked_emplace_back(reference[i].value), device.back());
1362+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1363+
}
12241364
}
12251365

1226-
TYPED_TEST(Modifiers, UncheckedPushBack) {
1366+
TYPED_TEST(Modifiers, UncheckedPushBackConstRef) {
12271367
// constexpr reference unchecked_push_back(const T& x);
12281368
// constexpr reference unchecked_push_back(T&& x);
12291369
// Preconditions: size() < capacity() is true.
12301370
// Effects: Equivalent to: return
12311371
// *try_push_back(std​::​forward<decltype(x)>(x));
12321372

1233-
// TODO
1234-
GTEST_SKIP();
1373+
using IV = TestFixture::IV;
1374+
1375+
const auto reference = this->unique();
1376+
1377+
IV device;
1378+
for (int i = 0; i < reference.size(); ++i) {
1379+
EXPECT_EQ(device.unchecked_push_back(reference[i]), device.back());
1380+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1381+
}
1382+
}
1383+
1384+
TYPED_TEST(Modifiers, UncheckedPushBackRV) {
1385+
// constexpr reference unchecked_push_back(const T& x);
1386+
// constexpr reference unchecked_push_back(T&& x);
1387+
// Preconditions: size() < capacity() is true.
1388+
// Effects: Equivalent to: return
1389+
// *try_push_back(std​::​forward<decltype(x)>(x));
1390+
1391+
using IV = TestFixture::IV;
1392+
using T = TestFixture::T;
1393+
1394+
const auto reference = this->unique();
1395+
1396+
IV device;
1397+
for (int i = 0; i < reference.size(); ++i) {
1398+
T val{reference[i].value};
1399+
EXPECT_EQ(device.unchecked_push_back(std::move(val)), device.back());
1400+
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
1401+
}
12351402
}
12361403

12371404
TYPED_TEST(Modifiers, ReserveNonEmpty) {
@@ -1242,7 +1409,7 @@ TYPED_TEST(Modifiers, ReserveNonEmpty) {
12421409

12431410
using IV = TestFixture::IV;
12441411

1245-
auto reference = this->unique();
1412+
const auto reference = this->unique();
12461413

12471414
IV device(reference);
12481415

0 commit comments

Comments
 (0)