Skip to content

[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

Merged
merged 6 commits into from
Apr 18, 2024

Conversation

hawkinsw
Copy link
Contributor

@hawkinsw hawkinsw commented Jan 23, 2024

Add additional tests for begin/end of std::ranges::take_view.

In partial fulfillment of #72406.

@hawkinsw hawkinsw self-assigned this Jan 23, 2024
@hawkinsw hawkinsw requested a review from a team as a code owner January 23, 2024 03:20
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 23, 2024

@llvm/pr-subscribers-libcxx

Author: Will Hawkins (hawkinsw)

Changes

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.

In partial fulfillment of #72406.


Full diff: https://github.com/llvm/llvm-project/pull/79085.diff

2 Files Affected:

  • (modified) libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp (+21)
  • (modified) libcxx/test/std/ranges/range.adaptors/range.take/types.h (+33)
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

@hawkinsw hawkinsw changed the title [libc++][NFC] Check for take_view returning different types depending on simple_view-ness [libc++][NFC] Add additional tests for begin/end of std::ranges::take_view Jan 25, 2024
@hawkinsw
Copy link
Contributor Author

Just let me know if there is anything I can add/change to make this more useful!

@hawkinsw
Copy link
Contributor Author

hawkinsw commented Mar 5, 2024

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!

@ldionne ldionne added the ranges Issues related to `<ranges>` label Mar 7, 2024
Copy link
Member

@var-const var-const left a 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.

hawkinsw added 6 commits April 8, 2024 08:22
… 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

Address helpful feedback.
@hawkinsw hawkinsw requested a review from var-const April 8, 2024 12:25
@hawkinsw
Copy link
Contributor Author

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>;
Copy link
Member

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. :)

@var-const var-const merged commit 8b37ec1 into llvm:main Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. ranges Issues related to `<ranges>`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants