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// Optimization for deque::iterators
12
13// template <class InputIterator, class OutputIterator>
14// OutputIterator
15// copy(InputIterator first, InputIterator last, OutputIterator result);
16
17#include "asan_testing.h"
18#include <deque>
19#include <cassert>
20
21#include "test_macros.h"
22#include "test_iterators.h"
23#include "min_allocator.h"
24
25template <class C>
26C make(int size, int start = 0) {
27 const int b = 4096 / sizeof(int);
28 int init = 0;
29 if (start > 0) {
30 init = (start + 1) / b + ((start + 1) % b != 0);
31 init *= b;
32 --init;
33 }
34 C c(init, 0);
35 for (int i = 0; i < init - start; ++i)
36 c.pop_back();
37 for (int i = 0; i < size; ++i)
38 c.push_back(i);
39 for (int i = 0; i < start; ++i)
40 c.pop_front();
41 return c;
42}
43
44template <class C>
45void testN(int start, int N) {
46 typedef typename C::iterator I;
47 typedef typename C::const_iterator CI;
48 typedef random_access_iterator<I> RAI;
49 typedef random_access_iterator<CI> RACI;
50 typedef cpp17_input_iterator<CI> ICI;
51 C c1 = make<C>(N, start);
52 C c2 = make<C>(N);
53 assert(std::copy(c1.cbegin(), c1.cend(), c2.begin()) == c2.end());
54 assert(c1 == c2);
55 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
56 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
57 assert(std::copy(c2.cbegin(), c2.cend(), c1.begin()) == c1.end());
58 assert(c1 == c2);
59 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
60 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
61 assert(std::copy(c1.cbegin(), c1.cend(), RAI(c2.begin())) == RAI(c2.end()));
62 assert(c1 == c2);
63 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
64 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
65 assert(std::copy(c2.cbegin(), c2.cend(), RAI(c1.begin())) == RAI(c1.end()));
66 assert(c1 == c2);
67 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
68 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
69 assert(std::copy(RACI(c1.cbegin()), RACI(c1.cend()), c2.begin()) == c2.end());
70 assert(c1 == c2);
71 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
72 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
73 assert(std::copy(ICI(c2.cbegin()), ICI(c2.cend()), c1.begin()) == c1.end());
74 assert(c1 == c2);
75 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
76 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
77}
78
79int main(int, char**) {
80 {
81 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
82 const int N = sizeof(rng) / sizeof(rng[0]);
83 for (int i = 0; i < N; ++i)
84 for (int j = 0; j < N; ++j)
85 testN<std::deque<int> >(start: rng[i], N: rng[j]);
86 }
87#if TEST_STD_VER >= 11
88 {
89 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
90 const int N = sizeof(rng) / sizeof(rng[0]);
91 for (int i = 0; i < N; ++i)
92 for (int j = 0; j < N; ++j)
93 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
94 }
95#endif
96
97 return 0;
98}
99

source code of libcxx/test/std/containers/sequences/deque/deque.special/copy.pass.cpp