xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_find/char_size.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
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(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, typename S::size_type x) {
22   LIBCPP_ASSERT_NOEXCEPT(s.find(c, pos));
23   assert(s.find(c, pos) == x);
24   if (x != S::npos)
25     assert(pos <= x && x + 1 <= s.size());
26 }
27 
28 template <class S>
test(const S & s,typename S::value_type c,typename S::size_type x)29 TEST_CONSTEXPR_CXX20 void test(const S& s, typename S::value_type c, typename S::size_type x) {
30   LIBCPP_ASSERT_NOEXCEPT(s.find(c));
31   assert(s.find(c) == x);
32   if (x != S::npos)
33     assert(0 <= x && x + 1 <= s.size());
34 }
35 
36 template <class S>
test_string()37 TEST_CONSTEXPR_CXX20 void test_string() {
38   test(S(""), 'c', 0, S::npos);
39   test(S(""), 'c', 1, S::npos);
40   test(S("abcde"), 'c', 0, 2);
41   test(S("abcde"), 'c', 1, 2);
42   test(S("abcde"), 'c', 2, 2);
43   test(S("abcde"), 'c', 4, S::npos);
44   test(S("abcde"), 'c', 5, S::npos);
45   test(S("abcde"), 'c', 6, S::npos);
46   test(S("abcdeabcde"), 'c', 0, 2);
47   test(S("abcdeabcde"), 'c', 1, 2);
48   test(S("abcdeabcde"), 'c', 5, 7);
49   test(S("abcdeabcde"), 'c', 9, S::npos);
50   test(S("abcdeabcde"), 'c', 10, S::npos);
51   test(S("abcdeabcde"), 'c', 11, S::npos);
52   test(S("abcdeabcdeabcdeabcde"), 'c', 0, 2);
53   test(S("abcdeabcdeabcdeabcde"), 'c', 1, 2);
54   test(S("abcdeabcdeabcdeabcde"), 'c', 10, 12);
55   test(S("abcdeabcdeabcdeabcde"), 'c', 19, S::npos);
56   test(S("abcdeabcdeabcdeabcde"), 'c', 20, S::npos);
57   test(S("abcdeabcdeabcdeabcde"), 'c', 21, S::npos);
58 
59   test(S(""), 'c', S::npos);
60   test(S("abcde"), 'c', 2);
61   test(S("abcdeabcde"), 'c', 2);
62   test(S("abcdeabcdeabcdeabcde"), 'c', 2);
63 }
64 
test()65 TEST_CONSTEXPR_CXX20 bool test() {
66   test_string<std::string>();
67 #if TEST_STD_VER >= 11
68   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
69 #endif
70 
71   return true;
72 }
73 
main(int,char **)74 int main(int, char**) {
75   test();
76 #if TEST_STD_VER > 17
77   static_assert(test());
78 #endif
79 
80   return 0;
81 }
82