Skip to content

Commit bfcd237

Browse files
committed
Refactor in a better way
1 parent 358d5f3 commit bfcd237

File tree

3 files changed

+73
-208
lines changed

3 files changed

+73
-208
lines changed

libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp

Lines changed: 62 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,133 +12,121 @@
1212
// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
1313

1414
#include <cstddef>
15-
#include <type_traits>
1615
#include <memory>
16+
#include <type_traits>
1717
#include <vector>
1818

19+
#include "../vector/common.h"
1920
#include "count_new.h"
2021
#include "test_iterators.h"
2122

22-
template <class T>
23-
struct Allocator {
24-
using value_type = T;
25-
using is_always_equal = std::false_type;
26-
27-
template <class U>
28-
Allocator(const Allocator<U>&) {}
29-
30-
Allocator(bool should_throw = true) {
31-
if (should_throw)
32-
throw 0;
33-
}
34-
35-
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
36-
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
37-
38-
template <class U>
39-
friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
40-
};
41-
42-
template <class IterCat>
43-
struct Iterator {
44-
using iterator_category = IterCat;
45-
using difference_type = std::ptrdiff_t;
46-
using value_type = bool;
47-
using reference = bool&;
48-
using pointer = bool*;
49-
50-
int i_;
51-
bool b_ = true;
52-
Iterator(int i = 0) : i_(i) {}
53-
bool& operator*() {
54-
if (i_ == 1)
55-
throw 1;
56-
return b_;
57-
}
58-
59-
friend bool operator==(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ == rhs.i_; }
60-
61-
friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ != rhs.i_; }
62-
63-
Iterator& operator++() {
64-
++i_;
65-
return *this;
66-
}
23+
int main(int, char**) {
24+
using AllocVec = std::vector<bool, throwing_allocator<bool> >;
6725

68-
Iterator operator++(int) {
69-
auto tmp = *this;
70-
++i_;
71-
return tmp;
26+
try { // Throw in vector() from allocator
27+
AllocVec vec;
28+
} catch (int) {
7229
}
73-
};
74-
75-
void check_new_delete_called() {
76-
assert(globalMemCounter.new_called == globalMemCounter.delete_called);
77-
assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called);
78-
assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called);
79-
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
80-
}
81-
82-
int main(int, char**) {
83-
using AllocVec = std::vector<bool, Allocator<bool> >;
30+
check_new_delete_called();
8431

8532
#if TEST_STD_VER >= 14
86-
try { // Throw in vector(size_type, const allocator_type&) from allocator
87-
Allocator<bool> alloc(false);
33+
try { // Throw in vector(size_type, const allocator_type&) from allocator
34+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
8835
AllocVec get_alloc(0, alloc);
8936
} catch (int) {
9037
}
9138
check_new_delete_called();
92-
#endif // TEST_STD_VER >= 14
39+
#endif // TEST_STD_VER >= 14
40+
41+
try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
42+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
43+
AllocVec get_alloc(0, true, alloc);
44+
} catch (int) {
45+
}
46+
check_new_delete_called();
9347

9448
try { // Throw in vector(InputIterator, InputIterator) from input iterator
95-
std::vector<bool> vec((Iterator<std::input_iterator_tag>()), Iterator<std::input_iterator_tag>(2));
49+
std::vector<bool> vec(
50+
throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2));
9651
} catch (int) {
9752
}
9853
check_new_delete_called();
9954

10055
try { // Throw in vector(InputIterator, InputIterator) from forward iterator
101-
std::vector<bool> vec((Iterator<std::forward_iterator_tag>()), Iterator<std::forward_iterator_tag>(2));
56+
std::vector<bool> vec(
57+
throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2));
10258
} catch (int) {
10359
}
10460
check_new_delete_called();
10561

