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