xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string.accessors/c_str.pass.cpp (revision 7fc6a55688c816f5fc1a5481ae7af25be7500356)
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* c_str() const;
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 template <class S>
20 void
21 test(const S& s)
22 {
23     typedef typename S::traits_type T;
24     const typename S::value_type* str = s.c_str();
25     if (s.size() > 0)
26     {
27         assert(T::compare(str, &s[0], s.size()) == 0);
28         assert(T::eq(str[s.size()], typename S::value_type()));
29     }
30     else
31         assert(T::eq(str[0], typename S::value_type()));
32 }
33 
34 int main(int, char**)
35 {
36     {
37     typedef std::string S;
38     test(S(""));
39     test(S("abcde"));
40     test(S("abcdefghij"));
41     test(S("abcdefghijklmnopqrst"));
42     }
43 #if TEST_STD_VER >= 11
44     {
45     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
46     test(S(""));
47     test(S("abcde"));
48     test(S("abcdefghij"));
49     test(S("abcdefghijklmnopqrst"));
50     }
51 #endif
52 
53   return 0;
54 }
55