Skip to content

Commit 9d584f7

Browse files
committed
Factor out increasing_allocator
1 parent 379ef73 commit 9d584f7

File tree

4 files changed

+128
-156
lines changed

4 files changed

+128
-156
lines changed

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

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,35 @@
1111

1212
// void shrink_to_fit();
1313

14-
#include <vector>
1514
#include <cassert>
15+
#include <vector>
1616

17-
#include "test_macros.h"
17+
#include "increasing_allocator.h"
1818
#include "min_allocator.h"
19+
#include "test_macros.h"
1920

20-
TEST_CONSTEXPR_CXX20 bool tests()
21-
{
22-
{
23-
std::vector<bool> v(100);
24-
v.push_back(1);
25-
v.shrink_to_fit();
26-
assert(v.capacity() >= 101);
27-
assert(v.size() >= 101);
28-
}
21+
TEST_CONSTEXPR_CXX20 bool tests() {
22+
{
23+
std::vector<bool> v(100);
24+
v.push_back(1);
25+
v.shrink_to_fit();
26+
assert(v.capacity() >= 101);
27+
assert(v.size() >= 101);
28+
}
2929
#if TEST_STD_VER >= 11
30-
{
31-
std::vector<bool, min_allocator<bool>> v(100);
32-
v.push_back(1);
33-
v.shrink_to_fit();
34-
assert(v.capacity() >= 101);
35-
assert(v.size() >= 101);
36-
}
37-
#endif
38-
39-
return true;
40-
}
41-
42-
#if TEST_STD_VER >= 23
43-
template <typename T>
44-
struct increasing_allocator {
45-
using value_type = T;
46-
std::size_t min_elements = 1000;
47-
increasing_allocator() = default;
48-
49-
template <typename U>
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);
30+
{
31+
std::vector<bool, min_allocator<bool>> v(100);
32+
v.push_back(1);
33+
v.shrink_to_fit();
34+
assert(v.capacity() >= 101);
35+
assert(v.size() >= 101);
5736
}
58-
constexpr T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
59-
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
60-
};
37+
#endif
6138

62-
template <typename T, typename U>
63-
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
6439
return true;
6540
}
6641

