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 | // <vector> |
10 | |
11 | // void assign(size_type n, const_reference v); |
12 | |
13 | #include <vector> |
14 | #include <algorithm> |
15 | #include <cassert> |
16 | #include "test_macros.h" |
17 | #include "min_allocator.h" |
18 | #include "asan_testing.h" |
19 | #include "test_iterators.h" |
20 | #if TEST_STD_VER >= 11 |
21 | #include "emplace_constructible.h" |
22 | #include "container_test_types.h" |
23 | #endif |
24 | |
25 | |
26 | TEST_CONSTEXPR_CXX20 bool test() { |
27 | #if TEST_STD_VER >= 11 |
28 | int arr1[] = {42}; |
29 | int arr2[] = {1, 101, 42}; |
30 | { |
31 | using T = EmplaceConstructibleMoveableAndAssignable<int>; |
32 | using It = forward_iterator<int*>; |
33 | { |
34 | std::vector<T> v; |
35 | v.assign(It(arr1), It(std::end(arr1))); |
36 | assert(v[0].value == 42); |
37 | } |
38 | { |
39 | std::vector<T> v; |
40 | v.assign(It(arr2), It(std::end(arr2))); |
41 | assert(v[0].value == 1); |
42 | assert(v[1].value == 101); |
43 | assert(v[2].value == 42); |
44 | } |
45 | } |
46 | { |
47 | using T = EmplaceConstructibleMoveableAndAssignable<int>; |
48 | using It = cpp17_input_iterator<int*>; |
49 | { |
50 | std::vector<T> v; |
51 | v.assign(It(arr1), It(std::end(arr1))); |
52 | assert(v[0].copied == 0); |
53 | assert(v[0].value == 42); |
54 | } |
55 | { |
56 | std::vector<T> v; |
57 | v.assign(It(arr2), It(std::end(arr2))); |
58 | //assert(v[0].copied == 0); |
59 | assert(v[0].value == 1); |
60 | //assert(v[1].copied == 0); |
61 | assert(v[1].value == 101); |
62 | assert(v[2].copied == 0); |
63 | assert(v[2].value == 42); |
64 | } |
65 | } |
66 | #endif |
67 | |
68 | // Test with a number of elements in the source range that is greater than capacity |
69 | { |
70 | typedef forward_iterator<int*> It; |
71 | |
72 | std::vector<int> dst(10); |
73 | |
74 | std::size_t n = dst.capacity() * 2; |
75 | std::vector<int> src(n); |
76 | |
77 | dst.assign(n: It(src.data()), val: It(src.data() + src.size())); |
78 | assert(dst == src); |
79 | } |
80 | |
81 | return true; |
82 | } |
83 | |
84 | int main(int, char**) { |
85 | test(); |
86 | #if TEST_STD_VER > 17 |
87 | static_assert(test()); |
88 | #endif |
89 | return 0; |
90 | } |
91 | |