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