File: iter_move.pass.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,245,064 kB
  • sloc: cpp: 7,619,731; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,676; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (420 lines) | stat: -rw-r--r-- 15,865 bytes parent folder | download | duplicates (3)
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// REQUIRES: std-at-least-c++23

// <ranges>

// friend constexpr decltype(auto) iter_move(const iterator& x);

#include <ranges>

#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <utility>
#include <vector>

#include "../types.h"

class MoveOnlyInt {
public:
  enum Status { constructed, move_constructed, moved_from_this };

  MoveOnlyInt() = default;
  constexpr MoveOnlyInt(int val) : val_(val) {}

  constexpr MoveOnlyInt(MoveOnlyInt&& other) noexcept : val_(other.val_), status_(move_constructed) {
    other.val_    = -1;
    other.status_ = moved_from_this;
  }

  constexpr MoveOnlyInt(const MoveOnlyInt&& other) noexcept : val_(other.val_), status_(move_constructed) {
    other.val_    = -1;
    other.status_ = moved_from_this;
  }

  MoveOnlyInt(const MoveOnlyInt&) { assert(false); } // Should never be called in this test.

  MoveOnlyInt& operator=(MoveOnlyInt&&) { // Should never be called in this test.
    assert(false);
    return *this;
  }

  constexpr bool was_normally_constructed() const { return status_ == constructed; }
  constexpr bool was_move_constructed() const { return status_ == move_constructed; }
  constexpr bool was_moved_from() const { return status_ == moved_from_this; }

  friend constexpr bool operator==(const MoveOnlyInt& left, int right) { return left.val_ == right; }
  friend constexpr bool operator==(const MoveOnlyInt& left, const MoveOnlyInt& right) {
    return left.val_ == right.val_;
  }

private:
  mutable int val_       = -1;
  mutable Status status_ = constructed;
};

static_assert(std::movable<MoveOnlyInt>);

struct ProxyRvalueRef {
  MoveOnlyInt&& val;
};

class CommonProxyRvalueRef {
public:
  constexpr CommonProxyRvalueRef(ProxyRvalueRef i) : val_(std::move(i.val)) {}
  constexpr CommonProxyRvalueRef(MoveOnlyInt i) : val_(std::move(i)) {}

  constexpr MoveOnlyInt&& get() { return std::move(val_); }

private:
  MoveOnlyInt val_;
};

template <template <class> class TQual, template <class> class UQual>
struct std::basic_common_reference<ProxyRvalueRef, MoveOnlyInt, TQual, UQual> {
  using type = CommonProxyRvalueRef;
};

template <template <class> class TQual, template <class> class UQual>
struct std::basic_common_reference<MoveOnlyInt, ProxyRvalueRef, TQual, UQual> {
  using type = CommonProxyRvalueRef;
};

static_assert(std::common_reference_with<MoveOnlyInt&&, ProxyRvalueRef>);
static_assert(std::common_reference_with<MoveOnlyInt&&, CommonProxyRvalueRef>);

class ProxyIter {
public:
  using value_type      = MoveOnlyInt;
  using difference_type = std::ptrdiff_t;

  constexpr ProxyIter() : ptr_(nullptr) {}
  constexpr explicit ProxyIter(MoveOnlyInt* it) : ptr_(std::move(it)) {}

  constexpr decltype(auto) operator*() const { return *ptr_; }

  constexpr ProxyIter& operator++() {
    ++ptr_;
    return *this;
  }

  constexpr ProxyIter operator++(int) {
    ProxyIter copy = *this;
    ++ptr_;
    return copy;
  }

  constexpr ProxyIter& operator--() {
    --ptr_;
    return *this;
  }

  constexpr ProxyIter operator--(int) {
    ProxyIter copy = *this;
    --ptr_;
    return copy;
  }

  friend bool operator==(const ProxyIter&, const ProxyIter&) = default;

  friend constexpr ProxyRvalueRef iter_move(const ProxyIter iter) {
    return ProxyRvalueRef{std::ranges::iter_move(iter.ptr_)};
  }

private:
  MoveOnlyInt* ptr_;
};

static_assert(std::forward_iterator<ProxyIter>);

