diff --git a/libcxx/docs/Status/RangesPaper.csv b/libcxx/docs/Status/RangesPaper.csv --- a/libcxx/docs/Status/RangesPaper.csv +++ b/libcxx/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] `_,"| `ranges::begin `_ | `ranges::end `_ diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -134,6 +134,7 @@ __iterator/advance.h __iterator/back_insert_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 --- /dev/null +++ b/libcxx/include/__iterator/counted_iterator.h @@ -0,0 +1,252 @@ +// -*- 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 <__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 <__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) + +// Named like this so they don't conflict with "__has_iterator_{concept,category}". +template +concept __concept_has_iterator_concept = requires { typename _Iter::iterator_concept; }; + +template +struct __counted_iterator_concept {}; + +template<__concept_has_iterator_concept _Iter> +struct __counted_iterator_concept<_Iter> { + using iterator_concept = typename _Iter::iterator_concept; +}; + +template +concept __concept_has_iterator_category = requires { typename _Iter::iterator_category; }; + +template +struct __counted_iterator_category {}; + +template<__concept_has_iterator_category _Iter> +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 = typename _Iter::value_type; +}; + +template +class counted_iterator + : public __counted_iterator_concept<_Iter> + , public __counted_iterator_category<_Iter> + , public __counted_iterator_value_type<_Iter> +{ +public: + _Iter __current_ = _Iter(); + iter_difference_t<_Iter> __count_ = 0; + + using iterator_type = _Iter; + using difference_type = iter_difference_t<_Iter>; + + constexpr counted_iterator() requires default_initializable<_Iter> = default; + + constexpr counted_iterator(_Iter __iter, iter_difference_t<_Iter> __n) + : __current_(_VSTD::move(__iter)), __count_(__n) {} + + template + requires convertible_to + constexpr counted_iterator(const counted_iterator<_I2>& __other) + : __current_(__other.__current_), __count_(__other.__count_) {} + + template + requires assignable_from<_Iter&, const _I2&> + constexpr counted_iterator& operator=(const counted_iterator<_I2>& __other) { + __current_ = __other.__current_; + __count_ = __other.__count_; + return *this; + } + + constexpr const _Iter& base() const& { return __current_; } + constexpr _Iter base() && { return _VSTD::move(__current_); } + + constexpr iter_difference_t<_Iter> count() const noexcept { return __count_; } + + constexpr decltype(auto) operator*() { return *__current_; } + constexpr decltype(auto) operator*() const + requires __referenceable { return *__current_; } + + constexpr auto operator->() const noexcept + requires contiguous_iterator<_Iter> + { + return _VSTD::to_address(__current_); + } + + constexpr counted_iterator& operator++() { + ++__current_; + --__count_; + return *this; + } + + decltype(auto) operator++(int) { + --__count_; + try { return __current_++; } + catch(...) { ++__count_; throw; } + } + + constexpr counted_iterator operator++(int) + requires forward_iterator<_Iter> + { + counted_iterator __tmp = *this; + ++*this; + return __tmp; + } + + constexpr counted_iterator& operator--() + requires bidirectional_iterator<_Iter> + { + --__current_; + ++__count_; + return *this; + } + + constexpr counted_iterator operator--(int) + requires bidirectional_iterator<_Iter> + { + counted_iterator __tmp = *this; + --*this; + return __tmp; + } + + constexpr counted_iterator operator+(iter_difference_t<_Iter> __n) const + requires random_access_iterator<_Iter> + { + return counted_iterator(__current_ + __n, __count_ - __n); + } + + friend constexpr counted_iterator operator+( + iter_difference_t<_Iter> __n, const counted_iterator& __x) + requires random_access_iterator<_Iter> + { + return __x + __n; + } + + constexpr counted_iterator& operator+=(iter_difference_t<_Iter> __n) + requires random_access_iterator<_Iter> + { + __current_ += __n; + __count_ -= __n; + return *this; + } + + constexpr counted_iterator operator-(iter_difference_t<_Iter> __n) const + requires random_access_iterator<_Iter> + { + return counted_iterator(__current_ - __n, __count_ + __n); + } + + template _I2> + friend constexpr iter_difference_t<_I2> operator-( + const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs) + { + return __lhs.__count_ - __rhs.__count_; + } + + friend constexpr iter_difference_t<_Iter> operator-( + const counted_iterator& __lhs, default_sentinel_t) + { + return -__lhs.__count_; + } + + friend constexpr iter_difference_t<_Iter> operator-( + default_sentinel_t, const counted_iterator& __rhs) + { + return __rhs.__count_; + } + + constexpr counted_iterator& operator-=(iter_difference_t<_Iter> __n) + requires random_access_iterator<_Iter> + { + __current_ -= __n; + __count_ += __n; + return *this; + } + + constexpr decltype(auto) operator[](iter_difference_t<_Iter> __n) const + requires random_access_iterator<_Iter> + { + return __current_[__n]; + } + + template _I2> + friend constexpr bool operator==( + const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs) + { + return __lhs.__count_ == __rhs.__count_; + } + + 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); + + friend constexpr iter_rvalue_reference_t<_Iter> iter_move(const counted_iterator& __i) + noexcept(noexcept(ranges::iter_move(__i.__current_))) + requires input_iterator<_Iter> + { + return ranges::iter_move(__i.__current_); + } + + template _I2> + friend constexpr void iter_swap(const counted_iterator& __x, const counted_iterator<_I2>& __y) + noexcept(noexcept(ranges::iter_swap(__x.__current_, __y.__current_))) + { + 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 --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -395,6 +395,13 @@ 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 @@ -565,6 +572,7 @@ #include <__iterator/advance.h> #include <__iterator/back_insert_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 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -478,6 +478,7 @@ module advance { header "__iterator/advance.h" } module back_insert_iterator { header "__iterator/back_insert_iterator.h" } module concepts { header "__iterator/concepts.h" } + module counted_iterator { header "__iterator/counted_iterator.h" } module data { header "__iterator/data.h" } module default_sentinel { header "__iterator/default_sentinel.h" } module distance { header "__iterator/distance.h" } 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/assign.pass.cpp @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// 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;} +}; + +constexpr bool test() { + int buffer[8] = {1,2 ,3 , 4, 5, 6, 7, 8}; + + { + 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); + iter1 = iter2; + assert(iter1.base().base() == buffer + 2); + assert(iter1.count() == 6); + } + { + 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); + iter1 = iter2; + assert(iter1.base().base() == buffer + 2); + assert(iter1.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/base.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/base.pass.cpp @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +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); + } + + { + 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&); + } + + 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/compare.pass.cpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// 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;} +}; + +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); + ++iter1; + assert(iter1 != iter2); + } + } + + { + { + 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)); + } + } + + + 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +constexpr bool test() { + int buffer[8] = {1,2 ,3 , 4, 5, 6, 7, 8}; + + { + std::counted_iterator iter(cpp20_input_iterator{buffer}, 8); + for (unsigned i = 8; i != 0; --i, ++iter) + assert(iter.count() == i); + } + + { + std::counted_iterator iter(forward_iterator{buffer}, 8); + for (unsigned i = 8; i != 0; --i, ++iter) + assert(iter.count() == i); + } + + { + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + for (unsigned i = 8; i != 0; --i, ++iter) + assert(iter.count() == i); + } + + { + 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); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/ctor.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +class ConvertibleIter +{ + 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_;} + + ConvertibleIter() = default; + explicit constexpr ConvertibleIter(int *it) : it_(it) {} + + constexpr reference operator*() const {return *it_;} + + constexpr ConvertibleIter& operator++() {++it_; return *this;} + constexpr ConvertibleIter operator++(int) + {ConvertibleIter tmp(*this); ++(*this); return tmp;} + + constexpr operator forward_iterator() const { return forward_iterator(it_); } +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + static_assert( std::default_initializable>>); + static_assert(!std::default_initializable>>); + } + + { + { + 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); + } + + { + 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); + } + } + + { + { + std::counted_iterator iter1(ConvertibleIter{buffer}, 8); + std::counted_iterator> iter2(iter1); + assert(iter2.base() == forward_iterator{buffer}); + assert(iter2.count() == 8); + } + { + const std::counted_iterator iter1(ConvertibleIter{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/deref.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/deref.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +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 = 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); + } + + { + 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); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_move.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +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&&); + } + + 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +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); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} 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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iterator_traits.compile.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 + +// 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); + } + + // Testing iterator conformance. + { + 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::random_access_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/member_types.compile.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/member_types.compile.pass.cpp new file mode 100644 --- /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 OutputIterator { + using difference_type = int; + + int operator*(); + void operator++(int); + OutputIterator& 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.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.pass.cpp @@ -0,0 +1,265 @@ +//===----------------------------------------------------------------------===// +// +// 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; +// template I2> +// friend constexpr iter_difference_t operator-( +// const counted_iterator& x, const counted_iterator& y); +// 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); +// constexpr counted_iterator& operator-=(iter_difference_t n) +// requires random_access_iterator; + +#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;} +}; + +template +concept MinusEnabled = requires(Iter& iter) { + iter - 1; +}; + +template +concept MinusEqEnabled = requires(Iter& iter) { + iter -= 1; +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer + 2}, 6); + assert(iter - 2 == counted_t(random_access_iterator{buffer}, 8)); + assert(iter - 0 == counted_t(random_access_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), counted_t); + } + { + using counted_t = const std::counted_iterator>; + const std::counted_iterator iter(random_access_iterator{buffer + 2}, 6); + assert(iter - 2 == counted_t(random_access_iterator{buffer}, 8)); + assert(iter - 0 == counted_t(random_access_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), std::remove_const_t); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer + 2}, 6); + assert(iter - 2 == counted_t(contiguous_iterator{buffer}, 8)); + assert(iter - 0 == counted_t(contiguous_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), counted_t); + } + { + using counted_t = const std::counted_iterator>; + const std::counted_iterator iter(contiguous_iterator{buffer + 2}, 6); + assert(iter - 2 == counted_t(contiguous_iterator{buffer}, 8)); + assert(iter - 0 == counted_t(contiguous_iterator{buffer + 2}, 6)); + assert(iter.count() == 6); + + ASSERT_SAME_TYPE(decltype(iter - 2), std::remove_const_t); + } + } + + { + { + 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); + } + // Note: not using random_access_iterator here. + { + 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); + } + } + + { + { + 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); + } + } + + { + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer + 2}, 6); + assert((iter -= 2) == counted_t(random_access_iterator{buffer}, 8)); + assert((iter -= 0) == counted_t(random_access_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter -= 2), counted_t&); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer + 2}, 6); + assert((iter -= 2) == counted_t(contiguous_iterator{buffer}, 8)); + assert((iter -= 0) == counted_t(contiguous_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter -= 2), counted_t&); + } + { + static_assert( MinusEnabled>>); + static_assert(!MinusEnabled>>); + + 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_minus.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus_minus.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus_minus.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_t = std::counted_iterator>; + std::counted_iterator iter(bidirectional_iterator{buffer + 2}, 6); + assert(iter-- == counted_t(bidirectional_iterator{buffer + 2}, 6)); + assert(--iter == counted_t(bidirectional_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), counted_t); + ASSERT_SAME_TYPE(decltype(--iter), counted_t&); + } + { + using counted_t = std::counted_iterator>; + counted_t iter(random_access_iterator{buffer + 2}, 6); + assert(iter-- == counted_t(random_access_iterator{buffer + 2}, 6)); + assert(--iter == counted_t(random_access_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), counted_t); + ASSERT_SAME_TYPE(decltype(--iter), counted_t&); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer + 2}, 6); + assert(iter-- == counted_t(contiguous_iterator{buffer + 2}, 6)); + assert(--iter == counted_t(contiguous_iterator{buffer}, 8)); + assert(iter.count() == 8); + + ASSERT_SAME_TYPE(decltype(iter--), counted_t); + ASSERT_SAME_TYPE(decltype(--iter), counted_t&); + } + + { + 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/plus.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/plus.pass.cpp new file mode 100644 --- /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_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(iter + 2 == counted_t(random_access_iterator{buffer + 2}, 6)); + assert(iter + 0 == counted_t(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), counted_t); + } + { + using counted_t = const std::counted_iterator>; + const std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(iter + 8 == counted_t(random_access_iterator{buffer + 8}, 0)); + assert(iter + 0 == counted_t(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t); + } + } + + { + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(2 + iter == counted_t(random_access_iterator{buffer + 2}, 6)); + assert(0 + iter == counted_t(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), counted_t); + } + { + using counted_t = const std::counted_iterator>; + const std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert(8 + iter == counted_t(random_access_iterator{buffer + 8}, 0)); + assert(0 + iter == counted_t(random_access_iterator{buffer}, 8)); + + ASSERT_SAME_TYPE(decltype(iter + 2), std::remove_const_t); + } + } + + { + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + assert((iter += 2) == counted_t(random_access_iterator{buffer + 2}, 6)); + assert((iter += 0) == counted_t(random_access_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter += 2), counted_t&); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + assert((iter += 8) == counted_t(contiguous_iterator{buffer + 8}, 0)); + assert((iter += 0) == counted_t(contiguous_iterator{buffer + 8}, 0)); + + ASSERT_SAME_TYPE(decltype(iter += 2), counted_t&); + } + { + 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/plus_plus.pass.cpp b/libcxx/test/std/iterators/predef.iterators/counted.iterator/plus_plus.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/plus_plus.pass.cpp @@ -0,0 +1,137 @@ +//===----------------------------------------------------------------------===// +// +// 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 ThrowsOnCopy +{ + It it_; + int count_ = 0; + +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_;} + + ThrowsOnCopy() = default; + constexpr ThrowsOnCopy(ThrowsOnCopy const& other) : count_(other.count_ + 1) { + if (count_ > 1) + throw 42; + } + explicit constexpr ThrowsOnCopy(It it) : it_(it) {} + + constexpr reference operator*() const {return *it_;} + + constexpr ThrowsOnCopy& operator++() {++it_; return *this;} + constexpr ThrowsOnCopy operator++(int) + {ThrowsOnCopy tmp(*this); ++(*this); return tmp;} +}; +#endif // TEST_HAS_NO_EXCEPTIONS + +template +concept PlusEnabled = requires(Iter& iter) { + iter++; + ++iter; +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(forward_iterator{buffer}, 8); + + assert(iter++ == counted_t(forward_iterator{buffer}, 8)); + assert(++iter == counted_t(forward_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter++), counted_t); + ASSERT_SAME_TYPE(decltype(++iter), counted_t&); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(random_access_iterator{buffer}, 8); + + assert(iter++ == counted_t(random_access_iterator{buffer}, 8)); + assert(++iter == counted_t(random_access_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter++), counted_t); + ASSERT_SAME_TYPE(decltype(++iter), counted_t&); + } + + { + static_assert( PlusEnabled< std::counted_iterator>>); + static_assert(!PlusEnabled>>); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + +#ifndef TEST_HAS_NO_EXCEPTIONS + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(cpp20_input_iterator{buffer}, 8); + + iter++; + assert(++iter == counted_t(cpp20_input_iterator{buffer + 2}, 6)); + + ASSERT_SAME_TYPE(decltype(iter++), void); + ASSERT_SAME_TYPE(decltype(++iter), counted_t&); + } + { + using counted_t = std::counted_iterator>; + std::counted_iterator iter(ThrowsOnCopy{buffer}, 8); + try { + auto other = iter++; + (void)other; + assert(false); + } catch (int x) { + assert(x == 42); + assert(iter.count() == 8); + } + try { + auto other = ++iter; + (void)other; + assert(false); + } catch (int x) { + assert(x == 42); + // TODO: LWG issue! This should be 8. + // We're missing a try-catch block around the second return. + assert(iter.count() == 7); + } + + ASSERT_SAME_TYPE(decltype(iter++), ThrowsOnCopy); + ASSERT_SAME_TYPE(decltype(++iter), counted_t&); + } +#endif // TEST_HAS_NO_EXCEPTIONS + + 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 --- /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/support/test_iterators.h b/libcxx/test/support/test_iterators.h --- a/libcxx/test/support/test_iterators.h +++ b/libcxx/test/support/test_iterators.h @@ -645,7 +645,7 @@ using difference_type = std::iter_difference_t; using iterator_concept = std::input_iterator_tag; - cpp20_input_iterator() = default; + cpp20_input_iterator() = delete; cpp20_input_iterator(cpp20_input_iterator&&) = default; cpp20_input_iterator& operator=(cpp20_input_iterator&&) = default;