File: ctor.range.element.pass.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (244 lines) | stat: -rw-r--r-- 8,458 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
//===----------------------------------------------------------------------===//
//
// 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>

// template<input_range R>
//   requires constructible_from<V, views::all_t<R>> &&
//   constructible_from<Pattern, single_view<range_value_t<InnerRng>>>
// constexpr explicit join_with_view(R&& r, range_value_t<InnerRng> e);

#include <ranges>

#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>
#include <utility>

#include "../types.h"
#include "test_iterators.h"
#include "test_range.h"

struct MoveOnlyInt {
  MoveOnlyInt()                         = default;
  MoveOnlyInt(MoveOnlyInt&&)            = default;
  MoveOnlyInt& operator=(MoveOnlyInt&&) = default;

  constexpr MoveOnlyInt(int val) : val_(val) {}
  constexpr operator int() const { return val_; }

  int val_ = 0;
};

template <>
struct std::common_type<MoveOnlyInt, int> {
  using type = int;
};

template <>
struct std::common_type<int, MoveOnlyInt> {
  using type = int;
};

struct OutputView : std::ranges::view_base {
  using It = cpp20_output_iterator<int*>;
  It begin() const;
  sentinel_wrapper<It> end() const;
};

static_assert(std::ranges::output_range<OutputView, int>);
static_assert(std::ranges::view<OutputView>);

struct InputRange {
  using It = cpp20_input_iterator<int*>;
  It begin() const;
  sentinel_wrapper<It> end() const;
};

struct InputView : InputRange, std::ranges::view_base {};

static_assert(std::ranges::input_range<InputRange>);
static_assert(std::ranges::input_range<const InputRange>);
static_assert(std::ranges::view<InputView>);
static_assert(std::ranges::input_range<InputView>);
static_assert(std::ranges::input_range<const InputView>);

class View : public std::ranges::view_base {
  using OuterRange = std::array<std::array<MoveOnlyInt, 2>, 3>;

  static constexpr OuterRange range_on_input_view            = {{{1, 1}, {1, 1}, {1, 1}}};
  static constexpr OuterRange range_on_ref_input_range       = {{{2, 2}, {2, 2}, {2, 2}}};
  static constexpr OuterRange range_on_const_ref_input_range = {{{3, 3}, {3, 3}, {3, 3}}};
  static constexpr OuterRange range_on_owning_input_range    = {{{4, 4}, {4, 4}, {4, 4}}};

  const OuterRange* r_;

public:
  // Those functions should never be called in this test.
  View(View&&) { assert(false); }
  View(OutputView) { assert(false); }
  View& operator=(View&&) {
    assert(false);
    return *this;
  }

  constexpr explicit View(InputView) : r_(&range_on_input_view) {}
  constexpr explicit View(InputRange) = delete;
  constexpr explicit View(std::ranges::ref_view<InputRange>) : r_(&range_on_ref_input_range) {}
  constexpr explicit View(std::ranges::ref_view<const InputRange>) : r_(&range_on_const_ref_input_range) {}
  constexpr explicit View(std::ranges::owning_view<InputRange>) : r_(&range_on_owning_input_range) {}

  constexpr auto begin() const { return r_->begin(); }
  constexpr auto end() const { return r_->end(); }
};

static_assert(std::ranges::input_range<View>);
static_assert(std::ranges::input_range<const View>);

class Pattern : public std::ranges::view_base {
  int val_;

public:
  // Those functions should never be called in this test.
  Pattern(Pattern&&) { assert(false); }
  template <class T>
  Pattern(const std::ranges::single_view<T>&) {
    assert(false);
  }
  Pattern& operator=(Pattern&&) {
    assert(false);
    return *this;
  }

  template <class T>
  constexpr explicit Pattern(std::ranges::single_view<T>&& v) : val_(v[0]) {}

  constexpr const int* begin() const { return &val_; }
  constexpr const int* end() const { return &val_ + 1; }
};

static_assert(std::ranges::forward_range<Pattern>);
static_assert(std::ranges::forward_range<const Pattern>);

