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 <algorithm> |
14 | #include <cassert> |
15 | #include <cstdint> |
16 | #include <limits> |
17 | #include <type_traits> |
18 | #include <vector> |
19 | |
20 | #include "min_allocator.h" |
21 | #include "sized_allocator.h" |
22 | #include "test_allocator.h" |
23 | #include "test_macros.h" |
24 | |
25 | #if TEST_STD_VER >= 11 |
26 | |
27 | template <typename T, typename Alloc> |
28 | TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) { |
29 | using Vector = std::vector<T, Alloc>; |
30 | using alloc_traits = std::allocator_traits<typename Vector::allocator_type>; |
31 | using size_type = typename Vector::size_type; |
32 | using difference_type = typename Vector::difference_type; |
33 | const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max()); |
34 | const size_type max_alloc = alloc_traits::max_size(v.get_allocator()); |
35 | assert(v.max_size() <= max_dist); |
36 | assert(v.max_size() <= max_alloc); |
37 | LIBCPP_ASSERT(v.max_size() == std::min<size_type>(max_dist, max_alloc)); |
38 | } |
39 | |
40 | #endif // TEST_STD_VER >= 11 |
41 | |
42 | TEST_CONSTEXPR_CXX20 bool tests() { |
43 | { |
44 | typedef limited_allocator<int, 10> A; |
45 | typedef std::vector<int, A> C; |
46 | C c; |
47 | assert(c.max_size() <= 10); |
48 | LIBCPP_ASSERT(c.max_size() == 10); |
49 | } |
50 | { |
51 | typedef limited_allocator<int, (std::size_t)-1> A; |
52 | typedef std::vector<int, A> C; |
53 | const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max()); |
54 | C c; |
55 | assert(c.max_size() <= max_dist); |
56 | LIBCPP_ASSERT(c.max_size() == max_dist); |
57 | } |
58 | { |
59 | typedef std::vector<char> C; |
60 | const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max()); |
61 | C c; |
62 | assert(c.max_size() <= max_dist); |
63 | assert(c.max_size() <= alloc_max_size(c.get_allocator())); |
64 | LIBCPP_ASSERT(c.max_size() == std::min(max_dist, alloc_max_size(c.get_allocator()))); |
65 | } |
66 | |
67 | #if TEST_STD_VER >= 11 |
68 | |
69 | // Test with various allocators and diffrent size_type |
70 | { |
71 | test(std::vector<int>()); |
72 | test(std::vector<short, std::allocator<short> >()); |
73 | test(std::vector<unsigned, min_allocator<unsigned> >()); |
74 | test(std::vector<char, test_allocator<char> >(test_allocator<char>(1))); |
75 | test(std::vector<std::size_t, other_allocator<std::size_t> >(other_allocator<std::size_t>(5))); |
76 | test(std::vector<int, sized_allocator<int, std::uint8_t, std::int8_t> >()); |
77 | test(std::vector<int, sized_allocator<int, std::uint16_t, std::int16_t> >()); |
78 | test(std::vector<int, sized_allocator<int, std::uint32_t, std::int32_t> >()); |
79 | test(std::vector<int, sized_allocator<int, std::uint64_t, std::int64_t> >()); |
80 | test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1)> >()); |
81 | test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 2> >()); |
82 | test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >()); |
83 | } |
84 | |
85 | #endif // TEST_STD_VER >= 11 |
86 | |
87 | return true; |
88 | } |
89 | |
90 | int main(int, char**) { |
91 | tests(); |
92 | |
93 | #if TEST_STD_VER > 17 |
94 | static_assert(tests()); |
95 | #endif |
96 | |
97 | return 0; |
98 | } |
99 | |