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
20template <class S>
21void 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);
24 if (x != S::npos)
25 assert(x <= pos && x + 1 <= s.size());
26}
27
28template <class S>
29void 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);
32 if (x != S::npos)
33 assert(x + 1 <= s.size());
34}
35
36int main(int, char**) {
37 {
38 typedef std::string_view S;
39 test(s: S(""), c: 'b', pos: 0, x: S::npos);
40 test(s: S(""), c: 'b', pos: 1, x: S::npos);
41 test(s: S("abcde"), c: 'b', pos: 0, x: S::npos);
42 test(s: S("abcde"), c: 'b', pos: 1, x: 1);
43 test(s: S("abcde"), c: 'b', pos: 2, x: 1);
44 test(s: S("abcde"), c: 'b', pos: 4, x: 1);
45 test(s: S("abcde"), c: 'b', pos: 5, x: 1);
46 test(s: S("abcde"), c: 'b', pos: 6, x: 1);
47 test(s: S("abcdeabcde"), c: 'b', pos: 0, x: S::npos);
48 test(s: S("abcdeabcde"), c: 'b', pos: 1, x: 1);
49 test(s: S("abcdeabcde"), c: 'b', pos: 5, x: 1);
50 test(s: S("abcdeabcde"), c: 'b', pos: 9, x: 6);
51 test(s: S("abcdeabcde"), c: 'b', pos: 10, x: 6);
52 test(s: S("abcdeabcde"), c: 'b', pos: 11, x: 6);
53 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 0, x: S::npos);
54 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 1, x: 1);
55 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 10, x: 6);
56 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 19, x: 16);
57 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 20, x: 16);
58 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', pos: 21, x: 16);
59
60 test(s: S(""), c: 'b', x: S::npos);
61 test(s: S("abcde"), c: 'b', x: 1);
62 test(s: S("abcdeabcde"), c: 'b', x: 6);
63 test(s: S("abcdeabcdeabcdeabcde"), c: 'b', x: 16);
64 }
65
66#if TEST_STD_VER > 11
67 {
68 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
69 constexpr SV sv1;
70 constexpr SV sv2{"abcde", 5};
71
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, "");
79 }
80#endif
81
82 return 0;
83}
84

source code of libcxx/test/std/strings/string.view/string.view.find/rfind_char_size.pass.cpp