[libcxx-commits] [libcxx] 1fe897d - [libc++] [test] Simplify some ranges tests.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 21 17:30:43 PST 2021


Author: Arthur O'Dwyer
Date: 2021-12-21T20:16:35-05:00
New Revision: 1fe897dffd315c811052ecc9cf37d81f66744afa

URL: https://github.com/llvm/llvm-project/commit/1fe897dffd315c811052ecc9cf37d81f66744afa
DIFF: https://github.com/llvm/llvm-project/commit/1fe897dffd315c811052ecc9cf37d81f66744afa.diff

LOG: [libc++] [test] Simplify some ranges tests.

Eliminate a bogus operator== overload.
Also, check more intermediate steps in the logic we're checking here.
Some of this simplification is possible only now that we've implemented
more of <ranges>.

Differential Revision: https://reviews.llvm.org/D116002

Added: 
    

Modified: 
    libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.transform/end.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.transform/types.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp
index 9fc24a66ee02d..f210c619ca685 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.common.view/begin.pass.cpp
@@ -69,13 +69,8 @@ int main(int, char**) {
     using CommonIter = std::common_iterator<ForwardIter, sized_sentinel<ForwardIter>>;
     std::same_as<CommonIter> auto begin = common.begin();
     assert(begin == std::ranges::begin(view));
-  }
-  {
-    SizedForwardView view{buf, buf + 8};
-    std::ranges::common_view<SizedForwardView> const common(view);
-    using CommonIter = std::common_iterator<ForwardIter, sized_sentinel<ForwardIter>>;
-    std::same_as<CommonIter> auto begin = common.begin();
-    assert(begin == std::ranges::begin(view));
+    std::same_as<CommonIter> auto cbegin = std::as_const(common).begin();
+    assert(cbegin == std::ranges::begin(view));
   }
 
   {

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp
index 1562a188a8024..8da2ec613e979 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp
@@ -24,20 +24,13 @@
 
 constexpr bool test() {
   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
-  auto sw = sentinel_wrapper<int *>(buffer + 8); // Note: not 4, but that's OK.
-
   {
-    const std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4);
-    assert(base(tv.end().base()) == base(sw));
-    ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper<int *>);
+    std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView(buffer), 4);
+    std::same_as<sentinel_wrapper<int*>> auto sw1 = tv.end().base();
+    assert(base(sw1) == buffer + 8);
+    std::same_as<sentinel_wrapper<int*>> auto sw2 = std::as_const(tv).end().base();
+    assert(base(sw2) == buffer + 8);
   }
-
-  {
-    std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4);
-    assert(base(tv.end().base()) == base(sw));
-    ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper<int *>);
-  }
-
   return true;
 }
 

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp
index 92a6c17705105..2b7b15d1092fa 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp
@@ -27,27 +27,39 @@ constexpr bool test() {
 
   {
     // Test the default ctor.
-    std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4);
-    assert(decltype(tv.end()){} == std::ranges::next(tv.begin(), 4));
+    using TakeView = std::ranges::take_view<MoveOnlyView>;
+    using Sentinel = std::ranges::sentinel_t<TakeView>;
+    Sentinel s;
+    TakeView tv = TakeView(MoveOnlyView(buffer), 4);
+    assert(tv.begin() + 4 == s);
   }
 
   {
-    std::ranges::take_view<MoveOnlyView> nonConst(MoveOnlyView{buffer}, 5);
-    const std::ranges::take_view<MoveOnlyView> tvConst(MoveOnlyView{buffer}, 5);
-    auto sent1 = nonConst.end();
-    // Convert to const. Note, we cannot go the other way.
-    std::remove_cv_t<decltype(tvConst.end())> sent2 = sent1;
-
-    assert(sent1 == std::ranges::next(tvConst.begin(), 5));
-    assert(sent2 == std::ranges::next(tvConst.begin(), 5));
+    // Test the conversion from "sentinel" to "sentinel-to-const".
+    using TakeView = std::ranges::take_view<MoveOnlyView>;
+    using Sentinel = std::ranges::sentinel_t<TakeView>;
+    using ConstSentinel = std::ranges::sentinel_t<const TakeView>;
+    static_assert(std::is_convertible_v<Sentinel, ConstSentinel>);
+    TakeView tv = TakeView(MoveOnlyView(buffer), 4);
+    Sentinel s = tv.end();
+    ConstSentinel cs = s;
+    cs = s;  // test assignment also
+    assert(tv.begin() + 4 == s);
+    assert(tv.begin() + 4 == cs);
+    assert(std::as_const(tv).begin() + 4 == s);
+    assert(std::as_const(tv).begin() + 4 == cs);
   }
 
   {
-    std::ranges::take_view<CopyableView> tv(CopyableView{buffer}, 6);
-    auto sw = sentinel_wrapper<int *>(buffer + 6);
-    using Sent = decltype(tv.end());
-    Sent sent = Sent(sw);
-    assert(base(sent.base()) == base(sw));
+    // Test the constructor from "base-sentinel" to "sentinel".
+    using TakeView = std::ranges::take_view<MoveOnlyView>;
+    using Sentinel = std::ranges::sentinel_t<TakeView>;
+    sentinel_wrapper<int*> sw1 = MoveOnlyView(buffer).end();
+    static_assert( std::is_constructible_v<Sentinel, sentinel_wrapper<int*>>);
+    static_assert(!std::is_convertible_v<sentinel_wrapper<int*>, Sentinel>);
+    auto s = Sentinel(sw1);
+    std::same_as<sentinel_wrapper<int*>> auto sw2 = s.base();
+    assert(base(sw2) == base(sw1));
   }
 
   return true;

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.transform/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.transform/end.pass.cpp
index c933e44f1a78f..80b8c92bf251a 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.transform/end.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.transform/end.pass.cpp
@@ -25,44 +25,58 @@
 #include "types.h"
 
 template<class T>
