back.pass.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 _CharT& front();
  10. #include <string_view>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. template <typename CharT>
  14. bool test ( const CharT *s, size_t len ) {
  15. typedef std::basic_string_view<CharT> SV;
  16. SV sv ( s, len );
  17. ASSERT_SAME_TYPE(decltype(sv.back()), typename SV::const_reference);
  18. LIBCPP_ASSERT_NOEXCEPT( sv.back());
  19. assert ( sv.length() == len );
  20. assert ( sv.back() == s[len-1] );
  21. return &sv.back() == s + len - 1;
  22. }
  23. int main(int, char**) {
  24. assert ( test ( "ABCDE", 5 ));
  25. assert ( test ( "a", 1 ));
  26. assert ( test ( L"ABCDE", 5 ));
  27. assert ( test ( L"a", 1 ));
  28. #if TEST_STD_VER >= 11
  29. assert ( test ( u"ABCDE", 5 ));
  30. assert ( test ( u"a", 1 ));
  31. assert ( test ( U"ABCDE", 5 ));
  32. assert ( test ( U"a", 1 ));
  33. #endif
  34. #if TEST_STD_VER >= 11
  35. {
  36. constexpr std::basic_string_view<char> sv ( "ABC", 2 );
  37. static_assert ( sv.length() == 2, "" );
  38. static_assert ( sv.back() == 'B', "" );
  39. }
  40. #endif
  41. return 0;
  42. }