xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string.accessors/c_str.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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* c_str() const; // constexpr since C++20
14 
15 #include <string>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void
23 test(const S& s)
24 {
25     typedef typename S::traits_type T;
26     const typename S::value_type* str = s.c_str();
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 TEST_CONSTEXPR_CXX20 bool test() {
37   {
38     typedef std::string S;
39     test(S(""));
40     test(S("abcde"));
41     test(S("abcdefghij"));
42     test(S("abcdefghijklmnopqrst"));
43   }
44 #if TEST_STD_VER >= 11
45   {
46     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
47     test(S(""));
48     test(S("abcde"));
49     test(S("abcdefghij"));
50     test(S("abcdefghijklmnopqrst"));
51   }
52 #endif
53 
54   return true;
55 }
56 
57 int main(int, char**)
58 {
59   test();
60 #if TEST_STD_VER > 17
61   static_assert(test());
62 #endif
63 
64   return 0;
65 }
66