[libcxx-commits] [libcxx] [libc++] Simplify vector<bool>::flip() and add new tests (PR #119607)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 17 09:29:16 PST 2024
================
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 flip();
+
+#include <cassert>
+#include <memory>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+template <typename Allocator = std::allocator<bool> >
+TEST_CONSTEXPR_CXX20 void test_vector_flip(std::size_t n, Allocator a = Allocator()) {
+ std::vector<bool, Allocator > v(n, false, a);
+ for (std::size_t i = 0; i < n; ++i)
+ v[i] = i & 1;
+ std::vector<bool, Allocator > original = v;
+ v.flip();
+ for (size_t i = 0; i < n; ++i)
+ assert(v[i] == !original[i]);
+ v.flip();
+ assert(v == original);
+}
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+ // Test small vectors with different allocators
+ test_vector_flip(3);
+ test_vector_flip(3, min_allocator<bool>());
+ test_vector_flip(3, test_allocator<bool>(5));
+
+ // Test large vectors with different allocators
+ test_vector_flip(1000);
----------------
philnik777 wrote:
```suggestion
test_vector_flip(1000, std::allocator<bool>());
```
https://github.com/llvm/llvm-project/pull/119607
More information about the libcxx-commits
mailing list