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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <string_view>
12 
13 //   constexpr bool starts_with(string_view x) const noexcept;
14 
15 #include <string_view>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "constexpr_char_traits.h"
20 
main(int,char **)21 int main(int, char**)
22 {
23     {
24     typedef std::string_view SV;
25     const char *s = "abcde";
26     SV  sv0 {};
27     SV  sv1 { s, 1 };
28     SV  sv2 { s, 2 };
29     SV  svNot {"def", 3 };
30 
31     LIBCPP_ASSERT_NOEXCEPT(sv0.starts_with(""));
32 
33     assert ( sv0.starts_with(""));
34     assert (!sv0.starts_with("a"));
35 
36     assert ( sv1.starts_with(""));
37     assert ( sv1.starts_with("a"));
38     assert (!sv1.starts_with("ab"));
39     assert (!sv1.starts_with("abc"));
40     assert (!sv1.starts_with("abcd"));
41     assert (!sv1.starts_with("abcde"));
42     assert (!sv1.starts_with("def"));
43 
44     assert ( sv2.starts_with(s + 5));
45     assert ( sv2.starts_with("a"));
46     assert ( sv2.starts_with("ab"));
47     assert (!sv2.starts_with("abc"));
48     assert (!sv2.starts_with("abcd"));
49     assert (!sv2.starts_with("abcde"));
50     assert (!sv2.starts_with("def"));
51 
52     assert ( svNot.starts_with(""));
53     assert (!svNot.starts_with("a"));
54     assert (!svNot.starts_with("ab"));
55     assert (!svNot.starts_with("abc"));
56     assert (!svNot.starts_with("abcd"));
57     assert (!svNot.starts_with("abcde"));
58     assert ( svNot.starts_with("def"));
59     }
60 
61 #if TEST_STD_VER > 11
62     {
63     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
64     constexpr const char *s = "abcde";
65     constexpr SV  sv0 {};
66     constexpr SV  sv1 { s, 1 };
67     constexpr SV  sv2 { s, 2 };
68     constexpr SV  svNot {"def", 3 };
69 
70     static_assert ( sv0.starts_with(""), "" );
71     static_assert (!sv0.starts_with("a"), "" );
72 
73     static_assert ( sv1.starts_with(""), "" );
74     static_assert ( sv1.starts_with("a"), "" );
75     static_assert (!sv1.starts_with("ab"), "" );
76     static_assert (!sv1.starts_with("abc"), "" );
77     static_assert (!sv1.starts_with("abcd"), "" );
78     static_assert (!sv1.starts_with("abcde"), "" );
79     static_assert (!sv1.starts_with("def"), "" );
80 
81     static_assert ( sv2.starts_with(s + 5), "" );
82     static_assert ( sv2.starts_with("a"), "" );
83     static_assert ( sv2.starts_with("ab"), "" );
84     static_assert (!sv2.starts_with("abc"), "" );
85     static_assert (!sv2.starts_with("abcd"), "" );
86     static_assert (!sv2.starts_with("abcde"), "" );
87     static_assert (!sv2.starts_with("def"), "" );
88 
89     static_assert ( svNot.starts_with(""), "" );
90     static_assert (!svNot.starts_with("a"), "" );
91     static_assert (!svNot.starts_with("ab"), "" );
92     static_assert (!svNot.starts_with("abc"), "" );
93     static_assert (!svNot.starts_with("abcd"), "" );
94     static_assert (!svNot.starts_with("abcde"), "" );
95     static_assert ( svNot.starts_with("def"), "" );
96     }
97 #endif
98 
99   return 0;
100 }
101