index.pass.cpp 1.4 KB

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