[libcxx][ranges] Add `counted_iterator`.

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

GitOrigin-RevId: 8a48e6dda9f7fb9c7ae7207dcb9570d2c11255ad
diff --git a/docs/Status/RangesPaper.csv b/docs/Status/RangesPaper.csv
index 024a5e4..a7d2897 100644
--- a/docs/Status/RangesPaper.csv
+++ b/docs/Status/RangesPaper.csv
@@ -82,7 +82,7 @@
 [counted.iterator],,"| [iterator.concepts]
 | [iterator.cust.swap]
 | [iterator.cust.move]
-| [default.sentinels]",Zoe Carver,In Progress
+| [default.sentinels]",Zoe Carver,✅
 [stream.iterators],,[default.sentinels],Unassigned,Not started
 `[range.access] <http://wg21.link/range.access>`_,"| `ranges::begin <https://llvm.org/D100255>`_
 | `ranges::end <https://llvm.org/D100255>`_
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index dd4b812..5fba760 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -135,6 +135,7 @@
   __iterator/back_insert_iterator.h
   __iterator/common_iterator.h
   __iterator/concepts.h
+  __iterator/counted_iterator.h
   __iterator/data.h
   __iterator/default_sentinel.h
   __iterator/distance.h
diff --git a/include/__iterator/counted_iterator.h b/include/__iterator/counted_iterator.h
new file mode 100644
index 0000000..7136aaf
--- /dev/null
+++ b/include/__iterator/counted_iterator.h
@@ -0,0 +1,306 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef _LIBCPP___ITERATOR_COUNTED_ITERATOR_H
+#define _LIBCPP___ITERATOR_COUNTED_ITERATOR_H
+
+#include <__config>
+#include <__debug>
+#include <__iterator/concepts.h>
+#include <__iterator/default_sentinel.h>
+#include <__iterator/iter_move.h>
+#include <__iterator/iter_swap.h>
+#include <__iterator/incrementable_traits.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/readable_traits.h>
+#include <__memory/pointer_traits.h>
+#include <concepts>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if !defined(_LIBCPP_HAS_NO_RANGES)
+
+template<class>
+struct __counted_iterator_concept {};
+
+template<class _Iter>
+  requires requires { typename _Iter::iterator_concept; }
+struct __counted_iterator_concept<_Iter> {
+  using iterator_concept = typename _Iter::iterator_concept;
+};
+
+template<class>
+struct __counted_iterator_category {};
+
+template<class _Iter>
+  requires requires { typename _Iter::iterator_category; }
+struct __counted_iterator_category<_Iter> {
+  using iterator_category = typename _Iter::iterator_category;
+};
+
+template<class>
+struct __counted_iterator_value_type {};
+
+template<indirectly_readable _Iter>
+struct __counted_iterator_value_type<_Iter> {
+  using value_type = iter_value_t<_Iter>;
+};
+
+template<input_or_output_iterator _Iter>
+class counted_iterator
+  : public __counted_iterator_concept<_Iter>
+  , public __counted_iterator_category<_Iter>
+  , public __counted_iterator_value_type<_Iter>
+{
+public:
+  [[no_unique_address]] _Iter __current_ = _Iter();
+  iter_difference_t<_Iter> __count_ = 0;
+
+  using iterator_type = _Iter;
+  using difference_type = iter_difference_t<_Iter>;
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator() requires default_initializable<_Iter> = default;
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator(_Iter __iter, iter_difference_t<_Iter> __n)
+   : __current_(_VSTD::move(__iter)), __count_(__n) {
+    _LIBCPP_ASSERT(__n >= 0, "__n must not be negative.");
+  }
+
+  template<class _I2>
+    requires convertible_to<const _I2&, _Iter>
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator(const counted_iterator<_I2>& __other)
+   : __current_(__other.__current_), __count_(__other.__count_) {}
+
+  template<class _I2>
+    requires assignable_from<_Iter&, const _I2&>
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator& operator=(const counted_iterator<_I2>& __other) {
+    __current_ = __other.__current_;
+    __count_ = __other.__count_;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr const _Iter& base() const& { return __current_; }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr _Iter base() && { return _VSTD::move(__current_); }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr iter_difference_t<_Iter> count() const noexcept { return __count_; }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr decltype(auto) operator*() {
+    _LIBCPP_ASSERT(__count_ > 0, "Iterator is equal to or past end.");
+    return *__current_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr decltype(auto) operator*() const
+    requires __dereferenceable<const _Iter>
+  {
+    _LIBCPP_ASSERT(__count_ > 0, "Iterator is equal to or past end.");
+    return *__current_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr auto operator->() const noexcept
+    requires contiguous_iterator<_Iter>
+  {
+    return _VSTD::to_address(__current_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator& operator++() {
+    _LIBCPP_ASSERT(__count_ > 0, "Iterator already at or past end.");
+    ++__current_;
+    --__count_;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  decltype(auto) operator++(int) {
+    _LIBCPP_ASSERT(__count_ > 0, "Iterator already at or past end.");
+    --__count_;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+    try { return __current_++; }
+    catch(...) { ++__count_; throw; }
+#else
+    return __current_++;
+#endif // _LIBCPP_NO_EXCEPTIONS
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator operator++(int)
+    requires forward_iterator<_Iter>
+  {
+    _LIBCPP_ASSERT(__count_ > 0, "Iterator already at or past end.");
+    counted_iterator __tmp = *this;
+    ++*this;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator& operator--()
+    requires bidirectional_iterator<_Iter>
+  {
+    --__current_;
+    ++__count_;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator operator--(int)
+    requires bidirectional_iterator<_Iter>
+  {
+    counted_iterator __tmp = *this;
+    --*this;
+    return __tmp;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator operator+(iter_difference_t<_Iter> __n) const
+    requires random_access_iterator<_Iter>
+  {
+    return counted_iterator(__current_ + __n, __count_ - __n);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr counted_iterator operator+(
+    iter_difference_t<_Iter> __n, const counted_iterator& __x)
+    requires random_access_iterator<_Iter>
+  {
+    return __x + __n;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator& operator+=(iter_difference_t<_Iter> __n)
+    requires random_access_iterator<_Iter>
+  {
+    _LIBCPP_ASSERT(__n <= __count_, "Cannot advance iterator past end.");
+    __current_ += __n;
+    __count_ -= __n;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator operator-(iter_difference_t<_Iter> __n) const
+    requires random_access_iterator<_Iter>
+  {
+    return counted_iterator(__current_ - __n, __count_ + __n);
+  }
+
+  template<common_with<_Iter> _I2>
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr iter_difference_t<_I2> operator-(
+    const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs)
+  {
+    return __rhs.__count_ - __lhs.__count_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr iter_difference_t<_Iter> operator-(
+    const counted_iterator& __lhs, default_sentinel_t)
+  {
+    return -__lhs.__count_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr iter_difference_t<_Iter> operator-(
+    default_sentinel_t, const counted_iterator& __rhs)
+  {
+    return __rhs.__count_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr counted_iterator& operator-=(iter_difference_t<_Iter> __n)
+    requires random_access_iterator<_Iter>
+  {
+    _LIBCPP_ASSERT(-__n <= __count_, "Attempt to subtract too large of a size: "
+                                     "counted_iterator would be decremented before the "
+                                     "first element of its range.");
+    __current_ -= __n;
+    __count_ += __n;
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  constexpr decltype(auto) operator[](iter_difference_t<_Iter> __n) const
+    requires random_access_iterator<_Iter>
+  {
+    _LIBCPP_ASSERT(__n < __count_, "Subscript argument must be less than size.");
+    return __current_[__n];
+  }
+
+  template<common_with<_Iter> _I2>
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr bool operator==(
+    const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs)
+  {
+    return __lhs.__count_ == __rhs.__count_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr bool operator==(
+    const counted_iterator& __lhs, default_sentinel_t)
+  {
+    return __lhs.__count_ == 0;
+  }
+
+  template<common_with<_Iter> _I2>
+  friend constexpr strong_ordering operator<=>(
+    const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs)
+  {
+    return __rhs.__count_ <=> __lhs.__count_;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr iter_rvalue_reference_t<_Iter> iter_move(const counted_iterator& __i)
+    noexcept(noexcept(ranges::iter_move(__i.__current_)))
+      requires input_iterator<_Iter>
+  {
+    _LIBCPP_ASSERT(__i.__count_ > 0, "Iterator must not be past end of range.");
+    return ranges::iter_move(__i.__current_);
+  }
+
+  template<indirectly_swappable<_Iter> _I2>
+  _LIBCPP_HIDE_FROM_ABI
+  friend constexpr void iter_swap(const counted_iterator& __x, const counted_iterator<_I2>& __y)
+    noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_)))
+  {
+    _LIBCPP_ASSERT(__x.__count_ > 0 && __y.__count_ > 0,
+                   "Iterators must not be past end of range.");
+    return ranges::iter_swap(__x.__current_, __y.__current_);
+  }
+};
+
+template<input_iterator _Iter>
+  requires same_as<_ITER_TRAITS<_Iter>, iterator_traits<_Iter>>
+struct iterator_traits<counted_iterator<_Iter>> : iterator_traits<_Iter> {
+  using pointer = conditional_t<contiguous_iterator<_Iter>,
+                                add_pointer_t<iter_reference_t<_Iter>>, void>;
+};
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ITERATOR_COUNTED_ITERATOR_H
diff --git a/include/iterator b/include/iterator
index a9f794c..e0b2520 100644
--- a/include/iterator
+++ b/include/iterator
@@ -403,6 +403,13 @@
 struct default_sentinel_t;
 inline constexpr default_sentinel_t default_sentinel{};
 
+// [iterators.counted], counted iterators
+template<input_or_output_iterator I> class counted_iterator;
+
+template<input_iterator I>
+  requires see below
+  struct iterator_traits<counted_iterator<I>>;
+
 template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t>
 class istream_iterator
     : public iterator<input_iterator_tag, T, Distance, const T*, const T&> // until C++17
@@ -574,6 +581,7 @@
 #include <__iterator/back_insert_iterator.h>
 #include <__iterator/common_iterator.h>
 #include <__iterator/concepts.h>
+#include <__iterator/counted_iterator.h>
 #include <__iterator/data.h>
 #include <__iterator/default_sentinel.h>
 #include <__iterator/distance.h>
diff --git a/include/module.modulemap b/include/module.modulemap
index 11cabad..b5781b0 100644
--- a/include/module.modulemap
+++ b/include/module.modulemap
@@ -485,6 +485,7 @@
       module back_insert_iterator  { private header "__iterator/back_insert_iterator.h" }
       module common_iterator       { private header "__iterator/common_iterator.h" }
       module concepts              { private header "__iterator/concepts.h" }
+      module counted_iterator      { private header "__iterator/counted_iterator.h" }
       module data                  { private header "__iterator/data.h" }
       module default_sentinel      { private header "__iterator/default_sentinel.h" }
       module distance              { private header "__iterator/distance.h" }
diff --git a/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp b/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp
new file mode 100644
index 0000000..a8aedc6
--- /dev/null
+++ b/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp
@@ -0,0 +1,16 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: modules-build
+
+// WARNING: This test was generated by 'generate_private_header_tests.py'
+// and should not be edited manually.
+
+// expected-error@*:* {{use of private header from outside its module: '__iterator/counted_iterator.h'}}
+#include <__iterator/counted_iterator.h>
diff --git a/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp
new file mode 100644
index 0000000..546874a
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr auto operator->() const noexcept
+//   requires contiguous_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept ArrowEnabled = requires(Iter& iter) {
+  iter.operator->();
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    for (int i = 0; i < 8; ++i, ++iter)
+      assert(iter.operator->() == buffer + i);
+
+    static_assert(noexcept(iter.operator->()));
+  }
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    assert(iter.operator->() == buffer);
+
+    static_assert(noexcept(iter.operator->()));
+  }
+
+  {
+      static_assert( ArrowEnabled<std::counted_iterator<contiguous_iterator<int*>>>);
+      static_assert(!ArrowEnabled<std::counted_iterator<random_access_iterator<int*>>>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp
new file mode 100644
index 0000000..8f9d5c4
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp
@@ -0,0 +1,129 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<class I2>
+//   requires assignable_from<I&, const I2&>
+//     constexpr counted_iterator& operator=(const counted_iterator<I2>& x);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+class AssignableFromIter
+{
+    int *it_;
+
+public:
+    typedef          std::input_iterator_tag                      iterator_category;
+    typedef int                                                   value_type;
+    typedef typename std::iterator_traits<int *>::difference_type difference_type;
+    typedef int *                                                 pointer;
+    typedef int &                                                 reference;
+
+    constexpr int *base() const {return it_;}
+
+    AssignableFromIter() = default;
+    explicit constexpr AssignableFromIter(int *it) : it_(it) {}
+    constexpr AssignableFromIter(const forward_iterator<int*>& it) : it_(it.base()) {}
+
+    constexpr AssignableFromIter& operator=(const forward_iterator<int*> &other) {
+      it_ = other.base();
+      return *this;
+    }
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr AssignableFromIter& operator++() {++it_; return *this;}
+    constexpr AssignableFromIter operator++(int)
+        {AssignableFromIter tmp(*this); ++(*this); return tmp;}
+};
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  int operator*() { return *ptr; }
+  void operator++(int) { ++ptr; }
+  InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    static_assert( std::is_assignable_v<std::counted_iterator<forward_iterator<int*>>,
+                                        std::counted_iterator<forward_iterator<int*>>>);
+    static_assert(!std::is_assignable_v<std::counted_iterator<forward_iterator<int*>>,
+                                        std::counted_iterator<random_access_iterator<int*>>>);
+  }
+
+  {
+    std::counted_iterator iter1(AssignableFromIter{buffer}, 8);
+    std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
+    assert(iter1.base().base() == buffer);
+    assert(iter1.count() == 8);
+    std::counted_iterator<AssignableFromIter>& result = (iter1 = iter2);
+    assert(&result == &iter1);
+    assert(iter1.base().base() == buffer + 2);
+    assert(iter1.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 = iter2), std::counted_iterator<AssignableFromIter>&);
+  }
+  {
+    std::counted_iterator iter1(AssignableFromIter{buffer}, 8);
+    const std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
+    assert(iter1.base().base() == buffer);
+    assert(iter1.count() == 8);
+    std::counted_iterator<AssignableFromIter>& result = (iter1 = iter2);
+    assert(&result == &iter1);
+    assert(iter1.base().base() == buffer + 2);
+    assert(iter1.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 = iter2), std::counted_iterator<AssignableFromIter>&);
+  }
+
+  {
+    std::counted_iterator iter1(InputOrOutputArchetype{buffer}, 8);
+    std::counted_iterator iter2(InputOrOutputArchetype{buffer + 2}, 6);
+    assert(iter1.base().ptr == buffer);
+    assert(iter1.count() == 8);
+    std::counted_iterator<InputOrOutputArchetype>& result = (iter1 = iter2);
+    assert(&result == &iter1);
+    assert(iter1.base().ptr == buffer + 2);
+    assert(iter1.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 = iter2), std::counted_iterator<InputOrOutputArchetype>&);
+  }
+  {
+    std::counted_iterator iter1(InputOrOutputArchetype{buffer}, 8);
+    const std::counted_iterator iter2(InputOrOutputArchetype{buffer + 2}, 6);
+    assert(iter1.base().ptr == buffer);
+    assert(iter1.count() == 8);
+    std::counted_iterator<InputOrOutputArchetype>& result = (iter1 = iter2);
+    assert(&result == &iter1);
+    assert(iter1.base().ptr == buffer + 2);
+    assert(iter1.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 = iter2), std::counted_iterator<InputOrOutputArchetype>&);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp
new file mode 100644
index 0000000..5b17ded
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp
@@ -0,0 +1,114 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr const I& base() const &;
+// constexpr I base() &&;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  int operator*() { return *ptr; }
+  void operator++(int) { ++ptr; }
+  InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter.base().base() == buffer);
+    assert(std::move(iter).base().base() == buffer);
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), cpp20_input_iterator<int*>);
+  }
+
+  {
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    assert(iter.base() == forward_iterator<int*>{buffer});
+    assert(std::move(iter).base() == forward_iterator<int*>{buffer});
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), forward_iterator<int*>);
+  }
+
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    assert(iter.base() == contiguous_iterator<int*>{buffer});
+    assert(std::move(iter).base() == contiguous_iterator<int*>{buffer});
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), contiguous_iterator<int*>);
+  }
+
+  {
+    std::counted_iterator iter(InputOrOutputArchetype{buffer}, 6);
+    assert(iter.base().ptr == buffer);
+    assert(std::move(iter).base().ptr == buffer);
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const InputOrOutputArchetype&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), InputOrOutputArchetype);
+  }
+
+  {
+    const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter.base().base() == buffer);
+    assert(std::move(iter).base().base() == buffer);
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const cpp20_input_iterator<int*>&);
+  }
+
+  {
+    const std::counted_iterator iter(forward_iterator<int*>{buffer}, 7);
+    assert(iter.base() == forward_iterator<int*>{buffer});
+    assert(std::move(iter).base() == forward_iterator<int*>{buffer});
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const forward_iterator<int*>&);
+  }
+
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 6);
+    assert(iter.base() == contiguous_iterator<int*>{buffer});
+    assert(std::move(iter).base() == contiguous_iterator<int*>{buffer});
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator<int*>&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const contiguous_iterator<int*>&);
+  }
+
+  {
+    const std::counted_iterator iter(InputOrOutputArchetype{buffer}, 6);
+    assert(iter.base().ptr == buffer);
+    assert(std::move(iter).base().ptr == buffer);
+
+    ASSERT_SAME_TYPE(decltype(iter.base()), const InputOrOutputArchetype&);
+    ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const InputOrOutputArchetype&);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp
new file mode 100644
index 0000000..1ceb93f
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp
@@ -0,0 +1,112 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<common_with<I> I2>
+//   friend constexpr bool operator==(
+//     const counted_iterator& x, const counted_iterator<I2>& y);
+// friend constexpr bool operator==(
+//   const counted_iterator& x, default_sentinel_t);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+// This iterator is common_with forward_iterator but NOT comparable with it.
+template <class It>
+class CommonWithForwardIter
+{
+    It it_;
+
+public:
+    typedef          std::input_iterator_tag                   iterator_category;
+    typedef typename std::iterator_traits<It>::value_type      value_type;
+    typedef typename std::iterator_traits<It>::difference_type difference_type;
+    typedef It                                                 pointer;
+    typedef typename std::iterator_traits<It>::reference       reference;
+
+    constexpr It base() const {return it_;}
+
+    CommonWithForwardIter() = default;
+    explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
+    constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
+    constexpr CommonWithForwardIter operator++(int)
+        {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
+};
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    {
+      std::counted_iterator iter1(forward_iterator<int*>(buffer), 8);
+      std::counted_iterator iter2(CommonWithForwardIter<int*>(buffer), 8);
+
+      assert(iter1 == iter2);
+      assert(iter2 == iter1);
+      ++iter1;
+      assert(iter1 != iter2);
+      assert(iter2 != iter1);
+    }
+  }
+
+  {
+    {
+      std::counted_iterator iter(cpp20_input_iterator<int*>(buffer), 8);
+
+      assert(iter != std::default_sentinel);
+      assert(std::default_sentinel == std::ranges::next(std::move(iter), 8));
+    }
+    {
+      std::counted_iterator iter(forward_iterator<int*>(buffer), 8);
+
+      assert(iter != std::default_sentinel);
+      assert(std::default_sentinel == std::ranges::next(iter, 8));
+    }
+    {
+      std::counted_iterator iter(random_access_iterator<int*>(buffer), 8);
+
+      assert(iter != std::default_sentinel);
+      assert(std::default_sentinel == std::ranges::next(iter, 8));
+    }
+    {
+      std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
+
+      assert(iter != std::default_sentinel);
+      assert(std::default_sentinel == std::ranges::next(iter, 8));
+    }
+  }
+
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp
new file mode 100644
index 0000000..4a89686
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr iter_difference_t<I> count() const noexcept;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    for (int i = 8; i != 0; --i, ++iter)
+      assert(iter.count() == i);
+
+    static_assert(noexcept(iter.count()));
+  }
+  {
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    for (int i = 8; i != 0; --i, ++iter)
+      assert(iter.count() == i);
+
+    static_assert(noexcept(iter.count()));
+  }
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    for (int i = 8; i != 0; --i, ++iter)
+      assert(iter.count() == i);
+  }
+  {
+    std::counted_iterator iter(InputOrOutputArchetype{buffer + 2}, 6);
+    assert(iter.count() == 6);
+  }
+
+  // Const tests.
+  {
+    const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter.count() == 8);
+  }
+  {
+    const std::counted_iterator iter(forward_iterator<int*>{buffer + 1}, 7);
+    assert(iter.count() == 7);
+  }
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter.count() == 6);
+  }
+  {
+    const std::counted_iterator iter(InputOrOutputArchetype{buffer + 2}, 6);
+    assert(iter.count() == 6);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp
new file mode 100644
index 0000000..e418a4d
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp
@@ -0,0 +1,78 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<class I2>
+//   requires convertible_to<const I2&, I>
+//     constexpr counted_iterator(const counted_iterator<I2>& x);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class T>
+class ConvertibleTo
+{
+    int *it_;
+
+public:
+    typedef          std::input_iterator_tag                      iterator_category;
+    typedef int                                                   value_type;
+    typedef typename std::iterator_traits<int *>::difference_type difference_type;
+    typedef int *                                                 pointer;
+    typedef int &                                                 reference;
+
+    constexpr int *base() const {return it_;}
+
+    ConvertibleTo() = default;
+    explicit constexpr ConvertibleTo(int *it) : it_(it) {}
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr ConvertibleTo& operator++() {++it_; return *this;}
+    constexpr ConvertibleTo operator++(int)
+        {ConvertibleTo tmp(*this); ++(*this); return tmp;}
+
+    constexpr operator T() const { return T(it_); }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    static_assert( std::is_constructible_v<std::counted_iterator<forward_iterator<int*>>,
+                                           std::counted_iterator<forward_iterator<int*>>>);
+    static_assert(!std::is_constructible_v<std::counted_iterator<forward_iterator<int*>>,
+                                           std::counted_iterator<random_access_iterator<int*>>>);
+  }
+  {
+    std::counted_iterator iter1(ConvertibleTo<forward_iterator<int*>>{buffer}, 8);
+    std::counted_iterator<forward_iterator<int*>> iter2(iter1);
+    assert(iter2.base() == forward_iterator<int*>{buffer});
+    assert(iter2.count() == 8);
+  }
+  {
+    const std::counted_iterator iter1(ConvertibleTo<forward_iterator<int*>>{buffer}, 8);
+    const std::counted_iterator<forward_iterator<int*>> iter2(iter1);
+    assert(iter2.base() == forward_iterator<int*>{buffer});
+    assert(iter2.count() == 8);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp
new file mode 100644
index 0000000..45b6d0b
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator() requires default_initializable<I> = default;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+constexpr bool test() {
+  static_assert( std::default_initializable<std::counted_iterator<cpp17_input_iterator<int*>>>);
+  static_assert(!std::default_initializable<std::counted_iterator<cpp20_input_iterator<int*>>>);
+
+  std::counted_iterator<cpp17_input_iterator<int*>> iter;
+  assert(iter.base() == cpp17_input_iterator<int*>());
+  assert(iter.count() == 0);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp
new file mode 100644
index 0000000..5ed8b68
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp
@@ -0,0 +1,93 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator() requires default_initializable<I> = default;
+// constexpr counted_iterator(I x, iter_difference_t<I> n);
+// template<class I2>
+//   requires convertible_to<const I2&, I>
+//     constexpr counted_iterator(const counted_iterator<I2>& x);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter.base().base() == buffer);
+    assert(iter.count() == 8);
+  }
+
+  {
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    assert(iter.base() == forward_iterator<int*>{buffer});
+    assert(iter.count() == 8);
+  }
+
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    assert(iter.base() == contiguous_iterator<int*>{buffer});
+    assert(iter.count() == 8);
+  }
+
+  {
+    std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
+    assert(iter.base().ptr == buffer);
+    assert(iter.count() == 8);
+  }
+
+  {
+    const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter.base().base() == buffer);
+    assert(iter.count() == 8);
+  }
+
+  {
+    const std::counted_iterator iter(forward_iterator<int*>{buffer}, 7);
+    assert(iter.base() == forward_iterator<int*>{buffer});
+    assert(iter.count() == 7);
+  }
+
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 6);
+    assert(iter.base() == contiguous_iterator<int*>{buffer});
+    assert(iter.count() == 6);
+  }
+
+  {
+    const std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
+    assert(iter.base().ptr == buffer);
+    assert(iter.count() == 8);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp
new file mode 100644
index 0000000..249bcb3
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp
@@ -0,0 +1,77 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator& operator--()
+//  requires bidirectional_iterator<I>;
+// constexpr counted_iterator operator--(int)
+//  requires bidirectional_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept MinusEnabled = requires(Iter& iter) {
+  iter--;
+  --iter;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    using Counted = std::counted_iterator<bidirectional_iterator<int*>>;
+    std::counted_iterator iter(bidirectional_iterator<int*>{buffer + 2}, 6);
+    assert(iter-- == Counted(bidirectional_iterator<int*>{buffer + 2}, 6));
+    assert(--iter == Counted(bidirectional_iterator<int*>{buffer}, 8));
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter--), Counted);
+    ASSERT_SAME_TYPE(decltype(--iter), Counted&);
+  }
+  {
+    using Counted = std::counted_iterator<random_access_iterator<int*>>;
+    Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
+    assert(iter-- == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+    assert(--iter == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter--), Counted);
+    ASSERT_SAME_TYPE(decltype(--iter), Counted&);
+  }
+  {
+    using Counted = std::counted_iterator<contiguous_iterator<int*>>;
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter-- == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
+    assert(--iter == Counted(contiguous_iterator<int*>{buffer}, 8));
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter--), Counted);
+    ASSERT_SAME_TYPE(decltype(--iter), Counted&);
+  }
+
+  {
+    static_assert( MinusEnabled<std::counted_iterator<contiguous_iterator<int*>>>);
+    static_assert(!MinusEnabled<const std::counted_iterator<contiguous_iterator<int*>>>);
+    static_assert(!MinusEnabled<std::counted_iterator<forward_iterator<int*>>>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp
new file mode 100644
index 0000000..5659129
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp
@@ -0,0 +1,109 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr decltype(auto) operator*();
+// constexpr decltype(auto) operator*() const
+//   requires dereferenceable<const I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() const { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+struct NonConstDeref {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr NonConstDeref& operator++() { ++ptr; return *this; }
+};
+
+template<class T>
+concept IsDereferenceable = requires(T& i) {
+  *i;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    static_assert( IsDereferenceable<std::counted_iterator<InputOrOutputArchetype>>);
+    static_assert( IsDereferenceable<const std::counted_iterator<InputOrOutputArchetype>>);
+    static_assert( IsDereferenceable<std::counted_iterator<NonConstDeref>>);
+    static_assert(!IsDereferenceable<const std::counted_iterator<NonConstDeref>>);
+  }
+
+  {
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    for (int i = 1; i < 9; ++i, ++iter)
+      assert(*iter == i);
+  }
+
+  {
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    for (int i = 1; i < 9; ++i, ++iter)
+      assert(*iter == i);
+  }
+
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    for (int i = 1; i < 9; ++i, ++iter)
+      assert(*iter == i);
+  }
+
+  {
+    std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
+    for (int i = 1; i < 9; ++i, ++iter)
+      assert(*iter == i);
+  }
+
+  {
+    const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(*iter == 1);
+  }
+
+  {
+    const std::counted_iterator iter(forward_iterator<int*>{buffer + 1}, 7);
+    assert(*iter == 2);
+  }
+
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(*iter == 3);
+  }
+
+  {
+    const std::counted_iterator iter(InputOrOutputArchetype{buffer + 2}, 6);
+    assert(*iter == 3);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/increment.cpp b/test/std/iterators/predef.iterators/counted.iterator/increment.cpp
new file mode 100644
index 0000000..3feee86
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/increment.cpp
@@ -0,0 +1,140 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator& operator++();
+// decltype(auto) operator++(int);
+// constexpr counted_iterator operator++(int)
+//   requires forward_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+#ifndef TEST_HAS_NO_EXCEPTIONS
+template <class It>
+class ThrowsOnInc
+{
+    It it_;
+
+public:
+    typedef          std::input_iterator_tag                   iterator_category;
+    typedef typename std::iterator_traits<It>::value_type      value_type;
+    typedef typename std::iterator_traits<It>::difference_type difference_type;
+    typedef It                                                 pointer;
+    typedef typename std::iterator_traits<It>::reference       reference;
+
+    constexpr It base() const {return it_;}
+
+    ThrowsOnInc() = default;
+    explicit constexpr ThrowsOnInc(It it) : it_(it) {}
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr ThrowsOnInc& operator++() {throw 42;}
+    constexpr ThrowsOnInc operator++(int) {throw 42;}
+};
+#endif // TEST_HAS_NO_EXCEPTIONS
+
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int *ptr;
+
+  constexpr int operator*() const { return *ptr; }
+  constexpr void operator++(int) { ++ptr; }
+  constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
+};
+
+template<class Iter>
+concept PlusEnabled = requires(Iter& iter) {
+  iter++;
+  ++iter;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    using Counted = std::counted_iterator<forward_iterator<int*>>;
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+
+    assert(iter++ == Counted(forward_iterator<int*>{buffer}, 8));
+    assert(++iter == Counted(forward_iterator<int*>{buffer + 2}, 6));
+
+    ASSERT_SAME_TYPE(decltype(iter++), Counted);
+    ASSERT_SAME_TYPE(decltype(++iter), Counted&);
+  }
+  {
+    using Counted = std::counted_iterator<random_access_iterator<int*>>;
+    std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+
+    assert(iter++ == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert(++iter == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+
+    ASSERT_SAME_TYPE(decltype(iter++), Counted);
+    ASSERT_SAME_TYPE(decltype(++iter), Counted&);
+  }
+
+  {
+    static_assert( PlusEnabled<      std::counted_iterator<random_access_iterator<int*>>>);
+    static_assert(!PlusEnabled<const std::counted_iterator<random_access_iterator<int*>>>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    using Counted = std::counted_iterator<InputOrOutputArchetype>;
+    std::counted_iterator iter(InputOrOutputArchetype{buffer}, 8);
+
+    iter++;
+    assert((++iter).base().ptr == buffer + 2);
+
+    ASSERT_SAME_TYPE(decltype(iter++), void);
+    ASSERT_SAME_TYPE(decltype(++iter), Counted&);
+  }
+  {
+    using Counted = std::counted_iterator<cpp20_input_iterator<int*>>;
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+
+    iter++;
+    assert(++iter == Counted(cpp20_input_iterator<int*>{buffer + 2}, 6));
+
+    ASSERT_SAME_TYPE(decltype(iter++), void);
+    ASSERT_SAME_TYPE(decltype(++iter), Counted&);
+  }
+#ifndef TEST_HAS_NO_EXCEPTIONS
+  {
+    using Counted = std::counted_iterator<ThrowsOnInc<int*>>;
+    std::counted_iterator iter(ThrowsOnInc<int*>{buffer}, 8);
+    try {
+      (void)iter++;
+      assert(false);
+    } catch (int x) {
+      assert(x == 42);
+      assert(iter.count() == 8);
+    }
+
+    ASSERT_SAME_TYPE(decltype(iter++), ThrowsOnInc<int*>);
+    ASSERT_SAME_TYPE(decltype(++iter), Counted&);
+  }
+#endif // TEST_HAS_NO_EXCEPTIONS
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp
new file mode 100644
index 0000000..dcb685b
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// friend constexpr iter_rvalue_reference_t<I>
+//   iter_move(const counted_iterator& i)
+//     noexcept(noexcept(ranges::iter_move(i.current)))
+//     requires input_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<bool IsNoexcept>
+class HasNoexceptIterMove
+{
+  int *it_;
+
+public:
+  typedef          std::input_iterator_tag                      iterator_category;
+  typedef int                                                   value_type;
+  typedef typename std::iterator_traits<int *>::difference_type difference_type;
+  typedef int *                                                 pointer;
+  typedef int &                                                 reference;
+
+  constexpr int *base() const {return it_;}
+
+  HasNoexceptIterMove() = default;
+  explicit constexpr HasNoexceptIterMove(int *it) : it_(it) {}
+
+  constexpr reference operator*() const noexcept(IsNoexcept) { return *it_; }
+
+  constexpr HasNoexceptIterMove& operator++() {++it_; return *this;}
+  constexpr HasNoexceptIterMove operator++(int)
+      {HasNoexceptIterMove tmp(*this); ++(*this); return tmp;}
+};
+
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    auto iter1 = cpp17_input_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    assert(std::ranges::iter_move(commonIter1) == 1);
+    ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(commonIter1)), int&&);
+  }
+  {
+    auto iter1 = forward_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    assert(std::ranges::iter_move(commonIter1) == 1);
+    ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(commonIter1)), int&&);
+  }
+  {
+    auto iter1 = random_access_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    assert(std::ranges::iter_move(commonIter1) == 1);
+    ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(commonIter1)), int&&);
+  }
+
+  // Test noexceptness.
+  {
+    static_assert( noexcept(std::ranges::iter_move(std::declval<std::counted_iterator<HasNoexceptIterMove<true>>>())));
+    static_assert(!noexcept(std::ranges::iter_move(std::declval<std::counted_iterator<HasNoexceptIterMove<false>>>())));
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp
new file mode 100644
index 0000000..b062072
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp
@@ -0,0 +1,107 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<indirectly_swappable<I> I2>
+//   friend constexpr void
+//     iter_swap(const counted_iterator& x, const counted_iterator<I2>& y)
+//       noexcept(noexcept(ranges::iter_swap(x.current, y.current)));
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<bool IsNoexcept>
+class HasNoexceptIterSwap
+{
+  int *it_;
+
+public:
+  typedef          std::input_iterator_tag                      iterator_category;
+  typedef int                                                   value_type;
+  typedef typename std::iterator_traits<int *>::difference_type difference_type;
+  typedef int *                                                 pointer;
+  typedef int &                                                 reference;
+
+  constexpr int *base() const {return it_;}
+
+  HasNoexceptIterSwap() = default;
+  explicit constexpr HasNoexceptIterSwap(int *it) : it_(it) {}
+
+  constexpr reference operator*() const {return *it_;}
+
+  constexpr HasNoexceptIterSwap& operator++() {++it_; return *this;}
+  constexpr HasNoexceptIterSwap operator++(int)
+      {HasNoexceptIterSwap tmp(*this); ++(*this); return tmp;}
+
+  friend void iter_swap(
+    const HasNoexceptIterSwap&, const HasNoexceptIterSwap&) noexcept(IsNoexcept) {}
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    auto iter1 = cpp17_input_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    auto commonIter2 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    for (auto i = 0; i < 4; ++i) ++commonIter2;
+    assert(*commonIter2 == 5);
+    std::ranges::iter_swap(commonIter1, commonIter2);
+    assert(*commonIter1 == 5);
+    assert(*commonIter2 == 1);
+    std::ranges::iter_swap(commonIter2, commonIter1);
+  }
+  {
+    auto iter1 = forward_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    auto commonIter2 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    for (auto i = 0; i < 4; ++i) ++commonIter2;
+    assert(*commonIter2 == 5);
+    std::ranges::iter_swap(commonIter1, commonIter2);
+    assert(*commonIter1 == 5);
+    assert(*commonIter2 == 1);
+    std::ranges::iter_swap(commonIter2, commonIter1);
+  }
+  {
+    auto iter1 = random_access_iterator<int*>(buffer);
+    auto commonIter1 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    auto commonIter2 = std::counted_iterator<decltype(iter1)>(iter1, 8);
+    for (auto i = 0; i < 4; ++i) ++commonIter2;
+    assert(*commonIter2 == 5);
+    std::ranges::iter_swap(commonIter1, commonIter2);
+    assert(*commonIter1 == 5);
+    assert(*commonIter2 == 1);
+    std::ranges::iter_swap(commonIter2, commonIter1);
+  }
+
+  // Test noexceptness.
+  {
+    static_assert( noexcept(std::ranges::iter_swap(
+      std::declval<std::counted_iterator<HasNoexceptIterSwap<true>>&>(),
+      std::declval<std::counted_iterator<HasNoexceptIterSwap<true>>&>()
+    )));
+    static_assert(!noexcept(std::ranges::iter_swap(
+      std::declval<std::counted_iterator<HasNoexceptIterSwap<false>>&>(),
+      std::declval<std::counted_iterator<HasNoexceptIterSwap<false>>&>()
+    )));
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp
new file mode 100644
index 0000000..9cd71b2
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// Iterator conformance tests for counted_iterator.
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+void test() {
+  static_assert(std::input_iterator<std::counted_iterator<cpp17_input_iterator<int*>>>);
+  static_assert(std::forward_iterator<std::counted_iterator<forward_iterator<int*>>>);
+  static_assert(std::bidirectional_iterator<std::counted_iterator<random_access_iterator<int*>>>);
+  static_assert(std::bidirectional_iterator<std::counted_iterator<contiguous_iterator<int*>>>);
+  static_assert(std::random_access_iterator<std::counted_iterator<random_access_iterator<int*>>>);
+  static_assert(std::contiguous_iterator<std::counted_iterator<contiguous_iterator<int*>>>);
+
+  using Iter = std::counted_iterator<forward_iterator<int*>>;
+  static_assert(std::indirectly_writable<Iter, int>);
+  static_assert(std::indirectly_swappable<Iter, Iter>);
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp
new file mode 100644
index 0000000..fe06b51
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<input_iterator I>
+//   requires same_as<ITER_TRAITS(I), iterator_traits<I>>   // see [iterator.concepts.general]
+// struct iterator_traits<counted_iterator<I>> : iterator_traits<I> {
+//   using pointer = conditional_t<contiguous_iterator<I>,
+//                                 add_pointer_t<iter_reference_t<I>>, void>;
+// };
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+void test() {
+  {
+    using Iter = cpp17_input_iterator<int*>;
+    using CommonIter = std::counted_iterator<Iter>;
+    using IterTraits = std::iterator_traits<CommonIter>;
+
+    static_assert(std::same_as<IterTraits::iterator_category, std::input_iterator_tag>);
+    static_assert(std::same_as<IterTraits::value_type, int>);
+    static_assert(std::same_as<IterTraits::difference_type, std::ptrdiff_t>);
+    static_assert(std::same_as<IterTraits::pointer, void>);
+    static_assert(std::same_as<IterTraits::reference, int&>);
+  }
+  {
+    using Iter = forward_iterator<int*>;
+    using CommonIter = std::counted_iterator<Iter>;
+    using IterTraits = std::iterator_traits<CommonIter>;
+
+    static_assert(std::same_as<IterTraits::iterator_category, std::forward_iterator_tag>);
+    static_assert(std::same_as<IterTraits::value_type, int>);
+    static_assert(std::same_as<IterTraits::difference_type, std::ptrdiff_t>);
+    static_assert(std::same_as<IterTraits::pointer, void>);
+    static_assert(std::same_as<IterTraits::reference, int&>);
+  }
+  {
+    using Iter = random_access_iterator<int*>;
+    using CommonIter = std::counted_iterator<Iter>;
+    using IterTraits = std::iterator_traits<CommonIter>;
+
+    static_assert(std::same_as<IterTraits::iterator_category, std::random_access_iterator_tag>);
+    static_assert(std::same_as<IterTraits::value_type, int>);
+    static_assert(std::same_as<IterTraits::difference_type, std::ptrdiff_t>);
+    static_assert(std::same_as<IterTraits::pointer, void>);
+    static_assert(std::same_as<IterTraits::reference, int&>);
+  }
+  {
+    using Iter = contiguous_iterator<int*>;
+    using CommonIter = std::counted_iterator<Iter>;
+    using IterTraits = std::iterator_traits<CommonIter>;
+
+    static_assert(std::same_as<IterTraits::iterator_category, std::contiguous_iterator_tag>);
+    static_assert(std::same_as<IterTraits::value_type, int>);
+    static_assert(std::same_as<IterTraits::difference_type, std::ptrdiff_t>);
+    static_assert(std::same_as<IterTraits::pointer, int*>);
+    static_assert(std::same_as<IterTraits::reference, int&>);
+  }
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp
new file mode 100644
index 0000000..534a94a
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// iterator_type, value_type, difference_type, iterator_concept, iterator_category
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+// No value_type.
+struct InputOrOutputArchetype {
+  using difference_type = int;
+
+  int operator*();
+  void operator++(int);
+  InputOrOutputArchetype& operator++();
+};
+
+template<class T>
+concept HasValueType = requires { typename T::value_type; };
+
+template<class T>
+concept HasIteratorConcept = requires { typename T::iterator_concept; };
+
+template<class T>
+concept HasIteratorCategory = requires { typename T::iterator_category; };
+
+void test() {
+  {
+    using Iter = std::counted_iterator<InputOrOutputArchetype>;
+    static_assert(std::same_as<Iter::iterator_type, InputOrOutputArchetype>);
+    static_assert(!HasValueType<Iter>);
+    static_assert(std::same_as<Iter::difference_type, int>);
+    static_assert(!HasIteratorConcept<Iter>);
+    static_assert(!HasIteratorCategory<Iter>);
+  }
+  {
+    using Iter = std::counted_iterator<cpp20_input_iterator<int*>>;
+    static_assert(std::same_as<Iter::iterator_type, cpp20_input_iterator<int*>>);
+    static_assert(std::same_as<Iter::value_type, int>);
+    static_assert(std::same_as<Iter::difference_type, std::ptrdiff_t>);
+    static_assert(std::same_as<Iter::iterator_concept, std::input_iterator_tag>);
+    static_assert(!HasIteratorCategory<Iter>);
+  }
+  {
+    using Iter = std::counted_iterator<random_access_iterator<int*>>;
+    static_assert(std::same_as<Iter::iterator_type, random_access_iterator<int*>>);
+    static_assert(std::same_as<Iter::value_type, int>);
+    static_assert(std::same_as<Iter::difference_type, std::ptrdiff_t>);
+    static_assert(!HasIteratorConcept<Iter>);
+    static_assert(std::same_as<Iter::iterator_category, std::random_access_iterator_tag>);
+  }
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp
new file mode 100644
index 0000000..0bc85f6
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp
@@ -0,0 +1,89 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// friend constexpr iter_difference_t<I> operator-(
+//   const counted_iterator& x, default_sentinel_t);
+// friend constexpr iter_difference_t<I> operator-(
+//   default_sentinel_t, const counted_iterator& y);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+  {
+    std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+  {
+    std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
+    assert(iter - std::default_sentinel == -8);
+    assert(std::default_sentinel - iter == 8);
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter - std::default_sentinel), std::iter_difference_t<int*>);
+    ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t<int*>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp
new file mode 100644
index 0000000..b6ca1e1
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator& operator-=(iter_difference_t<I> n)
+//   requires random_access_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept MinusEqEnabled = requires(Iter& iter) {
+  iter -= 1;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    using Counted = std::counted_iterator<random_access_iterator<int*>>;
+    Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
+    assert((iter -= 2) == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert((iter -= 0) == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&);
+  }
+  {
+    using Counted = std::counted_iterator<contiguous_iterator<int*>>;
+    Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert((iter -= 2) == Counted(contiguous_iterator<int*>{buffer}, 8));
+    assert((iter -= 0) == Counted(contiguous_iterator<int*>{buffer}, 8));
+    assert(iter.count() == 8);
+
+    ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&);
+  }
+  {
+    static_assert( MinusEqEnabled<      std::counted_iterator<random_access_iterator<int*>>>);
+    static_assert(!MinusEqEnabled<const std::counted_iterator<random_access_iterator<int*>>>);
+  }
+
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp
new file mode 100644
index 0000000..e425ec7
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp
@@ -0,0 +1,123 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<common_with<I> I2>
+//   friend constexpr iter_difference_t<I2> operator-(
+//     const counted_iterator& x, const counted_iterator<I2>& y);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+// This iterator is common_with forward_iterator but NOT comparable with it.
+template <class It>
+class CommonWithForwardIter
+{
+    It it_;
+
+public:
+    typedef          std::input_iterator_tag                   iterator_category;
+    typedef typename std::iterator_traits<It>::value_type      value_type;
+    typedef typename std::iterator_traits<It>::difference_type difference_type;
+    typedef It                                                 pointer;
+    typedef typename std::iterator_traits<It>::reference       reference;
+
+    constexpr It base() const {return it_;}
+
+    CommonWithForwardIter() = default;
+    explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
+    constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
+    constexpr CommonWithForwardIter operator++(int)
+        {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
+    std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
+    assert(iter1 - iter2 == -4);
+    assert(iter2 - iter1 == 4);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 4);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
+    const std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
+    assert(iter1 - iter2 == -4);
+    assert(iter2 - iter1 == 4);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 4);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+  {
+    std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
+    std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter1 - iter2 == -2);
+    assert(iter2 - iter1 == 2);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
+    const std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter1 - iter2 == -2);
+    assert(iter2 - iter1 == 2);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+  // The minus operator works even if Iter is not a random_access_iterator because
+  // counted_iterator is able to implement it by subtracting the counts rather than
+  // the underlying iterators.
+  {
+    std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
+    std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
+    assert(iter1 - iter2 == -2);
+    assert(iter2 - iter1 == 2);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+  {
+    const std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
+    const std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
+    assert(iter1 - iter2 == -2);
+    assert(iter2 - iter1 == 2);
+    assert(iter1.count() == 8);
+    assert(iter2.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp
new file mode 100644
index 0000000..5b72e2e
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator operator-(iter_difference_t<I> n) const
+//   requires random_access_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept MinusEnabled = requires(Iter& iter) {
+  iter - 1;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    using Counted = std::counted_iterator<random_access_iterator<int*>>;
+    Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
+    assert(iter - 2 == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert(iter - 0 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+    assert(iter.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
+  }
+  {
+    using Counted = std::counted_iterator<random_access_iterator<int*>>;
+    const Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
+    assert(iter - 2 == Counted(random_access_iterator<int*>{buffer}, 8));
+    assert(iter - 0 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+    assert(iter.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
+  }
+  {
+    using Counted = std::counted_iterator<contiguous_iterator<int*>>;
+    Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter - 2 == Counted(contiguous_iterator<int*>{buffer}, 8));
+    assert(iter - 0 == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
+    assert(iter.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
+  }
+  {
+    using Counted = std::counted_iterator<contiguous_iterator<int*>>;
+    const Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
+    assert(iter - 2 == Counted(contiguous_iterator<int*>{buffer}, 8));
+    assert(iter - 0 == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
+    assert(iter.count() == 6);
+
+    ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
+  }
+
+  {
+    static_assert( MinusEnabled<std::counted_iterator<random_access_iterator<int*>>>);
+    static_assert(!MinusEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp
new file mode 100644
index 0000000..36924a4
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp
@@ -0,0 +1,111 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr counted_iterator operator+(iter_difference_t<I> n) const
+//     requires random_access_iterator<I>;
+// friend constexpr counted_iterator operator+(
+//   iter_difference_t<I> n, const counted_iterator& x)
+//     requires random_access_iterator<I>;
+// constexpr counted_iterator& operator+=(iter_difference_t<I> n)
+//     requires random_access_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept PlusEnabled = requires(Iter& iter) {
+  iter + 1;
+};
+
+template<class Iter>
+concept PlusEqEnabled = requires(Iter& iter) {
+  iter += 1;
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    {
+      using Counted = std::counted_iterator<random_access_iterator<int*>>;
+      std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+      assert(iter + 2 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+      assert(iter + 0 == Counted(random_access_iterator<int*>{buffer}, 8));
+
+      ASSERT_SAME_TYPE(decltype(iter + 2), Counted);
+    }
+    {
+      using Counted = const std::counted_iterator<random_access_iterator<int*>>;
+      const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+      assert(iter + 8 == Counted(random_access_iterator<int*>{buffer + 8}, 0));
+      assert(iter + 0 == Counted(random_access_iterator<int*>{buffer}, 8));
+
+      ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t<Counted>);
+    }
+  }
+
+  {
+    {
+      using Counted = std::counted_iterator<random_access_iterator<int*>>;
+      std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+      assert(2 + iter == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+      assert(0 + iter == Counted(random_access_iterator<int*>{buffer}, 8));
+
+      ASSERT_SAME_TYPE(decltype(iter + 2), Counted);
+    }
+    {
+      using Counted = const std::counted_iterator<random_access_iterator<int*>>;
+      const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+      assert(8 + iter == Counted(random_access_iterator<int*>{buffer + 8}, 0));
+      assert(0 + iter == Counted(random_access_iterator<int*>{buffer}, 8));
+
+      ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t<Counted>);
+    }
+  }
+
+  {
+    {
+      using Counted = std::counted_iterator<random_access_iterator<int*>>;
+      std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+      assert((iter += 2) == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+      assert((iter += 0) == Counted(random_access_iterator<int*>{buffer + 2}, 6));
+
+      ASSERT_SAME_TYPE(decltype(iter += 2), Counted&);
+    }
+    {
+      using Counted = std::counted_iterator<contiguous_iterator<int*>>;
+      std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+      assert((iter += 8) == Counted(contiguous_iterator<int*>{buffer + 8}, 0));
+      assert((iter += 0) == Counted(contiguous_iterator<int*>{buffer + 8}, 0));
+
+      ASSERT_SAME_TYPE(decltype(iter += 2), Counted&);
+    }
+    {
+      static_assert( PlusEnabled<std::counted_iterator<random_access_iterator<int*>>>);
+      static_assert(!PlusEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
+
+      static_assert( PlusEqEnabled<      std::counted_iterator<random_access_iterator<int*>>>);
+      static_assert(!PlusEqEnabled<const std::counted_iterator<random_access_iterator<int*>>>);
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp
new file mode 100644
index 0000000..0259f3e
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp
@@ -0,0 +1,62 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr decltype(auto) operator[](iter_difference_t<I> n) const
+//   requires random_access_iterator<I>;
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+template<class Iter>
+concept SubscriptEnabled = requires(Iter& iter) {
+  iter[1];
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+    for (int i = 1; i < 9; ++i)
+      assert(iter[i - 1] == i);
+  }
+  {
+    std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    for (int i = 1; i < 9; ++i)
+      assert(iter[i - 1] == i);
+  }
+
+  {
+    const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8);
+    assert(iter[0] == 1);
+  }
+  {
+    const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
+    assert(iter[7] == 8);
+  }
+
+  {
+      static_assert( SubscriptEnabled<std::counted_iterator<random_access_iterator<int*>>>);
+      static_assert(!SubscriptEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp b/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp
new file mode 100644
index 0000000..900f698
--- /dev/null
+++ b/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp
@@ -0,0 +1,87 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<common_with<I> I2>
+//   friend constexpr strong_ordering operator<=>(
+//     const counted_iterator& x, const counted_iterator<I2>& y);
+
+#include <iterator>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+// This iterator is common_with forward_iterator but NOT comparable with it.
+template <class It>
+class CommonWithForwardIter
+{
+    It it_;
+
+public:
+    typedef          std::input_iterator_tag                   iterator_category;
+    typedef typename std::iterator_traits<It>::value_type      value_type;
+    typedef typename std::iterator_traits<It>::difference_type difference_type;
+    typedef It                                                 pointer;
+    typedef typename std::iterator_traits<It>::reference       reference;
+
+    constexpr It base() const {return it_;}
+
+    CommonWithForwardIter() = default;
+    explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
+    constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
+
+    constexpr reference operator*() const {return *it_;}
+
+    constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
+    constexpr CommonWithForwardIter operator++(int)
+        {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
+};
+
+constexpr bool test() {
+  int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  auto& Eq = std::strong_ordering::equal;
+  auto& Less = std::strong_ordering::less;
+  auto& Greater = std::strong_ordering::greater;
+
+  {
+    {
+      std::counted_iterator iter1(forward_iterator<int*>(buffer), 8);
+      std::counted_iterator iter2(CommonWithForwardIter<int*>(buffer), 8);
+
+      assert((iter1 <=> iter2) == Eq);
+      assert((iter2 <=> iter1) == Eq);
+      ++iter1;
+      assert((iter1 <=> iter2) == Greater);
+      assert((iter2 <=> iter1) == Less);
+    }
+    {
+      std::counted_iterator iter1(forward_iterator<int*>(buffer), 8);
+      std::counted_iterator iter2(forward_iterator<int*>(buffer), 8);
+
+      assert((iter1 <=> iter2) == Eq);
+      assert((iter2 <=> iter1) == Eq);
+      ++iter1;
+      assert((iter1 <=> iter2) == Greater);
+      assert((iter2 <=> iter1) == Less);
+    }
+  }
+
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}