-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++][NFC] Add additional tests for begin/end of std::ranges::take_view #79085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-libcxx Author: Will Hawkins (hawkinsw) ChangesAdd a check to make sure that an instance of a take_view::_iterator<false> is returned when the base type is not a simple view nor a sized view. In partial fulfillment of #72406. Full diff: https://github.com/llvm/llvm-project/pull/79085.diff 2 Files Affected:
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
index 6cab05daa9e059a..a54b81c83e6aadb 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
@@ -69,6 +69,27 @@ constexpr bool test() {
assert(tv.end() == std::ranges::next(tv.begin(), 8));
}
+ // Check that (non-)simple, non-sized views have different types for their end() member function.
+ {
+ // These assertions must be true in order to trigger the different paths through the end member function
+ // that will return values with different types:
+ static_assert(!simple_view<NonSimpleViewNonSized>);
+ static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
+ static_assert(simple_view<SimpleViewNonSized>);
+ static_assert(std::ranges::range<const SimpleViewNonSized>);
+ static_assert(!std::ranges::sized_range<const SimpleViewNonSized>);
+
+ std::ranges::take_view<NonSimpleViewNonSized> tvns(NonSimpleViewNonSized{buffer, buffer + 8}, 0);
+ std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0);
+
+ // __iterator<false> has base with type std::ranges::sentinel_t<NonSimpleViewNonSized>; adding a const qualifier
+ // would change the equality.
+ static_assert(!std::is_same_v<decltype(tvns.end().base()), std::ranges::sentinel_t<const NonSimpleViewNonSized>>);
+ // __iterator<true> has base with type std::ranges::sentinel_t<const NonSimpleViewNonSized>; adding a const qualifier
+ // would not change the equality.
+ static_assert(std::is_same_v<decltype(tvs.end().base()), std::ranges::sentinel_t<const SimpleViewNonSized>>);
+ }
+
return true;
}
diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/types.h b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
index db80e68bb21afef..083e02ac7cac7ca 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/types.h
@@ -65,4 +65,37 @@ struct View : std::ranges::view_base {
int* end_;
};
+template <bool Simple>
+struct InputView : std::ranges::view_base {
+ constexpr explicit InputView(int* b, int* e) : begin_(b), end_(e) {}
+
+ constexpr common_input_iterator<int*> begin() const { return common_input_iterator<int*>(begin_); }
+ constexpr common_input_iterator<int*> end() const { return common_input_iterator<int*>(end_); }
+
+ constexpr common_input_iterator<const int*> begin()
+ requires(!Simple)
+ {
+ return common_input_iterator<const int*>(begin_);
+ }
+ constexpr common_input_iterator<const int*> end()
+ requires(!Simple)
+ {
+ return common_input_iterator<const int*>(end_);
+ }
+
+private:
+ int* begin_;
+ int* end_;
+};
+
+using NonSimpleViewNonSized = InputView<false>;
+static_assert(std::ranges::view<NonSimpleViewNonSized>);
+static_assert(!simple_view<NonSimpleViewNonSized>);
+static_assert(!std::ranges::sized_range<NonSimpleViewNonSized>);
+
+using SimpleViewNonSized = InputView<true>;
+static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
+static_assert(std::ranges::view<SimpleViewNonSized>);
+static_assert(simple_view<SimpleViewNonSized>);
+
#endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H
|
6e3227a
to
15a08ac
Compare
Just let me know if there is anything I can add/change to make this more useful! |
Let me know if there is anything else that I can do to clean this up! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the extra tests! Looks great, just some minor comments.
libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
Outdated
Show resolved
Hide resolved
… on simple_view-ness Add a check to make sure that an instance of a take_view::_iterator<false> is returned when the base type is not a simple view nor a sized view.
…pending on simple_view-ness Rename test class and refactor.
…pending on simple_view-ness Rearrange static_asserts to match.
…pending on simple_view-ness Finish adding tests.
…pending on simple_view-ness Formatting.
…pending on simple_view-ness Address helpful feedback.
15a08ac
to
e28a3ea
Compare
Sorry, @var-const , for the additional review request. As I mentioned in DM, I am just nervous and don't want to screw anything up. |
@@ -27,55 +28,104 @@ struct NonCommonSimpleView : std::ranges::view_base { | |||
static_assert(std::ranges::sized_range<NonCommonSimpleView>); | |||
static_assert(!std::ranges::sized_range<const NonCommonSimpleView>); | |||
|
|||
using CommonInputIterPtrConstInt = common_input_iterator<const int*>; | |||
using CountedCommonInputIterPtrConstInt = std::counted_iterator<CommonInputIterPtrConstInt>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I'd use a shorter name (which IMO is fine since it's only used within a single file). It's a super minor comment that's not worth another around of review, so no action necessary. :)
Add additional tests for
begin
/end
ofstd::ranges::take_view
.In partial fulfillment of #72406.