Skip to content

Commit f475bdf

Browse files
committed
Address philnik777's suggestions regarding libc++ test organizations
1 parent 874496a commit f475bdf

File tree

3 files changed

+35
-44
lines changed

3 files changed

+35
-44
lines changed

libcxx/test/libcxx/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
// void shrink_to_fit(); // constexpr since C++20
1414

15-
// Make sure we use an allocation returned by allocate_at_least if it is smaller than the current allocation
16-
// even if it contains more bytes than we requested
17-
1815
#include <cassert>
1916
#include <string>
2017

18+
#include "asan_testing.h"
19+
#include "increasing_allocator.h"
20+
2121
template <typename T>
2222
struct oversizing_allocator {
2323
using value_type = T;
@@ -37,6 +37,9 @@ bool operator==(oversizing_allocator<T>, oversizing_allocator<U>) {
3737
return true;
3838
}
3939

40+
// Make sure we use an allocation returned by allocate_at_least if it is smaller than the current allocation
41+
// even if it contains more bytes than we requested.
42+
// Fix issue: https://github.com/llvm/llvm-project/pull/115659
4043
void test_oversizing_allocator() {
4144
std::basic_string<char, std::char_traits<char>, oversizing_allocator<char>> s{
4245
"String does not fit in the internal buffer and is a bit longer"};
@@ -48,8 +51,37 @@ void test_oversizing_allocator() {
4851
assert(s.size() == size);
4952
}
5053

54+
// Ensure that the libc++ implementation of shrink_to_fit does NOT swap buffer with equal allocation sizes
55+
void test_no_swap_with_equal_allocation_size() {
56+
{ // Test with custom allocator with a minimum allocation size
57+
std::basic_string<char, std::char_traits<char>, min_size_allocator<128, char> > s(
58+
"A long string exceeding SSO limit but within min alloc size");
59+
std::size_t capacity = s.capacity();
60+
std::size_t size = s.size();
61+
auto data = s.data();
62+
s.shrink_to_fit();
63+
assert(s.capacity() <= capacity);
64+
assert(s.size() == size);
65+
assert(is_string_asan_correct(s));
66+
assert(s.capacity() == capacity && s.data() == data);
67+
}
68+
{ // Test with custom allocator with a minimum power of two allocation size
69+
std::basic_string<char, std::char_traits<char>, pow2_allocator<char> > s(
70+
"This is a long string that exceeds the SSO limit");
71+
std::size_t capacity = s.capacity();
72+
std::size_t size = s.size();
73+
auto data = s.data();
74+
s.shrink_to_fit();
75+
assert(s.capacity() <= capacity);
76+
assert(s.size() == size);
77+
assert(is_string_asan_correct(s));
78+
assert(s.capacity() == capacity && s.data() == data);
79+
}
80+
}
81+
5182
int main(int, char**) {
5283
test_oversizing_allocator();
84+
test_no_swap_with_equal_allocation_size();
5385

5486
return 0;
5587
}

libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.pass.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,6 @@ TEST_CONSTEXPR_CXX20 bool test() {
7575
}
7676
#endif
7777

78-
{ // Ensure that the libc++ implementation of shrink_to_fit does NOT swap buffer with equal allocation sizes
79-
{ // Test with custom allocator with a minimum allocation size
80-
std::basic_string<char, std::char_traits<char>, min_size_allocator<128, char> > s(
81-
"A long string exceeding SSO limit but within min alloc size");
82-
std::size_t capacity = s.capacity();
83-
std::size_t size = s.size();
84-
auto data = s.data();
85-
s.shrink_to_fit();
86-
assert(s.capacity() <= capacity);
87-
assert(s.size() == size);
88-
LIBCPP_ASSERT(is_string_asan_correct(s));
89-
LIBCPP_ASSERT(s.capacity() == capacity && s.data() == data);
90-
}
91-
{ // Test with custom allocator with a minimum power of two allocation size
92-
std::basic_string<char, std::char_traits<char>, pow2_allocator<char> > s(
93-
"This is a long string that exceeds the SSO limit");
94-
std::size_t capacity = s.capacity();
95-
std::size_t size = s.size();
96-
auto data = s.data();
97-
s.shrink_to_fit();
98-
assert(s.capacity() <= capacity);
99-
assert(s.size() == size);
100-
LIBCPP_ASSERT(is_string_asan_correct(s));
101-
LIBCPP_ASSERT(s.capacity() == capacity && s.data() == data);
102-
}
103-
}
104-
10578
return true;
10679
}
10780

libcxx/test/support/increasing_allocator.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ class min_size_allocator {
5959
template <typename U>
6060
TEST_CONSTEXPR_CXX20 min_size_allocator(const min_size_allocator<MinAllocSize, U>&) TEST_NOEXCEPT {}
6161

62-
#if TEST_STD_VER >= 23
63-
TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
64-
if (n < MinAllocSize)
65-
n = MinAllocSize;
66-
return std::allocator<T>{}.allocate_at_least(n);
67-
}
68-
#endif // TEST_STD_VER >= 23
69-
7062
TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {
7163
if (n < MinAllocSize)
7264
n = MinAllocSize;
@@ -102,12 +94,6 @@ class pow2_allocator {
10294
template <typename U>
10395
TEST_CONSTEXPR_CXX20 pow2_allocator(const pow2_allocator<U>&) TEST_NOEXCEPT {}
10496

105-
#if TEST_STD_VER >= 23
106-
TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
107-
return std::allocator<T>{}.allocate_at_least(next_power_of_two(n));
108-
}
109-
#endif // TEST_STD_VER >= 23
110-
11197
TEST_NODISCARD TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) {
11298
return std::allocator<T>().allocate(next_power_of_two(n));
11399
}

0 commit comments

Comments
 (0)