| 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 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 |
|
| 11 |
// template<container-compatible-range<charT> R> |
| 12 |
// constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator()); // since C++23 |
| 13 |
|
| 14 |
#include <algorithm> |
| 15 |
#include <string> |
| 16 |
#include <utility> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "../../../containers/from_range_helpers.h" |
| 20 |
#include "../../../containers/sequences/from_range_sequence_containers.h" |
| 21 |
#include "test_macros.h" |
| 22 |
#include "asan_testing.h" |
| 23 |
|
| 24 |
template <class Container, class Range, class Alloc> |
| 25 |
concept StringHasFromRangeAllocCtr = |
| 26 |
requires(Range&& range, const Alloc& alloc) { Container(std::from_range, std::forward<Range>(range), alloc); }; |
| 27 |
|
| 28 |
constexpr bool test_constraints() { |
| 29 |
// (from_range, range) |
| 30 |
// |
| 31 |
// Input range with the same value type. |
| 32 |
static_assert(HasFromRangeCtr<std::string, InputRange<char>>); |
| 33 |
// Input range with a convertible value type. |
| 34 |
static_assert(HasFromRangeCtr<std::string, InputRange<int>>); |
| 35 |
// Input range with a non-convertible value type. |
| 36 |
static_assert(!HasFromRangeCtr<std::string, InputRange<Empty>>); |
| 37 |
// Not an input range. |
| 38 |
static_assert(!HasFromRangeCtr<std::string, InputRangeNotDerivedFrom>); |
| 39 |
static_assert(!HasFromRangeCtr<std::string, InputRangeNotIndirectlyReadable>); |
| 40 |
static_assert(!HasFromRangeCtr<std::string, InputRangeNotInputOrOutputIterator>); |
| 41 |
|
| 42 |
// (from_range, range, alloc) |
| 43 |
// |
| 44 |
// Input range with the same value type. |
| 45 |
using Alloc = test_allocator<char>; |
| 46 |
using StringWithAlloc = std::basic_string<char, std::char_traits<char>, Alloc>; |
| 47 |
static_assert(StringHasFromRangeAllocCtr<StringWithAlloc, InputRange<char>, Alloc>); |
| 48 |
// Input range with a convertible value type. |
| 49 |
static_assert(StringHasFromRangeAllocCtr<StringWithAlloc, InputRange<int>, Alloc>); |
| 50 |
// Input range with a non-convertible value type. |
| 51 |
static_assert(!StringHasFromRangeAllocCtr<StringWithAlloc, InputRange<Empty>, Alloc>); |
| 52 |
// Not an input range. |
| 53 |
static_assert(!StringHasFromRangeAllocCtr<StringWithAlloc, InputRangeNotDerivedFrom, Alloc>); |
| 54 |
static_assert(!StringHasFromRangeAllocCtr<StringWithAlloc, InputRangeNotIndirectlyReadable, Alloc>); |
| 55 |
static_assert(!StringHasFromRangeAllocCtr<StringWithAlloc, InputRangeNotInputOrOutputIterator, Alloc>); |
| 56 |
// Not an allocator. |
| 57 |
static_assert(!StringHasFromRangeAllocCtr<StringWithAlloc, InputRange<char>, Empty>); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
template <class Iter, class Sent, class Alloc> |
| 63 |
constexpr void test_with_input(std::vector<char> input) { |
| 64 |
auto b = Iter(input.data()); |
| 65 |
auto e = Iter(input.data() + input.size()); |
| 66 |
std::ranges::subrange in(std::move(b), Sent(std::move(e))); |
| 67 |
|
| 68 |
{ // (range) |
| 69 |
std::string c(std::from_range, in); |
| 70 |
|
| 71 |
LIBCPP_ASSERT(c.__invariants()); |
| 72 |
assert(c.size() == static_cast<std::size_t>(std::distance(c.begin(), c.end()))); |
| 73 |
assert(std::ranges::equal(in, c)); |
| 74 |
LIBCPP_ASSERT(is_string_asan_correct(c)); |
| 75 |
} |
| 76 |
|
| 77 |
{ // (range, allocator) |
| 78 |
Alloc alloc; |
| 79 |
std::basic_string<char, std::char_traits<char>, Alloc> c(std::from_range, in, alloc); |
| 80 |
|
| 81 |
LIBCPP_ASSERT(c.__invariants()); |
| 82 |
assert(c.get_allocator() == alloc); |
| 83 |
assert(c.size() == static_cast<std::size_t>(std::distance(c.begin(), c.end()))); |
| 84 |
assert(std::ranges::equal(in, c)); |
| 85 |
LIBCPP_ASSERT(is_string_asan_correct(c)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void test_string_exception_safety_throwing_allocator() { |
| 90 |
#if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 91 |
try { |
| 92 |
ThrowingAllocator<char> alloc; |
| 93 |
|
| 94 |
globalMemCounter.reset(); |
| 95 |
// Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used. |
| 96 |
std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c( |
| 97 |
std::from_range, std::vector<char>(64, 'A'), alloc); |
| 98 |
assert(false); // The constructor call should throw. |
| 99 |
|
| 100 |
} catch (int) { |
| 101 |
assert(globalMemCounter.new_called == globalMemCounter.delete_called); |
| 102 |
} |
| 103 |
#endif |
| 104 |
} |
| 105 |
|
| 106 |
constexpr bool test_inputs() { |
| 107 |
for_all_iterators_and_allocators<char>([]<class Iter, class Sent, class Alloc>() { |
| 108 |
// Shorter input -- SSO. |
| 109 |
test_with_input<Iter, Sent, Alloc>({'a', 'b', 'c', 'd', 'e'}); |
| 110 |
// Longer input -- no SSO. |
| 111 |
test_with_input<Iter, Sent, Alloc>(std::vector<char>(64, 'A')); |
| 112 |
// Empty input. |
| 113 |
test_with_input<Iter, Sent, Alloc>({}); |
| 114 |
// Single-element input. |
| 115 |
test_with_input<Iter, Sent, Alloc>({'a'}); |
| 116 |
}); |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
int main(int, char**) { |
| 122 |
test_inputs(); |
| 123 |
static_assert(test_inputs()); |
| 124 |
|
| 125 |
static_assert(test_constraints()); |
| 126 |
|
| 127 |
// Note: `test_exception_safety_throwing_copy` doesn't apply because copying a `char` cannot throw. |
| 128 |
test_string_exception_safety_throwing_allocator(); |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |