Skip to content

Commit 9dc3a89

Browse files
committed
Make tests portable
1 parent 15e39e1 commit 9dc3a89

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
// <vector>
10+
// vector<bool>
1011

1112
// size_type max_size() const;
1213

@@ -28,47 +29,46 @@
2829

2930
template <typename Alloc>
3031
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)
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

4854
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()
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
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
template <typename T, typename Alloc>
2828
TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
2929
using Vector = std::vector<T, Alloc>;
30-
using alloc_traits = typename Vector::__alloc_traits;
30+
using alloc_traits = std::allocator_traits<typename Vector::allocator_type>;
3131
using size_type = typename Vector::size_type;
3232
using difference_type = typename Vector::difference_type;
3333
const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
@@ -37,7 +37,7 @@ TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
3737
LIBCPP_ASSERT(v.max_size() == std::min<size_type>(max_dist, max_alloc));
3838
}
3939

40-
#endif
40+
#endif // TEST_STD_VER >= 11
4141

4242
TEST_CONSTEXPR_CXX20 bool tests() {
4343
{
@@ -82,7 +82,7 @@ TEST_CONSTEXPR_CXX20 bool tests() {
8282
test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >());
8383
}
8484

85-
#endif
85+
#endif // TEST_STD_VER >= 11
8686

8787
return true;
8888
}

0 commit comments

Comments
 (0)