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