xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.shift/shift_right.pass.cpp (revision 3fbd3eaf28c1e6f2bb9519022611829dfe3b0464)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <algorithm>
12 
13 // template<class ForwardIterator>
14 // constexpr ForwardIterator
15 //   shift_right(ForwardIterator first, ForwardIterator last,
16 //               typename iterator_traits<ForwardIterator>::difference_type n);
17 
18 #include <algorithm>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "test_iterators.h"
23 #include "MoveOnly.h"
24 
25 template<class T, class Iter>
test()26 constexpr bool test()
27 {
28     int orig[] = {3,1,4,1,5, 9,2,6,5,3, 5,8,9,7,9};
29     T work[] = {3,1,4,1,5, 9,2,6,5,3, 5,8,9,7,9};
30 
31     for (int n = 0; n <= 15; ++n) {
32         for (int k = 0; k <= n+2; ++k) {
33             std::copy(orig, orig+n, work);
34             Iter it = std::shift_right(Iter(work), Iter(work+n), k);
35             if (k < n) {
36                 assert(it == Iter(work+k));
37                 assert(std::equal(orig, orig+n-k, work+k, work+n));
38             } else {
39                 assert(it == Iter(work+n));
40                 assert(std::equal(orig, orig+n, work, work+n));
41             }
42         }
43     }
44 
45     // n == 0
46     {
47         T input[]          = { 0, 1, 2 };
48         const T expected[] = { 0, 1, 2 };
49         Iter b = Iter(std::begin(input));
50         Iter e = Iter(std::end(input));
51         Iter it = std::shift_right(b, e, 0);
52         assert(std::equal(std::begin(expected), std::end(expected), it, e));
53     }
54 
55     // n > 0 && n < len
56     {
57         T input[]          = { 0, 1, 2 };
58         const T expected[] = { 0, 1 };
59         Iter b = Iter(std::begin(input));
60         Iter e = Iter(std::end(input));
61         Iter it = std::shift_right(b, e, 1);
62         assert(std::equal(std::begin(expected), std::end(expected), it, e));
63     }
64     {
65         T input[]          = { 1, 2, 3, 4, 5, 6, 7, 8 };
66         const T expected[] = { 1, 2, 3, 4, 5, 6 };
67         Iter b = Iter(std::begin(input));
68         Iter e = Iter(std::end(input));
69         Iter it = std::shift_right(b, e, 2);
70         assert(std::equal(std::begin(expected), std::end(expected), it, e));
71     }
72     {
73         T input[]          = { 1, 2, 3, 4, 5, 6, 7, 8 };
74         const T expected[] = { 1, 2 };
75         Iter b = Iter(std::begin(input));
76         Iter e = Iter(std::end(input));
77         Iter it = std::shift_right(b, e, 6);
78         assert(std::equal(std::begin(expected), std::end(expected), it, e));
79     }
80 
81     // n == len
82     {
83         T input[]          = { 0, 1, 2 };
84         const T expected[] = { 0, 1, 2 };
85         Iter b = Iter(std::begin(input));
86         Iter e = Iter(std::end(input));
87         Iter it = std::shift_right(b, e, std::size(input));
88         assert(std::equal(std::begin(expected), std::end(expected), b, e));
89         assert(it == e);
90     }
91 
92     // n > len
93     {
94         T input[]          = { 0, 1, 2 };
95         const T expected[] = { 0, 1, 2 };
96         Iter b = Iter(std::begin(input));
97         Iter e = Iter(std::end(input));
98         Iter it = std::shift_right(b, e, std::size(input) + 1);
99         assert(std::equal(std::begin(expected), std::end(expected), b, e));
100         assert(it == e);
101     }
102 
103     return true;
104 }
105 
main(int,char **)106 int main(int, char**)
107 {
108     test<int, forward_iterator<int*>>();
109     test<int, bidirectional_iterator<int*>>();
110     test<int, random_access_iterator<int*>>();
111     test<int, int*>();
112     test<MoveOnly, forward_iterator<MoveOnly*>>();
113     test<MoveOnly, bidirectional_iterator<MoveOnly*>>();
114     test<MoveOnly, random_access_iterator<MoveOnly*>>();
115     test<MoveOnly, MoveOnly*>();
116 
117     static_assert(test<int, forward_iterator<int*>>());
118     static_assert(test<int, bidirectional_iterator<int*>>());
119     static_assert(test<int, random_access_iterator<int*>>());
120     static_assert(test<int, int*>());
121     static_assert(test<MoveOnly, forward_iterator<MoveOnly*>>());
122     static_assert(test<MoveOnly, bidirectional_iterator<MoveOnly*>>());
123     static_assert(test<MoveOnly, random_access_iterator<MoveOnly*>>());
124     static_assert(test<MoveOnly, MoveOnly*>());
125 
126     return 0;
127 }
128