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