Skip to content

Commit 23d43e6

Browse files
committed
[libc++] Add regression test for std::hash implementation in ABI v1
Differential Revision: https://reviews.llvm.org/D144107
1 parent c6c41c3 commit 23d43e6

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// UNSUPPORTED: c++03
10+
11+
// Test that the CityHash implementation returns the results we expect.
12+
//
13+
// Note that this implementation is technically incorrect, however changing it is
14+
// an ABI break. This test ensures that we don't unintentionally break the ABI v1
15+
// by "fixing" the hash implementation.
16+
// REQUIRES: libcpp-abi-version=1
17+
18+
#include <cassert>
19+
#include <string>
20+
#include <utility>
21+
22+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
23+
# define CHOOSE_BY_ENDIANESS(little, big) (little)
24+
#else
25+
# define CHOOSE_BY_ENDIANESS(little, big) (big)
26+
#endif
27+
28+
std::string CityHash[] = {
29+
{/* "abcdefgh" */ "\x61\x62\x63\x64\x65\x66\x67\x68"},
30+
{/* "abcDefgh" */ "\x61\x62\x63\x44\x65\x66\x67\x68"},
31+
{/* "CityHash" */ "\x43\x69\x74\x79\x48\x61\x73\x68"},
32+
{/* "CitYHash" */ "\x43\x69\x74\x59\x48\x61\x73\x68"},
33+
};
34+
35+
int main(int, char**) {
36+
const std::pair<std::string, uint64_t> TestCases[] = {
37+
{CityHash[0], CHOOSE_BY_ENDIANESS(0x87c69099911bab7eULL, 0x297621d7fa436a3ULL)},
38+
{CityHash[1], CHOOSE_BY_ENDIANESS(0x87c69099911bab7eULL, 0xb17be531dde56e57ULL)},
39+
{CityHash[2], CHOOSE_BY_ENDIANESS(0x85322632e188694aULL, 0xe14f578b688e266dULL)},
40+
{CityHash[3], CHOOSE_BY_ENDIANESS(0x85322632e188694aULL, 0xca5a764a0450eac6ULL)},
41+
};
42+
43+
std::__murmur2_or_cityhash<uint64_t> h64;
44+
for (const auto& test_case : TestCases) {
45+
assert(h64(test_case.first.data(), test_case.first.size()) == test_case.second);
46+
}
47+
48+
return 0;
49+
}

libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.pass.cpp renamed to libcxx/test/libcxx/utilities/utility/__murmur2_or_cityhash.abi-v2.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// Test the CityHash implementation is correct.
10-
119
// UNSUPPORTED: c++03
1210

11+
// Test that the CityHash implementation is correct.
12+
//
1313
// In ABI v1, our CityHash implementation is incorrect and fixing it would
1414
// be an ABI break.
1515
// REQUIRES: libcpp-abi-version=2

0 commit comments

Comments
 (0)