index.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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>
  9. // const_reference operator[](size_type pos) const;
  10. // reference operator[](size_type pos);
  11. #ifdef _LIBCPP_DEBUG
  12. #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
  13. #endif
  14. #include <string>
  15. #include <cassert>
  16. #include "min_allocator.h"
  17. int main(int, char**)
  18. {
  19. {
  20. typedef std::string S;
  21. S s("0123456789");
  22. const S& cs = s;
  23. ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
  24. ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
  25. LIBCPP_ASSERT_NOEXCEPT( s[0]);
  26. LIBCPP_ASSERT_NOEXCEPT( cs[0]);
  27. for (S::size_type i = 0; i < cs.size(); ++i)
  28. {
  29. assert(s[i] == static_cast<char>('0' + i));
  30. assert(cs[i] == s[i]);
  31. }
  32. assert(cs[cs.size()] == '\0');
  33. const S s2 = S();
  34. assert(s2[0] == '\0');
  35. }
  36. #if TEST_STD_VER >= 11
  37. {
  38. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  39. S s("0123456789");
  40. const S& cs = s;
  41. ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
  42. ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
  43. LIBCPP_ASSERT_NOEXCEPT( s[0]);
  44. LIBCPP_ASSERT_NOEXCEPT( cs[0]);
  45. for (S::size_type i = 0; i < cs.size(); ++i)
  46. {
  47. assert(s[i] == static_cast<char>('0' + i));
  48. assert(cs[i] == s[i]);
  49. }
  50. assert(cs[cs.size()] == '\0');
  51. const S s2 = S();
  52. assert(s2[0] == '\0');
  53. }
  54. #endif
  55. #ifdef _LIBCPP_DEBUG
  56. {
  57. std::string s;
  58. char c = s[0];
  59. assert(c == '\0');
  60. c = s[1];
  61. assert(false);
  62. }
  63. #endif
  64. return 0;
  65. }