[libcxx-commits] [libcxx] [libc++] Slightly simplify max_size and add new tests for vector (PR #119990)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 29 15:40:01 PST 2025


https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/119990

>From fff3da28f3b72f4f4b5d0fd66d68d7fb96cd4e54 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sat, 14 Dec 2024 18:35:36 -0500
Subject: [PATCH 1/2] Slightly simplify max_size and add new tests for vector

---
 libcxx/include/__vector/vector_bool.h         |   6 +-
 .../sequences/vector.bool/max_size.pass.cpp   | 106 ++++++++++++++++++
 .../vector/vector.capacity/max_size.pass.cpp  |  53 +++++++--
 libcxx/test/support/sized_allocator.h         |  58 ++++++++++
 4 files changed, 212 insertions(+), 11 deletions(-)
 create mode 100644 libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
 create mode 100644 libcxx/test/support/sized_allocator.h

diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 8d9257eddfcd2d..8cb6d13cf29367 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -532,10 +532,8 @@ template <class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::size_type
 vector<bool, _Allocator>::max_size() const _NOEXCEPT {
   size_type __amax = __storage_traits::max_size(__alloc_);
-  size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always
-  if (__nmax / __bits_per_word <= __amax)
-    return __nmax;
-  return __internal_cap_to_external(__amax);
+  size_type __nmax = numeric_limits<difference_type>::max();
+  return __nmax / __bits_per_word <= __amax ? __nmax : __internal_cap_to_external(__amax);
 }
 
 //  Precondition:  __new_size > capacity()
diff --git a/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
new file mode 100644
index 00000000000000..72ababbb21caea
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// size_type max_size() const;
+
+#include <algorithm>
+#include <cassert>
+#include <climits>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "min_allocator.h"
+#include "sized_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+#if TEST_STD_VER >= 11
+
+template <typename Alloc>
+TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) {
+  using Vector              = std::vector<bool, Alloc>;
+  using size_type           = typename Vector::size_type;
+  using difference_type     = typename Vector::difference_type;
+  using storage_type        = typename Vector::__storage_type;
+  using storage_alloc       = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
+  using storage_traits      = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>;
+  const size_type max_dist  = static_cast<size_type>(std::numeric_limits<difference_type>::max());
+  const size_type max_alloc = storage_traits::max_size(storage_alloc(v.get_allocator()));
+  std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
+  const size_type max_size  = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word;
+  assert(v.max_size() <= max_dist);
+  assert(v.max_size() / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow
+  LIBCPP_ASSERT(v.max_size() == max_size);
+}
+
+#endif
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+  // Test with limited_allocator where v.max_size() is determined by allocator::max_size()
+  {
+    using Alloc        = limited_allocator<bool, 10>;
+    using Vector       = std::vector<bool, Alloc>;
+    using storage_type = Vector::__storage_type;
+    Vector v;
+    std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
+    assert(v.max_size() <= 10 * bits_per_word);
+    LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
+  }
+  {
+    using Alloc        = limited_allocator<double, 10>;
+    using Vector       = std::vector<bool, Alloc>;
+    using storage_type = Vector::__storage_type;
+    Vector v;
+    std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
+    assert(v.max_size() <= 10 * bits_per_word);
+    LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
+  }
+
+#if TEST_STD_VER >= 11
+
+  // Test with various allocators and diffrent size_type
+  {
+    test(std::vector<bool>());
+    test(std::vector<bool, std::allocator<int> >());
+    test(std::vector<bool, min_allocator<bool> >());
+    test(std::vector<bool, test_allocator<bool> >(test_allocator<bool>(1)));
+    test(std::vector<bool, other_allocator<bool> >(other_allocator<bool>(5)));
+    test(std::vector<bool, sized_allocator<bool, std::uint8_t, std::int8_t> >());
+    test(std::vector<bool, sized_allocator<bool, std::uint16_t, std::int16_t> >());
+    test(std::vector<bool, sized_allocator<bool, std::uint32_t, std::int32_t> >());
+    test(std::vector<bool, sized_allocator<bool, std::uint64_t, std::int64_t> >());
+    test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1)> >());
+  }
+
+  // Test cases to identify incorrect implementations that unconditionally return:
+  //   std::min<size_type>(__nmax, __internal_cap_to_external(__amax))
+  // This can cause overflow in __internal_cap_to_external and lead to incorrect results.
+  {
+    test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 61> >());
+    test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 63> >());
+  }
+
+#endif
+
+  return true;
+}
+
+int main(int, char**) {
+  tests();
+
+#if TEST_STD_VER >= 20
+  static_assert(tests());
+#endif
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
index 33abaa04b12079..b85fbb1d82f350 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
@@ -10,16 +10,36 @@
 
 // size_type max_size() const;
 
+#include <algorithm>
 #include <cassert>
+#include <cstdint>
 #include <limits>
 #include <type_traits>
 #include <vector>
 
+#include "min_allocator.h"
+#include "sized_allocator.h"
 #include "test_allocator.h"
 #include "test_macros.h"
 
+#if TEST_STD_VER >= 11
 
-TEST_CONSTEXPR_CXX20 bool test() {
+template <typename T, typename Alloc>
+TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
+  using Vector              = std::vector<T, Alloc>;
+  using alloc_traits        = typename Vector::__alloc_traits;
+  using size_type           = typename Vector::size_type;
+  using difference_type     = typename Vector::difference_type;
+  const size_type max_dist  = static_cast<size_type>(std::numeric_limits<difference_type>::max());
+  const size_type max_alloc = alloc_traits::max_size(v.get_allocator());
+  assert(v.max_size() <= max_dist);
+  assert(v.max_size() <= max_alloc);
+  LIBCPP_ASSERT(v.max_size() == std::min<size_type>(max_dist, max_alloc));
+}
+
+#endif
+
+TEST_CONSTEXPR_CXX20 bool tests() {
   {
     typedef limited_allocator<int, 10> A;
     typedef std::vector<int, A> C;
@@ -30,29 +50,48 @@ TEST_CONSTEXPR_CXX20 bool test() {
   {
     typedef limited_allocator<int, (std::size_t)-1> A;
     typedef std::vector<int, A> C;
-    const C::size_type max_dist =
-        static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
+    const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
     C c;
     assert(c.max_size() <= max_dist);
     LIBCPP_ASSERT(c.max_size() == max_dist);
   }
   {
     typedef std::vector<char> C;
-    const C::size_type max_dist =
-        static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
+    const C::size_type max_dist = static_cast<C::size_type>(std::numeric_limits<C::difference_type>::max());
     C c;
     assert(c.max_size() <= max_dist);
     assert(c.max_size() <= alloc_max_size(c.get_allocator()));
+    LIBCPP_ASSERT(c.max_size() == std::min(max_dist, alloc_max_size(c.get_allocator())));
+  }
+
+#if TEST_STD_VER >= 11
+
+  // Test with various allocators and diffrent size_type
+  {
+    test(std::vector<int>());
+    test(std::vector<short, std::allocator<short> >());
+    test(std::vector<unsigned, min_allocator<unsigned> >());
+    test(std::vector<char, test_allocator<char> >(test_allocator<char>(1)));
+    test(std::vector<std::size_t, other_allocator<std::size_t> >(other_allocator<std::size_t>(5)));
+    test(std::vector<int, sized_allocator<int, std::uint8_t, std::int8_t> >());
+    test(std::vector<int, sized_allocator<int, std::uint16_t, std::int16_t> >());
+    test(std::vector<int, sized_allocator<int, std::uint32_t, std::int32_t> >());
+    test(std::vector<int, sized_allocator<int, std::uint64_t, std::int64_t> >());
+    test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1)> >());
+    test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 2> >());
+    test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >());
   }
 
