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