string_view.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // <functional>
  10. // template <class T>
  11. // struct hash
  12. // : public unary_function<T, size_t>
  13. // {
  14. // size_t operator()(T val) const;
  15. // };
  16. // Not very portable
  17. #include <string_view>
  18. #include <cassert>
  19. #include <type_traits>
  20. using std::string_view;
  21. template <class T>
  22. void
  23. test()
  24. {
  25. typedef std::hash<T> H;
  26. static_assert((std::is_same<typename H::argument_type, T>::value), "" );
  27. static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
  28. H h;
  29. // std::string g1 = "1234567890";
  30. // std::string g2 = "1234567891";
  31. typedef typename T::value_type char_type;
  32. char_type g1 [ 10 ];
  33. char_type g2 [ 10 ];
  34. for ( int i = 0; i < 10; ++i )
  35. g1[i] = g2[9-i] = '0' + i;
  36. T s1(g1, 10);
  37. T s2(g2, 10);
  38. assert(h(s1) != h(s2));
  39. }
  40. int main()
  41. {
  42. test<std::string_view>();
  43. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  44. test<std::u16string_view>();
  45. test<std::u32string_view>();
  46. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  47. test<std::wstring_view>();
  48. }