+#endif
+
   return true;
 }
 
 int main(int, char**) {
-  test();
+  tests();
 
 #if TEST_STD_VER > 17
-  static_assert(test());
+  static_assert(tests());
 #endif
 
   return 0;
diff --git a/libcxx/test/support/sized_allocator.h b/libcxx/test/support/sized_allocator.h
new file mode 100644
index 00000000000000..a877252e82962c
--- /dev/null
+++ b/libcxx/test/support/sized_allocator.h
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_SUPPORT_SIZED_ALLOCATOR_H
+#define TEST_SUPPORT_SIZED_ALLOCATOR_H
+
+#include <cstddef>
+#include <limits>
+#include <memory>
+#include <new>
+
+#include "test_macros.h"
+
+template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t>
+class sized_allocator {
+  template <typename U, typename Sz, typename Diff>
+  friend class sized_allocator;
+
+public:
+  using value_type                  = T;
+  using size_type                   = SIZE_TYPE;
+  using difference_type             = DIFF_TYPE;
+  using propagate_on_container_swap = std::true_type;
+
+  TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
+
+  template <typename U, typename Sz, typename Diff>
+  TEST_CONSTEXPR_CXX20 sized_allocator(const sized_allocator<U, Sz, Diff>& a) TEST_NOEXCEPT : data_(a.data_) {}
+
+  TEST_CONSTEXPR_CXX20 T* allocate(size_type n) {
+    if (n > max_size())
+      TEST_THROW(std::bad_array_new_length());
+    return std::allocator<T>().allocate(n);
+  }
+
+  TEST_CONSTEXPR_CXX20 void deallocate(T* p, size_type n) TEST_NOEXCEPT { std::allocator<T>().deallocate(p, n); }
+
+  TEST_CONSTEXPR size_type max_size() const TEST_NOEXCEPT {
+    return std::numeric_limits<size_type>::max() / sizeof(value_type);
+  }
+
+private:
+  int data_;
+
+  TEST_CONSTEXPR friend bool operator==(const sized_allocator& a, const sized_allocator& b) {
+    return a.data_ == b.data_;
+  }
+  TEST_CONSTEXPR friend bool operator!=(const sized_allocator& a, const sized_allocator& b) {
+    return a.data_ != b.data_;
+  }
+};
+
+#endif

>From 15989776859ec716ad48b905fc651bf6d26a6298 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Wed, 29 Jan 2025 18:37:54 -0500
Subject: [PATCH 2/2] Make tests portable

---
 .../sequences/vector.bool/max_size.pass.cpp   | 47 +++++++++----------
 .../vector/vector.capacity/max_size.pass.cpp  |  6 +--
 libcxx/test/support/sized_allocator.h         | 10 ++--
 3 files changed, 32 insertions(+), 31 deletions(-)

diff --git a/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
index 72ababbb21caea..86a163e0381ae4 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 // <vector>
+// vector<bool>
 
 // size_type max_size() const;
 
@@ -28,47 +29,46 @@
 
 template <typename Alloc>
 TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) {
-  using Vector              = std::vector<bool, Alloc>;
-  using size_type           = typename Vector::size_type;
-  using difference_type     = typename Vector::difference_type;
+  using Vector             = std::vector<bool, Alloc>;
+  using size_type          = typename Vector::size_type;
+  using difference_type    = typename Vector::difference_type;
+  const size_type max_dist = static_cast<size_type>(std::numeric_limits<difference_type>::max());
+  assert(v.max_size() <= max_dist);
+
+  // The following check is specific to libc++ implementation details and is not portable to libstdc++
+  // and MSVC STL, as they use different types for the underlying word storage.
+#  if defined(_LIBCPP_VERSION)
   using storage_type        = typename Vector::__storage_type;
   using storage_alloc       = typename std::allocator_traits<Alloc>::template rebind_alloc<storage_type>;
   using storage_traits      = typename std::allocator_traits<Alloc>::template rebind_traits<storage_type>;
-  const size_type max_dist  = static_cast<size_type>(std::numeric_limits<difference_type>::max());
   const size_type max_alloc = storage_traits::max_size(storage_alloc(v.get_allocator()));
   std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
   const size_type max_size  = max_dist / bits_per_word < max_alloc ? max_dist : max_alloc * bits_per_word;
-  assert(v.max_size() <= max_dist);
   assert(v.max_size() / bits_per_word <= max_alloc); // max_alloc * bits_per_word may overflow
-  LIBCPP_ASSERT(v.max_size() == max_size);
+  assert(v.max_size() == max_size);
+#  endif // defined(_LIBCPP_VERSION)
 }
 
