Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/rotate/rotate.cc

raw
1 // Copyright (C) 2001-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 // 25.?? algorithms, rotate()
19
20 #include <algorithm>
21 #include <testsuite_hooks.h>
22 #include <list>
23
24 int A[] = {1, 2, 3, 4, 5, 6, 7};
25 int B[] = {2, 3, 4, 5, 6, 7, 1};
26 int C[] = {1, 2, 3, 4, 5, 6, 7};
27 int D[] = {5, 6, 7, 1, 2, 3, 4};
28 const int N = sizeof(A) / sizeof(int);
29
30 /* need a test for a forward iterator -- can't think of one that makes sense */
31
32 /* bidirectional iterator */
33 void
34 test02()
35 {
36 using std::rotate;
37 typedef std::list<int> Container;
38
39 Container a(A, A + N);
40 VERIFY(std::equal(a.begin(), a.end(), A));
41
42 Container::iterator i = a.begin();
43 rotate(a.begin(), ++i, a.end());
44 VERIFY(std::equal(a.begin(), a.end(), B));
45
46 i = a.end();
47 rotate(a.begin(), --i, a.end());
48 VERIFY(std::equal(a.begin(), a.end(), C));
49
50 i = a.begin();
51 std::advance(i, 3);
52 rotate(a.begin(), ++i, a.end());
53 VERIFY(std::equal(a.begin(), a.end(), D));
54 }
55
56 /* random iterator */
57 void
58 test03()
59 {
60 using std::rotate;
61 rotate(A, A + 1, A + N);
62 VERIFY(std::equal(A, A + N, B));
63
64 rotate(A, A + N - 1, A + N);
65 VERIFY(std::equal(A, A + N, C));
66
67 rotate(A, A + 4, A + N);
68 VERIFY(std::equal(A, A + N, D));
69 }
70
71 int
72 main()
73 {
74 test02();
75 test03();
76 return 0;
77 }