[libcxx-commits] [libcxx] 8b37ec1 - [libc++][NFC] Add additional tests for begin/end of std::ranges::take_view (#79085)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 17 21:30:01 PDT 2024


Author: Will Hawkins
Date: 2024-04-17T21:29:57-07:00
New Revision: 8b37ec1f7bda40c240f7bfda6737df5043860103

URL: https://github.com/llvm/llvm-project/commit/8b37ec1f7bda40c240f7bfda6737df5043860103
DIFF: https://github.com/llvm/llvm-project/commit/8b37ec1f7bda40c240f7bfda6737df5043860103.diff

LOG: [libc++][NFC] Add additional tests for begin/end of std::ranges::take_view (#79085)

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

In partial fulfillment of #72406.

Added: 
    

Modified: 
    libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.take/types.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
index 1873481d732253..9f11a991535e77 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp
@@ -11,8 +11,9 @@
 // constexpr auto begin() requires (!simple-view<V>);
 // constexpr auto begin() const requires range<const V>;
 
-#include <ranges>
 #include <cassert>
+#include <ranges>
+#include <utility>
 
 #include "test_macros.h"
 #include "test_iterators.h"
@@ -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>;
+
 constexpr bool test() {
   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
 
-  // sized_range && random_access_iterator
+  // simple-view<V> && sized_range<V> && random_access_range<V>
   {
-    std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView(buffer), 4);
-    assert(tv.begin() == SizedRandomAccessView(buffer).begin());
-    ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter);
-  }
+    using ViewTested = SizedRandomAccessView;
+    static_assert(simple_view<ViewTested>);
+    static_assert(std::ranges::sized_range<ViewTested>);
+    static_assert(std::ranges::random_access_range<ViewTested>);
 
-  {
-    const std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView(buffer), 4);
-    assert(tv.begin() == SizedRandomAccessView(buffer).begin());
+    std::ranges::take_view<ViewTested> tv(ViewTested(buffer), 4);
+    assert(tv.begin() == ViewTested(buffer).begin());
     ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter);
-  }
 
-  // sized_range && !random_access_iterator
-  {
-    std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 4);
-    assert(tv.begin() == std::counted_iterator<ForwardIter>(ForwardIter(buffer), 4));
-    ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<ForwardIter>);
+    const std::ranges::take_view<ViewTested> ctv(ViewTested(buffer), 4);
+    assert(ctv.begin() == ViewTested(buffer).begin());
+    ASSERT_SAME_TYPE(decltype(ctv.begin()), RandomAccessIter);
   }
 
+  // simple-view<V> && sized_range<V> && !random_access_range<V>
   {
-    const std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 4);
-    assert(tv.begin() == std::counted_iterator<ForwardIter>(ForwardIter(buffer), 4));
+    using ViewTested = SizedForwardView;
+    static_assert(simple_view<ViewTested>);
+    static_assert(std::ranges::sized_range<ViewTested>);
+    static_assert(!std::ranges::random_access_range<ViewTested>);
+
+    std::ranges::take_view<ViewTested> tv(ViewTested{buffer}, 16);                    // underlying size is 8
+    assert(tv.begin() == std::counted_iterator<ForwardIter>(ForwardIter(buffer), 8)); // expect min(8, 16)
     ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<ForwardIter>);
+
+    const std::ranges::take_view<ViewTested> ctv(ViewTested{buffer}, 4);
+    assert(ctv.begin() == std::counted_iterator<ForwardIter>(ForwardIter(buffer), 4));
+    ASSERT_SAME_TYPE(decltype(ctv.begin()), std::counted_iterator<ForwardIter>);
   }
 
-  // !sized_range
+  // simple-view<V> && !sized_range<V>
   {
-    std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4);
+    using ViewTested = MoveOnlyView;
+    static_assert(simple_view<ViewTested>);
+    std::ranges::take_view<ViewTested> tv(ViewTested{buffer}, 4);
     assert(tv.begin() == std::counted_iterator<int*>(buffer, 4));
     ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<int*>);
+
+    const std::ranges::take_view<ViewTested> ctv(ViewTested{buffer}, 4);
+    assert(ctv.begin() == std::counted_iterator<int*>(buffer, 4));
+    ASSERT_SAME_TYPE(decltype(ctv.begin()), std::counted_iterator<int*>);
   }
 
+  // simple-view<V> && sized_range<V> && !sized_range<const V>
   {
-    const std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4);
-    assert(tv.begin() == std::counted_iterator<int*>(buffer, 4));
+    using ViewTested = NonCommonSimpleView;
+    static_assert(simple_view<ViewTested>);
+    static_assert(std::ranges::sized_range<ViewTested>);
+    static_assert(!std::ranges::sized_range<const ViewTested>);
+
+    std::ranges::take_view<ViewTested> tv{};
     ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<int*>);
+    ASSERT_SAME_TYPE(decltype(std::as_const(tv).begin()), std::counted_iterator<int*>);
   }
 
