at.pass.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // NOTE: Older versions of clang have a bug where they fail to evaluate
  9. // string_view::at as a constant expression.
  10. // XFAIL: clang-3.4, clang-3.3
  11. // <string_view>
  12. // constexpr const _CharT& at(size_type _pos) const;
  13. #include <string_view>
  14. #include <stdexcept>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. template <typename CharT>
  18. void test ( const CharT *s, size_t len ) {
  19. std::basic_string_view<CharT> sv ( s, len );
  20. assert ( sv.length() == len );
  21. for ( size_t i = 0; i < len; ++i ) {
  22. assert ( sv.at(i) == s[i] );
  23. assert ( &sv.at(i) == s + i );
  24. }
  25. #ifndef TEST_HAS_NO_EXCEPTIONS
  26. try { (void)sv.at(len); } catch ( const std::out_of_range & ) { return ; }
  27. assert ( false );
  28. #endif
  29. }
  30. int main(int, char**) {
  31. test ( "ABCDE", 5 );
  32. test ( "a", 1 );
  33. test ( L"ABCDE", 5 );
  34. test ( L"a", 1 );
  35. #if TEST_STD_VER >= 11
  36. test ( u"ABCDE", 5 );
  37. test ( u"a", 1 );
  38. test ( U"ABCDE", 5 );
  39. test ( U"a", 1 );
  40. #endif
  41. #if TEST_STD_VER >= 11
  42. {
  43. constexpr std::basic_string_view<char> sv ( "ABC", 2 );
  44. static_assert ( sv.length() == 2, "" );
  45. static_assert ( sv.at(0) == 'A', "" );
  46. static_assert ( sv.at(1) == 'B', "" );
  47. }
  48. #endif
  49. return 0;
  50. }