Skip to content

[libc++][vector<bool>] Tests shrink_to_fit requirement. #98009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,54 @@ TEST_CONSTEXPR_CXX20 bool tests()
return true;
}

#if TEST_STD_VER >= 23
template <typename T>
struct increasing_allocator {
using value_type = T;
std::size_t min_elements = 1000;
increasing_allocator() = default;

template <typename U>
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}

constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
if (n < min_elements)
n = min_elements;
min_elements += 1000;
return std::allocator<T>{}.allocate_at_least(n);
}
constexpr T* allocate(std::size_t n) { return allocate_at_least(n).ptr; }
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
};

template <typename T, typename U>
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
return true;
}

// https://github.com/llvm/llvm-project/issues/95161
constexpr bool test_increasing_allocator() {
std::vector<bool, increasing_allocator<bool>> v;
v.push_back(1);
std::size_t capacity = v.capacity();
v.shrink_to_fit();
assert(v.capacity() <= capacity);
assert(v.size() == 1);

return true;
}
#endif // TEST_STD_VER >= 23

int main(int, char**)
{
tests();
tests();
#if TEST_STD_VER > 17
static_assert(tests());
#endif
#if TEST_STD_VER >= 23
test_increasing_allocator();
static_assert(test_increasing_allocator());
#endif // TEST_STD_VER >= 23

return 0;
}
Loading