xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.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 // <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 }
106 
107 TEST_CONSTEXPR_CXX20 bool test() {
108   test_string<std::string>();
109 #if TEST_STD_VER >= 11
110   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
111 #endif
112 
113   return true;
114 }
115 
116 TEST_CONSTEXPR_CXX20 bool test_alloc() {
117   {
118     using alloc  = test_allocator<char>;
119     using string = std::basic_string<char, std::char_traits<char>, alloc>;
120     test_allocator_statistics stats;
121     {
122       string str = string(alloc(&stats));
123       stats      = test_allocator_statistics();
124       (void)str.substr();
125       assert(stats.moved == 0);
126       assert(stats.copied == 0);
127     }
128     {
129       string str = string(alloc(&stats));
130       stats      = test_allocator_statistics();
131       (void)std::move(str).substr();
132       assert(stats.moved == 0);
133       assert(stats.copied == 0);
134     }
135   }
136 
137   return true;
138 }
139 
140 int main(int, char**) {
141   test();
142   test_alloc();
143 #if TEST_STD_VER > 17
144   static_assert(test());
145   static_assert(test_alloc());
146 #endif
147 
148   return 0;
149 }
150