[libcxx-commits] [libcxx] [libc++] Optimize ranges::rotate for vector<bool>::iterator (PR #121168)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Dec 26 16:41:28 PST 2024


https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/121168

>From 7bd6e5840694e45f4f838560da8828530fded63d Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Thu, 26 Dec 2024 19:36:15 -0500
Subject: [PATCH] Optimize ranges::rotate for vector<bool>::iterator

---
 libcxx/include/__algorithm/rotate.h           |  74 ++
 libcxx/include/__bit_reference                |  80 +-
 .../benchmarks/algorithms/rotate.bench.cpp    |  40 +
 .../alg.rotate/ranges_rotate.pass.cpp         |  67 +-
 .../alg.rotate/rotate.pass.cpp                | 810 +++++++++---------
 5 files changed, 580 insertions(+), 491 deletions(-)
 create mode 100644 libcxx/test/benchmarks/algorithms/rotate.bench.cpp

diff --git a/libcxx/include/__algorithm/rotate.h b/libcxx/include/__algorithm/rotate.h
index df4ca95aac95bc..10e7fd77023790 100644
--- a/libcxx/include/__algorithm/rotate.h
+++ b/libcxx/include/__algorithm/rotate.h
@@ -9,12 +9,17 @@
 #ifndef _LIBCPP___ALGORITHM_ROTATE_H
 #define _LIBCPP___ALGORITHM_ROTATE_H
 
+#include <__algorithm/copy.h>
+#include <__algorithm/copy_backward.h>
 #include <__algorithm/iterator_operations.h>
 #include <__algorithm/move.h>
 #include <__algorithm/move_backward.h>
 #include <__algorithm/swap_ranges.h>
 #include <__config>
 #include <__iterator/iterator_traits.h>
+#include <__memory/construct_at.h>
+#include <__memory/pointer_traits.h>
+#include <__type_traits/is_constant_evaluated.h>
 #include <__type_traits/is_trivially_assignable.h>
 #include <__utility/move.h>
 #include <__utility/pair.h>
@@ -185,6 +190,75 @@ __rotate(_Iterator __first, _Iterator __middle, _Sentinel __last) {
   return _Ret(std::move(__result), std::move(__last_iter));
 }
 
+template <class _Cp>
+struct __bit_array {
+  using difference_type   = typename _Cp::difference_type;
+  using __storage_type    = typename _Cp::__storage_type;
+  using __storage_pointer = typename _Cp::__storage_pointer;
+  using iterator          = typename _Cp::iterator;
+
+  static const unsigned __bits_per_word = _Cp::__bits_per_word;
+  static const unsigned _Np             = 4;
+
+  difference_type __size_;
+  __storage_type __word_[_Np];
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
+    return static_cast<difference_type>(_Np * __bits_per_word);
+  }
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
+    if (__libcpp_is_constant_evaluated()) {
+      for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
+        std::__construct_at(__word_ + __i, 0);
+    }
+  }
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
+    return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
+  }
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
+    return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
+                    static_cast<unsigned>(__size_ % __bits_per_word));
+  }
+};
+
+template <class, class _Cp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<__bit_iterator<_Cp, false>, __bit_iterator<_Cp, false> >
+__rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) {
+  using _I1             = __bit_iterator<_Cp, false>;
+  using difference_type = typename _I1::difference_type;
+  difference_type __d1  = __middle - __first;
+  difference_type __d2  = __last - __middle;
+  _I1 __r               = __first + __d2;
+  while (__d1 != 0 && __d2 != 0) {
+    if (__d1 <= __d2) {
+      if (__d1 <= __bit_array<_Cp>::capacity()) {
+        __bit_array<_Cp> __b(__d1);
+        std::copy(__first, __middle, __b.begin());
+        std::copy(__b.begin(), __b.end(), std::copy(__middle, __last, __first));
+        break;
+      } else {
+        __bit_iterator<_Cp, false> __mp = std::swap_ranges(__first, __middle, __middle);
+        __first                         = __middle;
+        __middle                        = __mp;
+        __d2 -= __d1;
+      }
+    } else {
+      if (__d2 <= __bit_array<_Cp>::capacity()) {
+        __bit_array<_Cp> __b(__d2);
+        std::copy(__middle, __last, __b.begin());
+        std::copy_backward(__b.begin(), __b.end(), std::copy_backward(__first, __middle, __last));
+        break;
+      } else {
+        __bit_iterator<_Cp, false> __mp = __first + __d2;
+        std::swap_ranges(__first, __mp, __middle);
+        __first = __mp;
+        __d1 -= __d2;
+      }
+    }
+  }
+  return std::make_pair(__r, __last);
+}
+
 template <class _ForwardIterator>
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _ForwardIterator
 rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last) {
diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index 9fa24c98d493fd..d3e6e3cd8d4cb6 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -12,6 +12,7 @@
 
 #include <__algorithm/copy_n.h>
 #include <__algorithm/min.h>
+#include <__algorithm/rotate.h>
 #include <__bit/countr.h>
 #include <__compare/ordering.h>
 #include <__config>
@@ -22,6 +23,7 @@
 #include <__memory/pointer_traits.h>
 #include <__type_traits/conditional.h>
 #include <__type_traits/is_constant_evaluated.h>
+#include <__utility/pair.h>
 #include <__utility/swap.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -583,78 +585,6 @@ inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cr, false> swap_ranges(
   return std::__swap_ranges_unaligned(__first1, __last1, __first2);
 }
 
