Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.ops / string.accessors / data.pass.cpp
blob27762fbd9390d514f82c7f90897263b0ba4d9328
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // const charT* data() const; // constexpr since C++20
12 // charT* data(); // C++17, constexpr since C++20
14 #include <string>
15 #include <cassert>
17 #include "test_macros.h"
18 #include "min_allocator.h"
20 template <class S>
21 TEST_CONSTEXPR_CXX20 void test_const(const S& s) {
22 typedef typename S::traits_type T;
23 const typename S::value_type* str = s.data();
24 if (s.size() > 0) {
25 assert(T::compare(str, &s[0], s.size()) == 0);
26 assert(T::eq(str[s.size()], typename S::value_type()));
27 } else
28 assert(T::eq(str[0], typename S::value_type()));
31 template <class S>
32 TEST_CONSTEXPR_CXX20 void test_nonconst(S& s) {
33 typedef typename S::traits_type T;
34 typename S::value_type* str = s.data();
35 if (s.size() > 0) {
36 assert(T::compare(str, &s[0], s.size()) == 0);
37 assert(T::eq(str[s.size()], typename S::value_type()));
38 } else
39 assert(T::eq(str[0], typename S::value_type()));
42 template <class S>
43 TEST_CONSTEXPR_CXX20 void test_string() {
44 test_const(S(""));
45 test_const(S("abcde"));
46 test_const(S("abcdefghij"));
47 test_const(S("abcdefghijklmnopqrst"));
48 #if TEST_STD_VER > 14
50 S s1("");
51 test_nonconst(s1);
52 S s2("abcde");
53 test_nonconst(s2);
54 S s3("abcdefghij");
55 test_nonconst(s3);
56 S s4("abcdefghijklmnopqrst");
57 test_nonconst(s4);
59 #endif
62 TEST_CONSTEXPR_CXX20 bool test() {
63 test_string<std::string>();
64 #if TEST_STD_VER >= 11
65 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
66 #endif
68 return true;
71 int main(int, char**) {
72 test();
73 #if TEST_STD_VER > 17
74 static_assert(test());
75 #endif
77 return 0;