42+
#if TEST_STD_VER >= 23
6743
// https://github.com/llvm/llvm-project/issues/95161
6844
constexpr bool test_increasing_allocator() {
6945
std::vector<bool, increasing_allocator<bool>> v;
@@ -77,16 +53,15 @@ constexpr bool test_increasing_allocator() {
7753
}
7854
#endif // TEST_STD_VER >= 23
7955

80-
int main(int, char**)
81-
{
56+
int main(int, char**) {
8257
tests();
8358
#if TEST_STD_VER > 17
84-
static_assert(tests());
59+
static_assert(tests());
8560
#endif
8661
#if TEST_STD_VER >= 23
87-
test_increasing_allocator();
88-
static_assert(test_increasing_allocator());
62+
test_increasing_allocator();
63+
static_assert(test_increasing_allocator());
8964
#endif // TEST_STD_VER >= 23
9065

91-
return 0;
66+
return 0;
9267
}

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

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -10,92 +10,70 @@
1010

1111
// void shrink_to_fit();
1212

13-
#include <vector>
1413
#include <cassert>
15-
#include "test_macros.h"
16-
#include "test_allocator.h"
17-
#include "min_allocator.h"
14+
#include <vector>
15+
1816
#include "asan_testing.h"
17+
#include "increasing_allocator.h"
18+
#include "min_allocator.h"
19+
#include "test_allocator.h"
20+
#include "test_macros.h"
1921

2022
TEST_CONSTEXPR_CXX20 bool tests() {
21-
{
22-
std::vector<int> v(100);
23-
v.push_back(1);
24-
assert(is_contiguous_container_asan_correct(v));
25-
v.shrink_to_fit();
26-
assert(v.capacity() == 101);
27-
assert(v.size() == 101);
28-
assert(is_contiguous_container_asan_correct(v));
29-
}
30-
{
31-
std::vector<int, limited_allocator<int, 401> > v(100);
32-
v.push_back(1);
33-
assert(is_contiguous_container_asan_correct(v));
34-
v.shrink_to_fit();
35-
assert(v.capacity() == 101);
36-
assert(v.size() == 101);
37-
assert(is_contiguous_container_asan_correct(v));
38-
}
23+
{
24+
std::vector<int> v(100);
25+
v.push_back(1);
26+
assert(is_contiguous_container_asan_correct(v));
27+
v.shrink_to_fit();
28+
assert(v.capacity() == 101);
29+
assert(v.size() == 101);
30+
assert(is_contiguous_container_asan_correct(v));
31+
}
32+
{
33+
std::vector<int, limited_allocator<int, 401> > v(100);
34+
v.push_back(1);
35+
assert(is_contiguous_container_asan_correct(v));
36+
v.shrink_to_fit();
37+
assert(v.capacity() == 101);
38+
assert(v.size() == 101);
39+
assert(is_contiguous_container_asan_correct(v));
40+
}
3941
#ifndef TEST_HAS_NO_EXCEPTIONS
40-
if (!TEST_IS_CONSTANT_EVALUATED) {
41-
std::vector<int, limited_allocator<int, 400> > v(100);
42-
v.push_back(1);
43-
assert(is_contiguous_container_asan_correct(v));
44-
v.shrink_to_fit();
45-
LIBCPP_ASSERT(v.capacity() == 200); // assumes libc++'s 2x growth factor
46-
assert(v.size() == 101);
47-
assert(is_contiguous_container_asan_correct(v));
48-
}
42+
if (!TEST_IS_CONSTANT_EVALUATED) {
43+
std::vector<int, limited_allocator<int, 400> > v(100);
44+
v.push_back(1);
45+
assert(is_contiguous_container_asan_correct(v));
46+
v.shrink_to_fit();
47+
LIBCPP_ASSERT(v.capacity() == 200); // assumes libc++'s 2x growth factor
48+
assert(v.size() == 101);
49+
assert(is_contiguous_container_asan_correct(v));
50+
}
4951
#endif
5052
#if TEST_STD_VER >= 11
51-
{
52-
std::vector<int, min_allocator<int>> v(100);
53-
v.push_back(1);
54-
assert(is_contiguous_container_asan_correct(v));
55-
v.shrink_to_fit();
56-
assert(v.capacity() == 101);
57-
assert(v.size() == 101);
58-
assert(is_contiguous_container_asan_correct(v));
59-
}
60-
{
61-
std::vector<int, safe_allocator<int>> v(100);
62-
v.push_back(1);
63-
assert(is_contiguous_container_asan_correct(v));
64-
v.shrink_to_fit();
65-
assert(v.capacity() == 101);
66-
assert(v.size() == 101);
67-
assert(is_contiguous_container_asan_correct(v));
68-
}
69-
#endif
70-
71-
return true;
72-
}
73-
74-
#if TEST_STD_VER >= 23
75-
template <typename T>
76-
struct increasing_allocator {
77-
using value_type = T;
78-
std::size_t min_elements = 1000;
79-
increasing_allocator() = default;
80-
81-
template <typename U>
82-
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
83-
84-
constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
85-
if (n < min_elements)
86-
n = min_elements;
87-
min_elements += 1000;
88-
return std::allocator<T>{}.allocate_at_least(n);
53+
{
54+
std::vector<int, min_allocator<int>> v(100);
55+
v.push_back(1);
56+
assert(is_contiguous_container_asan_correct(v));
57+
v.shrink_to_fit();
58+
assert(v.capacity() == 101);
59+
assert(v.size() == 101);
60+
assert(is_contiguous_container_asan_correct(v));
61+
}
62+
{
63+
std::vector<int, safe_allocator<int>> v(100);
64+
v.push_back(1);
65+
assert(is_contiguous_container_asan_correct(v));
66+
v.shrink_to_fit();
67+
assert(v.capacity() == 101);
68+
assert(v.size() == 101);
69+
assert(is_contiguous_container_asan_correct(v));
8970
}
90-
constexpr T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
91-
constexpr void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
92-
};
71+
#endif
9372

94-
template <typename T, typename U>
95-
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
9673
return true;
9774
}
9875

76+
#if TEST_STD_VER >= 23
9977
// https://github.com/llvm/llvm-project/issues/95161
10078
constexpr bool test_increasing_allocator() {
10179
std::vector<int, increasing_allocator<int>> v;
@@ -111,16 +89,15 @@ constexpr bool test_increasing_allocator() {
11189
}
11290
#endif // TEST_STD_VER >= 23
11391

114-
int main(int, char**)
115-
{
92+
int main(int, char**) {
11693
tests();
11794
#if TEST_STD_VER > 17
118-
static_assert(tests());
95+
static_assert(tests());
11996
#endif
12097
#if TEST_STD_VER >= 23
121-
test_increasing_allocator();
122-
static_assert(test_increasing_allocator());
98+
test_increasing_allocator();
99+
static_assert(test_increasing_allocator());
123100
#endif
124101

125-
return 0;
102+
return 0;
126103
}

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

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
// void shrink_to_fit(); // constexpr since C++20
1212

13-
#include <string>
1413
#include <cassert>
14+
#include <string>
1515

16-
#include "test_macros.h"
17-
#include "min_allocator.h"
1816
#include "asan_testing.h"
17+
#include "increasing_allocator.h"
18+
#include "min_allocator.h"
19+
#include "test_macros.h"
1920

2021
template <class S>
2122
TEST_CONSTEXPR_CXX20 void test(S s) {
@@ -64,30 +65,6 @@ TEST_CONSTEXPR_CXX20 bool test() {
6465
}
6566

6667
#if TEST_STD_VER >= 23
67-
std::size_t min_bytes = 1000;
68-
69-
template <typename T>
70-
struct increasing_allocator {
71-
using value_type = T;
72-
increasing_allocator() = default;
73-
template <typename U>
74-
increasing_allocator(const increasing_allocator<U>&) noexcept {}
75-
std::allocation_result<T*> allocate_at_least(std::size_t n) {
76-
std::size_t allocation_amount = n * sizeof(T);
77-
if (allocation_amount < min_bytes)
78-
allocation_amount = min_bytes;
79-
min_bytes += 1000;
80-
return {static_cast<T*>(::operator new(allocation_amount)), allocation_amount / sizeof(T)};
81-
}
82-
T* allocate(std::size_t n) { return std::allocator<T>{}.allocate(n); }
83-
void deallocate(T* p, std::size_t n) noexcept { std::allocator<T>{}.deallocate(p, n); }
84-
};
85-
86-
template <typename T, typename U>
87-
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
88-
return true;
89-
}
90-
9168
// https://github.com/llvm/llvm-project/issues/95161
9269
void test_increasing_allocator() {
9370
std::basic_string<char, std::char_traits<char>, increasing_allocator<char>> s{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef INCREASING_ALLOCATOR_H
10+
#define INCREASING_ALLOCATOR_H
11+
12+
#include <cstddef>
13+
#include <memory>
14+
15+
#include "test_macros.h"
16+
17+
#if TEST_STD_VER >= 23
18+
template <typename T>
19+
struct increasing_allocator {
20+
using value_type = T;
21+
std::size_t min_elements = 1000;
22+
increasing_allocator() = default;
23+
24+
template <typename U>
25+
constexpr increasing_allocator(const increasing_allocator<U>& other) noexcept : min_elements(other.min_elements) {}
26+
27+
constexpr std::allocation_result<T*> allocate_at_least(std::size_t n) {
28+
if (n < min_elements)
29+
n = min_elements;
30+
min_elements += 1000;
31+
return std::allocator<T>{}.allocate_at_least(n);
32+
}
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); }
35+
};
36+
37+
template <typename T, typename U>
38+
bool operator==(increasing_allocator<T>, increasing_allocator<U>) {
39+
return true;
40+
}
41+
#endif // TEST_STD_VER >= 23
42+
43+
#endif // INCREASING_ALLOCATOR_H

0 commit comments

Comments
 (0)