Skip to content

Commit facbd58

Browse files
committed
[libc++][vector<bool>] Tests shrink_to_fit requirement.
vector<bool>'s shrink_to_fit implementation is using the "swap-to-free-container-resources-trick" which only shrinks when the input vector is empty. Since the request to shrink_to_fit is non-binding, this is a valid implementation. It is not a high-quality implementation. Since vector<bool> is not a very popular container the implementation has not been changed and only a test to validate the non-growing property has been added. This was discovered while investigating #95161
1 parent 857700f commit facbd58

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,48 @@ TEST_CONSTEXPR_CXX20 bool tests()
3939
return true;
4040
}
4141

42+
#if TEST_STD_VER >= 23
43+
std::size_t min_bytes = 1000;
44+
45+
template <typename T>
46+
struct increasing_allocator {
47+
using value_type = T;
48+
increasing_allocator() = default;
49+
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)};
57+
}
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)); }
60+
};
61+
62+
template <typename T, typename U>
63+
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
64+
return true;
65+
}
66+
67+
// https://github.com/llvm/llvm-project/issues/95161
68+
void test_increasing_allocator() {
69+
std::vector<bool, increasing_allocator<bool>> v;
70+
v.push_back(1);
71+
std::size_t capacity = v.capacity();
72+
v.shrink_to_fit();
73+
assert(v.capacity() <= capacity);
74+
assert(v.size() == 1);
75+
}
76+
#endif // TEST_STD_VER >= 23
77+
4278
int main(int, char**)
4379
{
4480
tests();
81+
#if TEST_STD_VER >= 23
82+
test_increasing_allocator();
83+
#endif // TEST_STD_VER >= 23
4584
#if TEST_STD_VER > 17
4685
static_assert(tests());
4786
#endif

0 commit comments

Comments
 (0)