Skip to content

Commit f698e05

Browse files
committed
Address review comments.
1 parent 56766bc commit f698e05

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ TEST_CONSTEXPR_CXX20 bool tests()
4040
}
4141

4242
#if TEST_STD_VER >= 23
43-
std::size_t min_bytes = 1000;
44-
4543
template <typename T>
4644
struct increasing_allocator {
47-
using value_type = T;
48-
increasing_allocator() = default;
45+
using value_type = T;
46+
std::size_t min_elements = 1000;
47+
increasing_allocator() = default;
48+
4949
template <typename U>
50-
increasing_allocator(const increasing_allocator<U>&) noexcept {}
51-
std::allocation_result<T*> allocate_at_least(std::size_t n) {
52-
std::size_t allocation_amount = n * sizeof(T);
53-
if (allocation_amount < min_bytes)
54-
allocation_amount = min_bytes;
55-
min_bytes += 1000;
56-
return {static_cast<T*>(::operator new(allocation_amount)), allocation_amount / sizeof(T)};
50+
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
51+
52+
constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
53+
if (n < min_elements)
54+
n = min_elements;
55+
min_elements += 1000;
56+
return std::allocator<T>{}.allocate_at_least(n);
5757
}
58-
T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
59-
void deallocate(T* p, std::size_t) noexcept { ::operator delete(static_cast<void*>(p)); }
58+
constexpr T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
59+
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
6060
};
6161

6262
template <typename T, typename U>
@@ -65,24 +65,28 @@ bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
6565
}
6666

6767
// https://github.com/llvm/llvm-project/issues/95161
68-
void test_increasing_allocator() {
68+
constexpr bool test_increasing_allocator() {
6969
std::vector<bool, increasing_allocator<bool>> v;
7070
v.push_back(1);
7171
std::size_t capacity = v.capacity();
7272
v.shrink_to_fit();
7373
assert(v.capacity() <= capacity);
7474
assert(v.size() == 1);
75+
76+
return true;
7577
}
7678
#endif // TEST_STD_VER >= 23
7779

7880
int main(int, char**)
7981
{
80-
tests();
81-
#if TEST_STD_VER >= 23
82-
test_increasing_allocator();
83-
#endif // TEST_STD_VER >= 23
82+
tests();
8483
#if TEST_STD_VER > 17
8584
static_assert(tests());
8685
#endif
86+
#if TEST_STD_VER >= 23
87+
test_increasing_allocator();
88+
static_assert(test_increasing_allocator());
89+
#endif // TEST_STD_VER >= 23
90+
8791
return 0;
8892
}

0 commit comments

Comments
 (0)