Skip to content

Commit f08d58c

Browse files
committed
Slightly simplify max_size and add new tests for vector
1 parent 3ad2399 commit f08d58c

File tree

4 files changed

+210
-11
lines changed

4 files changed

+210
-11
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,8 @@ template <class _Allocator>
511511
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
512512
vector<bool, _Allocator>::max_size() const _NOEXCEPT {
513513
size_type __amax = __storage_traits::max_size(__alloc_);
514-
size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
515-
if (__nmax / __bits_per_word <= __amax)
516-
return __nmax;
517-
return __internal_cap_to_external(__amax);
514+
size_type __nmax = numeric_limits<difference_type>::max();
515+
return __nmax / __bits_per_word <= __amax ? __nmax : __internal_cap_to_external(__amax);
518516
}
519517

520518
// Precondition: __new_size > capacity()
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
// <vector>
10+
11+
// size_type max_size() const;
12+
13+
#include <cassert>
14+
#include <climits>
15+
#include <cstdint>
16+
#include <limits>
17+
#include <memory>
18+
#include <type_traits>
19+
#include <vector>
20+
21+
#include "min_allocator.h"
22+
#include "sized_allocator.h"
23+
#include "test_allocator.h"
24+
#include "test_macros.h"
25+
26+
#if TEST_STD_VER >= 11
27+
28+
template <typename Alloc>
29+
TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) {
30+
using Vector = std::vector<bool, Alloc>;
31+
using size_type = typename Vector::size_type;
32+
using difference_type = typename Vector::difference_type;
33+
using storage_type = typename Vector::__storage_type;
34+
using storage_alloc = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
35+
using storage_traits = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>;
36+
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
37+
const size_type max_alloc = storage_traits::max_size(storage_alloc(v.get_allocator()));
38+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
39+
const size_type max_size = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word;
40+
assert(v.max_size() <= max_dist);
41+
assert(v.max_size() / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow
42+
LIBCPP_ASSERT(v.max_size() == max_size);
43+
}
44+
45+
#endif
46+
47+
TEST_CONSTEXPR_CXX20 bool tests() {
48+
// Test with limited_allocator where v.max_size() is determined by allocator::max_size()
49+
{
50+
using Alloc = limited_allocator<bool, 10>;
51+
using Vector = std::vector<bool, Alloc>;
52+
using storage_type = Vector::__storage_type;
53+
Vector v;
54+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
55+
assert(v.max_size() <= 10 * bits_per_word);
56+
LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
57+
}
58+
{
59+
using Alloc = limited_allocator<double, 10>;
60+
using Vector = std::vector<bool, Alloc>;
61+
using storage_type = Vector::__storage_type;
62+
Vector v;
63+
std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
64+
assert(v.max_size() <= 10 * bits_per_word);
65+
LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
66+
}
67+
68+
#if TEST_STD_VER >= 11
69+
70+
// Test with various allocators and diffrent size_type
71+
{
72+
test(std::vector<bool>());
73+
test(std::vector<bool, std::allocator<int> >());
74+
test(std::vector<bool, min_allocator<bool> >());
75+
test(std::vector<bool, test_allocator<bool> >(test_allocator<bool>(1)));
76+
test(std::vector<bool, other_allocator<bool> >(other_allocator<bool>(5)));
77+
test(std::vector<bool, sized_allocator<bool, std::uint8_t, std::int8_t> >());
78+
test(std::vector<bool, sized_allocator<bool, std::uint16_t, std::int16_t> >());
79+
test(std::vector<bool, sized_allocator<bool, std::uint32_t, std::int32_t> >());
80+
test(std::vector<bool, sized_allocator<bool, std::uint64_t, std::int64_t> >());
81+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1)> >());
82+
}
83+
84+
// Test cases to identify incorrect implementations that unconditionally return:
85+
// std::min<size_type>(__nmax, __internal_cap_to_external(__amax))
86+
// This can cause overflow in __internal_cap_to_external and lead to incorrect results.
87+
{
88+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 61> >());
89+
test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 63> >());
90+
}
91+
92+
#endif
93+
94+
return true;
95+
}
96+
97+
int main(int, char**) {
98+
tests();
99+
100+
#if TEST_STD_VER >= 20
101+
static_assert(tests());
102+
#endif
103+
104+
return 0;
105+
}

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,34 @@
1111
// size_type max_size() const;
1212

