-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Casey Carter <Casey@Carter.net>
- Loading branch information
1 parent
a293fad
commit aafee76
Showing
5 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
RUNALL_INCLUDE ..\concepts_matrix.lst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <concepts> | ||
#include <memory> | ||
#include <ranges> | ||
#include <span> | ||
#include <utility> | ||
|
||
#include <range_algorithm_support.hpp> | ||
using namespace std; | ||
|
||
// clang-format off | ||
template <class Range> | ||
concept can_empty = requires(Range& r) { ranges::empty(r); }; | ||
template <class Range> | ||
concept can_data = requires(Range& r) { ranges::data(r); }; | ||
template <class Range> | ||
concept can_size = requires(Range& r) { ranges::size(r); }; | ||
// clang-format on | ||
|
||
struct instantiator { | ||
template <ranges::range R> | ||
static constexpr void call() { | ||
using ranges::ref_view, ranges::begin, ranges::end, ranges::forward_range; | ||
int input[3] = {0, 1, 2}; | ||
|
||
{ // traits | ||
STATIC_ASSERT(ranges::input_range<R> || ranges::output_range<R, const int&>); | ||
STATIC_ASSERT(ranges::enable_borrowed_range<ref_view<R>>); | ||
} | ||
|
||
{ // constructors and assignment operators | ||
STATIC_ASSERT(!constructible_from<ref_view<R>, R>); | ||
|
||
ref_view<R> default_constructed{}; | ||
STATIC_ASSERT(is_nothrow_default_constructible_v<ref_view<R>>); | ||
|
||
R wrapped_input{input}; | ||
ref_view<R> same_range{wrapped_input}; | ||
STATIC_ASSERT(is_nothrow_constructible_v<ref_view<R>, R&>); | ||
|
||
auto copy_constructed = same_range; | ||
if constexpr (forward_range<R>) { | ||
assert(copy_constructed.begin().peek() == begin(input)); | ||
} | ||
assert(copy_constructed.end().peek() == end(input)); | ||
|
||
default_constructed = copy_constructed; | ||
if constexpr (forward_range<R>) { | ||
assert(default_constructed.begin().peek() == begin(input)); | ||
} | ||
assert(default_constructed.end().peek() == end(input)); | ||
|
||
[[maybe_unused]] auto move_constructed = std::move(default_constructed); | ||
if constexpr (forward_range<R>) { | ||
assert(move_constructed.begin().peek() == begin(input)); | ||
} | ||
assert(move_constructed.end().peek() == end(input)); | ||
|
||
same_range = std::move(copy_constructed); | ||
if constexpr (forward_range<R>) { | ||
assert(same_range.begin().peek() == begin(input)); | ||
} | ||
assert(same_range.end().peek() == end(input)); | ||
} | ||
|
||
{ // access | ||
R wrapped_input{input}; | ||
ref_view<R> test_view{wrapped_input}; | ||
same_as<R> auto& base_range = as_const(test_view).base(); | ||
assert(addressof(base_range) == addressof(wrapped_input)); | ||
|
||
STATIC_ASSERT(noexcept(as_const(test_view).base())); | ||
} | ||
|
||
{ // iterators | ||
R wrapped_input{input}; | ||
ref_view<R> test_view{wrapped_input}; | ||
const same_as<ranges::iterator_t<R>> auto first = as_const(test_view).begin(); | ||
assert(first.peek() == input); | ||
STATIC_ASSERT(noexcept(as_const(test_view).begin()) == noexcept(wrapped_input.begin())); | ||
|
||
const same_as<ranges::sentinel_t<R>> auto last = as_const(test_view).end(); | ||
assert(last.peek() == end(input)); | ||
STATIC_ASSERT(noexcept(as_const(test_view).end()) == noexcept(wrapped_input.end())); | ||
} | ||
|
||
{ // state | ||
STATIC_ASSERT(can_size<ref_view<R>> == ranges::sized_range<R>); | ||
if constexpr (ranges::sized_range<R>) { | ||
R wrapped_input{input}; | ||
ref_view<R> test_view{wrapped_input}; | ||
|
||
const same_as<ranges::range_size_t<R>> auto ref_size = as_const(test_view).size(); | ||
assert(ref_size == size(wrapped_input)); | ||
|
||
STATIC_ASSERT(noexcept(as_const(test_view).size()) == noexcept(wrapped_input.size())); | ||
} | ||
|
||
STATIC_ASSERT(can_data<ref_view<R>> == ranges::contiguous_range<R>); | ||
if constexpr (ranges::contiguous_range<R>) { | ||
R wrapped_input{input}; | ||
ref_view<R> test_view{wrapped_input}; | ||
|
||
const same_as<int*> auto ref_data = as_const(test_view).data(); | ||
assert(ref_data == input); | ||
|
||
STATIC_ASSERT(noexcept(as_const(test_view).data()) == noexcept(wrapped_input.data())); | ||
} | ||
|
||
STATIC_ASSERT(can_empty<ref_view<R>> == can_empty<R>); | ||
if constexpr (can_empty<R>) { | ||
R wrapped_input{input}; | ||
ref_view<R> test_view{wrapped_input}; | ||
|
||
const same_as<bool> auto ref_empty = as_const(test_view).empty(); | ||
assert(!ref_empty); | ||
|
||
STATIC_ASSERT(noexcept(as_const(test_view).empty()) == noexcept(ranges::empty(wrapped_input))); | ||
|
||
R empty_range{}; | ||
ref_view<R> empty_view{empty_range}; | ||
assert(empty_view.empty()); | ||
} | ||
} | ||
|
||
{ // CTAD | ||
span<const int, 3> spanInput{input}; | ||
ref_view span_view{spanInput}; | ||
STATIC_ASSERT(same_as<decltype(span_view), ref_view<span<const int, 3>>>); | ||
} | ||
} | ||
}; | ||
|
||
int main() { | ||
STATIC_ASSERT((test_inout<instantiator, int>(), true)); | ||
test_inout<instantiator, int>(); | ||
} |