xref: /llvm-project/libcxx/test/std/containers/sequences/deque/deque.special/copy_backward.pass.cpp (revision 10ec9276d40024c23a481e6671dad1521151dd85)
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_backward(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 
25 template <class C>
26 C
make(int size,int start=0)27 make(int size, int start = 0 )
28 {
29     const int b = 4096 / sizeof(int);
30     int init = 0;
31     if (start > 0)
32     {
33         init = (start+1) / b + ((start+1) % b != 0);
34         init *= b;
35         --init;
36     }
37     C c(init, 0);
38     for (int i = 0; i < init-start; ++i)
39         c.pop_back();
40     for (int i = 0; i < size; ++i)
41         c.push_back(i);
42     for (int i = 0; i < start; ++i)
43         c.pop_front();
44     return c;
45 }
46 
47 template <class C>
testN(int start,int N)48 void testN(int start, int N)
49 {
50     typedef typename C::iterator I;
51     typedef typename C::const_iterator CI;
52     typedef random_access_iterator<I> RAI;
53     typedef random_access_iterator<CI> RACI;
54     C c1 = make<C>(N, start);
55     C c2 = make<C>(N);
56     assert(std::copy_backward(c1.cbegin(), c1.cend(), c2.end()) == c2.begin());
57     assert(c1 == c2);
58     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
59     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
60     assert(std::copy_backward(c2.cbegin(), c2.cend(), c1.end()) == c1.begin());
61     assert(c1 == c2);
62     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
63     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
64     assert(std::copy_backward(c1.cbegin(), c1.cend(), RAI(c2.end())) == RAI(c2.begin()));
65     assert(c1 == c2);
66     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
67     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
68     assert(std::copy_backward(c2.cbegin(), c2.cend(), RAI(c1.end())) == RAI(c1.begin()));
69     assert(c1 == c2);
70     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
71     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
72     assert(std::copy_backward(RACI(c1.cbegin()), RACI(c1.cend()), c2.end()) == c2.begin());
73     assert(c1 == c2);
74     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
75     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
76     assert(std::copy_backward(RACI(c2.cbegin()), RACI(c2.cend()), c1.end()) == c1.begin());
77     assert(c1 == c2);
78     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
79     LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
80 }
81 
main(int,char **)82 int main(int, char**)
83 {
84     {
85     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
86     const int N = sizeof(rng)/sizeof(rng[0]);
87     for (int i = 0; i < N; ++i)
88         for (int j = 0; j < N; ++j)
89             testN<std::deque<int> >(rng[i], rng[j]);
90     }
91 #if TEST_STD_VER >= 11
92     {
93     int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
94     const int N = sizeof(rng)/sizeof(rng[0]);
95     for (int i = 0; i < N; ++i)
96         for (int j = 0; j < N; ++j)
97             testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j]);
98     }
99 #endif
100 
101   return 0;
102 }
103