-concept EndInvocable = requires(T t) { t.end(); };
-
-template<class T>
-concept EndIsIter = requires(T t) { ++t.end(); };
+concept HasConstQualifiedEnd = requires(const T& t) { t.end(); };
 
 constexpr bool test() {
   {
-    std::ranges::transform_view transformView(MoveOnlyView{}, PlusOneMutable{});
-    assert(transformView.end().base() == globalBuff + 8);
+    using TransformView = std::ranges::transform_view<ForwardView, PlusOneMutable>;
+    static_assert(std::ranges::common_range<TransformView>);
+    TransformView tv;
+    auto end = tv.end();
+    ASSERT_SAME_TYPE(decltype(end.base()), std::ranges::sentinel_t<ForwardView>);
+    assert(base(end.base()) == globalBuff + 8);
+    static_assert(!HasConstQualifiedEnd<TransformView>);
   }
-
   {
-    std::ranges::transform_view transformView(ForwardView{}, PlusOneMutable{});
-    assert(transformView.end().base().base() == globalBuff + 8);
+    using TransformView = std::ranges::transform_view<InputView, PlusOneMutable>;
+    static_assert(!std::ranges::common_range<TransformView>);
+    TransformView tv;
+    auto end = tv.end();
+    ASSERT_SAME_TYPE(decltype(end.base()), std::ranges::sentinel_t<InputView>);
+    assert(base(base(end.base())) == globalBuff + 8);
+    static_assert(!HasConstQualifiedEnd<TransformView>);
   }
-
   {
-    std::ranges::transform_view transformView(InputView{}, PlusOneMutable{});
-    assert(transformView.end().base() == globalBuff + 8);
+    using TransformView = std::ranges::transform_view<InputView, PlusOne>;
+    static_assert(!std::ranges::common_range<TransformView>);
+    TransformView tv;
+    auto end = tv.end();
+    ASSERT_SAME_TYPE(decltype(end.base()), std::ranges::sentinel_t<InputView>);
+    assert(base(base(end.base())) == globalBuff + 8);
+    auto cend = std::as_const(tv).end();
+    ASSERT_SAME_TYPE(decltype(cend.base()), std::ranges::sentinel_t<const InputView>);
+    assert(base(base(cend.base())) == globalBuff + 8);
   }
-
   {
-    const std::ranges::transform_view transformView(MoveOnlyView{}, PlusOne{});
-    assert(transformView.end().base() == globalBuff + 8);
+    using TransformView = std::ranges::transform_view<MoveOnlyView, PlusOneMutable>;
+    static_assert(std::ranges::common_range<TransformView>);
+    TransformView tv;
+    auto end = tv.end();
+    ASSERT_SAME_TYPE(decltype(end.base()), std::ranges::sentinel_t<MoveOnlyView>);
+    assert(end.base() == globalBuff + 8);
+    static_assert(!HasConstQualifiedEnd<TransformView>);
+  }
+  {
+    using TransformView = std::ranges::transform_view<MoveOnlyView, PlusOne>;
+    static_assert(std::ranges::common_range<TransformView>);
+    TransformView tv;
+    auto end = tv.end();
+    ASSERT_SAME_TYPE(decltype(end.base()), std::ranges::sentinel_t<MoveOnlyView>);
+    assert(end.base() == globalBuff + 8);
+    auto cend = std::as_const(tv).end();
+    ASSERT_SAME_TYPE(decltype(cend.base()), std::ranges::sentinel_t<const MoveOnlyView>);
+    assert(cend.base() == globalBuff + 8);
   }
-
-  static_assert(!EndInvocable<const std::ranges::transform_view<MoveOnlyView, PlusOneMutable>>);
-  static_assert( EndInvocable<      std::ranges::transform_view<MoveOnlyView, PlusOneMutable>>);
-  static_assert( EndInvocable<const std::ranges::transform_view<MoveOnlyView, PlusOne>>);
-  static_assert(!EndInvocable<const std::ranges::transform_view<InputView, PlusOneMutable>>);
-  static_assert( EndInvocable<      std::ranges::transform_view<InputView, PlusOneMutable>>);
-  static_assert( EndInvocable<const std::ranges::transform_view<InputView, PlusOne>>);
-
-  static_assert(!EndIsIter<const std::ranges::transform_view<InputView, PlusOne>>);
-  static_assert(!EndIsIter<      std::ranges::transform_view<InputView, PlusOneMutable>>);
-  static_assert( EndIsIter<const std::ranges::transform_view<MoveOnlyView, PlusOne>>);
-  static_assert( EndIsIter<      std::ranges::transform_view<MoveOnlyView, PlusOneMutable>>);
-
   return true;
 }
 

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp
index a69739647d6df..26fc953023c81 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp
@@ -17,30 +17,32 @@
 #include "test_macros.h"
 #include "../types.h"
 
