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