1053d81ceSMarshall Clow //===----------------------------------------------------------------------===// 2053d81ceSMarshall Clow // 3053d81ceSMarshall Clow // The LLVM Compiler Infrastructure 4053d81ceSMarshall Clow // 5053d81ceSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open 6053d81ceSMarshall Clow // Source Licenses. See LICENSE.TXT for details. 7053d81ceSMarshall Clow // 8053d81ceSMarshall Clow //===----------------------------------------------------------------------===// 9053d81ceSMarshall Clow 10053d81ceSMarshall Clow // <functional> 11053d81ceSMarshall Clow 12053d81ceSMarshall Clow // template <class T> 13053d81ceSMarshall Clow // struct hash 14053d81ceSMarshall Clow // : public unary_function<T, size_t> 15053d81ceSMarshall Clow // { 16053d81ceSMarshall Clow // size_t operator()(T val) const; 17053d81ceSMarshall Clow // }; 18053d81ceSMarshall Clow 19053d81ceSMarshall Clow // Not very portable 20053d81ceSMarshall Clow 21053d81ceSMarshall Clow #include <string_view> 22*f9127593SEric Fiselier #include <string> 23053d81ceSMarshall Clow #include <cassert> 24053d81ceSMarshall Clow #include <type_traits> 25053d81ceSMarshall Clow 26053d81ceSMarshall Clow using std::string_view; 27053d81ceSMarshall Clow 28*f9127593SEric Fiselier template <class SV> 29053d81ceSMarshall Clow void 30053d81ceSMarshall Clow test() 31053d81ceSMarshall Clow { 32*f9127593SEric Fiselier typedef std::hash<SV> H; 33*f9127593SEric Fiselier static_assert((std::is_same<typename H::argument_type, SV>::value), "" ); 34053d81ceSMarshall Clow static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 35*f9127593SEric Fiselier 36*f9127593SEric Fiselier typedef typename SV::value_type char_type; 37*f9127593SEric Fiselier typedef std::basic_string<char_type> String; 38*f9127593SEric Fiselier typedef std::hash<String> SH; 39*f9127593SEric Fiselier 40053d81ceSMarshall Clow char_type g1 [ 10 ]; 41053d81ceSMarshall Clow char_type g2 [ 10 ]; 42053d81ceSMarshall Clow for ( int i = 0; i < 10; ++i ) 43f2e24f56SStephan T. Lavavej g1[i] = g2[9-i] = static_cast<char_type>('0' + i); 44*f9127593SEric Fiselier H h; 45*f9127593SEric Fiselier SH sh; 46*f9127593SEric Fiselier SV s1(g1, 10); 47*f9127593SEric Fiselier String ss1(s1); 48*f9127593SEric Fiselier SV s2(g2, 10); 49*f9127593SEric Fiselier String ss2(s2); 50*f9127593SEric Fiselier assert(h(s1) == h(s1)); 51053d81ceSMarshall Clow assert(h(s1) != h(s2)); 52*f9127593SEric Fiselier assert(sh(ss1) == h(s1)); 53*f9127593SEric Fiselier assert(sh(ss2) == h(s2)); 54053d81ceSMarshall Clow } 55053d81ceSMarshall Clow 56053d81ceSMarshall Clow int main() 57053d81ceSMarshall Clow { 58053d81ceSMarshall Clow test<std::string_view>(); 59053d81ceSMarshall Clow #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS 60053d81ceSMarshall Clow test<std::u16string_view>(); 61053d81ceSMarshall Clow test<std::u32string_view>(); 62053d81ceSMarshall Clow #endif // _LIBCPP_HAS_NO_UNICODE_CHARS 63053d81ceSMarshall Clow test<std::wstring_view>(); 64053d81ceSMarshall Clow } 65