-template<class V, class F>
-concept BaseInvocable = requires(std::ranges::iterator_t<std::ranges::transform_view<V, F>> iter) {
-  iter.base();
+template<class It>
+concept HasBase = requires(It it) {
+  static_cast<It>(it).base();
 };
 
 constexpr bool test() {
   {
-    std::ranges::transform_view<MoveOnlyView, PlusOneMutable> transformView;
-    auto iter = std::move(transformView).begin();
-    ASSERT_SAME_TYPE(int*, decltype(iter.base()));
-    assert(iter.base() == globalBuff);
-    ASSERT_SAME_TYPE(int*, decltype(std::move(iter).base()));
-    assert(std::move(iter).base() == globalBuff);
+    using TransformView = std::ranges::transform_view<MoveOnlyView, PlusOneMutable>;
+    TransformView tv;
+    auto begin = tv.begin();
+    ASSERT_SAME_TYPE(decltype(begin.base()), int*);
+    assert(begin.base() == globalBuff);
+    ASSERT_SAME_TYPE(decltype(std::move(begin).base()), int*);
+    assert(std::move(begin).base() == globalBuff);
   }
-
   {
-    std::ranges::transform_view<InputView, PlusOneMutable> transformView;
-    auto iter = transformView.begin();
-    assert(std::move(iter).base() == globalBuff);
-    ASSERT_SAME_TYPE(cpp20_input_iterator<int *>, decltype(std::move(iter).base()));
+    using TransformView = std::ranges::transform_view<InputView, PlusOneMutable>;
+    TransformView tv;
+    auto begin = tv.begin();
+    static_assert(!HasBase<decltype(begin)&>);
+    static_assert(HasBase<decltype(begin)&&>);
+    static_assert(!HasBase<const decltype(begin)&>);
+    static_assert(!HasBase<const decltype(begin)&&>);
+    std::same_as<cpp20_input_iterator<int *>> auto it = std::move(begin).base();
+    assert(base(it) == globalBuff);
   }
-
-  static_assert(!BaseInvocable<InputView, PlusOneMutable>);
-
   return true;
 }
 

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.transform/types.h b/libcxx/test/std/ranges/range.adaptors/range.transform/types.h
index 3c1ed0f564a82..80ffa6a0a67d0 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.transform/types.h
+++ b/libcxx/test/std/ranges/range.adaptors/range.transform/types.h
@@ -85,12 +85,11 @@ static_assert( std::ranges::borrowed_range<BorrowableRange>);
 struct InputView : std::ranges::view_base {
   int *ptr_;
   constexpr explicit InputView(int* ptr = globalBuff) : ptr_(ptr) {}
-  constexpr cpp20_input_iterator<int*> begin() const { return cpp20_input_iterator<int*>(ptr_); }
-  constexpr int *end() const { return ptr_ + 8; }
+  constexpr auto begin() const { return cpp20_input_iterator<int*>(ptr_); }
+  constexpr auto end() const { return sentinel_wrapper<cpp20_input_iterator<int*>>(cpp20_input_iterator<int*>(ptr_ + 8)); }
 };
-// TODO: remove these bogus operators
-constexpr bool operator==(const cpp20_input_iterator<int*> &lhs, int* rhs) { return lhs.base() == rhs; }
-constexpr bool operator==(int* lhs, const cpp20_input_iterator<int*> &rhs) { return rhs.base() == lhs; }
+static_assert( std::ranges::view<InputView>);
+static_assert(!std::ranges::sized_range<InputView>);
 
 struct SizedSentinelView : std::ranges::view_base {
   int count_;


        


More information about the libcxx-commits mailing list