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