-  // simple-view<V> && sized_range<V> && !size_range<!V>
+  //  !simple-view<V> && !sized_range<V>
   {
-    std::ranges::take_view<NonCommonSimpleView> tv{};
-    ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator<int*>);
-    ASSERT_SAME_TYPE(decltype(std::as_const(tv).begin()), std::counted_iterator<int*>);
+    using ViewTested = NonSimpleNonSizedView;
+    static_assert(!simple_view<ViewTested>);
+    static_assert(!std::ranges::sized_range<ViewTested>);
+
+    std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2}, 4};
+    // The count for the counted iterator is the count of the take_view (i.e., 4)
+    assert(tv.begin() == CountedCommonInputIterPtrConstInt(CommonInputIterPtrConstInt(buffer), 4));
+    ASSERT_SAME_TYPE(decltype(tv.begin()), CountedCommonInputIterPtrConstInt);
   }
 
+  // !simple-view<V> && sized_range<V>
+  {
+    using ViewTested = NonSimpleSizedView;
+    static_assert(!simple_view<ViewTested>);
+    static_assert(std::ranges::sized_range<ViewTested>);
+
+    std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2}, 4};
+    // The count for the counted iterator is the min(2, 4) (i.e., 2).
+    assert(tv.begin() == CountedCommonInputIterPtrConstInt(CommonInputIterPtrConstInt(buffer), 2));
+    ASSERT_SAME_TYPE(decltype(tv.begin()), CountedCommonInputIterPtrConstInt);
+  }
+
+  // !simple-view<V> && sized_range<V> && random_access_range<V>
+  {
+    using ViewTested = NonSimpleSizedRandomView;
+    static_assert(!simple_view<ViewTested>);
+    static_assert(std::ranges::sized_range<ViewTested>);
+    static_assert(std::ranges::random_access_range<ViewTested>);
+
+    std::ranges::take_view<ViewTested> tv{ViewTested{buffer, buffer + 2}, 4};
+    assert(tv.begin() == random_access_iterator<const int*>(buffer));
+    ASSERT_SAME_TYPE(decltype(tv.begin()), random_access_iterator<const int*>);
+  }
   return true;
 }
 

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 6cab05daa9e059..eddb39af271214 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,20 @@ constexpr bool test() {
     assert(tv.end() == std::ranges::next(tv.begin(), 8));
   }
 
+  {
+    // __iterator<false> has base with type std::ranges::sentinel_t<NonSimpleViewNonSized>; adding a const qualifier
+    // would change the equality.
+    std::ranges::take_view<NonSimpleNonSizedView> tvns(NonSimpleNonSizedView{buffer, buffer + 8}, 0);
+    static_assert(!std::is_same_v<decltype(tvns.end().base()), std::ranges::sentinel_t<const NonSimpleNonSizedView>>);
+  }
+
+  {
+    // __iterator<true> has base with type std::ranges::sentinel_t<const NonSimpleViewNonSized>; adding a const qualifier
+    // would not change the equality.
+    std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0);
+    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 db80e68bb21afe..7590ce33bffc10 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,54 @@ struct View : std::ranges::view_base {
   int* end_;
 };
 
+template <template <class...> typename Iter, bool Simple, bool Sized>
+struct CommonInputView : std::ranges::view_base {
+  constexpr explicit CommonInputView(int* b, int* e) : begin_(b), end_(e) {}
+
+  constexpr Iter<int*> begin() const { return Iter<int*>(begin_); }
+  constexpr Iter<int*> end() const { return Iter<int*>(end_); }
+
+  constexpr Iter<const int*> begin()
+    requires(!Simple)
+  {
+    return Iter<const int*>(begin_);
+  }
+  constexpr Iter<const int*> end()
+    requires(!Simple)
+  {
+    return Iter<const int*>(end_);
+  }
+
+  constexpr auto size() const
+    requires Sized
+  {
+    return end_ - begin_;
+  }
+
+private:
+  int* begin_;
+  int* end_;
+};
+
+using NonSimpleNonSizedView = CommonInputView<common_input_iterator, /*Simple=*/false, /*Sized=*/false>;
+static_assert(std::ranges::view<NonSimpleNonSizedView>);
+static_assert(!simple_view<NonSimpleNonSizedView>);
+static_assert(!std::ranges::sized_range<NonSimpleNonSizedView>);
+
+using SimpleViewNonSized = CommonInputView<common_input_iterator, /*Simple=*/true, /*Sized=*/false>;
+static_assert(std::ranges::view<SimpleViewNonSized>);
+static_assert(simple_view<SimpleViewNonSized>);
+static_assert(!std::ranges::sized_range<SimpleViewNonSized>);
+
+using NonSimpleSizedView = CommonInputView<common_input_iterator, /*Simple=*/false, /*Sized=*/true>;
+static_assert(std::ranges::view<NonSimpleSizedView>);
+static_assert(!simple_view<NonSimpleSizedView>);
+static_assert(std::ranges::sized_range<NonSimpleSizedView>);
+
+using NonSimpleSizedRandomView = CommonInputView<random_access_iterator, /*Simple=*/false, /*Sized=*/true>;
+static_assert(std::ranges::view<NonSimpleSizedRandomView>);
+static_assert(!simple_view<NonSimpleSizedRandomView>);
+static_assert(std::ranges::sized_range<NonSimpleSizedRandomView>);
+static_assert(std::ranges::random_access_range<NonSimpleSizedRandomView>);
+
 #endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H


        


More information about the libcxx-commits mailing list