• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // <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 
24 #include "test_macros.h"
25 
26 template <class T>
27 void
test()28 test()
29 {
30     typedef std::hash<T> H;
31     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), "" );
33     ASSERT_NOEXCEPT(H()(T()));
34 
35     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 
main(int,char **)43 int main(int, char**)
44 {
45     test<std::string>();
46 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
47     test<std::u8string>();
48 #endif
49 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
50     test<std::u16string>();
51     test<std::u32string>();
52 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
53     test<std::wstring>();
54 
55   return 0;
56 }
57