Skip to content

[libc++][test] Refactor tests for rotate and rotate_copy #126458

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 2 commits into from
Feb 26, 2025

Conversation

winner245
Copy link
Contributor

This PR refactors the tests and fix some problems:

  • Refactor similar tests using types::for_each to remove redundant code;
  • Explicitly include the missing header type_algorithms.h instead of transitive include;
  • Fix the incorrect constexpr declaration in rotate.pass.cpp, where the test() function is incorrectly defined as TEST_CONSTEXPR_CXX17, which is wrong since std::rotate() becomes constexpr only since C++20.

@winner245 winner245 force-pushed the refactor-tests-for-rotate branch 3 times, most recently from db4a057 to ea4965c Compare February 10, 2025 14:13
@winner245 winner245 added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Feb 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 10, 2025

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

This PR refactors the tests and fix some problems:

  • Refactor similar tests using types::for_each to remove redundant code;
  • Explicitly include the missing header type_algorithms.h instead of transitive include;
  • Fix the incorrect constexpr declaration in rotate.pass.cpp, where the test() function is incorrectly defined as TEST_CONSTEXPR_CXX17, which is wrong since std::rotate() becomes constexpr only since C++20.

Patch is 39.64 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/126458.diff

6 Files Affected:

  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp (+1)
  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp (+17-26)
  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp (+24-36)
  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp (+150-153)
  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate_copy.pass.cpp (+120-127)
  • (modified) libcxx/test/support/test_iterators.h (+7)
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp
index 989313cc6d68d5c..5d1e34150c47208 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/pstl.rotate_copy.pass.cpp
@@ -27,6 +27,7 @@
 #include "test_macros.h"
 #include "test_execution_policies.h"
 #include "test_iterators.h"
