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