-// rotate
-
-template <class _Cp>
-struct __bit_array {
-  using difference_type   = typename _Cp::difference_type;
-  using __storage_type    = typename _Cp::__storage_type;
-  using __storage_pointer = typename _Cp::__storage_pointer;
-  using iterator          = typename _Cp::iterator;
-
-  static const unsigned __bits_per_word = _Cp::__bits_per_word;
-  static const unsigned _Np             = 4;
-
-  difference_type __size_;
-  __storage_type __word_[_Np];
-
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static difference_type capacity() {
-    return static_cast<difference_type>(_Np * __bits_per_word);
-  }
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit __bit_array(difference_type __s) : __size_(__s) {
-    if (__libcpp_is_constant_evaluated()) {
-      for (size_t __i = 0; __i != __bit_array<_Cp>::_Np; ++__i)
-        std::__construct_at(__word_ + __i, 0);
-    }
-  }
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() {
-    return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]), 0);
-  }
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() {
-    return iterator(pointer_traits<__storage_pointer>::pointer_to(__word_[0]) + __size_ / __bits_per_word,
-                    static_cast<unsigned>(__size_ % __bits_per_word));
-  }
-};
-
-template <class _Cp>
-_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, false>
-rotate(__bit_iterator<_Cp, false> __first, __bit_iterator<_Cp, false> __middle, __bit_iterator<_Cp, false> __last) {
-  using _I1             = __bit_iterator<_Cp, false>;
-  using difference_type = typename _I1::difference_type;
-
-  difference_type __d1 = __middle - __first;
-  difference_type __d2 = __last - __middle;
-  _I1 __r              = __first + __d2;
-  while (__d1 != 0 && __d2 != 0) {
-    if (__d1 <= __d2) {
-      if (__d1 <= __bit_array<_Cp>::capacity()) {
-        __bit_array<_Cp> __b(__d1);
-        std::copy(__first, __middle, __b.begin());
-        std::copy(__b.begin(), __b.end(), std::copy(__middle, __last, __first));
-        break;
-      } else {
-        __bit_iterator<_Cp, false> __mp = std::swap_ranges(__first, __middle, __middle);
-        __first                         = __middle;
-        __middle                        = __mp;
-        __d2 -= __d1;
-      }
-    } else {
-      if (__d2 <= __bit_array<_Cp>::capacity()) {
-        __bit_array<_Cp> __b(__d2);
-        std::copy(__middle, __last, __b.begin());
-        std::copy_backward(__b.begin(), __b.end(), std::copy_backward(__first, __middle, __last));
-        break;
-      } else {
-        __bit_iterator<_Cp, false> __mp = __first + __d2;
-        std::swap_ranges(__first, __mp, __middle);
-        __first = __mp;
-        __d1 -= __d2;
-      }
-    }
-  }
-  return __r;
-}
-
 // equal
 
 template <class _Cp, bool _IC1, bool _IC2>
@@ -995,9 +925,9 @@ private:
   template <class _Cl, class _Cr>
   friend __bit_iterator<_Cr, false>
       swap_ranges(__bit_iterator<_Cl, false>, __bit_iterator<_Cl, false>, __bit_iterator<_Cr, false>);
