Home | History | Annotate | Download | only in basic.string.hash
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      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 
     25 #include "test_macros.h"
     26 
     27 template <class T>
     28 void
     29 test()
     30 {
     31     typedef std::hash<T> H;
     32     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), "" );
     34     ASSERT_NOEXCEPT(H()(T()));
     35 
     36     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 
     44 int main()
     45 {
     46     test<std::string>();
     47 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
     48     test<std::u8string>();
     49 #endif
     50 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
     51     test<std::u16string>();
     52     test<std::u32string>();
     53 #endif  // _LIBCPP_HAS_NO_UNICODE_CHARS
     54     test<std::wstring>();
     55 }
     56