xref: /llvm-project/libcxx/test/std/strings/basic.string/string.access/back.pass.cpp (revision 786366b18fadc3d7c4f150e89ef49c165767a668)
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 template <class S>
36 TEST_CONSTEXPR_CXX20 void test_string() {
37   test(S("1"));
38   test(S("1234567890123456789012345678901234567890"));
39 }
40 
41 TEST_CONSTEXPR_CXX20 bool test() {
42   test_string<std::string>();
43 #if TEST_STD_VER >= 11
44   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
45 #endif
46 
47   return true;
48 }
49 
50 int main(int, char**)
51 {
52   test();
53 #if TEST_STD_VER > 17
54   static_assert(test());
55 #endif
56 
57   return 0;
58 }
59