-  template <class _Dp>
-  _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false>
-      rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
+  template <class, class _Dp>
+  _LIBCPP_CONSTEXPR_SINCE_CXX20 friend pair<__bit_iterator<_Cp, false>, __bit_iterator<_Cp, false> >
+      __rotate(__bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>, __bit_iterator<_Dp, false>);
   template <class _Dp, bool _IC1, bool _IC2>
   _LIBCPP_CONSTEXPR_SINCE_CXX20 friend bool
       __equal_aligned(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
diff --git a/libcxx/test/benchmarks/algorithms/rotate.bench.cpp b/libcxx/test/benchmarks/algorithms/rotate.bench.cpp
new file mode 100644
index 00000000000000..48843d00b84f5f
--- /dev/null
+++ b/libcxx/test/benchmarks/algorithms/rotate.bench.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <algorithm>
+#include <benchmark/benchmark.h>
+#include <vector>
+
+static void bm_ranges_rotate(benchmark::State& state) {
+  auto n = state.range();
+  std::vector<bool> v(n);
+  auto mid = std::ranges::begin(v) + n / 2;
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(std::ranges::rotate(v, mid));
+    benchmark::DoNotOptimize(&v);
+  }
+}
+
+static void bm_rotate(benchmark::State& state) {
+  auto n = state.range();
+  std::vector<bool> v(n);
+  auto beg = v.begin();
+  auto mid = v.begin() + n / 2;
+  auto end = v.end();
+  for (auto _ : state) {
+    benchmark::DoNotOptimize(std::rotate(beg, mid, end));
+    benchmark::DoNotOptimize(&v);
+  }
+}
+
+BENCHMARK(bm_ranges_rotate)->Range(8, 1 << 20);
+BENCHMARK(bm_rotate)->Range(8, 1 << 20);
+
+BENCHMARK_MAIN();
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 1506ce79b5dbd2..04df4bda00f7e7 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
@@ -21,6 +21,7 @@
 #include <array>
 #include <cassert>
 #include <ranges>
+#include <vector>
 
 #include "almost_satisfies_types.h"
 #include "test_iterators.h"
@@ -29,10 +30,9 @@
 // ======================================================
 
 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);
@@ -146,18 +145,39 @@ constexpr void test_iterators() {
   test_iter<int*>();
 }
 
+template <std::size_t N>
+TEST_CONSTEXPR_CXX20 bool test_bit_iterator() {
+  for (int offset = -1; offset <= 1; ++offset) {
+    std::vector<bool> a(N, false);
+    std::size_t mid = N / 2 + offset;
+    for (std::size_t i = mid; i < N; ++i)
+      a[i] = true;
+
+    // (iterator, sentinel)-overload
+    std::ranges::rotate(std::ranges::begin(a), std::ranges::begin(a) + mid, std::ranges::end(a));
+    for (std::size_t i = 0; i < N; ++i)
+      assert(a[i] == (i < N - mid));
+
+    // range-overload
+    std::ranges::rotate(a, std::ranges::begin(a) + (N - mid));
+    for (std::size_t i = 0; i < N; ++i)
+      assert(a[i] == (i >= mid));
+  }
+  return true;
+};
+
 constexpr bool test() {
   test_iterators();
 
   { // 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 +186,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) {
@@ -179,6 +199,11 @@ constexpr bool test() {
     }
   }
 
+  test_bit_iterator<8>();
+  test_bit_iterator<16>();
+  test_bit_iterator<64>();
+  test_bit_iterator<100>();
+
   return true;
 }
 
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 7bbf3abd43dc57..ce4a3c2211def8 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
@@ -15,433 +15,453 @@
 #include <algorithm>
 #include <cassert>
 #include <memory>
+#include <vector>
 
 #include "test_macros.h"
 #include "test_iterators.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));
