Index: include/CMakeLists.txt =================================================================== --- include/CMakeLists.txt +++ include/CMakeLists.txt @@ -128,6 +128,7 @@ set setjmp.h shared_mutex + span sstream stack stdbool.h Index: include/module.modulemap =================================================================== --- include/module.modulemap +++ include/module.modulemap @@ -559,6 +559,10 @@ header "experimental/set" export * } + module span { + header "span" + export * + } module string { header "experimental/string" export * Index: include/span =================================================================== --- include/span +++ include/span @@ -0,0 +1,606 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// + +#ifndef _LIBCPP_SPAN +#define _LIBCPP_SPAN + +/* + span synopsis + +namespace std { + +// constants +inline constexpr ptrdiff_t dynamic_extent = -1; + +// [views.span], class template span +template + class span; + +// [span.comparison], span comparison operators +template + constexpr bool operator==(span l, span r); +template + constexpr bool operator!=(span l, span r); +template + constexpr bool operator<(span l, span r); +template + constexpr bool operator<=(span l, span r); +template + constexpr bool operator>(span l, span r); +template + constexpr bool operator>=(span l, span r); + +// [span.objectrep], views of object representation +template + span(sizeof(ElementType)) * Extent))> as_bytes(span s) noexcept; + +template + span< byte, ((Extent == dynamic_extent) ? dynamic_extent : + (static_cast(sizeof(ElementType)) * Extent))> as_writable_bytes(span s) noexcept; + + +namespace std { +template +class span { +public: + // constants and types + using element_type = ElementType; + using value_type = remove_cv_t; + using index_type = ptrdiff_t; + using difference_type = ptrdiff_t; + using pointer = element_type*; + using reference = element_type&; + using iterator = implementation-defined; + using const_iterator = implementation-defined; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + static constexpr index_type extent = Extent; + + // [span.cons], span constructors, copy, assignment, and destructor + constexpr span() noexcept; + constexpr span(pointer ptr, index_type count); + constexpr span(pointer firstElem, pointer lastElem); + template + constexpr span(element_type (&arr)[N]) noexcept; + template + constexpr span(array& arr) noexcept; + template + constexpr span(const array& arr) noexcept; + template + constexpr span(Container& cont); + template + constexpr span(const Container& cont); + constexpr span(const span& other) noexcept = default; + template + constexpr span(const span& s) noexcept; + ~span() noexcept = default; + constexpr span& operator=(const span& other) noexcept = default; + + // [span.sub], span subviews + template + constexpr span first() const; + template + constexpr span last() const; + template + constexpr span subspan() const; + + constexpr span first(index_type count) const; + constexpr span last(index_type count) const; + constexpr span subspan(index_type offset, index_type count = dynamic_extent) const; + + // [span.obs], span observers + constexpr index_type size() const noexcept; + constexpr index_type size_bytes() const noexcept; + constexpr bool empty() const noexcept; + + // [span.elem], span element access + constexpr reference operator[](index_type idx) const; + constexpr reference operator()(index_type idx) const; + constexpr pointer data() const noexcept; + + // [span.iterators], span iterator support + constexpr iterator begin() const noexcept; + constexpr iterator end() const noexcept; + constexpr const_iterator cbegin() const noexcept; + constexpr const_iterator cend() const noexcept; + constexpr reverse_iterator rbegin() const noexcept; + constexpr reverse_iterator rend() const noexcept; + constexpr const_reverse_iterator crbegin() const noexcept; + constexpr const_reverse_iterator crend() const noexcept; + +private: + pointer data_; // exposition only + index_type size_; // exposition only +}; + +template + span(T (&)[N]) -> span; + +template + span(array&) -> span; + +template + span(const array&) -> span; + +template + span(Container&) -> span; + +template + span(const Container&) -> span; + +} // namespace std + +*/ + +#include // for ptrdiff_t +#include // for iterators +#include // for array +#include // for remove_cv, etc +#include // for byte + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER > 17 + +inline constexpr ptrdiff_t dynamic_extent = -1; +template class span; + + +template +struct __is_span_impl : public false_type {}; + +template +struct __is_span_impl> : public true_type {}; + +template +struct __is_span : public __is_span_impl> {}; + +template +struct __is_std_array_impl : public false_type {}; + +template +struct __is_std_array_impl> : public true_type {}; + +template +struct __is_std_array : public __is_std_array_impl> {}; + +template +struct __is_span_compatible_container : public false_type {}; + +template +struct __is_span_compatible_container<_Tp, _ElementType, + void_t< + // is not a specialization of span + typename enable_if::value, nullptr_t>::type, + // is not a specialization of array + typename enable_if::value, nullptr_t>::type, + // is_array_v is false, + typename enable_if, nullptr_t>::type, + // data(cont) and size(cont) are well formed + decltype(data(declval<_Tp>())), + decltype(size(declval<_Tp>())), + // remove_pointer_t(*)[] is convertible to ElementType(*)[] + typename enable_if< + is_convertible_v()))>(*)[], + _ElementType(*)[]>, + nullptr_t>::type + >> + : public true_type {}; + + +template +class _LIBCPP_TEMPLATE_VIS span { +public: +// constants and types + using element_type = _Tp; + using value_type = remove_cv_t<_Tp>; + using index_type = ptrdiff_t; + using difference_type = ptrdiff_t; + using pointer = _Tp *; + using const_pointer = const _Tp *; // not in standard + using reference = _Tp &; + using const_reference = const _Tp &; // not in standard + using iterator = __wrap_iter; + using const_iterator = __wrap_iter; + using reverse_iterator = _VSTD::reverse_iterator; + using const_reverse_iterator = _VSTD::reverse_iterator; + + static constexpr index_type extent = _Extent; + static_assert (_Extent >= 0, "Can't have a span with an extent < 0"); + +// [span.cons], span constructors, copy, assignment, and destructor + _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} + { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); } + + constexpr span (const span&) noexcept = default; + constexpr span& operator=(const span&) noexcept = default; + + _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr} + { (void)__count; _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (ptr, len)"); } + _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f} + { (void)__l; _LIBCPP_ASSERT(_Extent == distance(__f, __l), "size mismatch in span's constructor (ptr, ptr)"); } + + _LIBCPP_INLINE_VISIBILITY constexpr span(element_type (&__arr)[_Extent]) noexcept : __data{__arr} {} + _LIBCPP_INLINE_VISIBILITY constexpr span( array& __arr) noexcept : __data{__arr.data()} {} + _LIBCPP_INLINE_VISIBILITY constexpr span(const array& __arr) noexcept : __data{__arr.data()} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span( _Container& __c, + enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr) + : __data{_VSTD::data(__c)} + { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (container))"); } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const _Container& __c, + enable_if_t<__is_span_compatible_container::value, nullptr_t> = nullptr) + : __data{_VSTD::data(__c)} + { _LIBCPP_ASSERT(_Extent == _VSTD::size(__c), "size mismatch in span's constructor (const container)"); } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const span<_OtherElementType, _Extent>& __other, + enable_if_t< + is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, + nullptr_t> = nullptr) + : __data{__other.data()} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const span<_OtherElementType, dynamic_extent>& __other, + enable_if_t< + is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, + nullptr_t> = nullptr) noexcept + : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); } + + +// ~span() noexcept = default; + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span first() const noexcept + { + static_assert(_Count >= 0, "Count must be >= 0 in span::first()"); + static_assert(_Count <= _Extent, "Count out of range in span::first()"); + return {data(), _Count}; + } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span last() const noexcept + { + static_assert(_Count >= 0, "Count must be >= 0 in span::last()"); + static_assert(_Count <= _Extent, "Count out of range in span::last()"); + return {data() + size() - _Count, _Count}; + } + + _LIBCPP_INLINE_VISIBILITY + constexpr span first(index_type __count) const noexcept + { + _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)"); + return {data(), __count}; + } + + _LIBCPP_INLINE_VISIBILITY + constexpr span last(index_type __count) const noexcept + { + _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)"); + return {data() + size() - __count, __count}; + } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr auto subspan() const noexcept + -> span + { + _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()"); + return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; + } + + + inline _LIBCPP_INLINE_VISIBILITY + constexpr span + subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept + { + _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); + if (__count == dynamic_extent) + return {data() + __offset, size() - __offset}; + _LIBCPP_ASSERT(__offset + __count <= size(), "count + offset out of range in span::subspan(offset, count)"); + return {data() + __offset, __count}; + } + + _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return _Extent; } + _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return _Extent * sizeof(element_type); } + _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return _Extent == 0; } + + _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept + { + _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span[] index out of bounds"); + return __data[__idx]; + } + + _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept + { + _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span() index out of bounds"); + return __data[__idx]; + } + + _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } + +// [span.iter], span iterator support + _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } + _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } + _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } + _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } + + _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept + { + pointer __p = __data; + __data = __other.__data; + __other.__data = __p; + } + + _LIBCPP_INLINE_VISIBILITY span __as_bytes() const noexcept + { return {reinterpret_cast(data()), size_bytes()}; } + + _LIBCPP_INLINE_VISIBILITY span __as_writeable_bytes() const noexcept + { return {reinterpret_cast(data()), size_bytes()}; } + +private: + pointer __data; + +}; + + +template +class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> { +private: + +public: +// constants and types + using element_type = _Tp; + using value_type = remove_cv_t<_Tp>; + using index_type = ptrdiff_t; + using difference_type = ptrdiff_t; + using pointer = _Tp *; + using const_pointer = const _Tp *; // not in standard + using reference = _Tp &; + using const_reference = const _Tp &; // not in standard + using iterator = __wrap_iter; + using const_iterator = __wrap_iter; + using reverse_iterator = _VSTD::reverse_iterator; + using const_reverse_iterator = _VSTD::reverse_iterator; + + static constexpr index_type extent = dynamic_extent; + +// [span.cons], span constructors, copy, assignment, and destructor + _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}, __size{0} {} + + constexpr span (const span&) noexcept = default; + constexpr span& operator=(const span&) noexcept = default; + + _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {} + _LIBCPP_INLINE_VISIBILITY constexpr span(pointer __f, pointer __l) : __data{__f}, __size{distance(__f, __l)} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(element_type (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(array& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const array& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span( _Container& __c, + enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, nullptr_t> = nullptr) + : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {} + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const _Container& __c, + enable_if_t<__is_span_compatible_container::value, nullptr_t> = nullptr) + : __data{_VSTD::data(__c)}, __size{(index_type) _VSTD::size(__c)} {} + + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span(const span<_OtherElementType, _OtherExtent>& __other, + enable_if_t< + is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, + nullptr_t> = nullptr) noexcept + : __data{__other.data()}, __size{__other.size()} {} + +// ~span() noexcept = default; + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span first() const noexcept + { + static_assert(_Count >= 0); + _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); + return {data(), _Count}; + } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span last() const noexcept + { + static_assert(_Count >= 0); + _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); + return {data() + size() - _Count, _Count}; + } + + _LIBCPP_INLINE_VISIBILITY + constexpr span first(index_type __count) const noexcept + { + _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::first(count)"); + return {data(), __count}; + } + + _LIBCPP_INLINE_VISIBILITY + constexpr span last (index_type __count) const noexcept + { + _LIBCPP_ASSERT(__count >= 0 && __count <= size(), "Count out of range in span::last(count)"); + return {data() + size() - __count, __count}; + } + + template + inline _LIBCPP_INLINE_VISIBILITY + constexpr span<_Tp, dynamic_extent> subspan() const noexcept + { + _LIBCPP_ASSERT(_Offset >= 0 && _Offset <= size(), "Offset out of range in span::subspan()"); + _LIBCPP_ASSERT(_Count == dynamic_extent || _Offset + _Count <= size(), "Count out of range in span::subspan()"); + return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; + } + + constexpr span + inline _LIBCPP_INLINE_VISIBILITY + subspan(index_type __offset, index_type __count = dynamic_extent) const noexcept + { + _LIBCPP_ASSERT( __offset >= 0 && __offset <= size(), "Offset out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT((__count >= 0 && __count <= size()) || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); + if (__count == dynamic_extent) + return {data() + __offset, size() - __offset}; + _LIBCPP_ASSERT(__offset + __count <= size(), "Offset + count out of range in span::subspan(offset, count)"); + return {data() + __offset, __count}; + } + + _LIBCPP_INLINE_VISIBILITY constexpr index_type size() const noexcept { return __size; } + _LIBCPP_INLINE_VISIBILITY constexpr index_type size_bytes() const noexcept { return __size * sizeof(element_type); } + _LIBCPP_INLINE_VISIBILITY constexpr bool empty() const noexcept { return __size == 0; } + + _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](index_type __idx) const noexcept + { + _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span[] index out of bounds"); + return __data[__idx]; + } + + _LIBCPP_INLINE_VISIBILITY constexpr reference operator()(index_type __idx) const noexcept + { + _LIBCPP_ASSERT(__idx >= 0 && __idx < size(), "span() index out of bounds"); + return __data[__idx]; + } + + _LIBCPP_INLINE_VISIBILITY constexpr pointer data() const noexcept { return __data; } + +// [span.iter], span iterator support + _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { return iterator(data()); } + _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { return iterator(data() + size()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cbegin() const noexcept { return const_iterator(data()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_iterator cend() const noexcept { return const_iterator(data() + size()); } + _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } + _LIBCPP_INLINE_VISIBILITY constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); } + _LIBCPP_INLINE_VISIBILITY constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); } + + _LIBCPP_INLINE_VISIBILITY constexpr void swap(span &__other) noexcept + { + pointer __p = __data; + __data = __other.__data; + __other.__data = __p; + + index_type __sz = __size; + __size = __other.__size; + __other.__size = __sz; + } + + _LIBCPP_INLINE_VISIBILITY span __as_bytes() const noexcept + { return {reinterpret_cast(data()), size_bytes()}; } + + _LIBCPP_INLINE_VISIBILITY span __as_writeable_bytes() const noexcept + { return {reinterpret_cast(data()), size_bytes()}; } + +private: + pointer __data; + index_type __size; +}; + +template + constexpr bool + operator==(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return equal(__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); } + +template + constexpr bool + operator!=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return !(__rhs == __lhs); } + +template + constexpr bool + operator< (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return lexicographical_compare (__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); } + +template + constexpr bool + operator<=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return !(__rhs < __lhs); } + +template + constexpr bool + operator> (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return __rhs < __lhs; } + +template + constexpr bool + operator>=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs) + { return !(__lhs < __rhs); } + +// as_bytes & as_writeable_bytes +template + auto as_bytes(span<_Tp, _Extent> __s) noexcept + -> decltype(__s.__as_bytes()) + { return __s.__as_bytes(); } + +template + auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept + -> typename enable_if, decltype(__s.__as_writeable_bytes())>::type + { return __s.__as_writeable_bytes(); } + +template + constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept + { __lhs.swap(__rhs); } + + +// Deduction guides +template + span(_Tp (&)[_Sz]) -> span<_Tp, _Sz>; + +template + span(array<_Tp, _Sz>&) -> span<_Tp, _Sz>; + +template + span(const array<_Tp, _Sz>&) -> span; + +template + span(_Container&) -> span; + +template + span(const _Container&) -> span; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 17 +#endif // _LIBCPP_SPAN Index: test/libcxx/double_include.sh.cpp =================================================================== --- test/libcxx/double_include.sh.cpp +++ test/libcxx/double_include.sh.cpp @@ -102,6 +102,7 @@ #ifndef _LIBCPP_HAS_NO_THREADS #include #endif +#include #include #include #include Index: test/std/containers/views/span.comparison/op.eq.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.eq.pass.cpp +++ test/std/containers/views/span.comparison/op.eq.pass.cpp @@ -0,0 +1,168 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator==(span l, span r); +// +// +// Effects: Equivalent to: return equal(l.begin(), l.end(), r.begin(), r.end()); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert( (csp0d == csp0d), ""); + static_assert( (csp0s == csp0s), ""); + static_assert( (csp0s == csp0d), ""); + static_assert( (csp0d == csp0s), ""); + + static_assert(!(csp0d == csp1d), ""); + static_assert(!(csp0s == csp1s), ""); + static_assert(!(csp0s == csp1d), ""); + static_assert(!(csp0d == csp1s), ""); + + static_assert( (csp1d == csp1s), ""); + static_assert( (csp1s == csp1d), ""); + + static_assert( (csp2d == csp3d), ""); + static_assert( (csp2s == csp3s), ""); + static_assert( (csp2d == csp3s), ""); + static_assert( (csp2s == csp3d), ""); + + static_assert( (csp2d == csp3d), ""); + static_assert( (csp2s == csp3s), ""); + static_assert( (csp2d == csp3s), ""); + static_assert( (csp2s == csp3d), ""); + + static_assert(!(csp2d == csp4d), ""); + static_assert(!(csp2s == csp4s), ""); + static_assert(!(csp2d == csp4s), ""); + static_assert(!(csp2s == csp4d), ""); + + static_assert(!(csp4d == csp2d), ""); + static_assert(!(csp4s == csp2s), ""); + static_assert(!(csp4d == csp2s), ""); + static_assert(!(csp4s == csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert( (sp0d == sp0d)); + assert( (sp0s == sp0s)); + assert( (sp0s == sp0d)); + assert( (sp0d == sp0s)); + + assert(!(sp0d == sp1d)); + assert(!(sp0s == sp1s)); + assert(!(sp0s == sp1d)); + assert(!(sp0d == sp1s)); + + assert( (sp1d == sp1s)); + assert( (sp1s == sp1d)); + + assert( (sp2d == sp3d)); + assert( (sp2s == sp3s)); + assert( (sp2d == sp3s)); + assert( (sp2s == sp3d)); + + assert( (sp2d == sp3d)); + assert( (sp2s == sp3s)); + assert( (sp2d == sp3s)); + assert( (sp2s == sp3d)); + + assert(!(sp2d == sp4d)); + assert(!(sp2s == sp4s)); + assert(!(sp2d == sp4s)); + assert(!(sp2s == sp4d)); + + assert(!(sp4d == sp2d)); + assert(!(sp4s == sp2s)); + assert(!(sp4d == sp2s)); + assert(!(sp4s == sp2d)); + +// cross type comparisons + assert( (csp0d == sp0d)); + assert( (csp0s == sp0s)); + assert( (csp0s == sp0d)); + assert( (csp0d == sp0s)); + + assert(!(csp0d == sp1d)); + assert(!(csp0s == sp1s)); + assert(!(csp0s == sp1d)); + assert(!(csp0d == sp1s)); + + assert( (csp1d == sp1s)); + assert( (csp1s == sp1d)); + + assert( (csp2d == sp3d)); + assert( (csp2s == sp3s)); + assert( (csp2d == sp3s)); + assert( (csp2s == sp3d)); + + assert( (csp2d == sp3d)); + assert( (csp2s == sp3s)); + assert( (csp2d == sp3s)); + assert( (csp2s == sp3d)); + + assert(!(csp2d == sp4d)); + assert(!(csp2s == sp4s)); + assert(!(csp2d == sp4s)); + assert(!(csp2s == sp4d)); + + assert(!(csp4d == sp2d)); + assert(!(csp4s == sp2s)); + assert(!(csp4d == sp2s)); + assert(!(csp4s == sp2d)); + +// More cross-type comparisons (int vs float) + static_assert(std::span{fArr1} == std::span{iArr1}, ""); + static_assert(std::span{iArr1} == std::span{fArr1}, ""); + assert(std::span{fArr2} == std::span{iArr2}); + assert(std::span{iArr2} == std::span{fArr2}); + + static_assert(!(std::span{iArr1, 9} == std::span{fArr1, 8}), ""); +} \ No newline at end of file Index: test/std/containers/views/span.comparison/op.ge.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.ge.pass.cpp +++ test/std/containers/views/span.comparison/op.ge.pass.cpp @@ -0,0 +1,153 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator>=(span l, span r); +// +// +// Effects: Equivalent to: return !(l < r); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert( (csp0d >= csp0d), ""); + static_assert( (csp0s >= csp0s), ""); + static_assert( (csp0s >= csp0d), ""); + static_assert( (csp0d >= csp0s), ""); + + static_assert(!(csp0d >= csp1d), ""); + static_assert(!(csp0s >= csp1s), ""); + static_assert(!(csp0s >= csp1d), ""); + static_assert(!(csp0d >= csp1s), ""); + + static_assert( (csp1d >= csp1s), ""); + static_assert( (csp1s >= csp1d), ""); + + static_assert( (csp2d >= csp3d), ""); + static_assert( (csp2s >= csp3s), ""); + static_assert( (csp2d >= csp3s), ""); + static_assert( (csp2s >= csp3d), ""); + + static_assert(!(csp2d >= csp4d), ""); + static_assert(!(csp2s >= csp4s), ""); + static_assert(!(csp2d >= csp4s), ""); + static_assert(!(csp2s >= csp4d), ""); + + static_assert( (csp4d >= csp2d), ""); + static_assert( (csp4s >= csp2s), ""); + static_assert( (csp4d >= csp2s), ""); + static_assert( (csp4s >= csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert( (sp0d >= sp0d)); + assert( (sp0s >= sp0s)); + assert( (sp0s >= sp0d)); + assert( (sp0d >= sp0s)); + + assert(!(sp0d >= sp1d)); + assert(!(sp0s >= sp1s)); + assert(!(sp0s >= sp1d)); + assert(!(sp0d >= sp1s)); + + assert( (sp1d >= sp1s)); + assert( (sp1s >= sp1d)); + + assert( (sp2d >= sp3d)); + assert( (sp2s >= sp3s)); + assert( (sp2d >= sp3s)); + assert( (sp2s >= sp3d)); + + assert(!(sp2d >= sp4d)); + assert(!(sp2s >= sp4s)); + assert(!(sp2d >= sp4s)); + assert(!(sp2s >= sp4d)); + + assert( (sp4d > sp2d)); + assert( (sp4s > sp2s)); + assert( (sp4d > sp2s)); + assert( (sp4s > sp2d)); + +// cross type comparisons + assert( (csp0d >= sp0d)); + assert( (csp0s >= sp0s)); + assert( (csp0s >= sp0d)); + assert( (csp0d >= sp0s)); + + assert(!(csp0d >= sp1d)); + assert(!(csp0s >= sp1s)); + assert(!(csp0s >= sp1d)); + assert(!(csp0d >= sp1s)); + + assert( (csp1d >= sp1s)); + assert( (csp1s >= sp1d)); + + assert( (csp2d >= sp3d)); + assert( (csp2s >= sp3s)); + assert( (csp2d >= sp3s)); + assert( (csp2s >= sp3d)); + + assert(!(csp2d >= sp4d)); + assert(!(csp2s >= sp4s)); + assert(!(csp2d >= sp4s)); + assert(!(csp2s >= sp4d)); + + assert( (csp4d > sp2d)); + assert( (csp4s > sp2s)); + assert( (csp4d > sp2s)); + assert( (csp4s > sp2d)); + +// More cross-type comparisons (int vs float) + static_assert(!(std::span{fArr1, 8} >= std::span{iArr1, 9}), ""); + static_assert(!(std::span{iArr1, 8} >= std::span{fArr1, 9}), ""); + assert( (std::span{fArr2} >= std::span{iArr2})); + assert( (std::span{iArr2} >= std::span{fArr2})); + + static_assert( (std::span{iArr1, 9} >= std::span{fArr1, 8}), ""); +} Index: test/std/containers/views/span.comparison/op.gt.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.gt.pass.cpp +++ test/std/containers/views/span.comparison/op.gt.pass.cpp @@ -0,0 +1,154 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator>(span l, span r); +// +// +// Effects: Equivalent to: return (r < l); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert(!(csp0d > csp0d), ""); + static_assert(!(csp0s > csp0s), ""); + static_assert(!(csp0s > csp0d), ""); + static_assert(!(csp0d > csp0s), ""); + + static_assert(!(csp0d > csp1d), ""); + static_assert(!(csp0s > csp1s), ""); + static_assert(!(csp0s > csp1d), ""); + static_assert(!(csp0d > csp1s), ""); + + static_assert(!(csp1d > csp1s), ""); + static_assert(!(csp1s > csp1d), ""); + + static_assert(!(csp2d > csp3d), ""); + static_assert(!(csp2s > csp3s), ""); + static_assert(!(csp2d > csp3s), ""); + static_assert(!(csp2s > csp3d), ""); + + static_assert(!(csp2d > csp4d), ""); + static_assert(!(csp2s > csp4s), ""); + static_assert(!(csp2d > csp4s), ""); + static_assert(!(csp2s > csp4d), ""); + + static_assert( (csp4d > csp2d), ""); + static_assert( (csp4s > csp2s), ""); + static_assert( (csp4d > csp2s), ""); + static_assert( (csp4s > csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert(!(sp0d > sp0d)); + assert(!(sp0s > sp0s)); + assert(!(sp0s > sp0d)); + assert(!(sp0d > sp0s)); + + assert(!(sp0d > sp1d)); + assert(!(sp0s > sp1s)); + assert(!(sp0s > sp1d)); + assert(!(sp0d > sp1s)); + + assert(!(sp1d > sp1s)); + assert(!(sp1s > sp1d)); + + assert(!(sp2d > sp3d)); + assert(!(sp2s > sp3s)); + assert(!(sp2d > sp3s)); + assert(!(sp2s > sp3d)); + + assert(!(sp2d > sp4d)); + assert(!(sp2s > sp4s)); + assert(!(sp2d > sp4s)); + assert(!(sp2s > sp4d)); + + assert( (sp4d > sp2d)); + assert( (sp4s > sp2s)); + assert( (sp4d > sp2s)); + assert( (sp4s > sp2d)); + +// cross type comparisons + assert(!(csp0d > sp0d)); + assert(!(csp0s > sp0s)); + assert(!(csp0s > sp0d)); + assert(!(csp0d > sp0s)); + + assert(!(csp0d > sp1d)); + assert(!(csp0s > sp1s)); + assert(!(csp0s > sp1d)); + assert(!(csp0d > sp1s)); + + assert(!(csp1d > sp1s)); + assert(!(csp1s > sp1d)); + + assert(!(csp2d > sp3d)); + assert(!(csp2s > sp3s)); + assert(!(csp2d > sp3s)); + assert(!(csp2s > sp3d)); + + assert(!(csp2d > sp4d)); + assert(!(csp2s > sp4s)); + assert(!(csp2d > sp4s)); + assert(!(csp2s > sp4d)); + + assert( (csp4d > sp2d)); + assert( (csp4s > sp2s)); + assert( (csp4d > sp2s)); + assert( (csp4s > sp2d)); + + +// More cross-type comparisons (int vs float) + static_assert(!(std::span{fArr1, 8} > std::span{iArr1, 9}), ""); + static_assert(!(std::span{iArr1, 8} > std::span{fArr1, 9}), ""); + assert(!(std::span{fArr2} > std::span{iArr2})); + assert(!(std::span{iArr2} > std::span{fArr2})); + + static_assert( (std::span{iArr1, 9} > std::span{fArr1, 8}), ""); +} \ No newline at end of file Index: test/std/containers/views/span.comparison/op.le.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.le.pass.cpp +++ test/std/containers/views/span.comparison/op.le.pass.cpp @@ -0,0 +1,153 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator<=(span l, span r); +// +// +// Effects: Equivalent to: return !(r < l); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert( (csp0d <= csp0d), ""); + static_assert( (csp0s <= csp0s), ""); + static_assert( (csp0s <= csp0d), ""); + static_assert( (csp0d <= csp0s), ""); + + static_assert( (csp0d <= csp1d), ""); + static_assert( (csp0s <= csp1s), ""); + static_assert( (csp0s <= csp1d), ""); + static_assert( (csp0d <= csp1s), ""); + + static_assert( (csp1d <= csp1s), ""); + static_assert( (csp1s <= csp1d), ""); + + static_assert( (csp2d <= csp3d), ""); + static_assert( (csp2s <= csp3s), ""); + static_assert( (csp2d <= csp3s), ""); + static_assert( (csp2s <= csp3d), ""); + + static_assert( (csp2d <= csp4d), ""); + static_assert( (csp2s <= csp4s), ""); + static_assert( (csp2d <= csp4s), ""); + static_assert( (csp2s <= csp4d), ""); + + static_assert(!(csp4d <= csp2d), ""); + static_assert(!(csp4s <= csp2s), ""); + static_assert(!(csp4d <= csp2s), ""); + static_assert(!(csp4s <= csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert( (sp0d <= sp0d)); + assert( (sp0s <= sp0s)); + assert( (sp0s <= sp0d)); + assert( (sp0d <= sp0s)); + + assert( (sp0d <= sp1d)); + assert( (sp0s <= sp1s)); + assert( (sp0s <= sp1d)); + assert( (sp0d <= sp1s)); + + assert( (sp1d <= sp1s)); + assert( (sp1s <= sp1d)); + + assert( (sp2d <= sp3d)); + assert( (sp2s <= sp3s)); + assert( (sp2d <= sp3s)); + assert( (sp2s <= sp3d)); + + assert( (sp2d <= sp4d)); + assert( (sp2s <= sp4s)); + assert( (sp2d <= sp4s)); + assert( (sp2s <= sp4d)); + + assert(!(sp4d <= sp2d)); + assert(!(sp4s <= sp2s)); + assert(!(sp4d <= sp2s)); + assert(!(sp4s <= sp2d)); + +// cross type comparisons + assert( (csp0d <= sp0d)); + assert( (csp0s <= sp0s)); + assert( (csp0s <= sp0d)); + assert( (csp0d <= sp0s)); + + assert( (csp0d <= sp1d)); + assert( (csp0s <= sp1s)); + assert( (csp0s <= sp1d)); + assert( (csp0d <= sp1s)); + + assert( (csp1d <= sp1s)); + assert( (csp1s <= sp1d)); + + assert( (csp2d <= sp3d)); + assert( (csp2s <= sp3s)); + assert( (csp2d <= sp3s)); + assert( (csp2s <= sp3d)); + + assert( (csp2d <= sp4d)); + assert( (csp2s <= sp4s)); + assert( (csp2d <= sp4s)); + assert( (csp2s <= sp4d)); + + assert(!(csp4d <= sp2d)); + assert(!(csp4s <= sp2s)); + assert(!(csp4d <= sp2s)); + assert(!(csp4s <= sp2d)); + +// More cross-type comparisons (int vs float) + static_assert(std::span{fArr1, 8} <= std::span{iArr1, 9}, ""); + static_assert(std::span{iArr1, 8} <= std::span{fArr1, 9}, ""); + assert( (std::span{fArr2} <= std::span{iArr2})); + assert( (std::span{iArr2} <= std::span{fArr2})); + + static_assert(!(std::span{iArr1, 9} <= std::span{fArr1, 8}), ""); +} \ No newline at end of file Index: test/std/containers/views/span.comparison/op.lt.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.lt.pass.cpp +++ test/std/containers/views/span.comparison/op.lt.pass.cpp @@ -0,0 +1,154 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator<(span l, span r); +// +// +// Effects: Equivalent to: +// return lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert(!(csp0d < csp0d), ""); + static_assert(!(csp0s < csp0s), ""); + static_assert(!(csp0s < csp0d), ""); + static_assert(!(csp0d < csp0s), ""); + + static_assert( (csp0d < csp1d), ""); + static_assert( (csp0s < csp1s), ""); + static_assert( (csp0s < csp1d), ""); + static_assert( (csp0d < csp1s), ""); + + static_assert(!(csp1d < csp1s), ""); + static_assert(!(csp1s < csp1d), ""); + + static_assert(!(csp2d < csp3d), ""); + static_assert(!(csp2s < csp3s), ""); + static_assert(!(csp2d < csp3s), ""); + static_assert(!(csp2s < csp3d), ""); + + static_assert( (csp2d < csp4d), ""); + static_assert( (csp2s < csp4s), ""); + static_assert( (csp2d < csp4s), ""); + static_assert( (csp2s < csp4d), ""); + + static_assert(!(csp4d < csp2d), ""); + static_assert(!(csp4s < csp2s), ""); + static_assert(!(csp4d < csp2s), ""); + static_assert(!(csp4s < csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert(!(sp0d < sp0d)); + assert(!(sp0s < sp0s)); + assert(!(sp0s < sp0d)); + assert(!(sp0d < sp0s)); + + assert( (sp0d < sp1d)); + assert( (sp0s < sp1s)); + assert( (sp0s < sp1d)); + assert( (sp0d < sp1s)); + + assert(!(sp1d < sp1s)); + assert(!(sp1s < sp1d)); + + assert(!(sp2d < sp3d)); + assert(!(sp2s < sp3s)); + assert(!(sp2d < sp3s)); + assert(!(sp2s < sp3d)); + + assert( (sp2d < sp4d)); + assert( (sp2s < sp4s)); + assert( (sp2d < sp4s)); + assert( (sp2s < sp4d)); + + assert(!(sp4d < sp2d)); + assert(!(sp4s < sp2s)); + assert(!(sp4d < sp2s)); + assert(!(sp4s < sp2d)); + +// cross type comparisons + assert(!(csp0d < sp0d)); + assert(!(csp0s < sp0s)); + assert(!(csp0s < sp0d)); + assert(!(csp0d < sp0s)); + + assert( (csp0d < sp1d)); + assert( (csp0s < sp1s)); + assert( (csp0s < sp1d)); + assert( (csp0d < sp1s)); + + assert(!(csp1d < sp1s)); + assert(!(csp1s < sp1d)); + + assert(!(csp2d < sp3d)); + assert(!(csp2s < sp3s)); + assert(!(csp2d < sp3s)); + assert(!(csp2s < sp3d)); + + assert( (csp2d < sp4d)); + assert( (csp2s < sp4s)); + assert( (csp2d < sp4s)); + assert( (csp2s < sp4d)); + + assert(!(csp4d < sp2d)); + assert(!(csp4s < sp2s)); + assert(!(csp4d < sp2s)); + assert(!(csp4s < sp2d)); + +// More cross-type comparisons (int vs float) + static_assert(std::span{fArr1, 8} < std::span{iArr1, 9}, ""); + static_assert(std::span{iArr1, 8} < std::span{fArr1, 9}, ""); + assert(!(std::span{fArr2} < std::span{iArr2})); + assert(!(std::span{iArr2} < std::span{fArr2})); + + static_assert(!(std::span{iArr1, 9} < std::span{fArr1, 8}), ""); +} \ No newline at end of file Index: test/std/containers/views/span.comparison/op.ne.pass.cpp =================================================================== --- test/std/containers/views/span.comparison/op.ne.pass.cpp +++ test/std/containers/views/span.comparison/op.ne.pass.cpp @@ -0,0 +1,168 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr bool operator!=(span l, span r); +// +// +// Effects: Equivalent to: return !(l == r); +// + +#include +#include + +#include "test_macros.h" + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; + int iArr2[] = { 0, 1, 2, 1, 2, 5, 6, 7, 8, 9}; +constexpr float fArr1[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + float fArr2[] = {0., 1., 2., 1., 2., 5., 6., 7., 8., 9.}; + + +int main () { + + constexpr std::span csp0d{}; + constexpr std::span csp1d{iArr1, 10}; + constexpr std::span csp2d{iArr1 + 3, 2}; + constexpr std::span csp3d{iArr1 + 1, 2}; + constexpr std::span csp4d{iArr1 + 6, 2}; + + constexpr std::span csp0s{}; + constexpr std::span csp1s{iArr1, 10}; + constexpr std::span csp2s{iArr1 + 3, 2}; + constexpr std::span csp3s{iArr1 + 1, 2}; + constexpr std::span csp4s{iArr1 + 6, 2}; + + static_assert(!(csp0d != csp0d), ""); + static_assert(!(csp0s != csp0s), ""); + static_assert(!(csp0s != csp0d), ""); + static_assert(!(csp0d != csp0s), ""); + + static_assert( (csp0d != csp1d), ""); + static_assert( (csp0s != csp1s), ""); + static_assert( (csp0s != csp1d), ""); + static_assert( (csp0d != csp1s), ""); + + static_assert(!(csp1d != csp1s), ""); + static_assert(!(csp1s != csp1d), ""); + + static_assert(!(csp2d != csp3d), ""); + static_assert(!(csp2s != csp3s), ""); + static_assert(!(csp2d != csp3s), ""); + static_assert(!(csp2s != csp3d), ""); + + static_assert(!(csp2d != csp3d), ""); + static_assert(!(csp2s != csp3s), ""); + static_assert(!(csp2d != csp3s), ""); + static_assert(!(csp2s != csp3d), ""); + + static_assert( (csp2d != csp4d), ""); + static_assert( (csp2s != csp4s), ""); + static_assert( (csp2d != csp4s), ""); + static_assert( (csp2s != csp4d), ""); + + static_assert( (csp4d != csp2d), ""); + static_assert( (csp4s != csp2s), ""); + static_assert( (csp4d != csp2s), ""); + static_assert( (csp4s != csp2d), ""); + + std::span sp0d{}; + std::span sp1d{iArr2, 10}; + std::span sp2d{iArr2 + 3, 2}; + std::span sp3d{iArr2 + 1, 2}; + std::span sp4d{iArr2 + 6, 2}; + + std::span sp0s{}; + std::span sp1s{iArr2, 10}; + std::span sp2s{iArr2 + 3, 2}; + std::span sp3s{iArr2 + 1, 2}; + std::span sp4s{iArr2 + 6, 2}; + + assert(!(sp0d != sp0d)); + assert(!(sp0s != sp0s)); + assert(!(sp0s != sp0d)); + assert(!(sp0d != sp0s)); + + assert( (sp0d != sp1d)); + assert( (sp0s != sp1s)); + assert( (sp0s != sp1d)); + assert( (sp0d != sp1s)); + + assert(!(sp1d != sp1s)); + assert(!(sp1s != sp1d)); + + assert(!(sp2d != sp3d)); + assert(!(sp2s != sp3s)); + assert(!(sp2d != sp3s)); + assert(!(sp2s != sp3d)); + + assert(!(sp2d != sp3d)); + assert(!(sp2s != sp3s)); + assert(!(sp2d != sp3s)); + assert(!(sp2s != sp3d)); + + assert( (sp2d != sp4d)); + assert( (sp2s != sp4s)); + assert( (sp2d != sp4s)); + assert( (sp2s != sp4d)); + + assert( (sp4d != sp2d)); + assert( (sp4s != sp2s)); + assert( (sp4d != sp2s)); + assert( (sp4s != sp2d)); + +// cross type comparisons + assert(!(csp0d != sp0d)); + assert(!(csp0s != sp0s)); + assert(!(csp0s != sp0d)); + assert(!(csp0d != sp0s)); + + assert( (csp0d != sp1d)); + assert( (csp0s != sp1s)); + assert( (csp0s != sp1d)); + assert( (csp0d != sp1s)); + + assert(!(csp1d != sp1s)); + assert(!(csp1s != sp1d)); + + assert(!(csp2d != sp3d)); + assert(!(csp2s != sp3s)); + assert(!(csp2d != sp3s)); + assert(!(csp2s != sp3d)); + + assert(!(csp2d != sp3d)); + assert(!(csp2s != sp3s)); + assert(!(csp2d != sp3s)); + assert(!(csp2s != sp3d)); + + assert( (csp2d != sp4d)); + assert( (csp2s != sp4s)); + assert( (csp2d != sp4s)); + assert( (csp2s != sp4d)); + + assert( (csp4d != sp2d)); + assert( (csp4s != sp2s)); + assert( (csp4d != sp2s)); + assert( (csp4s != sp2d)); + +// More cross-type comparisons (int vs float) + static_assert(!(std::span{fArr1} != std::span{iArr1}), ""); + static_assert(!(std::span{iArr1} != std::span{fArr1}), ""); + assert(!(std::span{fArr2} != std::span{iArr2})); + assert(!(std::span{iArr2} != std::span{fArr2})); + + static_assert( (std::span{iArr1, 9} != std::span{fArr1, 8}), ""); +} \ No newline at end of file Index: test/std/containers/views/span.cons/array.fail.cpp =================================================================== --- test/std/containers/views/span.cons/array.fail.cpp +++ test/std/containers/views/span.cons/array.fail.cpp @@ -0,0 +1,72 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(element_type (&arr)[N]) noexcept; +// template +// constexpr span(array& arr) noexcept; +// template +// constexpr span(const array& arr) noexcept; +// +// Remarks: These constructors shall not participate in overload resolution unless: +// — extent == dynamic_extent || N == extent is true, and +// — remove_pointer_t(*)[] is convertible to ElementType(*)[]. +// + + +#include +#include +#include + +#include "test_macros.h" + + int arr[] = {1,2,3}; +const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; +const volatile int cvarr[] = {1,3,5}; + +int main () +{ +// Size wrong + { + std::span s1(arr); // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Type wrong + { + std::span s1(arr); // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2(arr); // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (dynamically sized) + { + std::span< int> s1{ carr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{ varr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s6{ carr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (statically sized) + { + std::span< int,3> s1{ carr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s2{ varr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s3{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s6{ carr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s7{cvarr}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } +} Index: test/std/containers/views/span.cons/array.pass.cpp =================================================================== --- test/std/containers/views/span.cons/array.pass.cpp +++ test/std/containers/views/span.cons/array.pass.cpp @@ -0,0 +1,123 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(element_type (&arr)[N]) noexcept; +// +// Remarks: These constructors shall not participate in overload resolution unless: +// — extent == dynamic_extent || N == extent is true, and +// — remove_pointer_t(*)[] is convertible to ElementType(*)[]. +// + + +#include +#include +#include + +#include "test_macros.h" + + +void checkCV() +{ + int arr[] = {1,2,3}; + const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; + const volatile int cvarr[] = {1,3,5}; + +// Types the same (dynamic sized) + { + std::span< int> s1{ arr}; // a span< int> pointing at int. + std::span s2{ carr}; // a span pointing at const int. + std::span< volatile int> s3{ varr}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + +// Types the same (static sized) + { + std::span< int,3> s1{ arr}; // a span< int> pointing at int. + std::span s2{ carr}; // a span pointing at const int. + std::span< volatile int,3> s3{ varr}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + + +// types different (dynamic sized) + { + std::span s1{ arr}; // a span pointing at int. + std::span< volatile int> s2{ arr}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{ arr}; // a span< volatile int> pointing at const int. + std::span s4{ arr}; // a span pointing at int. + std::span s5{carr}; // a span pointing at const int. + std::span s6{varr}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } + +// types different (static sized) + { + std::span s1{ arr}; // a span pointing at int. + std::span< volatile int,3> s2{ arr}; // a span< volatile int> pointing at int. + std::span< volatile int,3> s3{ arr}; // a span< volatile int> pointing at const int. + std::span s4{ arr}; // a span pointing at int. + std::span s5{carr}; // a span pointing at const int. + std::span s6{varr}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr T val[2] = {}; + + ASSERT_NOEXCEPT(std::span {val}); + ASSERT_NOEXCEPT(std::span{val}); + std::span s1{val}; + std::span s2{val}; + return + s1.data() == &val[0] && s1.size() == 2 + && s2.data() == &val[0] && s2.size() == 2; +} + + +template +void testRuntimeSpan() +{ + T val[2] = {}; + ASSERT_NOEXCEPT(std::span {val}); + ASSERT_NOEXCEPT(std::span{val}); + std::span s1{val}; + std::span s2{val}; + assert(s1.data() == &val[0] && s1.size() == 2); + assert(s2.data() == &val[0] && s2.size() == 2); +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.cons/assign.pass.cpp =================================================================== --- test/std/containers/views/span.cons/assign.pass.cpp +++ test/std/containers/views/span.cons/assign.pass.cpp @@ -0,0 +1,293 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span& operator=(const span& other) noexcept = default; + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool doAssign(T lhs, T rhs) +{ + ASSERT_NOEXCEPT(std::declval() = rhs); + lhs = rhs; + return lhs.data() == rhs.data() + && lhs.size() == rhs.size(); +} + +struct A{}; + +constexpr int carr1[] = {1,2,3,4}; +constexpr int carr2[] = {3,4,5}; +constexpr int carr3[] = {7,8}; + int arr[] = {5,6,7,9}; +std::string strs[] = {"ABC", "DEF", "GHI"}; + + +int main () +{ + +// constexpr dynamically sized assignment + { +// On systems where 'ptrdiff_t' is a synonym for 'int', +// the call span(ptr, 0) selects the (pointer, index_type) constructor. +// On systems where 'ptrdiff_t' is NOT a synonym for 'int', +// it is ambiguous, because of 0 also being convertible to a null pointer +// and so the compiler can't choose between: +// span(pointer, index_type) +// and span(pointer, pointer) +// We cast zero to std::ptrdiff_t to remove that ambiguity. +// Example: +// On darwin x86_64, ptrdiff_t is the same as long int. +// On darwin i386, ptrdiff_t is the same as int. + constexpr std::span spans[] = { + {}, + {carr1, static_cast(0)}, + {carr1, 1}, + {carr1, 2}, + {carr1, 3}, + {carr1, 4}, + {carr2, static_cast(0)}, + {carr2, 1}, + {carr2, 2}, + {carr2, 3}, + {carr3, static_cast(0)}, + {carr3, 1}, + {carr3, 2} + }; + + static_assert(std::size(spans) == 13, "" ); + +// No for loops in constexpr land :-( + static_assert(doAssign(spans[0], spans[0]), ""); + static_assert(doAssign(spans[0], spans[1]), ""); + static_assert(doAssign(spans[0], spans[2]), ""); + static_assert(doAssign(spans[0], spans[3]), ""); + static_assert(doAssign(spans[0], spans[4]), ""); + static_assert(doAssign(spans[0], spans[5]), ""); + static_assert(doAssign(spans[0], spans[6]), ""); + static_assert(doAssign(spans[0], spans[7]), ""); + static_assert(doAssign(spans[0], spans[8]), ""); + static_assert(doAssign(spans[0], spans[9]), ""); + static_assert(doAssign(spans[0], spans[10]), ""); + static_assert(doAssign(spans[0], spans[11]), ""); + static_assert(doAssign(spans[0], spans[12]), ""); + + static_assert(doAssign(spans[1], spans[1]), ""); + static_assert(doAssign(spans[1], spans[2]), ""); + static_assert(doAssign(spans[1], spans[3]), ""); + static_assert(doAssign(spans[1], spans[4]), ""); + static_assert(doAssign(spans[1], spans[5]), ""); + static_assert(doAssign(spans[1], spans[6]), ""); + static_assert(doAssign(spans[1], spans[7]), ""); + static_assert(doAssign(spans[1], spans[8]), ""); + static_assert(doAssign(spans[1], spans[9]), ""); + static_assert(doAssign(spans[1], spans[10]), ""); + static_assert(doAssign(spans[1], spans[11]), ""); + static_assert(doAssign(spans[1], spans[12]), ""); + + static_assert(doAssign(spans[2], spans[2]), ""); + static_assert(doAssign(spans[2], spans[3]), ""); + static_assert(doAssign(spans[2], spans[4]), ""); + static_assert(doAssign(spans[2], spans[5]), ""); + static_assert(doAssign(spans[2], spans[6]), ""); + static_assert(doAssign(spans[2], spans[7]), ""); + static_assert(doAssign(spans[2], spans[8]), ""); + static_assert(doAssign(spans[2], spans[9]), ""); + static_assert(doAssign(spans[2], spans[10]), ""); + static_assert(doAssign(spans[2], spans[11]), ""); + static_assert(doAssign(spans[2], spans[12]), ""); + + static_assert(doAssign(spans[3], spans[3]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[10]), ""); + static_assert(doAssign(spans[3], spans[11]), ""); + static_assert(doAssign(spans[3], spans[12]), ""); + + static_assert(doAssign(spans[4], spans[4]), ""); + static_assert(doAssign(spans[4], spans[5]), ""); + static_assert(doAssign(spans[4], spans[6]), ""); + static_assert(doAssign(spans[4], spans[7]), ""); + static_assert(doAssign(spans[4], spans[8]), ""); + static_assert(doAssign(spans[4], spans[9]), ""); + static_assert(doAssign(spans[4], spans[10]), ""); + static_assert(doAssign(spans[4], spans[11]), ""); + static_assert(doAssign(spans[4], spans[12]), ""); + + static_assert(doAssign(spans[5], spans[5]), ""); + static_assert(doAssign(spans[5], spans[6]), ""); + static_assert(doAssign(spans[5], spans[7]), ""); + static_assert(doAssign(spans[5], spans[8]), ""); + static_assert(doAssign(spans[5], spans[9]), ""); + static_assert(doAssign(spans[5], spans[10]), ""); + static_assert(doAssign(spans[5], spans[11]), ""); + static_assert(doAssign(spans[5], spans[12]), ""); + + static_assert(doAssign(spans[6], spans[6]), ""); + static_assert(doAssign(spans[6], spans[7]), ""); + static_assert(doAssign(spans[6], spans[8]), ""); + static_assert(doAssign(spans[6], spans[9]), ""); + static_assert(doAssign(spans[6], spans[10]), ""); + static_assert(doAssign(spans[6], spans[11]), ""); + static_assert(doAssign(spans[6], spans[12]), ""); + + static_assert(doAssign(spans[7], spans[7]), ""); + static_assert(doAssign(spans[7], spans[8]), ""); + static_assert(doAssign(spans[7], spans[9]), ""); + static_assert(doAssign(spans[7], spans[10]), ""); + static_assert(doAssign(spans[7], spans[11]), ""); + static_assert(doAssign(spans[7], spans[12]), ""); + + static_assert(doAssign(spans[8], spans[8]), ""); + static_assert(doAssign(spans[8], spans[9]), ""); + static_assert(doAssign(spans[8], spans[10]), ""); + static_assert(doAssign(spans[8], spans[11]), ""); + static_assert(doAssign(spans[8], spans[12]), ""); + + static_assert(doAssign(spans[9], spans[9]), ""); + static_assert(doAssign(spans[9], spans[10]), ""); + static_assert(doAssign(spans[9], spans[11]), ""); + static_assert(doAssign(spans[9], spans[12]), ""); + + static_assert(doAssign(spans[10], spans[10]), ""); + static_assert(doAssign(spans[10], spans[11]), ""); + static_assert(doAssign(spans[10], spans[12]), ""); + + static_assert(doAssign(spans[11], spans[11]), ""); + static_assert(doAssign(spans[11], spans[12]), ""); + + static_assert(doAssign(spans[12], spans[12]), ""); + +// for (size_t i = 0; i < std::size(spans); ++i) +// for (size_t j = i; j < std::size(spans); ++j) +// static_assert(doAssign(spans[i], spans[j]), ""); + } + +// constexpr statically sized assignment + { + constexpr std::span spans[] = { + {carr1, 2}, + {carr1 + 1, 2}, + {carr1 + 2, 2}, + {carr2, 2}, + {carr2 + 1, 2}, + {carr3, 2} + }; + + static_assert(std::size(spans) == 6, "" ); + +// No for loops in constexpr land :-( + static_assert(doAssign(spans[0], spans[0]), ""); + static_assert(doAssign(spans[0], spans[1]), ""); + static_assert(doAssign(spans[0], spans[2]), ""); + static_assert(doAssign(spans[0], spans[3]), ""); + static_assert(doAssign(spans[0], spans[4]), ""); + static_assert(doAssign(spans[0], spans[5]), ""); + + static_assert(doAssign(spans[1], spans[1]), ""); + static_assert(doAssign(spans[1], spans[2]), ""); + static_assert(doAssign(spans[1], spans[3]), ""); + static_assert(doAssign(spans[1], spans[4]), ""); + static_assert(doAssign(spans[1], spans[5]), ""); + + static_assert(doAssign(spans[2], spans[2]), ""); + static_assert(doAssign(spans[2], spans[3]), ""); + static_assert(doAssign(spans[2], spans[4]), ""); + static_assert(doAssign(spans[2], spans[5]), ""); + + static_assert(doAssign(spans[3], spans[3]), ""); + static_assert(doAssign(spans[3], spans[4]), ""); + static_assert(doAssign(spans[3], spans[5]), ""); + + static_assert(doAssign(spans[4], spans[4]), ""); + static_assert(doAssign(spans[4], spans[5]), ""); + + static_assert(doAssign(spans[5], spans[5]), ""); + +// for (size_t i = 0; i < std::size(spans); ++i) +// for (size_t j = i; j < std::size(spans); ++j) +// static_assert(doAssign(spans[i], spans[j]), ""); + } + + +// dynamically sized assignment + { + std::span spans[] = { + {}, + {arr, arr + 1}, + {arr, arr + 2}, + {arr, arr + 3}, + {arr + 1, arr + 3} // same size as s2 + }; + + for (size_t i = 0; i < std::size(spans); ++i) + for (size_t j = i; j < std::size(spans); ++j) + assert((doAssign(spans[i], spans[j]))); + } + +// statically sized assignment + { + std::span spans[] = { + {arr, arr + 2}, + {arr + 1, arr + 3}, + {arr + 2, arr + 4} + }; + + for (size_t i = 0; i < std::size(spans); ++i) + for (size_t j = i; j < std::size(spans); ++j) + assert((doAssign(spans[i], spans[j]))); + } + +// dynamically sized assignment + { + std::span spans[] = { + {strs, strs}, + {strs, strs + 1}, + {strs, strs + 2}, + {strs, strs + 3}, + {strs + 1, strs + 1}, + {strs + 1, strs + 2}, + {strs + 1, strs + 3}, + {strs + 2, strs + 2}, + {strs + 2, strs + 3}, + {strs + 3, strs + 3} + }; + + for (size_t i = 0; i < std::size(spans); ++i) + for (size_t j = i; j < std::size(spans); ++j) + assert((doAssign(spans[i], spans[j]))); + } + + { + std::span spans[] = { + {strs, strs + 1}, + {strs + 1, strs + 2}, + {strs + 2, strs + 3} + }; + + for (size_t i = 0; i < std::size(spans); ++i) + for (size_t j = i; j < std::size(spans); ++j) + assert((doAssign(spans[i], spans[j]))); + } +} Index: test/std/containers/views/span.cons/container.fail.cpp =================================================================== --- test/std/containers/views/span.cons/container.fail.cpp +++ test/std/containers/views/span.cons/container.fail.cpp @@ -0,0 +1,117 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(Container& cont); +// template +// constexpr span(const Container& cont); +// +// Remarks: These constructors shall not participate in overload resolution unless: +// — Container is not a specialization of span, +// — Container is not a specialization of array, +// — is_array_v is false, +// — data(cont) and size(cont) are both well-formed, and +// — remove_pointer_t(*)[] is convertible to ElementType(*)[]. +// + + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +// Look ma - I'm a container! +template +struct IsAContainer { + constexpr IsAContainer() : v_{} {} + constexpr size_t size() const {return 1;} + constexpr T *data() {return &v_;} + constexpr const T *data() const {return &v_;} + + constexpr const T *getV() const {return &v_;} // for checking + T v_; +}; + +template +struct NotAContainerNoData { + size_t size() const {return 0;} +}; + +template +struct NotAContainerNoSize { + const T *data() const {return nullptr;} +}; + +template +struct NotAContainerPrivate { +private: + size_t size() const {return 0;} + const T *data() const {return nullptr;} +}; + + +int main () +{ + +// Missing size and/or data + { + std::span s1{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s3{NotAContainerNoData()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{NotAContainerNoData()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{NotAContainerNoSize()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s6{NotAContainerNoSize()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s7{NotAContainerPrivate()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s8{NotAContainerPrivate()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + +// Again with the standard containers + std::span s11{std::deque()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s12{std::deque()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s13{std::list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s14{std::list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s15{std::forward_list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s16{std::forward_list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Not the same type + { + std::span s1{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (dynamically sized) + { + std::span< int> s1{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{IsAContainer< volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{IsAContainer< volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s6{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (statically sized) + { + std::span< int,1> s1{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,1> s2{IsAContainer< volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,1> s3{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{IsAContainer< volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,1> s6{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,1> s7{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +} Index: test/std/containers/views/span.cons/container.pass.cpp =================================================================== --- test/std/containers/views/span.cons/container.pass.cpp +++ test/std/containers/views/span.cons/container.pass.cpp @@ -0,0 +1,118 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(Container& cont); +// template +// constexpr span(const Container& cont); +// +// Remarks: These constructors shall not participate in overload resolution unless: +// — Container is not a specialization of span, +// — Container is not a specialization of array, +// — is_array_v is false, +// — data(cont) and size(cont) are both well-formed, and +// — remove_pointer_t(*)[] is convertible to ElementType(*)[]. +// + + +#include +#include +#include +#include + +#include "test_macros.h" + +// Look ma - I'm a container! +template +struct IsAContainer { + constexpr IsAContainer() : v_{} {} + constexpr size_t size() const {return 1;} + constexpr T *data() {return &v_;} + constexpr const T *data() const {return &v_;} + + constexpr T const *getV() const {return &v_;} // for checking + T v_; +}; + + +void checkCV() +{ + std::vector v = {1,2,3}; + +// Types the same (dynamic sized) + { + std::span< int> s1{v}; // a span< int> pointing at int. + } + +// Types the same (static sized) + { + std::span< int,3> s1{v}; // a span< int> pointing at int. + } + +// types different (dynamic sized) + { + std::span s1{v}; // a span pointing at int. + std::span< volatile int> s2{v}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{v}; // a span< volatile int> pointing at const int. + std::span s4{v}; // a span pointing at int. + } + +// types different (static sized) + { + std::span s1{v}; // a span pointing at int. + std::span< volatile int,3> s2{v}; // a span< volatile int> pointing at int. + std::span< volatile int,3> s3{v}; // a span< volatile int> pointing at const int. + std::span s4{v}; // a span pointing at int. + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr IsAContainer val{}; + std::span s1{val}; + std::span s2{val}; + return + s1.data() == val.getV() && s1.size() == 1 + && s2.data() == val.getV() && s2.size() == 1; +} + + +template +void testRuntimeSpan() +{ + IsAContainer val{}; + std::span s1{val}; + std::span s2{val}; + assert(s1.data() == val.getV() && s1.size() == 1); + assert(s2.data() == val.getV() && s2.size() == 1); +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.cons/copy.pass.cpp =================================================================== --- test/std/containers/views/span.cons/copy.pass.cpp +++ test/std/containers/views/span.cons/copy.pass.cpp @@ -0,0 +1,71 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span(const span& other) noexcept = default; + +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool doCopy(const T &rhs) +{ + ASSERT_NOEXCEPT(T{rhs}); + T lhs{rhs}; + return lhs.data() == rhs.data() + && lhs.size() == rhs.size(); +} + +struct A{}; + +template +void testCV () +{ + int arr[] = {1,2,3}; + assert((doCopy(std::span () ))); + assert((doCopy(std::span() ))); + assert((doCopy(std::span (&arr[0], 1)))); + assert((doCopy(std::span(&arr[0], 1)))); + assert((doCopy(std::span (&arr[0], 2)))); + assert((doCopy(std::span(&arr[0], 2)))); +} + + +int main () +{ + constexpr int carr[] = {1,2,3}; + + static_assert(doCopy(std::span< int> ()), ""); + static_assert(doCopy(std::span< int,0>()), ""); + static_assert(doCopy(std::span (&carr[0], 1)), ""); + static_assert(doCopy(std::span(&carr[0], 1)), ""); + static_assert(doCopy(std::span (&carr[0], 2)), ""); + static_assert(doCopy(std::span(&carr[0], 2)), ""); + + static_assert(doCopy(std::span()), ""); + static_assert(doCopy(std::span()), ""); + static_assert(doCopy(std::span()), ""); + + std::string s; + assert(doCopy(std::span () )); + assert(doCopy(std::span() )); + assert(doCopy(std::span (&s, 1))); + assert(doCopy(std::span(&s, 1))); + + testCV< int>(); + testCV(); + testCV< volatile int>(); + testCV(); +} Index: test/std/containers/views/span.cons/deduct.pass.cpp =================================================================== --- test/std/containers/views/span.cons/deduct.pass.cpp +++ test/std/containers/views/span.cons/deduct.pass.cpp @@ -0,0 +1,83 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// span(T (&)[N]) -> span; +// +// template +// span(array&) -> span; +// +// template +// span(const array&) -> span; +// +// template +// span(Container&) -> span; +// +// template +// span(const Container&) -> span; + + + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +int main () +{ + { + int arr[] = {1,2,3}; + std::span s{arr}; + using S = decltype(s); + ASSERT_SAME_TYPE(S, std::span); + assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end()))); + } + + { + std::array arr = {1.0, 2.0, 3.0, 4.0}; + std::span s{arr}; + using S = decltype(s); + ASSERT_SAME_TYPE(S, std::span); + assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end()))); + } + + { + const std::array arr = {4, 5, 6, 7, 8}; + std::span s{arr}; + using S = decltype(s); + ASSERT_SAME_TYPE(S, std::span); + assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end()))); + } + + { + std::string str{"ABCDE"}; + std::span s{str}; + using S = decltype(s); + ASSERT_SAME_TYPE(S, std::span); + assert((size_t)s.size() == str.size()); + assert((std::equal(s.begin(), s.end(), std::begin(s), std::end(s)))); + } + + { + const std::string str{"QWERTYUIOP"}; + std::span s{str}; + using S = decltype(s); + ASSERT_SAME_TYPE(S, std::span); + assert((size_t)s.size() == str.size()); + assert((std::equal(s.begin(), s.end(), std::begin(s), std::end(s)))); + } +} Index: test/std/containers/views/span.cons/default.fail.cpp =================================================================== --- test/std/containers/views/span.cons/default.fail.cpp +++ test/std/containers/views/span.cons/default.fail.cpp @@ -0,0 +1,32 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span() noexcept; +// +// Remarks: This constructor shall not participate in overload resolution +// unless Extent <= 0 is true. + + +#include +#include +#include + +#include "test_macros.h" + +int main () +{ + std::span s; // expected-error@span:* {{static_assert failed "Can't default construct a statically sized span with size > 0"}} + +// TODO: This is what I want: +// eXpected-error {{no matching constructor for initialization of 'std::span'}} +} Index: test/std/containers/views/span.cons/default.pass.cpp =================================================================== --- test/std/containers/views/span.cons/default.pass.cpp +++ test/std/containers/views/span.cons/default.pass.cpp @@ -0,0 +1,82 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span() noexcept; + +#include +#include +#include + +#include "test_macros.h" + +void checkCV() +{ +// Types the same (dynamic sized) + { + std::span< int> s1; + std::span s2; + std::span< volatile int> s3; + std::span s4; + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } + +// Types the same (static sized) + { + std::span< int,0> s1; + std::span s2; + std::span< volatile int,0> s3; + std::span s4; + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } +} + + +template +constexpr bool testConstexprSpan() +{ + std::span s1; + std::span s2; + return + s1.data() == nullptr && s1.size() == 0 + && s2.data() == nullptr && s2.size() == 0; +} + + +template +void testRuntimeSpan() +{ + ASSERT_NOEXCEPT(T{}); + std::span s1; + std::span s2; + assert(s1.data() == nullptr && s1.size() == 0); + assert(s2.data() == nullptr && s2.size() == 0); +} + + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.cons/ptr_len.fail.cpp =================================================================== --- test/std/containers/views/span.cons/ptr_len.fail.cpp +++ test/std/containers/views/span.cons/ptr_len.fail.cpp @@ -0,0 +1,63 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span(pointer ptr, index_type count); +// Requires: [ptr, ptr + count) shall be a valid range. +// If extent is not equal to dynamic_extent, then count shall be equal to extent. +// + +#include +#include +#include + +#include "test_macros.h" + + + int arr[] = {1,2,3}; +const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; +const volatile int cvarr[] = {1,3,5}; + +int main () +{ +// We can't check that the size doesn't match - because that's a runtime property +// std::span s1(arr, 3); + +// Type wrong + { + std::span s1(arr, 3); // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2(arr, 3); // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (dynamically sized) + { + std::span< int> s1{ carr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{ varr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s6{ carr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (statically sized) + { + std::span< int,3> s1{ carr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s2{ varr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s3{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s6{ carr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s7{cvarr, 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } +} Index: test/std/containers/views/span.cons/ptr_len.pass.cpp =================================================================== --- test/std/containers/views/span.cons/ptr_len.pass.cpp +++ test/std/containers/views/span.cons/ptr_len.pass.cpp @@ -0,0 +1,113 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span(pointer ptr, index_type count); +// Requires: [ptr, ptr + count) shall be a valid range. +// If extent is not equal to dynamic_extent, then count shall be equal to extent. +// + +#include +#include +#include + +#include "test_macros.h" + +void checkCV() +{ + int arr[] = {1,2,3}; + const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; + const volatile int cvarr[] = {1,3,5}; + +// Types the same (dynamic sized) + { + std::span< int> s1{ arr, 3}; // a span< int> pointing at int. + std::span s2{ carr, 3}; // a span pointing at const int. + std::span< volatile int> s3{ varr, 3}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr, 3}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + +// Types the same (static sized) + { + std::span< int,3> s1{ arr, 3}; // a span< int> pointing at int. + std::span s2{ carr, 3}; // a span pointing at const int. + std::span< volatile int,3> s3{ varr, 3}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr, 3}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + + +// types different (dynamic sized) + { + std::span s1{ arr, 3}; // a span pointing at int. + std::span< volatile int> s2{ arr, 3}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{ arr, 3}; // a span< volatile int> pointing at const int. + std::span s4{ arr, 3}; // a span pointing at int. + std::span s5{carr, 3}; // a span pointing at const int. + std::span s6{varr, 3}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } + +// types different (static sized) + { + std::span s1{ arr, 3}; // a span pointing at int. + std::span< volatile int,3> s2{ arr, 3}; // a span< volatile int> pointing at int. + std::span< volatile int,3> s3{ arr, 3}; // a span< volatile int> pointing at const int. + std::span s4{ arr, 3}; // a span pointing at int. + std::span s5{carr, 3}; // a span pointing at const int. + std::span s6{varr, 3}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr T val[2] = {}; + std::span s1{val, 2}; + std::span s2{val, 2}; + return + s1.data() == &val[0] && s1.size() == 2 + && s2.data() == &val[0] && s2.size() == 2; +} + + +template +void testRuntimeSpan() +{ + T val[2] = {}; + std::span s1{val, 2}; + std::span s2{val, 2}; + assert(s1.data() == &val[0] && s1.size() == 2); + assert(s2.data() == &val[0] && s2.size() == 2); +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.cons/ptr_ptr.fail.cpp =================================================================== --- test/std/containers/views/span.cons/ptr_ptr.fail.cpp +++ test/std/containers/views/span.cons/ptr_ptr.fail.cpp @@ -0,0 +1,63 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span(pointer first, pointer last); +// Requires: [first, last) shall be a valid range. +// If extent is not equal to dynamic_extent, then last - first shall be equal to extent. +// + +#include +#include +#include + +#include "test_macros.h" + + + int arr[] = {1,2,3}; +const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; +const volatile int cvarr[] = {1,3,5}; + +int main () +{ +// We can't check that the size doesn't match - because that's a runtime property +// std::span s1(arr, arr + 3); + +// Type wrong + { + std::span s1(arr, arr + 3); // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2(arr, arr + 3); // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (dynamically sized) + { + std::span< int> s1{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s6{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong (statically sized) + { + std::span< int,3> s1{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s2{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int,3> s3{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s6{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int,3> s7{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } +} Index: test/std/containers/views/span.cons/ptr_ptr.pass.cpp =================================================================== --- test/std/containers/views/span.cons/ptr_ptr.pass.cpp +++ test/std/containers/views/span.cons/ptr_ptr.pass.cpp @@ -0,0 +1,113 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr span(pointer first, pointer last); +// Requires: [first, last) shall be a valid range. +// If extent is not equal to dynamic_extent, then last - first shall be equal to extent. +// + +#include +#include +#include + +#include "test_macros.h" + +void checkCV() +{ + int arr[] = {1,2,3}; + const int carr[] = {4,5,6}; + volatile int varr[] = {7,8,9}; + const volatile int cvarr[] = {1,3,5}; + +// Types the same (dynamic sized) + { + std::span< int> s1{ arr, arr + 3}; // a span< int> pointing at int. + std::span s2{ carr, carr + 3}; // a span pointing at const int. + std::span< volatile int> s3{ varr, varr + 3}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr, cvarr + 3}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + +// Types the same (static sized) + { + std::span< int,3> s1{ arr, arr + 3}; // a span< int> pointing at int. + std::span s2{ carr, carr + 3}; // a span pointing at const int. + std::span< volatile int,3> s3{ varr, varr + 3}; // a span< volatile int> pointing at volatile int. + std::span s4{cvarr, cvarr + 3}; // a span pointing at const volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 12); + } + + +// types different (dynamic sized) + { + std::span s1{ arr, arr + 3}; // a span pointing at int. + std::span< volatile int> s2{ arr, arr + 3}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{ arr, arr + 3}; // a span< volatile int> pointing at const int. + std::span s4{ arr, arr + 3}; // a span pointing at int. + std::span s5{carr, carr + 3}; // a span pointing at const int. + std::span s6{varr, varr + 3}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } + +// types different (static sized) + { + std::span s1{ arr, arr + 3}; // a span pointing at int. + std::span< volatile int,3> s2{ arr, arr + 3}; // a span< volatile int> pointing at int. + std::span< volatile int,3> s3{ arr, arr + 3}; // a span< volatile int> pointing at const int. + std::span s4{ arr, arr + 3}; // a span pointing at int. + std::span s5{carr, carr + 3}; // a span pointing at const int. + std::span s6{varr, varr + 3}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18); + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr T val[2] = {}; + std::span s1{val, val+2}; + std::span s2{val, val+2}; + return + s1.data() == &val[0] && s1.size() == 2 + && s2.data() == &val[0] && s2.size() == 2; +} + + +template +void testRuntimeSpan() +{ + T val[2] = {}; + std::span s1{val, val+2}; + std::span s2{val, val+2}; + assert(s1.data() == &val[0] && s1.size() == 2); + assert(s2.data() == &val[0] && s2.size() == 2); +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.cons/span.fail.cpp =================================================================== --- test/std/containers/views/span.cons/span.fail.cpp +++ test/std/containers/views/span.cons/span.fail.cpp @@ -0,0 +1,104 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(const span& s) noexcept; +// +// Remarks: This constructor shall not participate in overload resolution unless: +// Extent == dynamic_extent || Extent == OtherExtent is true, and +// OtherElementType(*)[] is convertible to ElementType(*)[]. + + +#include +#include +#include + +#include "test_macros.h" + +void checkCV () +{ +// std::span< int> sp; + std::span csp; + std::span< volatile int> vsp; + std::span cvsp; + +// std::span< int, 0> sp0; + std::span csp0; + std::span< volatile int, 0> vsp0; + std::span cvsp0; + +// Try to remove const and/or volatile (dynamic -> dynamic) + { + std::span< int> s1{ csp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{ vsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span s4{ vsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span< volatile int> s6{ csp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Try to remove const and/or volatile (static -> static) + { + std::span< int, 0> s1{ csp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int, 0> s2{ vsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int, 0> s3{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span s4{ vsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span< volatile int, 0> s6{ csp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int, 0> s7{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Try to remove const and/or volatile (static -> dynamic) + { + std::span< int> s1{ csp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{ vsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span s4{ vsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span< volatile int> s6{ csp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cvsp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Try to remove const and/or volatile (static -> static) + { + std::span< int, 0> s1{ csp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int, 0> s2{ vsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int, 0> s3{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span s4{ vsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + std::span< volatile int, 0> s6{ csp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int, 0> s7{cvsp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } +} + +int main () +{ + std::span sp; + std::span sp0; + + std::span s1{sp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s2{sp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s3{sp}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{sp0}; // expected-error {{no matching constructor for initialization of 'std::span'}} + + checkCV(); +} Index: test/std/containers/views/span.cons/span.pass.cpp =================================================================== --- test/std/containers/views/span.cons/span.pass.cpp +++ test/std/containers/views/span.cons/span.pass.cpp @@ -0,0 +1,142 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(const span& s) noexcept; +// +// Remarks: This constructor shall not participate in overload resolution unless: +// Extent == dynamic_extent || Extent == OtherExtent is true, and +// OtherElementType(*)[] is convertible to ElementType(*)[]. + + +#include +#include +#include + +#include "test_macros.h" + +void checkCV() +{ + std::span< int> sp; +// std::span csp; + std::span< volatile int> vsp; +// std::span cvsp; + + std::span< int, 0> sp0; +// std::span csp0; + std::span< volatile int, 0> vsp0; +// std::span cvsp0; + +// dynamic -> dynamic + { + std::span s1{ sp}; // a span pointing at int. + std::span< volatile int> s2{ sp}; // a span< volatile int> pointing at int. + std::span s3{ sp}; // a span pointing at int. + std::span s4{ vsp}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } + +// static -> static + { + std::span s1{ sp0}; // a span pointing at int. + std::span< volatile int, 0> s2{ sp0}; // a span< volatile int> pointing at int. + std::span s3{ sp0}; // a span pointing at int. + std::span s4{ vsp0}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } + +// static -> dynamic + { + std::span s1{ sp0}; // a span pointing at int. + std::span< volatile int> s2{ sp0}; // a span< volatile int> pointing at int. + std::span s3{ sp0}; // a span pointing at int. + std::span s4{ vsp0}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } + +// dynamic -> static + { + std::span s1{ sp}; // a span pointing at int. + std::span< volatile int, 0> s2{ sp}; // a span< volatile int> pointing at int. + std::span s3{ sp}; // a span pointing at int. + std::span s4{ vsp}; // a span pointing at volatile int. + assert(s1.size() + s2.size() + s3.size() + s4.size() == 0); + } +} + + +template +constexpr bool testConstexprSpan() +{ + std::span s0{}; + std::span s1(s0); // dynamic -> static + std::span s2(s1); // static -> dynamic + ASSERT_NOEXCEPT(std::span {s0}); + ASSERT_NOEXCEPT(std::span{s1}); + ASSERT_NOEXCEPT(std::span {s1}); + ASSERT_NOEXCEPT(std::span{s0}); + + return + s1.data() == nullptr && s1.size() == 0 + && s2.data() == nullptr && s2.size() == 0; +} + + +template +void testRuntimeSpan() +{ + std::span s0{}; + std::span s1(s0); // dynamic -> static + std::span s2(s1); // static -> dynamic + ASSERT_NOEXCEPT(std::span {s0}); + ASSERT_NOEXCEPT(std::span{s1}); + ASSERT_NOEXCEPT(std::span {s1}); + ASSERT_NOEXCEPT(std::span{s0}); + + assert(s1.data() == nullptr && s1.size() == 0); + assert(s2.data() == nullptr && s2.size() == 0); +} + + +template +bool testConversionSpan() +{ + static_assert(std::is_convertible_v, "Bad input types to 'testConversionSpan"); + std::span s0d{}; + std::span s0s{}; + std::span s1(s0d); // dynamic -> static + std::span s2(s0s); // static -> dynamic + s1.data() == nullptr && s1.size() == 0 + && s2.data() == nullptr && s2.size() == 0; +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + +// TODO: Add some conversion tests here that aren't "X --> const X" +// assert((testConversionSpan())); + + checkCV(); +} Index: test/std/containers/views/span.cons/stdarray.pass.cpp =================================================================== --- test/std/containers/views/span.cons/stdarray.pass.cpp +++ test/std/containers/views/span.cons/stdarray.pass.cpp @@ -0,0 +1,111 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span(array& arr) noexcept; +// template +// constexpr span(const array& arr) noexcept; +// +// Remarks: These constructors shall not participate in overload resolution unless: +// — extent == dynamic_extent || N == extent is true, and +// — remove_pointer_t(*)[] is convertible to ElementType(*)[]. +// + + +#include +#include +#include + +#include "test_macros.h" + + +void checkCV() +{ + std::array arr = {1,2,3}; +// STL says these are not cromulent +// std::array carr = {4,5,6}; +// std::array varr = {7,8,9}; +// std::array cvarr = {1,3,5}; + +// Types the same (dynamic sized) + { + std::span< int> s1{ arr}; // a span< int> pointing at int. + } + +// Types the same (static sized) + { + std::span< int,3> s1{ arr}; // a span< int> pointing at int. + } + + +// types different (dynamic sized) + { + std::span s1{ arr}; // a span pointing at int. + std::span< volatile int> s2{ arr}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{ arr}; // a span< volatile int> pointing at const int. + std::span s4{ arr}; // a span pointing at int. + } + +// types different (static sized) + { + std::span s1{ arr}; // a span pointing at int. + std::span< volatile int,3> s2{ arr}; // a span< volatile int> pointing at int. + std::span< volatile int,3> s3{ arr}; // a span< volatile int> pointing at const int. + std::span s4{ arr}; // a span pointing at int. + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr std::array val = { T(), T() }; + ASSERT_NOEXCEPT(std::span {val}); + ASSERT_NOEXCEPT(std::span{val}); + std::span s1{val}; + std::span s2{val}; + return + s1.data() == &val[0] && s1.size() == 2 + && s2.data() == &val[0] && s2.size() == 2; +} + + +template +void testRuntimeSpan() +{ + std::array val; + ASSERT_NOEXCEPT(std::span {val}); + ASSERT_NOEXCEPT(std::span{val}); + std::span s1{val}; + std::span s2{val}; + assert(s1.data() == &val[0] && s1.size() == 2); + assert(s2.data() == &val[0] && s2.size() == 2); +} + +struct A{}; + +int main () +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + checkCV(); +} Index: test/std/containers/views/span.elem/data.pass.cpp =================================================================== --- test/std/containers/views/span.elem/data.pass.cpp +++ test/std/containers/views/span.elem/data.pass.cpp @@ -0,0 +1,121 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr pointer data() const noexcept; +// + + +#include +#include +#include + +#include "test_macros.h" + + +template +constexpr bool testConstexprSpan(Span sp, typename Span::pointer ptr) +{ + ASSERT_NOEXCEPT(sp.data()); + return sp.data() == ptr; +} + + +template +void testRuntimeSpan(Span sp, typename Span::pointer ptr) +{ + ASSERT_NOEXCEPT(sp.data()); + assert(sp.data() == ptr); +} + +struct A{}; +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + +int main () +{ + +// dynamic size + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), iArr1), ""); + + static_assert(testConstexprSpan(std::span(iArr1 + 1, 1), iArr1 + 1), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 2, 2), iArr1 + 2), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 3, 3), iArr1 + 3), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 4, 4), iArr1 + 4), ""); + +// static size + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + static_assert(testConstexprSpan(std::span(), nullptr), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), iArr1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), iArr1), ""); + + static_assert(testConstexprSpan(std::span(iArr1 + 1, 1), iArr1 + 1), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 2, 2), iArr1 + 2), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 3, 3), iArr1 + 3), ""); + static_assert(testConstexprSpan(std::span(iArr1 + 4, 4), iArr1 + 4), ""); + + +// dynamic size + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + + testRuntimeSpan(std::span(iArr2, 1), iArr2); + testRuntimeSpan(std::span(iArr2, 2), iArr2); + testRuntimeSpan(std::span(iArr2, 3), iArr2); + testRuntimeSpan(std::span(iArr2, 4), iArr2); + + testRuntimeSpan(std::span(iArr2 + 1, 1), iArr2 + 1); + testRuntimeSpan(std::span(iArr2 + 2, 2), iArr2 + 2); + testRuntimeSpan(std::span(iArr2 + 3, 3), iArr2 + 3); + testRuntimeSpan(std::span(iArr2 + 4, 4), iArr2 + 4); + +// static size + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + testRuntimeSpan(std::span(), nullptr); + + testRuntimeSpan(std::span(iArr2, 1), iArr2); + testRuntimeSpan(std::span(iArr2, 2), iArr2); + testRuntimeSpan(std::span(iArr2, 3), iArr2); + testRuntimeSpan(std::span(iArr2, 4), iArr2); + + testRuntimeSpan(std::span(iArr2 + 1, 1), iArr2 + 1); + testRuntimeSpan(std::span(iArr2 + 2, 2), iArr2 + 2); + testRuntimeSpan(std::span(iArr2 + 3, 3), iArr2 + 3); + testRuntimeSpan(std::span(iArr2 + 4, 4), iArr2 + 4); + + + std::string s; + testRuntimeSpan(std::span(&s, 1), &s); + testRuntimeSpan(std::span(&s, 1), &s); + +} Index: test/std/containers/views/span.elem/op_idx.pass.cpp =================================================================== --- test/std/containers/views/span.elem/op_idx.pass.cpp +++ test/std/containers/views/span.elem/op_idx.pass.cpp @@ -0,0 +1,119 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr reference operator[](index_type idx) const; +// constexpr reference operator()(index_type idx) const; +// + + +#include +#include +#include + +#include "test_macros.h" + + +template +constexpr bool testConstexprSpan(Span sp, ptrdiff_t idx) +{ + _LIBCPP_ASSERT(noexcept(sp[idx]), ""); + _LIBCPP_ASSERT(noexcept(sp(idx)), ""); + + typename Span::reference r1 = sp[idx]; + typename Span::reference r2 = sp(idx); + typename Span::reference r3 = *(sp.data() + idx); + return r1 == r2 && r2 == r3; +} + + +template +void testRuntimeSpan(Span sp, ptrdiff_t idx) +{ + _LIBCPP_ASSERT(noexcept(sp[idx]), ""); + _LIBCPP_ASSERT(noexcept(sp(idx)), ""); + + typename Span::reference r1 = sp[idx]; + typename Span::reference r2 = sp(idx); + typename Span::reference r3 = *(sp.data() + idx); + assert(r1 == r2 && r2 == r3); +} + +struct A{}; +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + +int main () +{ + static_assert(testConstexprSpan(std::span(iArr1, 1), 0), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 2), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), 1), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 3), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 2), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 4), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 2), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 3), ""); + + + static_assert(testConstexprSpan(std::span(iArr1, 1), 0), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 2), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), 1), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 3), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 2), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 4), 0), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 2), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 3), ""); + + + testRuntimeSpan(std::span(iArr2, 1), 0); + + testRuntimeSpan(std::span(iArr2, 2), 0); + testRuntimeSpan(std::span(iArr2, 2), 1); + + testRuntimeSpan(std::span(iArr2, 3), 0); + testRuntimeSpan(std::span(iArr2, 3), 1); + testRuntimeSpan(std::span(iArr2, 3), 2); + + testRuntimeSpan(std::span(iArr2, 4), 0); + testRuntimeSpan(std::span(iArr2, 4), 1); + testRuntimeSpan(std::span(iArr2, 4), 2); + testRuntimeSpan(std::span(iArr2, 4), 3); + + + testRuntimeSpan(std::span(iArr2, 1), 0); + + testRuntimeSpan(std::span(iArr2, 2), 0); + testRuntimeSpan(std::span(iArr2, 2), 1); + + testRuntimeSpan(std::span(iArr2, 3), 0); + testRuntimeSpan(std::span(iArr2, 3), 1); + testRuntimeSpan(std::span(iArr2, 3), 2); + + testRuntimeSpan(std::span(iArr2, 4), 0); + testRuntimeSpan(std::span(iArr2, 4), 1); + testRuntimeSpan(std::span(iArr2, 4), 2); + testRuntimeSpan(std::span(iArr2, 4), 3); + + std::string s; + testRuntimeSpan(std::span (&s, 1), 0); + testRuntimeSpan(std::span(&s, 1), 0); +} Index: test/std/containers/views/span.iterators/begin.pass.cpp =================================================================== --- test/std/containers/views/span.iterators/begin.pass.cpp +++ test/std/containers/views/span.iterators/begin.pass.cpp @@ -0,0 +1,116 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr iterator begin() const noexcept; +// constexpr const_iterator cbegin() const noexcept; + +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span s) +{ + bool ret = true; + typename Span::iterator b = s. begin(); + typename Span::const_iterator cb = s.cbegin(); + + if (s.empty()) + { + ret = ret && ( b == s.end()); + ret = ret && (cb == s.cend()); + } + else + { + ret = ret && ( *b == s[0]); + ret = ret && ( &*b == &s[0]); + ret = ret && ( *cb == s[0]); + ret = ret && (&*cb == &s[0]); + } + ret = ret && (b == cb); + return ret; +} + + +template +void testRuntimeSpan(Span s) +{ + typename Span::iterator b = s. begin(); + typename Span::const_iterator cb = s.cbegin(); + + if (s.empty()) + { + assert( b == s.end()); + assert(cb == s.cend()); + } + else + { + assert( *b == s[0]); + assert( &*b == &s[0]); + assert( *cb == s[0]); + assert(&*cb == &s[0]); + } + assert(b == cb); +} + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + + +int main() +{ + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5)), ""); + + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0)); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.iterators/end.pass.cpp =================================================================== --- test/std/containers/views/span.iterators/end.pass.cpp +++ test/std/containers/views/span.iterators/end.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr iterator end() const noexcept; +// constexpr const_iterator cend() const noexcept; + +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span s) +{ + bool ret = true; + typename Span::iterator e = s. end(); + typename Span::const_iterator ce = s.cend(); + if (s.empty()) + { + ret = ret && ( e == s.begin()); + ret = ret && (ce == s.cbegin()); + } + else + { + ret = ret && ( e != s.begin()); + ret = ret && (ce != s.cbegin()); + } + + ret = ret && (( e - s.begin()) == s.size()); + ret = ret && ((ce - s.cbegin()) == s.size()); + + ret = ret && (e == ce); + return ret; +} + +template +void testRuntimeSpan(Span s) +{ + typename Span::iterator e = s. end(); + typename Span::const_iterator ce = s.cend(); + if (s.empty()) + { + assert( e == s.begin()); + assert(ce == s.cbegin()); + } + else + { + assert( e != s.begin()); + assert(ce != s.cbegin()); + } + + assert(( e - s.begin()) == s.size()); + assert((ce - s.cbegin()) == s.size()); + + assert(e == ce); +} + + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + + +int main() +{ + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5)), ""); + + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0)); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.iterators/rbegin.pass.cpp =================================================================== --- test/std/containers/views/span.iterators/rbegin.pass.cpp +++ test/std/containers/views/span.iterators/rbegin.pass.cpp @@ -0,0 +1,117 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr reverse_iterator rbegin() const noexcept; +// constexpr const_reverse_iterator crbegin() const noexcept; + +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span s) +{ + bool ret = true; + typename Span::reverse_iterator b = s. rbegin(); + typename Span::const_reverse_iterator cb = s.crbegin(); + if (s.empty()) + { + ret = ret && ( b == s.rend()); + ret = ret && (cb == s.crend()); + } + else + { + const typename Span::index_type last = s.size() - 1; + ret = ret && ( *b == s[last]); + ret = ret && ( &*b == &s[last]); + ret = ret && ( *cb == s[last]); + ret = ret && (&*cb == &s[last]); + } + ret = ret && (b == cb); + return ret; +} + + +template +void testRuntimeSpan(Span s) +{ + typename Span::reverse_iterator b = s. rbegin(); + typename Span::const_reverse_iterator cb = s.crbegin(); + if (s.empty()) + { + assert( b == s.rend()); + assert(cb == s.crend()); + } + else + { + const typename Span::index_type last = s.size() - 1; + assert( *b == s[last]); + assert( &*b == &s[last]); + assert( *cb == s[last]); + assert(&*cb == &s[last]); + } + assert(b == cb); +} + + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + + +int main() +{ + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5)), ""); + + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, static_cast(0))); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.iterators/rend.pass.cpp =================================================================== --- test/std/containers/views/span.iterators/rend.pass.cpp +++ test/std/containers/views/span.iterators/rend.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr reverse_iterator rend() const noexcept; +// constexpr const_reverse_iterator crend() const noexcept; + +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span s) +{ + bool ret = true; + typename Span::reverse_iterator e = s. rend(); + typename Span::const_reverse_iterator ce = s.crend(); + if (s.empty()) + { + ret = ret && ( e == s.rbegin()); + ret = ret && (ce == s.crbegin()); + } + else + { + ret = ret && ( e != s.rbegin()); + ret = ret && (ce != s.crbegin()); + } + + ret = ret && (( e - s.rbegin()) == s.size()); + ret = ret && ((ce - s.crbegin()) == s.size()); + + ret = ret && (e == ce); + return ret; +} + +template +void testRuntimeSpan(Span s) +{ + typename Span::reverse_iterator e = s. rend(); + typename Span::const_reverse_iterator ce = s.crend(); + if (s.empty()) + { + assert( e == s.rbegin()); + assert(ce == s.crbegin()); + } + else + { + assert( e != s.rbegin()); + assert(ce != s.crbegin()); + } + + assert(( e - s.rbegin()) == s.size()); + assert((ce - s.crbegin()) == s.size()); + + assert(e == ce); +} + + +struct A{}; +bool operator==(A, A) {return true;} + +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + + +int main() +{ + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + static_assert(testConstexprSpan(std::span()), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4)), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5)), ""); + + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0)); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.objectrep/as_bytes.pass.cpp =================================================================== --- test/std/containers/views/span.objectrep/as_bytes.pass.cpp +++ test/std/containers/views/span.objectrep/as_bytes.pass.cpp @@ -0,0 +1,78 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// span(sizeof(ElementType)) * Extent> +// as_bytes(span s) noexcept; + + +#include +#include +#include + +#include "test_macros.h" + +template +void testRuntimeSpan(Span sp) +{ + ASSERT_NOEXCEPT(std::as_bytes(sp)); + + auto spBytes = std::as_bytes(sp); + using SB = decltype(spBytes); + ASSERT_SAME_TYPE(const std::byte, typename SB::element_type); + + if (sp.extent == std::dynamic_extent) + assert(spBytes.extent == std::dynamic_extent); + else + assert(spBytes.extent == static_cast(sizeof(typename Span::element_type)) * sp.extent); + + assert((void *) spBytes.data() == (void *) sp.data()); + assert(spBytes.size() == sp.size_bytes()); +} + +struct A{}; +int iArr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + +int main () +{ + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + testRuntimeSpan(std::span(iArr2 + 5, 1)); + testRuntimeSpan(std::span(iArr2 + 4, 2)); + testRuntimeSpan(std::span(iArr2 + 3, 3)); + testRuntimeSpan(std::span(iArr2 + 2, 4)); + testRuntimeSpan(std::span(iArr2 + 1, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0)); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.objectrep/as_writeable_bytes.fail.cpp =================================================================== --- test/std/containers/views/span.objectrep/as_writeable_bytes.fail.cpp +++ test/std/containers/views/span.objectrep/as_writeable_bytes.fail.cpp @@ -0,0 +1,48 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// span(sizeof(ElementType)) * Extent> +// as_writeable_bytes(span s) noexcept; + + +#include +#include +#include + +#include "test_macros.h" + +const int iArr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + +struct A {}; + +int main () +{ + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span()); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + + std::as_writeable_bytes(std::span (iArr2, 1)); // expected-error {{no matching function for call to 'as_writeable_bytes'}} + std::as_writeable_bytes(std::span(iArr2 + 5, 1)); // expected-error {{no matching function for call to 'as_writeable_bytes'}} +} Index: test/std/containers/views/span.objectrep/as_writeable_bytes.pass.cpp =================================================================== --- test/std/containers/views/span.objectrep/as_writeable_bytes.pass.cpp +++ test/std/containers/views/span.objectrep/as_writeable_bytes.pass.cpp @@ -0,0 +1,78 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// span(sizeof(ElementType)) * Extent> +// as_writeable_bytes(span s) noexcept; + + +#include +#include +#include + +#include "test_macros.h" + +template +void testRuntimeSpan(Span sp) +{ + ASSERT_NOEXCEPT(std::as_writeable_bytes(sp)); + + auto spBytes = std::as_writeable_bytes(sp); + using SB = decltype(spBytes); + ASSERT_SAME_TYPE(std::byte, typename SB::element_type); + + if (sp.extent == std::dynamic_extent) + assert(spBytes.extent == std::dynamic_extent); + else + assert(spBytes.extent == static_cast(sizeof(typename Span::element_type)) * sp.extent); + + assert(static_cast(spBytes.data()) == static_cast(sp.data())); + assert(spBytes.size() == sp.size_bytes()); +} + +struct A{}; +int iArr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + +int main () +{ + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span ()); + testRuntimeSpan(std::span()); + + testRuntimeSpan(std::span(iArr2, 1)); + testRuntimeSpan(std::span(iArr2, 2)); + testRuntimeSpan(std::span(iArr2, 3)); + testRuntimeSpan(std::span(iArr2, 4)); + testRuntimeSpan(std::span(iArr2, 5)); + + testRuntimeSpan(std::span(iArr2 + 5, 1)); + testRuntimeSpan(std::span(iArr2 + 4, 2)); + testRuntimeSpan(std::span(iArr2 + 3, 3)); + testRuntimeSpan(std::span(iArr2 + 2, 4)); + testRuntimeSpan(std::span(iArr2 + 1, 5)); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0)); + testRuntimeSpan(std::span(&s, 1)); +} Index: test/std/containers/views/span.obs/empty.pass.cpp =================================================================== --- test/std/containers/views/span.obs/empty.pass.cpp +++ test/std/containers/views/span.obs/empty.pass.cpp @@ -0,0 +1,73 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr bool empty() const noexcept; +// + + +#include +#include +#include + +#include "test_macros.h" + +struct A{}; +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + +int main () +{ + static_assert( noexcept(std::span ().empty()), ""); + static_assert( noexcept(std::span().empty()), ""); + + + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + static_assert( std::span().empty(), ""); + + static_assert(!std::span(iArr1, 1).empty(), ""); + static_assert(!std::span(iArr1, 2).empty(), ""); + static_assert(!std::span(iArr1, 3).empty(), ""); + static_assert(!std::span(iArr1, 4).empty(), ""); + static_assert(!std::span(iArr1, 5).empty(), ""); + + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty() )); + + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty() )); + assert( (std::span().empty())); + + assert(!(std::span(iArr2, 1).empty())); + assert(!(std::span(iArr2, 2).empty())); + assert(!(std::span(iArr2, 3).empty())); + assert(!(std::span(iArr2, 4).empty())); + assert(!(std::span(iArr2, 5).empty())); + + std::string s; + assert( ((std::span(&s, (std::ptrdiff_t) 0)).empty())); + assert(!((std::span(&s, 1).empty()))); +} Index: test/std/containers/views/span.obs/size.pass.cpp =================================================================== --- test/std/containers/views/span.obs/size.pass.cpp +++ test/std/containers/views/span.obs/size.pass.cpp @@ -0,0 +1,91 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr index_type size() const noexcept; +// + + +#include +#include +#include + +#include "test_macros.h" + + +template +constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz) +{ + ASSERT_NOEXCEPT(sp.size()); + return sp.size() == sz; +} + + +template +void testRuntimeSpan(Span sp, ptrdiff_t sz) +{ + ASSERT_NOEXCEPT(sp.size()); + assert(sp.size() == sz); +} + +struct A{}; +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + +int main () +{ + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), 2), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 3), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 4), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5), 5), ""); + + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span(), 0); + + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span(), 0); + + testRuntimeSpan(std::span(iArr2, 1), 1); + testRuntimeSpan(std::span(iArr2, 2), 2); + testRuntimeSpan(std::span(iArr2, 3), 3); + testRuntimeSpan(std::span(iArr2, 4), 4); + testRuntimeSpan(std::span(iArr2, 5), 5); + + testRuntimeSpan(std::span(iArr2 + 5, 1), 1); + testRuntimeSpan(std::span(iArr2 + 4, 2), 2); + testRuntimeSpan(std::span(iArr2 + 3, 3), 3); + testRuntimeSpan(std::span(iArr2 + 2, 4), 4); + testRuntimeSpan(std::span(iArr2 + 1, 5), 5); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0), 0); + testRuntimeSpan(std::span(&s, 1), 1); +} Index: test/std/containers/views/span.obs/size_bytes.pass.cpp =================================================================== --- test/std/containers/views/span.obs/size_bytes.pass.cpp +++ test/std/containers/views/span.obs/size_bytes.pass.cpp @@ -0,0 +1,92 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// constexpr index_type size_bytes() const noexcept; +// +// Effects: Equivalent to: return size() * sizeof(element_type); + + +#include +#include +#include + +#include "test_macros.h" + + +template +constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz) +{ + ASSERT_NOEXCEPT(sp.size_bytes()); + return (size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type); +} + + +template +void testRuntimeSpan(Span sp, ptrdiff_t sz) +{ + ASSERT_NOEXCEPT(sp.size_bytes()); + assert((size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type)); +} + +struct A{}; +constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; + +int main () +{ + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + static_assert(testConstexprSpan(std::span(), 0), ""); + + static_assert(testConstexprSpan(std::span(iArr1, 1), 1), ""); + static_assert(testConstexprSpan(std::span(iArr1, 2), 2), ""); + static_assert(testConstexprSpan(std::span(iArr1, 3), 3), ""); + static_assert(testConstexprSpan(std::span(iArr1, 4), 4), ""); + static_assert(testConstexprSpan(std::span(iArr1, 5), 5), ""); + + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span(), 0); + + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span (), 0); + testRuntimeSpan(std::span(), 0); + + testRuntimeSpan(std::span(iArr2, 1), 1); + testRuntimeSpan(std::span(iArr2, 2), 2); + testRuntimeSpan(std::span(iArr2, 3), 3); + testRuntimeSpan(std::span(iArr2, 4), 4); + testRuntimeSpan(std::span(iArr2, 5), 5); + + testRuntimeSpan(std::span(iArr2 + 5, 1), 1); + testRuntimeSpan(std::span(iArr2 + 4, 2), 2); + testRuntimeSpan(std::span(iArr2 + 3, 3), 3); + testRuntimeSpan(std::span(iArr2 + 2, 4), 4); + testRuntimeSpan(std::span(iArr2 + 1, 5), 5); + + std::string s; + testRuntimeSpan(std::span(&s, (std::ptrdiff_t) 0), 0); + testRuntimeSpan(std::span(&s, 1), 1); +} Index: test/std/containers/views/span.sub/first.pass.cpp =================================================================== --- test/std/containers/views/span.sub/first.pass.cpp +++ test/std/containers/views/span.sub/first.pass.cpp @@ -0,0 +1,136 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span first() const; +// +// constexpr span first(index_type count) const; +// +// Requires: 0 <= Count && Count <= size(). + + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template first()))); + LIBCPP_ASSERT((noexcept(sp.first(Count)))); + auto s1 = sp.template first(); + auto s2 = sp.first(Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == Count, ""); + static_assert(S2::extent == std::dynamic_extent, ""); + return + s1.data() == s2.data() + && s1.size() == s2.size() + && std::equal(s1.begin(), s1.end(), sp.begin()); +} + + +template +void testRuntimeSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template first()))); + LIBCPP_ASSERT((noexcept(sp.first(Count)))); + auto s1 = sp.template first(); + auto s2 = sp.first(Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == Count, ""); + static_assert(S2::extent == std::dynamic_extent, ""); + assert(s1.data() == s2.data()); + assert(s1.size() == s2.size()); + assert(std::equal(s1.begin(), s1.end(), sp.begin())); +} + + +constexpr int carr1[] = {1,2,3,4}; + int arr[] = {5,6,7}; +std::string sarr [] = { "ABC", "DEF", "GHI", "JKL", "MNO"}; + +int main () +{ + { + using Sp = std::span; + static_assert(testConstexprSpan(Sp{}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + testRuntimeSpan(Sp{}); + + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + } + + { + using Sp = std::span; + testConstexprSpan(Sp{}); + + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + } +} Index: test/std/containers/views/span.sub/last.pass.cpp =================================================================== --- test/std/containers/views/span.sub/last.pass.cpp +++ test/std/containers/views/span.sub/last.pass.cpp @@ -0,0 +1,136 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span last() const; +// +// constexpr span last(index_type count) const; +// +// Requires: 0 <= Count && Count <= size(). + + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template last()))); + LIBCPP_ASSERT((noexcept(sp.last(Count)))); + auto s1 = sp.template last(); + auto s2 = sp.last(Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == Count, ""); + static_assert(S2::extent == std::dynamic_extent, ""); + return + s1.data() == s2.data() + && s1.size() == s2.size() + && std::equal(s1.begin(), s1.end(), sp.end() - Count); +} + + +template +void testRuntimeSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template last()))); + LIBCPP_ASSERT((noexcept(sp.last(Count)))); + auto s1 = sp.template last(); + auto s2 = sp.last(Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == Count, ""); + static_assert(S2::extent == std::dynamic_extent, ""); + assert(s1.data() == s2.data()); + assert(s1.size() == s2.size()); + assert(std::equal(s1.begin(), s1.end(), sp.end() - Count)); +} + + +constexpr int carr1[] = {1,2,3,4}; + int arr[] = {5,6,7}; +std::string sarr [] = { "ABC", "DEF", "GHI", "JKL", "MNO"}; + +int main () +{ + { + using Sp = std::span; + static_assert(testConstexprSpan(Sp{}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + testRuntimeSpan(Sp{}); + + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + testRuntimeSpan(Sp{arr}); + } + + { + using Sp = std::span; + testConstexprSpan(Sp{}); + + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + testRuntimeSpan(Sp{sarr}); + } +} Index: test/std/containers/views/span.sub/subspan.pass.cpp =================================================================== --- test/std/containers/views/span.sub/subspan.pass.cpp +++ test/std/containers/views/span.sub/subspan.pass.cpp @@ -0,0 +1,210 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// constexpr span subspan() const; +// +// constexpr span subspan( +// index_type offset, index_type count = dynamic_extent) const; +// +// Requires: (0 <= Offset && Offset <= size()) +// && (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()) + +#include +#include +#include +#include + +#include "test_macros.h" + +template +constexpr bool testConstexprSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template subspan()))); + LIBCPP_ASSERT((noexcept(sp.subspan(Offset, Count)))); + auto s1 = sp.template subspan(); + auto s2 = sp.subspan(Offset, Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Count), ""); + static_assert(S2::extent == std::dynamic_extent, ""); + return + s1.data() == s2.data() + && s1.size() == s2.size() + && std::equal(s1.begin(), s1.end(), sp.begin() + Offset); +} + +template +constexpr bool testConstexprSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template subspan()))); + LIBCPP_ASSERT((noexcept(sp.subspan(Offset)))); + auto s1 = sp.template subspan(); + auto s2 = sp.subspan(Offset); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Span::extent - Offset), ""); + static_assert(S2::extent == std::dynamic_extent, ""); + return + s1.data() == s2.data() + && s1.size() == s2.size() + && std::equal(s1.begin(), s1.end(), sp.begin() + Offset, sp.end()); +} + + +template +void testRuntimeSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template subspan()))); + LIBCPP_ASSERT((noexcept(sp.subspan(Offset, Count)))); + auto s1 = sp.template subspan(); + auto s2 = sp.subspan(Offset, Count); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Count), ""); + static_assert(S2::extent == std::dynamic_extent, ""); + assert(s1.data() == s2.data()); + assert(s1.size() == s2.size()); + assert(std::equal(s1.begin(), s1.end(), sp.begin() + Offset)); +} + + +template +void testRuntimeSpan(Span sp) +{ + LIBCPP_ASSERT((noexcept(sp.template subspan()))); + LIBCPP_ASSERT((noexcept(sp.subspan(Offset)))); + auto s1 = sp.template subspan(); + auto s2 = sp.subspan(Offset); + using S1 = decltype(s1); + using S2 = decltype(s2); + ASSERT_SAME_TYPE(typename Span::value_type, typename S1::value_type); + ASSERT_SAME_TYPE(typename Span::value_type, typename S2::value_type); + static_assert(S1::extent == (Span::extent == std::dynamic_extent ? std::dynamic_extent : Span::extent - Offset), ""); + static_assert(S2::extent == std::dynamic_extent, ""); + assert(s1.data() == s2.data()); + assert(s1.size() == s2.size()); + assert(std::equal(s1.begin(), s1.end(), sp.begin() + Offset, sp.end())); +} + + +constexpr int carr1[] = {1,2,3,4}; + int arr1[] = {5,6,7}; + +int main () +{ + { + using Sp = std::span; + static_assert(testConstexprSpan(Sp{}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + static_assert(testConstexprSpan(Sp{}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + + static_assert(testConstexprSpan(Sp{carr1}), ""); + + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + static_assert(testConstexprSpan(Sp{carr1}), ""); + } + + { + using Sp = std::span; + testRuntimeSpan(Sp{}); + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + } + + { + using Sp = std::span; + testRuntimeSpan(Sp{}); + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + } + + { + using Sp = std::span; + + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + testRuntimeSpan(Sp{arr1}); + } +} Index: test/std/containers/views/types.pass.cpp =================================================================== --- test/std/containers/views/types.pass.cpp +++ test/std/containers/views/types.pass.cpp @@ -0,0 +1,107 @@ +// -*- C++ -*- +//===------------------------------ span ---------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +// + +// template +// class span { +// public: +// // constants and types +// using element_type = ElementType; +// using value_type = remove_cv_t; +// using index_type = ptrdiff_t; +// using difference_type = ptrdiff_t; +// using pointer = element_type *; +// using reference = element_type &; +// using iterator = implementation-defined; +// using const_iterator = implementation-defined; +// using reverse_iterator = std::reverse_iterator; +// using const_reverse_iterator = std::reverse_iterator; +// +// static constexpr index_type extent = Extent; +// + +#include +#include +#include +#include + +#include "test_macros.h" + +template +void testIterator() +{ + typedef std::iterator_traits ItT; + + ASSERT_SAME_TYPE(typename ItT::iterator_category, std::random_access_iterator_tag); + ASSERT_SAME_TYPE(typename ItT::value_type, typename S::value_type); + ASSERT_SAME_TYPE(typename ItT::reference, typename S::reference); + ASSERT_SAME_TYPE(typename ItT::pointer, typename S::pointer); + ASSERT_SAME_TYPE(typename ItT::difference_type, typename S::difference_type); +} + +template +void testConstIterator() +{ + typedef std::iterator_traits ItT; + + ASSERT_SAME_TYPE(typename ItT::iterator_category, std::random_access_iterator_tag); + ASSERT_SAME_TYPE(typename ItT::value_type, typename S::value_type); +// I'd like to say 'const typename S::pointer' here, but that gives me +// a const pointer to a non-const value, which is not what I want. + ASSERT_SAME_TYPE(typename ItT::reference, typename S::element_type const &); + ASSERT_SAME_TYPE(typename ItT::pointer, typename S::element_type const *); + ASSERT_SAME_TYPE(typename ItT::difference_type, typename S::difference_type); +} + +template +void testSpan() +{ + ASSERT_SAME_TYPE(typename S::element_type, ElementType); + ASSERT_SAME_TYPE(typename S::value_type, std::remove_cv_t); + ASSERT_SAME_TYPE(typename S::index_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(typename S::difference_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(typename S::pointer, ElementType *); + ASSERT_SAME_TYPE(typename S::reference, ElementType &); + + static_assert(S::extent == Size); // check that it exists + + testIterator(); + testIterator(); + testConstIterator(); + testConstIterator(); +} + + +template +void test() +{ + testSpan, T, -1>(); + testSpan, const T, -1>(); + testSpan, volatile T, -1>(); + testSpan, const volatile T, -1>(); + + testSpan, T, 5>(); + testSpan, const T, 5>(); + testSpan, volatile T, 5>(); + testSpan, const volatile T, 5>(); +} + +struct A{}; + +int main () +{ + test(); + test(); + test(); + test(); + test(); +}