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_view> 22 #include <string> 23 #include <cassert> 24 #include <type_traits> 25 26 #include "test_macros.h" 27 28 using std::string_view; 29 30 template <class SV> 31 void 32 test() 33 { 34 typedef std::hash<SV> H; 35 static_assert((std::is_same<typename H::argument_type, SV>::value), "" ); 36 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 37 38 typedef typename SV::value_type char_type; 39 typedef std::basic_string<char_type> String; 40 typedef std::hash<String> SH; 41 ASSERT_NOEXCEPT(H()(SV())); 42 43 char_type g1 [ 10 ]; 44 char_type g2 [ 10 ]; 45 for ( int i = 0; i < 10; ++i ) 46 g1[i] = g2[9-i] = static_cast<char_type>('0' + i); 47 H h; 48 SH sh; 49 SV s1(g1, 10); 50 String ss1(s1); 51 SV s2(g2, 10); 52 String ss2(s2); 53 assert(h(s1) == h(s1)); 54 assert(h(s1) != h(s2)); 55 assert(sh(ss1) == h(s1)); 56 assert(sh(ss2) == h(s2)); 57 } 58 59 int main() 60 { 61 test<std::string_view>(); 62 #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS 63 test<std::u16string_view>(); 64 test<std::u32string_view>(); 65 #endif // _LIBCPP_HAS_NO_UNICODE_CHARS 66 test<std::wstring_view>(); 67 } 68