Skip to content

Commit 66b038d

Browse files
committed
Improve tests
1 parent 60d1b6d commit 66b038d

File tree

3 files changed

+128
-90
lines changed

3 files changed

+128
-90
lines changed

libcxx/test/std/ranges/range.utility/range.elementsof/ctad.compile.pass.cpp

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <ranges>
12+
//
13+
// template<range R, class Allocator = allocator<byte>>
14+
// struct std::ranges::elements_of;
15+
//
16+
// template<class R, class Allocator = allocator<byte>>
17+
// elements_of(R&&, Allocator = Allocator()) -> elements_of<R&&, Allocator>;
18+
19+
#include <concepts>
20+
#include <cstddef>
21+
#include <memory>
22+
#include <ranges>
23+
24+
#include "min_allocator.h"
25+
#include "test_allocator.h"
26+
#include "test_iterators.h"
27+
28+
template <class Allocator, class Range>
29+
constexpr void test_impl() {
30+
Range r;
31+
Allocator a;
32+
33+
// With a lvalue range
34+
{
35+
std::ranges::elements_of elements(r);
36+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&, std::allocator<std::byte>>>);
37+
}
38+
39+
// With a rvalue range
40+
{
41+
std::ranges::elements_of elements((Range()));
42+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&&, std::allocator<std::byte>>>);
43+
}
44+
45+
// With lvalue range and allocator
46+
{
47+
std::ranges::elements_of elements(r, a);
48+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&, Allocator>>);
49+
}
50+
51+
// With rvalue range and allocator
52+
{
53+
std::ranges::elements_of elements((Range()), Allocator());
54+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&&, Allocator>>);
55+
}
56+
57+
// Ensure we can use designated initializers
58+
{
59+
// lvalues
60+
{
61+
std::ranges::elements_of elements{.range = r, .allocator = a};
62+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&, Allocator>>);
63+
}
64+
65+
// rvalues
66+
{
67+
std::ranges::elements_of elements{.range = Range(), .allocator = Allocator()};
68+
static_assert(std::same_as<decltype(elements), std::ranges::elements_of<Range&&, Allocator>>);
69+
}
70+
}
71+
}
72+
73+
template <class Iterator>
74+
struct Range {
75+
Iterator begin() const;
76+
sentinel_wrapper<Iterator> end() const;
77+
};
78+
79+
constexpr bool test() {
80+
types::for_each(types::type_list<std::allocator<std::byte>, min_allocator<std::byte>, test_allocator<std::byte>>{},
81+
[]<class Allocator> {
82+
types::for_each(types::cpp20_input_iterator_list<int*>{}, []<class Iterator> {
83+
test_impl<Allocator, Range<Iterator>>();
84+
});
85+
});
86+
87+
return true;
88+
}
89+
90+
int main(int, char**) {
91+
test();
92+
static_assert(test());
93+
return 0;
94+
}

libcxx/test/std/ranges/range.utility/range.elementsof/elements_of.pass.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1010

11-
// std::ranges::elements_of;
12-
13-
#include <ranges>
11+
// <ranges>
12+
//
13+
// template<range R, class Allocator = allocator<byte>>
14+
// struct std::ranges::elements_of;
1415

16+
#include <cassert>
1517
#include <concepts>
1618
#include <memory>
19+
#include <ranges>
1720
#include <type_traits>
21+
#include <utility>
1822
#include <vector>
1923

