From 8a48e6dda9f7fb9c7ae7207dcb9570d2c11255ad Mon Sep 17 00:00:00 2001 From: zoecarver Date: Tue, 13 Jul 2021 11:06:10 -0700 Subject: [PATCH] [libcxx][ranges] Add `counted_iterator`. Differential Revision: https://reviews.llvm.org/D106205 --- libcxx/docs/Status/RangesPaper.csv | 2 +- libcxx/include/CMakeLists.txt | 1 + libcxx/include/__iterator/counted_iterator.h | 306 ++++++++++++++++++ libcxx/include/iterator | 8 + libcxx/include/module.modulemap | 1 + .../counted_iterator.module.verify.cpp | 16 + .../counted.iterator/arrow.pass.cpp | 56 ++++ .../counted.iterator/assign.pass.cpp | 129 ++++++++ .../counted.iterator/base.pass.cpp | 114 +++++++ .../counted.iterator/compare.pass.cpp | 112 +++++++ .../counted.iterator/count.pass.cpp | 83 +++++ .../counted.iterator/ctor.conv.pass.cpp | 78 +++++ .../counted.iterator/ctor.default.pass.cpp | 36 +++ .../counted.iterator/ctor.iter.pass.cpp | 93 ++++++ .../counted.iterator/decrement.pass.cpp | 77 +++++ .../counted.iterator/deref.pass.cpp | 109 +++++++ .../counted.iterator/increment.cpp | 140 ++++++++ .../counted.iterator/iter_move.pass.cpp | 84 +++++ .../counted.iterator/iter_swap.pass.cpp | 107 ++++++ ...rator_concept_conformance.compile.pass.cpp | 31 ++ .../iterator_traits.compile.pass.cpp | 70 ++++ .../member_types.compile.pass.cpp | 63 ++++ .../minus.default_sentinel.pass.cpp | 89 +++++ .../counted.iterator/minus.eq.pass.cpp | 61 ++++ .../counted.iterator/minus.iter.pass.cpp | 123 +++++++ .../counted.iterator/minus.size.pass.cpp | 79 +++++ .../counted.iterator/plus.pass.cpp | 111 +++++++ .../counted.iterator/subscript.pass.cpp | 62 ++++ .../three_way_compare.pass.cpp | 87 +++++ 29 files changed, 2327 insertions(+), 1 deletion(-) create mode 100644 libcxx/include/__iterator/counted_iterator.h create mode 100644 libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/increment.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp create mode 100644 libcxx/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp diff --git a/libcxx/docs/Status/RangesPaper.csv b/libcxx/docs/Status/RangesPaper.csv index 024a5e4462be..a7d289706691 100644 --- a/libcxx/docs/Status/RangesPaper.csv +++ b/libcxx/docs/Status/RangesPaper.csv @@ -82,7 +82,7 @@ Section,Description,Dependencies,Assignee,Complete [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] `_,"| `ranges::begin `_ | `ranges::end `_ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index dd4b812e639d..5fba7607756e 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -135,6 +135,7 @@ set(files __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/libcxx/include/__iterator/counted_iterator.h b/libcxx/include/__iterator/counted_iterator.h new file mode 100644 index 000000000000..7136aaf02584 --- /dev/null +++ b/libcxx/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 +#include + +#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 +struct __counted_iterator_concept {}; + +template + requires requires { typename _Iter::iterator_concept; } +struct __counted_iterator_concept<_Iter> { + using iterator_concept = typename _Iter::iterator_concept; +}; + +template +struct __counted_iterator_category {}; + +template + requires requires { typename _Iter::iterator_category; } +struct __counted_iterator_category<_Iter> { + using iterator_category = typename _Iter::iterator_category; +}; + +template +struct __counted_iterator_value_type {}; + +template +struct __counted_iterator_value_type<_Iter> { + using value_type = iter_value_t<_Iter>; +}; + +template +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 + requires convertible_to + _LIBCPP_HIDE_FROM_ABI + constexpr counted_iterator(const counted_iterator<_I2>& __other) + : __current_(__other.__current_), __count_(__other.__count_) {} + + template + 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 + { + _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 _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 _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 _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 _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 + requires same_as<_ITER_TRAITS<_Iter>, iterator_traits<_Iter>> +struct iterator_traits> : iterator_traits<_Iter> { + using pointer = conditional_t, + add_pointer_t>, void>; +}; + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ITERATOR_COUNTED_ITERATOR_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator index a9f794c7e2bf..e0b25200bd9d 100644 --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -403,6 +403,13 @@ constexpr move_iterator make_move_iterator(const Iterator& i); struct default_sentinel_t; inline constexpr default_sentinel_t default_sentinel{}; +// [iterators.counted], counted iterators +template class counted_iterator; + +template + requires see below + struct iterator_traits>; + template , class Distance = ptrdiff_t> class istream_iterator : public iterator // until C++17 @@ -574,6 +581,7 @@ template constexpr const E* data(initializer_list il) noexcept; #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/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index 11cabad8cae5..b5781b027f96 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -485,6 +485,7 @@ module std [system] { 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/libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/iterator/counted_iterator.module.verify.cpp new file mode 100644 index 000000000000..a8aedc699908 --- /dev/null +++ b/libcxx/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/libcxx/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/arrow.pass.cpp new file mode 100644 index 000000000000..546874a7bb46 --- /dev/null +++ b/libcxx/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; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +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{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{buffer}, 8); + assert(iter.operator->() == buffer); + + static_assert(noexcept(iter.operator->())); + } + + { + static_assert( ArrowEnabled>>); + static_assert(!ArrowEnabled>>); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp new file mode 100644 index 000000000000..8f9d5c4fcb9b --- /dev/null +++ b/libcxx/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 +// requires assignable_from +// constexpr counted_iterator& operator=(const counted_iterator& x); + +#include + +#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::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& it) : it_(it.base()) {} + + constexpr AssignableFromIter& operator=(const forward_iterator &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>>); + static_assert(!std::is_assignable_v>, + std::counted_iterator>>); + } + + { + std::counted_iterator iter1(AssignableFromIter{buffer}, 8); + std::counted_iterator iter2(forward_iterator{buffer + 2}, 6); + assert(iter1.base().base() == buffer); + assert(iter1.count() == 8); + std::counted_iterator& 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&); + } + { + std::counted_iterator iter1(AssignableFromIter{buffer}, 8); + const std::counted_iterator iter2(forward_iterator{buffer + 2}, 6); + assert(iter1.base().base() == buffer); + assert(iter1.count() == 8); + std::counted_iterator& 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&); + } + + { + 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& 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&); + } + { + 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& 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&); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp new file mode 100644 index 000000000000..5b17ded96dae --- /dev/null +++ b/libcxx/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 + +#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{buffer}, 8); + assert(iter.base().base() == buffer); + assert(std::move(iter).base().base() == buffer); + + ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), cpp20_input_iterator); + } + + { + std::counted_iterator iter(forward_iterator{buffer}, 8); + assert(iter.base() == forward_iterator{buffer}); + assert(std::move(iter).base() == forward_iterator{buffer}); + + ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), forward_iterator); + } + + { + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + assert(iter.base() == contiguous_iterator{buffer}); + assert(std::move(iter).base() == contiguous_iterator{buffer}); + + ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), contiguous_iterator); + } + + { + 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{buffer}, 8); + assert(iter.base().base() == buffer); + assert(std::move(iter).base().base() == buffer); + + ASSERT_SAME_TYPE(decltype(iter.base()), const cpp20_input_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const cpp20_input_iterator&); + } + + { + const std::counted_iterator iter(forward_iterator{buffer}, 7); + assert(iter.base() == forward_iterator{buffer}); + assert(std::move(iter).base() == forward_iterator{buffer}); + + ASSERT_SAME_TYPE(decltype(iter.base()), const forward_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const forward_iterator&); + } + + { + const std::counted_iterator iter(contiguous_iterator{buffer}, 6); + assert(iter.base() == contiguous_iterator{buffer}); + assert(std::move(iter).base() == contiguous_iterator{buffer}); + + ASSERT_SAME_TYPE(decltype(iter.base()), const contiguous_iterator&); + ASSERT_SAME_TYPE(decltype(std::move(iter).base()), const contiguous_iterator&); + } + + { + 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/libcxx/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp new file mode 100644 index 000000000000..1ceb93f8e4dc --- /dev/null +++ b/libcxx/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 I2> +// friend constexpr bool operator==( +// const counted_iterator& x, const counted_iterator& y); +// friend constexpr bool operator==( +// const counted_iterator& x, default_sentinel_t); + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +// This iterator is common_with forward_iterator but NOT comparable with it. +template +class CommonWithForwardIter +{ + It it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::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.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(buffer), 8); + std::counted_iterator iter2(CommonWithForwardIter(buffer), 8); + + assert(iter1 == iter2); + assert(iter2 == iter1); + ++iter1; + assert(iter1 != iter2); + assert(iter2 != iter1); + } + } + + { + { + std::counted_iterator iter(cpp20_input_iterator(buffer), 8); + + assert(iter != std::default_sentinel); + assert(std::default_sentinel == std::ranges::next(std::move(iter), 8)); + } + { + std::counted_iterator iter(forward_iterator(buffer), 8); + + assert(iter != std::default_sentinel); + assert(std::default_sentinel == std::ranges::next(iter, 8)); + } + { + std::counted_iterator iter(random_access_iterator(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/libcxx/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp new file mode 100644 index 000000000000..4a896862cddf --- /dev/null +++ b/libcxx/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 count() const noexcept; + +#include + +#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{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{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{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{buffer}, 8); + assert(iter.count() == 8); + } + { + const std::counted_iterator iter(forward_iterator{buffer + 1}, 7); + assert(iter.count() == 7); + } + { + const std::counted_iterator iter(contiguous_iterator{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/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.conv.pass.cpp new file mode 100644 index 000000000000..e418a4db87ea --- /dev/null +++ b/libcxx/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 +// requires convertible_to +// constexpr counted_iterator(const counted_iterator& x); + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +class ConvertibleTo +{ + int *it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef int value_type; + typedef typename std::iterator_traits::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>>); + static_assert(!std::is_constructible_v>, + std::counted_iterator>>); + } + { + std::counted_iterator iter1(ConvertibleTo>{buffer}, 8); + std::counted_iterator> iter2(iter1); + assert(iter2.base() == forward_iterator{buffer}); + assert(iter2.count() == 8); + } + { + const std::counted_iterator iter1(ConvertibleTo>{buffer}, 8); + const std::counted_iterator> iter2(iter1); + assert(iter2.base() == forward_iterator{buffer}); + assert(iter2.count() == 8); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.default.pass.cpp new file mode 100644 index 000000000000..45b6d0bdfc37 --- /dev/null +++ b/libcxx/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 = default; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +constexpr bool test() { + static_assert( std::default_initializable>>); + static_assert(!std::default_initializable>>); + + std::counted_iterator> iter; + assert(iter.base() == cpp17_input_iterator()); + assert(iter.count() == 0); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.iter.pass.cpp new file mode 100644 index 000000000000..5ed8b68a1b9c --- /dev/null +++ b/libcxx/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 = default; +// constexpr counted_iterator(I x, iter_difference_t n); +// template +// requires convertible_to +// constexpr counted_iterator(const counted_iterator& x); + +#include + +#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{buffer}, 8); + assert(iter.base().base() == buffer); + assert(iter.count() == 8); + } + + { + std::counted_iterator iter(forward_iterator{buffer}, 8); + assert(iter.base() == forward_iterator{buffer}); + assert(iter.count() == 8); + } + + { + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + assert(iter.base() == contiguous_iterator{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{buffer}, 8); + assert(iter.base().base() == buffer); + assert(iter.count() == 8); + } + + { + const std::counted_iterator iter(forward_iterator{buffer}, 7); + assert(iter.base() == forward_iterator{buffer}); + assert(iter.count() == 7); + } + + { + const std::counted_iterator iter(contiguous_iterator{buffer}, 6); + assert(iter.base() == contiguous_iterator{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/libcxx/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/decrement.pass.cpp new file mode 100644 index 000000000000..249bcb3d0504 --- /dev/null +++ b/libcxx/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; +// constexpr counted_iterator operator--(int) +// requires bidirectional_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +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>; + std::counted_iterator iter(bidirectional_iterator{buffer + 2}, 6); + assert(iter-- == Counted(bidirectional_iterator{buffer + 2}, 6)); + assert(--iter == Counted(bidirectional_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), Counted); + ASSERT_SAME_TYPE(decltype(--iter), Counted&); + } + { + using Counted = std::counted_iterator>; + Counted iter(random_access_iterator{buffer + 2}, 6); + assert(iter-- == Counted(random_access_iterator{buffer + 2}, 6)); + assert(--iter == Counted(random_access_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), Counted); + ASSERT_SAME_TYPE(decltype(--iter), Counted&); + } + { + using Counted = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer + 2}, 6); + assert(iter-- == Counted(contiguous_iterator{buffer + 2}, 6)); + assert(--iter == Counted(contiguous_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), Counted); + ASSERT_SAME_TYPE(decltype(--iter), Counted&); + } + + { + static_assert( MinusEnabled>>); + static_assert(!MinusEnabled>>); + static_assert(!MinusEnabled>>); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp new file mode 100644 index 000000000000..5659129f62d2 --- /dev/null +++ b/libcxx/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; + +#include + +#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 +concept IsDereferenceable = requires(T& i) { + *i; +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + static_assert( IsDereferenceable>); + static_assert( IsDereferenceable>); + static_assert( IsDereferenceable>); + static_assert(!IsDereferenceable>); + } + + { + std::counted_iterator iter(cpp20_input_iterator{buffer}, 8); + for (int i = 1; i < 9; ++i, ++iter) + assert(*iter == i); + } + + { + std::counted_iterator iter(forward_iterator{buffer}, 8); + for (int i = 1; i < 9; ++i, ++iter) + assert(*iter == i); + } + + { + std::counted_iterator iter(contiguous_iterator{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{buffer}, 8); + assert(*iter == 1); + } + + { + const std::counted_iterator iter(forward_iterator{buffer + 1}, 7); + assert(*iter == 2); + } + + { + const std::counted_iterator iter(contiguous_iterator{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/libcxx/test/std/iterators/predef.iterators/counted.iterator/increment.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/increment.cpp new file mode 100644 index 000000000000..3feee86b968f --- /dev/null +++ b/libcxx/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; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +#ifndef TEST_HAS_NO_EXCEPTIONS +template +class ThrowsOnInc +{ + It it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::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 +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>; + std::counted_iterator iter(forward_iterator{buffer}, 8); + + assert(iter++ == Counted(forward_iterator{buffer}, 8)); + assert(++iter == Counted(forward_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter++), Counted); + ASSERT_SAME_TYPE(decltype(++iter), Counted&); + } + { + using Counted = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + + assert(iter++ == Counted(random_access_iterator{buffer}, 8)); + assert(++iter == Counted(random_access_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter++), Counted); + ASSERT_SAME_TYPE(decltype(++iter), Counted&); + } + + { + static_assert( PlusEnabled< std::counted_iterator>>); + static_assert(!PlusEnabled>>); + } + + 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; + 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>; + std::counted_iterator iter(cpp20_input_iterator{buffer}, 8); + + iter++; + assert(++iter == Counted(cpp20_input_iterator{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>; + std::counted_iterator iter(ThrowsOnInc{buffer}, 8); + try { + (void)iter++; + assert(false); + } catch (int x) { + assert(x == 42); + assert(iter.count() == 8); + } + + ASSERT_SAME_TYPE(decltype(iter++), ThrowsOnInc); + ASSERT_SAME_TYPE(decltype(++iter), Counted&); + } +#endif // TEST_HAS_NO_EXCEPTIONS + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp new file mode 100644 index 000000000000..dcb685bb11aa --- /dev/null +++ b/libcxx/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 +// iter_move(const counted_iterator& i) +// noexcept(noexcept(ranges::iter_move(i.current))) +// requires input_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +class HasNoexceptIterMove +{ + int *it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef int value_type; + typedef typename std::iterator_traits::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(buffer); + auto commonIter1 = std::counted_iterator(iter1, 8); + assert(std::ranges::iter_move(commonIter1) == 1); + ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(commonIter1)), int&&); + } + { + auto iter1 = forward_iterator(buffer); + auto commonIter1 = std::counted_iterator(iter1, 8); + assert(std::ranges::iter_move(commonIter1) == 1); + ASSERT_SAME_TYPE(decltype(std::ranges::iter_move(commonIter1)), int&&); + } + { + auto iter1 = random_access_iterator(buffer); + auto commonIter1 = std::counted_iterator(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>>()))); + static_assert(!noexcept(std::ranges::iter_move(std::declval>>()))); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp new file mode 100644 index 000000000000..b062072e9f58 --- /dev/null +++ b/libcxx/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 I2> +// friend constexpr void +// iter_swap(const counted_iterator& x, const counted_iterator& y) +// noexcept(noexcept(ranges::iter_swap(x.current, y.current))); + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +class HasNoexceptIterSwap +{ + int *it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef int value_type; + typedef typename std::iterator_traits::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(buffer); + auto commonIter1 = std::counted_iterator(iter1, 8); + auto commonIter2 = std::counted_iterator(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(buffer); + auto commonIter1 = std::counted_iterator(iter1, 8); + auto commonIter2 = std::counted_iterator(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(buffer); + auto commonIter1 = std::counted_iterator(iter1, 8); + auto commonIter2 = std::counted_iterator(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::declval>&>() + ))); + static_assert(!noexcept(std::ranges::iter_swap( + std::declval>&>(), + std::declval>&>() + ))); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_concept_conformance.compile.pass.cpp new file mode 100644 index 000000000000..9cd71b2e0cfe --- /dev/null +++ b/libcxx/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 + +#include "test_macros.h" +#include "test_iterators.h" + +void test() { + static_assert(std::input_iterator>>); + static_assert(std::forward_iterator>>); + static_assert(std::bidirectional_iterator>>); + static_assert(std::bidirectional_iterator>>); + static_assert(std::random_access_iterator>>); + static_assert(std::contiguous_iterator>>); + + using Iter = std::counted_iterator>; + static_assert(std::indirectly_writable); + static_assert(std::indirectly_swappable); +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.pass.cpp new file mode 100644 index 000000000000..fe06b5183f56 --- /dev/null +++ b/libcxx/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 +// requires same_as> // see [iterator.concepts.general] +// struct iterator_traits> : iterator_traits { +// using pointer = conditional_t, +// add_pointer_t>, void>; +// }; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +void test() { + { + using Iter = cpp17_input_iterator; + using CommonIter = std::counted_iterator; + using IterTraits = std::iterator_traits; + + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + using Iter = forward_iterator; + using CommonIter = std::counted_iterator; + using IterTraits = std::iterator_traits; + + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + using Iter = random_access_iterator; + using CommonIter = std::counted_iterator; + using IterTraits = std::iterator_traits; + + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } + { + using Iter = contiguous_iterator; + using CommonIter = std::counted_iterator; + using IterTraits = std::iterator_traits; + + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + } +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp new file mode 100644 index 000000000000..534a94a6367a --- /dev/null +++ b/libcxx/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 + +#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 +concept HasValueType = requires { typename T::value_type; }; + +template +concept HasIteratorConcept = requires { typename T::iterator_concept; }; + +template +concept HasIteratorCategory = requires { typename T::iterator_category; }; + +void test() { + { + using Iter = std::counted_iterator; + static_assert(std::same_as); + static_assert(!HasValueType); + static_assert(std::same_as); + static_assert(!HasIteratorConcept); + static_assert(!HasIteratorCategory); + } + { + using Iter = std::counted_iterator>; + static_assert(std::same_as>); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(!HasIteratorCategory); + } + { + using Iter = std::counted_iterator>; + static_assert(std::same_as>); + static_assert(std::same_as); + static_assert(std::same_as); + static_assert(!HasIteratorConcept); + static_assert(std::same_as); + } +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.default_sentinel.pass.cpp new file mode 100644 index 000000000000..0bc85f648c08 --- /dev/null +++ b/libcxx/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 operator-( +// const counted_iterator& x, default_sentinel_t); +// friend constexpr iter_difference_t operator-( +// default_sentinel_t, const counted_iterator& y); + +#include + +#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{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + { + const std::counted_iterator iter(random_access_iterator{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + { + std::counted_iterator iter(forward_iterator{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + { + const std::counted_iterator iter(forward_iterator{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + { + std::counted_iterator iter(cpp20_input_iterator{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + { + const std::counted_iterator iter(cpp20_input_iterator{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); + ASSERT_SAME_TYPE(decltype(std::default_sentinel - iter), std::iter_difference_t); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.eq.pass.cpp new file mode 100644 index 000000000000..b6ca1e188b8f --- /dev/null +++ b/libcxx/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 n) +// requires random_access_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +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>; + Counted iter(random_access_iterator{buffer + 2}, 6); + assert((iter -= 2) == Counted(random_access_iterator{buffer}, 8)); + assert((iter -= 0) == Counted(random_access_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&); + } + { + using Counted = std::counted_iterator>; + Counted iter(contiguous_iterator{buffer + 2}, 6); + assert((iter -= 2) == Counted(contiguous_iterator{buffer}, 8)); + assert((iter -= 0) == Counted(contiguous_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&); + } + { + static_assert( MinusEqEnabled< std::counted_iterator>>); + static_assert(!MinusEqEnabled>>); + } + + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.iter.pass.cpp new file mode 100644 index 000000000000..e425ec7490a0 --- /dev/null +++ b/libcxx/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 I2> +// friend constexpr iter_difference_t operator-( +// const counted_iterator& x, const counted_iterator& y); + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +// This iterator is common_with forward_iterator but NOT comparable with it. +template +class CommonWithForwardIter +{ + It it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::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.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{buffer}, 8); + std::counted_iterator iter2(random_access_iterator{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); + } + { + const std::counted_iterator iter1(random_access_iterator{buffer}, 8); + const std::counted_iterator iter2(random_access_iterator{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); + } + { + std::counted_iterator iter1(contiguous_iterator{buffer}, 8); + std::counted_iterator iter2(contiguous_iterator{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); + } + { + const std::counted_iterator iter1(contiguous_iterator{buffer}, 8); + const std::counted_iterator iter2(contiguous_iterator{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); + } + // 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{buffer}, 8); + std::counted_iterator iter2(forward_iterator{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); + } + { + const std::counted_iterator iter1(CommonWithForwardIter{buffer}, 8); + const std::counted_iterator iter2(forward_iterator{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); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp new file mode 100644 index 000000000000..5b72e2e05597 --- /dev/null +++ b/libcxx/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 n) const +// requires random_access_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +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>; + Counted iter(random_access_iterator{buffer + 2}, 6); + assert(iter - 2 == Counted(random_access_iterator{buffer}, 8)); + assert(iter - 0 == Counted(random_access_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), Counted); + } + { + using Counted = std::counted_iterator>; + const Counted iter(random_access_iterator{buffer + 2}, 6); + assert(iter - 2 == Counted(random_access_iterator{buffer}, 8)); + assert(iter - 0 == Counted(random_access_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), Counted); + } + { + using Counted = std::counted_iterator>; + Counted iter(contiguous_iterator{buffer + 2}, 6); + assert(iter - 2 == Counted(contiguous_iterator{buffer}, 8)); + assert(iter - 0 == Counted(contiguous_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), Counted); + } + { + using Counted = std::counted_iterator>; + const Counted iter(contiguous_iterator{buffer + 2}, 6); + assert(iter - 2 == Counted(contiguous_iterator{buffer}, 8)); + assert(iter - 0 == Counted(contiguous_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), Counted); + } + + { + static_assert( MinusEnabled>>); + static_assert(!MinusEnabled>>); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp new file mode 100644 index 000000000000..36924a49d562 --- /dev/null +++ b/libcxx/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 n) const +// requires random_access_iterator; +// friend constexpr counted_iterator operator+( +// iter_difference_t n, const counted_iterator& x) +// requires random_access_iterator; +// constexpr counted_iterator& operator+=(iter_difference_t n) +// requires random_access_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +concept PlusEnabled = requires(Iter& iter) { + iter + 1; +}; + +template +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>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(iter + 2 == Counted(random_access_iterator{buffer + 2}, 6)); + assert(iter + 0 == Counted(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), Counted); + } + { + using Counted = const std::counted_iterator>; + const std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(iter + 8 == Counted(random_access_iterator{buffer + 8}, 0)); + assert(iter + 0 == Counted(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t); + } + } + + { + { + using Counted = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(2 + iter == Counted(random_access_iterator{buffer + 2}, 6)); + assert(0 + iter == Counted(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), Counted); + } + { + using Counted = const std::counted_iterator>; + const std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(8 + iter == Counted(random_access_iterator{buffer + 8}, 0)); + assert(0 + iter == Counted(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t); + } + } + + { + { + using Counted = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert((iter += 2) == Counted(random_access_iterator{buffer + 2}, 6)); + assert((iter += 0) == Counted(random_access_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter += 2), Counted&); + } + { + using Counted = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + assert((iter += 8) == Counted(contiguous_iterator{buffer + 8}, 0)); + assert((iter += 0) == Counted(contiguous_iterator{buffer + 8}, 0)); + + ASSERT_SAME_TYPE(decltype(iter += 2), Counted&); + } + { + static_assert( PlusEnabled>>); + static_assert(!PlusEnabled>>); + + static_assert( PlusEqEnabled< std::counted_iterator>>); + static_assert(!PlusEqEnabled>>); + } + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/subscript.pass.cpp new file mode 100644 index 000000000000..0259f3ec8958 --- /dev/null +++ b/libcxx/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 n) const +// requires random_access_iterator; + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +template +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{buffer}, 8); + for (int i = 1; i < 9; ++i) + assert(iter[i - 1] == i); + } + { + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + for (int i = 1; i < 9; ++i) + assert(iter[i - 1] == i); + } + + { + const std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(iter[0] == 1); + } + { + const std::counted_iterator iter(contiguous_iterator{buffer}, 8); + assert(iter[7] == 8); + } + + { + static_assert( SubscriptEnabled>>); + static_assert(!SubscriptEnabled>>); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/three_way_compare.pass.cpp new file mode 100644 index 000000000000..900f698faa97 --- /dev/null +++ b/libcxx/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 I2> +// friend constexpr strong_ordering operator<=>( +// const counted_iterator& x, const counted_iterator& y); + +#include + +#include "test_macros.h" +#include "test_iterators.h" + +// This iterator is common_with forward_iterator but NOT comparable with it. +template +class CommonWithForwardIter +{ + It it_; + +public: + typedef std::input_iterator_tag iterator_category; + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + typedef It pointer; + typedef typename std::iterator_traits::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.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(buffer), 8); + std::counted_iterator iter2(CommonWithForwardIter(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(buffer), 8); + std::counted_iterator iter2(forward_iterator(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; +}