[libcxx-commits] [libcxx] [libc++][test] Make tests for constexpr `hash<vector<bool>>` libc++-specific (PR #132617)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 23 08:48:43 PDT 2025


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/132617

libc++ makes the `hash<vector<bool, A>>::operator()` `constexpr` since C++20, which is a conforming extension. This patch move the test cases for `constexpr` along with the assumption of hash values to the `libcxx/test/libcxx/` subdirectory.

>From 7db09d6e776ee3ed07b9dca5d321391e95c780fb Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Sun, 23 Mar 2025 23:31:21 +0800
Subject: [PATCH] [libc++][test ] Make tests for constexpr `hash`
 libcxx-specific

libc++ makes the `hash<vector<bool, A>>::operator()` `constexpr` since
C++20, which is a conforming extension. This patch move the cases to
the `libcxx/test/libcxx/` subdirectory.
---
 .../vector.bool/hash.constexpr.pass.cpp       | 53 +++++++++++++++++++
 .../vector.bool/enabled_hash.pass.cpp         |  7 +--
 .../vector.bool/vector_bool.pass.cpp          | 10 ++--
 3 files changed, 60 insertions(+), 10 deletions(-)
 create mode 100644 libcxx/test/libcxx/containers/sequences/vector.bool/hash.constexpr.pass.cpp

diff --git a/libcxx/test/libcxx/containers/sequences/vector.bool/hash.constexpr.pass.cpp b/libcxx/test/libcxx/containers/sequences/vector.bool/hash.constexpr.pass.cpp
new file mode 100644
index 0000000000000..49874d0504e8a
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/vector.bool/hash.constexpr.pass.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++20
+
+// <vector>
+
+// template<class Allocator> struct hash<vector<bool, Allocator>>;
+
+// libc++ makes the operator() of this partial specialization constexpr since C++20, which is a conforming extension.
+
+#include <cassert>
+#include <vector>
+
+#include "min_allocator.h"
+#include "poisoned_hash_helper.h"
+
+constexpr bool test() {
+  {
+    using VB = std::vector<bool>;
+    bool ba[]{true, false, true, true, false};
+    VB vb(std::begin(ba), std::end(ba));
+
+    const std::hash<VB> h{};
+    const auto hash_value = h(vb);
+    assert(hash_value == h(vb));
+    assert(hash_value != 0);
+  }
+  {
+    using VB  = std::vector<bool, min_allocator<bool>>;
+    bool ba[] = {true, false, true, true, false};
+    VB vb(std::begin(ba), std::end(ba));
+
+    const std::hash<VB> h{};
+    const auto hash_value = h(vb);
+    assert(hash_value == h(vb));
+    assert(hash_value != 0);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp
index 41cedd68fe50e..cba3101ef5009 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp
@@ -19,19 +19,14 @@
 #include "test_macros.h"
 #include "min_allocator.h"
 
-TEST_CONSTEXPR_CXX20 bool test() {
+void test() {
   test_hash_enabled<std::vector<bool> >();
   test_hash_enabled<std::vector<bool, min_allocator<bool>>>();
-
-  return true;
 }
 
 int main(int, char**) {
   test_library_hash_specializations_available();
   test();
-#if TEST_STD_VER > 17
-  static_assert(test());
-#endif
 
   return 0;
 }
diff --git a/libcxx/test/std/containers/sequences/vector.bool/vector_bool.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/vector_bool.pass.cpp
index e270869a8320f..f6bdb7fd5b3ef 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/vector_bool.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/vector_bool.pass.cpp
@@ -14,8 +14,6 @@
 //     size_t operator()(T val) const;
 // };
 
-// Not very portable
-
 #include <vector>
 #include <cassert>
 #include <iterator>
@@ -37,7 +35,9 @@ TEST_CONSTEXPR_CXX20 bool tests() {
     bool ba[] = {true, false, true, true, false};
     T vb(std::begin(ba), std::end(ba));
     H h;
-    assert(h(vb) != 0);
+    if (!TEST_IS_CONSTANT_EVALUATED) {
+      assert(h(vb) == h(vb));
+    }
   }
 #if TEST_STD_VER >= 11
   {
@@ -51,7 +51,9 @@ TEST_CONSTEXPR_CXX20 bool tests() {
     bool ba[] = {true, false, true, true, false};
     T vb(std::begin(ba), std::end(ba));
     H h;
-    assert(h(vb) != 0);
+    if (!TEST_IS_CONSTANT_EVALUATED) {
+      assert(h(vb) == h(vb));
+    }
   }
 #endif
 



More information about the libcxx-commits mailing list