[libcxx-commits] [libcxx] b8d54d1 - [libc++][ranges][NFC] Test that range algorithms support iterators requiring `iter_move`.

Konstantin Varlamov via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 19 17:21:32 PDT 2022


Author: Konstantin Varlamov
Date: 2022-07-19T17:21:08-07:00
New Revision: b8d54d1d6acadea950de3da407427129fe0aabf2

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

LOG: [libc++][ranges][NFC] Test that range algorithms support iterators requiring `iter_move`.

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

Added: 
    

Modified: 
    libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
    libcxx/test/support/MoveOnly.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index fda245d5a6700..b2973b720ef04 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -24,6 +24,7 @@
 #include <random>
 #include <ranges>
 
+#include "MoveOnly.h"
 #include "test_iterators.h"
 
 template <class Func, std::ranges::range Input, class ...Args>
@@ -44,26 +45,27 @@ constexpr void test_mid(Func&& func, Input& in, std::ranges::iterator_t<Input> m
   func(in, mid, std::forward<Args>(args)...);
 }
 
-constexpr bool test_all() {
-  std::array input = {1, 2, 3};
+template <class T>
+constexpr void run_tests() {
+  std::array input = {T{1}, T{2}, T{3}};
   ProxyRange in{input};
-  std::array input2 = {4, 5, 6};
+  std::array input2 = {T{4}, T{5}, T{6}};
   ProxyRange in2{input2};
 
   auto mid = in.begin() + 1;
 
-  std::array output = {4, 5, 6, 7, 8, 9};
+  std::array output = {T{4}, T{5}, T{6}, T{7}, T{8}, T{9}};
   ProxyIterator out{output.begin()};
   //auto out2 = output.begin() + 1;
 
-  int num = 2;
-  Proxy<int&> x{num};
+  T num{2};
+  Proxy<T&> x{num};
   int count = 1;
 
-  auto unary_pred = [](const Proxy<int>& val) { return val.getData() > 0; };
-  //auto binary_pred = [](const Proxy<int>& lhs, const Proxy<int>& rhs) { return lhs.getData() == rhs.getData(); };
-  auto binary_func = [](const Proxy<int>& lhs, const Proxy<int>&) -> Proxy<int> { return lhs.getData(); };
-  //auto gen = [] { return Proxy<int>(42); };
+  auto unary_pred = [](const Proxy<T&>&) { return true; };
+  //auto binary_pred = [](const Proxy<T>&, const Proxy<T>&) { return return false; };
+  auto binary_func = [](const Proxy<T>&, const Proxy<T>&) -> Proxy<T> { return Proxy<T>(T()); };
+  //auto gen = [] { return Proxy<T>(T{42}); };
   //std::mt19937 rand_gen;
 
   test(std::ranges::any_of, in, unary_pred);
@@ -100,43 +102,51 @@ constexpr bool test_all() {
   //test(std::ranges::is_permutation, in, in2);
   test(std::ranges::for_each, in, std::identity{});
   std::ranges::for_each_n(in.begin(), count, std::identity{});
-  test(std::ranges::copy, in, out);
-  std::ranges::copy_n(in.begin(), count, out);
-  test(std::ranges::copy_if, in, out, unary_pred);
-  // TODO: uncomment `copy_backward` once https://reviews.llvm.org/D128864 lands.
-  //test(std::ranges::copy_backward, in, out);
+  if constexpr (std::copyable<T>) {
+    test(std::ranges::copy, in, out);
+    std::ranges::copy_n(in.begin(), count, out);
+    test(std::ranges::copy_if, in, out, unary_pred);
+    // TODO: uncomment `copy_backward` once https://reviews.llvm.org/D128864 lands.
+    //test(std::ranges::copy_backward, in, out);
+  }
   test(std::ranges::move, in, out);
   // TODO: uncomment `move_backward` once https://reviews.llvm.org/D128864 lands.
   // test(std::ranges::move_backward, in, out);
-  test(std::ranges::fill, in, x);
-  std::ranges::fill_n(in.begin(), count, x);
-  test(std::ranges::transform, in, out, std::identity{});
-  test(std::ranges::transform, in, in2, out, binary_func);
+  if constexpr (std::copyable<T>) {
+    test(std::ranges::fill, in, x);
+    std::ranges::fill_n(in.begin(), count, x);
+    test(std::ranges::transform, in, out, std::identity{});
+    test(std::ranges::transform, in, in2, out, binary_func);
+  }
   //test(std::ranges::generate, in, gen);
   //std::ranges::generate_n(in.begin(), count, gen);
+  if constexpr (std::copyable<T>) {
   //test(std::ranges::remove_copy, in, out, x);
   //test(std::ranges::remove_copy_if, in, out, unary_pred);
-  test(std::ranges::replace, in, x, x);
-  test(std::ranges::replace_if, in, unary_pred, x);
-  //test(std::ranges::replace_copy, in, out, x, x);
-  //test(std::ranges::replace_copy_if, in, out, unary_pred, x);
-  //test(std::ranges::swap_ranges, in, in2);
-  test(std::ranges::reverse_copy, in, out);
-  test_mid(std::ranges::rotate_copy, in, mid, out);
-  //test(std::ranges::sample, in, out, count, rand_gen);
-  //test(std::ranges::unique_copy, in, out);
-  //test(std::ranges::partition_copy, in, out, out2, unary_pred);
-  //test_mid(std::ranges::partial_sort_copy, in, in2);
-  test(std::ranges::merge, in, in2, out);
-  test(std::ranges::set_
diff erence, in, in2, out);
-  test(std::ranges::set_intersection, in, in2, out);
-  test(std::ranges::set_symmetric_
diff erence, in, in2, out);
-  test(std::ranges::set_union, in, in2, out);
+    test(std::ranges::replace, in, x, x);
+    test(std::ranges::replace_if, in, unary_pred, x);
+    //test(std::ranges::replace_copy, in, out, x, x);
+    //test(std::ranges::replace_copy_if, in, out, unary_pred, x);
+  }
+  test(std::ranges::swap_ranges, in, in2);
+  if constexpr (std::copyable<T>) {
+    test(std::ranges::reverse_copy, in, out);
+    test_mid(std::ranges::rotate_copy, in, mid, out);
+    //test(std::ranges::unique_copy, in, out);
+    //test(std::ranges::partition_copy, in, out, out2, unary_pred);
+    //test_mid(std::ranges::partial_sort_copy, in, in2);
+    test(std::ranges::merge, in, in2, out);
+    test(std::ranges::set_
diff erence, in, in2, out);
+    test(std::ranges::set_intersection, in, in2, out);
+    test(std::ranges::set_symmetric_
diff erence, in, in2, out);
+    test(std::ranges::set_union, in, in2, out);
+  }
   test(std::ranges::remove, in, x);
   test(std::ranges::remove_if, in, unary_pred);
   test(std::ranges::reverse, in);
   //test_mid(std::ranges::rotate, in, mid);
   //test(std::ranges::shuffle, in, rand_gen);
+  //test(std::ranges::sample, in, out, count, rand_gen);
   //test(std::ranges::unique, in);
   test(std::ranges::partition, in, unary_pred);
   if (!std::is_constant_evaluated())
@@ -158,6 +168,11 @@ constexpr bool test_all() {
 
   // The algorithms that work on uninitialized memory have constraints that prevent proxy iterators from being used with
   // them.
+}
+
+constexpr bool test_all() {
+  run_tests<int>();
+  run_tests<MoveOnly>();
 
   return true;
 }

diff  --git a/libcxx/test/support/MoveOnly.h b/libcxx/test/support/MoveOnly.h
index 4178b55e6f41a..5cd2b246c6510 100644
--- a/libcxx/test/support/MoveOnly.h
+++ b/libcxx/test/support/MoveOnly.h
@@ -43,6 +43,10 @@ class MoveOnly
     friend TEST_CONSTEXPR bool operator>=(const MoveOnly& x, const MoveOnly& y)
         { return x.data_ >= y.data_; }
 
+#if TEST_STD_VER > 17
+    friend constexpr auto operator<=>(const MoveOnly&, const MoveOnly&) = default;
+#endif // TEST_STD_VER > 17
+
     TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const
         { return MoveOnly(data_ + x.data_); }
     TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const


        


More information about the libcxx-commits mailing list