xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.cons/deduct.pass.cpp (revision 10ec9276d40024c23a481e6671dad1521151dd85)
1dbb6f8a8SMarshall Clow //===----------------------------------------------------------------------===//
2dbb6f8a8SMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6dbb6f8a8SMarshall Clow //
7dbb6f8a8SMarshall Clow //===----------------------------------------------------------------------===//
8dbb6f8a8SMarshall Clow 
9dd15c272SArthur O'Dwyer // <deque>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
11dbb6f8a8SMarshall Clow 
12dbb6f8a8SMarshall Clow // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
13dbb6f8a8SMarshall Clow //    deque(InputIterator, InputIterator, Allocator = Allocator())
14dbb6f8a8SMarshall Clow //    -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
15dbb6f8a8SMarshall Clow //
1643377cc4Svarconst // template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>>
1743377cc4Svarconst //   deque(from_range_t, R&&, Allocator = Allocator())
1843377cc4Svarconst //     -> deque<ranges::range_value_t<R>, Allocator>; // C++23
19dbb6f8a8SMarshall Clow 
20*10ec9276SAdvenam Tacet #include "asan_testing.h"
2143377cc4Svarconst #include <array>
2243377cc4Svarconst #include <cassert>
2343377cc4Svarconst #include <climits> // INT_MAX
2443377cc4Svarconst #include <cstddef>
25dbb6f8a8SMarshall Clow #include <deque>
26dbb6f8a8SMarshall Clow #include <iterator>
27dbb6f8a8SMarshall Clow 
2868072a71SKonstantin Varlamov #include "deduction_guides_sfinae_checks.h"
29dbb6f8a8SMarshall Clow #include "test_macros.h"
30dbb6f8a8SMarshall Clow #include "test_iterators.h"
31dbb6f8a8SMarshall Clow #include "test_allocator.h"
32dbb6f8a8SMarshall Clow 
33dbb6f8a8SMarshall Clow struct A {};
34dbb6f8a8SMarshall Clow 
main(int,char **)352df59c50SJF Bastien int main(int, char**)
36dbb6f8a8SMarshall Clow {
37dbb6f8a8SMarshall Clow 
38dbb6f8a8SMarshall Clow //  Test the explicit deduction guides
39dbb6f8a8SMarshall Clow     {
40dbb6f8a8SMarshall Clow     const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
41dbb6f8a8SMarshall Clow     std::deque deq(std::begin(arr), std::end(arr));
42dbb6f8a8SMarshall Clow 
43dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq), std::deque<int>>, "");
44dbb6f8a8SMarshall Clow     assert(std::equal(deq.begin(), deq.end(), std::begin(arr), std::end(arr)));
45*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
46dbb6f8a8SMarshall Clow     }
47dbb6f8a8SMarshall Clow 
48dbb6f8a8SMarshall Clow     {
49926b0b2bSMarshall Clow     const long arr[] = {INT_MAX, 1L, 2L, 3L };
50dbb6f8a8SMarshall Clow     std::deque deq(std::begin(arr), std::end(arr), std::allocator<long>());
51dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, long>, "");
52dbb6f8a8SMarshall Clow     assert(deq.size() == 4);
53dbb6f8a8SMarshall Clow     assert(deq[0] == INT_MAX);
54926b0b2bSMarshall Clow     assert(deq[1] == 1L);
55dbb6f8a8SMarshall Clow     assert(deq[2] == 2L);
56*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
57dbb6f8a8SMarshall Clow     }
58dbb6f8a8SMarshall Clow 
59dbb6f8a8SMarshall Clow //  Test the implicit deduction guides
60dbb6f8a8SMarshall Clow 
61dbb6f8a8SMarshall Clow     {
62dbb6f8a8SMarshall Clow //  We don't expect this one to work.
63dbb6f8a8SMarshall Clow //  std::deque deq(std::allocator<int>()); // deque (allocator &)
64dbb6f8a8SMarshall Clow     }
65dbb6f8a8SMarshall Clow 
66dbb6f8a8SMarshall Clow     {
67dbb6f8a8SMarshall Clow     std::deque deq(1, A{}); // deque (size_type, T)
68dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, A>, "");
69dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::allocator_type, std::allocator<A>>, "");
70dbb6f8a8SMarshall Clow     assert(deq.size() == 1);
71*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
72dbb6f8a8SMarshall Clow     }
73dbb6f8a8SMarshall Clow 
74dbb6f8a8SMarshall Clow     {
75dbb6f8a8SMarshall Clow     std::deque deq(1, A{}, test_allocator<A>()); // deque (size_type, T, allocator)
76dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, A>, "");
77dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::allocator_type, test_allocator<A>>, "");
78dbb6f8a8SMarshall Clow     assert(deq.size() == 1);
79*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
80dbb6f8a8SMarshall Clow     }
81dbb6f8a8SMarshall Clow 
82dbb6f8a8SMarshall Clow     {
83dbb6f8a8SMarshall Clow     std::deque deq{1U, 2U, 3U, 4U, 5U}; // deque(initializer-list)
84dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, unsigned>, "");
85dbb6f8a8SMarshall Clow     assert(deq.size() == 5);
86dbb6f8a8SMarshall Clow     assert(deq[2] == 3U);
87*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
88dbb6f8a8SMarshall Clow     }
89dbb6f8a8SMarshall Clow 
90dbb6f8a8SMarshall Clow     {
91dbb6f8a8SMarshall Clow     std::deque deq({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // deque(initializer-list, allocator)
92dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, double>, "");
93dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::allocator_type, test_allocator<double>>, "");
94dbb6f8a8SMarshall Clow     assert(deq.size() == 4);
95dbb6f8a8SMarshall Clow     assert(deq[3] == 4.0);
96*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
97dbb6f8a8SMarshall Clow     }
98dbb6f8a8SMarshall Clow 
99dbb6f8a8SMarshall Clow     {
100dbb6f8a8SMarshall Clow     std::deque<long double> source;
101dbb6f8a8SMarshall Clow     std::deque deq(source); // deque(deque &)
102dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::value_type, long double>, "");
103dbb6f8a8SMarshall Clow     static_assert(std::is_same_v<decltype(deq)::allocator_type, std::allocator<long double>>, "");
104dbb6f8a8SMarshall Clow     assert(deq.size() == 0);
105*10ec9276SAdvenam Tacet     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
106dbb6f8a8SMarshall Clow     }
1072df59c50SJF Bastien 
108dd15c272SArthur O'Dwyer     {
109dd15c272SArthur O'Dwyer         typedef test_allocator<short> Alloc;
110dd15c272SArthur O'Dwyer         typedef test_allocator<int> ConvertibleToAlloc;
111dd15c272SArthur O'Dwyer 
112dd15c272SArthur O'Dwyer         {
113dd15c272SArthur O'Dwyer         std::deque<short, Alloc> source;
114dd15c272SArthur O'Dwyer         std::deque deq(source, Alloc(2));
115dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(deq), decltype(source)>);
116*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
117*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source));
118dd15c272SArthur O'Dwyer         }
119dd15c272SArthur O'Dwyer 
120dd15c272SArthur O'Dwyer         {
121dd15c272SArthur O'Dwyer         std::deque<short, Alloc> source;
122dd15c272SArthur O'Dwyer         std::deque deq(source, ConvertibleToAlloc(2));
123dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(deq), decltype(source)>);
124*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
125*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source));
126dd15c272SArthur O'Dwyer         }
127dd15c272SArthur O'Dwyer 
128dd15c272SArthur O'Dwyer         {
129dd15c272SArthur O'Dwyer         std::deque<short, Alloc> source;
130dd15c272SArthur O'Dwyer         std::deque deq(std::move(source), Alloc(2));
131dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(deq), decltype(source)>);
132*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
133*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source));
134dd15c272SArthur O'Dwyer         }
135dd15c272SArthur O'Dwyer 
136dd15c272SArthur O'Dwyer         {
137dd15c272SArthur O'Dwyer         std::deque<short, Alloc> source;
138dd15c272SArthur O'Dwyer         std::deque deq(std::move(source), ConvertibleToAlloc(2));
139dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(deq), decltype(source)>);
140*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(deq));
141*10ec9276SAdvenam Tacet         LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(source));
142dd15c272SArthur O'Dwyer         }
143dd15c272SArthur O'Dwyer     }
144dd15c272SArthur O'Dwyer 
14543377cc4Svarconst #if TEST_STD_VER >= 23
14643377cc4Svarconst     {
14743377cc4Svarconst       {
14843377cc4Svarconst         std::deque c(std::from_range, std::array<int, 0>());
14943377cc4Svarconst         static_assert(std::is_same_v<decltype(c), std::deque<int>>);
15043377cc4Svarconst       }
15143377cc4Svarconst 
15243377cc4Svarconst       {
15343377cc4Svarconst         using Alloc = test_allocator<int>;
15443377cc4Svarconst         std::deque c(std::from_range, std::array<int, 0>(), Alloc());
15543377cc4Svarconst         static_assert(std::is_same_v<decltype(c), std::deque<int, Alloc>>);
15643377cc4Svarconst       }
15743377cc4Svarconst     }
15843377cc4Svarconst #endif
15943377cc4Svarconst 
16068072a71SKonstantin Varlamov     SequenceContainerDeductionGuidesSfinaeAway<std::deque, std::deque<int>>();
16168072a71SKonstantin Varlamov 
1622df59c50SJF Bastien     return 0;
163dbb6f8a8SMarshall Clow }
164