begin.pass.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // <string_view>
  10. // constexpr const_iterator begin() const;
  11. #include <string_view>
  12. #include <cassert>
  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 b = s.begin();
  20. typename S::const_iterator cb1 = cs.begin();
  21. typename S::const_iterator cb2 = s.cbegin();
  22. if (!s.empty())
  23. {
  24. assert( *b == s[0]);
  25. assert( &*b == &s[0]);
  26. assert( *cb1 == s[0]);
  27. assert(&*cb1 == &s[0]);
  28. assert( *cb2 == s[0]);
  29. assert(&*cb2 == &s[0]);
  30. }
  31. assert( b == cb1);
  32. assert( b == cb2);
  33. assert(cb1 == cb2);
  34. }
  35. int main()
  36. {
  37. typedef std::string_view string_view;
  38. typedef std::u16string_view u16string_view;
  39. typedef std::u32string_view u32string_view;
  40. typedef std::wstring_view wstring_view;
  41. test(string_view ());
  42. test(u16string_view());
  43. test(u32string_view());
  44. test(wstring_view ());
  45. test(string_view ( "123"));
  46. test(wstring_view (L"123"));
  47. #if TEST_STD_VER >= 11
  48. test(u16string_view{u"123"});
  49. test(u32string_view{U"123"});
  50. #endif
  51. #if _LIBCPP_STD_VER > 11
  52. {
  53. constexpr string_view sv { "123", 3 };
  54. constexpr u16string_view u16sv {u"123", 3 };
  55. constexpr u32string_view u32sv {U"123", 3 };
  56. constexpr wstring_view wsv {L"123", 3 };
  57. static_assert ( *sv.begin() == sv[0], "" );
  58. static_assert ( *u16sv.begin() == u16sv[0], "" );
  59. static_assert ( *u32sv.begin() == u32sv[0], "" );
  60. static_assert ( *wsv.begin() == wsv[0], "" );
  61. static_assert ( *sv.cbegin() == sv[0], "" );
  62. static_assert ( *u16sv.cbegin() == u16sv[0], "" );
  63. static_assert ( *u32sv.cbegin() == u32sv[0], "" );
  64. static_assert ( *wsv.cbegin() == wsv[0], "" );
  65. }
  66. #endif
  67. }