xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.find/find_char_size.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10 
11 // <string_view>
12 
13 // constexpr size_type find(charT c, size_type pos = 0) const;
14 
15 #include <string_view>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "constexpr_char_traits.h"
20 
21 template <class S>
test(const S & s,typename S::value_type c,typename S::size_type pos,typename S::size_type x)22 void test(const S& s, typename S::value_type c, typename S::size_type pos, typename S::size_type x) {
23   LIBCPP_ASSERT_NOEXCEPT(s.find(c, pos));
24   assert(s.find(c, pos) == x);
25   if (x != S::npos)
26     assert(pos <= x && x + 1 <= s.size());
27 }
28 
29 template <class S>
test(const S & s,typename S::value_type c,typename S::size_type x)30 void test(const S& s, typename S::value_type c, typename S::size_type x) {
31   assert(s.find(c) == x);
32   if (x != S::npos)
33     assert(0 <= x && x + 1 <= s.size());
34 }
35 
main(int,char **)36 int main(int, char**) {
37   {
38     typedef std::string_view S;
39     test(S(""), 'c', 0, S::npos);
40     test(S(""), 'c', 1, S::npos);
41     test(S("abcde"), 'c', 0, 2);
42     test(S("abcde"), 'c', 1, 2);
43     test(S("abcde"), 'c', 2, 2);
44     test(S("abcde"), 'c', 4, S::npos);
45     test(S("abcde"), 'c', 5, S::npos);
46     test(S("abcde"), 'c', 6, S::npos);
47     test(S("abcdeabcde"), 'c', 0, 2);
48     test(S("abcdeabcde"), 'c', 1, 2);
49     test(S("abcdeabcde"), 'c', 5, 7);
50     test(S("abcdeabcde"), 'c', 9, S::npos);
51     test(S("abcdeabcde"), 'c', 10, S::npos);
52     test(S("abcdeabcde"), 'c', 11, S::npos);
53     test(S("abcdeabcdeabcdeabcde"), 'c', 0, 2);
54     test(S("abcdeabcdeabcdeabcde"), 'c', 1, 2);
55     test(S("abcdeabcdeabcdeabcde"), 'c', 10, 12);
56     test(S("abcdeabcdeabcdeabcde"), 'c', 19, S::npos);
57     test(S("abcdeabcdeabcdeabcde"), 'c', 20, S::npos);
58     test(S("abcdeabcdeabcdeabcde"), 'c', 21, S::npos);
59 
60     test(S(""), 'c', S::npos);
61     test(S("abcde"), 'c', 2);
62     test(S("abcdeabcde"), 'c', 2);
63     test(S("abcdeabcdeabcdeabcde"), 'c', 2);
64   }
65 
66 #if TEST_STD_VER > 11
67   {
68     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
69     constexpr SV sv1;
70     constexpr SV sv2{"abcde", 5};
71 
72     static_assert(sv1.find('c', 0) == SV::npos, "");
73     static_assert(sv1.find('c', 1) == SV::npos, "");
74     static_assert(sv2.find('c', 0) == 2, "");
75     static_assert(sv2.find('c', 1) == 2, "");
76     static_assert(sv2.find('c', 2) == 2, "");
77     static_assert(sv2.find('c', 3) == SV::npos, "");
78     static_assert(sv2.find('c', 4) == SV::npos, "");
79   }
80 #endif
81 
82   return 0;
83 }
84