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