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_reference operator[](size_type pos) const;
12 //       reference operator[](size_type pos);
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 int main(int, char**)
24 {
25     {
26     typedef std::string S;
27     S s("0123456789");
28     const S& cs = s;
29     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
30     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
31     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
32     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
33     for (S::size_type i = 0; i < cs.size(); ++i)
34     {
35         assert(s[i] == static_cast<char>('0' + i));
36         assert(cs[i] == s[i]);
37     }
38     assert(cs[cs.size()] == '\0');
39     const S s2 = S();
40     assert(s2[0] == '\0');
41     }
42 #if TEST_STD_VER >= 11
43     {
44     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
45     S s("0123456789");
46     const S& cs = s;
47     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
48     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
49     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
50     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
51     for (S::size_type i = 0; i < cs.size(); ++i)
52     {
53         assert(s[i] == static_cast<char>('0' + i));
54         assert(cs[i] == s[i]);
55     }
56     assert(cs[cs.size()] == '\0');
57     const S s2 = S();
58     assert(s2[0] == '\0');
59     }
60 #endif
61 #ifdef _LIBCPP_DEBUG
62     {
63         std::string s;
64         char c = s[0];
65         assert(c == '\0');
66         c = s[1];
67         assert(false);
68     }
69 #endif
70 
71   return 0;
72 }
73