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