| 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::random_access_iterator_wrapper; |
| 28 | |
| 29 | namespace ranges = std::ranges; |
| 30 | |
| 31 | struct X |
| 32 | { |
| 33 | int i; |
| 34 | X () : i(0) { } |
| 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 | T w[7]; |
| 56 | container<T, wrapper> rx(x), rw(w); |
| 57 | auto i = ranges::begin(rx); |
| 58 | std::advance(i, a); |
| 59 | auto [in,out] = ranges::rotate_copy(rx, i, ranges::begin(rw)); |
| 60 | VERIFY( in == ranges::end(rx) ); |
| 61 | VERIFY( out == ranges::end(rw) ); |
| 62 | for (int k = 0; k < 7; k++) |
| 63 | VERIFY( w[k] == (k+a)%7 + 1 ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | constexpr bool |
| 68 | test02() |
| 69 | { |
| 70 | const int x[] = {1, 2, 3, 4}; |
| 71 | int w[3]; |
| 72 | const int y[] = { 2, 3, 1}; |
| 73 | auto [in,out] = ranges::rotate_copy(x, x+1, x+3, w); |
| 74 | return (in == x+3 |
| 75 | && out == w+3 |
| 76 | && ranges::equal(w, y)); |
| 77 | } |
| 78 | |
| 79 | int |
| 80 | main() |
| 81 | { |
| 82 | test01<test_container, forward_iterator_wrapper>(); |
| 83 | test01<test_range, forward_iterator_wrapper>(); |
| 84 | |
| 85 | test01<test_container, random_access_iterator_wrapper>(); |
| 86 | test01<test_range, random_access_iterator_wrapper>(); |
| 87 | |
| 88 | test01<test_container, random_access_iterator_wrapper, X>(); |
| 89 | test01<test_range, random_access_iterator_wrapper, X>(); |
| 90 | |
| 91 | static_assert(test02()); |
| 92 | } |