-    assert(base(r) == ia);
-    assert(ia[0] == 0);
-    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));
-    assert(base(r) == ia);
-    assert(ia[0] == 0);
+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));
+  assert(base(r) == ia);
+  assert(ia[0] == 0);
+  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));
+  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);
-    assert(ib[0] == 0);
-    assert(ib[1] == 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));
-    assert(base(r) == ib);
-    assert(ib[0] == 1);
-    assert(ib[1] == 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);
+  assert(ib[0] == 0);
+  assert(ib[1] == 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));
+  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);
-    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);
-    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);
-    assert(ic[0] == 0);
-    assert(ic[1] == 1);
-    assert(ic[2] == 2);
-    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 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);
+  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);
+  assert(ic[0] == 0);
+  assert(ic[1] == 1);
+  assert(ic[2] == 2);
+  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);
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == id);
-    assert(id[0] == 2);
-    assert(id[1] == 3);
-    assert(id[2] == 0);
-    assert(id[3] == 1);
+  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);
+  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);
+  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);
+  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));
+  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);
-    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);
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == ie);
-    assert(ie[0] == 0);
-    assert(ie[1] == 1);
-    assert(ie[2] == 2);
-    assert(ie[3] == 3);
-    assert(ie[4] == 4);
+  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);
+  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);
+  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);
+  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);
+  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));
+  assert(base(r) == ie);
+  assert(ie[0] == 0);
+  assert(ie[1] == 1);
+  assert(ie[2] == 2);
+  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);
-    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);
-    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);
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == ig);
-    assert(ig[0] == 3);
-    assert(ig[1] == 4);
-    assert(ig[2] == 5);
-    assert(ig[3] == 0);
-    assert(ig[4] == 1);
-    assert(ig[5] == 2);
+  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);
+  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);
+  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);
+  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);
+  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);
+  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));
+  assert(base(r) == ig);
+  assert(ig[0] == 3);
+  assert(ig[1] == 4);
+  assert(ig[2] == 5);
+  assert(ig[3] == 0);
+  assert(ig[4] == 1);
+  assert(ig[5] == 2);
 
-    return true;
+  return true;
 }
 
 #if TEST_STD_VER >= 11
 
 template <class Iter>
-void
-test1()
-{
-    std::unique_ptr<int> ia[1];
-    const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
-    for (int i = 0; i < sa; ++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);
-    assert(*ia[0] == 0);
-    r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
-    assert(base(r) == ia);
-    assert(*ia[0] == 0);
+void test1() {
+  std::unique_ptr<int> ia[1];
+  const int sa = static_cast<int>(sizeof(ia) / sizeof(ia[0]));
+  for (int i = 0; i < sa; ++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);
+  assert(*ia[0] == 0);
+  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]));
-    for (int i = 0; i < sb; ++i)
-        ib[i].reset(new int(i));
-    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);
-    assert(*ib[0] == 1);
-    assert(*ib[1] == 0);
-    r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
-    assert(base(r) == ib);
-    assert(*ib[0] == 1);
-    assert(*ib[1] == 0);
+  std::unique_ptr<int> ib[2];
+  const int sb = static_cast<int>(sizeof(ib) / sizeof(ib[0]));
+  for (int i = 0; i < sb; ++i)
+    ib[i].reset(new int(i));
+  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);
+  assert(*ib[0] == 1);
+  assert(*ib[1] == 0);
+  r = std::rotate(Iter(ib), Iter(ib + sb), Iter(ib + sb));
+  assert(base(r) == ib);
+  assert(*ib[0] == 1);
+  assert(*ib[1] == 0);
 
-    std::unique_ptr<int> ic[3];
-    const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
-    for (int i = 0; i < sc; ++i)
-        ic[i].reset(new int(i));
-    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);
-    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);
-    assert(*ic[0] == 0);
-    assert(*ic[1] == 1);
-    assert(*ic[2] == 2);
-    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);
+  std::unique_ptr<int> ic[3];
+  const int sc = static_cast<int>(sizeof(ic) / sizeof(ic[0]));
+  for (int i = 0; i < sc; ++i)
+    ic[i].reset(new int(i));
+  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);
+  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);
+  assert(*ic[0] == 0);
+  assert(*ic[1] == 1);
+  assert(*ic[2] == 2);
+  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);
 
-    std::unique_ptr<int> id[4];
-    const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
-    for (int i = 0; i < sd; ++i)
-        id[i].reset(new int(i));
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == id);
-    assert(*id[0] == 2);
-    assert(*id[1] == 3);
-    assert(*id[2] == 0);
-    assert(*id[3] == 1);
+  std::unique_ptr<int> id[4];
+  const int sd = static_cast<int>(sizeof(id) / sizeof(id[0]));
+  for (int i = 0; i < sd; ++i)
+    id[i].reset(new int(i));
+  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);
+  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);
+  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);
+  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));
+  assert(base(r) == id);
+  assert(*id[0] == 2);
+  assert(*id[1] == 3);
+  assert(*id[2] == 0);
+  assert(*id[3] == 1);
 
