| 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::test_range; |
| 26 | using __gnu_test::forward_iterator_wrapper; |
| 27 | using __gnu_test::bidirectional_iterator_wrapper; |
| 28 | using __gnu_test::random_access_iterator_wrapper; |
| 29 | |
| 30 | namespace ranges = std::ranges; |
| 31 | |
| 32 | struct X |
| 33 | { |
| 34 | int i; |
| 35 | X (int a) : i(a) { } |
| 36 | |
| 37 | friend bool |
| 38 | operator==(const X& lhs, const X& rhs) |
| 39 | { |
| 40 | return lhs.i == rhs.i; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | static_assert(!std::is_trivially_default_constructible_v<X>); |
| 45 | |
| 46 | template<template<typename, template<typename> typename> typename container, |
| 47 | template<typename> typename wrapper, |
| 48 | typename T = int> |
| 49 | void |
| 50 | test01() |
| 51 | { |
| 52 | for (int a = 0; a <= 7; a++) |
| 53 | { |
| 54 | T x[] = {1, 2, 3, 4, 5, 6, 7}; |
| 55 | container<T, wrapper> rx(x); |
| 56 | auto i = ranges::begin(rx); |
| 57 | std::advance(i, a); |
| 58 | auto res = ranges::rotate(rx, i); |
| 59 | if (a == 0) |
| 60 | VERIFY( ranges::begin(res) == ranges::end(rx) ); |
| 61 | else |
| 62 | VERIFY( ranges::begin(res) |
| 63 | == std::next(ranges::begin(rx), |
| 64 | ranges::distance(i, ranges::end(rx))) ); |
| 65 | VERIFY( ranges::end(res) == ranges::end(rx) ); |
| 66 | for (int k = 0; k < 7; k++) |
| 67 | VERIFY( x[k] == (k+a)%7 + 1 ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | constexpr bool |
| 72 | test02() |
| 73 | { |
| 74 | int x[] = {1, 2, 3, 4}; |
| 75 | const int y[] = { 2, 3, 1, 4 }; |
| 76 | ranges::rotate(x, x+1, x+3); |
| 77 | return ranges::equal(x, y); |
| 78 | } |
| 79 | |
| 80 | int |
| 81 | main() |
| 82 | { |
| 83 | test01<test_container, forward_iterator_wrapper>(); |
| 84 | test01<test_range, forward_iterator_wrapper>(); |
| 85 | |
| 86 | test01<test_container, bidirectional_iterator_wrapper>(); |
| 87 | test01<test_range, bidirectional_iterator_wrapper>(); |
| 88 | |
| 89 | test01<test_container, random_access_iterator_wrapper>(); |
| 90 | test01<test_range, random_access_iterator_wrapper>(); |
| 91 | |
| 92 | test01<test_container, random_access_iterator_wrapper, X>(); |
| 93 | test01<test_range, random_access_iterator_wrapper, X>(); |
| 94 | |
| 95 | static_assert(test02()); |
| 96 | } |