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