summaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms/alg.modifying.operations/alg.unique/ranges_unique_copy.pass.cpp
blob: 801385cf691ad130ea5152f6d6455f8ca8db7bbe (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//===----------------------------------------------------------------------===//
//
// 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_iterator I, sentinel_for<I> S, weakly_incrementable O, class Proj = identity,
//          indirect_equivalence_relation<projected<I, Proj>> C = ranges::equal_to>
//   requires indirectly_copyable<I, O> &&
//            (forward_iterator<I> ||
//             (input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>) ||
//             indirectly_copyable_storable<I, O>)
//   constexpr unique_copy_result<I, O>
//     unique_copy(I first, S last, O result, C comp = {}, Proj proj = {});                         // Since C++20
//
// template<input_range R, weakly_incrementable O, class Proj = identity,
//          indirect_equivalence_relation<projected<iterator_t<R>, Proj>> C = ranges::equal_to>
//   requires indirectly_copyable<iterator_t<R>, O> &&
//            (forward_iterator<iterator_t<R>> ||
//             (input_iterator<O> && same_as<range_value_t<R>, iter_value_t<O>>) ||
//             indirectly_copyable_storable<iterator_t<R>, O>)
//   constexpr unique_copy_result<borrowed_iterator_t<R>, O>
//     unique_copy(R&& r, O result, C comp = {}, Proj proj = {});                                   // Since C++20

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

#include "almost_satisfies_types.h"
#include "counting_predicates.h"
#include "counting_projection.h"
#include "MoveOnly.h"
#include "test_iterators.h"

template <
    class InIter  = int*,
    class Sent    = int*,
    class OutIter = int*,
    class Comp    = std::ranges::equal_to,
    class Proj    = std::identity>
concept HasUniqueCopyIter =
    requires(InIter&& in, Sent&& sent, OutIter&& out, Comp&& comp, Proj&& proj) {
      std::ranges::unique_copy(
          std::forward<InIter>(in),
          std::forward<Sent>(sent),
          std::forward<OutIter>(out),
          std::forward<Comp>(comp),
          std::forward<Proj>(proj));
    };

static_assert(HasUniqueCopyIter<int*, int*, int*>);

// !input_iterator<I>
static_assert(!HasUniqueCopyIter<InputIteratorNotDerivedFrom, sentinel_wrapper<InputIteratorNotDerivedFrom>>);

// !sentinel_for<S, I>
static_assert(!HasUniqueCopyIter<int*, SentinelForNotSemiregular>);

// !weakly_incrementable<O>
static_assert(!HasUniqueCopyIter<int*, int*, WeaklyIncrementableNotMovable>);

// !indirect_equivalence_relation<Comp, projected<I, Proj>>
static_assert(!HasUniqueCopyIter<int*, int*, int*, ComparatorNotCopyable<int>>);

// !indirectly_copyable<I, O>
static_assert(!HasUniqueCopyIter<const int*, const int*, const int*>);

// forward_iterator<I>
// !(input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
// !indirectly_copyable_storable<I, O>
struct AssignableFromMoveOnly {
  int data;
  constexpr AssignableFromMoveOnly& operator=(MoveOnly const& m) {
    data = m.get();
    return *this;
  }
};
static_assert(HasUniqueCopyIter<MoveOnly*, MoveOnly*, AssignableFromMoveOnly*>);
// because:
static_assert(std::forward_iterator<MoveOnly*>);
static_assert(!std::same_as<std::iter_value_t<MoveOnly*>, std::iter_value_t<AssignableFromMoveOnly*>>);
static_assert(!std::indirectly_copyable_storable<MoveOnly*, AssignableFromMoveOnly*>);

// !forward_iterator<I>
// (input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
// !indirectly_copyable_storable<I, O>
struct CopyAssignableNotCopyConstructible {
  int data;
  constexpr CopyAssignableNotCopyConstructible(int i = 0) : data(i) {}
  CopyAssignableNotCopyConstructible(const CopyAssignableNotCopyConstructible&)            = delete;
  CopyAssignableNotCopyConstructible& operator=(const CopyAssignableNotCopyConstructible&) = default;
  friend constexpr bool
  operator==(CopyAssignableNotCopyConstructible const&, CopyAssignableNotCopyConstructible const&) = default;
};

using InputAndOutputIterator = cpp17_input_iterator<CopyAssignableNotCopyConstructible*>;
static_assert(std::input_iterator<InputAndOutputIterator>);
static_assert(std::output_iterator<InputAndOutputIterator, CopyAssignableNotCopyConstructible>);

static_assert(
    HasUniqueCopyIter<
        cpp20_input_iterator<CopyAssignableNotCopyConstructible*>,
        sentinel_wrapper<cpp20_input_iterator<CopyAssignableNotCopyConstructible*>>,
        InputAndOutputIterator>);
// because:
static_assert(!std::forward_iterator< cpp20_input_iterator<CopyAssignableNotCopyConstructible*>>);
static_assert(
    std::input_iterator<InputAndOutputIterator> &&
    std::same_as<std::iter_value_t<cpp20_input_iterator<CopyAssignableNotCopyConstructible*>>,
                 std::iter_value_t<InputAndOutputIterator>>);
static_assert(
    !std::indirectly_copyable_storable<
        cpp20_input_iterator<CopyAssignableNotCopyConstructible*>,
        InputAndOutputIterator>);

// !forward_iterator<I>
// !(input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
// indirectly_copyable_storable<I, O>
static_assert(
    HasUniqueCopyIter<
        cpp20_input_iterator<int*>,
        sentinel_wrapper<cpp20_input_iterator<int*>>,
        cpp20_output_iterator<int*>>);
// because:
static_assert(!std::forward_iterator<cpp20_input_iterator<int*>>);
static_assert(!std::input_iterator<cpp20_output_iterator<int*>>);
static_assert(std::indirectly_copyable_storable<cpp20_input_iterator<int*>, cpp20_output_iterator<int*>>);

// !forward_iterator<I>
// !(input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
// !indirectly_copyable_storable<I, O>
static_assert(
    !HasUniqueCopyIter<
        cpp20_input_iterator<MoveOnly*>,
        sentinel_wrapper<cpp20_input_iterator<MoveOnly*>>,
        cpp20_output_iterator<AssignableFromMoveOnly*>>);
// because:
static_assert(!std::forward_iterator<cpp20_input_iterator<MoveOnly*>>);
static_assert(!std::input_iterator<cpp20_output_iterator<MoveOnly*>>);
static_assert(
    !std::
        indirectly_copyable_storable<cpp20_input_iterator<MoveOnly*>, cpp20_output_iterator<AssignableFromMoveOnly*>>);

template < class Range, class OutIter = int*, class Comp = std::ranges::equal_to, class Proj = std::identity>
concept HasUniqueCopyRange =
    requires(Range&& range, OutIter&& out, Comp&& comp, Proj&& proj) {
      std::ranges::unique_copy(
          std::forward<Range>(range), std::forward<OutIter>(out), std::forward<Comp>(comp), std::forward<Proj>(proj));
    };

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

static_assert(HasUniqueCopyRange<R<int*>, int*>);

// !input_range<R>
static_assert(!HasUniqueCopyRange<R<InputIteratorNotDerivedFrom>>);

// !weakly_incrementable<O>
static_assert(!HasUniqueCopyIter<R<int*>, WeaklyIncrementableNotMovable>);

// !indirect_equivalence_relation<Comp, projected<I, Proj>>
static_assert(!HasUniqueCopyIter<R<int*>, int*, ComparatorNotCopyable<int>>);

// !indirectly_copyable<I, O>
static_assert(!HasUniqueCopyIter<R<const int*>, const int*>);

// !forward_iterator<iterator_t<R>>
// !(input_iterator<O> && same_as<range_value_t<R>, iter_value_t<O>>)
// !indirectly_copyable_storable<iterator_t<R>, O>
static_assert(!HasUniqueCopyIter< R<cpp20_input_iterator<MoveOnly*>>, cpp20_output_iterator<AssignableFromMoveOnly*>>);

template <class InIter, class OutIter, template <class> class SentWrapper, std::size_t N1, std::size_t N2>
constexpr void testUniqueCopyImpl(std::array<int, N1> in, std::array<int, N2> expected) {
  using Sent = SentWrapper<InIter>;

  // iterator overload
  {
    std::array<int, N2> out;
    std::same_as<std::ranges::unique_copy_result<InIter, OutIter>> decltype(auto) result =
        std::ranges::unique_copy(InIter{in.data()}, Sent{InIter{in.data() + in.size()}}, OutIter{out.begin()});
    assert(std::ranges::equal(out, expected));
    assert(base(result.in) == in.data() + in.size());
    assert(base(result.out) == out.data() + out.size());
  }

  // range overload
  {
    std::array<int, N2> out;
    std::ranges::subrange r{InIter{in.data()}, Sent{InIter{in.data() + in.size()}}};
    std::same_as<std::ranges::unique_copy_result<InIter, OutIter>> decltype(auto) result =
        std::ranges::unique_copy(r, OutIter{out.begin()});
    assert(std::ranges::equal(out, expected));
    assert(base(result.in) == in.data() + in.size());
    assert(base(result.out) == out.data() + out.size());
  }
}

template <class InIter, class OutIter, template <class> class SentWrapper>
constexpr void testImpl() {
  // no consecutive elements
  {
    std::array in{1, 2, 3, 2, 1};
    std::array expected{1, 2, 3, 2, 1};
    testUniqueCopyImpl<InIter, OutIter, SentWrapper>(in, expected);
  }

  // one group of consecutive elements
  {
    std::array in{2, 3, 3, 3, 4, 3};
    std::array expected{2, 3, 4, 3};
    testUniqueCopyImpl<InIter, OutIter, SentWrapper>(in, expected);
  }

  // multiple groups of consecutive elements
  {
    std::array in{2, 3, 3, 3, 4, 3, 3, 5, 5, 5};
    std::array expected{2, 3, 4, 3, 5};
    testUniqueCopyImpl<InIter, OutIter, SentWrapper>(in, expected);
  }

  // all the same
  {
    std::array in{1, 1, 1, 1, 1, 1};
    std::array expected{1};
    testUniqueCopyImpl<InIter, OutIter, SentWrapper>(in, expected);
  }

  // empty range
  {
    std::array<int, 0> in{};
    std::array<int, 0> expected{};
    testUniqueCopyImpl<InIter, OutIter, SentWrapper>(in, expected);
  }
}

template <class OutIter, template <class> class SentWrapper>
constexpr void withAllPermutationsOfInIter() {
  testImpl<cpp20_input_iterator<int*>, OutIter, sentinel_wrapper>();
  testImpl<forward_iterator<int*>, OutIter, SentWrapper>();
  testImpl<bidirectional_iterator<int*>, OutIter, SentWrapper>();
  testImpl<random_access_iterator<int*>, OutIter, SentWrapper>();
  testImpl<contiguous_iterator<int*>, OutIter, SentWrapper>();
  testImpl<int*, OutIter, SentWrapper>();
}

template <template <class> class SentWrapper>
constexpr void withAllPermutationsOfInIterAndOutIter() {
  withAllPermutationsOfInIter<cpp20_output_iterator<int*>, SentWrapper>();
  withAllPermutationsOfInIter<forward_iterator<int*>, SentWrapper>();
  withAllPermutationsOfInIter<bidirectional_iterator<int*>, SentWrapper>();
  withAllPermutationsOfInIter<random_access_iterator<int*>, SentWrapper>();
  withAllPermutationsOfInIter<contiguous_iterator<int*>, SentWrapper>();
  withAllPermutationsOfInIter<int*, SentWrapper>();
}

constexpr bool test() {
  withAllPermutationsOfInIterAndOutIter<std::type_identity_t>();
  withAllPermutationsOfInIterAndOutIter<sentinel_wrapper>();

  // Test the overload that re-reads from the input iterator
  // forward_iterator<I>
  // !(input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
  // !indirectly_copyable_storable<I, O>
  {
    MoveOnly in[5] = {1, 3, 3, 3, 1};
    // iterator overload
    {
      AssignableFromMoveOnly out[3] = {};
      auto result                   = std::ranges::unique_copy(in, in + 5, out);
      assert(std::ranges::equal(out, std::array{1, 3, 1}, {}, &AssignableFromMoveOnly::data));
      assert(result.in == in + 5);
      assert(result.out == out + 3);
    }
    // range overload
    {
      AssignableFromMoveOnly out[3] = {};
      auto result                   = std::ranges::unique_copy(std::ranges::subrange{in, in + 5}, out);
      assert(std::ranges::equal(out, std::array{1, 3, 1}, {}, &AssignableFromMoveOnly::data));
      assert(result.in == in + 5);
      assert(result.out == out + 3);
    }
  }

  // Test the overload that re-reads from the output iterator
  // !forward_iterator<I>
  // (input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
  // !indirectly_copyable_storable<I, O>
  {
    using InIter                             = cpp20_input_iterator<CopyAssignableNotCopyConstructible*>;
    using Sent                               = sentinel_wrapper<InIter>;
    CopyAssignableNotCopyConstructible in[6] = {1, 1, 2, 2, 3, 3};
    // iterator overload
    {
      CopyAssignableNotCopyConstructible out[3];
      auto result = std::ranges::unique_copy(InIter{in}, Sent{InIter{in + 6}}, InputAndOutputIterator{out});
      assert(std::ranges::equal(out, std::array{1, 2, 3}, {}, &CopyAssignableNotCopyConstructible::data));
      assert(base(result.in) == in + 6);
      assert(base(result.out) == out + 3);
    }
    // range overload
    {
      CopyAssignableNotCopyConstructible out[3];
      auto r      = std::ranges::subrange(InIter{in}, Sent{InIter{in + 6}});
      auto result = std::ranges::unique_copy(r, InputAndOutputIterator{out});
      assert(std::ranges::equal(out, std::array{1, 2, 3}, {}, &CopyAssignableNotCopyConstructible::data));
      assert(base(result.in) == in + 6);
      assert(base(result.out) == out + 3);
    }
  }

  // Test the overload that reads from the temporary copy of the value
  // !forward_iterator<I>
  // !(input_iterator<O> && same_as<iter_value_t<I>, iter_value_t<O>>)
  // indirectly_copyable_storable<I, O>
  {
    using InIter = cpp20_input_iterator<int*>;
    using Sent   = sentinel_wrapper<InIter>;
    int in[4]    = {1, 1, 1, 2};
    // iterator overload
    {
      int out[2];
      auto result = std::ranges::unique_copy(InIter{in}, Sent{InIter{in + 4}}, cpp20_output_iterator<int*>{out});
      assert(std::ranges::equal(out, std::array{1, 2}));
      assert(base(result.in) == in + 4);
      assert(base(result.out) == out + 2);
    }
    // range overload
    {
      int out[2];
      auto r      = std::ranges::subrange(InIter{in}, Sent{InIter{in + 4}});
      auto result = std::ranges::unique_copy(r, cpp20_output_iterator<int*>{out});
      assert(std::ranges::equal(out, std::array{1, 2}));
      assert(base(result.in) == in + 4);
      assert(base(result.out) == out + 2);
    }
  }

  struct Data {
    int data;
  };

  // Test custom comparator
  {
    std::array in{Data{4}, Data{8}, Data{8}, Data{8}};
    std::array expected{Data{4}, Data{8}};
    const auto comp = [](const Data& x, const Data& y) { return x.data == y.data; };

    // iterator overload
    {
      std::array<Data, 2> out;
      auto result = std::ranges::unique_copy(in.begin(), in.end(), out.begin(), comp);
      assert(std::ranges::equal(out, expected, comp));
      assert(base(result.in) == in.begin() + 4);
      assert(base(result.out) == out.begin() + 2);
    }

    // range overload
    {
      std::array<Data, 2> out;
      auto result = std::ranges::unique_copy(in, out.begin(), comp);
      assert(std::ranges::equal(out, expected, comp));
      assert(base(result.in) == in.begin() + 4);
      assert(base(result.out) == out.begin() + 2);
    }
  }

  // Test custom projection
  {
    std::array in{Data{4}, Data{8}, Data{8}, Data{8}};
    std::array expected{Data{4}, Data{8}};

    const auto proj = &Data::data;

    // iterator overload
    {
      std::array<Data, 2> out;
      auto result = std::ranges::unique_copy(in.begin(), in.end(), out.begin(), {}, proj);
      assert(std::ranges::equal(out, expected, {}, proj, proj));
      assert(base(result.in) == in.begin() + 4);
      assert(base(result.out) == out.begin() + 2);
    }

    // range overload
    {
      std::array<Data, 2> out;
      auto result = std::ranges::unique_copy(in, out.begin(), {}, proj);
      assert(std::ranges::equal(out, expected, {}, proj, proj));
      assert(base(result.in) == in.begin() + 4);
      assert(base(result.out) == out.begin() + 2);
    }
  }

  // Exactly last - first - 1 applications of the corresponding predicate and no
  // more than twice as many applications of any projection.
  {
    std::array in{1, 2, 3, 3, 3, 4, 3, 3, 5, 5, 6, 6, 1};
    std::array expected{1, 2, 3, 4, 3, 5, 6, 1};
    // iterator overload
    {
      std::array<int, 8> out;
      int numberOfComp = 0;
      int numberOfProj = 0;
      auto result      = std::ranges::unique_copy(
          in.begin(),
          in.end(),
          out.begin(),
          counting_predicate{std::ranges::equal_to{}, numberOfComp},
          counting_projection{numberOfProj});
      assert(std::ranges::equal(out, expected));
      assert(base(result.in) == in.end());
      assert(base(result.out) == out.end());
      assert(numberOfComp == in.size() - 1);
      assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
    }
    // range overload
    {
      std::array<int, 8> out;
      int numberOfComp = 0;
      int numberOfProj = 0;
      auto result      = std::ranges::unique_copy(
          in,
          out.begin(),
          counting_predicate{std::ranges::equal_to{}, numberOfComp},
          counting_projection{numberOfProj});
      assert(std::ranges::equal(out, expected));
      assert(base(result.in) == in.end());
      assert(base(result.out) == out.end());
      assert(numberOfComp == in.size() - 1);
      assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
    }
  }
  return true;
}

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

  return 0;
}