1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
12 // constexpr size_type rfind(charT c, size_type pos = npos) const;
14 #include <string_view>
17 #include "test_macros.h"
18 #include "constexpr_char_traits.h"
21 void test(const S
& s
, typename
S::value_type c
, typename
S::size_type pos
, typename
S::size_type x
) {
22 LIBCPP_ASSERT_NOEXCEPT(s
.rfind(c
, pos
));
23 assert(s
.rfind(c
, pos
) == x
);
25 assert(x
<= pos
&& x
+ 1 <= s
.size());
29 void test(const S
& s
, typename
S::value_type c
, typename
S::size_type x
) {
30 LIBCPP_ASSERT_NOEXCEPT(s
.rfind(c
));
31 assert(s
.rfind(c
) == x
);
33 assert(x
+ 1 <= s
.size());
36 int main(int, char**) {
38 typedef std::string_view S
;
39 test(S(""), 'b', 0, S::npos
);
40 test(S(""), 'b', 1, S::npos
);
41 test(S("abcde"), 'b', 0, S::npos
);
42 test(S("abcde"), 'b', 1, 1);
43 test(S("abcde"), 'b', 2, 1);
44 test(S("abcde"), 'b', 4, 1);
45 test(S("abcde"), 'b', 5, 1);
46 test(S("abcde"), 'b', 6, 1);
47 test(S("abcdeabcde"), 'b', 0, S::npos
);
48 test(S("abcdeabcde"), 'b', 1, 1);
49 test(S("abcdeabcde"), 'b', 5, 1);
50 test(S("abcdeabcde"), 'b', 9, 6);
51 test(S("abcdeabcde"), 'b', 10, 6);
52 test(S("abcdeabcde"), 'b', 11, 6);
53 test(S("abcdeabcdeabcdeabcde"), 'b', 0, S::npos
);
54 test(S("abcdeabcdeabcdeabcde"), 'b', 1, 1);
55 test(S("abcdeabcdeabcdeabcde"), 'b', 10, 6);
56 test(S("abcdeabcdeabcdeabcde"), 'b', 19, 16);
57 test(S("abcdeabcdeabcdeabcde"), 'b', 20, 16);
58 test(S("abcdeabcdeabcdeabcde"), 'b', 21, 16);
60 test(S(""), 'b', S::npos
);
61 test(S("abcde"), 'b', 1);
62 test(S("abcdeabcde"), 'b', 6);
63 test(S("abcdeabcdeabcdeabcde"), 'b', 16);
68 typedef std::basic_string_view
<char, constexpr_char_traits
<char>> SV
;
70 constexpr SV sv2
{"abcde", 5};
72 static_assert(sv1
.rfind('b', 0) == SV::npos
, "");
73 static_assert(sv1
.rfind('b', 1) == SV::npos
, "");
74 static_assert(sv2
.rfind('b', 0) == SV::npos
, "");
75 static_assert(sv2
.rfind('b', 1) == 1, "");
76 static_assert(sv2
.rfind('b', 2) == 1, "");
77 static_assert(sv2
.rfind('b', 3) == 1, "");
78 static_assert(sv2
.rfind('b', 4) == 1, "");