brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · da39f95 Raw
146 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// template<forward_iterator I, sentinel_for<I> S, weakly_incrementable O>12//   requires indirectly_copyable<I, O>13//   constexpr ranges::rotate_copy_result<I, O>14//     ranges::rotate_copy(I first, I middle, S last, O result);15// template<forward_range R, weakly_incrementable O>16//   requires indirectly_copyable<iterator_t<R>, O>17//   constexpr ranges::rotate_copy_result<borrowed_iterator_t<R>, O>18//     ranges::rotate_copy(R&& r, iterator_t<R> middle, O result);19 20// <algorithm>21 22#include <algorithm>23#include <array>24#include <cassert>25#include <ranges>26 27#include "almost_satisfies_types.h"28#include "test_iterators.h"29#include "type_algorithms.h"30 31template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>32concept HasRotateCopyIt =33    requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };34 35template <class Range, class Out = int*>36concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };37 38static_assert(HasRotateCopyIt<int*>);39static_assert(!HasRotateCopyIt<ForwardIteratorNotDerivedFrom>);40static_assert(!HasRotateCopyIt<ForwardIteratorNotIncrementable>);41static_assert(!HasRotateCopyIt<int*, SentinelForNotSemiregular>);42static_assert(!HasRotateCopyIt<int*, SentinelForNotWeaklyEqualityComparableWith>);43static_assert(!HasRotateCopyIt<int*, OutputIteratorNotIndirectlyWritable>);44static_assert(!HasRotateCopyIt<int*, OutputIteratorNotInputOrOutputIterator>);45 46static_assert(HasRotateCopyR<UncheckedRange<int*>>);47static_assert(!HasRotateCopyR<ForwardRangeNotDerivedFrom>);48static_assert(!HasRotateCopyR<ForwardRangeNotIncrementable>);49static_assert(!HasRotateCopyR<UncheckedRange<int*, SentinelForNotSemiregular>>);50static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);51static_assert(!HasRotateCopyR<UncheckedRange<int*>, OutputIteratorNotInputOrOutputIterator>);52 53static_assert(std::is_same_v<std::ranges::rotate_copy_result<int, int>, std::ranges::in_out_result<int, int>>);54 55template <class Iter, class OutIter, class Sent, int N>56constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) {57  {58    std::array<int, N> out;59    std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = std::ranges::rotate_copy(60        Iter(value.data()), Iter(value.data() + middle), Sent(Iter(value.data() + value.size())), OutIter(out.data()));61    assert(base(ret.in) == value.data() + value.size());62    assert(base(ret.out) == out.data() + out.size());63    assert(out == expected);64  }65  {66    std::array<int, N> out;67    auto range = std::ranges::subrange(Iter(value.data()), Sent(Iter(value.data() + value.size())));68    std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =69        std::ranges::rotate_copy(range, Iter(value.data() + middle), OutIter(out.data()));70    assert(base(ret.in) == value.data() + value.size());71    assert(base(ret.out) == out.data() + out.size());72    assert(out == expected);73  }74}75 76template <class Iter, class OutIter, class Sent>77constexpr void test_iterators() {78  // simple test79  test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});80 81  // check that an empty range works82  test<Iter, OutIter, Sent, 0>({}, 0, {});83 84  // check that a single element range works85  test<Iter, OutIter, Sent, 1>({1}, 0, {1});86 87  // check that a two element range works88  test<Iter, OutIter, Sent, 2>({1, 2}, 1, {2, 1});89 90  // rotate on the first element91  test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 0, {1, 2, 3, 4, 5, 6, 7});92 93  // rotate on the second element94  test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 1, {2, 3, 4, 5, 6, 7, 1});95 96  // rotate on the last element97  test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 6, {7, 1, 2, 3, 4, 5, 6});98 99  // rotate on the one-past-the-end pointer100  test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});101}102 103constexpr bool test() {104  types::for_each(types::forward_iterator_list<const int*>(), []<class Iter>() {105    types::for_each(106        types::concatenate_t<types::forward_iterator_list<int*>, types::type_list<cpp20_output_iterator<int*> > >(),107        []<class OutIter>() { test_iterators<Iter, OutIter, Iter>(); });108  });109 110  {111    struct AssignmentCounter {112      int* counter;113 114      constexpr AssignmentCounter(int* counter_) : counter(counter_) {}115      constexpr AssignmentCounter& operator=(const AssignmentCounter&) {116        ++*counter;117        return *this;118      }119    };120 121    {122      int c                 = 0;123      AssignmentCounter a[] = {&c, &c, &c, &c};124      AssignmentCounter b[] = {&c, &c, &c, &c};125      std::ranges::rotate_copy(a, a + 2, a + 4, b);126      assert(c == 4);127    }128    {129      int c                 = 0;130      AssignmentCounter a[] = {&c, &c, &c, &c};131      AssignmentCounter b[] = {&c, &c, &c, &c};132      std::ranges::rotate_copy(a, a + 2, b);133      assert(c == 4);134    }135  }136 137  return true;138}139 140int main(int, char**) {141  test();142  static_assert(test());143 144  return 0;145}146