index.pass.cpp 1.3 KB

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