|
| 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 | +} |
0 commit comments