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 charT* data() const; // constexpr since C++20
12 // charT* data(); // C++17, constexpr since C++20
13
14 #include <string>
15 #include <cassert>
16
17 #include "test_macros.h"
18 #include "min_allocator.h"
19
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void
test_const(const S & s)22 test_const(const S& s)
23 {
24 typedef typename S::traits_type T;
25 const typename S::value_type* str = s.data();
26 if (s.size() > 0)
27 {
28 assert(T::compare(str, &s[0], s.size()) == 0);
29 assert(T::eq(str[s.size()], typename S::value_type()));
30 }
31 else
32 assert(T::eq(str[0], typename S::value_type()));
33 }
34
35 template <class S>
36 TEST_CONSTEXPR_CXX20 void
test_nonconst(S & s)37 test_nonconst(S& s)
38 {
39 typedef typename S::traits_type T;
40 typename S::value_type* str = s.data();
41 if (s.size() > 0)
42 {
43 assert(T::compare(str, &s[0], s.size()) == 0);
44 assert(T::eq(str[s.size()], typename S::value_type()));
45 }
46 else
47 assert(T::eq(str[0], typename S::value_type()));
48 }
49
test()50 TEST_CONSTEXPR_CXX20 bool test() {
51 {
52 typedef std::string S;
53 test_const(S(""));
54 test_const(S("abcde"));
55 test_const(S("abcdefghij"));
56 test_const(S("abcdefghijklmnopqrst"));
57 }
58 #if TEST_STD_VER >= 11
59 {
60 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
61 test_const(S(""));
62 test_const(S("abcde"));
63 test_const(S("abcdefghij"));
64 test_const(S("abcdefghijklmnopqrst"));
65 }
66 #endif
67 #if TEST_STD_VER > 14
68 {
69 typedef std::string S;
70 S s1(""); test_nonconst(s1);
71 S s2("abcde"); test_nonconst(s2);
72 S s3("abcdefghij"); test_nonconst(s3);
73 S s4("abcdefghijklmnopqrst"); test_nonconst(s4);
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