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 #include <string>
15 #include <cassert>
16 
17 #include "test_macros.h"
18 #include "min_allocator.h"
19 
main(int,char **)20 int main(int, char**)
21 {
22     {
23     typedef std::string S;
24     S s("0123456789");
25     const S& cs = s;
26     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
27     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
28     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
29     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
30     for (S::size_type i = 0; i < cs.size(); ++i)
31     {
32         assert(s[i] == static_cast<char>('0' + i));
33         assert(cs[i] == s[i]);
34     }
35     assert(cs[cs.size()] == '\0');
36     const S s2 = S();
37     assert(s2[0] == '\0');
38     }
39 #if TEST_STD_VER >= 11
40     {
41     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
42     S s("0123456789");
43     const S& cs = s;
44     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
45     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
46     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
47     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
48     for (S::size_type i = 0; i < cs.size(); ++i)
49     {
50         assert(s[i] == static_cast<char>('0' + i));
51         assert(cs[i] == s[i]);
52     }
53     assert(cs[cs.size()] == '\0');
54     const S s2 = S();
55     assert(s2[0] == '\0');
56     }
57 #endif
58 
59     return 0;
60 }
61