From c2fa384d666aea1b119736f33116ac09e870fda0 Mon Sep 17 00:00:00 2001 From: trav90 Date: Sat, 6 Oct 2018 17:29:50 -0500 Subject: Add mozilla::Span --- mfbt/Casting.h | 13 + mfbt/Range.h | 32 + mfbt/Span.h | 1041 +++++++++++++++++++++ mfbt/moz.build | 1 + mfbt/tests/gtest/TestSpan.cpp | 2079 +++++++++++++++++++++++++++++++++++++++++ mfbt/tests/gtest/moz.build | 15 + mfbt/tests/moz.build | 5 + 7 files changed, 3186 insertions(+) create mode 100644 mfbt/Span.h create mode 100644 mfbt/tests/gtest/TestSpan.cpp create mode 100644 mfbt/tests/gtest/moz.build (limited to 'mfbt') diff --git a/mfbt/Casting.h b/mfbt/Casting.h index a7d0fb50d..adf2c9045 100644 --- a/mfbt/Casting.h +++ b/mfbt/Casting.h @@ -238,6 +238,19 @@ AssertedCast(const From aFrom) return static_cast(aFrom); } +/** + * Cast a value of integral type |From| to a value of integral type |To|, + * release asserting that the cast will be a safe cast per C++ (that is, that + * |to| is in the range of values permitted for the type |From|). + */ +template +inline To +ReleaseAssertedCast(const From aFrom) +{ + MOZ_RELEASE_ASSERT((detail::IsInBounds(aFrom))); + return static_cast(aFrom); +} + } // namespace mozilla #endif /* mozilla_Casting_h */ diff --git a/mfbt/Range.h b/mfbt/Range.h index 47d91bb0c..753fe07f8 100644 --- a/mfbt/Range.h +++ b/mfbt/Range.h @@ -9,6 +9,7 @@ #include "mozilla/RangedPtr.h" #include "mozilla/TypeTraits.h" +#include "mozilla/Span.h" #include @@ -44,6 +45,19 @@ public: mEnd(aOther.mEnd) {} + MOZ_IMPLICIT Range(Span aSpan) + : Range(aSpan.Elements(), aSpan.Length()) + { + } + + template::value, + int>::Type> + MOZ_IMPLICIT Range(const Span& aSpan) + : Range(aSpan.Elements(), aSpan.Length()) + { + } + RangedPtr begin() const { return mStart; } RangedPtr end() const { return mEnd; } size_t length() const { return mEnd - mStart; } @@ -51,8 +65,26 @@ public: T& operator[](size_t aOffset) const { return mStart[aOffset]; } explicit operator bool() const { return mStart != nullptr; } + + operator Span() { return Span(mStart.get(), length()); } + + operator Span() const { return Span(mStart.get(), length()); } }; +template +Span +MakeSpan(Range& aRange) +{ + return aRange; +} + +template +Span +MakeSpan(const Range& aRange) +{ + return aRange; +} + } // namespace mozilla #endif /* mozilla_Range_h */ diff --git a/mfbt/Span.h b/mfbt/Span.h new file mode 100644 index 000000000..5acaafcbd --- /dev/null +++ b/mfbt/Span.h @@ -0,0 +1,1041 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +/////////////////////////////////////////////////////////////////////////////// + +// Adapted from https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span +// and https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util + +#ifndef mozilla_Span_h +#define mozilla_Span_h + +#include "mozilla/Array.h" +#include "mozilla/Assertions.h" +#include "mozilla/Casting.h" +#include "mozilla/IntegerTypeTraits.h" +#include "mozilla/Move.h" +#include "mozilla/TypeTraits.h" +#include "mozilla/UniquePtr.h" + +#include +#include +#include +#include + +// Classifications for reasons why constexpr was removed in C++14 to C++11 +// conversion. Once we upgrade compilers, we can try defining each of these +// to constexpr to restore a category of constexprs at a time. +#define MOZ_SPAN_ASSERTION_CONSTEXPR +#define MOZ_SPAN_GCC_CONSTEXPR +#define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR +#define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN +#define MOZ_SPAN_NON_CONST_CONSTEXPR + +#ifdef _MSC_VER +#pragma warning(push) + +// turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements +#pragma warning(disable : 4127) // conditional expression is constant + +// blanket turn off warnings from CppCoreCheck for now +// so people aren't annoyed by them when running the tool. +// more targeted suppressions will be added in a future update to the GSL +#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495) + +#if _MSC_VER < 1910 +#pragma push_macro("constexpr") +#define constexpr /*constexpr*/ + +#endif // _MSC_VER < 1910 +#endif // _MSC_VER + +namespace mozilla { + +// Stuff from gsl_util + +// narrow_cast(): a searchable way to do narrowing casts of values +template +inline constexpr T +narrow_cast(U&& u) +{ + return static_cast(mozilla::Forward(u)); +} + +// end gsl_util + +// [views.constants], constants +// This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t +// and reserving a magic value that realistically doesn't occur in +// compile-time-constant Span sizes makes things a lot less messy in terms of +// comparison between signed and unsigned. +constexpr const size_t dynamic_extent = mozilla::MaxValue::value; + +template +class Span; + +// implementation details +namespace span_details { + +// C++14 types that we don't have because we build as C++11. +template +using remove_cv_t = typename mozilla::RemoveCV::Type; +template +using remove_const_t = typename mozilla::RemoveConst::Type; +template +using conditional_t = typename mozilla::Conditional::Type; +template +using add_pointer_t = typename mozilla::AddPointer::Type; +template +using enable_if_t = typename mozilla::EnableIf::Type; + +template +struct is_span_oracle : mozilla::FalseType +{ +}; + +template +struct is_span_oracle> : mozilla::TrueType +{ +}; + +template +struct is_span : public is_span_oracle> +{ +}; + +template +struct is_std_array_oracle : mozilla::FalseType +{ +}; + +template +struct is_std_array_oracle> : mozilla::TrueType +{ +}; + +template +struct is_std_array : public is_std_array_oracle> +{ +}; + +template +struct is_allowed_extent_conversion + : public mozilla::IntegralConstant +{ +}; + +template +struct is_allowed_element_type_conversion + : public mozilla::IntegralConstant::value> +{ +}; + +template +class span_iterator +{ + using element_type_ = typename Span::element_type; + +public: + using iterator_category = std::random_access_iterator_tag; + using value_type = remove_const_t; + using difference_type = typename Span::index_type; + + using reference = conditional_t&; + using pointer = add_pointer_t; + + constexpr span_iterator() : span_iterator(nullptr, 0) {} + + MOZ_SPAN_ASSERTION_CONSTEXPR span_iterator(const Span* span, + typename Span::index_type index) + : span_(span) + , index_(index) + { + MOZ_RELEASE_ASSERT(span == nullptr || + (index_ >= 0 && index <= span_->Length())); + } + + friend class span_iterator; + constexpr MOZ_IMPLICIT span_iterator(const span_iterator& other) + : span_iterator(other.span_, other.index_) + { + } + + MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR span_iterator& + operator=(const span_iterator&) = default; + + MOZ_SPAN_GCC_CONSTEXPR reference operator*() const + { + MOZ_RELEASE_ASSERT(span_); + return (*span_)[index_]; + } + + MOZ_SPAN_GCC_CONSTEXPR pointer operator->() const + { + MOZ_RELEASE_ASSERT(span_); + return &((*span_)[index_]); + } + + MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator++() + { + MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length()); + ++index_; + return *this; + } + + MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator operator++(int) + { + auto ret = *this; + ++(*this); + return ret; + } + + MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator--() + { + MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length()); + --index_; + return *this; + } + + MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator operator--(int) + { + auto ret = *this; + --(*this); + return ret; + } + + MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN span_iterator + operator+(difference_type n) const + { + auto ret = *this; + return ret += n; + } + + MOZ_SPAN_GCC_CONSTEXPR span_iterator& operator+=(difference_type n) + { + MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 && + (index_ + n) <= span_->Length()); + index_ += n; + return *this; + } + + MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN span_iterator + operator-(difference_type n) const + { + auto ret = *this; + return ret -= n; + } + + MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator-=(difference_type n) + + { + return *this += -n; + } + + MOZ_SPAN_GCC_CONSTEXPR difference_type + operator-(const span_iterator& rhs) const + { + MOZ_RELEASE_ASSERT(span_ == rhs.span_); + return index_ - rhs.index_; + } + + constexpr reference operator[](difference_type n) const + { + return *(*this + n); + } + + constexpr friend bool operator==(const span_iterator& lhs, + const span_iterator& rhs) + { + return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_; + } + + constexpr friend bool operator!=(const span_iterator& lhs, + const span_iterator& rhs) + { + return !(lhs == rhs); + } + + MOZ_SPAN_GCC_CONSTEXPR friend bool operator<(const span_iterator& lhs, + const span_iterator& rhs) + { + MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_); + return lhs.index_ < rhs.index_; + } + + constexpr friend bool operator<=(const span_iterator& lhs, + const span_iterator& rhs) + { + return !(rhs < lhs); + } + + constexpr friend bool operator>(const span_iterator& lhs, + const span_iterator& rhs) + { + return rhs < lhs; + } + + constexpr friend bool operator>=(const span_iterator& lhs, + const span_iterator& rhs) + { + return !(rhs > lhs); + } + + void swap(span_iterator& rhs) + { + std::swap(index_, rhs.index_); + std::swap(span_, rhs.span_); + } + +protected: + const Span* span_; + size_t index_; +}; + +template +inline constexpr span_iterator +operator+(typename span_iterator::difference_type n, + const span_iterator& rhs) +{ + return rhs + n; +} + +template +class extent_type +{ +public: + using index_type = size_t; + + static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size."); + + constexpr extent_type() {} + + template + MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(extent_type ext) + { + static_assert( + Other == Ext || Other == dynamic_extent, + "Mismatch between fixed-size extent and size of initializing data."); + MOZ_RELEASE_ASSERT(ext.size() == Ext); + } + + MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(index_type length) + { + MOZ_RELEASE_ASSERT(length == Ext); + } + + constexpr index_type size() const { return Ext; } +}; + +template<> +class extent_type +{ +public: + using index_type = size_t; + + template + explicit constexpr extent_type(extent_type ext) + : size_(ext.size()) + { + } + + explicit constexpr extent_type(index_type length) + : size_(length) + { + } + + constexpr index_type size() const { return size_; } + +private: + index_type size_; +}; +} // namespace span_details + +/** + * Span - slices for C++ + * + * Span implements Rust's slice concept for C++. It's called "Span" instead of + * "Slice" to follow the naming used in C++ Core Guidelines. + * + * A Span wraps a pointer and a length that identify a non-owning view to a + * contiguous block of memory of objects of the same type. Various types, + * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array, + * mozilla::Range and contiguous standard-library containers, auto-convert + * into Spans when attempting to pass them as arguments to methods that take + * Spans. MakeSpan() functions can be used for explicit conversion in other + * contexts. (Span itself autoconverts into mozilla::Range.) + * + * Like Rust's slices, Span provides safety against out-of-bounds access by + * performing run-time bound checks. However, unlike Rust's slices, Span + * cannot provide safety against use-after-free. + * + * (Note: Span is like Rust's slice only conceptually. Due to the lack of + * ABI guarantees, you should still decompose spans/slices to raw pointer + * and length parts when crossing the FFI.) + * + * In addition to having constructors and MakeSpan() functions that take + * various well-known types, a Span for an arbitrary type can be constructed + * (via constructor or MakeSpan()) from a pointer and a length or a pointer + * and another pointer pointing just past the last element. + * + * A Span can be obtained for const char* pointing to a + * zero-terminated C string using the MakeCStringSpan() function. A + * corresponding implicit constructor does not exist in order to avoid + * accidental construction in cases where const char* does not point to a + * zero-terminated C string. + * + * Span has methods that follow the Mozilla naming style and methods that + * don't. The methods that follow the Mozilla naming style are meant to be + * used directly from Mozilla code. The methods that don't are meant for + * integration with C++11 range-based loops and with meta-programming that + * expects the same methods that are found on the standard-library + * containers. For example, to decompose a Span into its parts in Mozilla + * code, use Elements() and Length() (as with nsTArray) instead of data() + * and size() (as with std::vector). + * + * The pointer and length wrapped by a Span cannot be changed after a Span has + * been created. When new values are required, simply create a new Span. Span + * has a method called Subspan() that works analogously to the Substring() + * method of XPCOM strings taking a start index and an optional length. As a + * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is + * based on), Span has methods From(start), To(end) and FromTo(start, end) + * that correspond to Rust's &slice[start..], &slice[..end] and + * &slice[start..end], respectively. (That is, the end index is the index of + * the first element not to be included in the new subspan.) + * + * When indicating a Span that's only read from, const goes inside the type + * parameter. Don't put const in front of Span. That is: + * size_t ReadsFromOneSpanAndWritesToAnother(Span aReadFrom, + * Span aWrittenTo); + * + * Any Span can be viewed as Span using the function + * AsBytes(). Any Span can be viewed as Span using the function + * AsWritableBytes(). + */ +template +class Span +{ +public: + // constants and types + using element_type = ElementType; + using index_type = size_t; + using pointer = element_type*; + using reference = element_type&; + + using iterator = + span_details::span_iterator, false>; + using const_iterator = + span_details::span_iterator, true>; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + constexpr static const index_type extent = Extent; + + // [Span.cons], Span constructors, copy, assignment, and destructor + // "Dependent" is needed to make "span_details::enable_if_t<(Dependent || Extent == 0 || Extent == mozilla::MaxValue::value)>" SFINAE, + // since "span_details::enable_if_t<(Extent == 0 || Extent == mozilla::MaxValue::value)>" is ill-formed when Extent is neither of the extreme values. + /** + * Constructor with no args. + */ + template< + bool Dependent = false, + class = span_details::enable_if_t< + (Dependent || Extent == 0 || Extent == mozilla::MaxValue::value)>> + constexpr Span() + : storage_(nullptr, span_details::extent_type<0>()) + { + } + + /** + * Constructor for nullptr. + */ + constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {} + + /** + * Constructor for pointer and length. + */ + constexpr Span(pointer aPtr, index_type aLength) + : storage_(aPtr, aLength) + { + } + + /** + * Constructor for start pointer and pointer past end. + */ + constexpr Span(pointer aStartPtr, pointer aEndPtr) + : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) + { + } + + /** + * Constructor for C array. + */ + template + constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N]) + : storage_(&aArr[0], span_details::extent_type()) + { + } + + /** + * Constructor for std::array. + */ + template> + constexpr MOZ_IMPLICIT Span(std::array& aArr) + : storage_(&aArr[0], span_details::extent_type()) + { + } + + /** + * Constructor for const std::array. + */ + template + constexpr MOZ_IMPLICIT Span( + const std::array, N>& aArr) + : storage_(&aArr[0], span_details::extent_type()) + { + } + + /** + * Constructor for mozilla::Array. + */ + template> + constexpr MOZ_IMPLICIT Span(mozilla::Array& aArr) + : storage_(&aArr[0], span_details::extent_type()) + { + } + + /** + * Constructor for const mozilla::Array. + */ + template + constexpr MOZ_IMPLICIT Span( + const mozilla::Array, N>& aArr) + : storage_(&aArr[0], span_details::extent_type()) + { + } + + /** + * Constructor for mozilla::UniquePtr holding an array and length. + */ + template> + constexpr Span(const mozilla::UniquePtr& aPtr, + index_type aLength) + : storage_(aPtr.get(), aLength) + { + } + + // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement + // on Container to be a contiguous sequence container. + /** + * Constructor for standard-library containers. + */ + template< + class Container, + class = span_details::enable_if_t< + !span_details::is_span::value && + !span_details::is_std_array::value && + mozilla::IsConvertible::value && + mozilla::IsConvertible().data())>::value>> + constexpr MOZ_IMPLICIT Span(Container& cont) + : Span(cont.data(), ReleaseAssertedCast(cont.size())) + { + } + + /** + * Constructor for standard-library containers (const version). + */ + template< + class Container, + class = span_details::enable_if_t< + mozilla::IsConst::value && + !span_details::is_span::value && + mozilla::IsConvertible::value && + mozilla::IsConvertible().data())>::value>> + constexpr MOZ_IMPLICIT Span(const Container& cont) + : Span(cont.data(), ReleaseAssertedCast(cont.size())) + { + } + + /** + * Constructor from other Span. + */ + constexpr Span(const Span& other) = default; + + /** + * Constructor from other Span. + */ + constexpr Span(Span&& other) = default; + + /** + * Constructor from other Span with conversion of element type. + */ + template< + class OtherElementType, + size_t OtherExtent, + class = span_details::enable_if_t< + span_details::is_allowed_extent_conversion::value && + span_details::is_allowed_element_type_conversion::value>> + constexpr MOZ_IMPLICIT Span(const Span& other) + : storage_(other.data(), + span_details::extent_type(other.size())) + { + } + + /** + * Constructor from other Span with conversion of element type. + */ + template< + class OtherElementType, + size_t OtherExtent, + class = span_details::enable_if_t< + span_details::is_allowed_extent_conversion::value && + span_details::is_allowed_element_type_conversion::value>> + constexpr MOZ_IMPLICIT Span(Span&& other) + : storage_(other.data(), + span_details::extent_type(other.size())) + { + } + + ~Span() = default; + MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(const Span& other) + = default; + + MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(Span&& other) + = default; + + // [Span.sub], Span subviews + /** + * Subspan with first N elements with compile-time N. + */ + template + MOZ_SPAN_GCC_CONSTEXPR Span First() const + { + MOZ_RELEASE_ASSERT(Count <= size()); + return { data(), Count }; + } + + /** + * Subspan with last N elements with compile-time N. + */ + template + MOZ_SPAN_GCC_CONSTEXPR Span Last() const + { + MOZ_RELEASE_ASSERT(Count <= size()); + return { data() + (size() - Count), Count }; + } + + /** + * Subspan with compile-time start index and length. + */ + template + MOZ_SPAN_GCC_CONSTEXPR Span Subspan() const + { + MOZ_RELEASE_ASSERT(Offset <= size() && + (Count == dynamic_extent || (Offset + Count <= size()))); + return { data() + Offset, + Count == dynamic_extent ? size() - Offset : Count }; + } + + /** + * Subspan with first N elements with run-time N. + */ + MOZ_SPAN_GCC_CONSTEXPR Span First( + index_type aCount) const + { + MOZ_RELEASE_ASSERT(aCount <= size()); + return { data(), aCount }; + } + + /** + * Subspan with last N elements with run-time N. + */ + MOZ_SPAN_GCC_CONSTEXPR Span Last( + index_type aCount) const + { + MOZ_RELEASE_ASSERT(aCount <= size()); + return { data() + (size() - aCount), aCount }; + } + + /** + * Subspan with run-time start index and length. + */ + MOZ_SPAN_GCC_CONSTEXPR Span Subspan( + index_type aStart, + index_type aLength = dynamic_extent) const + { + MOZ_RELEASE_ASSERT(aStart <= size() && + (aLength == dynamic_extent || + (aStart + aLength <= size()))); + return { data() + aStart, + aLength == dynamic_extent ? size() - aStart : aLength }; + } + + /** + * Subspan with run-time start index. (Rust's &foo[start..]) + */ + MOZ_SPAN_GCC_CONSTEXPR Span From( + index_type aStart) const + { + return Subspan(aStart); + } + + /** + * Subspan with run-time exclusive end index. (Rust's &foo[..end]) + */ + MOZ_SPAN_GCC_CONSTEXPR Span To( + index_type aEnd) const + { + return Subspan(0, aEnd); + } + + /** + * Subspan with run-time start index and exclusive end index. + * (Rust's &foo[start..end]) + */ + MOZ_SPAN_GCC_CONSTEXPR Span FromTo( + index_type aStart, + index_type aEnd) const + { + MOZ_RELEASE_ASSERT(aStart <= aEnd); + return Subspan(aStart, aEnd - aStart); + } + + // [Span.obs], Span observers + /** + * Number of elements in the span. + */ + constexpr index_type Length() const { return size(); } + + /** + * Number of elements in the span (standard-libray duck typing version). + */ + constexpr index_type size() const { return storage_.size(); } + + /** + * Size of the span in bytes. + */ + constexpr index_type LengthBytes() const { return size_bytes(); } + + /** + * Size of the span in bytes (standard-library naming style version). + */ + constexpr index_type size_bytes() const + { + return size() * narrow_cast(sizeof(element_type)); + } + + /** + * Checks if the the length of the span is zero. + */ + constexpr bool IsEmpty() const { return empty(); } + + /** + * Checks if the the length of the span is zero (standard-libray duck + * typing version). + */ + constexpr bool empty() const { return size() == 0; } + + // [Span.elem], Span element access + MOZ_SPAN_GCC_CONSTEXPR reference operator[](index_type idx) const + { + MOZ_RELEASE_ASSERT(idx < storage_.size()); + return data()[idx]; + } + + /** + * Access element of span by index (standard-library duck typing version). + */ + constexpr reference at(index_type idx) const { return this->operator[](idx); } + + constexpr reference operator()(index_type idx) const + { + return this->operator[](idx); + } + + /** + * Pointer to the first element of the span. + */ + constexpr pointer Elements() const { return data(); } + + /** + * Pointer to the first element of the span (standard-libray duck typing version). + */ + constexpr pointer data() const { return storage_.data(); } + + // [Span.iter], Span iterator support + iterator begin() const { return { this, 0 }; } + iterator end() const { return { this, Length() }; } + + const_iterator cbegin() const { return { this, 0 }; } + const_iterator cend() const { return { this, Length() }; } + + reverse_iterator rbegin() const + { + return reverse_iterator{ end() }; + } + reverse_iterator rend() const + { + return reverse_iterator{ begin() }; + } + + const_reverse_iterator crbegin() const + { + return const_reverse_iterator{ cend() }; + } + const_reverse_iterator crend() const + { + return const_reverse_iterator{ cbegin() }; + } + +private: + // this implementation detail class lets us take advantage of the + // empty base class optimization to pay for only storage of a single + // pointer in the case of fixed-size Spans + template + class storage_type : public ExtentType + { + public: + template + MOZ_SPAN_ASSERTION_CONSTEXPR storage_type(pointer elements, + OtherExtentType ext) + : ExtentType(ext) + , data_(elements) + { + MOZ_RELEASE_ASSERT( + (!elements && ExtentType::size() == 0) || + (elements && ExtentType::size() != mozilla::MaxValue::value)); + } + + constexpr pointer data() const { return data_; } + + private: + pointer data_; + }; + + storage_type> storage_; +}; + +// [Span.comparison], Span comparison operators +template +inline constexpr bool +operator==(const Span& l, + const Span& r) +{ + return (l.size() == r.size()) && std::equal(l.begin(), l.end(), r.begin()); +} + +template +inline constexpr bool +operator!=(const Span& l, + const Span& r) +{ + return !(l == r); +} + +template +inline constexpr bool +operator<(const Span& l, + const Span& r) +{ + return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); +} + +template +inline constexpr bool +operator<=(const Span& l, + const Span& r) +{ + return !(l > r); +} + +template +inline constexpr bool +operator>(const Span& l, + const Span& r) +{ + return r < l; +} + +template +inline constexpr bool +operator>=(const Span& l, + const Span& r) +{ + return !(l < r); +} + +namespace span_details { +// if we only supported compilers with good constexpr support then +// this pair of classes could collapse down to a constexpr function + +// we should use a narrow_cast<> to go to size_t, but older compilers may not see it as +// constexpr +// and so will fail compilation of the template +template +struct calculate_byte_size + : mozilla::IntegralConstant(sizeof(ElementType) * + static_cast(Extent))> +{ +}; + +template +struct calculate_byte_size + : mozilla::IntegralConstant +{ +}; +} + +// [Span.objectrep], views of object representation +/** + * View span as Span. + */ +template +Span::value> +AsBytes(Span s) +{ + return { reinterpret_cast(s.data()), s.size_bytes() }; +} + +/** + * View span as Span. + */ +template::value>> +Span::value> +AsWritableBytes(Span s) +{ + return { reinterpret_cast(s.data()), s.size_bytes() }; +} + +// +// MakeSpan() - Utility functions for creating Spans +// +/** + * Create span from pointer and length. + */ +template +Span +MakeSpan(ElementType* aPtr, typename Span::index_type aLength) +{ + return Span(aPtr, aLength); +} + +/** + * Create span from start pointer and pointer past end. + */ +template +Span +MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr) +{ + return Span(aStartPtr, aEndPtr); +} + +/** + * Create span from C array. + */ +template +Span MakeSpan(ElementType (&aArr)[N]) +{ + return Span(aArr); +} + +/** + * Create span from mozilla::Array. + */ +template +Span +MakeSpan(mozilla::Array& aArr) +{ + return aArr; +} + +/** + * Create span from const mozilla::Array. + */ +template +Span +MakeSpan(const mozilla::Array& arr) +{ + return arr; +} + +/** + * Create span from standard-library container. + */ +template +Span +MakeSpan(Container& cont) +{ + return Span(cont); +} + +/** + * Create span from standard-library container (const version). + */ +template +Span +MakeSpan(const Container& cont) +{ + return Span(cont); +} + +/** + * Create span from smart pointer and length. + */ +template +Span +MakeSpan(Ptr& aPtr, size_t aLength) +{ + return Span(aPtr, aLength); +} + +/** + * Create span from C string. + */ +inline Span +MakeCStringSpan(const char* aStr) +{ + return Span(aStr, std::strlen(aStr)); +} + +} // namespace mozilla + +#ifdef _MSC_VER +#if _MSC_VER < 1910 +#undef constexpr +#pragma pop_macro("constexpr") + +#endif // _MSC_VER < 1910 + +#pragma warning(pop) +#endif // _MSC_VER + +#undef MOZ_SPAN_ASSERTION_CONSTEXPR +#undef MOZ_SPAN_GCC_CONSTEXPR +#undef MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR +#undef MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN +#undef MOZ_SPAN_NON_CONST_CONSTEXPR + +#endif // mozilla_Span_h diff --git a/mfbt/moz.build b/mfbt/moz.build index 897a686f4..ea3c3b701 100644 --- a/mfbt/moz.build +++ b/mfbt/moz.build @@ -82,6 +82,7 @@ EXPORTS.mozilla = [ 'SegmentedVector.h', 'SHA1.h', 'SizePrintfMacros.h', + 'Span.h', 'SplayTree.h', 'Sprintf.h', 'StaticAnalysisFunctions.h', diff --git a/mfbt/tests/gtest/TestSpan.cpp b/mfbt/tests/gtest/TestSpan.cpp new file mode 100644 index 000000000..f3aa000a4 --- /dev/null +++ b/mfbt/tests/gtest/TestSpan.cpp @@ -0,0 +1,2079 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// Copyright (c) 2015 Microsoft Corporation. All rights reserved. +// +// This code is licensed under the MIT License (MIT). +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +/////////////////////////////////////////////////////////////////////////////// + +// Adapted from https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/tests/Span_tests.cpp + +#include "gtest/gtest.h" + +#include "mozilla/Span.h" + +#include "nsString.h" +#include "nsTArray.h" +#include "mozilla/Range.h" +#include "mozilla/TypeTraits.h" + +#define SPAN_TEST(name) TEST(SpanTest, name) +#define CHECK_THROW(a, b) + +using namespace std; +using namespace mozilla; + +static_assert(IsConvertible, Span>::value, + "Range should convert into const"); +static_assert(IsConvertible, Span>::value, + "const Range should convert into const"); +static_assert(!IsConvertible, Span>::value, + "Range should not drop const in conversion"); +static_assert(IsConvertible, Range>::value, + "Span should convert into const"); +static_assert(IsConvertible, Range>::value, + "const Span should convert into const"); +static_assert(!IsConvertible, Range>::value, + "Span should not drop const in conversion"); +static_assert(IsConvertible, Span>::value, + "const Span should convert into const"); +static_assert(IsConvertible, Span>::value, + "Span should convert into const"); +static_assert(!IsConvertible, Span>::value, + "Span should not drop const in conversion"); +static_assert(IsConvertible, Span>::value, + "const nsTArray should convert into const"); +static_assert(IsConvertible, Span>::value, + "nsTArray should convert into const"); +static_assert(!IsConvertible, Span>::value, + "nsTArray should not drop const in conversion"); +static_assert(IsConvertible, Span>::value, + "nsTArray should convert into const"); +static_assert(!IsConvertible, Span>::value, + "nsTArray should not drop const in conversion"); + +namespace { +struct BaseClass +{ +}; +struct DerivedClass : BaseClass +{ +}; +} + +void +AssertSpanOfThreeInts(Span s) +{ + ASSERT_EQ(s.size(), 3U); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + ASSERT_EQ(s[2], 3); +} + +void +AssertSpanOfThreeChars(Span s) +{ + ASSERT_EQ(s.size(), 3U); + ASSERT_EQ(s[0], 'a'); + ASSERT_EQ(s[1], 'b'); + ASSERT_EQ(s[2], 'c'); +} + +void +AssertSpanOfThreeChar16s(Span s) +{ + ASSERT_EQ(s.size(), 3U); + ASSERT_EQ(s[0], 'a'); + ASSERT_EQ(s[1], 'b'); + ASSERT_EQ(s[2], 'c'); +} + +void +AssertSpanOfThreeCharsViaString(const nsACString& aStr) +{ + AssertSpanOfThreeChars(aStr); +} + +void +AssertSpanOfThreeChar16sViaString(const nsAString& aStr) +{ + AssertSpanOfThreeChar16s(aStr); +} + +SPAN_TEST(default_constructor) +{ + { + Span s; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { + Span s; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + Span s; + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), nullptr); // explains why it can't compile +#endif + } + + { + Span s{}; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{}; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } +} + +SPAN_TEST(size_optimization) +{ + { + Span s; + ASSERT_EQ(sizeof(s), sizeof(int*) + sizeof(size_t)); + } + + { + Span s; + ASSERT_EQ(sizeof(s), sizeof(int*)); + } +} + +SPAN_TEST(from_nullptr_constructor) +{ + { + Span s = nullptr; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs = nullptr; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { + Span s = nullptr; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs = nullptr; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + Span s = nullptr; + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), nullptr); // explains why it can't compile +#endif + } + + { + Span s{ nullptr }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{ nullptr }; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { + Span s{ nullptr }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{ nullptr }; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } +} + +SPAN_TEST(from_nullptr_length_constructor) +{ + { + Span s{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + + { + Span s{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } + +#if 0 + { + auto workaround_macro = []() { Span s{ nullptr, static_cast::index_type>(0) }; }; + CHECK_THROW(workaround_macro(), fail_fast); + } + + { + auto workaround_macro = []() { Span s{nullptr, 1}; }; + CHECK_THROW(workaround_macro(), fail_fast); + + auto const_workaround_macro = []() { Span cs{nullptr, 1}; }; + CHECK_THROW(const_workaround_macro(), fail_fast); + } + + { + auto workaround_macro = []() { Span s{nullptr, 1}; }; + CHECK_THROW(workaround_macro(), fail_fast); + + auto const_workaround_macro = []() { Span s{nullptr, 1}; }; + CHECK_THROW(const_workaround_macro(), fail_fast); + } +#endif + { + Span s{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + + Span cs{ nullptr, static_cast::index_type>(0) }; + ASSERT_EQ(cs.Length(), 0U); + ASSERT_EQ(cs.data(), nullptr); + } +} + +SPAN_TEST(from_pointer_length_constructor) +{ + int arr[4] = { 1, 2, 3, 4 }; + + { + Span s{ &arr[0], 2 }; + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + Span s{ &arr[0], 2 }; + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + int* p = nullptr; + Span s{ p, static_cast::index_type>(0) }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + } + +#if 0 + { + int* p = nullptr; + auto workaround_macro = [=]() { Span s{p, 2}; }; + CHECK_THROW(workaround_macro(), fail_fast); + } +#endif + + { + auto s = MakeSpan(&arr[0], 2); + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + int* p = nullptr; + auto s = MakeSpan(p, static_cast::index_type>(0)); + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + } + +#if 0 + { + int* p = nullptr; + auto workaround_macro = [=]() { MakeSpan(p, 2); }; + CHECK_THROW(workaround_macro(), fail_fast); + } +#endif +} + +SPAN_TEST(from_pointer_pointer_constructor) +{ + int arr[4] = { 1, 2, 3, 4 }; + + { + Span s{ &arr[0], &arr[2] }; + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + Span s{ &arr[0], &arr[2] }; + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + Span s{ &arr[0], &arr[0] }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ &arr[0], &arr[0] }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + // this will fail the std::distance() precondition, which asserts on MSVC debug builds + //{ + // auto workaround_macro = [&]() { Span s{&arr[1], &arr[0]}; }; + // CHECK_THROW(workaround_macro(), fail_fast); + //} + + // this will fail the std::distance() precondition, which asserts on MSVC debug builds + //{ + // int* p = nullptr; + // auto workaround_macro = [&]() { Span s{&arr[0], p}; }; + // CHECK_THROW(workaround_macro(), fail_fast); + //} + + { + int* p = nullptr; + Span s{ p, p }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + } + + { + int* p = nullptr; + Span s{ p, p }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + } + + // this will fail the std::distance() precondition, which asserts on MSVC debug builds + //{ + // int* p = nullptr; + // auto workaround_macro = [&]() { Span s{&arr[0], p}; }; + // CHECK_THROW(workaround_macro(), fail_fast); + //} + + { + auto s = MakeSpan(&arr[0], &arr[2]); + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[1], 2); + } + + { + auto s = MakeSpan(&arr[0], &arr[0]); + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + int* p = nullptr; + auto s = MakeSpan(p, p); + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), nullptr); + } +} + +SPAN_TEST(from_array_constructor) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + + { + Span s{ arr }; + ASSERT_EQ(s.Length(), 5U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.Length(), 5U); + ASSERT_EQ(s.data(), &arr[0]); + } + + int arr2d[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + } + + { + Span s{ arr }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr2d }; + ASSERT_EQ(s.Length(), 6U); + ASSERT_EQ(s.data(), &arr2d[0][0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[5], 6); + } + + { + Span s{ arr2d }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr2d[0][0]); + } + + { + Span s{ arr2d }; + } +#endif + { + Span s{ &(arr2d[0]), 1 }; + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), &arr2d[0]); + } + + int arr3d[2][3][2] = { { { 1, 2 }, { 3, 4 }, { 5, 6 } }, + { { 7, 8 }, { 9, 10 }, { 11, 12 } } }; + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr3d }; + ASSERT_EQ(s.Length(), 12U); + ASSERT_EQ(s.data(), &arr3d[0][0][0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[11], 12); + } + + { + Span s{ arr3d }; + ASSERT_EQ(s.Length(), 0U); + ASSERT_EQ(s.data(), &arr3d[0][0][0]); + } + + { + Span s{ arr3d }; + } + + { + Span s{ arr3d }; + ASSERT_EQ(s.Length(), 12U); + ASSERT_EQ(s.data(), &arr3d[0][0][0]); + ASSERT_EQ(s[0], 1); + ASSERT_EQ(s[5], 6); + } +#endif + { + Span s{ &arr3d[0], 1 }; + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), &arr3d[0]); + } + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.Length(), 5U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + auto s = MakeSpan(&(arr2d[0]), 1); + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), &arr2d[0]); + } + + { + auto s = MakeSpan(&arr3d[0], 1); + ASSERT_EQ(s.Length(), 1U); + ASSERT_EQ(s.data(), &arr3d[0]); + } +} + +SPAN_TEST(from_dynamic_array_constructor) +{ + double(*arr)[3][4] = new double[100][3][4]; + + { + Span s(&arr[0][0][0], 10); + ASSERT_EQ(s.Length(), 10U); + ASSERT_EQ(s.data(), &arr[0][0][0]); + } + + { + auto s = MakeSpan(&arr[0][0][0], 10); + ASSERT_EQ(s.Length(), 10U); + ASSERT_EQ(s.data(), &arr[0][0][0]); + } + + delete[] arr; +} + +SPAN_TEST(from_std_array_constructor) +{ + std::array arr = { { 1, 2, 3, 4 } }; + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), narrow_cast(arr.size())); + ASSERT_EQ(cs.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), narrow_cast(arr.size())); + ASSERT_EQ(cs.data(), arr.data()); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), arr.data()); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), 2U); + ASSERT_EQ(cs.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), arr.data()); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), 0U); + ASSERT_EQ(cs.data(), arr.data()); + } + + { + Span s{ arr }; + } + + { + auto get_an_array = []() -> std::array { return { 1, 2, 3, 4 }; }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary std::array + take_a_Span(get_an_array()); + } +#endif + + { + auto get_an_array = []() -> std::array { + return { { 1, 2, 3, 4 } }; + }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary std::array + take_a_Span(get_an_array()); + } + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } +} + +SPAN_TEST(from_const_std_array_constructor) +{ + const std::array arr = { { 1, 2, 3, 4 } }; + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + } +#endif + + { + auto get_an_array = []() -> const std::array { + return { { 1, 2, 3, 4 } }; + }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary std::array + take_a_Span(get_an_array()); + } + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } +} + +SPAN_TEST(from_std_array_const_constructor) +{ + std::array arr = { { 1, 2, 3, 4 } }; + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), arr.data()); + } + + { + Span s{ arr }; + } + + { + Span s{ arr }; + } +#endif + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.size())); + ASSERT_EQ(s.data(), arr.data()); + } +} + +SPAN_TEST(from_mozilla_array_constructor) +{ + mozilla::Array arr(1, 2, 3, 4); + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(cs.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(cs.data(), &arr[0]); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), 2U); + ASSERT_EQ(cs.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + + Span cs{ arr }; + ASSERT_EQ(cs.size(), 0U); + ASSERT_EQ(cs.data(), &arr[0]); + } + + { + Span s{ arr }; + } + + { + auto get_an_array = []() -> mozilla::Array { + return { 1, 2, 3, 4 }; + }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary mozilla::Array + take_a_Span(get_an_array()); + } +#endif + + { + auto get_an_array = []() -> mozilla::Array { + return { 1, 2, 3, 4 }; + }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary mozilla::Array + take_a_Span(get_an_array()); + } + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } +} + +SPAN_TEST(from_const_mozilla_array_constructor) +{ + const mozilla::Array arr(1, 2, 3, 4); + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + } +#endif + +#if 0 + { + auto get_an_array = []() -> const mozilla::Array { + return { 1, 2, 3, 4 }; + }; + auto take_a_Span = [](Span s) { static_cast(s); }; + // try to take a temporary mozilla::Array + take_a_Span(get_an_array()); + } +#endif + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } +} + +SPAN_TEST(from_mozilla_array_const_constructor) +{ + mozilla::Array arr(1, 2, 3, 4); + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s{ arr }; + ASSERT_EQ(s.size(), 2U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + ASSERT_EQ(s.size(), 0U); + ASSERT_EQ(s.data(), &arr[0]); + } + + { + Span s{ arr }; + } + + { + Span s{ arr }; + } +#endif + + { + auto s = MakeSpan(arr); + ASSERT_EQ(s.size(), narrow_cast(arr.cend() - arr.cbegin())); + ASSERT_EQ(s.data(), &arr[0]); + } +} + +SPAN_TEST(from_container_constructor) +{ + std::vector v = { 1, 2, 3 }; + const std::vector cv = v; + + { + AssertSpanOfThreeInts(v); + + Span s{ v }; + ASSERT_EQ(s.size(), narrow_cast(v.size())); + ASSERT_EQ(s.data(), v.data()); + + Span cs{ v }; + ASSERT_EQ(cs.size(), narrow_cast(v.size())); + ASSERT_EQ(cs.data(), v.data()); + } + + std::string str = "hello"; + const std::string cstr = "hello"; + + { +#ifdef CONFIRM_COMPILATION_ERRORS + Span s{ str }; + ASSERT_EQ(s.size(), narrow_cast(str.size())); + ASSERT_EQ(s.data(), str.data()); +#endif + Span cs{ str }; + ASSERT_EQ(cs.size(), narrow_cast(str.size())); + ASSERT_EQ(cs.data(), str.data()); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + Span s{ cstr }; +#endif + Span cs{ cstr }; + ASSERT_EQ(cs.size(), narrow_cast(cstr.size())); + ASSERT_EQ(cs.data(), cstr.data()); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + auto get_temp_vector = []() -> std::vector { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_vector()); +#endif + } + + { + auto get_temp_vector = []() -> std::vector { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_vector()); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + auto get_temp_string = []() -> std::string { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_string()); +#endif + } + + { + auto get_temp_string = []() -> std::string { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_string()); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + auto get_temp_vector = []() -> const std::vector { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_vector()); +#endif + } + + { + auto get_temp_string = []() -> const std::string { return {}; }; + auto use_Span = [](Span s) { static_cast(s); }; + use_Span(get_temp_string()); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + std::map m; + Span s{ m }; +#endif + } + + { + auto s = MakeSpan(v); + ASSERT_EQ(s.size(), narrow_cast(v.size())); + ASSERT_EQ(s.data(), v.data()); + + auto cs = MakeSpan(cv); + ASSERT_EQ(cs.size(), narrow_cast(cv.size())); + ASSERT_EQ(cs.data(), cv.data()); + } +} + +SPAN_TEST(from_xpcom_collections) +{ + { + nsTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + AssertSpanOfThreeInts(v); + + Span s{ v }; + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + + Span cs{ v }; + ASSERT_EQ(cs.size(), narrow_cast(v.Length())); + ASSERT_EQ(cs.data(), v.Elements()); + ASSERT_EQ(cs[2], 3); + } + { + nsTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + AssertSpanOfThreeInts(v); + + auto s = MakeSpan(v); + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + } + { + AutoTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + AssertSpanOfThreeInts(v); + + Span s{ v }; + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + + Span cs{ v }; + ASSERT_EQ(cs.size(), narrow_cast(v.Length())); + ASSERT_EQ(cs.data(), v.Elements()); + ASSERT_EQ(cs[2], 3); + } + { + AutoTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + AssertSpanOfThreeInts(v); + + auto s = MakeSpan(v); + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + } + { + FallibleTArray v; + *(v.AppendElement(fallible)) = 1; + *(v.AppendElement(fallible)) = 2; + *(v.AppendElement(fallible)) = 3; + + AssertSpanOfThreeInts(v); + + Span s{ v }; + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + + Span cs{ v }; + ASSERT_EQ(cs.size(), narrow_cast(v.Length())); + ASSERT_EQ(cs.data(), v.Elements()); + ASSERT_EQ(cs[2], 3); + } + { + FallibleTArray v; + *(v.AppendElement(fallible)) = 1; + *(v.AppendElement(fallible)) = 2; + *(v.AppendElement(fallible)) = 3; + + AssertSpanOfThreeInts(v); + + auto s = MakeSpan(v); + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + } + { + nsAutoString str; + str.AssignLiteral("abc"); + + AssertSpanOfThreeChar16s(str); + AssertSpanOfThreeChar16sViaString(str); + + Span s{ str }; + ASSERT_EQ(s.size(), narrow_cast(str.Length())); + ASSERT_EQ(s.data(), str.BeginWriting()); + ASSERT_EQ(s[2], 'c'); + + Span cs{ str }; + ASSERT_EQ(cs.size(), narrow_cast(str.Length())); + ASSERT_EQ(cs.data(), str.BeginReading()); + ASSERT_EQ(cs[2], 'c'); + } + { + nsAutoString str; + str.AssignLiteral("abc"); + + AssertSpanOfThreeChar16s(str); + AssertSpanOfThreeChar16sViaString(str); + + auto s = MakeSpan(str); + ASSERT_EQ(s.size(), narrow_cast(str.Length())); + ASSERT_EQ(s.data(), str.BeginWriting()); + ASSERT_EQ(s[2], 'c'); + } + { + nsAutoCString str; + str.AssignLiteral("abc"); + + AssertSpanOfThreeChars(str); + AssertSpanOfThreeCharsViaString(str); + + Span s{ str }; + ASSERT_EQ(s.size(), narrow_cast(str.Length())); + ASSERT_EQ(s.data(), reinterpret_cast(str.BeginWriting())); + ASSERT_EQ(s[2], 'c'); + + Span cs{ str }; + ASSERT_EQ(cs.size(), narrow_cast(str.Length())); + ASSERT_EQ(cs.data(), reinterpret_cast(str.BeginReading())); + ASSERT_EQ(cs[2], 'c'); + } + { + nsAutoCString str; + str.AssignLiteral("abc"); + + AssertSpanOfThreeChars(str); + AssertSpanOfThreeCharsViaString(str); + + auto s = MakeSpan(str); + ASSERT_EQ(s.size(), narrow_cast(str.Length())); + ASSERT_EQ(s.data(), str.BeginWriting()); + ASSERT_EQ(s[2], 'c'); + } + { + nsTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + Range r(v.Elements(), v.Length()); + + AssertSpanOfThreeInts(r); + + Span s{ r }; + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + + Span cs{ r }; + ASSERT_EQ(cs.size(), narrow_cast(v.Length())); + ASSERT_EQ(cs.data(), v.Elements()); + ASSERT_EQ(cs[2], 3); + } + { + nsTArray v; + v.AppendElement(1); + v.AppendElement(2); + v.AppendElement(3); + + Range r(v.Elements(), v.Length()); + + AssertSpanOfThreeInts(r); + + auto s = MakeSpan(r); + ASSERT_EQ(s.size(), narrow_cast(v.Length())); + ASSERT_EQ(s.data(), v.Elements()); + ASSERT_EQ(s[2], 3); + } +} + +SPAN_TEST(from_cstring) +{ + { + const char* str = "abc"; + + auto cs = MakeCStringSpan(str); + ASSERT_EQ(cs.size(), 3U); + ASSERT_EQ(cs.data(), str); + ASSERT_EQ(cs[2], 'c'); + } +} + +SPAN_TEST(from_convertible_Span_constructor){ + { + Span avd; + Span avcd = avd; + static_cast(avcd); + } + + { +#ifdef CONFIRM_COMPILATION_ERRORS + Span avd; + Span avb = avd; + static_cast(avb); +#endif + } + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s; + Span s2 = s; + static_cast(s2); + } + + { + Span s; + Span s2 = s; + static_cast(s2); + } + + { + Span s; + Span s2 = s; + static_cast(s2); + } +#endif +} + +SPAN_TEST(copy_move_and_assignment) +{ + Span s1; + ASSERT_TRUE(s1.empty()); + + int arr[] = { 3, 4, 5 }; + + Span s2 = arr; + ASSERT_EQ(s2.Length(), 3U); + ASSERT_EQ(s2.data(), &arr[0]); + + s2 = s1; + ASSERT_TRUE(s2.empty()); + + auto get_temp_Span = [&]() -> Span { return { &arr[1], 2 }; }; + auto use_Span = [&](Span s) { + ASSERT_EQ(s.Length(), 2U); + ASSERT_EQ(s.data(), &arr[1]); + }; + use_Span(get_temp_Span()); + + s1 = get_temp_Span(); + ASSERT_EQ(s1.Length(), 2U); + ASSERT_EQ(s1.data(), &arr[1]); +} + +SPAN_TEST(first) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + + { + Span av = arr; + ASSERT_EQ(av.First<2>().Length(), 2U); + ASSERT_EQ(av.First(2).Length(), 2U); + } + + { + Span av = arr; + ASSERT_EQ(av.First<0>().Length(), 0U); + ASSERT_EQ(av.First(0).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ(av.First<5>().Length(), 5U); + ASSERT_EQ(av.First(5).Length(), 5U); + } + +#if 0 + { + Span av = arr; +#ifdef CONFIRM_COMPILATION_ERRORS + ASSERT_EQ(av.First<6>().Length() , 6U); + ASSERT_EQ(av.First<-1>().Length() , -1); +#endif + CHECK_THROW(av.First(6).Length(), fail_fast); + } +#endif + + { + Span av; + ASSERT_EQ(av.First<0>().Length(), 0U); + ASSERT_EQ(av.First(0).Length(), 0U); + } +} + +SPAN_TEST(last) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + + { + Span av = arr; + ASSERT_EQ(av.Last<2>().Length(), 2U); + ASSERT_EQ(av.Last(2).Length(), 2U); + } + + { + Span av = arr; + ASSERT_EQ(av.Last<0>().Length(), 0U); + ASSERT_EQ(av.Last(0).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ(av.Last<5>().Length(), 5U); + ASSERT_EQ(av.Last(5).Length(), 5U); + } + +#if 0 + { + Span av = arr; +#ifdef CONFIRM_COMPILATION_ERRORS + ASSERT_EQ(av.Last<6>().Length() , 6U); +#endif + CHECK_THROW(av.Last(6).Length(), fail_fast); + } +#endif + + { + Span av; + ASSERT_EQ(av.Last<0>().Length(), 0U); + ASSERT_EQ(av.Last(0).Length(), 0U); + } +} + +SPAN_TEST(from_to) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + + { + Span av = arr; + ASSERT_EQ(av.From(3).Length(), 2U); + ASSERT_EQ(av.From(2)[1], 4); + } + + { + Span av = arr; + ASSERT_EQ(av.From(5).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ(av.From(0).Length(), 5U); + } + + { + Span av = arr; + ASSERT_EQ(av.To(3).Length(), 3U); + ASSERT_EQ(av.To(3)[1], 2); + } + + { + Span av = arr; + ASSERT_EQ(av.To(0).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ(av.To(5).Length(), 5U); + } + + { + Span av = arr; + ASSERT_EQ(av.FromTo(1, 4).Length(), 3U); + ASSERT_EQ(av.FromTo(1, 4)[1], 3); + } + + { + Span av = arr; + ASSERT_EQ(av.FromTo(2, 2).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ(av.FromTo(0, 5).Length(), 5U); + } +} + +SPAN_TEST(Subspan) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + + { + Span av = arr; + ASSERT_EQ((av.Subspan<2, 2>().Length()), 2U); + ASSERT_EQ(av.Subspan(2, 2).Length(), 2U); + ASSERT_EQ(av.Subspan(2, 3).Length(), 3U); + } + + { + Span av = arr; + ASSERT_EQ((av.Subspan<0, 0>().Length()), 0U); + ASSERT_EQ(av.Subspan(0, 0).Length(), 0U); + } + + { + Span av = arr; + ASSERT_EQ((av.Subspan<0, 5>().Length()), 5U); + ASSERT_EQ(av.Subspan(0, 5).Length(), 5U); + CHECK_THROW(av.Subspan(0, 6).Length(), fail_fast); + CHECK_THROW(av.Subspan(1, 5).Length(), fail_fast); + } + + { + Span av = arr; + ASSERT_EQ((av.Subspan<4, 0>().Length()), 0U); + ASSERT_EQ(av.Subspan(4, 0).Length(), 0U); + ASSERT_EQ(av.Subspan(5, 0).Length(), 0U); + CHECK_THROW(av.Subspan(6, 0).Length(), fail_fast); + } + + { + Span av; + ASSERT_EQ((av.Subspan<0, 0>().Length()), 0U); + ASSERT_EQ(av.Subspan(0, 0).Length(), 0U); + CHECK_THROW((av.Subspan<1, 0>().Length()), fail_fast); + } + + { + Span av; + ASSERT_EQ(av.Subspan(0).Length(), 0U); + CHECK_THROW(av.Subspan(1).Length(), fail_fast); + } + + { + Span av = arr; + ASSERT_EQ(av.Subspan(0).Length(), 5U); + ASSERT_EQ(av.Subspan(1).Length(), 4U); + ASSERT_EQ(av.Subspan(4).Length(), 1U); + ASSERT_EQ(av.Subspan(5).Length(), 0U); + CHECK_THROW(av.Subspan(6).Length(), fail_fast); + auto av2 = av.Subspan(1); + for (int i = 0; i < 4; ++i) + ASSERT_EQ(av2[i], i + 2); + } + + { + Span av = arr; + ASSERT_EQ(av.Subspan(0).Length(), 5U); + ASSERT_EQ(av.Subspan(1).Length(), 4U); + ASSERT_EQ(av.Subspan(4).Length(), 1U); + ASSERT_EQ(av.Subspan(5).Length(), 0U); + CHECK_THROW(av.Subspan(6).Length(), fail_fast); + auto av2 = av.Subspan(1); + for (int i = 0; i < 4; ++i) + ASSERT_EQ(av2[i], i + 2); + } +} + +SPAN_TEST(at_call) +{ + int arr[4] = { 1, 2, 3, 4 }; + + { + Span s = arr; + ASSERT_EQ(s.at(0), 1); + CHECK_THROW(s.at(5), fail_fast); + } + + { + int arr2d[2] = { 1, 6 }; + Span s = arr2d; + ASSERT_EQ(s.at(0), 1); + ASSERT_EQ(s.at(1), 6); + CHECK_THROW(s.at(2), fail_fast); + } +} + +SPAN_TEST(operator_function_call) +{ + int arr[4] = { 1, 2, 3, 4 }; + + { + Span s = arr; + ASSERT_EQ(s(0), 1); + CHECK_THROW(s(5), fail_fast); + } + + { + int arr2d[2] = { 1, 6 }; + Span s = arr2d; + ASSERT_EQ(s(0), 1); + ASSERT_EQ(s(1), 6); + CHECK_THROW(s(2), fail_fast); + } +} + +SPAN_TEST(iterator_default_init) +{ + Span::iterator it1; + Span::iterator it2; + ASSERT_EQ(it1, it2); +} + +SPAN_TEST(const_iterator_default_init) +{ + Span::const_iterator it1; + Span::const_iterator it2; + ASSERT_EQ(it1, it2); +} + +SPAN_TEST(iterator_conversions) +{ + Span::iterator badIt; + Span::const_iterator badConstIt; + ASSERT_EQ(badIt, badConstIt); + + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + auto it = s.begin(); + auto cit = s.cbegin(); + + ASSERT_EQ(it, cit); + ASSERT_EQ(cit, it); + + Span::const_iterator cit2 = it; + ASSERT_EQ(cit2, cit); + + Span::const_iterator cit3 = it + 4; + ASSERT_EQ(cit3, s.cend()); +} + +SPAN_TEST(iterator_comparisons) +{ + int a[] = { 1, 2, 3, 4 }; + { + Span s = a; + Span::iterator it = s.begin(); + auto it2 = it + 1; + Span::const_iterator cit = s.cbegin(); + + ASSERT_EQ(it, cit); + ASSERT_EQ(cit, it); + ASSERT_EQ(it, it); + ASSERT_EQ(cit, cit); + ASSERT_EQ(cit, s.begin()); + ASSERT_EQ(s.begin(), cit); + ASSERT_EQ(s.cbegin(), cit); + ASSERT_EQ(it, s.begin()); + ASSERT_EQ(s.begin(), it); + + ASSERT_NE(it, it2); + ASSERT_NE(it2, it); + ASSERT_NE(it, s.end()); + ASSERT_NE(it2, s.end()); + ASSERT_NE(s.end(), it); + ASSERT_NE(it2, cit); + ASSERT_NE(cit, it2); + + ASSERT_LT(it, it2); + ASSERT_LE(it, it2); + ASSERT_LE(it2, s.end()); + ASSERT_LT(it, s.end()); + ASSERT_LE(it, cit); + ASSERT_LE(cit, it); + ASSERT_LT(cit, it2); + ASSERT_LE(cit, it2); + ASSERT_LT(cit, s.end()); + ASSERT_LE(cit, s.end()); + + ASSERT_GT(it2, it); + ASSERT_GE(it2, it); + ASSERT_GT(s.end(), it2); + ASSERT_GE(s.end(), it2); + ASSERT_GT(it2, cit); + ASSERT_GE(it2, cit); + } +} + +SPAN_TEST(begin_end) +{ + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + Span::iterator it = s.begin(); + Span::iterator it2 = std::begin(s); + ASSERT_EQ(it, it2); + + it = s.end(); + it2 = std::end(s); + ASSERT_EQ(it, it2); + } + + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + auto it = s.begin(); + auto first = it; + ASSERT_EQ(it, first); + ASSERT_EQ(*it, 1); + + auto beyond = s.end(); + ASSERT_NE(it, beyond); + CHECK_THROW(*beyond, fail_fast); + + ASSERT_EQ(beyond - first, 4U); + ASSERT_EQ(first - first, 0U); + ASSERT_EQ(beyond - beyond, 0U); + + ++it; + ASSERT_EQ(it - first, 1U); + ASSERT_EQ(*it, 2); + *it = 22; + ASSERT_EQ(*it, 22); + ASSERT_EQ(beyond - it, 3U); + + it = first; + ASSERT_EQ(it, first); + while (it != s.end()) { + *it = 5; + ++it; + } + + ASSERT_EQ(it, beyond); + ASSERT_EQ(it - beyond, 0U); + + for (auto& n : s) { + ASSERT_EQ(n, 5); + } + } +} + +SPAN_TEST(cbegin_cend) +{ +#if 0 + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + Span::const_iterator cit = s.cbegin(); + Span::const_iterator cit2 = std::cbegin(s); + ASSERT_EQ(cit , cit2); + + cit = s.cend(); + cit2 = std::cend(s); + ASSERT_EQ(cit , cit2); + } +#endif + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + auto it = s.cbegin(); + auto first = it; + ASSERT_EQ(it, first); + ASSERT_EQ(*it, 1); + + auto beyond = s.cend(); + ASSERT_NE(it, beyond); + CHECK_THROW(*beyond, fail_fast); + + ASSERT_EQ(beyond - first, 4U); + ASSERT_EQ(first - first, 0U); + ASSERT_EQ(beyond - beyond, 0U); + + ++it; + ASSERT_EQ(it - first, 1U); + ASSERT_EQ(*it, 2); + ASSERT_EQ(beyond - it, 3U); + + int last = 0; + it = first; + ASSERT_EQ(it, first); + while (it != s.cend()) { + ASSERT_EQ(*it, last + 1); + + last = *it; + ++it; + } + + ASSERT_EQ(it, beyond); + ASSERT_EQ(it - beyond, 0U); + } +} + +SPAN_TEST(rbegin_rend) +{ + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + auto it = s.rbegin(); + auto first = it; + ASSERT_EQ(it, first); + ASSERT_EQ(*it, 4); + + auto beyond = s.rend(); + ASSERT_NE(it, beyond); + CHECK_THROW(*beyond, fail_fast); + + ASSERT_EQ(beyond - first, 4U); + ASSERT_EQ(first - first, 0U); + ASSERT_EQ(beyond - beyond, 0U); + + ++it; + ASSERT_EQ(it - first, 1U); + ASSERT_EQ(*it, 3); + *it = 22; + ASSERT_EQ(*it, 22); + ASSERT_EQ(beyond - it, 3U); + + it = first; + ASSERT_EQ(it, first); + while (it != s.rend()) { + *it = 5; + ++it; + } + + ASSERT_EQ(it, beyond); + ASSERT_EQ(it - beyond, 0U); + + for (auto& n : s) { + ASSERT_EQ(n, 5); + } + } +} + +SPAN_TEST(crbegin_crend) +{ + { + int a[] = { 1, 2, 3, 4 }; + Span s = a; + + auto it = s.crbegin(); + auto first = it; + ASSERT_EQ(it, first); + ASSERT_EQ(*it, 4); + + auto beyond = s.crend(); + ASSERT_NE(it, beyond); + CHECK_THROW(*beyond, fail_fast); + + ASSERT_EQ(beyond - first, 4U); + ASSERT_EQ(first - first, 0U); + ASSERT_EQ(beyond - beyond, 0U); + + ++it; + ASSERT_EQ(it - first, 1U); + ASSERT_EQ(*it, 3); + ASSERT_EQ(beyond - it, 3U); + + it = first; + ASSERT_EQ(it, first); + int last = 5; + while (it != s.crend()) { + ASSERT_EQ(*it, last - 1); + last = *it; + + ++it; + } + + ASSERT_EQ(it, beyond); + ASSERT_EQ(it - beyond, 0U); + } +} + +SPAN_TEST(comparison_operators) +{ + { + Span s1 = nullptr; + Span s2 = nullptr; + ASSERT_EQ(s1, s2); + ASSERT_FALSE(s1 != s2); + ASSERT_FALSE(s1 < s2); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s1, s2); + ASSERT_EQ(s2, s1); + ASSERT_FALSE(s2 != s1); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s2, s1); + ASSERT_FALSE(s2 > s1); + ASSERT_GE(s2, s1); + } + + { + int arr[] = { 2, 1 }; + Span s1 = arr; + Span s2 = arr; + + ASSERT_EQ(s1, s2); + ASSERT_FALSE(s1 != s2); + ASSERT_FALSE(s1 < s2); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s1, s2); + ASSERT_EQ(s2, s1); + ASSERT_FALSE(s2 != s1); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s2, s1); + ASSERT_FALSE(s2 > s1); + ASSERT_GE(s2, s1); + } + + { + int arr[] = { 2, 1 }; // bigger + + Span s1 = nullptr; + Span s2 = arr; + + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_LT(s1, s2); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s2 <= s1); + ASSERT_GT(s2, s1); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s2, s1); + ASSERT_FALSE(s1 >= s2); + } + + { + int arr1[] = { 1, 2 }; + int arr2[] = { 1, 2 }; + Span s1 = arr1; + Span s2 = arr2; + + ASSERT_EQ(s1, s2); + ASSERT_FALSE(s1 != s2); + ASSERT_FALSE(s1 < s2); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s1, s2); + ASSERT_EQ(s2, s1); + ASSERT_FALSE(s2 != s1); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s2, s1); + ASSERT_FALSE(s2 > s1); + ASSERT_GE(s2, s1); + } + + { + int arr[] = { 1, 2, 3 }; + + AssertSpanOfThreeInts(arr); + + Span s1 = { &arr[0], 2 }; // shorter + Span s2 = arr; // longer + + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_LT(s1, s2); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s2 <= s1); + ASSERT_GT(s2, s1); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s2, s1); + ASSERT_FALSE(s1 >= s2); + } + + { + int arr1[] = { 1, 2 }; // smaller + int arr2[] = { 2, 1 }; // bigger + + Span s1 = arr1; + Span s2 = arr2; + + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_NE(s1, s2); + ASSERT_NE(s2, s1); + ASSERT_LT(s1, s2); + ASSERT_FALSE(s2 < s1); + ASSERT_LE(s1, s2); + ASSERT_FALSE(s2 <= s1); + ASSERT_GT(s2, s1); + ASSERT_FALSE(s1 > s2); + ASSERT_GE(s2, s1); + ASSERT_FALSE(s1 >= s2); + } +} + +SPAN_TEST(as_bytes) +{ + int a[] = { 1, 2, 3, 4 }; + + { + Span s = a; + ASSERT_EQ(s.Length(), 4U); + Span bs = AsBytes(s); + ASSERT_EQ(static_cast(bs.data()), + static_cast(s.data())); + ASSERT_EQ(bs.Length(), s.LengthBytes()); + } + + { + Span s; + auto bs = AsBytes(s); + ASSERT_EQ(bs.Length(), s.Length()); + ASSERT_EQ(bs.Length(), 0U); + ASSERT_EQ(bs.size_bytes(), 0U); + ASSERT_EQ(static_cast(bs.data()), + static_cast(s.data())); + ASSERT_EQ(bs.data(), nullptr); + } + + { + Span s = a; + auto bs = AsBytes(s); + ASSERT_EQ(static_cast(bs.data()), + static_cast(s.data())); + ASSERT_EQ(bs.Length(), s.LengthBytes()); + } +} + +SPAN_TEST(as_writable_bytes) +{ + int a[] = { 1, 2, 3, 4 }; + + { +#ifdef CONFIRM_COMPILATION_ERRORS + // you should not be able to get writeable bytes for const objects + Span s = a; + ASSERT_EQ(s.Length(), 4U); + Span bs = AsWritableBytes(s); + ASSERT_EQ(static_cast(bs.data()), static_cast(s.data())); + ASSERT_EQ(bs.Length(), s.LengthBytes()); +#endif + } + + { + Span s; + auto bs = AsWritableBytes(s); + ASSERT_EQ(bs.Length(), s.Length()); + ASSERT_EQ(bs.Length(), 0U); + ASSERT_EQ(bs.size_bytes(), 0U); + ASSERT_EQ(static_cast(bs.data()), static_cast(s.data())); + ASSERT_EQ(bs.data(), nullptr); + } + + { + Span s = a; + auto bs = AsWritableBytes(s); + ASSERT_EQ(static_cast(bs.data()), static_cast(s.data())); + ASSERT_EQ(bs.Length(), s.LengthBytes()); + } +} + +SPAN_TEST(fixed_size_conversions) +{ + int arr[] = { 1, 2, 3, 4 }; + + // converting to an Span from an equal size array is ok + Span s4 = arr; + ASSERT_EQ(s4.Length(), 4U); + + // converting to dynamic_range is always ok + { + Span s = s4; + ASSERT_EQ(s.Length(), s4.Length()); + static_cast(s); + } + +// initialization or assignment to static Span that REDUCES size is NOT ok +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s = arr; + } + { + Span s2 = s4; + static_cast(s2); + } +#endif + +#if 0 + // even when done dynamically + { + Span s = arr; + auto f = [&]() { + Span s2 = s; + static_cast(s2); + }; + CHECK_THROW(f(), fail_fast); + } +#endif + + // but doing so explicitly is ok + + // you can convert statically + { + Span s2 = { arr, 2 }; + static_cast(s2); + } + { + Span s1 = s4.First<1>(); + static_cast(s1); + } + + // ...or dynamically + { + // NB: implicit conversion to Span from Span + Span s1 = s4.First(1); + static_cast(s1); + } + +#if 0 + // initialization or assignment to static Span that requires size INCREASE is not ok. + int arr2[2] = {1, 2}; +#endif + +#ifdef CONFIRM_COMPILATION_ERRORS + { + Span s3 = arr2; + } + { + Span s2 = arr2; + Span s4a = s2; + } +#endif + +#if 0 + { + auto f = [&]() { + Span _s4 = {arr2, 2}; + static_cast(_s4); + }; + CHECK_THROW(f(), fail_fast); + } + + // this should fail - we are trying to assign a small dynamic Span to a fixed_size larger one + Span av = arr2; + auto f = [&]() { + Span _s4 = av; + static_cast(_s4); + }; + CHECK_THROW(f(), fail_fast); +#endif +} + +#if 0 + SPAN_TEST(interop_with_std_regex) + { + char lat[] = { '1', '2', '3', '4', '5', '6', 'E', 'F', 'G' }; + Span s = lat; + auto f_it = s.begin() + 7; + + std::match_results::iterator> match; + + std::regex_match(s.begin(), s.end(), match, std::regex(".*")); + ASSERT_EQ(match.ready()); + ASSERT_TRUE(!match.empty()); + ASSERT_TRUE(match[0].matched); + ASSERT_TRUE(match[0].first , s.begin()); + ASSERT_EQ(match[0].second , s.end()); + + std::regex_search(s.begin(), s.end(), match, std::regex("F")); + ASSERT_TRUE(match.ready()); + ASSERT_TRUE(!match.empty()); + ASSERT_TRUE(match[0].matched); + ASSERT_EQ(match[0].first , f_it); + ASSERT_EQ(match[0].second , (f_it + 1)); + } + +SPAN_TEST(interop_with_gsl_at) +{ + int arr[5] = { 1, 2, 3, 4, 5 }; + Span s{ arr }; + ASSERT_EQ(at(s, 0) , 1 ); +ASSERT_EQ(at(s, 1) , 2U); +} +#endif + +SPAN_TEST(default_constructible) +{ + ASSERT_TRUE((std::is_default_constructible>::value)); + ASSERT_TRUE((std::is_default_constructible>::value)); + ASSERT_TRUE((!std::is_default_constructible>::value)); +} diff --git a/mfbt/tests/gtest/moz.build b/mfbt/tests/gtest/moz.build new file mode 100644 index 000000000..bd559d60b --- /dev/null +++ b/mfbt/tests/gtest/moz.build @@ -0,0 +1,15 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +UNIFIED_SOURCES += [ + 'TestSpan.cpp', +] + +#LOCAL_INCLUDES += [ +# '../../base', +#] + +FINAL_LIBRARY = 'xul-gtest' diff --git a/mfbt/tests/moz.build b/mfbt/tests/moz.build index bd25ab1d0..e69de5d75 100644 --- a/mfbt/tests/moz.build +++ b/mfbt/tests/moz.build @@ -4,6 +4,11 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +if not CONFIG['JS_STANDALONE']: + TEST_DIRS += [ + 'gtest', + ] + CppUnitTests([ 'TestArray', 'TestArrayUtils', -- cgit v1.2.3