template <std::forward_iterator Iter>
class IterMoveTrackingIterator {
public:
  using value_type      = std::iter_value_t<Iter>;
  using difference_type = std::iter_difference_t<Iter>;

  IterMoveTrackingIterator() = default;
  constexpr explicit IterMoveTrackingIterator(Iter iter, bool* flag = nullptr) : iter_(std::move(iter)), flag_(flag) {}

  constexpr IterMoveTrackingIterator& operator++() {
    ++iter_;
    return *this;
  }

  constexpr IterMoveTrackingIterator operator++(int) {
    auto tmp = *this;
    ++*this;
    return tmp;
  }

  constexpr decltype(auto) operator*() const { return *iter_; }

  constexpr bool operator==(const IterMoveTrackingIterator& other) const { return iter_ == other.iter_; }

  friend constexpr decltype(auto) iter_move(const IterMoveTrackingIterator& iter) {
    assert(iter.flag_ != nullptr);
    *iter.flag_ = true;
    return std::ranges::iter_move(iter.iter_);
  }

private:
  Iter iter_  = Iter();
  bool* flag_ = nullptr;
};

static_assert(std::forward_iterator<IterMoveTrackingIterator<int*>> &&
              !std::bidirectional_iterator<IterMoveTrackingIterator<int*>>);

