[libcxx-commits] [libcxx] 4039a79 - [libc++][test] Improve tests for assign in std::vector and vector<bool> (#119163)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 19 08:19:30 PST 2024


Author: Peng Liu
Date: 2024-12-19T11:19:25-05:00
New Revision: 4039a79de71bd969ef5bf944fd9f46430338ff7e

URL: https://github.com/llvm/llvm-project/commit/4039a79de71bd969ef5bf944fd9f46430338ff7e
DIFF: https://github.com/llvm/llvm-project/commit/4039a79de71bd969ef5bf944fd9f46430338ff7e.diff

LOG: [libc++][test] Improve tests for assign in std::vector and vector<bool> (#119163)

This PR enhances the test coverage for std::vector::assign by adding new
tests for several important test cases that were previously missing, as
shown in the following table:

| test cases                        | forward_iterator | input_iterator |
|-----------------------------------|------------------|----------------|
| new_size > capacity()             | Yes              | Yes            |
| size() < new_size <= capacity()   | No               | No             |
| new_size <= size()                | No               | No             |

Similarly, no tests have previously covered `assign(InputIterator, InputIterator)`
and `assign(size_type, const value_type&)` for `vector<bool>`.

With this patch applied, all missing tests are covered.

Added: 
    libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp
    libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp

Modified: 
    libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp
new file mode 100644
index 00000000000000..91788b3707592a
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp
@@ -0,0 +1,93 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template <class InputIt>
+// constexpr void assign(InputIt first, InputIt last);
+
+#include <vector>
+#include <cassert>
+#include "test_macros.h"
+#include "test_iterators.h"
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+  {   // Test with various cases where assign may or may not trigger reallocations for forward_iterator
+    { // Reallocation happens
+      std::vector<bool> in(128, true);
+      std::vector<bool> v(5, false);
+      assert(v.capacity() < in.size());
+      using It = forward_iterator<std::vector<bool>::iterator>;
+      v.assign(It(in.begin()), It(in.end()));
+      assert(v == in);
+    }
+    { // No reallocation: fit within current size
+      bool in[]     = {false, true, false, true, true};
+      std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(2 * N, false);
+      using It = forward_iterator<bool*>;
+      v.assign(It(in), It(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+    { // No reallocation: fit within spare space
+      bool in[]     = {false, true, false, true, true};
+      std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(N / 2, false);
+      v.reserve(N * 2);
+      using It = forward_iterator<bool*>;
+      v.assign(It(in), It(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+  }
+
+  {   // Test with various cases where assign may or may not trigger reallocations for input_iterator
+    { // Reallocation happens
+      std::vector<bool> in(128, true);
+      std::vector<bool> v(5, false);
+      assert(v.capacity() < in.size());
+      using It = cpp17_input_iterator<std::vector<bool>::iterator>;
+      v.assign(It(in.begin()), It(in.end()));
+      assert(v == in);
+    }
+    { // No reallocation: fit within current size
+      bool in[]     = {false, true, false, true, true};
+      std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(2 * N, false);
+      using It = cpp17_input_iterator<bool*>;
+      v.assign(It(in), It(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+    { // No reallocation: fit within spare space
+      bool in[]     = {false, true, false, true, true};
+      std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(N / 2, false);
+      v.reserve(N * 2);
+      using It = cpp17_input_iterator<bool*>;
+      v.assign(It(in), It(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  tests();
+#if TEST_STD_VER > 17
+  static_assert(tests());
+#endif
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp
new file mode 100644
index 00000000000000..d2513bb84c806b
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp
@@ -0,0 +1,57 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// void assign(size_type n, const value_type& x);
+
+#include <vector>
+#include <cassert>
+#include "test_macros.h"
+#include "test_iterators.h"
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+  {   // Test with various cases where assign may or may not trigger reallocations
+    { // Reallocation happens
+      std::size_t N = 128;
+      std::vector<bool> v(5, false);
+      assert(v.capacity() < N);
+      v.assign(N, true);
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == true);
+    }
+    { // No reallocation: fit within current size
+      std::size_t N = 5;
+      std::vector<bool> v(2 * N, false);
+      v.assign(N, true);
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == true);
+    }
+    { // No reallocation: fit within spare space
+      std::size_t N = 5;
+      std::vector<bool> v(N / 2, false);
+      v.reserve(N * 2);
+      v.assign(N, true);
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == true);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  tests();
+#if TEST_STD_VER > 17
+  static_assert(tests());
+#endif
+  return 0;
+}

diff  --git a/libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
index b6aeba4e4eef3d..91060679cd7f92 100644
--- a/libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
@@ -19,17 +19,16 @@
 #include "asan_testing.h"
 #include "test_iterators.h"
 #if TEST_STD_VER >= 11
-#include "emplace_constructible.h"
-#include "container_test_types.h"
+#  include "emplace_constructible.h"
+#  include "container_test_types.h"
 #endif
 
-
 TEST_CONSTEXPR_CXX20 bool test() {
 #if TEST_STD_VER >= 11
   int arr1[] = {42};
   int arr2[] = {1, 101, 42};
-  {
-    using T = EmplaceConstructibleMoveableAndAssignable<int>;
+  { // Test with new_size > capacity() == 0 for forward_iterator, resulting in reallocation during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
     using It = forward_iterator<int*>;
     {
       std::vector<T> v;
@@ -44,8 +43,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
       assert(v[2].value == 42);
     }
   }
-  {
-    using T = EmplaceConstructibleMoveableAndAssignable<int>;
+  { // Test with new_size > capacity() == 0 for input_iterator, resulting in reallocation during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
     using It = cpp17_input_iterator<int*>;
     {
       std::vector<T> v;
@@ -64,6 +63,100 @@ TEST_CONSTEXPR_CXX20 bool test() {
       assert(v[2].value == 42);
     }
   }
+
+  { // Test with new_size < size() for forward_iterator, resulting in destruction at end during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
+    using It = forward_iterator<int*>;
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < v.capacity(); ++i)
+        v.emplace_back(99);
+      v.assign(It(arr1), It(std::end(arr1)));
+      assert(v.size() == 1);
+      assert(v[0].value == 42);
+    }
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < v.capacity(); ++i)
+        v.emplace_back(99);
+      v.assign(It(arr2), It(std::end(arr2)));
+      assert(v.size() == 3);
+      assert(v[0].value == 1);
+      assert(v[1].value == 101);
+      assert(v[2].value == 42);
+    }
+  }
+  { // Test with new_size < size() for input_iterator, resulting in destruction at end during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
+    using It = cpp17_input_iterator<int*>;
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < v.capacity(); ++i)
+        v.emplace_back(99);
+      v.assign(It(arr1), It(std::end(arr1)));
+      assert(v.size() == 1);
+      assert(v[0].value == 42);
+    }
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < v.capacity(); ++i)
+        v.emplace_back(99);
+      v.assign(It(arr2), It(std::end(arr2)));
+      assert(v.size() == 3);
+      assert(v[0].value == 1);
+      assert(v[1].value == 101);
+      assert(v[2].value == 42);
+    }
+  }
+
+  { // Test with size() < new_size < capacity() for forward_iterator, resulting in construction at end during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
+    using It = forward_iterator<int*>;
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      v.assign(It(arr1), It(std::end(arr1)));
+      assert(v.size() == 1);
+      assert(v[0].value == 42);
+    }
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < 2; ++i)
+        v.emplace_back(99);
+      v.assign(It(arr2), It(std::end(arr2)));
+      assert(v.size() == 3);
+      assert(v[0].value == 1);
+      assert(v[1].value == 101);
+      assert(v[2].value == 42);
+    }
+  }
+  { // Test with size() < new_size < capacity() for input_iterator, resulting in construction at end during assign
+    using T  = EmplaceConstructibleMoveableAndAssignable<int>;
+    using It = cpp17_input_iterator<int*>;
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      v.assign(It(arr1), It(std::end(arr1)));
+      assert(v.size() == 1);
+      assert(v[0].value == 42);
+    }
+    {
+      std::vector<T> v;
+      v.reserve(5);
+      for (std::size_t i = 0; i < 2; ++i)
+        v.emplace_back(99);
+      v.assign(It(arr2), It(std::end(arr2)));
+      assert(v.size() == 3);
+      assert(v[0].value == 1);
+      assert(v[1].value == 101);
+      assert(v[2].value == 42);
+    }
+  }
 #endif
 
   // Test with a number of elements in the source range that is greater than capacity


        


More information about the libcxx-commits mailing list