mirror of
https://github.com/microsoft/GSL.git
synced 2025-01-18 17:55:01 -05:00
Merge pull request #844 from JordanMaples/dev/jomaples/unsigned
gsl::span: Migrate from signed to unsigned
This commit is contained in:
commit
691a78c016
@ -37,8 +37,8 @@ namespace gsl
|
||||
{
|
||||
// Note: this will generate faster code than std::copy using span iterator in older msvc+stl
|
||||
// not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
|
||||
template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
|
||||
std::ptrdiff_t DestExtent>
|
||||
template <class SrcElementType, std::size_t SrcExtent, class DestElementType,
|
||||
std::size_t DestExtent>
|
||||
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
|
||||
{
|
||||
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
|
||||
|
591
include/gsl/span
591
include/gsl/span
File diff suppressed because it is too large
Load Diff
@ -23,10 +23,10 @@
|
||||
|
||||
#include <algorithm> // for equal, lexicographical_compare
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
||||
#include <cstddef> // for size_t, nullptr_t
|
||||
#include <cstdint> // for PTRDIFF_MAX
|
||||
#include <cstring>
|
||||
#include <string> // for basic_string, allocator, char_traits
|
||||
#include <string> // for basic_string, allocator, char_traits
|
||||
#include <type_traits> // for declval, is_convertible, enable_if_t, add_...
|
||||
|
||||
#if defined(_MSC_VER) && !defined(__clang__)
|
||||
@ -56,43 +56,43 @@ namespace gsl
|
||||
// (sometimes needlessly) break existing programs when introduced.
|
||||
//
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <typename CharT, std::size_t Extent = dynamic_extent>
|
||||
using basic_zstring = CharT*;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using czstring = basic_zstring<const char, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cwzstring = basic_zstring<const wchar_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cu16zstring = basic_zstring<const char16_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cu32zstring = basic_zstring<const char32_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using zstring = basic_zstring<char, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using wzstring = basic_zstring<wchar_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using u16zstring = basic_zstring<char16_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using u32zstring = basic_zstring<char32_t, Extent>;
|
||||
|
||||
namespace details
|
||||
{
|
||||
template <class CharT>
|
||||
std::ptrdiff_t string_length(const CharT* str, std::ptrdiff_t n)
|
||||
std::size_t string_length(const CharT* str, std::size_t n)
|
||||
{
|
||||
if (str == nullptr || n <= 0) return 0;
|
||||
if (str == nullptr || n == dynamic_extent) return 0;
|
||||
|
||||
const span<const CharT> str_span{str, n};
|
||||
|
||||
std::ptrdiff_t len = 0;
|
||||
std::size_t len = 0;
|
||||
while (len < n && str_span[len]) len++;
|
||||
|
||||
return len;
|
||||
@ -108,18 +108,20 @@ namespace details
|
||||
// Will fail-fast if sentinel cannot be found before max elements are examined.
|
||||
//
|
||||
template <typename T, const T Sentinel>
|
||||
span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX)
|
||||
span<T, dynamic_extent> ensure_sentinel(T* seq,
|
||||
std::size_t max = static_cast<std::size_t>(-1))
|
||||
{
|
||||
Ensures(seq != nullptr);
|
||||
|
||||
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
|
||||
GSL_SUPPRESS(
|
||||
f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
|
||||
auto cur = seq;
|
||||
Ensures(cur != nullptr); // workaround for removing the warning
|
||||
|
||||
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work
|
||||
while ((cur - seq) < max && *cur != Sentinel) ++cur;
|
||||
while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur;
|
||||
Ensures(*cur == Sentinel);
|
||||
return {seq, cur - seq};
|
||||
return {seq, static_cast<std::size_t>(cur - seq)};
|
||||
}
|
||||
|
||||
//
|
||||
@ -128,7 +130,8 @@ span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX
|
||||
// the limit of size_type.
|
||||
//
|
||||
template <typename CharT>
|
||||
span<CharT, dynamic_extent> ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRDIFF_MAX)
|
||||
span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
|
||||
std::size_t max = static_cast<std::size_t>(-1))
|
||||
{
|
||||
return ensure_sentinel<CharT, CharT(0)>(sz, max);
|
||||
}
|
||||
@ -136,26 +139,27 @@ span<CharT, dynamic_extent> ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRD
|
||||
template <typename CharT, std::size_t N>
|
||||
span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
|
||||
{
|
||||
return ensure_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
|
||||
return ensure_z(&sz[0], N);
|
||||
}
|
||||
|
||||
template <class Cont>
|
||||
span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
|
||||
ensure_z(Cont& cont)
|
||||
{
|
||||
return ensure_z(cont.data(), narrow_cast<std::ptrdiff_t>(cont.size()));
|
||||
return ensure_z(cont.data(), cont.size());
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t>
|
||||
template <typename CharT, std::size_t>
|
||||
class basic_string_span;
|
||||
|
||||
namespace details {
|
||||
namespace details
|
||||
{
|
||||
template <typename T>
|
||||
struct is_basic_string_span_oracle : std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent>
|
||||
template <typename CharT, std::size_t Extent>
|
||||
struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
|
||||
{
|
||||
};
|
||||
@ -169,7 +173,7 @@ namespace details {
|
||||
//
|
||||
// string_span and relatives
|
||||
//
|
||||
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <typename CharT, std::size_t Extent = dynamic_extent>
|
||||
class basic_string_span
|
||||
{
|
||||
public:
|
||||
@ -180,14 +184,12 @@ public:
|
||||
using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
|
||||
using impl_type = span<element_type, Extent>;
|
||||
|
||||
using index_type = typename impl_type::index_type;
|
||||
using size_type = typename impl_type::size_type;
|
||||
using iterator = typename impl_type::iterator;
|
||||
using const_iterator = typename impl_type::const_iterator;
|
||||
using reverse_iterator = typename impl_type::reverse_iterator;
|
||||
using const_reverse_iterator = typename impl_type::const_reverse_iterator;
|
||||
|
||||
using size_type = index_type;
|
||||
|
||||
// default (empty)
|
||||
constexpr basic_string_span() noexcept = default;
|
||||
|
||||
@ -197,7 +199,7 @@ public:
|
||||
// assign
|
||||
constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
|
||||
|
||||
constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {}
|
||||
constexpr basic_string_span(pointer ptr, size_type length) : span_(ptr, length) {}
|
||||
constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
|
||||
|
||||
// From static arrays - if 0-terminated, remove 0 from the view
|
||||
@ -218,7 +220,7 @@ public:
|
||||
template <class Traits, class Allocator>
|
||||
// GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
|
||||
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
|
||||
: span_(&str[0], narrow_cast<std::ptrdiff_t>(str.length()))
|
||||
: span_(&str[0], str.length())
|
||||
{}
|
||||
|
||||
template <class Traits, class Allocator>
|
||||
@ -247,56 +249,56 @@ public:
|
||||
|
||||
// from string_span
|
||||
template <
|
||||
class OtherValueType, std::ptrdiff_t OtherExtent,
|
||||
class OtherValueType, std::size_t OtherExtent,
|
||||
class = std::enable_if_t<std::is_convertible<
|
||||
typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
|
||||
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
|
||||
: span_(other.data(), other.length())
|
||||
{}
|
||||
|
||||
template <index_type Count>
|
||||
template <size_type Count>
|
||||
constexpr basic_string_span<element_type, Count> first() const
|
||||
{
|
||||
return {span_.template first<Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<element_type, dynamic_extent> first(index_type count) const
|
||||
constexpr basic_string_span<element_type, dynamic_extent> first(size_type count) const
|
||||
{
|
||||
return {span_.first(count)};
|
||||
}
|
||||
|
||||
template <index_type Count>
|
||||
template <size_type Count>
|
||||
constexpr basic_string_span<element_type, Count> last() const
|
||||
{
|
||||
return {span_.template last<Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<element_type, dynamic_extent> last(index_type count) const
|
||||
constexpr basic_string_span<element_type, dynamic_extent> last(size_type count) const
|
||||
{
|
||||
return {span_.last(count)};
|
||||
}
|
||||
|
||||
template <index_type Offset, index_type Count>
|
||||
template <size_type Offset, size_type Count>
|
||||
constexpr basic_string_span<element_type, Count> subspan() const
|
||||
{
|
||||
return {span_.template subspan<Offset, Count>()};
|
||||
}
|
||||
|
||||
constexpr basic_string_span<element_type, dynamic_extent>
|
||||
subspan(index_type offset, index_type count = dynamic_extent) const
|
||||
subspan(size_type offset, size_type count = dynamic_extent) const
|
||||
{
|
||||
return {span_.subspan(offset, count)};
|
||||
}
|
||||
|
||||
constexpr reference operator[](index_type idx) const { return span_[idx]; }
|
||||
constexpr reference operator()(index_type idx) const { return span_[idx]; }
|
||||
constexpr reference operator[](size_type idx) const { return span_[idx]; }
|
||||
constexpr reference operator()(size_type idx) const { return span_[idx]; }
|
||||
|
||||
constexpr pointer data() const { return span_.data(); }
|
||||
|
||||
constexpr index_type length() const noexcept { return span_.size(); }
|
||||
constexpr index_type size() const noexcept { return span_.size(); }
|
||||
constexpr index_type size_bytes() const noexcept { return span_.size_bytes(); }
|
||||
constexpr index_type length_bytes() const noexcept { return span_.length_bytes(); }
|
||||
constexpr size_type length() const noexcept { return span_.size(); }
|
||||
constexpr size_type size() const noexcept { return span_.size(); }
|
||||
constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); }
|
||||
constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); }
|
||||
constexpr bool empty() const noexcept { return size() == 0; }
|
||||
|
||||
constexpr iterator begin() const noexcept { return span_.begin(); }
|
||||
@ -312,7 +314,7 @@ public:
|
||||
constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); }
|
||||
|
||||
private:
|
||||
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max)
|
||||
static impl_type remove_z(pointer const& sz, std::size_t max)
|
||||
{
|
||||
return {sz, details::string_length(sz, max)};
|
||||
}
|
||||
@ -320,41 +322,41 @@ private:
|
||||
template <std::size_t N>
|
||||
static impl_type remove_z(element_type (&sz)[N])
|
||||
{
|
||||
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
|
||||
return remove_z(&sz[0], N);
|
||||
}
|
||||
|
||||
impl_type span_;
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using string_span = basic_string_span<char, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cstring_span = basic_string_span<const char, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using wstring_span = basic_string_span<wchar_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cwstring_span = basic_string_span<const wchar_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using u16string_span = basic_string_span<char16_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cu16string_span = basic_string_span<const char16_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using u32string_span = basic_string_span<char32_t, Extent>;
|
||||
|
||||
template <std::ptrdiff_t Extent = dynamic_extent>
|
||||
template <std::size_t Extent = dynamic_extent>
|
||||
using cu32string_span = basic_string_span<const char32_t, Extent>;
|
||||
|
||||
//
|
||||
// to_string() allow (explicit) conversions from string_span to string
|
||||
//
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent>
|
||||
template <typename CharT, std::size_t Extent>
|
||||
std::basic_string<typename std::remove_const<CharT>::type>
|
||||
to_string(basic_string_span<CharT, Extent> view)
|
||||
{
|
||||
@ -362,13 +364,13 @@ to_string(basic_string_span<CharT, Extent> view)
|
||||
}
|
||||
|
||||
template <typename CharT, typename Traits = typename std::char_traits<CharT>,
|
||||
typename Allocator = std::allocator<CharT>, typename gCharT, std::ptrdiff_t Extent>
|
||||
typename Allocator = std::allocator<CharT>, typename gCharT, std::size_t Extent>
|
||||
std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
|
||||
{
|
||||
return {view.data(), narrow_cast<std::size_t>(view.length())};
|
||||
}
|
||||
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
template <class ElementType, std::size_t Extent>
|
||||
basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
|
||||
as_bytes(basic_string_span<ElementType, Extent> s) noexcept
|
||||
{
|
||||
@ -376,10 +378,10 @@ as_bytes(basic_string_span<ElementType, Extent> s) noexcept
|
||||
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
|
||||
}
|
||||
|
||||
template <class ElementType, std::ptrdiff_t Extent,
|
||||
template <class ElementType, std::size_t Extent,
|
||||
class = std::enable_if_t<!std::is_const<ElementType>::value>>
|
||||
basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
|
||||
as_writeable_bytes(basic_string_span<ElementType, Extent> s) noexcept
|
||||
as_writable_bytes(basic_string_span<ElementType, Extent> s) noexcept
|
||||
{
|
||||
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
|
||||
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
|
||||
@ -387,8 +389,9 @@ as_writeable_bytes(basic_string_span<ElementType, Extent> s) noexcept
|
||||
|
||||
// zero-terminated string span, used to convert
|
||||
// zero-terminated spans to legacy strings
|
||||
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
|
||||
class basic_zstring_span {
|
||||
template <typename CharT, std::size_t Extent = dynamic_extent>
|
||||
class basic_zstring_span
|
||||
{
|
||||
public:
|
||||
using value_type = CharT;
|
||||
using const_value_type = std::add_const_t<CharT>;
|
||||
@ -435,32 +438,32 @@ private:
|
||||
impl_type span_;
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using zstring_span = basic_zstring_span<char, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using wzstring_span = basic_zstring_span<wchar_t, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using u16zstring_span = basic_zstring_span<char16_t, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using u32zstring_span = basic_zstring_span<char32_t, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using czstring_span = basic_zstring_span<const char, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
|
||||
|
||||
template <std::ptrdiff_t Max = dynamic_extent>
|
||||
template <std::size_t Max = dynamic_extent>
|
||||
using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
|
||||
|
||||
// operator ==
|
||||
template <class CharT, std::ptrdiff_t Extent, class T,
|
||||
template <class CharT, std::size_t Extent, class T,
|
||||
class = std::enable_if_t<
|
||||
details::is_basic_string_span<T>::value ||
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
|
||||
@ -470,7 +473,7 @@ bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other
|
||||
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
template <class CharT, std::ptrdiff_t Extent, class T,
|
||||
template <class CharT, std::size_t Extent, class T,
|
||||
class = std::enable_if_t<
|
||||
!details::is_basic_string_span<T>::value &&
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
|
||||
@ -481,7 +484,7 @@ bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other
|
||||
}
|
||||
|
||||
// operator !=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
@ -490,7 +493,7 @@ bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
@ -500,7 +503,7 @@ bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
}
|
||||
|
||||
// operator<
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
@ -510,7 +513,7 @@ bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
@ -526,7 +529,7 @@ bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -540,7 +543,7 @@ bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -555,7 +558,7 @@ bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
#endif
|
||||
|
||||
// operator <=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
@ -564,7 +567,7 @@ bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
@ -579,7 +582,7 @@ bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -592,7 +595,7 @@ bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -606,7 +609,7 @@ bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
#endif
|
||||
|
||||
// operator>
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
@ -615,7 +618,7 @@ bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
@ -630,7 +633,7 @@ bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -643,7 +646,7 @@ bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -657,7 +660,7 @@ bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
#endif
|
||||
|
||||
// operator >=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
template <typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<std::is_convertible<
|
||||
T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>>
|
||||
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
@ -666,7 +669,7 @@ bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
|
||||
!gsl::details::is_basic_string_span<T>::value>>
|
||||
@ -681,7 +684,7 @@ bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
|
||||
// so the cases below are already covered by the previous operators
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
@ -694,7 +697,7 @@ bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
|
||||
}
|
||||
|
||||
template <
|
||||
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
|
||||
typename DataType = typename T::value_type,
|
||||
typename = std::enable_if_t<
|
||||
!gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
|
||||
|
@ -153,6 +153,7 @@ function(add_gsl_test name)
|
||||
endfunction()
|
||||
|
||||
add_gsl_test(span_tests)
|
||||
add_gsl_test(span_compatibility_tests)
|
||||
add_gsl_test(multi_span_tests)
|
||||
add_gsl_test(strided_span_tests)
|
||||
add_gsl_test(string_span_tests)
|
||||
|
@ -4,7 +4,7 @@ project(googletest-download NONE)
|
||||
include(ExternalProject)
|
||||
ExternalProject_Add(googletest
|
||||
GIT_REPOSITORY https://github.com/google/googletest.git
|
||||
GIT_TAG master
|
||||
GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e
|
||||
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
|
||||
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
|
||||
CONFIGURE_COMMAND ""
|
||||
|
1071
tests/span_compatibility_tests.cpp
Normal file
1071
tests/span_compatibility_tests.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -25,15 +25,11 @@
|
||||
#include <iterator> // for reverse_iterator, operator-, operator==
|
||||
#include <memory> // for unique_ptr, shared_ptr, make_unique, allo...
|
||||
#include <regex> // for match_results, sub_match, match_results<>...
|
||||
#include <stddef.h> // for ptrdiff_t
|
||||
#include <cstddef> // for ptrdiff_t
|
||||
#include <string> // for string
|
||||
#include <type_traits> // for integral_constant<>::value, is_default_co...
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace gsl
|
||||
{
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
#include <utility>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
@ -110,17 +106,17 @@ TEST(span_test, from_nullptr_size_constructor)
|
||||
std::abort();
|
||||
});
|
||||
{
|
||||
span<int> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
||||
span<int> s{nullptr, narrow_cast<span<int>::size_type>(0)};
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
|
||||
span<int> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
|
||||
span<int> cs{nullptr, narrow_cast<span<int>::size_type>(0)};
|
||||
EXPECT_TRUE(cs.size() == 0);
|
||||
EXPECT_TRUE(cs.data() == nullptr);
|
||||
}
|
||||
{
|
||||
auto workaround_macro = []() {
|
||||
const span<int, 1> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
||||
const span<int, 1> s{nullptr, narrow_cast<span<int>::size_type>(0)};
|
||||
};
|
||||
EXPECT_DEATH(workaround_macro(), deathstring);
|
||||
}
|
||||
@ -139,11 +135,11 @@ TEST(span_test, from_nullptr_size_constructor)
|
||||
EXPECT_DEATH(const_workaround_macro(), deathstring);
|
||||
}
|
||||
{
|
||||
span<int*> s{nullptr, narrow_cast<span<int>::index_type>(0)};
|
||||
span<int*> s{nullptr, narrow_cast<span<int>::size_type>(0)};
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
|
||||
span<const int*> cs{nullptr, narrow_cast<span<int>::index_type>(0)};
|
||||
span<const int*> cs{nullptr, narrow_cast<span<int>::size_type>(0)};
|
||||
EXPECT_TRUE(cs.size() == 0);
|
||||
EXPECT_TRUE(cs.data() == nullptr);
|
||||
}
|
||||
@ -161,29 +157,21 @@ TEST(span_test, from_pointer_length_constructor)
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
{
|
||||
span<int> s = {&arr[0], i};
|
||||
EXPECT_TRUE(s.size() == i);
|
||||
span<int> s = {&arr[0], narrow_cast<std::size_t>(i)};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<std::size_t>(i));
|
||||
EXPECT_TRUE(s.data() == &arr[0]);
|
||||
EXPECT_TRUE(s.empty() == (i == 0));
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
EXPECT_TRUE(arr[j] == s[j]);
|
||||
EXPECT_TRUE(arr[j] == s.at(j));
|
||||
EXPECT_TRUE(arr[j] == s(j));
|
||||
}
|
||||
EXPECT_TRUE(arr[j] == s[narrow_cast<std::size_t>(j)]);
|
||||
}
|
||||
{
|
||||
span<int> s = {&arr[i], 4 - narrow_cast<ptrdiff_t>(i)};
|
||||
EXPECT_TRUE(s.size() == 4 - i);
|
||||
span<int> s = {&arr[i], 4 - narrow_cast<std::size_t>(i)};
|
||||
EXPECT_TRUE(s.size() == 4 - narrow_cast<std::size_t>(i));
|
||||
EXPECT_TRUE(s.data() == &arr[i]);
|
||||
EXPECT_TRUE(s.empty() == ((4 - i) == 0));
|
||||
|
||||
for (int j = 0; j < 4 - i; ++j)
|
||||
{
|
||||
EXPECT_TRUE(arr[j + i] == s[j]);
|
||||
EXPECT_TRUE(arr[j + i] == s.at(j));
|
||||
EXPECT_TRUE(arr[j + i] == s(j));
|
||||
}
|
||||
EXPECT_TRUE(arr[j + i] == s[narrow_cast<std::size_t>(j)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -198,7 +186,7 @@ TEST(span_test, from_pointer_length_constructor)
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
span<int> s{p, narrow_cast<span<int>::index_type>(0)};
|
||||
span<int> s{p, narrow_cast<span<int>::size_type>(0)};
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
@ -219,7 +207,7 @@ TEST(span_test, from_pointer_length_constructor)
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto s = make_span(p, narrow_cast<span<int>::index_type>(0));
|
||||
auto s = make_span(p, narrow_cast<span<int>::size_type>(0));
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
@ -457,21 +445,21 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<int> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
|
||||
span<const int> cs{arr};
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(cs.size() == arr.size());
|
||||
EXPECT_TRUE(cs.data() == arr.data());
|
||||
}
|
||||
|
||||
{
|
||||
span<int, 4> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
|
||||
span<const int, 4> cs{arr};
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(cs.size() == arr.size());
|
||||
EXPECT_TRUE(cs.data() == arr.data());
|
||||
}
|
||||
|
||||
@ -486,7 +474,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<AddressOverloaded, 4> fs{ao_arr};
|
||||
EXPECT_TRUE(fs.size() == narrow_cast<ptrdiff_t>(ao_arr.size()));
|
||||
EXPECT_TRUE(fs.size() == ao_arr.size());
|
||||
EXPECT_TRUE(ao_arr.data() == fs.data());
|
||||
}
|
||||
|
||||
@ -532,7 +520,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
@ -561,13 +549,13 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<const int> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
{
|
||||
span<const int, 4> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
@ -575,7 +563,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<const AddressOverloaded, 4> s{ao_arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(ao_arr.size()));
|
||||
EXPECT_TRUE(s.size() == ao_arr.size());
|
||||
EXPECT_TRUE(s.data() == ao_arr.data());
|
||||
}
|
||||
|
||||
@ -606,7 +594,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
}
|
||||
@ -617,13 +605,13 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<const int> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
{
|
||||
span<const int, 4> s{arr};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
|
||||
@ -651,7 +639,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
auto s = make_span(arr);
|
||||
EXPECT_TRUE(s.size() == narrow_cast<ptrdiff_t>(arr.size()));
|
||||
EXPECT_TRUE(s.size() == arr.size());
|
||||
EXPECT_TRUE(s.data() == arr.data());
|
||||
}
|
||||
}
|
||||
@ -663,11 +651,11 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<int> s{v};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(v.size()));
|
||||
EXPECT_TRUE(s.size() == v.size());
|
||||
EXPECT_TRUE(s.data() == v.data());
|
||||
|
||||
span<const int> cs{v};
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(v.size()));
|
||||
EXPECT_TRUE(cs.size() == v.size());
|
||||
EXPECT_TRUE(cs.data() == v.data());
|
||||
}
|
||||
|
||||
@ -677,11 +665,11 @@ TEST(span_test, from_array_constructor)
|
||||
{
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
span<char> s{str};
|
||||
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(str.size()));
|
||||
EXPECT_TRUE(s.size() == str.size());
|
||||
EXPECT_TRUE(s.data() == str.data()));
|
||||
#endif
|
||||
span<const char> cs{str};
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(str.size()));
|
||||
EXPECT_TRUE(cs.size() == str.size());
|
||||
EXPECT_TRUE(cs.data() == str.data());
|
||||
}
|
||||
|
||||
@ -690,7 +678,7 @@ TEST(span_test, from_array_constructor)
|
||||
span<char> s{cstr};
|
||||
#endif
|
||||
span<const char> cs{cstr};
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(cstr.size()));
|
||||
EXPECT_TRUE(cs.size() == cstr.size());
|
||||
EXPECT_TRUE(cs.data() == cstr.data());
|
||||
}
|
||||
|
||||
@ -745,11 +733,11 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
auto s = make_span(v);
|
||||
EXPECT_TRUE(s.size() == narrow_cast<std::ptrdiff_t>(v.size()));
|
||||
EXPECT_TRUE(s.size() == v.size());
|
||||
EXPECT_TRUE(s.data() == v.data());
|
||||
|
||||
auto cs = make_span(cv);
|
||||
EXPECT_TRUE(cs.size() == narrow_cast<std::ptrdiff_t>(cv.size()));
|
||||
EXPECT_TRUE(cs.size() == cv.size());
|
||||
EXPECT_TRUE(cs.data() == cv.data());
|
||||
}
|
||||
}
|
||||
@ -966,7 +954,7 @@ TEST(span_test, from_array_constructor)
|
||||
EXPECT_TRUE(av.subspan(5).size() == 0);
|
||||
EXPECT_DEATH(av.subspan(6).size(), deathstring);
|
||||
const auto av2 = av.subspan(1);
|
||||
for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2);
|
||||
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
|
||||
}
|
||||
|
||||
{
|
||||
@ -977,53 +965,7 @@ TEST(span_test, from_array_constructor)
|
||||
EXPECT_TRUE(av.subspan(5).size() == 0);
|
||||
EXPECT_DEATH(av.subspan(6).size(), deathstring);
|
||||
const auto av2 = av.subspan(1);
|
||||
for (int i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, at_call)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. at_call";
|
||||
std::abort();
|
||||
});
|
||||
int arr[4] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
span<int> s = arr;
|
||||
EXPECT_TRUE(s.at(0) == 1);
|
||||
EXPECT_DEATH(s.at(5), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
int arr2d[2] = {1, 6};
|
||||
span<int, 2> s = arr2d;
|
||||
EXPECT_TRUE(s.at(0) == 1);
|
||||
EXPECT_TRUE(s.at(1) == 6);
|
||||
EXPECT_DEATH(s.at(2), deathstring);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, operator_function_call)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. operator_function_call";
|
||||
std::abort();
|
||||
});
|
||||
int arr[4] = {1, 2, 3, 4};
|
||||
|
||||
{
|
||||
span<int> s = arr;
|
||||
EXPECT_TRUE(s(0) == 1);
|
||||
EXPECT_DEATH(s(5), deathstring);
|
||||
}
|
||||
|
||||
{
|
||||
int arr2d[2] = {1, 6};
|
||||
span<int, 2> s = arr2d;
|
||||
EXPECT_TRUE(s(0) == 1);
|
||||
EXPECT_TRUE(s(1) == 6);
|
||||
EXPECT_DEATH(s(2), deathstring);
|
||||
for (std::size_t i = 0; i < 4; ++i) EXPECT_TRUE(av2[i] == static_cast<int>(i) + 2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1067,28 +1009,28 @@ TEST(span_test, from_array_constructor)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
|
||||
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.cbegin()), decltype(cbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.cend()), decltype(cend(s))>::value));
|
||||
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value));
|
||||
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.crbegin()), decltype(crbegin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.crend()), decltype(crend(s))>::value));
|
||||
|
||||
EXPECT_TRUE(s.begin() == begin(s));
|
||||
EXPECT_TRUE(s.end() == end(s));
|
||||
|
||||
EXPECT_TRUE(s.cbegin() == cbegin(s));
|
||||
EXPECT_TRUE(s.cend() == cend(s));
|
||||
|
||||
|
||||
EXPECT_TRUE(s.begin() == std::begin(s));
|
||||
EXPECT_TRUE(s.end() == std::end(s));
|
||||
|
||||
EXPECT_TRUE(s.cbegin() == std::cbegin(s));
|
||||
EXPECT_TRUE(s.cend() == std::cend(s));
|
||||
|
||||
EXPECT_TRUE(s.rbegin() == rbegin(s));
|
||||
EXPECT_TRUE(s.rend() == rend(s));
|
||||
|
||||
|
||||
EXPECT_TRUE(s.crbegin() == crbegin(s));
|
||||
EXPECT_TRUE(s.crend() == crend(s));
|
||||
}
|
||||
@ -1097,9 +1039,9 @@ TEST(span_test, from_array_constructor)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
|
||||
EXPECT_TRUE(s.size() == ssize(s));
|
||||
|
||||
EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
|
||||
EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
|
||||
}
|
||||
|
||||
TEST(span_test, iterator_comparisons)
|
||||
@ -1149,6 +1091,28 @@ TEST(span_test, from_array_constructor)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, incomparable_iterators)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. incomparable_iterators";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
int a[] = {1, 2, 3, 4};
|
||||
int b[] = {1, 2, 3, 4};
|
||||
{
|
||||
span<int> s = a;
|
||||
span<int> s2 = b;
|
||||
#if (__cplusplus > 201402L)
|
||||
EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() == s2.begin()), deathstring);
|
||||
EXPECT_DEATH([[maybe_unused]] bool _ = (s.begin() <= s2.begin()), deathstring);
|
||||
#else
|
||||
EXPECT_DEATH(bool _ = (s.begin() == s2.begin()), deathstring);
|
||||
EXPECT_DEATH(bool _ = (s.begin() <= s2.begin()), deathstring);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, begin_end)
|
||||
{
|
||||
std::set_terminate([] {
|
||||
@ -1484,8 +1448,12 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
TEST(span_test, as_bytes)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
std::set_terminate([] {
|
||||
std::cerr << "Expected Death. as_bytes";
|
||||
std::abort();
|
||||
});
|
||||
|
||||
int a[] = {1, 2, 3, 4};
|
||||
{
|
||||
const span<const int> s = a;
|
||||
EXPECT_TRUE(s.size() == 4);
|
||||
@ -1510,9 +1478,15 @@ TEST(span_test, from_array_constructor)
|
||||
EXPECT_TRUE(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
||||
EXPECT_TRUE(bs.size() == s.size_bytes());
|
||||
}
|
||||
|
||||
int b[5] = {1, 2, 3, 4, 5};
|
||||
{
|
||||
span<int> sp(begin(b), static_cast<size_t>(-2));
|
||||
EXPECT_DEATH((void) sp.size_bytes(), deathstring);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(span_test, as_writeable_bytes)
|
||||
TEST(span_test, as_writable_bytes)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
|
||||
@ -1521,7 +1495,7 @@ TEST(span_test, from_array_constructor)
|
||||
// you should not be able to get writeable bytes for const objects
|
||||
span<const int> s = a;
|
||||
EXPECT_TRUE(s.size() == 4);
|
||||
span<const byte> bs = as_writeable_bytes(s);
|
||||
span<const byte> bs = as_writable_bytes(s);
|
||||
EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
||||
EXPECT_TRUE(bs.size() == s.size_bytes());
|
||||
#endif
|
||||
@ -1529,7 +1503,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<int> s;
|
||||
const auto bs = as_writeable_bytes(s);
|
||||
const auto bs = as_writable_bytes(s);
|
||||
EXPECT_TRUE(bs.size() == s.size());
|
||||
EXPECT_TRUE(bs.size() == 0);
|
||||
EXPECT_TRUE(bs.size_bytes() == 0);
|
||||
@ -1539,7 +1513,7 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
{
|
||||
span<int> s = a;
|
||||
const auto bs = as_writeable_bytes(s);
|
||||
const auto bs = as_writable_bytes(s);
|
||||
EXPECT_TRUE(static_cast<void*>(bs.data()) == static_cast<void*>(s.data()));
|
||||
EXPECT_TRUE(bs.size() == s.size_bytes());
|
||||
}
|
||||
@ -1577,12 +1551,16 @@ TEST(span_test, from_array_constructor)
|
||||
|
||||
// even when done dynamically
|
||||
{
|
||||
/*
|
||||
// this now results in a compile-time error, rather than runtime.
|
||||
// There is no suitable conversion from dynamic span to fixed span.
|
||||
span<int> s = arr;
|
||||
auto f = [&]() {
|
||||
const span<int, 2> s2 = s;
|
||||
static_cast<void>(s2);
|
||||
};
|
||||
EXPECT_DEATH(f(), deathstring);
|
||||
*/
|
||||
}
|
||||
|
||||
// but doing so explicitly is ok
|
||||
@ -1597,12 +1575,19 @@ TEST(span_test, from_array_constructor)
|
||||
static_cast<void>(s1);
|
||||
}
|
||||
|
||||
// ...or dynamically
|
||||
/*
|
||||
// this is not a legal operation in std::span, so we are no longer supporting it
|
||||
// conversion from span<int, 4> to span<int, dynamic_extent> via call to `first`
|
||||
// then convert from span<int, dynamic_extent> to span<int, 1>
|
||||
// The dynamic to fixed extents are not supported in the standard
|
||||
// to make this work, span<int, 1> would need to be span<int>.
|
||||
{
|
||||
|
||||
// NB: implicit conversion to span<int,1> from span<int>
|
||||
span<int, 1> s1 = s4.first(1);
|
||||
static_cast<void>(s1);
|
||||
}
|
||||
*/
|
||||
|
||||
// initialization or assignment to static span that requires size INCREASE is not ok.
|
||||
int arr2[2] = {1, 2};
|
||||
@ -1624,12 +1609,15 @@ TEST(span_test, from_array_constructor)
|
||||
EXPECT_DEATH(f(), deathstring);
|
||||
}
|
||||
|
||||
/*
|
||||
// This no longer compiles. There is no suitable conversion from dynamic span to a fixed size span.
|
||||
// this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
|
||||
span<int> av = arr2; auto f = [&]() {
|
||||
const span<int, 4> _s4 = av;
|
||||
static_cast<void>(_s4);
|
||||
};
|
||||
EXPECT_DEATH(f(), deathstring);
|
||||
*/
|
||||
}
|
||||
|
||||
TEST(span_test, interop_with_std_regex)
|
||||
|
@ -84,7 +84,7 @@ czstring_span<> CreateTempName(string_span<> span)
|
||||
{
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
std::size_t last = 0;
|
||||
if (span.size() > 4) {
|
||||
span[0] = 't';
|
||||
span[1] = 'm';
|
||||
@ -101,7 +101,7 @@ cwzstring_span<> CreateTempNameW(wstring_span<> span)
|
||||
{
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
std::size_t last = 0;
|
||||
if (span.size() > 4) {
|
||||
span[0] = L't';
|
||||
span[1] = L'm';
|
||||
@ -118,7 +118,7 @@ cu16zstring_span<> CreateTempNameU16(u16string_span<> span)
|
||||
{
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
std::size_t last = 0;
|
||||
if (span.size() > 4) {
|
||||
span[0] = u't';
|
||||
span[1] = u'm';
|
||||
@ -135,7 +135,7 @@ cu32zstring_span<> CreateTempNameU32(u32string_span<> span)
|
||||
{
|
||||
Expects(span.size() > 1);
|
||||
|
||||
int last = 0;
|
||||
std::size_t last = 0;
|
||||
if (span.size() > 4) {
|
||||
span[0] = U't';
|
||||
span[1] = U'm';
|
||||
@ -163,14 +163,14 @@ TEST(string_span_tests, TestConstructFromStdString)
|
||||
{
|
||||
std::string s = "Hello there world";
|
||||
cstring_span<> v = s;
|
||||
EXPECT_TRUE(v.length() == static_cast<cstring_span<>::index_type>(s.length()));
|
||||
EXPECT_TRUE(v.length() == static_cast<cstring_span<>::size_type>(s.length()));
|
||||
}
|
||||
|
||||
TEST(string_span_tests, TestConstructFromStdVector)
|
||||
{
|
||||
std::vector<char> vec(5, 'h');
|
||||
string_span<> v{vec};
|
||||
EXPECT_TRUE(v.length() == static_cast<string_span<>::index_type>(vec.size()));
|
||||
EXPECT_TRUE(v.length() == static_cast<string_span<>::size_type>(vec.size()));
|
||||
}
|
||||
|
||||
TEST(string_span_tests, TestStackArrayConstruction)
|
||||
@ -232,7 +232,7 @@ TEST(string_span_tests, TestToString)
|
||||
char stack_string[] = "Hello";
|
||||
cstring_span<> v = ensure_z(stack_string);
|
||||
auto s2 = gsl::to_string(v);
|
||||
EXPECT_TRUE(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
|
||||
EXPECT_TRUE(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
|
||||
EXPECT_TRUE(s2.length() == static_cast<size_t>(5));
|
||||
}
|
||||
|
||||
@ -245,7 +245,7 @@ TEST(string_span_tests, TestToBasicString)
|
||||
char stack_string[] = "Hello";
|
||||
cstring_span<> v = ensure_z(stack_string);
|
||||
auto s2 = gsl::to_basic_string<char, std::char_traits<char>, ::std::allocator<char>>(v);
|
||||
EXPECT_TRUE(static_cast<cstring_span<>::index_type>(s2.length()) == v.length());
|
||||
EXPECT_TRUE(static_cast<cstring_span<>::size_type>(s2.length()) == v.length());
|
||||
EXPECT_TRUE(s2.length() == static_cast<size_t>(5));
|
||||
}
|
||||
|
||||
@ -1206,12 +1206,12 @@ TEST(string_span_tests, as_bytes)
|
||||
EXPECT_TRUE(bs.size() == s.size_bytes());
|
||||
}
|
||||
|
||||
TEST(string_span_tests, as_writeable_bytes)
|
||||
TEST(string_span_tests, as_writable_bytes)
|
||||
{
|
||||
wchar_t buf[]{L"qwerty"};
|
||||
wzstring_span<> v(buf);
|
||||
const auto s = v.as_string_span();
|
||||
const auto bs = as_writeable_bytes(s);
|
||||
const auto bs = as_writable_bytes(s);
|
||||
EXPECT_TRUE(static_cast<const void*>(bs.data()) == static_cast<const void*>(s.data()));
|
||||
EXPECT_TRUE(bs.size() == s.size_bytes());
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <limits> // for numeric_limits
|
||||
#include <stdint.h> // for uint32_t, int32_t
|
||||
#include <type_traits> // for is_same
|
||||
#include <cstddef> // for std::ptrdiff_t
|
||||
|
||||
using namespace gsl;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user