blob: c2a2ef95647243a678da395f95275a1eb916aa8a [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant3e519522010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9// <functional>
10
11// template <class T>
12// struct hash
13// : public unary_function<T, size_t>
14// {
15// size_t operator()(T val) const;
16// };
17
18// Not very portable
19
20#include <string>
21#include <cassert>
22#include <type_traits>
23
Marshall Clow7c803382017-03-23 02:40:28 +000024#include "test_macros.h"
25
Howard Hinnant3e519522010-05-11 19:42:16 +000026template <class T>
27void
28test()
29{
30 typedef std::hash<T> H;
Marshall Clowd95510e2015-01-07 21:53:23 +000031 static_assert((std::is_same<typename H::argument_type, T>::value), "" );
32 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
Marshall Clow7c803382017-03-23 02:40:28 +000033 ASSERT_NOEXCEPT(H()(T()));
34
Howard Hinnant3e519522010-05-11 19:42:16 +000035 H h;
36 std::string g1 = "1234567890";
37 std::string g2 = "1234567891";
38 T s1(g1.begin(), g1.end());
39 T s2(g2.begin(), g2.end());
40 assert(h(s1) != h(s2));
41}
42
JF Bastien2df59c52019-02-04 20:31:13 +000043int main(int, char**)
Howard Hinnant3e519522010-05-11 19:42:16 +000044{
45 test<std::string>();
Marshall Clow7dad0bd2018-12-11 04:35:44 +000046#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
47 test<std::u8string>();
48#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000049#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
50 test<std::u16string>();
51 test<std::u32string>();
Howard Hinnantbf2897c2010-08-22 00:47:54 +000052#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnant3e519522010-05-11 19:42:16 +000053 test<std::wstring>();
JF Bastien2df59c52019-02-04 20:31:13 +000054
55 return 0;
Howard Hinnant3e519522010-05-11 19:42:16 +000056}