10662
try { // Throw in vector(InputIterator, InputIterator) from allocator
107-
int a[] = {1, 2};
108-
AllocVec vec(cpp17_input_iterator<int*>(a), cpp17_input_iterator<int*>(a + 2));
63+
bool a[] = {true, true};
64+
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2));
10965
} catch (int) {
11066
}
11167
check_new_delete_called();
11268

11369
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
11470
std::allocator<bool> alloc;
115-
std::vector<bool> vec(Iterator<std::input_iterator_tag>(), Iterator<std::input_iterator_tag>(2), alloc);
71+
std::vector<bool> vec(
72+
throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
11673
} catch (int) {
11774
}
11875
check_new_delete_called();
11976

12077
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
12178
std::allocator<bool> alloc;
122-
std::vector<bool> vec(Iterator<std::forward_iterator_tag>(), Iterator<std::forward_iterator_tag>(2), alloc);
79+
std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
80+
throwing_iterator<bool, std::forward_iterator_tag>(2),
81+
alloc);
12382
} catch (int) {
12483
}
12584
check_new_delete_called();
12685

12786
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
128-
bool a[] = {true, false};
129-
Allocator<bool> alloc(false);
87+
bool a[] = {true, false};
88+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
13089
AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
13190
} catch (int) {
13291
}
13392
check_new_delete_called();
13493

13594
try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
136-
bool a[] = {true, false};
137-
Allocator<bool> alloc(false);
95+
bool a[] = {true, false};
96+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
13897
AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
13998
} catch (int) {
14099
}
141100
check_new_delete_called();
142101

102+
#if TEST_STD_VER >= 11
103+
try { // Throw in vector(const vector&, const allocator_type&) from allocator
104+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
105+
AllocVec vec(alloc);
106+
vec.push_back(true);
107+
alloc.throw_on_copy_ = true;
108+
AllocVec vec2(vec, alloc);
109+
} catch (int) {
110+
}
111+
check_new_delete_called();
112+
113+
try { // Throw in vector(vector&&, const allocator_type&) from allocator
114+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
115+
AllocVec vec(alloc);
116+
vec.push_back(true);
117+
alloc.throw_on_copy_ = true;
118+
AllocVec vec2(std::move(vec), alloc);
119+
} catch (int) {
120+
}
121+
check_new_delete_called();
122+
123+
try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
124+
throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
125+
AllocVec vec({true, true}, alloc);
126+
} catch (int) {
127+
}
128+
check_new_delete_called();
129+
#endif // TEST_STD_VER >= 11
130+
143131
return 0;
144132
}

libcxx/test/std/containers/sequences/vector.bool/exceptions.pass.cpp

Lines changed: 0 additions & 130 deletions
This file was deleted.

libcxx/test/support/exception_test_helpers.h renamed to libcxx/test/std/containers/sequences/vector/common.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef EXCEPTION_TEST_HELPERS_H
10-
#define EXCEPTION_TEST_HELPERS_H
9+
#ifndef COMMON_H
10+
#define COMMON_H
11+
12+
#include <cassert>
13+
#include <cstddef>
14+
#include <memory>
15+
#include <type_traits>
1116

1217
#include "count_new.h"
1318

@@ -18,7 +23,9 @@ struct throwing_allocator {
1823

1924
bool throw_on_copy_ = false;
2025

21-
throwing_allocator(bool throw_on_ctor = true, bool throw_on_copy = false) : throw_on_copy_(throw_on_copy) {
26+
throwing_allocator() : throwing_allocator(/*throw_on_ctor = */ true, /*throw_on_copy = */ false) {}
27+
28+
explicit throwing_allocator(bool throw_on_ctor, bool throw_on_copy) : throw_on_copy_(throw_on_copy) {
2229
if (throw_on_ctor)
2330
throw 0;
2431
}
@@ -84,4 +91,4 @@ inline void check_new_delete_called() {
8491
assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called);
8592
}
8693

87-
#endif // EXCEPTION_TEST_HELPERS_H
94+
#endif // COMMON_H

0 commit comments

Comments
 (0)