• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, 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.hpp"
20 
main()21 int main()
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  sv3 { s, 3 };
30 //     SV  sv4 { s, 4 };
31 //     SV  sv5 { s, 5 };
32     SV  svNot {"def", 3 };
33 
34     LIBCPP_ASSERT_NOEXCEPT(sv0.starts_with(""));
35 
36     assert ( sv0.starts_with(""));
37     assert (!sv0.starts_with("a"));
38 
39     assert ( sv1.starts_with(""));
40     assert ( sv1.starts_with("a"));
41     assert (!sv1.starts_with("ab"));
42     assert (!sv1.starts_with("abc"));
43     assert (!sv1.starts_with("abcd"));
44     assert (!sv1.starts_with("abcde"));
45     assert (!sv1.starts_with("def"));
46 
47     assert ( sv2.starts_with(s + 5));
48     assert ( sv2.starts_with("a"));
49     assert ( sv2.starts_with("ab"));
50     assert (!sv2.starts_with("abc"));
51     assert (!sv2.starts_with("abcd"));
52     assert (!sv2.starts_with("abcde"));
53     assert (!sv2.starts_with("def"));
54 
55     assert ( svNot.starts_with(""));
56     assert (!svNot.starts_with("a"));
57     assert (!svNot.starts_with("ab"));
58     assert (!svNot.starts_with("abc"));
59     assert (!svNot.starts_with("abcd"));
60     assert (!svNot.starts_with("abcde"));
61     assert ( svNot.starts_with("def"));
62     }
63 
64 #if TEST_STD_VER > 11
65     {
66     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
67     constexpr const char *s = "abcde";
68     constexpr SV  sv0 {};
69     constexpr SV  sv1 { s, 1 };
70     constexpr SV  sv2 { s, 2 };
71 //     constexpr SV  sv3 { s, 3 };
72 //     constexpr SV  sv4 { s, 4 };
73 //     constexpr SV  sv5 { s, 5 };
74     constexpr SV  svNot {"def", 3 };
75 
76     static_assert ( sv0.starts_with(""), "" );
77     static_assert (!sv0.starts_with("a"), "" );
78 
79     static_assert ( sv1.starts_with(""), "" );
80     static_assert ( sv1.starts_with("a"), "" );
81     static_assert (!sv1.starts_with("ab"), "" );
82     static_assert (!sv1.starts_with("abc"), "" );
83     static_assert (!sv1.starts_with("abcd"), "" );
84     static_assert (!sv1.starts_with("abcde"), "" );
85     static_assert (!sv1.starts_with("def"), "" );
86 
87     static_assert ( sv2.starts_with(s + 5), "" );
88     static_assert ( sv2.starts_with("a"), "" );
89     static_assert ( sv2.starts_with("ab"), "" );
90     static_assert (!sv2.starts_with("abc"), "" );
91     static_assert (!sv2.starts_with("abcd"), "" );
92     static_assert (!sv2.starts_with("abcde"), "" );
93     static_assert (!sv2.starts_with("def"), "" );
94 
95     static_assert ( svNot.starts_with(""), "" );
96     static_assert (!svNot.starts_with("a"), "" );
97     static_assert (!svNot.starts_with("ab"), "" );
98     static_assert (!svNot.starts_with("abc"), "" );
99     static_assert (!svNot.starts_with("abcd"), "" );
100     static_assert (!svNot.starts_with("abcde"), "" );
101     static_assert ( svNot.starts_with("def"), "" );
102     }
103 #endif
104 }
105