string_view.pass.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <functional>
  9. // template <class T>
  10. // struct hash
  11. // : public unary_function<T, size_t>
  12. // {
  13. // size_t operator()(T val) const;
  14. // };
  15. // Not very portable
  16. #include <string_view>
  17. #include <string>
  18. #include <cassert>
  19. #include <type_traits>
  20. #include "test_macros.h"
  21. using std::string_view;
  22. template <class SV>
  23. void
  24. test()
  25. {
  26. typedef std::hash<SV> H;
  27. static_assert((std::is_same<typename H::argument_type, SV>::value), "" );
  28. static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
  29. typedef typename SV::value_type char_type;
  30. typedef std::basic_string<char_type> String;
  31. typedef std::hash<String> SH;
  32. ASSERT_NOEXCEPT(H()(SV()));
  33. char_type g1 [ 10 ];
  34. char_type g2 [ 10 ];
  35. for ( int i = 0; i < 10; ++i )
  36. g1[i] = g2[9-i] = static_cast<char_type>('0' + i);
  37. H h;
  38. SH sh;
  39. SV s1(g1, 10);
  40. String ss1(s1);
  41. SV s2(g2, 10);
  42. String ss2(s2);
  43. assert(h(s1) == h(s1));
  44. assert(h(s1) != h(s2));
  45. assert(sh(ss1) == h(s1));
  46. assert(sh(ss2) == h(s2));
  47. }
  48. int main(int, char**)
  49. {
  50. test<std::string_view>();
  51. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  52. test<std::u8string_view>();
  53. #endif
  54. #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
  55. test<std::u16string_view>();
  56. test<std::u32string_view>();
  57. #endif // _LIBCPP_HAS_NO_UNICODE_CHARS
  58. test<std::wstring_view>();
  59. return 0;
  60. }