begin.pass.cpp 2.7 KB

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