Skip to content

Conversation

jwakely
Copy link
Contributor

@jwakely jwakely commented Sep 11, 2025

The complexity is "at most N swaps" for each invocation of rotate, but the tests currently assert that the total number of swaps for N calls is at most N. The standard allows that to be N squared, so the test is either requiring more than the standard (and the comment in the test) promises, or somebody just forgot to reset the counter on each iteration.

If the intent really is to verify the libc++ guarantee, not the standard one, then shouldn't expected be set to N/2 i.e. 5, since that's what libc++ actually does?

Either way, to be portable to other implementations, swaps should be reset on each loop. If you want to guard that with #ifndef _LIBCPP_VERSION it would not change the test for libc++.

The complexity is "at most N swaps" _for each invocation of `rotate`_, but the tests currently assert that the total number of swaps for N calls is at most N. The standard allows that to be N squared, so the test is either requiring more than the standard (and the comment in the test) promises, or somebody just forgot to reset the counter on each iteration.

If the intent really is to verify the libc++ guarantee, not the standard one, then shouldn't `expected` be set to N/2 i.e. 5, since that's what libc++ actually does?

Either way, to be portable to other implementations, swaps should be reset on each loop. If you want to guard that with `#ifndef _LIBCPP_VERSION` it would not change the test for libc++.
@jwakely jwakely requested a review from a team as a code owner September 11, 2025 20:06
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 11, 2025

@llvm/pr-subscribers-libcxx

Author: Jonathan Wakely (jwakely)

Changes

The complexity is "at most N swaps" for each invocation of rotate, but the tests currently assert that the total number of swaps for N calls is at most N. The standard allows that to be N squared, so the test is either requiring more than the standard (and the comment in the test) promises, or somebody just forgot to reset the counter on each iteration.

If the intent really is to verify the libc++ guarantee, not the standard one, then shouldn't expected be set to N/2 i.e. 5, since that's what libc++ actually does?

Either way, to be portable to other implementations, swaps should be reset on each loop. If you want to guard that with #ifndef _LIBCPP_VERSION it would not change the test for libc++.


Full diff: https://github.com/llvm/llvm-project/pull/158144.diff

1 Files Affected:

  • (modified) libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/ranges_rotate.pass.cpp (+2)
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 5f594400e8321..574e96dea46a0 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
@@ -173,6 +173,7 @@ constexpr bool test() {
       auto end   = adl::Iterator::TrackSwaps(in.data() + in.size(), swaps);
 
       for (std::size_t mid = 0; mid != input.size(); ++mid) {
+        swaps = 0;
         std::ranges::rotate(begin, begin + mid, end);
         assert(swaps <= expected);
       }
@@ -186,6 +187,7 @@ constexpr bool test() {
       auto range = std::ranges::subrange(begin, end);
 
       for (std::size_t mid = 0; mid != input.size(); ++mid) {
+        swaps = 0;
         std::ranges::rotate(range, begin + mid);
         assert(swaps <= expected);
       }

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.

This seems correct to me. We're trying to test the standard guarantee, and clearly we just forgot to reset it.

@ldionne ldionne merged commit 69e3ff6 into llvm:main Sep 12, 2025
69 of 73 checks passed
@jwakely jwakely deleted the patch-3 branch September 12, 2025 07:09
@jwakely
Copy link
Contributor Author

jwakely commented Sep 12, 2025

Thanks! one more local change I can drop when using these for libstdc++ testing :)

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