-#endif
+#endif // TEST_STD_VER >= 11
 
 TEST_CONSTEXPR_CXX20 bool tests() {
-  // Test with limited_allocator where v.max_size() is determined by allocator::max_size()
+  // The following check is specific to libc++ implementation details and is not portable to libstdc++
+  // and MSVC STL, as they use different types for the underlying word storage.
+#if defined(_LIBCPP_VERSION)
+  // Test cases where v.max_size() is determined by allocator::max_size()
   {
     using Alloc        = limited_allocator<bool, 10>;
     using Vector       = std::vector<bool, Alloc>;
     using storage_type = Vector::__storage_type;
     Vector v;
     std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
-    assert(v.max_size() <= 10 * bits_per_word);
-    LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
-  }
-  {
-    using Alloc        = limited_allocator<double, 10>;
-    using Vector       = std::vector<bool, Alloc>;
-    using storage_type = Vector::__storage_type;
-    Vector v;
-    std::size_t bits_per_word = sizeof(storage_type) * CHAR_BIT;
-    assert(v.max_size() <= 10 * bits_per_word);
-    LIBCPP_ASSERT(v.max_size() == 10 * bits_per_word);
+    assert(v.max_size() == 10 * bits_per_word);
   }
+#endif // defined(_LIBCPP_VERSION)
 
 #if TEST_STD_VER >= 11
 
-  // Test with various allocators and diffrent size_type
+  // Test with various allocators and different `size_type`s
   {
     test(std::vector<bool>());
     test(std::vector<bool, std::allocator<int> >());
@@ -82,15 +82,14 @@ TEST_CONSTEXPR_CXX20 bool tests() {
     test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1)> >());
   }
 
