Skip to content

Commit

Permalink
Containers: better diagnostics for ArrayView::slice() assertions.
Browse files Browse the repository at this point in the history
Python-like. I like.
  • Loading branch information
mosra committed Mar 29, 2017
1 parent 2b688e5 commit 0bd7984
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Corrade/Containers/ArrayView.h
Expand Up @@ -560,13 +560,19 @@ template<class U, std::size_t size, class T> StaticArrayView<size*sizeof(T)/size

template<class T> ArrayView<T> ArrayView<T>::slice(T* begin, T* end) const {
CORRADE_ASSERT(_data <= begin && begin <= end && end <= _data + _size,
"Containers::ArrayView::slice(): slice out of range", nullptr);
"Containers::ArrayView::slice(): slice [" << Utility::Debug::nospace
<< begin - _data << Utility::Debug::nospace << ":"
<< Utility::Debug::nospace << end - _data << Utility::Debug::nospace
<< "] out of range for" << _size << "elements", nullptr);
return ArrayView<T>{begin, std::size_t(end - begin)};
}

template<class T> template<std::size_t viewSize> StaticArrayView<viewSize, T> ArrayView<T>::slice(T* begin) const {
CORRADE_ASSERT(_data <= begin && begin + viewSize <= _data + _size,
"Containers::ArrayView::slice(): slice out of range", nullptr);
"Containers::ArrayView::slice(): slice [" << Utility::Debug::nospace
<< begin - _data << Utility::Debug::nospace << ":"
<< Utility::Debug::nospace << begin + viewSize - _data << Utility::Debug::nospace
<< "] out of range for" << _size << "elements", nullptr);
return StaticArrayView<viewSize, T>{begin};
}

Expand Down
9 changes: 5 additions & 4 deletions src/Corrade/Containers/Test/ArrayViewTest.cpp
Expand Up @@ -305,10 +305,11 @@ void ArrayViewTest::sliceInvalid() {
a.slice(a + 2, a + 1);
a.slice<5>(1);

CORRADE_COMPARE(out.str(), "Containers::ArrayView::slice(): slice out of range\n"
"Containers::ArrayView::slice(): slice out of range\n"
"Containers::ArrayView::slice(): slice out of range\n"
"Containers::ArrayView::slice(): slice out of range\n");
CORRADE_COMPARE(out.str(),
"Containers::ArrayView::slice(): slice [-1:0] out of range for 5 elements\n"
"Containers::ArrayView::slice(): slice [5:6] out of range for 5 elements\n"
"Containers::ArrayView::slice(): slice [2:1] out of range for 5 elements\n"
"Containers::ArrayView::slice(): slice [1:6] out of range for 5 elements\n");
}

void ArrayViewTest::sliceNullptr() {
Expand Down

0 comments on commit 0bd7984

Please sign in to comment.