Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/shift_right/1.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <algorithm>
21 #include <testsuite_hooks.h>
22 #include <testsuite_iterators.h>
23
24 using __gnu_test::test_container;
25 using __gnu_test::forward_iterator_wrapper;
26 using __gnu_test::bidirectional_iterator_wrapper;
27 using __gnu_test::random_access_iterator_wrapper;
28
29 struct X
30 {
31 int a = -1;
32 bool moved_from = false;
33
34 X() = default;
35
36 X(int a)
37 : a(a)
38 { }
39
40 X(const X&) = delete;
41 X& operator=(const X&) = delete;
42
43 X(X&& other)
44 {
45 if (this != &other)
46 *this = std::move(other);
47 }
48
49 X&
50 operator=(X&& other)
51 {
52 a = other.a;
53 other.moved_from = true;
54 moved_from = false;
55 return *this;
56 }
57 };
58
59 template<int N, template<typename> typename Wrapper>
60 void
61 test01()
62 {
63 for (int n = 0; n < N+5; n++)
64 {
65 X x[N];
66 for (int i = 0; i < N; i++)
67 x[i] = X(i);
68 test_container<X, Wrapper> cx(x);
69 auto out = std::shift_right(cx.begin(), cx.end(), n);
70 if (n < N)
71 {
72 VERIFY( out.ptr == x+n );
73 for (int i = n; i < N; i++)
74 VERIFY( x[i].a == i-n );
75 for (int i = 0; i < std::min(n, N-n); i++)
76 VERIFY( x[i].moved_from );
77 for (int i = std::min(n, N-n); i < std::max(n, N-n); i++)
78 VERIFY( !x[i].moved_from );
79 }
80 else
81 {
82 VERIFY( out.ptr == x+N );
83 for (int i = 0; i < N; i++)
84 {
85 VERIFY( x[i].a == i );
86 VERIFY( !x[i].moved_from );
87 }
88 }
89 }
90 }
91
92 int
93 main()
94 {
95 test01<23, forward_iterator_wrapper>();
96 test01<23, bidirectional_iterator_wrapper>();
97 test01<23, random_access_iterator_wrapper>();
98
99 test01<24, forward_iterator_wrapper>();
100 test01<24, bidirectional_iterator_wrapper>();
101 test01<24, random_access_iterator_wrapper>();
102 }