7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
// <vector>
10
+ // vector<bool>
10
11
11
12
// size_type max_size() const;
12
13
28
29
29
30
template <typename Alloc>
30
31
TEST_CONSTEXPR_CXX20 void test (const std::vector<bool , Alloc>& v) {
31
- using Vector = std::vector<bool , Alloc>;
32
- using size_type = typename Vector::size_type;
33
- using difference_type = typename Vector::difference_type;
32
+ using Vector = std::vector<bool , Alloc>;
33
+ using size_type = typename Vector::size_type;
34
+ using difference_type = typename Vector::difference_type;
35
+ const size_type max_dist = static_cast <size_type>(std::numeric_limits<difference_type>::max ());
36
+ assert (v.max_size () <= max_dist);
37
+
38
+ // The following check is specific to libc++ implementation details and is not portable to libstdc++
39
+ // and MSVC STL, as they use different types for the underlying word storage.
40
+ # if defined(_LIBCPP_VERSION)
34
41
using storage_type = typename Vector::__storage_type;
35
42
using storage_alloc = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
36
43
using storage_traits = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>;
37
- const size_type max_dist = static_cast <size_type>(std::numeric_limits<difference_type>::max ());
38
44
const size_type max_alloc = storage_traits::max_size (storage_alloc (v.get_allocator ()));
39
45
std::size_t bits_per_word = sizeof (storage_type) * CHAR_BIT;
40
46
const size_type max_size = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word;
41
- assert (v.max_size () <= max_dist);
42
47
assert (v.max_size () / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow
43
- LIBCPP_ASSERT (v.max_size () == max_size);
48
+ assert (v.max_size () == max_size);
49
+ # endif // defined(_LIBCPP_VERSION)
44
50
}
45
51
46
- #endif
52
+ #endif // TEST_STD_VER >= 11
47
53
48
54
TEST_CONSTEXPR_CXX20 bool tests () {
49
- // Test with limited_allocator where v.max_size() is determined by allocator::max_size()
55
+ // The following check is specific to libc++ implementation details and is not portable to libstdc++
56
+ // and MSVC STL, as they use different types for the underlying word storage.
57
+ #if defined(_LIBCPP_VERSION)
58
+ // Test cases where v.max_size() is determined by allocator::max_size()
50
59
{
51
60
using Alloc = limited_allocator<bool , 10 >;
52
61
using Vector = std::vector<bool , Alloc>;
53
62
using storage_type = Vector::__storage_type;
54
63
Vector v;
55
64
std::size_t bits_per_word = sizeof (storage_type) * CHAR_BIT;
56
- assert (v.max_size () <= 10 * bits_per_word);
57
- LIBCPP_ASSERT (v.max_size () == 10 * bits_per_word);
58
- }
59
- {
60
- using Alloc = limited_allocator<double , 10 >;
61
- using Vector = std::vector<bool , Alloc>;
62
- using storage_type = Vector::__storage_type;
63
- Vector v;
64
- std::size_t bits_per_word = sizeof (storage_type) * CHAR_BIT;
65
- assert (v.max_size () <= 10 * bits_per_word);
66
- LIBCPP_ASSERT (v.max_size () == 10 * bits_per_word);
65
+ assert (v.max_size () == 10 * bits_per_word);
67
66
}
67
+ #endif // defined(_LIBCPP_VERSION)
68
68
69
69
#if TEST_STD_VER >= 11
70
70
71
- // Test with various allocators and diffrent size_type
71
+ // Test with various allocators and different ` size_type`s
72
72
{
73
73
test (std::vector<bool >());
74
74
test (std::vector<bool , std::allocator<int > >());
@@ -82,15 +82,14 @@ TEST_CONSTEXPR_CXX20 bool tests() {
82
82
test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 )> >());
83
83
}
84
84
85
- // Test cases to identify incorrect implementations that unconditionally return:
86
- // std::min<size_type>(__nmax, __internal_cap_to_external(__amax))
87
- // This can cause overflow in __internal_cap_to_external and lead to incorrect results.
85
+ // Test cases to identify incorrect implementations that unconditionally compute an internal-to-external
86
+ // capacity in a way that can overflow, leading to incorrect results.
88
87
{
89
88
test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 ) / 61 > >());
90
89
test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 ) / 63 > >());
91
90
}
92
91
93
- #endif
92
+ #endif // TEST_STD_VER >= 11
94
93
95
94
return true ;
96
95
}
0 commit comments