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: !stdlib=libc++ && (c++03 || c++11 || c++14)
10
11 // <string_view>
12
13 // constexpr size_type find(charT c, size_type pos = 0) const;
14
15 #include <string_view>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "constexpr_char_traits.h"
20
21 template <class S>
22 void
test(const S & s,typename S::value_type c,typename S::size_type pos,typename S::size_type x)23 test(const S& s, typename S::value_type c, typename S::size_type pos,
24 typename S::size_type x)
25 {
26 LIBCPP_ASSERT_NOEXCEPT(s.find(c, pos));
27 assert(s.find(c, pos) == x);
28 if (x != S::npos)
29 assert(pos <= x && x + 1 <= s.size());
30 }
31
32 template <class S>
33 void
test(const S & s,typename S::value_type c,typename S::size_type x)34 test(const S& s, typename S::value_type c, typename S::size_type x)
35 {
36 assert(s.find(c) == x);
37 if (x != S::npos)
38 assert(0 <= x && x + 1 <= s.size());
39 }
40
main(int,char **)41 int main(int, char**)
42 {
43 {
44 typedef std::string_view S;
45 test(S(""), 'c', 0, S::npos);
46 test(S(""), 'c', 1, S::npos);
47 test(S("abcde"), 'c', 0, 2);
48 test(S("abcde"), 'c', 1, 2);
49 test(S("abcde"), 'c', 2, 2);
50 test(S("abcde"), 'c', 4, S::npos);
51 test(S("abcde"), 'c', 5, S::npos);
52 test(S("abcde"), 'c', 6, S::npos);
53 test(S("abcdeabcde"), 'c', 0, 2);
54 test(S("abcdeabcde"), 'c', 1, 2);
55 test(S("abcdeabcde"), 'c', 5, 7);
56 test(S("abcdeabcde"), 'c', 9, S::npos);
57 test(S("abcdeabcde"), 'c', 10, S::npos);
58 test(S("abcdeabcde"), 'c', 11, S::npos);
59 test(S("abcdeabcdeabcdeabcde"), 'c', 0, 2);
60 test(S("abcdeabcdeabcdeabcde"), 'c', 1, 2);
61 test(S("abcdeabcdeabcdeabcde"), 'c', 10, 12);
62 test(S("abcdeabcdeabcdeabcde"), 'c', 19, S::npos);
63 test(S("abcdeabcdeabcdeabcde"), 'c', 20, S::npos);
64 test(S("abcdeabcdeabcdeabcde"), 'c', 21, S::npos);
65
66 test(S(""), 'c', S::npos);
67 test(S("abcde"), 'c', 2);
68 test(S("abcdeabcde"), 'c', 2);
69 test(S("abcdeabcdeabcdeabcde"), 'c', 2);
70 }
71
72 #if TEST_STD_VER > 11
73 {
74 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
75 constexpr SV sv1;
76 constexpr SV sv2 { "abcde", 5 };
77
78 static_assert (sv1.find( 'c', 0 ) == SV::npos, "" );
79 static_assert (sv1.find( 'c', 1 ) == SV::npos, "" );
80 static_assert (sv2.find( 'c', 0 ) == 2, "" );
81 static_assert (sv2.find( 'c', 1 ) == 2, "" );
82 static_assert (sv2.find( 'c', 2 ) == 2, "" );
83 static_assert (sv2.find( 'c', 3 ) == SV::npos, "" );
84 static_assert (sv2.find( 'c', 4 ) == SV::npos, "" );
85 }
86 #endif
87
88 return 0;
89 }
90