| 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 | // vector(const vector& v); |
| 12 | |
| 13 | #include <vector> |
| 14 | #include <cassert> |
| 15 | |
| 16 | #include "test_macros.h" |
| 17 | #include "test_allocator.h" |
| 18 | #include "min_allocator.h" |
| 19 | #include "asan_testing.h" |
| 20 | |
| 21 | template <class C> |
| 22 | TEST_CONSTEXPR_CXX20 void test(const C& x) { |
| 23 | typename C::size_type s = x.size(); |
| 24 | C c(x); |
| 25 | LIBCPP_ASSERT(c.__invariants()); |
| 26 | assert(c.size() == s); |
| 27 | assert(c == x); |
| 28 | LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); |
| 29 | } |
| 30 | |
| 31 | TEST_CONSTEXPR_CXX20 bool tests() { |
| 32 | { |
| 33 | int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; |
| 34 | int* an = a + sizeof(a) / sizeof(a[0]); |
| 35 | test(std::vector<int>(a, an)); |
| 36 | } |
| 37 | { |
| 38 | std::vector<int, test_allocator<int> > v(3, 2, test_allocator<int>(5)); |
| 39 | std::vector<int, test_allocator<int> > v2 = v; |
| 40 | assert(is_contiguous_container_asan_correct(v)); |
| 41 | assert(is_contiguous_container_asan_correct(v2)); |
| 42 | assert(v2 == v); |
| 43 | assert(v2.get_allocator() == v.get_allocator()); |
| 44 | assert(is_contiguous_container_asan_correct(v)); |
| 45 | assert(is_contiguous_container_asan_correct(v2)); |
| 46 | } |
| 47 | { |
| 48 | // Test copy ctor with empty source |
| 49 | std::vector<int, test_allocator<int> > v(test_allocator<int>(5)); |
| 50 | std::vector<int, test_allocator<int> > v2 = v; |
| 51 | assert(is_contiguous_container_asan_correct(v)); |
| 52 | assert(is_contiguous_container_asan_correct(v2)); |
| 53 | assert(v2 == v); |
| 54 | assert(v2.get_allocator() == v.get_allocator()); |
| 55 | assert(is_contiguous_container_asan_correct(v)); |
| 56 | assert(is_contiguous_container_asan_correct(v2)); |
| 57 | assert(v2.empty()); |
| 58 | } |
| 59 | #if TEST_STD_VER >= 11 |
| 60 | { |
| 61 | std::vector<int, other_allocator<int> > v(3, 2, other_allocator<int>(5)); |
| 62 | std::vector<int, other_allocator<int> > v2 = v; |
| 63 | assert(is_contiguous_container_asan_correct(v)); |
| 64 | assert(is_contiguous_container_asan_correct(v2)); |
| 65 | assert(v2 == v); |
| 66 | assert(v2.get_allocator() == other_allocator<int>(-2)); |
| 67 | assert(is_contiguous_container_asan_correct(v)); |
| 68 | assert(is_contiguous_container_asan_correct(v2)); |
| 69 | } |
| 70 | { |
| 71 | int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; |
| 72 | int* an = a + sizeof(a) / sizeof(a[0]); |
| 73 | test(std::vector<int, min_allocator<int>>(a, an)); |
| 74 | test(std::vector<int, safe_allocator<int>>(a, an)); |
| 75 | } |
| 76 | { |
| 77 | std::vector<int, min_allocator<int> > v(3, 2, min_allocator<int>()); |
| 78 | std::vector<int, min_allocator<int> > v2 = v; |
| 79 | assert(is_contiguous_container_asan_correct(v)); |
| 80 | assert(is_contiguous_container_asan_correct(v2)); |
| 81 | assert(v2 == v); |
| 82 | assert(v2.get_allocator() == v.get_allocator()); |
| 83 | assert(is_contiguous_container_asan_correct(v)); |
| 84 | assert(is_contiguous_container_asan_correct(v2)); |
| 85 | } |
| 86 | { |
| 87 | std::vector<int, safe_allocator<int> > v(3, 2, safe_allocator<int>()); |
| 88 | std::vector<int, safe_allocator<int> > v2 = v; |
| 89 | assert(is_contiguous_container_asan_correct(v)); |
| 90 | assert(is_contiguous_container_asan_correct(v2)); |
| 91 | assert(v2 == v); |
| 92 | assert(v2.get_allocator() == v.get_allocator()); |
| 93 | assert(is_contiguous_container_asan_correct(v)); |
| 94 | assert(is_contiguous_container_asan_correct(v2)); |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | void test_copy_from_volatile_src() { |
| 102 | volatile int src[] = {1, 2, 3}; |
| 103 | std::vector<int> v(src, src + 3); |
| 104 | assert(v[0] == 1); |
| 105 | assert(v[1] == 2); |
| 106 | assert(v[2] == 3); |
| 107 | } |
| 108 | |
| 109 | int main(int, char**) { |
| 110 | tests(); |
| 111 | #if TEST_STD_VER > 17 |
| 112 | static_assert(tests()); |
| 113 | #endif |
| 114 | test_copy_from_volatile_src(); |
| 115 | return 0; |
| 116 | } |
| 117 | |