66//
77// ===----------------------------------------------------------------------===//
88
9- #ifndef INCREASING_ALLOCATOR_H
10- #define INCREASING_ALLOCATOR_H
9+ #ifndef TEST_SUPPORT_INCREASING_ALLOCATOR_H
10+ #define TEST_SUPPORT_INCREASING_ALLOCATOR_H
1111
1212#include < cstddef>
1313#include < memory>
1414
1515#include " test_macros.h"
1616
17- #if TEST_STD_VER >= 23
17+ // The increasing_allocator is a custom allocator that enforces an increasing minimum allocation size,
18+ // ensuring that it allocates an increasing amount of memory, possibly exceeding the requested amount.
19+ // This unique behavior is particularly useful for testing the shrink_to_fit functionality in std::vector,
20+ // vector<bool>, and std::basic_string, ensuring that shrink_to_fit does not increase the capacity of
21+ // the allocated memory.
22+
1823template <typename T>
1924struct increasing_allocator {
2025 using value_type = T;
2126 std::size_t min_elements = 1000 ;
2227 increasing_allocator () = default ;
2328
2429 template <typename U>
25- constexpr increasing_allocator (const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
30+ TEST_CONSTEXPR_CXX20 increasing_allocator (const increasing_allocator<U>& other) TEST_NOEXCEPT
31+ : min_elements(other.min_elements) {}
2632
27- constexpr std::allocation_result<T*> allocate_at_least (std::size_t n) {
33+ #if TEST_STD_VER >= 23
34+ TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least (std::size_t n) {
2835 if (n < min_elements)
2936 n = min_elements;
3037 min_elements += 1000 ;
3138 return std::allocator<T>{}.allocate_at_least (n);
3239 }
33- constexpr T* allocate (std::size_t n) { return std::allocator<T>{}.allocate (n); }
34- constexpr void deallocate (T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate (p, n); }
40+ #endif // TEST_STD_VER >= 23
41+
42+ TEST_CONSTEXPR_CXX20 T* allocate (std::size_t n) {
43+ #if TEST_STD_VER >= 23
44+ return allocate_at_least (n).ptr ;
45+ #else
46+ if (n < min_elements)
47+ n = min_elements;
48+ min_elements += 1000 ;
49+ return std::allocator<T>{}.allocate (n);
50+ #endif
51+ }
52+
53+ TEST_CONSTEXPR_CXX20 void deallocate (T* p, std::size_t n) TEST_NOEXCEPT { std::allocator<T>{}.deallocate (p, n); }
3554};
3655
3756template <typename T, typename U>
38- bool operator ==(increasing_allocator<T>, increasing_allocator<U>) {
57+ TEST_CONSTEXPR_CXX20 bool operator ==(increasing_allocator<T>, increasing_allocator<U>) TEST_NOEXCEPT {
3958 return true ;
4059}
41- #endif // TEST_STD_VER >= 23
4260
43- #endif // INCREASING_ALLOCATOR_H
61+ #endif // TEST_SUPPORT_INCREASING_ALLOCATOR_H
0 commit comments