[libcxx-commits] [libcxx] [libc++][test] Fix zero-length arrays and copy-pasted lambdas in `ranges.contains.pass.cpp` (PR #79792)

Stephan T. Lavavej via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jan 28 23:30:04 PST 2024


https://github.com/StephanTLavavej updated https://github.com/llvm/llvm-project/pull/79792

>From 168918e36164372a9d17d87f4753c9de86cb3def Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sun, 28 Jan 2024 21:59:46 -0800
Subject: [PATCH 1/3] Fix MSVC error C2466: cannot allocate an array of
 constant size 0

Previous fixes: LLVM-74183
---
 .../alg.nonmodifying/alg.contains/ranges.contains.pass.cpp   | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
index c928698e453013f..c87dc8f9bf4b463 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
@@ -19,6 +19,7 @@
 //     constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {});                 // since C++23
 
 #include <algorithm>
+#include <array>
 #include <cassert>
 #include <list>
 #include <ranges>
@@ -89,8 +90,8 @@ constexpr void test_iterators() {
   }
 
   { // check that an empty range works
-    ValueT a[] = {};
-    auto whole = std::ranges::subrange(Iter(a), Sent(Iter(a)));
+    std::array<ValueT, 0> a = {};
+    auto whole = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data())));
     {
       bool ret = std::ranges::contains(whole.begin(), whole.end(), 1);
       assert(!ret);

>From f3c60eddf687df9b69905c40d32f57b9ccfa2fda Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sun, 28 Jan 2024 22:43:14 -0800
Subject: [PATCH 2/3] Fix MSVC warning C4805: '==': unsafe mix of type 'int'
 and type 'const bool' in operation

AFAICT, these lambdas were copy-pasted, and didn't intend to take and return int here. This part of the test is using `vector<bool>` for random-access but non-contiguous iterators,
and it's checking how many times the projection is invoked, but the projection doesn't need to do anything squirrely, it should otherwise be an identity.

Also, fix typos: "continuous" => "contiguous".
---
 .../alg.contains/ranges.contains.pass.cpp            | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
index c87dc8f9bf4b463..7b792bb3ac80c61 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
@@ -165,7 +165,7 @@ constexpr bool test() {
     });
   });
 
-  { // count invocations of the projection for continuous iterators
+  { // count invocations of the projection for contiguous iterators
     int a[]              = {1, 9, 0, 13, 25};
     int projection_count = 0;
     {
@@ -216,22 +216,22 @@ constexpr bool test() {
     }
   }
 
-  { // check invocations of the projection for non-continuous iterators
+  { // check invocations of the projection for non-contiguous iterators
     std::vector<bool> whole{false, false, true, false};
     int projection_count = 0;
     {
-      bool ret = std::ranges::contains(whole.begin(), whole.end(), true, [&](int i) {
+      bool ret = std::ranges::contains(whole.begin(), whole.end(), true, [&](bool b) {
         ++projection_count;
-        return i;
+        return b;
       });
       assert(ret);
       assert(projection_count == 3);
       projection_count = 0;
     }
     {
-      bool ret = std::ranges::contains(whole, true, [&](int i) {
+      bool ret = std::ranges::contains(whole, true, [&](bool b) {
         ++projection_count;
-        return i;
+        return b;
       });
       assert(ret);
       assert(projection_count == 3);

>From 604eb5dfaffa365d9b68ab79f13116a9fd69b26d Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at nuwen.net>
Date: Sun, 28 Jan 2024 23:29:36 -0800
Subject: [PATCH 3/3] clang-format.

---
 .../alg.nonmodifying/alg.contains/ranges.contains.pass.cpp      | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
index 7b792bb3ac80c61..f710ca2c319dd6a 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.contains/ranges.contains.pass.cpp
@@ -91,7 +91,7 @@ constexpr void test_iterators() {
 
   { // check that an empty range works
     std::array<ValueT, 0> a = {};
-    auto whole = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data())));
+    auto whole              = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data())));
     {
       bool ret = std::ranges::contains(whole.begin(), whole.end(), 1);
       assert(!ret);



More information about the libcxx-commits mailing list