+#include "type_algorithms.h"
 
 template <class Iter>
 struct Test {
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp
index 58b0f75d9b5f209..35856a65cc7036a 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges.rotate_copy.pass.cpp
@@ -26,9 +26,11 @@
 
 #include "almost_satisfies_types.h"
 #include "test_iterators.h"
+#include "type_algorithms.h"
 
 template <class Iter, class Out = int*, class Sent = sentinel_wrapper<Iter>>
-concept HasRotateCopyIt = requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
+concept HasRotateCopyIt =
+    requires(Iter first, Sent last, Out out) { std::ranges::rotate_copy(first, first, last, out); };
 
 template <class Range, class Out = int*>
 concept HasRotateCopyR = requires(Range range, Out out) { std::ranges::rotate_copy(range, nullptr, out); };
@@ -54,11 +56,8 @@ template <class Iter, class OutIter, class Sent, int N>
 constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int, N> expected) {
   {
     std::array<int, N> out;
-    std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret =
-        std::ranges::rotate_copy(Iter(value.data()),
-                                 Iter(value.data() + middle),
-                                 Sent(Iter(value.data() + value.size())),
-                                 OutIter(out.data()));
+    std::same_as<std::ranges::in_out_result<Iter, OutIter>> decltype(auto) ret = std::ranges::rotate_copy(
+        Iter(value.data()), Iter(value.data() + middle), Sent(Iter(value.data() + value.size())), OutIter(out.data()));
     assert(base(ret.in) == value.data() + value.size());
     assert(base(ret.out) == out.data() + out.size());
     assert(out == expected);
@@ -74,7 +73,7 @@ constexpr void test(std::array<int, N> value, std::size_t middle, std::array<int
   }
 }
 
-template <class Iter, class OutIter, class Sent>
+template <class Iter, class OutIter, class Sent = Iter>
 constexpr void test_iterators() {
   // simple test
   test<Iter, OutIter, Sent, 4>({1, 2, 3, 4}, 2, {3, 4, 1, 2});
@@ -101,41 +100,33 @@ constexpr void test_iterators() {
   test<Iter, OutIter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
 }
 
-template <class Iter, class Sent = Iter>
-constexpr void test_out_iterators() {
-  test_iterators<Iter, cpp20_output_iterator<int*>, Sent>();
-  test_iterators<Iter, forward_iterator<int*>, Sent>();
-  test_iterators<Iter, bidirectional_iterator<int*>, Sent>();
-  test_iterators<Iter, random_access_iterator<int*>, Sent>();
-  test_iterators<Iter, contiguous_iterator<int*>, Sent>();
-  test_iterators<Iter, int*, Sent>();
-}
-
 constexpr bool test() {
-  test_out_iterators<forward_iterator<int*>>();
-  test_out_iterators<bidirectional_iterator<int*>>();
-  test_out_iterators<random_access_iterator<int*>>();
-  test_out_iterators<contiguous_iterator<int*>>();
-  test_out_iterators<int*>();
-  test_out_iterators<const int*>();
+  types::for_each(types::forward_iterator_list<int*>(), []<class Iter>() {
+    types::for_each(types::cpp20_output_iterator_list<int*>(), []<class OutIter>() {
+      test_iterators<Iter, OutIter>();
+    });
+  });
 
   {
     struct AssignmentCounter {
       int* counter;
 
       constexpr AssignmentCounter(int* counter_) : counter(counter_) {}
-      constexpr AssignmentCounter& operator=(const AssignmentCounter&) { ++*counter; return *this; }
+      constexpr AssignmentCounter& operator=(const AssignmentCounter&) {
+        ++*counter;
+        return *this;
+      }
     };
 
     {
-      int c = 0;
+      int c                 = 0;
       AssignmentCounter a[] = {&c, &c, &c, &c};
       AssignmentCounter b[] = {&c, &c, &c, &c};
       std::ranges::rotate_copy(a, a + 2, a + 4, b);
       assert(c == 4);
     }
     {
-      int c = 0;
+      int c                 = 0;
       AssignmentCounter a[] = {&c, &c, &c, &c};
       AssignmentCounter b[] = {&c, &c, &c, &c};
       std::ranges::rotate_copy(a, a + 2, b);
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
index 1506ce79b5dbd2e..56d0dfaf9ee2782 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp
@@ -24,15 +24,15 @@
 
 #include "almost_satisfies_types.h"
 #include "test_iterators.h"
+#include "type_algorithms.h"
 
 // Test constraints of the (iterator, sentinel) overload.
 // ======================================================
 
 template <class Iter = int*, class Sent = int*>
-concept HasRotateIter =
-    requires(Iter&& iter, Sent&& sent) {
-      std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
-    };
+concept HasRotateIter = requires(Iter&& iter, Sent&& sent) {
+  std::ranges::rotate(std::forward<Iter>(iter), std::forward<Iter>(iter), std::forward<Sent>(sent));
+};
 
 static_assert(HasRotateIter<int*, int*>);
 
@@ -48,10 +48,9 @@ static_assert(!HasRotateIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
 // =========================================
 
 template <class Range>
-concept HasRotateRange =
-    requires(Range&& range, std::ranges::iterator_t<Range> iter) {
-      std::ranges::rotate(std::forward<Range>(range), iter);
-    };
+concept HasRotateRange = requires(Range&& range, std::ranges::iterator_t<Range> iter) {
+  std::ranges::rotate(std::forward<Range>(range), iter);
+};
 
 template <class T>
 using R = UncheckedRange<T>;
@@ -73,10 +72,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
   assert(mid_index <= N);
 
   { // (iterator, sentinel) overload.
-    auto in = input;
+    auto in    = input;
     auto begin = Iter(in.data());
-    auto mid = Iter(in.data() + mid_index);
-    auto end = Sent(Iter(in.data() + in.size()));
+    auto mid   = Iter(in.data() + mid_index);
+    auto end   = Sent(Iter(in.data() + in.size()));
 
     std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(begin, mid, end);
     assert(base(result.begin()) == in.data() + in.size() - mid_index);
@@ -85,10 +84,10 @@ constexpr void test_one(const std::array<int, N> input, std::size_t mid_index, s
   }
 
   { // (range) overload.
-    auto in = input;
+    auto in    = input;
     auto begin = Iter(in.data());
-    auto mid = Iter(in.data() + mid_index);
-    auto end = Sent(Iter(in.data() + in.size()));
+    auto mid   = Iter(in.data() + mid_index);
+    auto end   = Sent(Iter(in.data() + in.size()));
     auto range = std::ranges::subrange(std::move(begin), std::move(end));
 
     std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::rotate(range, mid);
@@ -132,32 +131,21 @@ constexpr void test_iter_sent() {
   test_one<Iter, Sent, 7>({1, 2, 3, 4, 5, 6, 7}, 7, {1, 2, 3, 4, 5, 6, 7});
 }
 
-template <class Iter>
-constexpr void test_iter() {
-  test_iter_sent<Iter, Iter>();
-  test_iter_sent<Iter, sentinel_wrapper<Iter>>();
-}
-
-constexpr void test_iterators() {
-  test_iter<forward_iterator<int*>>();
-  test_iter<bidirectional_iterator<int*>>();
-  test_iter<random_access_iterator<int*>>();
-  test_iter<contiguous_iterator<int*>>();
-  test_iter<int*>();
-}
-
 constexpr bool test() {
-  test_iterators();
+  types::for_each(types::forward_iterator_list<int*>(), []<class Iter>() {
+    test_iter_sent<Iter, Iter>();
+    test_iter_sent<Iter, sentinel_wrapper<Iter>>();
+  });
 
   { // Complexity: at most `last - first` swaps.
     const std::array input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
-    auto expected = static_cast<int>(input.size());
+    auto expected          = static_cast<int>(input.size());
 
     {
-      auto in = input;
-      int swaps = 0;
+      auto in    = input;
+      int swaps  = 0;
       auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
-      auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
+      auto end   = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
 
       for (std::size_t mid = 0; mid != input.size(); ++mid) {
         std::ranges::rotate(begin, begin + mid, end);
@@ -166,10 +154,10 @@ constexpr bool test() {
     }
 
     {
-      auto in = input;
-      int swaps = 0;
+      auto in    = input;
+      int swaps  = 0;
       auto begin = adl::Iterator::TrackSwaps(in.data(), swaps);
-      auto end = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
+      auto end   = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
       auto range = std::ranges::subrange(begin, end);
 
       for (std::size_t mid = 0; mid != input.size(); ++mid) {
diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
index 7bbf3abd43dc57f..91a74786216ec6e 100644
--- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
@@ -9,141 +9,141 @@
 // <algorithm>
 
 // template<ShuffleIterator Iter>
-//   Iter
-//   rotate(Iter first, Iter middle, Iter last);
+//   Iter rotate(Iter first, Iter middle, Iter last); // constexpr since C++20
 
 #include <algorithm>
 #include <cassert>
 #include <memory>
+#include <type_traits>
 
 #include "test_macros.h"
 #include "test_iterators.h"
+#include "type_algorithms.h"
 
-template <class Iter>
-TEST_CONSTEXPR_CXX17 bool
-test()
-{
-    int ia[] = {0};
-    const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
-    Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
+struct TestIter {
+  template <class Iter>
+  TEST_CONSTEXPR_CXX20 void operator()() const {
+    int ia[]     = {0};
+    const int sa = static_cast<int>(sizeof(ia) / sizeof(ia[0]));
+    Iter r       = std::rotate(Iter(ia), Iter(ia), Iter(ia));
     assert(base(r) == ia);
     assert(ia[0] == 0);
-    r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
-    assert(base(r) == ia+sa);
+    r = std::rotate(Iter(ia), Iter(ia), Iter(ia + sa));
+    assert(base(r) == ia + sa);
     assert(ia[0] == 0);
-    r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
+    r = std::rotate(Iter(ia), Iter(ia + sa), Iter(ia + sa));
     assert(base(r) == ia);
     assert(ia[0] == 0);
 
-    int ib[] = {0, 1};
-    const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
-    r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
-    assert(base(r) == ib+sb);
+    int ib[]     = {0, 1};
+    const int sb = static_cast<int>(sizeof(ib) / sizeof(ib[0]));
+    r            = std::rotate(Iter(ib), Iter(ib), Iter(ib + sb));
+    assert(base(r) == ib + sb);
     assert(ib[0] == 0);
     assert(ib[1] == 1);
-    r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
-    assert(base(r) == ib+1);
+    r = std::rotate(Iter(ib), Iter(ib + 1), Iter(ib + sb));
+    assert(base(r) == ib + 1);
     assert(ib[0] == 1);
     assert(ib[1] == 0);
-    r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
+    r = std::rotate(Iter(ib), Iter(ib + sb), Iter(ib + sb));
     assert(base(r) == ib);
     assert(ib[0] == 1);
     assert(ib[1] == 0);
 
-    int ic[] = {0, 1, 2};
-    const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
-    r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
-    assert(base(r) == ic+sc);
+    int ic[]     = {0, 1, 2};
+    const int sc = static_cast<int>(sizeof(ic) / sizeof(ic[0]));
+    r            = std::rotate(Iter(ic), Iter(ic), Iter(ic + sc));
+    assert(base(r) == ic + sc);
     assert(ic[0] == 0);
     assert(ic[1] == 1);
     assert(ic[2] == 2);
-    r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
-    assert(base(r) == ic+2);
+    r = std::rotate(Iter(ic), Iter(ic + 1), Iter(ic + sc));
+    assert(base(r) == ic + 2);
     assert(ic[0] == 1);
     assert(ic[1] == 2);
     assert(ic[2] == 0);
-    r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
-    assert(base(r) == ic+1);
+    r = std::rotate(Iter(ic), Iter(ic + 2), Iter(ic + sc));
+    assert(base(r) == ic + 1);
     assert(ic[0] == 0);
     assert(ic[1] == 1);
     assert(ic[2] == 2);
-    r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
+    r = std::rotate(Iter(ic), Iter(ic + sc), Iter(ic + sc));
     assert(base(r) == ic);
     assert(ic[0] == 0);
     assert(ic[1] == 1);
     assert(ic[2] == 2);
 
-    int id[] = {0, 1, 2, 3};
-    const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
-    r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
-    assert(base(r) == id+sd);
+    int id[]     = {0, 1, 2, 3};
+    const int sd = static_cast<int>(sizeof(id) / sizeof(id[0]));
+    r            = std::rotate(Iter(id), Iter(id), Iter(id + sd));
+    assert(base(r) == id + sd);
     assert(id[0] == 0);
     assert(id[1] == 1);
     assert(id[2] == 2);
     assert(id[3] == 3);
-    r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
-    assert(base(r) == id+3);
+    r = std::rotate(Iter(id), Iter(id + 1), Iter(id + sd));
+    assert(base(r) == id + 3);
     assert(id[0] == 1);
     assert(id[1] == 2);
     assert(id[2] == 3);
     assert(id[3] == 0);
-    r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
-    assert(base(r) == id+2);
+    r = std::rotate(Iter(id), Iter(id + 2), Iter(id + sd));
+    assert(base(r) == id + 2);
     assert(id[0] == 3);
     assert(id[1] == 0);
     assert(id[2] == 1);
     assert(id[3] == 2);
-    r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
-    assert(base(r) == id+1);
+    r = std::rotate(Iter(id), Iter(id + 3), Iter(id + sd));
+    assert(base(r) == id + 1);
     assert(id[0] == 2);
     assert(id[1] == 3);
     assert(id[2] == 0);
     assert(id[3] == 1);
-    r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
+    r = std::rotate(Iter(id), Iter(id + sd), Iter(id + sd));
     assert(base(r) == id);
     assert(id[0] == 2);
     assert(id[1] == 3);
     assert(id[2] == 0);
     assert(id[3] == 1);
 
-    int ie[] = {0, 1, 2, 3, 4};
-    const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
-    r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
-    assert(base(r) == ie+se);
+    int ie[]     = {0, 1, 2, 3, 4};
+    const int se = static_cast<int>(sizeof(ie) / sizeof(ie[0]));
+    r            = std::rotate(Iter(ie), Iter(ie), Iter(ie + se));
+    assert(base(r) == ie + se);
     assert(ie[0] == 0);
     assert(ie[1] == 1);
     assert(ie[2] == 2);
     assert(ie[3] == 3);
     assert(ie[4] == 4);
-    r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
-    assert(base(r) == ie+4);
+    r = std::rotate(Iter(ie), Iter(ie + 1), Iter(ie + se));
+    assert(base(r) == ie + 4);
     assert(ie[0] == 1);
     assert(ie[1] == 2);
     assert(ie[2] == 3);
     assert(ie[3] == 4);
     assert(ie[4] == 0);
-    r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
-    assert(base(r) == ie+3);
+    r = std::rotate(Iter(ie), Iter(ie + 2), Iter(ie + se));
+    assert(base(r) == ie + 3);
     assert(ie[0] == 3);
     assert(ie[1] == 4);
     assert(ie[2] == 0);
     assert(ie[3] == 1);
     assert(ie[4] == 2);
-    r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
-    assert(base(r) == ie+2);
+    r = std::rotate(Iter(ie), Iter(ie + 3), Iter(ie + se));
+    assert(base(r) == ie + 2);
     assert(ie[0] == 1);
     assert(ie[1] == 2);
     assert(ie[2] == 3);
     assert(ie[3] == 4);
     assert(ie[4] == 0);
-    r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
-    assert(base(r) == ie+1);
+    r = std::rotate(Iter(ie), Iter(ie + 4), Iter(ie + se));
+    assert(base(r) == ie + 1);
     assert(ie[0] == 0);
     assert(ie[1] == 1);
     assert(ie[2] == 2);
     assert(ie[3] == 3);
     assert(ie[4] == 4);
-    r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
+    r = std::rotate(Iter(ie), Iter(ie + se), Iter(ie + se));
     assert(base(r) == ie);
     assert(ie[0] == 0);
     assert(ie[1] == 1);
@@ -151,57 +151,57 @@ test()
     assert(ie[3] == 3);
     assert(ie[4] == 4);
 
-    int ig[] = {0, 1, 2, 3, 4, 5};
-    const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
-    r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
-    assert(base(r) == ig+sg);
+    int ig[]     = {0, 1, 2, 3, 4, 5};
+    const int sg = static_cast<int>(sizeof(ig) / sizeof(ig[0]));
+    r            = std::rotate(Iter(ig), Iter(ig), Iter(ig + sg));
+    assert(base(r) == ig + sg);
     assert(ig[0] == 0);
     assert(ig[1] == 1);
     assert(ig[2] == 2);
     assert(ig[3] == 3);
     assert(ig[4] == 4);
     assert(ig[5] == 5);
-    r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
-    assert(base(r) == ig+5);
+    r = std::rotate(Iter(ig), Iter(ig + 1), Iter(ig + sg));
+    assert(base(r) == ig + 5);
     assert(ig[0] == 1);
     assert(ig[1] == 2);
     assert(ig[2] == 3);
     assert(ig[3] == 4);
     assert(ig[4] == 5);
     assert(ig[5] == 0);
-    r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
-    assert(base(r) == ig+4);
+    r = std::rotate(Iter(ig), Iter(ig + 2), Iter(ig + sg));
+    assert(base(r) == ig + 4);
     assert(ig[0] == 3);
     assert(ig[1] == 4);
     assert(ig[2] == 5);
     assert(ig[3] == 0);
     assert(ig[4] == 1);
     assert(ig[5] == 2);
-    r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
-    assert(base(r) == ig+3);
+    r = std::rotate(Iter(ig), Iter(ig + 3), Iter(ig + sg));
+    assert(base(r) == ig + 3);
     assert(ig[0] == 0);
     assert(ig[1] == 1);
     assert(ig[2] == 2);
     assert(ig[3] == 3);
     assert(ig[4] == 4);
     assert(ig[5] == 5);
-    r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
-    assert(base(r) == ig+2);
+    r = std::rotate(Iter(ig), Iter(ig + 4), Iter(ig + sg));
+    assert(base(r) == ig + 2);
     assert(ig[0] == 4);
     assert(ig[1] == 5);
     assert(ig[2] == 0);
     assert(ig[3] == 1);
     assert(ig[4] == 2);
     assert(ig[5] == 3);
-    r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
-    assert(base(r) == ig+1);
+    r = std::rotate(Iter(ig), Iter(ig + 5), Iter(ig + sg));
+    assert(base(r) == ig + 1);
     assert(ig[0] == 3);
     assert(ig[1] == 4);
     assert(ig[2] == 5);
     assert(ig[3] == 0);
     assert(ig[4] == 1);
     assert(ig[5] == 2);
-    r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
+    r = std::rotate(Iter(ig), Iter(ig + sg), Iter(ig + sg));
     assert(base(r) == ig);
     assert(ig[0] == 3);
     assert(ig[1] == 4);
@@ -209,101 +209,99 @@ test()
     assert(ig[3] == 0);
     assert(ig[4] == 1);
     assert(ig[5] == 2);
-
-    return true;
-}
+  }
+};
 
 #if TEST_STD_VER >= 11
 
-template <class Iter>
-void
-test1()
-{
+struct TestUniquePtr {
+  template <class Iter>
+  TEST_CONSTEXPR_CXX23 void operator()() const {
     std::unique_ptr<int> ia[1];
-    const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
+    const int sa = static_cast<int>(sizeof(ia) / sizeof(ia[0]));
     for (int i = 0; i < sa; ++i)
-        ia[i].reset(new int(i));
+      ia[i].reset(new int(i));
     Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
     assert(base(r) == ia);
     assert(*ia[0] == 0);
-    r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
-    assert(base(r) == ia+sa);
+    r = std::rotate(Iter(ia), Iter(ia), Iter(ia + sa));
+    assert(base(r) == ia + sa);
     assert(*ia[0] == 0);
-    r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
+    r = std::rotate(Iter(ia), Iter(ia + sa), Iter(ia + sa));
     assert(base(r) == ia);
     assert(*ia[0] == 0);
 
     std::unique_ptr<int> ib[2];
-    const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
+    const int sb = static_cast<int>(sizeof(ib) / sizeof(ib[0]));
     for (int i = 0; i < ...
[truncated]

@winner245 winner245 marked this pull request as ready for review February 12, 2025 16:41
@winner245 winner245 requested a review from a team as a code owner February 12, 2025 16:41
Copy link
Member

@ldionne ldionne 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 refactoring! This basically LGTM but I'd like to see this again since I requested some changes. I don't expect to have further comments once they are applied, though.

@winner245 winner245 force-pushed the refactor-tests-for-rotate branch 2 times, most recently from 35664a0 to 0d946c1 Compare February 22, 2025 03:07
@winner245 winner245 force-pushed the refactor-tests-for-rotate branch from 0d946c1 to a4bc400 Compare February 26, 2025 16:53
@ldionne ldionne merged commit f161b1b into llvm:main Feb 26, 2025
13 of 22 checks passed
@winner245 winner245 deleted the refactor-tests-for-rotate branch February 26, 2025 17:01
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants