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