xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_rfind/char_size.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
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 // <string>
10 
11 // size_type rfind(charT c, size_type pos = npos) const; // constexpr since C++20
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 template <class S>
20 TEST_CONSTEXPR_CXX20 void
test(const S & s,typename S::value_type c,typename S::size_type pos,typename S::size_type x)21 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 
28 template <class S>
test(const S & s,typename S::value_type c,typename S::size_type x)29 TEST_CONSTEXPR_CXX20 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);
32   if (x != S::npos)
33     assert(x + 1 <= s.size());
34 }
35 
36 template <class S>
test_string()37 TEST_CONSTEXPR_CXX20 void test_string() {
38   test(S(""), 'b', 0, S::npos);
39   test(S(""), 'b', 1, S::npos);
40   test(S("abcde"), 'b', 0, S::npos);
41   test(S("abcde"), 'b', 1, 1);
42   test(S("abcde"), 'b', 2, 1);
43   test(S("abcde"), 'b', 4, 1);
44   test(S("abcde"), 'b', 5, 1);
45   test(S("abcde"), 'b', 6, 1);
46   test(S("abcdeabcde"), 'b', 0, S::npos);
47   test(S("abcdeabcde"), 'b', 1, 1);
48   test(S("abcdeabcde"), 'b', 5, 1);
49   test(S("abcdeabcde"), 'b', 9, 6);
50   test(S("abcdeabcde"), 'b', 10, 6);
51   test(S("abcdeabcde"), 'b', 11, 6);
52   test(S("abcdeabcdeabcdeabcde"), 'b', 0, S::npos);
53   test(S("abcdeabcdeabcdeabcde"), 'b', 1, 1);
54   test(S("abcdeabcdeabcdeabcde"), 'b', 10, 6);
55   test(S("abcdeabcdeabcdeabcde"), 'b', 19, 16);
56   test(S("abcdeabcdeabcdeabcde"), 'b', 20, 16);
57   test(S("abcdeabcdeabcdeabcde"), 'b', 21, 16);
58 
59   test(S(""), 'b', S::npos);
60   test(S("abcde"), 'b', 1);
61   test(S("abcdeabcde"), 'b', 6);
62   test(S("abcdeabcdeabcdeabcde"), 'b', 16);
63 }
64 
test()65 TEST_CONSTEXPR_CXX20 bool test() {
66   test_string<std::string>();
67 #if TEST_STD_VER >= 11
68   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
69 #endif
70 
71   return true;
72 }
73 
main(int,char **)74 int main(int, char**) {
75   test();
76 #if TEST_STD_VER > 17
77   static_assert(test());
78 #endif
79 
80   return 0;
81 }
82