| 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 | // <deque> |
| 10 | |
| 11 | // template <class InputIterator> |
| 12 | // deque(InputIterator f, InputIterator l, const allocator_type& a); |
| 13 | |
| 14 | #include "asan_testing.h" |
| 15 | #include <deque> |
| 16 | #include <cassert> |
| 17 | #include <cstddef> |
| 18 | |
| 19 | #include "test_macros.h" |
| 20 | #include "test_iterators.h" |
| 21 | #include "test_allocator.h" |
| 22 | #include "min_allocator.h" |
| 23 | #if TEST_STD_VER >= 11 |
| 24 | # include "emplace_constructible.h" |
| 25 | #endif |
| 26 | |
| 27 | template <class InputIterator, class Allocator> |
| 28 | void test(InputIterator f, InputIterator l, const Allocator& a) { |
| 29 | typedef typename std::iterator_traits<InputIterator>::value_type T; |
| 30 | typedef std::deque<T, Allocator> C; |
| 31 | typedef typename C::const_iterator const_iterator; |
| 32 | C d(f, l, a); |
| 33 | assert(d.get_allocator() == a); |
| 34 | assert(d.size() == static_cast<std::size_t>(std::distance(f, l))); |
| 35 | assert(static_cast<std::size_t>(std::distance(d.begin(), d.end())) == d.size()); |
| 36 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(d)); |
| 37 | for (const_iterator i = d.begin(), e = d.end(); i != e; ++i, ++f) |
| 38 | assert(*i == *f); |
| 39 | } |
| 40 | |
| 41 | void basic_test() { |
| 42 | int ab[] = {3, 4, 2, 8, 0, 1, 44, 34, 45, 96, 80, 1, 13, 31, 45}; |
| 43 | int* an = ab + sizeof(ab) / sizeof(ab[0]); |
| 44 | test(cpp17_input_iterator<const int*>(ab), cpp17_input_iterator<const int*>(an), test_allocator<int>(3)); |
| 45 | test(forward_iterator<const int*>(ab), forward_iterator<const int*>(an), test_allocator<int>(4)); |
| 46 | test(bidirectional_iterator<const int*>(ab), bidirectional_iterator<const int*>(an), test_allocator<int>(5)); |
| 47 | test(random_access_iterator<const int*>(ab), random_access_iterator<const int*>(an), test_allocator<int>(6)); |
| 48 | #if TEST_STD_VER >= 11 |
| 49 | test(cpp17_input_iterator<const int*>(ab), cpp17_input_iterator<const int*>(an), min_allocator<int>()); |
| 50 | test(forward_iterator<const int*>(ab), forward_iterator<const int*>(an), min_allocator<int>()); |
| 51 | test(bidirectional_iterator<const int*>(ab), bidirectional_iterator<const int*>(an), min_allocator<int>()); |
| 52 | test(random_access_iterator<const int*>(ab), random_access_iterator<const int*>(an), min_allocator<int>()); |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | void test_emplacable_concept() { |
| 57 | #if TEST_STD_VER >= 11 |
| 58 | int arr1[] = {42}; |
| 59 | int arr2[] = {1, 101, 42}; |
| 60 | { |
| 61 | using T = EmplaceConstructibleAndMoveable<int>; |
| 62 | using It = random_access_iterator<int*>; |
| 63 | std::allocator<T> a; |
| 64 | { |
| 65 | std::deque<T> v(It(arr1), It(std::end(arr1)), a); |
| 66 | assert(v[0].value == 42); |
| 67 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v)); |
| 68 | } |
| 69 | { |
| 70 | std::deque<T> v(It(arr2), It(std::end(arr2)), a); |
| 71 | assert(v[0].value == 1); |
| 72 | assert(v[1].value == 101); |
| 73 | assert(v[2].value == 42); |
| 74 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v)); |
| 75 | } |
| 76 | } |
| 77 | { |
| 78 | using T = EmplaceConstructibleAndMoveable<int>; |
| 79 | using It = cpp17_input_iterator<int*>; |
| 80 | std::allocator<T> a; |
| 81 | { |
| 82 | std::deque<T> v(It(arr1), It(std::end(arr1)), a); |
| 83 | assert(v[0].copied == 0); |
| 84 | assert(v[0].value == 42); |
| 85 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v)); |
| 86 | } |
| 87 | { |
| 88 | std::deque<T> v(It(arr2), It(std::end(arr2)), a); |
| 89 | //assert(v[0].copied == 0); |
| 90 | assert(v[0].value == 1); |
| 91 | //assert(v[1].copied == 0); |
| 92 | assert(v[1].value == 101); |
| 93 | assert(v[2].copied == 0); |
| 94 | assert(v[2].value == 42); |
| 95 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(v)); |
| 96 | } |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 | |
| 101 | int main(int, char**) { |
| 102 | basic_test(); |
| 103 | test_emplacable_concept(); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |