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 // XFAIL: LIBCXX-AIX-FIXME 10 11 // <string> 12 13 // const_reference at(size_type pos) const; // constexpr since C++20 14 // reference at(size_type pos); // constexpr since C++20 15 16 #include <string> 17 #include <stdexcept> 18 #include <cassert> 19 20 #include "min_allocator.h" 21 22 #include "test_macros.h" 23 24 template <class S> 25 TEST_CONSTEXPR_CXX20 void 26 test(S s, typename S::size_type pos) 27 { 28 const S& cs = s; 29 if (pos < s.size()) 30 { 31 assert(s.at(pos) == s[pos]); 32 assert(cs.at(pos) == cs[pos]); 33 } 34 #ifndef TEST_HAS_NO_EXCEPTIONS 35 else if (!TEST_IS_CONSTANT_EVALUATED) 36 { 37 try 38 { 39 TEST_IGNORE_NODISCARD s.at(pos); 40 assert(false); 41 } 42 catch (std::out_of_range&) 43 { 44 assert(pos >= s.size()); 45 } 46 try 47 { 48 TEST_IGNORE_NODISCARD cs.at(pos); 49 assert(false); 50 } 51 catch (std::out_of_range&) 52 { 53 assert(pos >= s.size()); 54 } 55 } 56 #endif 57 } 58 59 TEST_CONSTEXPR_CXX20 bool test() { 60 { 61 typedef std::string S; 62 test(S(), 0); 63 test(S("123"), 0); 64 test(S("123"), 1); 65 test(S("123"), 2); 66 test(S("123"), 3); 67 } 68 #if TEST_STD_VER >= 11 69 { 70 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 71 test(S(), 0); 72 test(S("123"), 0); 73 test(S("123"), 1); 74 test(S("123"), 2); 75 test(S("123"), 3); 76 } 77 #endif 78 79 return true; 80 } 81 82 int main(int, char**) 83 { 84 test(); 85 #if TEST_STD_VER > 17 86 static_assert(test()); 87 #endif 88 89 return 0; 90 } 91