xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp (revision 29378ab24b98137b4959034a0882c3bbd97c46e4)
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 // basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20, removed in C++23
12 // basic_string substr(size_type pos = 0, size_type n = npos) const&; // since in C++23
13 
14 #include <string>
15 #include <stdexcept>
16 #include <algorithm>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 
22 template <class S>
23 TEST_CONSTEXPR_CXX20 void
24 test(const S& s, typename S::size_type pos, typename S::size_type n)
25 {
26     if (pos <= s.size())
27     {
28         S str = s.substr(pos, n);
29         LIBCPP_ASSERT(str.__invariants());
30         assert(pos <= s.size());
31         typename S::size_type rlen = std::min(n, s.size() - pos);
32         assert(str.size() == rlen);
33         assert(S::traits_type::compare(s.data()+pos, str.data(), rlen) == 0);
34     }
35 #ifndef TEST_HAS_NO_EXCEPTIONS
36     else if (!TEST_IS_CONSTANT_EVALUATED)
37     {
38         try
39         {
40             S str = s.substr(pos, n);
41             assert(false);
42         }
43         catch (std::out_of_range&)
44         {
45             assert(pos > s.size());
46         }
47     }
48 #endif
49 }
50 
51 template <class S>
52 TEST_CONSTEXPR_CXX20 void test_string() {
53   test(S(""), 0, 0);
54   test(S(""), 1, 0);
55   test(S("pniot"), 0, 0);
56   test(S("htaob"), 0, 1);
57   test(S("fodgq"), 0, 2);
58   test(S("hpqia"), 0, 4);
59   test(S("qanej"), 0, 5);
60   test(S("dfkap"), 1, 0);
61   test(S("clbao"), 1, 1);
62   test(S("ihqrf"), 1, 2);
63   test(S("mekdn"), 1, 3);
64   test(S("ngtjf"), 1, 4);
65   test(S("srdfq"), 2, 0);
66   test(S("qkdrs"), 2, 1);
67   test(S("ikcrq"), 2, 2);
68   test(S("cdaih"), 2, 3);
69   test(S("dmajb"), 4, 0);
70   test(S("karth"), 4, 1);
71   test(S("lhcdo"), 5, 0);
72   test(S("acbsj"), 6, 0);
73   test(S("pbsjikaole"), 0, 0);
74   test(S("pcbahntsje"), 0, 1);
75   test(S("mprdjbeiak"), 0, 5);
76   test(S("fhepcrntko"), 0, 9);
77   test(S("eqmpaidtls"), 0, 10);
78   test(S("joidhalcmq"), 1, 0);
79   test(S("omigsphflj"), 1, 1);
80   test(S("kocgbphfji"), 1, 4);
81   test(S("onmjekafbi"), 1, 8);
82   test(S("fbslrjiqkm"), 1, 9);
83   test(S("oqmrjahnkg"), 5, 0);
84   test(S("jeidpcmalh"), 5, 1);
85   test(S("schfalibje"), 5, 2);
86   test(S("crliponbqe"), 5, 4);
87   test(S("igdscopqtm"), 5, 5);
88   test(S("qngpdkimlc"), 9, 0);
89   test(S("thdjgafrlb"), 9, 1);
90   test(S("hcjitbfapl"), 10, 0);
91   test(S("mgojkldsqh"), 11, 0);
92   test(S("gfshlcmdjreqipbontak"), 0, 0);
93   test(S("nadkhpfemgclosibtjrq"), 0, 1);
94   test(S("nkodajteqplrbifhmcgs"), 0, 10);
95   test(S("ofdrqmkeblthacpgijsn"), 0, 19);
96   test(S("gbmetiprqdoasckjfhln"), 0, 20);
97   test(S("bdfjqgatlksriohemnpc"), 1, 0);
98   test(S("crnklpmegdqfiashtojb"), 1, 1);
99   test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
100   test(S("jsbtafedocnirgpmkhql"), 1, 18);
101   test(S("prqgnlbaejsmkhdctoif"), 1, 19);
102   test(S("qnmodrtkebhpasifgcjl"), 10, 0);
103   test(S("pejafmnokrqhtisbcdgl"), 10, 1);
104   test(S("cpebqsfmnjdolhkratgi"), 10, 5);
105   test(S("odnqkgijrhabfmcestlp"), 10, 9);
106   test(S("lmofqdhpkibagnrcjste"), 10, 10);
107   test(S("lgjqketopbfahrmnsicd"), 19, 0);
108   test(S("ktsrmnqagdecfhijpobl"), 19, 1);
109   test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
110   test(S("dplqartnfgejichmoskb"), 21, 0);
111 }
112 
113 TEST_CONSTEXPR_CXX20 bool test() {
114   test_string<std::string>();
115 #if TEST_STD_VER >= 11
116   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
117 #endif
118 
119   return true;
120 }
121 
122 int main(int, char**)
123 {
124   test();
125 #if TEST_STD_VER > 17
126   static_assert(test());
127 #endif
128 
129   return 0;
130 }
131