| 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 | // UNSUPPORTED: c++03, c++11, c++14 |
| 11 | |
| 12 | // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> |
| 13 | // deque(InputIterator, InputIterator, Allocator = Allocator()) |
| 14 | // -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; |
| 15 | // |
| 16 | // template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>> |
| 17 | // deque(from_range_t, R&&, Allocator = Allocator()) |
| 18 | // -> deque<ranges::range_value_t<R>, Allocator>; // C++23 |
| 19 | |
| 20 | #include "asan_testing.h" |
| 21 | #include <array> |
| 22 | #include <cassert> |
| 23 | #include <climits> // INT_MAX |
| 24 | #include <cstddef> |
| 25 | #include <deque> |
| 26 | #include <iterator> |
| 27 | |
| 28 | #include "deduction_guides_sfinae_checks.h" |
| 29 | #include "test_macros.h" |
| 30 | #include "test_iterators.h" |
| 31 | #include "test_allocator.h" |
| 32 | |
| 33 | struct A {}; |
| 34 | |
| 35 | int main(int, char**) { |
| 36 | // Test the explicit deduction guides |
| 37 | { |
| 38 | const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 39 | std::deque deq(std::begin(arr: arr), std::end(arr: arr)); |
| 40 | |
| 41 | static_assert(std::is_same_v<decltype(deq), std::deque<int>>, "" ); |
| 42 | assert(std::equal(deq.begin(), deq.end(), std::begin(arr), std::end(arr))); |
| 43 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | const long arr[] = {INT_MAX, 1L, 2L, 3L}; |
| 48 | std::deque deq(std::begin(arr: arr), std::end(arr: arr), std::allocator<long>()); |
| 49 | static_assert(std::is_same_v<decltype(deq)::value_type, long>, "" ); |
| 50 | assert(deq.size() == 4); |
| 51 | assert(deq[0] == INT_MAX); |
| 52 | assert(deq[1] == 1L); |
| 53 | assert(deq[2] == 2L); |
| 54 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 55 | } |
| 56 | |
| 57 | // Test the implicit deduction guides |
| 58 | |
| 59 | { |
| 60 | // We don't expect this one to work. |
| 61 | // std::deque deq(std::allocator<int>()); // deque (allocator &) |
| 62 | } |
| 63 | |
| 64 | { |
| 65 | std::deque deq(1, A{}); // deque (size_type, T) |
| 66 | static_assert(std::is_same_v<decltype(deq)::value_type, A>, "" ); |
| 67 | static_assert(std::is_same_v<decltype(deq)::allocator_type, std::allocator<A>>, "" ); |
| 68 | assert(deq.size() == 1); |
| 69 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | std::deque deq(1, A{}, test_allocator<A>()); // deque (size_type, T, allocator) |
| 74 | static_assert(std::is_same_v<decltype(deq)::value_type, A>, "" ); |
| 75 | static_assert(std::is_same_v<decltype(deq)::allocator_type, test_allocator<A>>, "" ); |
| 76 | assert(deq.size() == 1); |
| 77 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | std::deque deq{1U, 2U, 3U, 4U, 5U}; // deque(initializer-list) |
| 82 | static_assert(std::is_same_v<decltype(deq)::value_type, unsigned>, "" ); |
| 83 | assert(deq.size() == 5); |
| 84 | assert(deq[2] == 3U); |
| 85 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | std::deque deq({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // deque(initializer-list, allocator) |
| 90 | static_assert(std::is_same_v<decltype(deq)::value_type, double>, "" ); |
| 91 | static_assert(std::is_same_v<decltype(deq)::allocator_type, test_allocator<double>>, "" ); |
| 92 | assert(deq.size() == 4); |
| 93 | assert(deq[3] == 4.0); |
| 94 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 95 | } |
| 96 | |
| 97 | { |
| 98 | std::deque<long double> source; |
| 99 | std::deque deq(source); // deque(deque &) |
| 100 | static_assert(std::is_same_v<decltype(deq)::value_type, long double>, "" ); |
| 101 | static_assert(std::is_same_v<decltype(deq)::allocator_type, std::allocator<long double>>, "" ); |
| 102 | assert(deq.size() == 0); |
| 103 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 104 | } |
| 105 | |
| 106 | { |
| 107 | typedef test_allocator<short> Alloc; |
| 108 | typedef test_allocator<int> ConvertibleToAlloc; |
| 109 | |
| 110 | { |
| 111 | std::deque<short, Alloc> source; |
| 112 | std::deque deq(source, Alloc(2)); |
| 113 | static_assert(std::is_same_v<decltype(deq), decltype(source)>); |
| 114 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 115 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source)); |
| 116 | } |
| 117 | |
| 118 | { |
| 119 | std::deque<short, Alloc> source; |
| 120 | std::deque deq(source, ConvertibleToAlloc(2)); |
| 121 | static_assert(std::is_same_v<decltype(deq), decltype(source)>); |
| 122 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 123 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source)); |
| 124 | } |
| 125 | |
| 126 | { |
| 127 | std::deque<short, Alloc> source; |
| 128 | std::deque deq(std::move(source), Alloc(2)); |
| 129 | static_assert(std::is_same_v<decltype(deq), decltype(source)>); |
| 130 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 131 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source)); |
| 132 | } |
| 133 | |
| 134 | { |
| 135 | std::deque<short, Alloc> source; |
| 136 | std::deque deq(std::move(source), ConvertibleToAlloc(2)); |
| 137 | static_assert(std::is_same_v<decltype(deq), decltype(source)>); |
| 138 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq)); |
| 139 | LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #if TEST_STD_VER >= 23 |
| 144 | { |
| 145 | { |
| 146 | std::deque c(std::from_range, std::array<int, 0>()); |
| 147 | static_assert(std::is_same_v<decltype(c), std::deque<int>>); |
| 148 | } |
| 149 | |
| 150 | { |
| 151 | using Alloc = test_allocator<int>; |
| 152 | std::deque c(std::from_range, std::array<int, 0>(), Alloc()); |
| 153 | static_assert(std::is_same_v<decltype(c), std::deque<int, Alloc>>); |
| 154 | } |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | SequenceContainerDeductionGuidesSfinaeAway<std::deque, std::deque<int>>(); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |