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; // constexpr since C++20 12 13 #include <string> 14 #include <cassert> 15 16 #include "test_macros.h" 17 #include "min_allocator.h" 18 19 template <class S> 20 TEST_CONSTEXPR_CXX20 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 TEST_CONSTEXPR_CXX20 bool test() { 35 { 36 typedef std::string S; 37 test(S("")); 38 test(S("abcde")); 39 test(S("abcdefghij")); 40 test(S("abcdefghijklmnopqrst")); 41 } 42 #if TEST_STD_VER >= 11 43 { 44 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 45 test(S("")); 46 test(S("abcde")); 47 test(S("abcdefghij")); 48 test(S("abcdefghijklmnopqrst")); 49 } 50 #endif 51 52 return true; 53 } 54 55 int main(int, char**) 56 { 57 test(); 58 #if TEST_STD_VER > 17 59 static_assert(test()); 60 #endif 61 62 return 0; 63 } 64