summaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/ranges_generate.pass.cpp
blob: 56fbe58e2dfd0917a5da9999c66a36ef048b870d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

// <algorithm>

// template<input_or_output_iterator O, sentinel_for<O> S, copy_constructible F>
//   requires invocable<F&> && indirectly_writable<O, invoke_result_t<F&>>
//   constexpr O generate(O first, S last, F gen);                                                  // Since C++20
//
// template<class R, copy_constructible F>
//   requires invocable<F&> && output_range<R, invoke_result_t<F&>>
//   constexpr borrowed_iterator_t<R> generate(R&& r, F gen);                                       // Since C++20

#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <ranges>
#include <utility>

#include "almost_satisfies_types.h"
#include "test_iterators.h"

struct IntGen {
  int operator()() const;
};

struct UncopyableGen {
  UncopyableGen(const UncopyableGen&) = delete;
  int operator()() const;
};
static_assert(!std::copy_constructible<UncopyableGen>);
static_assert(std::invocable<UncopyableGen>);

struct UninvocableGen {
};
static_assert(std::copy_constructible<UninvocableGen>);
static_assert(!std::invocable<UninvocableGen>);

struct IntPtrGen {
  int* operator()() const;
};

// Test constraints of the (iterator, sentinel) overload.
// ======================================================

template <class Iter = int*, class Sent = int*, class Gen = IntGen>
concept HasGenerateIter =
    requires(Iter&& iter, Sent&& sent, Gen&& gen) {
      std::ranges::generate(std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<Gen>(gen));
    };

static_assert(HasGenerateIter<int*, int*, IntGen>);

// !input_or_output_iterator<O>
static_assert(!HasGenerateIter<InputIteratorNotInputOrOutputIterator>);

// !sentinel_for<S, O>
static_assert(!HasGenerateIter<int*, SentinelForNotSemiregular>);
static_assert(!HasGenerateIter<int*, SentinelForNotWeaklyEqualityComparableWith>);

// !copy_constructible<F>
static_assert(!HasGenerateIter<int*, int*, UncopyableGen>);

// !invocable<F&>
static_assert(!HasGenerateIter<int*, int*, UninvocableGen>);

// !indirectly_writable<O, invoke_result_t<F&>>
static_assert(!HasGenerateIter<int*, int*, IntPtrGen>);

// Test constraints of the (range) overload.
// =========================================

template <class Range, class Gen = IntGen>
concept HasGenerateRange =
    requires(Range&& range, Gen&& gen) {
      std::ranges::generate(std::forward<Range>(range), std::forward<Gen>(gen));
    };

template <class T>
using R = UncheckedRange<T>;

static_assert(HasGenerateRange<R<int*>, IntGen>);

// !copy_constructible<F>
static_assert(!HasGenerateRange<R<int*>, UncopyableGen>);

// !invocable<F&>
static_assert(!HasGenerateRange<R<int*>, UninvocableGen>);

// !output_range<R, invoke_result_t<F&>>
static_assert(!HasGenerateRange<InputRangeNotInputOrOutputIterator>);
static_assert(!HasGenerateRange<R<int*>, IntPtrGen>);

template <class Iter, class Sent, std::size_t N, class Gen>
constexpr void test_one(const std::array<int, N> input, Gen gen, std::array<int, N> expected) {
  { // (iterator, sentinel) overload.
    auto in = input;
    auto begin = Iter(in.data());
    auto end = Sent(Iter(in.data() + in.size()));

    std::same_as<Iter> decltype(auto) result = std::ranges::generate(std::move(begin), std::move(end), gen);
    assert(base(result) == in.data() + in.size());
    assert(in == expected);
  }

  { // (range) overload.
    auto in = input;
    auto begin = Iter(in.data());
    auto end = Sent(Iter(in.data() + in.size()));
    auto range = std::ranges::subrange(std::move(begin), std::move(end));

    // For some reason `ranges::generate` accepts both input and output iterators but only output (not input) ranges.
    if constexpr (std::ranges::output_range<decltype(range), std::invoke_result_t<Gen&>>) {
      std::same_as<Iter> decltype(auto) result = std::ranges::generate(std::move(range), gen);
      assert(base(result) == in.data() + in.size());
      assert(in == expected);
    }
  }
}

template <class Iter, class Sent>
constexpr void test_iter_sent() {
  auto gen = [ctr = 1] () mutable { return ctr++; };

  // Empty sequence.
  test_one<Iter, Sent, 0>({}, gen, {});
  // 1-element sequence.
  test_one<Iter, Sent>(std::array{-10}, gen, {1});
  // Longer sequence.
  test_one<Iter, Sent>(std::array<int, 5>{}, gen, {1, 2, 3, 4, 5});
}

template <class Iter>
constexpr void test_iter() {
  if constexpr (std::sentinel_for<Iter, Iter>) {
    test_iter_sent<Iter, Iter>();
  }
  test_iter_sent<Iter, sentinel_wrapper<Iter>>();
}

constexpr void test_iterators() {
  test_iter<cpp17_input_iterator<int*>>();
  test_iter<cpp20_input_iterator<int*>>();
  test_iter<cpp17_output_iterator<int*>>();
  test_iter<cpp20_output_iterator<int*>>();
  test_iter<forward_iterator<int*>>();
  test_iter<bidirectional_iterator<int*>>();
  test_iter<random_access_iterator<int*>>();
  test_iter<contiguous_iterator<int*>>();
  test_iter<int*>();
}

constexpr bool test() {
  test_iterators();

  { // Complexity: exactly N evaluations of `gen()` and assignments.
    struct AssignedOnce {
      bool assigned = false;
      constexpr AssignedOnce& operator=(const AssignedOnce&) {
        assert(!assigned);
        assigned = true;
        return *this;
      }
    };

    { // (iterator, sentinel) overload.
      int gen_invocations = 0;
      auto gen = [&gen_invocations] { ++gen_invocations; return AssignedOnce(); };
      constexpr std::size_t N = 10;
      std::array<AssignedOnce, N> in;

      std::ranges::generate(in.begin(), in.end(), gen);
      assert(std::ranges::all_of(in, &AssignedOnce::assigned));
      assert(gen_invocations == N);
    }

    { // (range) overload.
      int gen_invocations = 0;
      auto gen = [&gen_invocations] { ++gen_invocations; return AssignedOnce(); };
      constexpr std::size_t N = 10;
      std::array<AssignedOnce, N> in;

      std::ranges::generate(in, gen);
      assert(std::ranges::all_of(in, &AssignedOnce::assigned));
      assert(gen_invocations == N);
    }
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}