back.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 charT& back() const;
  10. // charT& back();
  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. template <class S>
  18. void
  19. test(S s)
  20. {
  21. const S& cs = s;
  22. ASSERT_SAME_TYPE(decltype( s.back()), typename S::reference);
  23. ASSERT_SAME_TYPE(decltype(cs.back()), typename S::const_reference);
  24. LIBCPP_ASSERT_NOEXCEPT( s.back());
  25. LIBCPP_ASSERT_NOEXCEPT( cs.back());
  26. assert(&cs.back() == &cs[cs.size()-1]);
  27. assert(&s.back() == &s[cs.size()-1]);
  28. s.back() = typename S::value_type('z');
  29. assert(s.back() == typename S::value_type('z'));
  30. }
  31. int main(int, char**)
  32. {
  33. {
  34. typedef std::string S;
  35. test(S("1"));
  36. test(S("1234567890123456789012345678901234567890"));
  37. }
  38. #if TEST_STD_VER >= 11
  39. {
  40. typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
  41. test(S("1"));
  42. test(S("1234567890123456789012345678901234567890"));
  43. }
  44. #endif
  45. #ifdef _LIBCPP_DEBUG
  46. {
  47. std::string s;
  48. char c = s.back();
  49. assert(false);
  50. }
  51. #endif
  52. return 0;
  53. }