77// ===----------------------------------------------------------------------===//
88
99// <vector>
10+ // vector<bool>
1011
1112// size_type max_size() const;
1213
2829
2930template <typename Alloc>
3031TEST_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)
3441 using storage_type = typename Vector::__storage_type;
3542 using storage_alloc = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
3643 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 ());
3844 const size_type max_alloc = storage_traits::max_size (storage_alloc (v.get_allocator ()));
3945 std::size_t bits_per_word = sizeof (storage_type) * CHAR_BIT;
4046 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);
4247 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)
4450}
4551
46- #endif
52+ #endif // TEST_STD_VER >= 11
4753
4854TEST_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()
5059 {
5160 using Alloc = limited_allocator<bool , 10 >;
5261 using Vector = std::vector<bool , Alloc>;
5362 using storage_type = Vector::__storage_type;
5463 Vector v;
5564 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);
6766 }
67+ #endif // defined(_LIBCPP_VERSION)
6868
6969#if TEST_STD_VER >= 11
7070
71- // Test with various allocators and diffrent size_type
71+ // Test with various allocators and different ` size_type`s
7272 {
7373 test (std::vector<bool >());
7474 test (std::vector<bool , std::allocator<int > >());
@@ -82,15 +82,14 @@ TEST_CONSTEXPR_CXX20 bool tests() {
8282 test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 )> >());
8383 }
8484
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.
8887 {
8988 test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 ) / 61 > >());
9089 test (std::vector<bool , limited_allocator<bool , static_cast <std::size_t >(-1 ) / 63 > >());
9190 }
9291
93- #endif
92+ #endif // TEST_STD_VER >= 11
9493
9594 return true ;
9695}
0 commit comments