at.pass.cpp 1.6 KB

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