1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <string>
11
12 // const charT* data() const;
13 // charT* data(); // C++17
14
15 #include <string>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "min_allocator.h"
20
21 template <class S>
22 void
test_const(const S & s)23 test_const(const S& s)
24 {
25 typedef typename S::traits_type T;
26 const typename S::value_type* str = s.data();
27 if (s.size() > 0)
28 {
29 assert(T::compare(str, &s[0], s.size()) == 0);
30 assert(T::eq(str[s.size()], typename S::value_type()));
31 }
32 else
33 assert(T::eq(str[0], typename S::value_type()));
34 }
35
36 template <class S>
37 void
test_nonconst(S & s)38 test_nonconst(S& s)
39 {
40 typedef typename S::traits_type T;
41 typename S::value_type* str = s.data();
42 if (s.size() > 0)
43 {
44 assert(T::compare(str, &s[0], s.size()) == 0);
45 assert(T::eq(str[s.size()], typename S::value_type()));
46 }
47 else
48 assert(T::eq(str[0], typename S::value_type()));
49 }
50
main()51 int main()
52 {
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