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 `_ @@ -139,7 +139,7 @@ `[range.filter] `_,filter_view,[range.all],Louis Dionne,Not started `[range.transform] `_,`transform_view `_,[range.all],Zoe Carver,✅ `[range.iota] `_,iota_view,[range.all],Louis Dionne,Not started -`[range.take] `_,take_view,[range.all],Zoe Carver,In Progress +`[range.take] `_,take_view,[range.all],Zoe Carver,✅ `[range.join] `_,join_view,[range.all],Christopher Di Bella,Not started `[range.empty] `_,`empty_view `_,[view.interface],Zoe Carver,✅ `[range.single] `_,single_view,[view.interface],Zoe Carver,In Progress diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -135,6 +135,7 @@ __iterator/back_insert_iterator.h __iterator/common_iterator.h __iterator/concepts.h + __iterator/counted_iterator.h __iterator/data.h __iterator/default_sentinel.h __iterator/distance.h @@ -196,6 +197,7 @@ __ranges/enable_view.h __ranges/non_propagating_cache.h __ranges/ref_view.h + __ranges/take_view.h __ranges/size.h __ranges/subrange.h __ranges/transform_view.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,301 @@ +// -*- 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 <__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_, "Cannot advance iterator back before begin."); + __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; + } + +// TODO: implement this once three_way_comparable is landed. +// template _I2> +// friend constexpr strong_ordering operator<=>( +// const counted_iterator& __lhs, const counted_iterator<_I2>& __rhs); + + _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/__ranges/take_view.h b/libcxx/include/__ranges/take_view.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__ranges/take_view.h @@ -0,0 +1,175 @@ +// -*- 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___RANGES_TAKE_VIEW_H +#define _LIBCPP___RANGES_TAKE_VIEW_H + +#include <__algorithm/min.h> +#include <__config> +#include <__iterator/counted_iterator.h> +#include <__iterator/iterator_traits.h> +#include <__ranges/access.h> +#include <__ranges/concepts.h> +#include <__ranges/view_interface.h> + +#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) + +namespace ranges { + template + class take_view : public view_interface> { + [[no_unique_address]] _View __base_ = _View(); + range_difference_t<_View> __count_ = 0; + + template class __sentinel; + + public: + _LIBCPP_HIDE_FROM_ABI + take_view() requires default_initializable<_View> = default; + + _LIBCPP_HIDE_FROM_ABI + constexpr take_view(_View __base, range_difference_t<_View> __count) + : __base_(_VSTD::move(__base)), __count_(__count) {} + + _LIBCPP_HIDE_FROM_ABI + constexpr _View base() const& requires copy_constructible<_View> { return __base_; } + + _LIBCPP_HIDE_FROM_ABI + constexpr _View base() && { return _VSTD::move(__base_); } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto begin() requires (!__simple_view<_View>) { + if constexpr (sized_range<_View>) { + if constexpr (random_access_range<_View>) { + return ranges::begin(__base_); + } else { + using _DifferenceT = range_difference_t<_View>; + auto __size = size(); + return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size)); + } + } else { + return counted_iterator(ranges::begin(__base_), __count_); + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto begin() const requires range { + if constexpr (sized_range) { + if constexpr (random_access_range) { + return ranges::begin(__base_); + } else { + using _DifferenceT = range_difference_t; + auto __size = size(); + return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size)); + } + } else { + return counted_iterator(ranges::begin(__base_), __count_); + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto end() requires (!__simple_view<_View>) { + if constexpr (sized_range<_View>) { + if constexpr (random_access_range<_View>) { + return ranges::begin(__base_) + size(); + } else { + return default_sentinel; + } + } else { + return __sentinel{ranges::end(__base_)}; + } + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto end() const requires range { + if constexpr (sized_range) { + if constexpr (random_access_range) { + return ranges::begin(__base_) + size(); + } else { + return default_sentinel; + } + } else { + return __sentinel{ranges::end(__base_)}; + } + } + + + _LIBCPP_HIDE_FROM_ABI + constexpr auto size() requires sized_range<_View> { + auto __n = ranges::size(__base_); + return _VSTD::min(__n, static_cast(__count_)); + } + + _LIBCPP_HIDE_FROM_ABI + constexpr auto size() const requires sized_range { + auto __n = ranges::size(__base_); + return _VSTD::min(__n, static_cast(__count_)); + } + }; + + template + template + class take_view<_View>::__sentinel { + using _Base = __maybe_const<_Const, _View>; + template + using _Iter = counted_iterator>>; + [[no_unique_address]] sentinel_t<_Base> __end_ = sentinel_t<_Base>(); + + template + friend class take_view<_View>::__sentinel; + +public: + _LIBCPP_HIDE_FROM_ABI + __sentinel() = default; + + _LIBCPP_HIDE_FROM_ABI + constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(_VSTD::move(__end)) {} + + _LIBCPP_HIDE_FROM_ABI + constexpr __sentinel(__sentinel __s) + requires _Const && convertible_to, sentinel_t<_Base>> + : __end_(_VSTD::move(__s.__end_)) {} + + _LIBCPP_HIDE_FROM_ABI + constexpr sentinel_t<_Base> base() const { return __end_; } + + _LIBCPP_HIDE_FROM_ABI + friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) { + return __lhs.count() == 0 || __lhs.base() == __rhs.__end_; + } + + template + requires sentinel_for, iterator_t<__maybe_const<_OtherConst, _View>>> + _LIBCPP_HIDE_FROM_ABI + friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) { + return __lhs.count() == 0 || __lhs.base() == __rhs.__end_; + } + }; + + template + take_view(_Range&&, range_difference_t<_Range>) -> take_view>; + + template + inline constexpr bool enable_borrowed_range> = enable_borrowed_range<_Tp>; +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_TAKE_VIEW_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -399,6 +399,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 @@ -570,6 +577,7 @@ #include <__iterator/back_insert_iterator.h> #include <__iterator/common_iterator.h> #include <__iterator/concepts.h> +#include <__iterator/counted_iterator.h> #include <__iterator/data.h> #include <__iterator/default_sentinel.h> #include <__iterator/distance.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -485,6 +485,7 @@ module back_insert_iterator { private header "__iterator/back_insert_iterator.h" } module common_iterator { private header "__iterator/common_iterator.h" } module concepts { private header "__iterator/concepts.h" } + module counted_iterator { private header "__iterator/counted_iterator.h" } module data { private header "__iterator/data.h" } module default_sentinel { private header "__iterator/default_sentinel.h" } module distance { private header "__iterator/distance.h" } @@ -627,6 +628,7 @@ module ref_view { private header "__ranges/ref_view.h" } module size { private header "__ranges/size.h" } module subrange { private header "__ranges/subrange.h" } + module take_view { private header "__ranges/take_view.h" } module transform_view { private header "__ranges/transform_view.h" } module view_interface { private header "__ranges/view_interface.h" } } diff --git a/libcxx/include/ranges b/libcxx/include/ranges --- a/libcxx/include/ranges +++ b/libcxx/include/ranges @@ -153,6 +153,12 @@ template inline constexpr bool enable_borrowed_range> = enable_borrowed_range; + + // [range.take], take view + template class take_view; + + template + inline constexpr bool enable_borrowed_range> = enable_borrowed_range; } */ @@ -170,6 +176,7 @@ #include <__ranges/enable_borrowed_range.h> #include <__ranges/enable_view.h> #include <__ranges/ref_view.h> +#include <__ranges/take_view.h> #include <__ranges/size.h> #include <__ranges/subrange.h> #include <__ranges/transform_view.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 --- /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/libcxx/diagnostics/detail.headers/ranges/take_view.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/take_view.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/ranges/take_view.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: '__ranges/take_view.h'}} +#include <__ranges/take_view.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 --- /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 --- /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 --- /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 --- /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 --- /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 (unsigned i = 8; i != 0; --i, ++iter) + assert(iter.count() == i); + + static_assert(noexcept(iter.count())); + } + { + std::counted_iterator iter(forward_iterator{buffer}, 8); + for (unsigned i = 8; i != 0; --i, ++iter) + assert(iter.count() == i); + + static_assert(noexcept(iter.count())); + } + { + std::counted_iterator iter(contiguous_iterator{buffer}, 8); + for (unsigned 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 --- /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 --- /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 --- /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 --- /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 --- /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 --- /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 --- /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 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/counted.iterator/iter_swap.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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_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 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 --- /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 --- /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 --- /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 --- /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 --- /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 --- /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/ranges/range.adaptors/range.take/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/base.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/base.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 V base() const& requires copy_constructible; +// constexpr V base() &&; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +constexpr bool hasLValueQualifiedBase(auto&& view) { + return requires { view.base(); }; +} + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + std::ranges::take_view tv(CopyableView{buffer}, 0); + assert(tv.base().ptr_ == buffer); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(tv.base()), CopyableView); + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), CopyableView); + static_assert(hasLValueQualifiedBase(tv)); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 1); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), ContiguousView); + static_assert(!hasLValueQualifiedBase(tv)); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 2); + assert(tv.base().ptr_ == buffer); + assert(std::move(tv).base().ptr_ == buffer); + + ASSERT_SAME_TYPE(decltype(tv.base()), CopyableView); + ASSERT_SAME_TYPE(decltype(std::move(tv).base()), CopyableView); + static_assert(hasLValueQualifiedBase(tv)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/begin.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/begin.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 auto begin() requires (!simple-view); +// constexpr auto begin() const requires range; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" +#include "types.h" + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 4); + assert(tv.begin() == begin(SizedRandomAccessView(buffer))); + ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 4); + assert(tv.begin() == begin(SizedRandomAccessView(buffer))); + ASSERT_SAME_TYPE(decltype(tv.begin()), RandomAccessIter); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(ForwardIter(buffer), 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(ForwardIter(buffer), 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 4); + assert(tv.begin() == std::counted_iterator(buffer, 4)); + ASSERT_SAME_TYPE(decltype(tv.begin()), std::counted_iterator); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/borrowing.compile.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// inline constexpr bool enable_borrowed_range> = enable_borrowed_range; + +#include +#include + +#include "test_iterators.h" + +struct View : std::ranges::view_base { + friend int* begin(View&); + friend int* begin(View const&); + friend sentinel_wrapper end(View&); + friend sentinel_wrapper end(View const&); +}; + +struct BorrowableView : std::ranges::view_base { + friend int* begin(BorrowableView&); + friend int* begin(BorrowableView const&); + friend sentinel_wrapper end(BorrowableView&); + friend sentinel_wrapper end(BorrowableView const&); +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +static_assert(!std::ranges::enable_borrowed_range>); +static_assert( std::ranges::enable_borrowed_range>); diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/ctad.compile.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// take_view(R&&, range_difference_t) -> take_view>; + +#include +#include + +#include "test_iterators.h" + +struct View : std::ranges::view_base { + friend int* begin(View&); + friend int* begin(View const&); + friend sentinel_wrapper end(View&); + friend sentinel_wrapper end(View const&); +}; + +struct Range { + friend int* begin(Range&); + friend int* begin(Range const&); + friend sentinel_wrapper end(Range&); + friend sentinel_wrapper end(Range const&); +}; + +struct BorrowedRange { + friend int* begin(BorrowedRange&); + friend int* begin(BorrowedRange const&); + friend sentinel_wrapper end(BorrowedRange&); + friend sentinel_wrapper end(BorrowedRange const&); +}; + +template<> +inline constexpr bool std::ranges::enable_borrowed_range = true; + +void testCTAD() { + View v; + Range r; + BorrowedRange br; + static_assert(std::same_as< + decltype(std::ranges::take_view(v, 0)), + std::ranges::take_view + >); + static_assert(std::same_as< + decltype(std::ranges::take_view(r, 0)), + std::ranges::take_view> + >); + // std::ranges::take_view(std::move(r), 0) invalid. RValue range must be borrowed. + static_assert(std::same_as< + decltype(std::ranges::take_view(br, 0)), + std::ranges::take_view> + >); + static_assert(std::same_as< + decltype(std::ranges::take_view(std::move(br), 0)), + std::ranges::take_view, std::ranges::subrange_kind::unsized>> + >); +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/ctor.pass.cpp @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// take_view() requires default_initializable = default; +// constexpr take_view(V base, range_difference_t count); + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +template +struct DefaultConstructible : std::ranges::view_base { + DefaultConstructible() requires IsDefaultCtorable = default; + DefaultConstructible(int*); + int* begin(); + sentinel_wrapper end(); +}; + +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::ranges::take_view tv(CopyableView{buffer}, 0); + assert(tv.base().ptr_ == buffer); + assert(tv.begin() == tv.end()); // Checking we have correct size. + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 1); + assert(std::move(tv).base().ptr_ == buffer); + assert(std::ranges::next(tv.begin(), 1) == tv.end()); // Checking we have correct size. + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 2); + assert(tv.base().ptr_ == buffer); + assert(std::ranges::next(tv.begin(), 2) == tv.end()); // Checking we have correct size. + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/end.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/end.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 + +// constexpr auto end() requires (!simple-view) +// constexpr auto end() const requires range + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" +#include "types.h" + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 0); + assert(tv.end() == std::ranges::next(tv.begin(), 0)); + ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 1); + assert(tv.end() == std::ranges::next(tv.begin(), 1)); + ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 2); + assert(tv.end() == std::ranges::next(tv.begin(), 2)); + ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 3); + assert(tv.end() == std::ranges::next(tv.begin(), 3)); + ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + + // The type. + static_assert(!std::same_as); + static_assert(!std::same_as); + } + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 5); + assert(tv.end() == std::ranges::next(tv.begin(), 5)); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 6); + assert(tv.end() == std::ranges::next(tv.begin(), 6)); + } + + { + const std::ranges::take_view tv(CopyableView{buffer}, 7); + assert(tv.end() == std::ranges::next(tv.begin(), 7)); + } + + // Just to cover the case where count == 8. + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 8); + assert(tv.end() == std::ranges::next(tv.begin(), 8)); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/base.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + auto sw = sentinel_wrapper(buffer + 8); // Note: not 4, but that's OK. + + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end().base().base() == sw.base()); + ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end().base().base() == sw.base()); + ASSERT_SAME_TYPE(decltype(tv.end().base()), sentinel_wrapper); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/ctor.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 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + // Test the default ctor. + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(decltype(tv.end()){} == std::ranges::next(tv.begin(), 4)); + } + + { + std::ranges::take_view nonConst(ContiguousView{buffer}, 5); + const std::ranges::take_view tvConst(ContiguousView{buffer}, 5); + auto sent1 = nonConst.end(); + // Convert to const. Note, we cannot go the other way. + std::remove_cv_t sent2 = sent1; + + assert(sent1 == std::ranges::next(tvConst.begin(), 5)); + assert(sent2 == std::ranges::next(tvConst.begin(), 5)); + } + + { + std::ranges::take_view tv(CopyableView{buffer}, 6); + auto sw = sentinel_wrapper(buffer + 6); + using Sent = decltype(tv.end()); + Sent sent = Sent(sw); + assert(sent.base().base() == sw.base()); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/sentinel/eq.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// sentinel() = default; +// constexpr explicit sentinel(sentinel_t end); +// constexpr sentinel(sentinel s) +// requires Const && convertible_to, sentinel_t>; + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + { + const std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + assert(std::ranges::next(tv.begin(), 4) == tv.end()); + } + + { + std::ranges::take_view tv(ContiguousView{buffer}, 4); + assert(tv.end() == std::ranges::next(tv.begin(), 4)); + assert(std::ranges::next(tv.begin(), 4) == tv.end()); + } + } + + { + std::ranges::take_view tvNonConst(ContiguousView{buffer}, 4); + const std::ranges::take_view tvConst(ContiguousView{buffer}, 4); + assert(tvNonConst.end() == std::ranges::next(tvConst.begin(), 4)); + assert(std::ranges::next(tvConst.begin(), 4) == tvNonConst.end()); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/size.pass.cpp @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// 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 size() requires sized_range +// constexpr auto size() const requires sized_range + +#include +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" +#include "types.h" + +template +concept SizeEnabled = requires(const std::ranges::take_view& tv) { + tv.size(); +}; + +constexpr bool test() { + int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; + + { + static_assert( SizeEnabled); + static_assert(!SizeEnabled); + } + + { + std::ranges::take_view tv(SizedRandomAccessView{buffer}, 0); + assert(tv.size() == 0); + } + + { + const std::ranges::take_view tv(SizedRandomAccessView{buffer}, 2); + assert(tv.size() == 2); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 4); + assert(tv.size() == 4); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 6); + assert(tv.size() == 6); + } + + { + std::ranges::take_view tv(SizedForwardView{buffer}, 8); + assert(tv.size() == 8); + } + + { + const std::ranges::take_view tv(SizedForwardView{buffer}, 10); + assert(tv.size() == 8); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/ranges/range.adaptors/range.take/types.h b/libcxx/test/std/ranges/range.adaptors/range.take/types.h new file mode 100644 --- /dev/null +++ b/libcxx/test/std/ranges/range.adaptors/range.take/types.h @@ -0,0 +1,52 @@ +#ifndef TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H +#define TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H + +#include + +#include "test_macros.h" +#include "test_iterators.h" +#include "test_range.h" + +using ForwardIter = forward_iterator; +struct SizedForwardView : std::ranges::view_base { + int *ptr_; + constexpr SizedForwardView(int* ptr) : ptr_(ptr) {} + constexpr friend ForwardIter begin(SizedForwardView& view) { return ForwardIter(view.ptr_); } + constexpr friend ForwardIter begin(SizedForwardView const& view) { return ForwardIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedForwardView& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedForwardView const& view) { + return sentinel_wrapper{ForwardIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedForwardView a sized view. +constexpr auto operator-(sentinel_wrapper sent, ForwardIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(ForwardIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +using RandomAccessIter = random_access_iterator; +struct SizedRandomAccessView : std::ranges::view_base { + int *ptr_; + constexpr SizedRandomAccessView(int* ptr) : ptr_(ptr) {} + constexpr friend RandomAccessIter begin(SizedRandomAccessView& view) { return RandomAccessIter(view.ptr_); } + constexpr friend RandomAccessIter begin(SizedRandomAccessView const& view) { return RandomAccessIter(view.ptr_); } + constexpr friend sentinel_wrapper end(SizedRandomAccessView& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } + constexpr friend sentinel_wrapper end(SizedRandomAccessView const& view) { + return sentinel_wrapper{RandomAccessIter(view.ptr_ + 8)}; + } +}; +// Required to make SizedRandomAccessView a sized view. +constexpr auto operator-(sentinel_wrapper sent, RandomAccessIter iter) { + return sent.base().base() - iter.base(); +} +constexpr auto operator-(RandomAccessIter iter, sentinel_wrapper sent) { + return iter.base() - sent.base().base(); +} + +#endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_TAKE_TYPES_H 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 @@ -698,7 +698,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; diff --git a/libcxx/test/support/test_range.h b/libcxx/test/support/test_range.h --- a/libcxx/test/support/test_range.h +++ b/libcxx/test/support/test_range.h @@ -21,7 +21,6 @@ bool operator==(std::input_or_output_iterator auto const&) const; }; -// clang-format off template