2024
#include "min_allocator.h"
@@ -25,9 +29,9 @@ template <class Iterator>
2529
struct Range {
2630
using Sentinel = sentinel_wrapper<Iterator>;
2731

28-
Iterator begin() { return Iterator(data_.data()); }
32+
constexpr Iterator begin() { return Iterator(data_.data()); }
2933

30-
Sentinel end() { return Sentinel(Iterator(data_.data() + data_.size())); }
34+
constexpr Sentinel end() { return Sentinel(Iterator(data_.data() + data_.size())); }
3135

3236
private:
3337
std::vector<int> data_ = {0, 1, 2, 3};
@@ -40,50 +44,45 @@ constexpr bool test_range() {
4044
using elements_of_t = std::ranges::elements_of<Range&, Allocator>;
4145
{
4246
// constructor
43-
std::same_as<elements_of_t> decltype(auto) elements_of = std::ranges::elements_of(r, Allocator());
44-
[[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_range = elements_of.range;
45-
if (!std::is_constant_evaluated()) {
46-
assert(std::ranges::distance(elements_of_range) == 4);
47-
}
48-
[[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_allocator = elements_of.allocator;
47+
std::same_as<elements_of_t> decltype(auto) e = std::ranges::elements_of(r, Allocator());
48+
std::same_as<Range&> decltype(auto) range = e.range;
49+
assert(std::ranges::distance(range) == 4);
50+
[[maybe_unused]] std::same_as<Allocator> decltype(auto) allocator = e.allocator;
4951
}
5052
{
5153
// designated initializer
52-
std::same_as<elements_of_t> decltype(auto) elements_of = std::ranges::elements_of{
54+
std::same_as<elements_of_t> decltype(auto) e = std::ranges::elements_of{
5355
.range = r,
5456
.allocator = Allocator(),
5557
};
56-
[[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_range = elements_of.range;
57-
if (!std::is_constant_evaluated()) {
58-
assert(std::ranges::distance(elements_of_range) == 4);
59-
}
60-
[[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_allocator = elements_of.allocator;
58+
std::same_as<Range&> decltype(auto) range = e.range;
59+
assert(&range == &r);
60+
assert(std::ranges::distance(range) == 4);
61+
[[maybe_unused]] std::same_as<Allocator> decltype(auto) allocator = e.allocator;
6162
}
6263
{
6364
// copy constructor
64-
std::same_as<elements_of_t> decltype(auto) elements_of_1 = std::ranges::elements_of(r, Allocator());
65-
std::same_as<elements_of_t> auto elements_of_2 = elements_of_1;
66-
[[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_1_range = elements_of_1.range;
67-
[[maybe_unused]] std::same_as<Range&> decltype(auto) elements_of_2_range = elements_of_2.range;
68-
if (!std::is_constant_evaluated()) {
69-
assert(std::ranges::distance(elements_of_1_range) == 4);
70-
assert(std::ranges::distance(elements_of_2_range) == 4);
71-
}
72-
[[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_2_allocator = elements_of_2.allocator;
65+
std::same_as<elements_of_t> decltype(auto) e = std::ranges::elements_of(r, Allocator());
66+
std::same_as<elements_of_t> auto copy = e;
67+
std::same_as<Range&> decltype(auto) range = e.range;
68+
std::same_as<Range&> decltype(auto) copy_range = copy.range;
69+
assert(&range == &r);
70+
assert(&range == &copy_range);
71+
assert(std::ranges::distance(range) == 4);
72+
[[maybe_unused]] std::same_as<Allocator> decltype(auto) copy_allocator = copy.allocator;
7373
}
7474

7575
using elements_of_r_t = std::ranges::elements_of<Range&&, Allocator>;
7676
{
7777
// move constructor
78-
std::same_as<elements_of_r_t> decltype(auto) elements_of_1 = std::ranges::elements_of(std::move(r), Allocator());
79-
std::same_as<elements_of_r_t> auto elements_of_2 = std::move(elements_of_1);
80-
[[maybe_unused]] std::same_as<Range&&> decltype(auto) elements_of_1_range = std::move(elements_of_1.range);
81-
[[maybe_unused]] std::same_as<Range&&> decltype(auto) elements_of_2_range = std::move(elements_of_2.range);
82-
if (!std::is_constant_evaluated()) {
83-
assert(std::ranges::distance(elements_of_1_range) == 4);
84-
assert(std::ranges::distance(elements_of_2_range) == 4);
85-
}
86-
[[maybe_unused]] std::same_as<Allocator> decltype(auto) elements_of_2_allocator = elements_of_2.allocator;
78+
std::same_as<elements_of_r_t> decltype(auto) e = std::ranges::elements_of(std::move(r), Allocator());
79+
std::same_as<elements_of_r_t> auto copy = std::move(e);
80+
std::same_as<Range&&> decltype(auto) range = std::move(e.range);
81+
std::same_as<Range&&> decltype(auto) copy_range = std::move(copy.range);
82+
assert(&range == &r);
83+
assert(&range == &copy_range);
84+
assert(std::ranges::distance(range) == 4);
85+
[[maybe_unused]] std::same_as<Allocator> decltype(auto) copy_allocator = copy.allocator;
8786
}
8887
return true;
8988
}

0 commit comments

Comments
 (0)