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 <vector> |
16 | |
17 | #include "../../insert_range_sequence_containers.h" |
18 | #include "asan_testing.h" |
19 | #include "test_macros.h" |
20 | |
21 | // Tested cases: |
22 | // - different kinds of assignments (assigning an {empty/one-element/mid-sized/long range} to an |
23 | // {empty/one-element/full} container); |
24 | // - assigning move-only elements; |
25 | // - an exception is thrown when copying the elements or when allocating new elements. |
26 | constexpr bool test() { |
27 | static_assert(test_constraints_assign_range<std::vector, int, double>()); |
28 | |
29 | for_all_iterators_and_allocators<int, const int*>(f: []<class Iter, class Sent, class Alloc>() { |
30 | test_sequence_assign_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) { |
31 | LIBCPP_ASSERT(c.__invariants()); |
32 | LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); |
33 | }); |
34 | }); |
35 | test_sequence_assign_range_move_only<std::vector>(); |
36 | |
37 | { // Vector may or may not need to reallocate because of the assignment -- make sure to test both cases. |
38 | { // Ensure reallocation happens. |
39 | int in[] = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10}; |
40 | std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8}; |
41 | v.shrink_to_fit(); |
42 | assert(v.capacity() < v.size() + std::ranges::size(in)); |
43 | |
44 | v.assign_range(in); |
45 | assert(std::ranges::equal(v, in)); |
46 | } |
47 | |
48 | { // Ensure no reallocation happens -- the input range is shorter than the vector. |
49 | int in[] = {-1, -2, -3, -4, -5}; |
50 | std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8}; |
51 | |
52 | v.assign_range(in); |
53 | assert(std::ranges::equal(v, in)); |
54 | } |
55 | |
56 | { // Ensure no reallocation happens -- the input range is longer than the vector but within capacity. |
57 | int in[] = {-1, -2, -3, -4, -5, -6, -7, -8}; |
58 | std::vector<int> v = {1, 2, 3, 4, 5}; |
59 | v.reserve(std::ranges::size(in)); |
60 | assert(v.capacity() >= std::ranges::size(in)); |
61 | |
62 | v.assign_range(in); |
63 | assert(std::ranges::equal(v, in)); |
64 | } |
65 | } |
66 | |
67 | return true; |
68 | } |
69 | |
70 | int main(int, char**) { |
71 | test(); |
72 | static_assert(test()); |
73 | |
74 | test_assign_range_exception_safety_throwing_copy<std::vector>(); |
75 | test_assign_range_exception_safety_throwing_allocator<std::vector, int>(); |
76 | |
77 | return 0; |
78 | } |
79 | |