Skip to content

Commit 4b951fc

Browse files
committed
Add no exceptions support for tests
1 parent 25eb57b commit 4b951fc

File tree

8 files changed

+52
-28
lines changed

8 files changed

+52
-28
lines changed

.github/workflows/ci_tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ jobs:
5555
args: "-DCMAKE_CXX_FLAGS=-fsanitize=thread"
5656
- description: "ASan"
5757
args: "-DCMAKE_CXX_FLAGS='-fsanitize=address -fsanitize=undefined'"
58+
- description: "NoExcep"
59+
args: "-DBEMAN_INPLACE_VECTOR_NO_EXCEPTIONS=on"
5860
include:
5961
- platform: ubuntu-24.04
6062
compiler:

tests/beman/inplace_vector/constructors.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TYPED_TEST(Constructors, SizedDefault) {
2121

2222
EXPECT_EQ(IV(0), IV{});
2323

24-
EXPECT_THROW(IV(IV::capacity() + 1), std::bad_alloc);
24+
SAFE_EXPECT_THROW(IV(IV::capacity() + 1), std::bad_alloc);
2525

2626
constexpr auto mid_size = std::midpoint(0ul, IV::capacity());
2727
IV mid(mid_size);
@@ -57,7 +57,7 @@ TYPED_TEST(Constructors, SizedValue) {
5757
IV device(0, value);
5858
EXPECT_EQ(device, IV{});
5959

60-
EXPECT_THROW(IV(IV::capacity() + 1, value), std::bad_alloc);
60+
SAFE_EXPECT_THROW(IV(IV::capacity() + 1, value), std::bad_alloc);
6161
}
6262

6363
if constexpr (IV::capacity() < 1u)

tests/beman/inplace_vector/container_requirements.test.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ TYPED_TEST(SequenceContainerRequirements, ConstructorInitializerList) {
626626
using T = TestFixture::T;
627627

628628
if (IV::capacity() == 0) {
629-
EXPECT_THROW(IV({T{20}}), std::bad_alloc);
629+
SAFE_EXPECT_THROW(IV({T{20}}), std::bad_alloc);
630630
return;
631631
}
632632

@@ -657,7 +657,7 @@ TYPED_TEST(SequenceContainerRequirements, AssignInitializerList) {
657657

658658
if (IV::capacity() == 0) {
659659
IV device;
660-
EXPECT_THROW(device = {T{52}}, std::bad_alloc);
660+
SAFE_EXPECT_THROW(device = {T{52}}, std::bad_alloc);
661661
return;
662662
}
663663

@@ -787,7 +787,7 @@ TYPED_TEST(SequenceContainerRequirements, AssignIterRange) {
787787
EXPECT_EQ(device, correct);
788788

789789
std::array<T, IV::capacity() + 1> ref{};
790-
EXPECT_THROW(device.assign(ref.begin(), ref.end()), std::bad_alloc);
790+
SAFE_EXPECT_THROW(device.assign(ref.begin(), ref.end()), std::bad_alloc);
791791
}
792792

793793
{
@@ -802,7 +802,7 @@ TYPED_TEST(SequenceContainerRequirements, AssignIterRange) {
802802
// [containers.sequences.inplace.vector.overview]
803803
// 5. Any member function of inplace_vector<T, N> that would cause the size
804804
// to exceed N throws an exception of type bad_alloc.
805-
EXPECT_THROW(
805+
SAFE_EXPECT_THROW(
806806
device.assign(InputIterator{0}, InputIterator{IV::max_size() + 1}),
807807
std::bad_alloc);
808808
}
@@ -833,7 +833,7 @@ TYPED_TEST(SequenceContainerRequirements, AssignRange) {
833833
std::array<T, IV::capacity() + 1> ref;
834834
std::copy(correct.begin(), correct.end(), ref.begin());
835835
ref.back() = T{5};
836-
EXPECT_THROW(device.assign_range(ref), std::bad_alloc);
836+
SAFE_EXPECT_THROW(device.assign_range(ref), std::bad_alloc);
837837
}
838838

839839
// a.assign(il)
@@ -846,7 +846,7 @@ TYPED_TEST(SequenceContainerRequirements, AssignFuncInitializerList) {
846846
auto device = this->unique();
847847

848848
if (device.capacity() == 0) {
849-
EXPECT_THROW(device.assign({T{50}}), std::bad_alloc);
849+
SAFE_EXPECT_THROW(device.assign({T{50}}), std::bad_alloc);
850850
return;
851851
}
852852

@@ -916,7 +916,8 @@ TYPED_TEST(SequenceContainerRequirements, AssignMulti) {
916916
}
917917

918918
device.clear();
919-
EXPECT_THROW(device.assign(device.capacity() + 1, T{12}), std::bad_alloc);
919+
SAFE_EXPECT_THROW(device.assign(device.capacity() + 1, T{12}),
920+
std::bad_alloc);
920921
}
921922

922923
// a.front()
@@ -1017,7 +1018,7 @@ TYPED_TEST(SequenceContainerRequirements, ElementAccessAt) {
10171018
EXPECT_EQ(device.at(i), *(device.begin() + i));
10181019
}
10191020

1020-
EXPECT_THROW(device.at(IV::capacity()), std::out_of_range);
1021+
SAFE_EXPECT_THROW(device.at(IV::capacity()), std::out_of_range);
10211022
}
10221023

10231024
}; // namespace

tests/beman/inplace_vector/gtest_setup.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,9 @@ template <typename Param> class IVBasicTest : public ::testing::Test {
392392

393393
template <typename Param>
394394
std::size_t IVBasicTest<Param>::InputIterator::num_deref;
395+
396+
#if BEMAN_INPLACE_VECTOR_NO_EXCEPTIONS()
397+
#define SAFE_EXPECT_THROW(x, y) EXPECT_DEATH(x, ".*")
398+
#else
399+
#define SAFE_EXPECT_THROW(x, y) EXPECT_THROW(x, y)
400+
#endif

tests/beman/inplace_vector/inplace_vector.test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ template <typename T> constexpr void test() {
6565
assert(const_data == std::addressof(const_front));
6666
}
6767

68+
#if BEMAN_INPLACE_VECTOR_NO_EXCEPTIONS()
69+
int main() {
70+
test<int>();
71+
return 0;
72+
}
73+
#else
6874
void test_exceptions() {
6975
using vec = inplace_vector<int, 42>;
7076
{
@@ -86,8 +92,10 @@ void test_exceptions() {
8692
}
8793
}
8894
}
95+
8996
int main() {
9097
test<int>();
9198
test_exceptions();
9299
return 0;
93100
}
101+
#endif

tests/beman/inplace_vector/modifiers.test.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ TYPED_TEST(Modifiers, InsertSingleConstRef) {
4242
}
4343

4444
T val{272};
45-
EXPECT_THROW(device.insert(device.begin(), val), std::bad_alloc);
45+
SAFE_EXPECT_THROW(device.insert(device.begin(), val), std::bad_alloc);
4646
EXPECT_EQ(device, reference);
4747

48-
EXPECT_THROW(device.insert(device.begin(), val), std::bad_alloc);
48+
SAFE_EXPECT_THROW(device.insert(device.begin(), val), std::bad_alloc);
4949
EXPECT_EQ(device, reference);
5050
}
5151

@@ -82,10 +82,10 @@ TYPED_TEST(Modifiers, InsertSingleRV) {
8282
}
8383
}
8484

85-
EXPECT_THROW(device.insert(device.begin(), T{272}), std::bad_alloc);
85+
SAFE_EXPECT_THROW(device.insert(device.begin(), T{272}), std::bad_alloc);
8686
EXPECT_EQ(device, reference);
8787

88-
EXPECT_THROW(device.insert(device.begin(), T{272}), std::bad_alloc);
88+
SAFE_EXPECT_THROW(device.insert(device.begin(), T{272}), std::bad_alloc);
8989
EXPECT_EQ(device, reference);
9090
}
9191

@@ -121,10 +121,10 @@ TYPED_TEST(Modifiers, InsertEmplace) {
121121
}
122122
}
123123

