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;
12 // charT* data(); // C++17
13
14 #include <string>
15 #include <cassert>
16
17 #include "test_macros.h"
18 #include "min_allocator.h"
19
20 template <class S>
21 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 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
main(int,char **)50 int main(int, char**)
51 {
52 {
53 typedef std::string S;
54 test_const(S(""));
55 test_const(S("abcde"));
56 test_const(S("abcdefghij"));
57 test_const(S("abcdefghijklmnopqrst"));
58 }
59 #if TEST_STD_VER >= 11
60 {
61 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
62 test_const(S(""));
63 test_const(S("abcde"));
64 test_const(S("abcdefghij"));
65 test_const(S("abcdefghijklmnopqrst"));
66 }
67 #endif
68 #if TEST_STD_VER > 14
69 {
70 typedef std::string S;
71 S s1(""); test_nonconst(s1);
72 S s2("abcde"); test_nonconst(s2);
73 S s3("abcdefghij"); test_nonconst(s3);
74 S s4("abcdefghijklmnopqrst"); test_nonconst(s4);
75 }
76 #endif
77
78 return 0;
79 }
80