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 find_last_not_of(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,
22      typename S::size_type x)
23 {
24     LIBCPP_ASSERT_NOEXCEPT(s.find_last_not_of(c, pos));
25     assert(s.find_last_not_of(c, pos) == x);
26     if (x != S::npos)
27         assert(x <= pos && x < s.size());
28 }
29 
30 template <class S>
31 TEST_CONSTEXPR_CXX20 void
test(const S & s,typename S::value_type c,typename S::size_type x)32 test(const S& s, typename S::value_type c, typename S::size_type x)
33 {
34     LIBCPP_ASSERT_NOEXCEPT(s.find_last_not_of(c));
35     assert(s.find_last_not_of(c) == x);
36     if (x != S::npos)
37         assert(x < s.size());
38 }
39 
test()40 TEST_CONSTEXPR_CXX20 bool test() {
41   {
42     typedef std::string S;
43     test(S(""), 'i', 0, S::npos);
44     test(S(""), 'i', 1, S::npos);
45     test(S("kitcj"), 'i', 0, 0);
46     test(S("qkamf"), 'i', 1, 1);
47     test(S("nhmko"), 'i', 2, 2);
48     test(S("tpsaf"), 'i', 4, 4);
49     test(S("lahfb"), 'i', 5, 4);
50     test(S("irkhs"), 'i', 6, 4);
51     test(S("gmfhdaipsr"), 'i', 0, 0);
52     test(S("kantesmpgj"), 'i', 1, 1);
53     test(S("odaftiegpm"), 'i', 5, 4);
54     test(S("oknlrstdpi"), 'i', 9, 8);
55     test(S("eolhfgpjqk"), 'i', 10, 9);
56     test(S("pcdrofikas"), 'i', 11, 9);
57     test(S("nbatdlmekrgcfqsophij"), 'i', 0, 0);
58     test(S("bnrpehidofmqtcksjgla"), 'i', 1, 1);
59     test(S("jdmciepkaqgotsrfnhlb"), 'i', 10, 10);
60     test(S("jtdaefblsokrmhpgcnqi"), 'i', 19, 18);
61     test(S("hkbgspofltajcnedqmri"), 'i', 20, 18);
62     test(S("oselktgbcapndfjihrmq"), 'i', 21, 19);
63 
64     test(S(""), 'i', S::npos);
65     test(S("csope"), 'i', 4);
66     test(S("gfsmthlkon"), 'i', 9);
67     test(S("laenfsbridchgotmkqpj"), 'i', 19);
68   }
69 #if TEST_STD_VER >= 11
70   {
71     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
72     test(S(""), 'i', 0, S::npos);
73     test(S(""), 'i', 1, S::npos);
74     test(S("kitcj"), 'i', 0, 0);
75     test(S("qkamf"), 'i', 1, 1);
76     test(S("nhmko"), 'i', 2, 2);
77     test(S("tpsaf"), 'i', 4, 4);
78     test(S("lahfb"), 'i', 5, 4);
79     test(S("irkhs"), 'i', 6, 4);
80     test(S("gmfhdaipsr"), 'i', 0, 0);
81     test(S("kantesmpgj"), 'i', 1, 1);
82     test(S("odaftiegpm"), 'i', 5, 4);
83     test(S("oknlrstdpi"), 'i', 9, 8);
84     test(S("eolhfgpjqk"), 'i', 10, 9);
85     test(S("pcdrofikas"), 'i', 11, 9);
86     test(S("nbatdlmekrgcfqsophij"), 'i', 0, 0);
87     test(S("bnrpehidofmqtcksjgla"), 'i', 1, 1);
88     test(S("jdmciepkaqgotsrfnhlb"), 'i', 10, 10);
89     test(S("jtdaefblsokrmhpgcnqi"), 'i', 19, 18);
90     test(S("hkbgspofltajcnedqmri"), 'i', 20, 18);
91     test(S("oselktgbcapndfjihrmq"), 'i', 21, 19);
92 
93     test(S(""), 'i', S::npos);
94     test(S("csope"), 'i', 4);
95     test(S("gfsmthlkon"), 'i', 9);
96     test(S("laenfsbridchgotmkqpj"), 'i', 19);
97   }
98 #endif
99 
100   return true;
101 }
102 
main(int,char **)103 int main(int, char**)
104 {
105   test();
106 #if TEST_STD_VER > 17
107   static_assert(test());
108 #endif
109 
110   return 0;
111 }
112