end.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 end() 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::iterator e = s.end();
  20. typename S::const_iterator ce1 = cs.end();
  21. typename S::const_iterator ce2 = s.cend();
  22. if (s.empty())
  23. {
  24. assert( e == s.begin());
  25. assert(ce1 == cs.begin());
  26. assert(ce2 == s.begin());
  27. }
  28. else
  29. {
  30. assert( e != s.begin());
  31. assert(ce1 != cs.begin());
  32. assert(ce2 != s.begin());
  33. }
  34. assert(static_cast<std::size_t>( e - s.begin()) == s.size());
  35. assert(static_cast<std::size_t>(ce1 - cs.begin()) == cs.size());
  36. assert(static_cast<std::size_t>(ce2 - s.cbegin()) == 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 > 11
  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.begin() != sv.end(), "" );
  73. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  74. static_assert ( u8sv.begin() != u8sv.end(), "" );
  75. #endif
  76. static_assert ( u16sv.begin() != u16sv.end(), "" );
  77. static_assert ( u32sv.begin() != u32sv.end(), "" );
  78. static_assert ( wsv.begin() != wsv.end(), "" );
  79. static_assert ( sv.begin() != sv.cend(), "" );
  80. #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
  81. static_assert ( u8sv.begin() != u8sv.cend(), "" );
  82. #endif
  83. static_assert ( u16sv.begin() != u16sv.cend(), "" );
  84. static_assert ( u32sv.begin() != u32sv.cend(), "" );
  85. static_assert ( wsv.begin() != wsv.cend(), "" );
  86. }
  87. #endif
  88. return 0;
  89. }