[libcxx-commits] [PATCH] D116614: [libc++] Refactor the test for join_view's default constructor

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 4 11:46:49 PST 2022


ldionne created this revision.
ldionne requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

In particular, this removes the need for adding a ad-hoc `operator==`
to forward_iterator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116614

Files:
  libcxx/test/std/ranges/range.adaptors/range.join.view/iterator/ctor.default.pass.cpp


Index: libcxx/test/std/ranges/range.adaptors/range.join.view/iterator/ctor.default.pass.cpp
===================================================================
--- libcxx/test/std/ranges/range.adaptors/range.join.view/iterator/ctor.default.pass.cpp
+++ libcxx/test/std/ranges/range.adaptors/range.join.view/iterator/ctor.default.pass.cpp
@@ -12,45 +12,46 @@
 
 // iterator() requires default_initializable<OuterIter> = default;
 
-#include <cassert>
 #include <ranges>
 
-#include "test_macros.h"
+#include <cassert>
+#include <type_traits>
+#include "test_iterators.h"
 #include "../types.h"
 
-template<class T>
-struct DefaultCtorParent : std::ranges::view_base {
-  T *ptr_;
-  constexpr DefaultCtorParent(T *ptr) : ptr_(ptr) {}
-
-  constexpr forward_iterator<T *> begin() { return forward_iterator<T *>(ptr_); }
-  constexpr forward_iterator<const T *> begin() const { return forward_iterator<const T *>(ptr_); }
-  constexpr T *end() { return ptr_ + 4; }
-  constexpr const T *end() const { return ptr_ + 4; }
+template <class Iter>
+struct view : std::ranges::view_base {
+  Iter begin() const;
+  sentinel_wrapper<Iter> end() const;
 };
 
-template<class T>
-constexpr bool operator==(const forward_iterator<T*> &lhs, const T *rhs) { return lhs.base() == rhs; }
-template<class T>
-constexpr bool operator==(const T *lhs, const forward_iterator<T*> &rhs) { return rhs.base() == lhs; }
-
-constexpr bool test() {
-  using Base = DefaultCtorParent<ChildView>;
-  // Note, only the outer iterator is default_initializable:
-  static_assert( std::default_initializable<std::ranges::iterator_t<Base>>);
-  static_assert(!std::default_initializable<std::ranges::iterator_t<std::ranges::range_reference_t<Base>>>);
-
-  std::ranges::iterator_t<std::ranges::join_view<Base>> iter1;
-  (void) iter1;
+template <class Iter>
+constexpr void test_default_constructible() {
+  using JoinedView = std::ranges::join_view<view<Iter>>;
+  using JoinIterator = std::ranges::iterator_t<JoinedView>;
+  static_assert(std::is_default_constructible_v<JoinIterator>);
+  JoinIterator it; (void)it;
+}
 
-  static_assert(!std::default_initializable<std::ranges::iterator_t<std::ranges::join_view<ParentView<ChildView>>>>);
+template <class Iter>
+constexpr void test_non_default_constructible() {
+  using JoinedView = std::ranges::join_view<view<Iter>>;
+  using JoinIterator = std::ranges::iterator_t<JoinedView>;
+  static_assert(!std::is_default_constructible_v<JoinIterator>);
+}
 
+constexpr bool test() {
+  test_non_default_constructible<cpp17_input_iterator<ChildView*>>();
+  // NOTE: cpp20_input_iterator can't be used with join_view because it is not copyable.
+  test_default_constructible<forward_iterator<ChildView*>>();
+  test_default_constructible<bidirectional_iterator<ChildView*>>();
+  test_default_constructible<random_access_iterator<ChildView*>>();
+  test_default_constructible<contiguous_iterator<ChildView*>>();
   return true;
 }
 
 int main(int, char**) {
   test();
   static_assert(test());
-
   return 0;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116614.397360.patch
Type: text/x-patch
Size: 3026 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220104/803adde1/attachment.bin>


More information about the libcxx-commits mailing list