xref: /llvm-project/libcxx/test/std/strings/basic.string/string.access/back.pass.cpp (revision 9ea0e473f0b96455b918eefcf8fc535638674a1f)
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 
9 // <string>
10 
11 // const charT& back() const;
12 //       charT& back();
13 
14 #ifdef _LIBCPP_DEBUG
15 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
16 #endif
17 
18 #include <string>
19 #include <cassert>
20 
21 #include "min_allocator.h"
22 
23 template <class S>
24 void
25 test(S s)
26 {
27     const S& cs = s;
28     ASSERT_SAME_TYPE(decltype( s.back()), typename S::reference);
29     ASSERT_SAME_TYPE(decltype(cs.back()), typename S::const_reference);
30     LIBCPP_ASSERT_NOEXCEPT(    s.back());
31     LIBCPP_ASSERT_NOEXCEPT(   cs.back());
32     assert(&cs.back() == &cs[cs.size()-1]);
33     assert(&s.back() == &s[cs.size()-1]);
34     s.back() = typename S::value_type('z');
35     assert(s.back() == typename S::value_type('z'));
36 }
37 
38 int main(int, char**)
39 {
40     {
41     typedef std::string S;
42     test(S("1"));
43     test(S("1234567890123456789012345678901234567890"));
44     }
45 #if TEST_STD_VER >= 11
46     {
47     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
48     test(S("1"));
49     test(S("1234567890123456789012345678901234567890"));
50     }
51 #endif
52 #ifdef _LIBCPP_DEBUG
53     {
54         std::string s;
55         char c = s.back();
56         assert(false);
57     }
58 #endif
59 
60   return 0;
61 }
62