-    std::unique_ptr<int> ie[5];
-    const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
-    for (int i = 0; i < se; ++i)
-        ie[i].reset(new int(i));
-    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);
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == ie);
-    assert(*ie[0] == 0);
-    assert(*ie[1] == 1);
-    assert(*ie[2] == 2);
-    assert(*ie[3] == 3);
-    assert(*ie[4] == 4);
+  std::unique_ptr<int> ie[5];
+  const int se = static_cast<int>(sizeof(ie) / sizeof(ie[0]));
+  for (int i = 0; i < se; ++i)
+    ie[i].reset(new int(i));
+  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);
+  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);
+  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);
+  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);
+  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));
+  assert(base(r) == ie);
+  assert(*ie[0] == 0);
+  assert(*ie[1] == 1);
+  assert(*ie[2] == 2);
+  assert(*ie[3] == 3);
+  assert(*ie[4] == 4);
 
-    std::unique_ptr<int> ig[6];
-    const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
-    for (int i = 0; i < sg; ++i)
-        ig[i].reset(new int(i));
-    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);
-    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);
-    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);
-    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);
-    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);
-    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));
-    assert(base(r) == ig);
-    assert(*ig[0] == 3);
-    assert(*ig[1] == 4);
-    assert(*ig[2] == 5);
-    assert(*ig[3] == 0);
-    assert(*ig[4] == 1);
-    assert(*ig[5] == 2);
+  std::unique_ptr<int> ig[6];
+  const int sg = static_cast<int>(sizeof(ig) / sizeof(ig[0]));
+  for (int i = 0; i < sg; ++i)
+    ig[i].reset(new int(i));
+  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);
+  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);
+  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);
+  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);
+  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);
+  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));
+  assert(base(r) == ig);
+  assert(*ig[0] == 3);
+  assert(*ig[1] == 4);
+  assert(*ig[2] == 5);
+  assert(*ig[3] == 0);
+  assert(*ig[4] == 1);
+  assert(*ig[5] == 2);
 }
 
 #endif // TEST_STD_VER >= 11
 
-int main(int, char**)
-{
-    test<forward_iterator<int*> >();
-    test<bidirectional_iterator<int*> >();
-    test<random_access_iterator<int*> >();
-    test<int*>();
+template <std::size_t N>
+TEST_CONSTEXPR_CXX20 bool test_bit_iterator() {
+  for (int offset = -1; offset <= 1; ++offset) {
+    std::vector<bool> a(N, false);
+    std::size_t mid = N / 2 + offset;
+    for (std::size_t i = mid; i < N; ++i)
+      a[i] = true;
+    std::rotate(a.begin(), a.begin() + mid, a.end());
+    for (std::size_t i = 0; i < N; ++i)
+      assert(a[i] == (i < N - mid));
+  }
+  return true;
+};
+
+int main(int, char**) {
+  test<forward_iterator<int*> >();
+  test<bidirectional_iterator<int*> >();
+  test<random_access_iterator<int*> >();
+  test<int*>();
+
+  test_bit_iterator<8>();
+  test_bit_iterator<16>();
+  test_bit_iterator<64>();
+  test_bit_iterator<100>();
 
 #if TEST_STD_VER >= 11
 
-    test1<forward_iterator<std::unique_ptr<int>*> >();
-    test1<bidirectional_iterator<std::unique_ptr<int>*> >();
-    test1<random_access_iterator<std::unique_ptr<int>*> >();
-    test1<std::unique_ptr<int>*>();
+  test1<forward_iterator<std::unique_ptr<int>*> >();
+  test1<bidirectional_iterator<std::unique_ptr<int>*> >();
+  test1<random_access_iterator<std::unique_ptr<int>*> >();
+  test1<std::unique_ptr<int>*>();
 
 #endif
 
 #if TEST_STD_VER > 17
-    static_assert(test<forward_iterator<int*> >());
-    static_assert(test<bidirectional_iterator<int*> >());
-    static_assert(test<random_access_iterator<int*> >());
-    static_assert(test<int*>());
+  static_assert(test<forward_iterator<int*> >());
+  static_assert(test<bidirectional_iterator<int*> >());
+  static_assert(test<random_access_iterator<int*> >());
+  static_assert(test<int*>());
+
+  static_assert(test_bit_iterator<8>());
+  static_assert(test_bit_iterator<16>());
+  static_assert(test_bit_iterator<64>());
+  static_assert(test_bit_iterator<100>());
 #endif // TEST_STD_VER > 17
 
   return 0;



More information about the libcxx-commits mailing list