124-
EXPECT_THROW(device.emplace(device.begin(), 272), std::bad_alloc);
124+
SAFE_EXPECT_THROW(device.emplace(device.begin(), 272), std::bad_alloc);
125125
EXPECT_EQ(device, reference);
126126

127-
EXPECT_THROW(device.emplace(device.begin(), 272), std::bad_alloc);
127+
SAFE_EXPECT_THROW(device.emplace(device.begin(), 272), std::bad_alloc);
128128
EXPECT_EQ(device, reference);
129129
}
130130

@@ -161,7 +161,7 @@ TYPED_TEST(Modifiers, InsertMulti) {
161161
}
162162

163163
EXPECT_NO_THROW(device.insert(device.begin(), 0, {2538}));
164-
EXPECT_THROW(device.insert(device.begin(), 1, {2538}), std::bad_alloc);
164+
SAFE_EXPECT_THROW(device.insert(device.begin(), 1, {2538}), std::bad_alloc);
165165
}
166166

167167
TYPED_TEST(Modifiers, InsertInitList) {
@@ -201,7 +201,7 @@ TYPED_TEST(Modifiers, InsertInitList) {
201201

202202
auto full = this->unique();
203203
EXPECT_NO_THROW(full.insert(full.begin(), {}));
204-
EXPECT_THROW(full.insert(full.begin(), {T{25}}), std::bad_alloc);
204+
SAFE_EXPECT_THROW(full.insert(full.begin(), {T{25}}), std::bad_alloc);
205205
}
206206

207207
TYPED_TEST(Modifiers, InsertRange) {
@@ -257,8 +257,9 @@ TYPED_TEST(Modifiers, InsertRange) {
257257
EXPECT_NO_THROW(device.insert_range(device.begin(), std::array<T, 0>{}));
258258
EXPECT_EQ(device, reference);
259259

260-
EXPECT_THROW(device.insert_range(device.begin(), std::array<T, 1>{T{25}}),
261-
std::bad_alloc);
260+
SAFE_EXPECT_THROW(
261+
device.insert_range(device.begin(), std::array<T, 1>{T{25}}),
262+
std::bad_alloc);
262263
}
263264

264265
TYPED_TEST(Modifiers, InsertItrRange) {
@@ -315,7 +316,7 @@ TYPED_TEST(Modifiers, InsertItrRange) {
315316
EXPECT_EQ(device, reference);
316317

317318
std::array<T, 1> single_array{T{25}};
318-
EXPECT_THROW(
319+
SAFE_EXPECT_THROW(
319320
device.insert(device.begin(), single_array.begin(), single_array.end()),
320321
std::bad_alloc);
321322
}
@@ -375,7 +376,7 @@ TYPED_TEST(Modifiers, PushBackConstRef) {
375376
}
376377

377378
T val{0};
378-
EXPECT_THROW(device.push_back(val), std::bad_alloc);
379+
SAFE_EXPECT_THROW(device.push_back(val), std::bad_alloc);
379380
}
380381

381382
TYPED_TEST(Modifiers, PushBackRV) {
@@ -401,7 +402,7 @@ TYPED_TEST(Modifiers, PushBackRV) {
401402
}
402403

403404
T val{0};
404-
EXPECT_THROW(device.push_back(val), std::bad_alloc);
405+
SAFE_EXPECT_THROW(device.push_back(val), std::bad_alloc);
405406
}
406407

407408
// TODO: Check if there's extra copies
@@ -427,7 +428,7 @@ TYPED_TEST(Modifiers, EmplaceBack) {
427428
EXPECT_EQ(device, IV(reference.begin(), reference.begin() + i + 1));
428429
}
429430

430-
EXPECT_THROW(device.emplace_back(0), std::bad_alloc);
431+
SAFE_EXPECT_THROW(device.emplace_back(0), std::bad_alloc);
431432
}
432433

433434
TYPED_TEST(Modifiers, TryEmplaceBack) {
@@ -701,7 +702,7 @@ TYPED_TEST(Modifiers, ReserveNonEmpty) {
701702
device.reserve(device.capacity());
702703
EXPECT_EQ(device, reference);
703704

704-
EXPECT_THROW(device.reserve(device.capacity() + 1), std::bad_alloc);
705+
SAFE_EXPECT_THROW(device.reserve(device.capacity() + 1), std::bad_alloc);
705706
}
706707

707708
TYPED_TEST(Modifiers, ReserveEmpty) {
@@ -723,7 +724,7 @@ TYPED_TEST(Modifiers, ReserveEmpty) {
723724
device.reserve(device.capacity());
724725
EXPECT_EQ(device, IV());
725726

726-
EXPECT_THROW(device.reserve(device.capacity() + 1), std::bad_alloc);
727+
SAFE_EXPECT_THROW(device.reserve(device.capacity() + 1), std::bad_alloc);
727728
}
728729

729730
TYPED_TEST(Modifiers, ShrinkToFitNonEmpty) {

tests/beman/inplace_vector/ref_impl.test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static constexpr void __assert_failure(char const *__file, int __line,
4242
: ::__assert_failure(static_cast<const char *>(__FILE__), __LINE__, \
4343
"assertion failed: " #__VA_ARGS__))
4444

45+
#if BEMAN_INPLACE_VECTOR_NO_EXCEPTIONS()
46+
#define CHECK_THROWS(EXPR, EXCEPT) \
47+
do { \
48+
} while (0)
49+
#else
4550
#define CHECK_THROWS(EXPR, EXCEPT) \
4651
if (auto e = \
4752
[&] { \
@@ -58,6 +63,7 @@ static constexpr void __assert_failure(char const *__file, int __line,
5863
__assert_failure(static_cast<const char *>(__FILE__), __LINE__, \
5964
"expression failed to throw " #EXCEPT ": " #EXPR); \
6065
}
66+
#endif
6167

6268
template struct beman::details::inplace_vector::storage::zero_sized<int>;
6369
template struct beman::details::inplace_vector::storage::trivial<int, 10>;

tests/beman/inplace_vector/size_n_data.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ TYPED_TEST(SizeNCapacity, ResizeUp) {
5656

5757
IV device;
5858

59-
EXPECT_THROW(device.resize(device.capacity() + 1), std::bad_alloc);
59+
SAFE_EXPECT_THROW(device.resize(device.capacity() + 1), std::bad_alloc);
6060
EXPECT_EQ(device, IV{});
6161

6262
if (device.capacity() == 0)
@@ -86,7 +86,7 @@ TYPED_TEST(SizeNCapacity, ResizeUp) {
8686
}
8787

8888
IV before_resize(device);
89-
EXPECT_THROW(device.resize(device.capacity() + 1), std::bad_alloc);
89+
SAFE_EXPECT_THROW(device.resize(device.capacity() + 1), std::bad_alloc);
9090
EXPECT_EQ(device, before_resize);
9191
}
9292

0 commit comments

Comments
 (0)