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(charT c, 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,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_first_of(c, pos));
25 assert(s.find_first_of(c, 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,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_first_of(c));
35 assert(s.find_first_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(""), 'e', 0, S::npos);
44 test(S(""), 'e', 1, S::npos);
45 test(S("kitcj"), 'e', 0, S::npos);
46 test(S("qkamf"), 'e', 1, S::npos);
47 test(S("nhmko"), 'e', 2, S::npos);
48 test(S("tpsaf"), 'e', 4, S::npos);
49 test(S("lahfb"), 'e', 5, S::npos);
50 test(S("irkhs"), 'e', 6, S::npos);
51 test(S("gmfhdaipsr"), 'e', 0, S::npos);
52 test(S("kantesmpgj"), 'e', 1, 4);
53 test(S("odaftiegpm"), 'e', 5, 6);
54 test(S("oknlrstdpi"), 'e', 9, S::npos);
55 test(S("eolhfgpjqk"), 'e', 10, S::npos);
56 test(S("pcdrofikas"), 'e', 11, S::npos);
57 test(S("nbatdlmekrgcfqsophij"), 'e', 0, 7);
58 test(S("bnrpehidofmqtcksjgla"), 'e', 1, 4);
59 test(S("jdmciepkaqgotsrfnhlb"), 'e', 10, S::npos);
60 test(S("jtdaefblsokrmhpgcnqi"), 'e', 19, S::npos);
61 test(S("hkbgspofltajcnedqmri"), 'e', 20, S::npos);
62 test(S("oselktgbcapndfjihrmq"), 'e', 21, S::npos);
63
64 test(S(""), 'e', S::npos);
65 test(S("csope"), 'e', 4);
66 test(S("gfsmthlkon"), 'e', S::npos);
67 test(S("laenfsbridchgotmkqpj"), 'e', 2);
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(""), 'e', 0, S::npos);
73 test(S(""), 'e', 1, S::npos);
74 test(S("kitcj"), 'e', 0, S::npos);
75 test(S("qkamf"), 'e', 1, S::npos);
76 test(S("nhmko"), 'e', 2, S::npos);
77 test(S("tpsaf"), 'e', 4, S::npos);
78 test(S("lahfb"), 'e', 5, S::npos);
79 test(S("irkhs"), 'e', 6, S::npos);
80 test(S("gmfhdaipsr"), 'e', 0, S::npos);
81 test(S("kantesmpgj"), 'e', 1, 4);
82 test(S("odaftiegpm"), 'e', 5, 6);
83 test(S("oknlrstdpi"), 'e', 9, S::npos);
84 test(S("eolhfgpjqk"), 'e', 10, S::npos);
85 test(S("pcdrofikas"), 'e', 11, S::npos);
86 test(S("nbatdlmekrgcfqsophij"), 'e', 0, 7);
87 test(S("bnrpehidofmqtcksjgla"), 'e', 1, 4);
88 test(S("jdmciepkaqgotsrfnhlb"), 'e', 10, S::npos);
89 test(S("jtdaefblsokrmhpgcnqi"), 'e', 19, S::npos);
90 test(S("hkbgspofltajcnedqmri"), 'e', 20, S::npos);
91 test(S("oselktgbcapndfjihrmq"), 'e', 21, S::npos);
92
93 test(S(""), 'e', S::npos);
94 test(S("csope"), 'e', 4);
95 test(S("gfsmthlkon"), 'e', S::npos);
96 test(S("laenfsbridchgotmkqpj"), 'e', 2);
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