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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 13 06:41:56 PST 2024


================
@@ -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 InputIter>
+// void assign(InputIter first, InputIter 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 wintin current size
+      bool in[]                    = {false, true, false, true, true};
+      TEST_CONSTEXPR std::size_t N = sizeof(in) / sizeof(in[0]);
----------------
ldionne wrote:

This doesn't need to be constexpr so I'd leave it out. We use `TEST_CONSTEXPR` only when it's really required for the test to work.

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


More information about the libcxx-commits mailing list