constexpr bool test() {
  { // Test `iter_move` when result is true rvalue reference. Test return types.
    using V       = std::array<std::array<char, 1>, 2>;
    using Pattern = std::array<char, 1>;
    using JWV     = std::ranges::join_with_view<std::ranges::owning_view<V>, std::ranges::owning_view<Pattern>>;

    JWV jwv(V{{{'0'}, {'1'}}}, Pattern{','});

    {
      auto it                                     = jwv.begin();
      std::same_as<char&&> decltype(auto) v_rref1 = iter_move(it);
      std::same_as<char&&> decltype(auto) v_rref2 = iter_move(std::as_const(it));
      std::same_as<char&&> decltype(auto) v_rref3 = std::ranges::iter_move(it);
      std::same_as<char&&> decltype(auto) v_rref4 = std::ranges::iter_move(std::as_const(it));
      assert(std::ranges::equal(std::array{v_rref1, v_rref2, v_rref3, v_rref4}, std::views::repeat('0', 4)));

      ++it; // `it` points to element of `Pattern` from here
      std::same_as<char&&> decltype(auto) pattern_rref1 = iter_move(it);
      std::same_as<char&&> decltype(auto) pattern_rref2 = iter_move(std::as_const(it));
      std::same_as<char&&> decltype(auto) pattern_rref3 = std::ranges::iter_move(it);
      std::same_as<char&&> decltype(auto) pattern_rref4 = std::ranges::iter_move(std::as_const(it));
      assert(std::ranges::equal(
          std::array{pattern_rref1, pattern_rref2, pattern_rref3, pattern_rref4}, std::views::repeat(',', 4)));
    }

    {
      auto cit                                           = std::prev(std::as_const(jwv).end());
      std::same_as<const char&&> decltype(auto) cv_rref1 = iter_move(cit);
      std::same_as<const char&&> decltype(auto) cv_rref2 = iter_move(std::as_const(cit));
      std::same_as<const char&&> decltype(auto) cv_rref3 = std::ranges::iter_move(cit);
      std::same_as<const char&&> decltype(auto) cv_rref4 = std::ranges::iter_move(std::as_const(cit));
      assert(std::ranges::equal(std::array{cv_rref1, cv_rref2, cv_rref3, cv_rref4}, std::views::repeat('1', 4)));

      cit--; // `cit` points to element of `Pattern` from here
      std::same_as<const char&&> decltype(auto) cpattern_rref1 = iter_move(cit);
      std::same_as<const char&&> decltype(auto) cpattern_rref2 = iter_move(std::as_const(cit));
      std::same_as<const char&&> decltype(auto) cpattern_rref3 = std::ranges::iter_move(cit);
      std::same_as<const char&&> decltype(auto) cpattern_rref4 = std::ranges::iter_move(std::as_const(cit));
      assert(std::ranges::equal(
          std::array{cpattern_rref1, cpattern_rref2, cpattern_rref3, cpattern_rref4}, std::views::repeat(',', 4)));
    }
  }

  { // Test `iter_move` when result is true rvalue reference. Test moving.
    using Inner   = std::vector<MoveOnlyInt>;
    using V       = std::vector<Inner>;
    using Pattern = std::vector<MoveOnlyInt>;
    using JWV     = std::ranges::join_with_view<std::ranges::owning_view<V>, std::ranges::owning_view<Pattern>>;

    V v;
    v.reserve(2);
    v.emplace_back(std::ranges::to<Inner>(std::views::iota(0, 4)));
    v.emplace_back(std::ranges::to<Inner>(std::views::iota(12, 16)));
    JWV jwv(std::move(v), std::ranges::to<Pattern>(std::views::iota(4, 12)));
    assert(std::ranges::all_of(jwv, &MoveOnlyInt::was_normally_constructed));

    {
      std::vector<MoveOnlyInt> values;
      values.reserve(8);

      auto it = jwv.begin();
      values.emplace_back(iter_move(it));
      ++it;
      values.emplace_back(iter_move(std::as_const(it)));
      it++;
      values.emplace_back(std::ranges::iter_move(it));
      ++it;
      values.emplace_back(std::ranges::iter_move(std::as_const(it)));
      it++; // `it` points to element of `Pattern` from here
      values.emplace_back(iter_move(it));
      ++it;
      values.emplace_back(iter_move(std::as_const(it)));
      it++;
      values.emplace_back(std::ranges::iter_move(it));
      ++it;
      values.emplace_back(std::ranges::iter_move(std::as_const(it)));

      assert(std::ranges::equal(values, std::views::iota(0, 8)));
      assert(std::ranges::all_of(values, &MoveOnlyInt::was_move_constructed));
    }

    {
      std::vector<MoveOnlyInt> values;
      values.reserve(8);

      auto cit = std::prev(std::as_const(jwv).end());
      values.emplace_back(iter_move(cit));
      cit--;
      values.emplace_back(iter_move(std::as_const(cit)));
      --cit;
      values.emplace_back(std::ranges::iter_move(cit));
      cit--;
      values.emplace_back(std::ranges::iter_move(std::as_const(cit)));
      --cit; // `it` points to element of `Pattern` from here
      values.emplace_back(iter_move(cit));
      cit--;
      values.emplace_back(iter_move(std::as_const(cit)));
      --cit;
      values.emplace_back(std::ranges::iter_move(cit));
      cit--;
      values.emplace_back(std::ranges::iter_move(std::as_const(cit)));

      assert(std::ranges::equal(std::views::reverse(values), std::views::iota(8, 16)));
      assert(std::ranges::all_of(values, &MoveOnlyInt::was_move_constructed));
    }

    assert(std::ranges::all_of(jwv, &MoveOnlyInt::was_moved_from));
  }

  { // Test `iter_move` when result is proxy rvalue reference type, which is different from
    // range_rvalue_reference_t<InnerRng> and range_rvalue_reference_t<Pattern>.
    using Inner   = std::vector<MoveOnlyInt>;
    using V       = std::vector<Inner>;
    using Pattern = std::ranges::subrange<ProxyIter, ProxyIter>;
    using JWV     = std::ranges::join_with_view<std::ranges::owning_view<V>, Pattern>;

    static_assert(!std::same_as<std::ranges::range_rvalue_reference_t<V>, std::ranges::range_rvalue_reference_t<JWV>>);
    static_assert(
        !std::same_as<std::ranges::range_rvalue_reference_t<Pattern>, std::ranges::range_rvalue_reference_t<JWV>>);
    static_assert(std::same_as<CommonProxyRvalueRef, std::ranges::range_rvalue_reference_t<JWV>>);

    V v;
    v.reserve(2);
    v.emplace_back(std::ranges::to<Inner>(std::views::iota(0, 4)));
    v.emplace_back(std::ranges::to<Inner>(std::views::iota(12, 16)));

    auto pattern = std::ranges::to<std::vector<MoveOnlyInt>>(std::views::iota(4, 12));
    Pattern pattern_as_subrange(ProxyIter{pattern.data()}, ProxyIter{pattern.data() + pattern.size()});

    JWV jwv(std::move(v), pattern_as_subrange);
    assert(std::ranges::all_of(jwv, &MoveOnlyInt::was_normally_constructed));

    {
      std::vector<MoveOnlyInt> values;
      values.reserve(8);

      auto it                                                 = jwv.begin();
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref1 = iter_move(it);
      values.emplace_back(rref1.get());
      ++it;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref2 = iter_move(std::as_const(it));
      values.emplace_back(rref2.get());
      it++;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref3 = std::ranges::iter_move(it);
      values.emplace_back(rref3.get());
      ++it;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref4 = std::ranges::iter_move(std::as_const(it));
      values.emplace_back(rref4.get());
      it++; // `it` points to element of `Pattern` from here
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref5 = iter_move(it);
      values.emplace_back(rref5.get());
      ++it;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref6 = iter_move(std::as_const(it));
      values.emplace_back(rref6.get());
      it++;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref7 = std::ranges::iter_move(it);
      values.emplace_back(rref7.get());
      ++it;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref8 = std::ranges::iter_move(std::as_const(it));
      values.emplace_back(rref8.get());

      assert(std::ranges::equal(values, std::views::iota(0, 8)));
      assert(std::ranges::all_of(values, &MoveOnlyInt::was_move_constructed));
    }

    {
      std::vector<MoveOnlyInt> values;
      values.reserve(8);

      auto cit                                                = std::prev(std::as_const(jwv).end());
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref1 = iter_move(cit);
      values.emplace_back(rref1.get());
      cit--;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref2 = iter_move(std::as_const(cit));
      values.emplace_back(rref2.get());
      --cit;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref3 = std::ranges::iter_move(cit);
      values.emplace_back(rref3.get());
      cit--;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref4 = std::ranges::iter_move(std::as_const(cit));
      values.emplace_back(rref4.get());
      --cit; // `it` points to element of `Pattern` from here
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref5 = iter_move(cit);
      values.emplace_back(rref5.get());
      cit--;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref6 = iter_move(std::as_const(cit));
      values.emplace_back(rref6.get());
      --cit;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref7 = std::ranges::iter_move(cit);
      values.emplace_back(rref7.get());
      cit--;
      std::same_as<CommonProxyRvalueRef> decltype(auto) rref8 = std::ranges::iter_move(std::as_const(cit));
      values.emplace_back(rref8.get());

      assert(std::ranges::equal(std::views::reverse(values), std::views::iota(8, 16)));
      assert(std::ranges::all_of(values, &MoveOnlyInt::was_move_constructed));
    }

    assert(std::ranges::all_of(jwv, &MoveOnlyInt::was_moved_from));
  }

  { // Make sure `iter_move` calls underlying's iterator `iter_move` (not `std::move(*i)`).
    using Inner               = std::vector<int>;
    using InnerTrackingIter   = IterMoveTrackingIterator<Inner::iterator>;
    using TrackingInner       = std::ranges::subrange<InnerTrackingIter>;
    using Pattern             = std::array<int, 1>;
    using PatternTrackingIter = IterMoveTrackingIterator<Pattern::iterator>;
    using TrackingPattern     = std::ranges::subrange<PatternTrackingIter>;
    using JWV                 = std::ranges::join_with_view<std::span<TrackingInner>, TrackingPattern>;

    std::array<Inner, 2> v{{{1}, {2}}};
    Pattern pat{-1};

    bool v_moved = false;
    std::array<TrackingInner, 2> tracking_v{
        TrackingInner(InnerTrackingIter(v[0].begin(), &v_moved), InnerTrackingIter(v[0].end())),
        TrackingInner(InnerTrackingIter(v[1].begin()), InnerTrackingIter(v[1].end()))};

    bool pat_moved = false;
    TrackingPattern tracking_pat(PatternTrackingIter(pat.begin(), &pat_moved), PatternTrackingIter(pat.end()));

    JWV jwv(tracking_v, tracking_pat);
    auto it = jwv.begin();

    // Test calling `iter_move` when `it` points to element of `v`
    assert(!v_moved);
    assert(iter_move(it) == 1);
    assert(v_moved);

    // Test calling `iter_move` when `it` points to element of `pat`
    ++it;
    assert(!pat_moved);
    assert(iter_move(it) == -1);
    assert(pat_moved);
  }

  return true;
}

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

  return 0;
}