-  // Test cases to identify incorrect implementations that unconditionally return:
-  //   std::min<size_type>(__nmax, __internal_cap_to_external(__amax))
-  // This can cause overflow in __internal_cap_to_external and lead to incorrect results.
+  // Test cases to identify incorrect implementations that unconditionally compute an internal-to-external
+  // capacity in a way that can overflow, leading to incorrect results.
   {
     test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 61> >());
     test(std::vector<bool, limited_allocator<bool, static_cast<std::size_t>(-1) / 63> >());
   }
 
-#endif
+#endif // TEST_STD_VER >= 11
 
   return true;
 }
diff --git a/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
index b85fbb1d82f350..a3f44f33028e42 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.capacity/max_size.pass.cpp
@@ -27,7 +27,7 @@
 template <typename T, typename Alloc>
 TEST_CONSTEXPR_CXX20 void test(const std::vector<T, Alloc>& v) {
   using Vector              = std::vector<T, Alloc>;
-  using alloc_traits        = typename Vector::__alloc_traits;
+  using alloc_traits        = std::allocator_traits<typename Vector::allocator_type>;
   using size_type           = typename Vector::size_type;
   using difference_type     = typename Vector::difference_type;
   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) {
   LIBCPP_ASSERT(v.max_size() == std::min<size_type>(max_dist, max_alloc));
 }
 
-#endif
+#endif // TEST_STD_VER >= 11
 
 TEST_CONSTEXPR_CXX20 bool tests() {
   {
@@ -82,7 +82,7 @@ TEST_CONSTEXPR_CXX20 bool tests() {
     test(std::vector<int, limited_allocator<int, static_cast<std::size_t>(-1) / 4> >());
   }
 
-#endif
+#endif // TEST_STD_VER >= 11
 
   return true;
 }
diff --git a/libcxx/test/support/sized_allocator.h b/libcxx/test/support/sized_allocator.h
index a877252e82962c..8d52f5bf252c74 100644
--- a/libcxx/test/support/sized_allocator.h
+++ b/libcxx/test/support/sized_allocator.h
@@ -16,15 +16,17 @@
 
 #include "test_macros.h"
 
-template <typename T, typename SIZE_TYPE = std::size_t, typename DIFF_TYPE = std::ptrdiff_t>
+// Allocator with a provided size_type and difference_type, used to test corner cases
+// like arithmetic on Allocator::size_type in generic code.
+template <typename T, typename Size = std::size_t, typename Difference = std::ptrdiff_t>
 class sized_allocator {
   template <typename U, typename Sz, typename Diff>
   friend class sized_allocator;
 
 public:
   using value_type                  = T;
-  using size_type                   = SIZE_TYPE;
-  using difference_type             = DIFF_TYPE;
+  using size_type                   = Size;
+  using difference_type             = Difference;
   using propagate_on_container_swap = std::true_type;
 
   TEST_CONSTEXPR_CXX20 explicit sized_allocator(int d = 0) : data_(d) {}
@@ -55,4 +57,4 @@ class sized_allocator {
   }
 };
 
-#endif
+#endif // TEST_SUPPORT_SIZED_ALLOCATOR_H



More information about the libcxx-commits mailing list