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