aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/algorithms/alg.sorting/alg.sort/partial.sort.copy/ranges_partial_sort_copy.pass.cpp
blob: 15c0b06c7e0226e4a634e0c696f23ddf170f36ea (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
//===----------------------------------------------------------------------===//
//
// 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
// UNSUPPORTED: libcpp-has-no-incomplete-ranges

// <algorithm>

// template<input_iterator I1, sentinel_for<I1> S1,
//          random_access_iterator I2, sentinel_for<I2> S2,
//          class Comp = ranges::less, class Proj1 = identity, class Proj2 = identity>
//   requires indirectly_copyable<I1, I2> && sortable<I2, Comp, Proj2> &&
//            indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
//   constexpr partial_sort_copy_result<I1, I2>
//     partial_sort_copy(I1 first, S1 last, I2 result_first, S2 result_last,
//                       Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {});               // Since C++20
//
// template<input_range R1, random_access_range R2, class Comp = ranges::less,
//          class Proj1 = identity, class Proj2 = identity>
//   requires indirectly_copyable<iterator_t<R1>, iterator_t<R2>> &&
//            sortable<iterator_t<R2>, Comp, Proj2> &&
//            indirect_strict_weak_order<Comp, projected<iterator_t<R1>, Proj1>,
//                                       projected<iterator_t<R2>, Proj2>>
//   constexpr partial_sort_copy_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
//     partial_sort_copy(R1&& r, R2&& result_r, Comp comp = {},
//                       Proj1 proj1 = {}, Proj2 proj2 = {});                               // Since C++20

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

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

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

template <class Iter1 = int*, class Sent1 = int*, class Iter2 = int*, class Sent2 = int*,
          class Comp = std::ranges::less>
concept HasPartialSortCopyIter =
    requires(Iter1&& first1, Sent1&& last1, Iter2&& first2, Sent2&& last2, Comp&& comp) {
      std::ranges::partial_sort_copy(std::forward<Iter1>(first1), std::forward<Sent1>(last1),
          std::forward<Iter2>(first2), std::forward<Sent2>(last2), std::forward<Comp>(comp));
    };

static_assert(HasPartialSortCopyIter<int*, int*, int*, int*, std::ranges::less>);

// !input_iterator<I1>
static_assert(!HasPartialSortCopyIter<InputIteratorNotDerivedFrom>);
static_assert(!HasPartialSortCopyIter<InputIteratorNotIndirectlyReadable>);
static_assert(!HasPartialSortCopyIter<InputIteratorNotInputOrOutputIterator>);

// !sentinel_for<S1, I1>
static_assert(!HasPartialSortCopyIter<int*, SentinelForNotSemiregular>);
static_assert(!HasPartialSortCopyIter<int*, SentinelForNotWeaklyEqualityComparableWith>);

// !random_access_iterator<I2>
static_assert(!HasPartialSortCopyIter<int*, int*, RandomAccessIteratorNotDerivedFrom>);
static_assert(!HasPartialSortCopyIter<int*, int*, RandomAccessIteratorBadIndex>);

// !sentinel_for<S2, I2>
static_assert(!HasPartialSortCopyIter<int*, int*, int*, SentinelForNotSemiregular>);
static_assert(!HasPartialSortCopyIter<int*, int*, int*, SentinelForNotWeaklyEqualityComparableWith>);

// !indirect_unary_predicate<projected<I, Proj>>
static_assert(!HasPartialSortCopyIter<int*, int*, int*, int*, IndirectUnaryPredicateNotPredicate>);
static_assert(!HasPartialSortCopyIter<int*, int*, int*, int*, IndirectUnaryPredicateNotCopyConstructible>);

// !indirectly_copyable<I1, I2>
static_assert(!HasPartialSortCopyIter<int*, int*, MoveOnly*>);

// !sortable<I2, Comp, Proj2>
static_assert(!HasPartialSortCopyIter<int*, int*, const int*, const int*>);

struct NoComparator {};
// !indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
static_assert(!HasPartialSortCopyIter<NoComparator*, NoComparator*, NoComparator*, NoComparator*>);

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

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

template <class Range1 = R<int*>, class Range2 = R<int*>, class Comp = std::ranges::less>
concept HasPartialSortCopyRange =
    requires(Range1&& range1, Range2&& range2, Comp&& comp) {
      std::ranges::partial_sort_copy(std::forward<Range1>(range1), std::forward<Range2>(range2),
          std::forward<Comp>(comp));
    };

static_assert(HasPartialSortCopyRange<R<int*>, R<int*>, std::ranges::less>);

// !input_range<R1>
static_assert(!HasPartialSortCopyRange<InputRangeNotDerivedFrom>);
static_assert(!HasPartialSortCopyRange<InputRangeNotIndirectlyReadable>);
static_assert(!HasPartialSortCopyRange<InputRangeNotInputOrOutputIterator>);

// !random_access_range<R2>
static_assert(!HasPartialSortCopyRange<R<int*>, RandomAccessRangeNotDerivedFrom>);
static_assert(!HasPartialSortCopyRange<R<int*>, RandomAccessRangeBadIndex>);

// !indirectly_copyable<iterator_t<R1>, iterator_t<R2>>
static_assert(!HasPartialSortCopyRange<R<int*>, R<MoveOnly*>>);

// !sortable<iterator_t<R2>, Comp, Proj2>
static_assert(!HasPartialSortCopyRange<R<int*>, R<const int*>>);

// !indirect_strict_weak_order<Comp, projected<iterator_t<R1>, Proj1>, projected<iterator_t<R2>, Proj2>>
static_assert(!HasPartialSortCopyRange<R<NoComparator*>, R<NoComparator*>>);

static_assert(std::is_same_v<std::ranges::partial_sort_copy_result<int, int>, std::ranges::in_out_result<int, int>>);

template <class Iter, class Sent, class OutIter, class OutSent, size_t N>
constexpr void test_one(
    std::array<int, N> input, size_t input_size, size_t output_size, std::array<int, N> sorted) {
  assert(input_size <= N);
  assert(output_size <= N + 1); // To support testing the case where output size exceeds input size.

  using ResultT = std::ranges::partial_sort_copy_result<Iter, OutIter>;
  // To support testing the case where output size exceeds input size; also makes sure calling `out.data() + int()` is
  // valid.
  constexpr size_t OutputSize = N + 1;
  size_t result_size = std::ranges::min(input_size, output_size);

  auto begin = input.data();
  auto end = input.data() + input_size;

  { // (iterator, sentinel) overload.
    std::array<int, OutputSize> out;
    auto out_begin = out.data();
    auto out_end = out.data() + output_size;

    std::same_as<ResultT> decltype(auto) result = std::ranges::partial_sort_copy(
        Iter(begin), Sent(Iter(end)), OutIter(out_begin), OutSent(OutIter(out_end)));

    assert(base(result.in) == input.data() + input_size);
    assert(base(result.out) == out.data() + result_size);

    assert(std::ranges::equal(std::ranges::subrange(out.begin(), out.begin() + result_size),
           std::ranges::subrange(sorted.begin(), sorted.begin() + result_size)));
  }

  { // (range) overload.
    std::array<int, OutputSize> out;
    auto out_begin = out.data();
    auto out_end = out.data() + output_size;
    auto in_range = std::ranges::subrange(Iter(begin), Sent(Iter(end)));
    auto out_range = std::ranges::subrange(OutIter(out_begin), OutSent(OutIter(out_end)));

    std::same_as<ResultT> decltype(auto) result = std::ranges::partial_sort_copy(in_range, out_range);

    assert(base(result.in) == input.data() + input_size);
    assert(base(result.out) == out.data() + result_size);

    assert(std::ranges::equal(std::ranges::subrange(out.begin(), out.begin() + result_size),
           std::ranges::subrange(sorted.begin(), sorted.begin() + result_size)));
  }

}

template <class Iter, class Sent, class OutIter, class OutSent, size_t N>
constexpr void test_all_subsequences(const std::array<int, N> input) {
  auto sorted = input;
  std::sort(sorted.begin(), sorted.end());

  // Whole input, increasing output size. Also check the case when `output_size` exceeds input size.
  for (size_t out_size = 0; out_size <= N + 1; ++out_size) {
    test_one<Iter, Sent, OutIter, OutSent>(input, N, out_size, sorted);
  }
}

template <class InIter, class Sent1, class OutIter, class Sent2>
constexpr void test_iterators_in_sent1_out_sent2() {
  // Empty sequence.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2, 0>({});

  // 1-element sequence.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1});

  // 2-element sequence.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1});

  // 3-element sequence.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3});

  // Longer sequence.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3, 6, 8, 4, 11, 5});

  // Longer sequence with duplicates.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{2, 1, 3, 6, 2, 8, 6});

  // All elements are the same.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1, 1, 1});

  // Already sorted.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{1, 2, 3, 4, 5});

  // Descending.
  test_all_subsequences<InIter, Sent1, OutIter, Sent2>(std::array{5, 4, 3, 2, 1});
}

template <class InIter, class Sent1, class OutIter>
constexpr void test_iterators_in_sent1_out() {
  test_iterators_in_sent1_out_sent2<InIter, Sent1, OutIter, OutIter>();
  test_iterators_in_sent1_out_sent2<InIter, Sent1, OutIter, sentinel_wrapper<OutIter>>();
}

template <class InIter, class Sent1>
constexpr void test_iterators_in_sent1() {
  test_iterators_in_sent1_out<InIter, Sent1, random_access_iterator<int*>>();
}

template <class InIter>
constexpr void test_iterators_in() {
  if constexpr (std::sentinel_for<InIter, InIter>) {
    test_iterators_in_sent1<InIter, InIter>();
  }
  test_iterators_in_sent1<InIter, sentinel_wrapper<InIter>>();
}

constexpr void test_iterators() {
  test_iterators_in<cpp20_input_iterator<int*>>();
  // Omitting other iterator types to reduce the combinatorial explosion.
  test_iterators_in<random_access_iterator<int*>>();
  test_iterators_in<const int*>();
}

constexpr bool test() {
  test_iterators();

  { // A custom comparator works.
    const std::array in = {1, 2, 3, 4, 5};
    std::ranges::greater comp;

    {
      std::array<int, 2> out;

      auto result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), comp);
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{5, 4}));
    }

    {
      std::array<int, 2> out;

      auto result = std::ranges::partial_sort_copy(in, out, comp);
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{5, 4}));
    }
  }

  { // A custom projection works.
    struct A {
      int a;

      constexpr A() = default;
      constexpr A(int value) : a(value) {}
      constexpr auto operator<=>(const A&) const = default;
    };

    const std::array in = {2, 1, 3};
    auto proj1 = [](int value) { return value * -1; };
    auto proj2 = [](A value) { return value.a * -1; };
    // The projections negate the argument, so the array will appear to be sorted in descending order: [3, 2, 1]
    // (the projections make it appear to the algorithm as [-3, -2, -1]).

    {
      std::array<A, 2> out;

      // No projections: ascending order.
      auto result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), {});
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{1, 2}));
      // Using projections: descending order.
      result = std::ranges::partial_sort_copy(in.begin(), in.end(), out.begin(), out.end(), {}, proj1, proj2);
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{3, 2}));
    }

    {
      std::array<int, 2> out;

      auto result = std::ranges::partial_sort_copy(in, out, {});
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{1, 2}));
      result = std::ranges::partial_sort_copy(in, out, {}, proj1, proj2);
      assert(std::ranges::equal(std::ranges::subrange(out.begin(), result.out), std::array{3, 2}));
    }
  }

  return true;
}

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

  return 0;
}