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