blob: 449ad8f1139b0983470280717f2917b4608b3432 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <functional>
11
12// template <class T>
13// struct hash
14// : public unary_function<T, size_t>
15// {
16// size_t operator()(T val) const;
17// };
18
19// Not very portable
20
21#include <string>
22#include <cassert>
23#include <type_traits>
24
Marshall Clow7c803382017-03-23 02:40:28 +000025#include "test_macros.h"
26
Howard Hinnant3e519522010-05-11 19:42:16 +000027template <class T>
28void
29test()
30{
31 typedef std::hash<T> H;
Marshall Clowd95510e2015-01-07 21:53:23 +000032 static_assert((std::is_same<typename H::argument_type, T>::value), "" );
33 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
Marshall Clow7c803382017-03-23 02:40:28 +000034 ASSERT_NOEXCEPT(H()(T()));
35
Howard Hinnant3e519522010-05-11 19:42:16 +000036 H h;
37 std::string g1 = "1234567890";
38 std::string g2 = "1234567891";
39 T s1(g1.begin(), g1.end());
40 T s2(g2.begin(), g2.end());
41 assert(h(s1) != h(s2));
42}
43
44int main()
45{
46 test<std::string>();
Marshall Clow7dad0bd2018-12-11 04:35:44 +000047#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
48 test<std::u8string>();
49#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000050#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
51 test<std::u16string>();
52 test<std::u32string>();
Howard Hinnantbf2897c2010-08-22 00:47:54 +000053#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnant3e519522010-05-11 19:42:16 +000054 test<std::wstring>();
55}