[libcxx-commits] [libcxx] [libc++][test] Improve tests for assign in std::vector (PR #119163)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 9 10:05:24 PST 2024


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

>From 0a51f3f40b26c147d0e4e93a7fcb4a13977ab900 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sun, 8 Dec 2024 22:54:51 -0500
Subject: [PATCH 1/2] Improve tests for assign in std::vector

---
 .../vector.cons/assign_iter_iter.pass.cpp     | 107 ++++++++++++++++--
 1 file changed, 100 insertions(+), 7 deletions(-)

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 9b52885b9bf8d2..584f641edd7c52 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
@@ -18,17 +18,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;
@@ -43,8 +42,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;
@@ -63,6 +62,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 inputs_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

>From 6e84df268b7394c888948dd54d1097c29d912774 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Mon, 9 Dec 2024 13:01:20 -0500
Subject: [PATCH 2/2] Add test coverage for vector<bool>::assign

---
 .../vector.bool/assign_iter_iter.pass.cpp     | 87 +++++++++++++++++++
 .../vector.bool/assign_size_value.pass.cpp    | 57 ++++++++++++
 .../vector.cons/assign_iter_iter.pass.cpp     |  5 +-
 3 files changed, 147 insertions(+), 2 deletions(-)
 create mode 100644 libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp
 create mode 100644 libcxx/test/std/containers/sequences/vector.bool/assign_size_value.pass.cpp

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..fe4481d6e07c2e
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/assign_iter_iter.pass.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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(_InputIterator __first, _InputIterator __last);
+// void assign(_ForwardIterator __first, _ForwardIterator __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());
+      v.assign(forward_iterator(in.begin()), forward_iterator(in.end()));
+      assert(v == in);
+    }
+    { // No reallocation: fit wintin current size
+      bool in[]                    = {false, true, false, true, true};
+      TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(2 * N, false);
+      v.assign(forward_iterator(in), forward_iterator(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+    { // No reallocation: fit wintin spare space
+      bool in[]                    = {false, true, false, true, true};
+      TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(N / 2, false);
+      v.reserve(N * 2);
+      v.assign(forward_iterator(in), forward_iterator(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());
+      v.assign(cpp17_input_iterator(in.begin()), cpp17_input_iterator(in.end()));
+      assert(v == in);
+    }
+    { // No reallocation: fit wintin current size
+      bool in[]                    = {false, true, false, true, true};
+      TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(2 * N, false);
+      v.assign(cpp17_input_iterator(in), cpp17_input_iterator(in + N));
+      assert(v.size() == N);
+      for (std::size_t i = 0; i < N; ++i)
+        assert(v[i] == in[i]);
+    }
+    { // No reallocation: fit wintin spare space
+      bool in[]                    = {false, true, false, true, true};
+      TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
+      std::vector<bool> v(N / 2, false);
+      v.reserve(N * 2);
+      v.assign(cpp17_input_iterator(in), cpp17_input_iterator(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;
+}
\ No newline at end of file
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..7b889d0dcc777b
--- /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
+      TEST_CONSTEXPR 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 wintin current size
+      TEST_CONSTEXPR 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 wintin spare space
+      TEST_CONSTEXPR 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;
+}
\ No newline at end of file
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 584f641edd7c52..5a694b23b7922e 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
@@ -8,7 +8,8 @@
 
 // <vector>
 
-// void assign(size_type n, const_reference v);
+// void assign(_InputIterator __first, _InputIterator __last);
+// void assign(_ForwardIterator __first, _ForwardIterator __last);
 
 #include <vector>
 #include <algorithm>
@@ -134,7 +135,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
       assert(v[2].value == 42);
     }
   }
-  { // Test with size() < new_size < capacity() for inputs_iterator, resulting in construction at end during assign
+  { // 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*>;
     {



More information about the libcxx-commits mailing list