Skip to content

Commit 7db09d6

Browse files
[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.
1 parent 2f1416b commit 7db09d6

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++20
10+
11+
// <vector>
12+
13+
// template<class Allocator> struct hash<vector<bool, Allocator>>;
14+
15+
// libc++ makes the operator() of this partial specialization constexpr since C++20, which is a conforming extension.
16+
17+
#include <cassert>
18+
#include <vector>
19+
20+
#include "min_allocator.h"
21+
#include "poisoned_hash_helper.h"
22+
23+
constexpr bool test() {
24+
{
25+
using VB = std::vector<bool>;
26+
bool ba[]{true, false, true, true, false};
27+
VB vb(std::begin(ba), std::end(ba));
28+
29+
const std::hash<VB> h{};
30+
const auto hash_value = h(vb);
31+
assert(hash_value == h(vb));
32+
assert(hash_value != 0);
33+
}
34+
{
35+
using VB = std::vector<bool, min_allocator<bool>>;
36+
bool ba[] = {true, false, true, true, false};
37+
VB vb(std::begin(ba), std::end(ba));
38+
39+
const std::hash<VB> h{};
40+
const auto hash_value = h(vb);
41+
assert(hash_value == h(vb));
42+
assert(hash_value != 0);
43+
}
44+
45+
return true;
46+
}
47+
48+
int main(int, char**) {
49+
test();
50+
static_assert(test());
51+
52+
return 0;
53+
}

libcxx/test/std/containers/sequences/vector.bool/enabled_hash.pass.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@
1919
#include "test_macros.h"
2020
#include "min_allocator.h"
2121

22-
TEST_CONSTEXPR_CXX20 bool test() {
22+
void test() {
2323
test_hash_enabled<std::vector<bool> >();
2424
test_hash_enabled<std::vector<bool, min_allocator<bool>>>();
25-
26-
return true;
2725
}
2826

2927
int main(int, char**) {
3028
test_library_hash_specializations_available();
3129
test();
32-
#if TEST_STD_VER > 17
33-
static_assert(test());
34-
#endif
3530

3631
return 0;
3732
}

libcxx/test/std/containers/sequences/vector.bool/vector_bool.pass.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// size_t operator()(T val) const;
1515
// };
1616

17-
// Not very portable
18-
1917
#include <vector>
2018
#include <cassert>
2119
#include <iterator>
@@ -37,7 +35,9 @@ TEST_CONSTEXPR_CXX20 bool tests() {
3735
bool ba[] = {true, false, true, true, false};
3836
T vb(std::begin(ba), std::end(ba));
3937
H h;
40-
assert(h(vb) != 0);
38+
if (!TEST_IS_CONSTANT_EVALUATED) {
39+
assert(h(vb) == h(vb));
40+
}
4141
}
4242
#if TEST_STD_VER >= 11
4343
{
@@ -51,7 +51,9 @@ TEST_CONSTEXPR_CXX20 bool tests() {
5151
bool ba[] = {true, false, true, true, false};
5252
T vb(std::begin(ba), std::end(ba));
5353
H h;
54-
assert(h(vb) != 0);
54+
if (!TEST_IS_CONSTANT_EVALUATED) {
55+
assert(h(vb) == h(vb));
56+
}
5557
}
5658
#endif
5759

0 commit comments

Comments
 (0)