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(const basic_string& str, 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>
test(const S & s,const S & str,typename S::size_type pos,typename S::size_type x)20 TEST_CONSTEXPR_CXX20 void test(const S& s, const S& str, typename S::size_type pos, typename S::size_type x) {
21   LIBCPP_ASSERT_NOEXCEPT(s.find_last_not_of(str, pos));
22   assert(s.find_last_not_of(str, pos) == x);
23   if (x != S::npos)
24     assert(x <= pos && x < s.size());
25 }
26 
27 template <class S>
test(const S & s,const S & str,typename S::size_type x)28 TEST_CONSTEXPR_CXX20 void test(const S& s, const S& str, typename S::size_type x) {
29   LIBCPP_ASSERT_NOEXCEPT(s.find_last_not_of(str));
30   assert(s.find_last_not_of(str) == x);
31   if (x != S::npos)
32     assert(x < s.size());
33 }
34 
35 template <class S>
test0()36 TEST_CONSTEXPR_CXX20 void test0() {
37   test(S(""), S(""), 0, S::npos);
38   test(S(""), S("laenf"), 0, S::npos);
39   test(S(""), S("pqlnkmbdjo"), 0, S::npos);
40   test(S(""), S("qkamfogpnljdcshbreti"), 0, S::npos);
41   test(S(""), S(""), 1, S::npos);
42   test(S(""), S("bjaht"), 1, S::npos);
43   test(S(""), S("hjlcmgpket"), 1, S::npos);
44   test(S(""), S("htaobedqikfplcgjsmrn"), 1, S::npos);
45   test(S("fodgq"), S(""), 0, 0);
46   test(S("qanej"), S("dfkap"), 0, 0);
47   test(S("clbao"), S("ihqrfebgad"), 0, 0);
48   test(S("mekdn"), S("ngtjfcalbseiqrphmkdo"), 0, S::npos);
49   test(S("srdfq"), S(""), 1, 1);
50   test(S("oemth"), S("ikcrq"), 1, 1);
51   test(S("cdaih"), S("dmajblfhsg"), 1, 0);
52   test(S("qohtk"), S("oqftjhdmkgsblacenirp"), 1, S::npos);
53   test(S("cshmd"), S(""), 2, 2);
54   test(S("lhcdo"), S("oebqi"), 2, 2);
55   test(S("qnsoh"), S("kojhpmbsfe"), 2, 1);
56   test(S("pkrof"), S("acbsjqogpltdkhinfrem"), 2, S::npos);
57   test(S("fmtsp"), S(""), 4, 4);
58   test(S("khbpm"), S("aobjd"), 4, 4);
59   test(S("pbsji"), S("pcbahntsje"), 4, 4);
60   test(S("mprdj"), S("fhepcrntkoagbmldqijs"), 4, S::npos);
61   test(S("eqmpa"), S(""), 5, 4);
62   test(S("omigs"), S("kocgb"), 5, 4);
63   test(S("onmje"), S("fbslrjiqkm"), 5, 4);
64   test(S("oqmrj"), S("jeidpcmalhfnqbgtrsko"), 5, S::npos);
65   test(S("schfa"), S(""), 6, 4);
66   test(S("igdsc"), S("qngpd"), 6, 4);
67   test(S("brqgo"), S("rodhqklgmb"), 6, S::npos);
68   test(S("tnrph"), S("thdjgafrlbkoiqcspmne"), 6, S::npos);
69   test(S("hcjitbfapl"), S(""), 0, 0);
70   test(S("daiprenocl"), S("ashjd"), 0, S::npos);
71   test(S("litpcfdghe"), S("mgojkldsqh"), 0, S::npos);
72   test(S("aidjksrolc"), S("imqnaghkfrdtlopbjesc"), 0, S::npos);
73   test(S("qpghtfbaji"), S(""), 1, 1);
74   test(S("gfshlcmdjr"), S("nadkh"), 1, 1);
75   test(S("nkodajteqp"), S("ofdrqmkebl"), 1, 0);
76   test(S("gbmetiprqd"), S("bdfjqgatlksriohemnpc"), 1, S::npos);
77   test(S("crnklpmegd"), S(""), 5, 5);
78   test(S("jsbtafedoc"), S("prqgn"), 5, 5);
79   test(S("qnmodrtkeb"), S("pejafmnokr"), 5, 4);
80   test(S("cpebqsfmnj"), S("odnqkgijrhabfmcestlp"), 5, S::npos);
81   test(S("lmofqdhpki"), S(""), 9, 9);
82   test(S("hnefkqimca"), S("rtjpa"), 9, 8);
83   test(S("drtasbgmfp"), S("ktsrmnqagd"), 9, 9);
84   test(S("lsaijeqhtr"), S("rtdhgcisbnmoaqkfpjle"), 9, S::npos);
85   test(S("elgofjmbrq"), S(""), 10, 9);
86   test(S("mjqdgalkpc"), S("dplqa"), 10, 9);
87   test(S("kthqnfcerm"), S("dkacjoptns"), 10, 9);
88   test(S("dfsjhanorc"), S("hqfimtrgnbekpdcsjalo"), 10, S::npos);
89   test(S("eqsgalomhb"), S(""), 11, 9);
90   test(S("akiteljmoh"), S("lofbc"), 11, 9);
91   test(S("hlbdfreqjo"), S("astoegbfpn"), 11, 8);
92   test(S("taqobhlerg"), S("pdgreqomsncafklhtibj"), 11, S::npos);
93   test(S("snafbdlghrjkpqtoceim"), S(""), 0, 0);
94   test(S("aemtbrgcklhndjisfpoq"), S("lbtqd"), 0, 0);
95   test(S("pnracgfkjdiholtbqsem"), S("tboimldpjh"), 0, S::npos);
96   test(S("dicfltehbsgrmojnpkaq"), S("slcerthdaiqjfnobgkpm"), 0, S::npos);
97   test(S("jlnkraeodhcspfgbqitm"), S(""), 1, 1);
98   test(S("lhosrngtmfjikbqpcade"), S("aqibs"), 1, 1);
99   test(S("rbtaqjhgkneisldpmfoc"), S("gtfblmqinc"), 1, 0);
100   test(S("gpifsqlrdkbonjtmheca"), S("mkqpbtdalgniorhfescj"), 1, S::npos);
101   test(S("hdpkobnsalmcfijregtq"), S(""), 10, 10);
102   test(S("jtlshdgqaiprkbcoenfm"), S("pblas"), 10, 9);
103   test(S("fkdrbqltsgmcoiphneaj"), S("arosdhcfme"), 10, 9);
104   test(S("crsplifgtqedjohnabmk"), S("blkhjeogicatqfnpdmsr"), 10, S::npos);
105   test(S("niptglfbosehkamrdqcj"), S(""), 19, 19);
106   test(S("copqdhstbingamjfkler"), S("djkqc"), 19, 19);
107   test(S("mrtaefilpdsgocnhqbjk"), S("lgokshjtpb"), 19, 16);
108   test(S("kojatdhlcmigpbfrqnes"), S("bqjhtkfepimcnsgrlado"), 19, S::npos);
109   test(S("eaintpchlqsbdgrkjofm"), S(""), 20, 19);
110   test(S("gjnhidfsepkrtaqbmclo"), S("nocfa"), 20, 18);
111   test(S("spocfaktqdbiejlhngmr"), S("bgtajmiedc"), 20, 19);
112   test(S("rphmlekgfscndtaobiqj"), S("lsckfnqgdahejiopbtmr"), 20, S::npos);
113   test(S("liatsqdoegkmfcnbhrpj"), S(""), 21, 19);
114   test(S("binjagtfldkrspcomqeh"), S("gfsrt"), 21, 19);
115   test(S("latkmisecnorjbfhqpdg"), S("pfsocbhjtm"), 21, 19);
116   test(S("lecfratdjkhnsmqpoigb"), S("tpflmdnoicjgkberhqsa"), 21, S::npos);
117 }
118 
119 template <class S>
test1()120 TEST_CONSTEXPR_CXX20 void test1() {
121   test(S(""), S(""), S::npos);
122   test(S(""), S("laenf"), S::npos);
123   test(S(""), S("pqlnkmbdjo"), S::npos);
124   test(S(""), S("qkamfogpnljdcshbreti"), S::npos);
125   test(S("nhmko"), S(""), 4);
126   test(S("lahfb"), S("irkhs"), 4);
127   test(S("gmfhd"), S("kantesmpgj"), 4);
128   test(S("odaft"), S("oknlrstdpiqmjbaghcfe"), S::npos);
129   test(S("eolhfgpjqk"), S(""), 9);
130   test(S("nbatdlmekr"), S("bnrpe"), 8);
131   test(S("jdmciepkaq"), S("jtdaefblso"), 9);
132   test(S("hkbgspoflt"), S("oselktgbcapndfjihrmq"), S::npos);
133   test(S("gprdcokbnjhlsfmtieqa"), S(""), 19);
134   test(S("qjghlnftcaismkropdeb"), S("bjaht"), 18);
135   test(S("pnalfrdtkqcmojiesbhg"), S("hjlcmgpket"), 17);
136   test(S("pniotcfrhqsmgdkjbael"), S("htaobedqikfplcgjsmrn"), S::npos);
137 }
138 
139 template <class S>
test_string()140 TEST_CONSTEXPR_CXX20 void test_string() {
141   test0<S>();
142   test1<S>();
143 }
144 
test()145 TEST_CONSTEXPR_CXX20 bool test() {
146   test_string<std::string>();
147 #if TEST_STD_VER >= 11
148   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
149   { // LWG 2946
150     std::string s = " !";
151     assert(s.find_last_not_of({"abc", 1}) == s.size() - 1);
152   }
153 #endif
154 
155   return true;
156 }
157 
main(int,char **)158 int main(int, char**) {
159   test();
160 #if TEST_STD_VER > 17
161   static_assert(test());
162 #endif
163 
164   return 0;
165 }
166