1313
#include <cassert>
14+
#include <cstdint>
1415
#include <limits>
1516
#include <type_traits>
1617
#include <vector>
1718

19+
#include "min_allocator.h"
20+
#include "sized_allocator.h"
1821
#include "test_allocator.h"
1922
#include "test_macros.h"
2023

24+
#if TEST_STD_VER >= 11
2125

22-
TEST_CONSTEXPR_CXX20 bool test() {
26+
template <typename T, typename Alloc>
27+
TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
28+
using Vector = std::vector<T, Alloc>;
29+
using alloc_traits = typename Vector::__alloc_traits;
30+
using size_type = typename Vector::size_type;
31+
using difference_type = typename Vector::difference_type;
32+
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
33+
const size_type max_alloc = alloc_traits::max_size(v.get_allocator());
34+
assert(v.max_size() <= max_dist);
35+
assert(v.max_size() <= max_alloc);
36+
LIBCPP_ASSERT(v.max_size() == std::min<size_type>(max_dist, max_alloc));
37+
}
38+
39+
#endif
40+
41+
TEST_CONSTEXPR_CXX20 bool tests() {
2342
{
2443
typedef limited_allocator<int, 10> A;
2544
typedef std::vector<int, A> C;
@@ -30,29 +49,48 @@ TEST_CONSTEXPR_CXX20 bool test() {
3049
{
3150
typedef limited_allocator<int, (std::size_t)-1> A;
3251
typedef std::vector<int, A> C;
33-
const C::size_type max_dist =
34-
static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
52+
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
3553
C c;
3654
assert(c.max_size() <= max_dist);
3755
LIBCPP_ASSERT(c.max_size() == max_dist);
3856
}
3957
{
4058
typedef std::vector<char> C;
41-
const C::size_type max_dist =
42-
static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
59+
const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
4360
C c;
4461
assert(c.max_size() <= max_dist);
4562
assert(c.max_size() <= alloc_max_size(c.get_allocator()));
63+
LIBCPP_ASSERT(c.max_size() == std::min(max_dist, alloc_max_size(c.get_allocator())));
64+
}
65+
66+
#if TEST_STD_VER >= 11
67+
68+
// Test with various allocators and diffrent size_type
69+
{
70+
test(std::vector<int>());
71+
test(std::vector<short, std::allocator<short> >());
72+
test(std::vector<unsigned, min_allocator<unsigned> >());
73+
test(std::vector<char, test_allocator<char> >(test_allocator<char>(1)));
74+
test(std::vector<std::size_t, other_allocator<std::size_t> >(other_allocator<std::size_t>(5)));
75+
test(std::vector<int, sized_allocator<int, std::uint8_t, std::int8_t> >());
76+
test(std::vector<int, sized_allocator<int, std::uint16_t, std::int16_t> >());
77+
test(std::vector<int, sized_allocator<int, std::uint32_t, std::int32_t> >());
78+
test(std::vector<int, sized_allocator<int, std::uint64_t, std::int64_t> >());
79+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1)> >());
80+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 2> >());
81+
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >());
4682
}
4783

84+
#endif
85+
4886
return true;
4987
}
5088

5189
int main(int, char**) {
52-
test();
90+
tests();
5391

5492
#if TEST_STD_VER > 17
55-
static_assert(test());
93+
static_assert(tests());
5694
#endif
5795

5896
return 0;

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_TYPE = std::size_t, typename DIFF_TYPE = 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_TYPE;
27+
using difference_type = DIFF_TYPE;
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

0 commit comments

Comments
 (0)