Skip to content

Commit 09f029c

Browse files
committed
Address @ldionne comments
1 parent b91cc18 commit 09f029c

File tree

2 files changed

+78
-39
lines changed

2 files changed

+78
-39
lines changed

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

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,74 @@
2020
#include <new>
2121
#include <vector>
2222

23+
#include "sized_allocator.h"
2324
#include "test_macros.h"
2425

25-
template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t>
26-
class sized_allocator {
27-
template <typename U, typename Sz, typename Diff>
28-
friend class sized_allocator;
29-
30-
public:
31-
using value_type = T;
32-
using size_type = SIZE_TYPE;
33-
using difference_type = DIFF_TYPE;
34-
using propagate_on_container_swap = std::true_type;
35-
36-
TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
37-
38-
template <typename U, typename Sz, typename Diff>
39-
TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}
40-
41-
TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
42-
if (n > max_size())
43-
TEST_THROW(std::bad_array_new_length());
44-
return std::allocator<T>().allocate(n);
45-
}
46-
47-
TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
48-
49-
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
50-
return std::numeric_limits<size_type>::max() / sizeof(value_type);
51-
}
52-
53-
private:
54-
int data_;
55-
56-
TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {
57-
return a.data_ == b.data_;
58-
}
59-
TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {
60-
return a.data_ != b.data_;
61-
}
62-
};
63-
6426
TEST_CONSTEXPR_CXX20 bool tests() {
6527
// The following tests are typical ways to trigger reallocations where `std::max` is used to calculate the capacity.
28+
// The purpose of these tests is to ensure that the ambiguous internal calls to `std::max` have been fixed.
6629
{
6730
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
6831
std::vector<bool, Alloc> c(Alloc(1));
6932
c.resize(10);
33+
assert(c.size() == 10);
34+
assert(c.capacity() >= 10);
7035
}
7136
{
7237
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7338
std::vector<bool, Alloc> c(Alloc(1));
7439
c.assign(10, true);
40+
assert(c.size() == 10);
41+
assert(c.capacity() >= 10);
7542
}
7643
{
7744
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
7845
std::vector<bool, Alloc> c(Alloc(1));
7946
c.insert(c.end(), true);
47+
assert(c.size() == 1);
48+
assert(c.capacity() >= 1);
8049
}
8150
{
8251
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8352
std::vector<bool, Alloc> c(Alloc(1));
8453
c.insert(c.end(), 10, true);
54+
assert(c.size() == 10);
55+
assert(c.capacity() >= 10);
8556
}
8657
{
8758
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
8859
std::vector<bool, Alloc> c(Alloc(1));
8960
c.push_back(true);
61+
assert(c.size() == 1);
62+
assert(c.capacity() >= 1);
9063
}
9164
{
9265
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
9366
std::vector<bool, Alloc> c(Alloc(1));
9467
c.resize(10, true);
68+
assert(c.size() == 10);
69+
assert(c.capacity() >= 10);
9570
}
9671
{
9772
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
9873
std::vector<bool, Alloc> c(Alloc(1));
9974
c.resize(10);
75+
assert(c.size() == 10);
76+
assert(c.capacity() >= 10);
10077
}
10178
{
10279
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
10380
std::vector<bool, Alloc> c(Alloc(1));
10481
c.resize(10);
82+
assert(c.size() == 10);
83+
assert(c.capacity() >= 10);
10584
}
10685
{
10786
using Alloc = sized_allocator<bool, std::size_t, std::ptrdiff_t>;
10887
std::vector<bool, Alloc> c(Alloc(1));
10988
c.resize(10);
89+
assert(c.size() == 10);
90+
assert(c.capacity() >= 10);
11091
}
11192

11293
return true;

libcxx/test/support/sized_allocator.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 TEST_SUPPORT_SIZED_ALLOCATOR_H
10+
#define TEST_SUPPORT_SIZED_ALLOCATOR_H
11+
12+
#include <cstddef>
13+
#include <limits>
14+
#include <memory>
15+
#include <new>
16+
17+
#include "test_macros.h"
18+
19+
template <typename T, typename Size = std::size_t, typename Difference = std::ptrdiff_t>
20+
class sized_allocator {
21+
template <typename U, typename Sz, typename Diff>
22+
friend class sized_allocator;
23+
24+
public:
25+
using value_type = T;
26+
using size_type = Size;
27+
using difference_type = Difference;
28+
using propagate_on_container_swap = std::true_type;
29+
30+
TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
31+
32+
template <typename U, typename Sz, typename Diff>
33+
TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}
34+
35+
TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
36+
if (n > max_size())
37+
TEST_THROW(std::bad_array_new_length());
38+
return std::allocator<T>().allocate(n);
39+
}
40+
41+
TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
42+
43+
TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
44+
return std::numeric_limits<size_type>::max() / sizeof(value_type);
45+
}
46+
47+
private:
48+
int data_;
49+
50+
TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {
51+
return a.data_ == b.data_;
52+
}
53+
TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {
54+
return a.data_ != b.data_;
55+
}
56+
};
57+
58+
#endif // TEST_SUPPORT_SIZED_ALLOCATOR_H

0 commit comments

Comments
 (0)