constexpr void test_ctor_with_view_and_element() {
  // Check construction from `r` and `e`, when `r` models `std::ranges::view`

  { // `r` and `e` are glvalues
    InputView r;
    int e = 0;
    std::ranges::join_with_view<View, Pattern> jwv(r, e);
    assert(std::ranges::equal(jwv, std::array{1, 1, 0, 1, 1, 0, 1, 1}));
  }

  { // `r` and `e` are const glvalues
    const InputView r;
    const int e = 1;
    std::ranges::join_with_view<View, Pattern> jwv(r, e);
    assert(std::ranges::equal(jwv, std::array{1, 1, 1, 1, 1, 1, 1, 1}));
  }

  { // `r` and `e` are prvalues
    std::ranges::join_with_view<View, Pattern> jwv(InputView{}, MoveOnlyInt{2});
    assert(std::ranges::equal(jwv, std::array{1, 1, 2, 1, 1, 2, 1, 1}));
  }

  { // `r` and `e` are xvalues
    InputView r;
    MoveOnlyInt e = 3;
    std::ranges::join_with_view<View, Pattern> jwv(std::move(r), std::move(e));
    assert(std::ranges::equal(jwv, std::array{1, 1, 3, 1, 1, 3, 1, 1}));
  }

  // Check explicitness
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputView, MoveOnlyInt>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputView, int>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputView&, int&>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, const InputView, const int>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, const InputView&, const int&>);
}

constexpr void test_ctor_with_non_view_and_element() {
  // Check construction from `r` and `e`, when `r` does not model `std::ranges::view`

  { // `r` and `e` are glvalues
    InputRange r;
    int e = 0;
    std::ranges::join_with_view<View, Pattern> jwv(r, e);
    assert(std::ranges::equal(jwv, std::array{2, 2, 0, 2, 2, 0, 2, 2}));
  }

  { // `r` and `e` are const glvalues
    const InputRange r;
    const int e = 1;
    std::ranges::join_with_view<View, Pattern> jwv(r, e);
    assert(std::ranges::equal(jwv, std::array{3, 3, 1, 3, 3, 1, 3, 3}));
  }

  { // `r` and `e` are prvalues
    std::ranges::join_with_view<View, Pattern> jwv(InputRange{}, MoveOnlyInt{2});
    assert(std::ranges::equal(jwv, std::array{4, 4, 2, 4, 4, 2, 4, 4}));
  }

  { // `r` and `e` are xvalues
    InputRange r;
    MoveOnlyInt e = 3;
    std::ranges::join_with_view<View, Pattern> jwv(std::move(r), std::move(e));
    assert(std::ranges::equal(jwv, std::array{4, 4, 3, 4, 4, 3, 4, 4}));
  }

  // Check explicitness
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputRange, MoveOnlyInt>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputRange, int>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, InputRange&, int&>);
  static_assert(ConstructionIsExplicit<std::ranges::join_with_view<View, Pattern>, const InputRange&, const int&>);
}

constexpr void test_constraints() {
  { // `R` is not an input range
    using R = OutputView;
    static_assert(!std::ranges::input_range<R>);
    static_assert(std::constructible_from<View, std::views::all_t<R>>);
    static_assert(std::constructible_from<Pattern, std::ranges::single_view<int>>);
    static_assert(!std::constructible_from<std::ranges::join_with_view<View, Pattern>, R, int>);
  }

  { // `V` is not constructible from `views::all_t<R>`
    using R = test_range<cpp20_input_iterator>;
    static_assert(std::ranges::input_range<R>);
    static_assert(!std::constructible_from<View, std::views::all_t<R>>);
    static_assert(std::constructible_from<Pattern, std::ranges::single_view<int>>);
    static_assert(!std::constructible_from<std::ranges::join_with_view<View, Pattern>, R, int>);
  }

  { // `Pattern` is not constructible from `single_view<range_value_t<InnerRng>>`
    using R   = InputView;
    using Pat = test_view<forward_iterator>;
    static_assert(std::ranges::input_range<R>);
    static_assert(std::constructible_from<View, std::views::all_t<R>>);
    static_assert(!std::constructible_from<Pat, std::ranges::single_view<int>>);
    static_assert(!std::constructible_from<std::ranges::join_with_view<View, Pat>, R, int>);
  }
}

constexpr bool test() {
  test_ctor_with_view_and_element();
  test_ctor_with_non_view_and_element();
  test_constraints();

  return true;
}

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

  return 0;
}