rend.pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // <string_view>
  9. // constexpr const_iterator rend() const;
  10. #include <string_view>
  11. #include <cassert>
  12. #include <cstddef>
  13. #include "test_macros.h"
  14. template <class S>
  15. void
  16. test(S s)
  17. {
  18. const S& cs = s;
  19. typename S::reverse_iterator e = s.rend();
  20. typename S::const_reverse_iterator ce1 = cs.rend();
  21. typename S::const_reverse_iterator ce2 = s.crend();
  22. if (s.empty())
  23. {
  24. assert( e == s.rbegin());
  25. assert(ce1 == cs.rbegin());
  26. assert(ce2 == s.rbegin());
  27. }
  28. else
  29. {
  30. assert( e != s.rbegin());
  31. assert(ce1 != cs.rbegin());
  32. assert(ce2 != s.rbegin());
  33. }
  34. assert(static_cast<std::size_t>( e - s.rbegin()) == s.size());
  35. assert(static_cast<std::size_t>(ce1 - cs.rbegin()) == cs.size());
  36. assert(static_cast<std::size_t>(ce2 - s.crbegin()) == s.size());
  37. assert( e == ce1);
  38. assert( e == ce2);
  39. assert(ce1 == ce2);
  40. }
  41. int main(int, char**)
  42. {
  43. typedef std::string_view string_view;
  44. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  45. typedef std::u8string_view u8string_view;
  46. #endif
  47. typedef std::u16string_view u16string_view;
  48. typedef std::u32string_view u32string_view;
  49. typedef std::wstring_view wstring_view;
  50. test(string_view ());
  51. test(u16string_view());
  52. test(u32string_view());
  53. test(wstring_view ());
  54. test(string_view ( "123"));
  55. test(wstring_view (L"123"));
  56. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  57. test(u8string_view{u8"123"});
  58. #endif
  59. #if TEST_STD_VER >= 11
  60. test(u16string_view{u"123"});
  61. test(u32string_view{U"123"});
  62. #endif
  63. #if TEST_STD_VER > 14
  64. {
  65. constexpr string_view sv { "123", 3 };
  66. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  67. constexpr u8string_view u8sv {u8"123", 3 };
  68. #endif
  69. constexpr u16string_view u16sv {u"123", 3 };
  70. constexpr u32string_view u32sv {U"123", 3 };
  71. constexpr wstring_view wsv {L"123", 3 };
  72. static_assert ( *--sv.rend() == sv[0], "" );
  73. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  74. static_assert ( *--u8sv.rend() == u8sv[0], "" );
  75. #endif
  76. static_assert ( *--u16sv.rend() == u16sv[0], "" );
  77. static_assert ( *--u32sv.rend() == u32sv[0], "" );
  78. static_assert ( *--wsv.rend() == wsv[0], "" );
  79. static_assert ( *--sv.crend() == sv[0], "" );
  80. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  81. static_assert ( *--u8sv.crend() == u8sv[0], "" );
  82. #endif
  83. static_assert ( *--u16sv.crend() == u16sv[0], "" );
  84. static_assert ( *--u32sv.crend() == u32sv[0], "" );
  85. static_assert ( *--wsv.crend() == wsv[0], "" );
  86. }
  87. #endif
  88. return 0;
  89. }