| 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 | // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000 |
| 11 | |
| 12 | // template<container-compatible-range<T> R> |
| 13 | // constexpr void assign_range(R&& rg); // C++23 |
| 14 | |
| 15 | #include <sstream> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "../../insert_range_sequence_containers.h" |
| 19 | #include "asan_testing.h" |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | // Tested cases: |
| 23 | // - different kinds of assignments (assigning an {empty/one-element/mid-sized/long range} to an |
| 24 | // {empty/one-element/full} container); |
| 25 | // - assigning move-only elements; |
| 26 | // - an exception is thrown when copying the elements or when allocating new elements. |
| 27 | constexpr bool test() { |
| 28 | static_assert(test_constraints_assign_range<std::vector, int, double>()); |
| 29 | |
| 30 | for_all_iterators_and_allocators<int, const int*>(f: []<class Iter, class Sent, class Alloc>() { |
| 31 | test_sequence_assign_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) { |
| 32 | LIBCPP_ASSERT(c.__invariants()); |
| 33 | LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); |
| 34 | }); |
| 35 | }); |
| 36 | test_sequence_assign_range_move_only<std::vector>(); |
| 37 | |
| 38 | { // Vector may or may not need to reallocate because of the assignment -- make sure to test both cases. |
| 39 | { // Ensure reallocation happens. |
| 40 | int in[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10}; |
| 41 | std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 42 | v.shrink_to_fit(); |
| 43 | assert(v.capacity() < v.size() + std::ranges::size(in)); |
| 44 | |
| 45 | v.assign_range(in); |
| 46 | assert(std::ranges::equal(v, in)); |
| 47 | } |
| 48 | |
| 49 | { // Ensure no reallocation happens -- the input range is shorter than the vector. |
| 50 | int in[] = {-1, -2, -3, -4, -5}; |
| 51 | std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 52 | |
| 53 | v.assign_range(in); |
| 54 | assert(std::ranges::equal(v, in)); |
| 55 | } |
| 56 | |
| 57 | { // Ensure no reallocation happens -- the input range is longer than the vector but within capacity. |
| 58 | int in[] = {-1, -2, -3, -4, -5, -6, -7, -8}; |
| 59 | std::vector<int> v = {1, 2, 3, 4, 5}; |
| 60 | v.reserve(std::ranges::size(in)); |
| 61 | assert(v.capacity() >= std::ranges::size(in)); |
| 62 | |
| 63 | v.assign_range(in); |
| 64 | assert(std::ranges::equal(v, in)); |
| 65 | } |
| 66 | |
| 67 | { // Ensure input-only sized ranges are accepted. |
| 68 | using input_iter = cpp20_input_iterator<const int*>; |
| 69 | const int in[]{1, 2, 3, 4}; |
| 70 | std::vector<int> v; |
| 71 | v.assign_range(std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in))); |
| 72 | assert(std::ranges::equal(v, std::vector<int>{1, 2, 3, 4})); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | #ifndef TEST_HAS_NO_LOCALIZATION |
| 80 | void test_counted_istream_view() { |
| 81 | std::istringstream is{"1 2 3 4" }; |
| 82 | auto vals = std::views::istream<int>(is); |
| 83 | std::vector<int> v; |
| 84 | v.assign_range(std::views::counted(vals.begin(), 3)); |
| 85 | assert(v == (std::vector{1, 2, 3})); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | int main(int, char**) { |
| 90 | test(); |
| 91 | static_assert(test()); |
| 92 | |
| 93 | test_assign_range_exception_safety_throwing_copy<std::vector>(); |
| 94 | test_assign_range_exception_safety_throwing_allocator<std::vector, int>(); |
| 95 | |
| 96 | #ifndef TEST_HAS_NO_LOCALIZATION |
| 97 | test_counted_istream_view(); |
| 98 | #endif |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |