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_last_of(charT c, size_type pos = npos) const;
14
15#include <string_view>
16#include <cassert>
17
18#include "test_macros.h"
19#include "constexpr_char_traits.h"
20
21template <class S>
22void test(const S& s, typename S::value_type c, typename S::size_type pos, typename S::size_type x) {
23 LIBCPP_ASSERT_NOEXCEPT(s.find_last_of(c, pos));
24 assert(s.find_last_of(c, pos) == x);
25 if (x != S::npos)
26 assert(x <= pos && x < s.size());
27}
28
29template <class S>
30void test(const S& s, typename S::value_type c, typename S::size_type x) {
31 LIBCPP_ASSERT_NOEXCEPT(s.find_last_of(c));
32 assert(s.find_last_of(c) == x);
33 if (x != S::npos)
34 assert(x < s.size());
35}
36
37int main(int, char**) {
38 {
39 typedef std::string_view S;
40 test(s: S(""), c: 'm', pos: 0, x: S::npos);
41 test(s: S(""), c: 'm', pos: 1, x: S::npos);
42 test(s: S("kitcj"), c: 'm', pos: 0, x: S::npos);
43 test(s: S("qkamf"), c: 'm', pos: 1, x: S::npos);
44 test(s: S("nhmko"), c: 'm', pos: 2, x: 2);
45 test(s: S("tpsaf"), c: 'm', pos: 4, x: S::npos);
46 test(s: S("lahfb"), c: 'm', pos: 5, x: S::npos);
47 test(s: S("irkhs"), c: 'm', pos: 6, x: S::npos);
48 test(s: S("gmfhdaipsr"), c: 'm', pos: 0, x: S::npos);
49 test(s: S("kantesmpgj"), c: 'm', pos: 1, x: S::npos);
50 test(s: S("odaftiegpm"), c: 'm', pos: 5, x: S::npos);
51 test(s: S("oknlrstdpi"), c: 'm', pos: 9, x: S::npos);
52 test(s: S("eolhfgpjqk"), c: 'm', pos: 10, x: S::npos);
53 test(s: S("pcdrofikas"), c: 'm', pos: 11, x: S::npos);
54 test(s: S("nbatdlmekrgcfqsophij"), c: 'm', pos: 0, x: S::npos);
55 test(s: S("bnrpehidofmqtcksjgla"), c: 'm', pos: 1, x: S::npos);
56 test(s: S("jdmciepkaqgotsrfnhlb"), c: 'm', pos: 10, x: 2);
57 test(s: S("jtdaefblsokrmhpgcnqi"), c: 'm', pos: 19, x: 12);
58 test(s: S("hkbgspofltajcnedqmri"), c: 'm', pos: 20, x: 17);
59 test(s: S("oselktgbcapndfjihrmq"), c: 'm', pos: 21, x: 18);
60
61 test(s: S(""), c: 'm', x: S::npos);
62 test(s: S("csope"), c: 'm', x: S::npos);
63 test(s: S("gfsmthlkon"), c: 'm', x: 3);
64 test(s: S("laenfsbridchgotmkqpj"), c: 'm', x: 15);
65 }
66
67#if TEST_STD_VER > 11
68 {
69 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
70 constexpr SV sv1;
71 constexpr SV sv2{"abcde", 5};
72
73 static_assert(sv1.find_last_of('i', 0) == SV::npos, "");
74 static_assert(sv1.find_last_of('i', 1) == SV::npos, "");
75 static_assert(sv2.find_last_of('a', 0) == 0, "");
76 static_assert(sv2.find_last_of('a', 1) == 0, "");
77 static_assert(sv2.find_last_of('e', 5) == 4, "");
78 }
79#endif
80
81 return 0;
82}
83

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