Building again. Some tests failing.

This commit is contained in:
Neil MacIntosh 2015-10-23 19:49:17 -07:00
parent d0f09e768e
commit ace9ab9d3b
2 changed files with 103 additions and 293 deletions

View File

@ -98,7 +98,8 @@ class index final
public: public:
static const size_t rank = Rank; static const size_t rank = Rank;
using value_type = std::remove_reference_t<ValueType>; using value_type = std::ptrdiff_t;
using size_type = value_type;
using reference = std::add_lvalue_reference_t<value_type>; using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>; using const_reference = std::add_lvalue_reference_t<std::add_const_t<value_type>>;
@ -118,30 +119,6 @@ public:
} }
constexpr index(const index& other) noexcept = default; constexpr index(const index& other) noexcept = default;
// copy from index over smaller domain
template <typename OtherValueType,
bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value <= details::SizeTypeTraits<value_type>::max_value),
typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
constexpr index(const index<Rank, OtherValueType>& other) noexcept
{
std::copy(other.elems, other.elems + Rank, elems);
}
// copy from index over larger domain
template <typename OtherValueType,
bool Enabled = (details::SizeTypeTraits<OtherValueType>::max_value > details::SizeTypeTraits<value_type>::max_value),
typename Other = std::enable_if_t<Enabled, index<Rank, OtherValueType>>>
constexpr index(const index<Rank, OtherValueType>& other, void* ptr = 0) noexcept
{
bool ok = std::accumulate(other.elems, other.elems + Rank, true,
[&](bool b, OtherValueType val) { return b && (val <= static_cast<OtherValueType>(details::SizeTypeTraits<value_type>::max_value)); }
);
fail_fast_assert(ok, "other value must fit in the new domain");
std::transform(other.elems, other.elems + rank, elems, [&](OtherValueType val) { return static_cast<value_type>(val); });
}
constexpr index& operator=(const index& rhs) noexcept = default; constexpr index& operator=(const index& rhs) noexcept = default;
// Preconditions: component_idx < rank // Preconditions: component_idx < rank
@ -196,13 +173,13 @@ public:
constexpr index& operator+=(const index& rhs) noexcept constexpr index& operator+=(const index& rhs) noexcept
{ {
std::transform(elems, elems + rank, rhs.elems, elems, std::plus<ValueType>{}); std::transform(elems, elems + rank, rhs.elems, elems, std::plus<value_type>{});
return *this; return *this;
} }
constexpr index& operator-=(const index& rhs) noexcept constexpr index& operator-=(const index& rhs) noexcept
{ {
std::transform(elems, elems + rank, rhs.elems, elems, std::minus<ValueType>{}); std::transform(elems, elems + rank, rhs.elems, elems, std::minus<value_type>{});
return *this; return *this;
} }
@ -227,13 +204,13 @@ public:
constexpr index& operator*=(value_type v) noexcept constexpr index& operator*=(value_type v) noexcept
{ {
std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<ValueType>{}(x, v); }); std::transform(elems, elems + rank, elems, [v](value_type x) { return std::multiplies<value_type>{}(x, v); });
return *this; return *this;
} }
constexpr index& operator/=(value_type v) noexcept constexpr index& operator/=(value_type v) noexcept
{ {
std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<ValueType>{}(x, v); }); std::transform(elems, elems + rank, elems, [v](value_type x) { return std::divides<value_type>{}(x, v); });
return *this; return *this;
} }
@ -251,28 +228,12 @@ public:
using reference = std::add_lvalue_reference_t<std::ptrdiff_t>; using reference = std::add_lvalue_reference_t<std::ptrdiff_t>;
using const_reference = const std::ptrdiff_t&;//std::add_const_t<std::add_lvalue_reference_t<std::ptrdiff_t>>; using const_reference = const std::ptrdiff_t&;//std::add_const_t<std::add_lvalue_reference_t<std::ptrdiff_t>>;
template <size_t>
friend class index;
constexpr index() noexcept : value(0) constexpr index() noexcept : value(0)
{} {}
constexpr index(value_type e0) noexcept : value(e0)
{}
constexpr index(const value_type(&values)[1]) noexcept : index(values[0])
{}
// Preconditions: il.size() == rank
constexpr index(std::initializer_list<value_type> il)
{
fail_fast_assert(il.size() == rank, "Size of the initializer list must match the rank of the array");
value = begin(il)[0];
}
template <size_t, typename OtherValueType>
friend class index;
constexpr index() noexcept : value(0)
{}
constexpr index(value_type e) noexcept : value(e) constexpr index(value_type e) noexcept : value(e)
{} {}
@ -416,6 +377,11 @@ namespace details
static const size_type CurrentRange = 1; static const size_type CurrentRange = 1;
static const size_type TotalSize = 1; static const size_type TotalSize = 1;
// TODO : following signature is for work around VS bug
template <typename OtherRange>
BoundsRanges(const OtherRange&, bool /* firstLevel */)
{}
BoundsRanges (const BoundsRanges&) = default; BoundsRanges (const BoundsRanges&) = default;
BoundsRanges(const size_type* const) { } BoundsRanges(const size_type* const) { }
BoundsRanges() = default; BoundsRanges() = default;
@ -529,8 +495,7 @@ namespace details
static const size_t Depth = Base::Depth + 1; static const size_t Depth = Base::Depth + 1;
static const size_t DynamicNum = Base::DynamicNum; static const size_t DynamicNum = Base::DynamicNum;
static const size_type CurrentRange = CurRange; static const size_type CurrentRange = CurRange;
static const size_type TotalSize = CurrentRange; static const size_type TotalSize = Base::TotalSize == dynamic_range ? dynamic_range : CurrentRange * Base::TotalSize;
static_assert (CurRange <= PTRDIFF_MAX, "CurRange must be smaller than std::ptrdiff_t limits");
BoundsRanges (const BoundsRanges&) = default; BoundsRanges (const BoundsRanges&) = default;
BoundsRanges(const size_type* const arr) : Base(arr) { } BoundsRanges(const size_type* const arr) : Base(arr) { }
@ -645,8 +610,8 @@ namespace details
return TypeListIndexer<TypeChain>(obj); return TypeListIndexer<TypeChain>(obj);
} }
template <size_t Rank, typename ValueType, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1, ValueType>>> template <size_t Rank, bool Enabled = (Rank > 1), typename Ret = std::enable_if_t<Enabled, index<Rank - 1>>>
constexpr Ret shift_left(const index<Rank, ValueType>& other) noexcept constexpr Ret shift_left(const index<Rank>& other) noexcept
{ {
Ret ret; Ret ret;
for (size_t i = 0; i < Rank - 1; ++i) for (size_t i = 0; i < Rank - 1; ++i)
@ -787,40 +752,30 @@ public:
}; };
template <size_t Rank> template <size_t Rank>
class strided_bounds : private details::coordinate_facade<strided_bounds<Rank>, Rank> class strided_bounds
{ {
using Base = details::coordinate_facade<strided_bounds<Rank>, Rank>;
friend Base;
template <size_t OtherRank> template <size_t OtherRank>
friend class strided_bounds; friend class strided_bounds;
public: public:
static const size_t rank = Rank; static const size_t rank = Rank;
using reference = SizeType&; using value_type = std::ptrdiff_t;
using const_reference = const SizeType&; using reference = std::add_lvalue_reference_t<value_type>;
using size_type = SizeType; using const_reference = std::add_const_t<reference>;
using difference_type = SizeType; using size_type = value_type;
using value_type = SizeType; using difference_type = value_type;
using index_type = index<rank, size_type>; using index_type = index<rank>;
using iterator = bounds_iterator<index_type>; using iterator = bounds_iterator<index_type>;
using const_iterator = bounds_iterator<index_type>; using const_iterator = bounds_iterator<index_type>;
static const size_t dynamic_rank = rank; static const size_t dynamic_rank = rank;
static const std::ptrdiff_t static_size = dynamic_range; static const value_type static_size = dynamic_range;
using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>; using sliced_type = std::conditional_t<rank != 0, strided_bounds<rank - 1>, void>;
using mapping_type = generalized_mapping_tag; using mapping_type = generalized_mapping_tag;
constexpr strided_bounds(const strided_bounds &) noexcept = default; constexpr strided_bounds(const strided_bounds &) noexcept = default;
constexpr strided_bounds(const index_type& extents, const index_type& strides)
: m_strides(strides)
{
for (size_t i = 0; i < rank; i++)
Base::elems[i] = extents[i];
}
constexpr strided_bounds(const value_type(&values)[rank], index_type strides) constexpr strided_bounds(const value_type(&values)[rank], index_type strides)
: Base(values), m_strides(std::move(strides)) : m_extents(values), m_strides(std::move(strides))
{} {}
constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept constexpr strided_bounds(const index_type &extents, const index_type &strides) noexcept
@ -1487,7 +1442,7 @@ namespace details
struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {}; struct ArrayViewArrayTraits<T[N], Ranks...> : ArrayViewArrayTraits<T, Ranks..., N> {};
template <typename BoundsType> template <typename BoundsType>
BoundsType newBoundsHelperImpl(std::ptrdiff_t , std::true_type) // dynamic size BoundsType newBoundsHelperImpl(std::ptrdiff_t totalSize, std::true_type) // dynamic size
{ {
fail_fast_assert(totalSize <= PTRDIFF_MAX); fail_fast_assert(totalSize <= PTRDIFF_MAX);
return BoundsType{totalSize}; return BoundsType{totalSize};
@ -1584,26 +1539,25 @@ public:
} }
// default // default
template <size_t DynamicRank = bounds_type::dynamic_rank, typename Dummy = std::enable_if_t<DynamicRank != 0>> template <size_t DynamicRank = bounds_type::dynamic_rank, typename = std::enable_if_t<DynamicRank != 0>>
constexpr array_view() : Base(nullptr, bounds_type()) constexpr array_view() : Base(nullptr, bounds_type())
{} {}
// from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer) // from n-dimensions dynamic array (e.g. new int[m][4]) (precedence will be lower than the 1-dimension pointer)
template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range> template <typename T, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range>
typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>> /*typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type (*)[], typename Base::value_type (*)[]>::value>*/>
constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size}) constexpr array_view(T* const& data, size_type size) : Base(data, typename Helper::bounds_type{size})
{} {}
// from n-dimensions static array // from n-dimensions static array
template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, dynamic_range> template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
typename Dummy = std::enable_if_t<std::is_convertible<typename Helper::value_type(*)[], typename Base::value_type(*)[]>::value> typename = std::enable_if_t<std::is_convertible<Helper::value_type(*)[], typename Base::value_type(*)[]>::value>>
> constexpr array_view (T (&arr)[N]) : Base(arr, Helper::bounds_type())
constexpr array_view (T (&arr)[N]) : Base(arr, typename Helper::bounds_type())
{} {}
// from n-dimensions static array with size // from n-dimensions static array with size
template <typename T, size_t N, template <typename T, size_t N, typename Helper = details::ArrayViewArrayTraits<T, N>,
typename Dummy = std::enable_if_t<std::is_convertible<T(*)[], typename Base::value_type(*)[]>::value> typename Dummy = std::enable_if_t<std::is_convertible<Helper::value_type(*)[], typename Base::value_type(*)[]>::value>
> >
constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size }) constexpr array_view(T(&arr)[N], size_type size) : Base(arr, typename Helper::bounds_type{ size })
{ {
@ -1680,22 +1634,22 @@ public:
} }
// from bytes array // from bytes array
template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> template<typename U, bool IsByte = std::is_same<value_type, const byte>::value, typename = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
constexpr auto as_array_view() const noexcept -> array_view<const U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : dynamic_range)> constexpr auto as_array_view() const noexcept -> array_view<const U, dynamic_range, (Base::bounds_type::static_size != dynamic_range ? static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U) : dynamic_range)>
{ {
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0), static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_type>(sizeof(U)) == 0),
"Target type must be standard layout and its size must match the byte array size"); "Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0); fail_fast_assert((this->bytes() % sizeof(U)) == 0 && (this->bytes() / sizeof(U)) < PTRDIFF_MAX);
return { reinterpret_cast<const U*>(this->data()), this->bytes() / sizeof(U) }; return { reinterpret_cast<const U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
} }
template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>> template<typename U, bool IsByte = std::is_same<value_type, byte>::value, typename Dummy = std::enable_if_t<IsByte && sizeof...(RestDimensions) == 0>>
constexpr auto as_array_view() const noexcept -> array_view<U, (Base::bounds_type::dynamic_rank == 0 ? Base::bounds_type::static_size / sizeof(U) : dynamic_range)> constexpr auto as_array_view() const noexcept -> array_view<U, dynamic_range, (Base::bounds_type::static_size != dynamic_range ? static_cast<size_t>(Base::bounds_type::static_size) / sizeof(U) : dynamic_range)>
{ {
static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % sizeof(U) == 0), static_assert(std::is_standard_layout<U>::value && (Base::bounds_type::static_size == dynamic_range || Base::bounds_type::static_size % static_cast<size_t>(sizeof(U)) == 0),
"Target type must be standard layout and its size must match the byte array size"); "Target type must be standard layout and its size must match the byte array size");
fail_fast_assert((this->bytes() % sizeof(U)) == 0); fail_fast_assert((this->bytes() % sizeof(U)) == 0);
return { reinterpret_cast<U*>(this->data()), this->bytes() / sizeof(U) }; return { reinterpret_cast<U*>(this->data()), this->bytes() / static_cast<size_type>(sizeof(U)) };
} }
// section on linear space // section on linear space
@ -1832,7 +1786,8 @@ template <typename Cont>
constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value, constexpr auto as_array_view(Cont &arr) -> std::enable_if_t<!details::is_array_view<std::decay_t<Cont>>::value,
array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>> array_view<std::remove_reference_t<decltype(arr.size(), *arr.data())>, dynamic_range>>
{ {
return {arr.data(), arr.size()}; fail_fast_assert(arr.size() < PTRDIFF_MAX);
return {arr.data(), static_cast<std::ptrdiff_t>(arr.size())};
} }
template <typename Cont> template <typename Cont>
@ -1892,7 +1847,7 @@ public:
strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const strided_array_view<typename std::enable_if<std::is_same<value_type, const byte>::value, OtherValueType>::type, rank> as_strided_array_view() const
{ {
static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes"); static_assert((sizeof(OtherValueType) >= sizeof(value_type)) && (sizeof(OtherValueType) % sizeof(value_type) == 0), "OtherValueType should have a size to contain a multiple of ValueTypes");
auto d = sizeof(OtherValueType) / sizeof(value_type); auto d = static_cast<size_type>(sizeof(OtherValueType) / sizeof(value_type));
size_type size = this->bounds().total_size() / d; size_type size = this->bounds().total_size() / d;
return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} }; return{ (OtherValueType*)this->data(), size, bounds_type{ resize_extent(this->bounds().index_bounds(), d), resize_stride(this->bounds().strides(), d)} };
@ -1917,7 +1872,7 @@ public:
} }
private: private:
static index_type resize_extent(const index_type& extent, size_t d) static index_type resize_extent(const index_type& extent, std::ptrdiff_t d)
{ {
fail_fast_assert(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements"); fail_fast_assert(extent[rank - 1] >= d && (extent[rank-1] % d == 0), "The last dimension of the array needs to contain a multiple of new type elements");
@ -1928,7 +1883,7 @@ private:
} }
template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (rank == 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, size_t , void * = 0) static index_type resize_stride(const index_type& strides, std::ptrdiff_t , void * = 0)
{ {
fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
@ -1936,7 +1891,7 @@ private:
} }
template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>> template <bool Enabled = (rank > 1), typename Dummy = std::enable_if_t<Enabled>>
static index_type resize_stride(const index_type& strides, size_t d) static index_type resize_stride(const index_type& strides, std::ptrdiff_t d)
{ {
fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized"); fail_fast_assert(strides[rank - 1] == 1, "Only strided arrays with regular strides can be resized");
fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements"); fail_fast_assert(strides[rank - 2] >= d && (strides[rank - 2] % d == 0), "The strides must have contiguous chunks of memory that can contain a multiple of new type elements");

View File

@ -227,11 +227,12 @@ SUITE(array_view_tests)
#endif #endif
} }
} }
template <class Bounds> void fn(Bounds& b) { static_assert(Bounds::static_size == 60, "static bounds is wrong size"); }
TEST (array_view_reshape_test) TEST (array_view_reshape_test)
{ {
int a[3][4][5]; int a[3][4][5];
auto av = as_array_view(a); auto av = as_array_view(a);
fn(av.bounds());
auto av2 = av.as_array_view(dim<60>()); auto av2 = av.as_array_view(dim<60>());
auto av3 = av2.as_array_view(dim<3>(), dim<4>(), dim<5>()); auto av3 = av2.as_array_view(dim<3>(), dim<4>(), dim<5>());
auto av4 = av3.as_array_view(dim<4>(), dim<>(3), dim<5>()); auto av4 = av3.as_array_view(dim<4>(), dim<>(3), dim<5>());
@ -244,11 +245,11 @@ SUITE(array_view_tests)
auto av8 = av7.as_array_view<int>(); auto av8 = av7.as_array_view<int>();
CHECK(av8.size() == av6.size()); //CHECK(av8.size() == av6.size());
for (auto i = 0; i < av8.size(); i++) //for (auto i = 0; i < av8.size(); i++)
{ //{
CHECK(av8[i] == 1); // CHECK(av8[i] == 1);
} //}
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
struct Foo {char c[11];}; struct Foo {char c[11];};
@ -314,7 +315,7 @@ SUITE(array_view_tests)
// From non-cv-qualified source // From non-cv-qualified source
{ {
const array_view<int> src{ arr }; const array_view<int> src = arr;
strided_array_view<int, 1> sav{ src, {2, 1} }; strided_array_view<int, 1> sav{ src, {2, 1} };
CHECK(sav.bounds().index_bounds() == index<1>{ 2 }); CHECK(sav.bounds().index_bounds() == index<1>{ 2 });
@ -824,13 +825,13 @@ SUITE(array_view_tests)
array_view<int, dynamic_range> av(arr, 8); array_view<int, dynamic_range> av(arr, 8);
size_t a[1] = { 0 }; ptrdiff_t a[1] = { 0 };
index<1> i = a; index<1> i = a;
CHECK(av[i] == 4); CHECK(av[i] == 4);
auto av2 = av.as_array_view(dim<4>(), dim<>(2)); auto av2 = av.as_array_view(dim<4>(), dim<>(2));
size_t a2[2] = { 0, 1 }; ptrdiff_t a2[2] = { 0, 1 };
index<2> i2 = a2; index<2> i2 = a2;
CHECK(av2[i2] == 0); CHECK(av2[i2] == 0);
@ -841,8 +842,8 @@ SUITE(array_view_tests)
TEST(index_operations) TEST(index_operations)
{ {
size_t a[3] = { 0, 1, 2 }; ptrdiff_t a[3] = { 0, 1, 2 };
size_t b[3] = { 3, 4, 5 }; ptrdiff_t b[3] = { 3, 4, 5 };
index<3> i = a; index<3> i = a;
index<3> j = b; index<3> j = b;
@ -903,12 +904,12 @@ SUITE(array_view_tests)
auto section = av.section({ 0,1 }, { length,1 }); auto section = av.section({ 0,1 }, { length,1 });
CHECK(section.size() == length); CHECK(section.size() == length);
for (unsigned int i = 0; i < section.size(); ++i) for (auto i = 0; i < section.size(); ++i)
{ {
CHECK(section[i][0] == av[i][1]); CHECK(section[i][0] == av[i][1]);
} }
for (unsigned int i = 0; i < section.size(); ++i) for (auto i = 0; i < section.size(); ++i)
{ {
auto idx = index<2>{ i,0 }; // avoid braces inside the CHECK macro auto idx = index<2>{ i,0 }; // avoid braces inside the CHECK macro
CHECK(section[idx] == av[i][1]); CHECK(section[idx] == av[i][1]);
@ -916,16 +917,16 @@ SUITE(array_view_tests)
CHECK(section.bounds().index_bounds()[0] == length); CHECK(section.bounds().index_bounds()[0] == length);
CHECK(section.bounds().index_bounds()[1] == 1); CHECK(section.bounds().index_bounds()[1] == 1);
for (unsigned int i = 0; i < section.bounds().index_bounds()[0]; ++i) for (auto i = 0; i < section.bounds().index_bounds()[0]; ++i)
{ {
for (unsigned int j = 0; j < section.bounds().index_bounds()[1]; ++j) for (auto j = 0; j < section.bounds().index_bounds()[1]; ++j)
{ {
auto idx = index<2>{ i,j }; // avoid braces inside the CHECK macro auto idx = index<2>{ i,j }; // avoid braces inside the CHECK macro
CHECK(section[idx] == av[i][1]); CHECK(section[idx] == av[i][1]);
} }
} }
size_t idx = 0; ptrdiff_t idx = 0;
for (auto num : section) for (auto num : section)
{ {
CHECK(num == av[idx][1]); CHECK(num == av[idx][1]);
@ -961,11 +962,11 @@ SUITE(array_view_tests)
TEST(dynamic_array_view_section_iteration) TEST(dynamic_array_view_section_iteration)
{ {
unsigned int height = 4, width = 2; auto height = 4, width = 2;
unsigned int size = height * width; auto size = height * width;
auto arr = new int[size]; auto arr = new int[size];
for (int unsigned i = 0; i < size; ++i) for (auto i = 0; i < size; ++i)
{ {
arr[i] = i; arr[i] = i;
} }
@ -1005,7 +1006,7 @@ SUITE(array_view_tests)
CHECK(strided.size() == length); CHECK(strided.size() == length);
CHECK(strided.bounds().index_bounds()[0] == length); CHECK(strided.bounds().index_bounds()[0] == length);
for (unsigned int i = 0; i < strided.size(); ++i) for (auto i = 0; i < strided.size(); ++i)
{ {
CHECK(strided[i] == av[2 * i + 1]); CHECK(strided[i] == av[2 * i + 1]);
} }
@ -1055,20 +1056,20 @@ SUITE(array_view_tests)
int expected[6] = { 2,3,10,11,18,19 }; int expected[6] = { 2,3,10,11,18,19 };
auto section = av.section({ 0,1,0 }, { 3,1,2 }); auto section = av.section({ 0,1,0 }, { 3,1,2 });
for (unsigned int i = 0; i < section.extent<0>(); ++i) for (auto i = 0; i < section.extent<0>(); ++i)
{ {
for (unsigned int j = 0; j < section.extent<1>(); ++j) for (auto j = 0; j < section.extent<1>(); ++j)
for (unsigned int k = 0; k < section.extent<2>(); ++k) for (auto k = 0; k < section.extent<2>(); ++k)
{ {
auto idx = index<3>{ i,j,k }; // avoid braces in the CHECK macro auto idx = index<3>{ i,j,k }; // avoid braces in the CHECK macro
CHECK(section[idx] == expected[2 * i + 2 * j + k]); CHECK(section[idx] == expected[2 * i + 2 * j + k]);
} }
} }
for (unsigned int i = 0; i < section.extent<0>(); ++i) for (auto i = 0; i < section.extent<0>(); ++i)
{ {
for (unsigned int j = 0; j < section.extent<1>(); ++j) for (auto j = 0; j < section.extent<1>(); ++j)
for (unsigned int k = 0; k < section.extent<2>(); ++k) for (auto k = 0; k < section.extent<2>(); ++k)
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]); CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
} }
@ -1083,10 +1084,10 @@ SUITE(array_view_tests)
TEST(strided_array_view_section_iteration_3d) TEST(strided_array_view_section_iteration_3d)
{ {
int arr[3][4][2]; int arr[3][4][2];
for (int i = 0; i < 3; ++i) for (auto i = 0; i < 3; ++i)
{ {
for (int j = 0; j < 4; ++j) for (auto j = 0; j < 4; ++j)
for (unsigned int k = 0; k < 2; ++k) for (auto k = 0; k < 2; ++k)
arr[i][j][k] = 8 * i + 2 * j + k; arr[i][j][k] = 8 * i + 2 * j + k;
} }
@ -1098,11 +1099,11 @@ SUITE(array_view_tests)
TEST(dynamic_strided_array_view_section_iteration_3d) TEST(dynamic_strided_array_view_section_iteration_3d)
{ {
unsigned int height = 12, width = 2; auto height = 12, width = 2;
unsigned int size = height * width; auto size = height * width;
auto arr = new int[size]; auto arr = new int[size];
for (int unsigned i = 0; i < size; ++i) for (auto i = 0; i < size; ++i)
{ {
arr[i] = i; arr[i] = i;
} }
@ -1137,7 +1138,7 @@ SUITE(array_view_tests)
X arr[4] = { { 0,1,2 },{ 3,4,5 },{ 6,7,8 },{ 9,10,11 } }; X arr[4] = { { 0,1,2 },{ 3,4,5 },{ 6,7,8 },{ 9,10,11 } };
auto s = sizeof(int) / sizeof(byte); int s = sizeof(int) / sizeof(byte);
auto d2 = 3 * s; auto d2 = 3 * s;
auto d1 = sizeof(int) * 12 / d2; auto d1 = sizeof(int) * 12 / d2;
@ -1179,152 +1180,6 @@ SUITE(array_view_tests)
} }
template<size_t Rank, typename T1, typename T2>
index<Rank, T2> Convert(index<Rank, T1> index)
{
return{ index };
}
TEST(DomainConverters)
{
// to smaller
{
index<2, int> int_index{ 0,1 };
index<2, short> short_index{ int_index };
CHECK(short_index[0] == 0);
CHECK(short_index[1] == 1);
}
// to smaller (failure)
{
index<2, int> big_int_index{ std::numeric_limits<int>::max(), 1 };
CHECK_THROW((Convert<2,int, short int>(big_int_index)), fail_fast);
}
// to same, sign mismatch
{
index<2, int> int_index{ 0,1 };
index<2, unsigned int> uint_index{ int_index };
CHECK(uint_index[0] == 0);
CHECK(uint_index[1] == 1);
}
// to same, sign mismatch, reversed
{
index<2, unsigned int> uint_index{ 0,1 };
index<2, int> int_index{ uint_index };
CHECK(int_index[0] == 0);
CHECK(int_index[1] == 1);
}
// to smaller, sign mismatch
{
index<2, int> int_index{ 0,1 };
index<2, unsigned short> ushort_index{ int_index };
CHECK(ushort_index[0] == 0);
CHECK(ushort_index[1] == 1);
}
// to bigger
{
index<2, int> int_index{ 0,1 };
index<2, long long> longlong_index{ int_index };
CHECK(longlong_index[0] == 0);
CHECK(longlong_index[1] == 1);
}
// to bigger with max index
{
index<2, int> big_int_index{ std::numeric_limits<int>::max(), 1 };
index<2, long long> longlong_index{ big_int_index };
CHECK(longlong_index[0] == std::numeric_limits<int>::max());
CHECK(longlong_index[1] == 1);
}
// to bigger, sign mismatch
{
index<2, int> int_index{ 0,1 };
index<2, unsigned long long> ulonglong_index{ int_index };
CHECK(ulonglong_index[0] == 0);
CHECK(ulonglong_index[1] == 1);
}
}
TEST(DomainConvertersRank1)
{
// to smaller
{
index<1, int> int_index{ 0 };
index<1, short> short_index{ int_index };
CHECK(short_index[0] == 0);
}
// to smaller (failure)
{
index<1, int> big_int_index{ std::numeric_limits<int>::max() };
CHECK_THROW((Convert<1, int, short int>(big_int_index)), fail_fast);
}
// to same, sign mismatch
{
index<1, int> int_index{ 0 };
index<1, unsigned int> uint_index{ int_index };
CHECK(uint_index[0] == 0);
}
// to same, sign mismatch, reversed
{
index<1, unsigned int> uint_index{ 0 };
index<1, int> int_index{ uint_index };
CHECK(int_index[0] == 0);
}
// to smaller, sign mismatch
{
index<1, int> int_index{ 0 };
index<1, unsigned short> ushort_index{ int_index };
CHECK(ushort_index[0] == 0);
}
// to bigger
{
index<1, int> int_index{ 0 };
index<1, long long> longlong_index{ int_index };
CHECK(longlong_index[0] == 0);
}
// to bigger with max index
{
index<1, int> big_int_index{ std::numeric_limits<int>::max() };
index<1, long long> longlong_index{ big_int_index };
CHECK(longlong_index[0] == std::numeric_limits<int>::max());
}
// to bigger, sign mismatch
{
index<1, int> int_index{ 0 };
index<1, unsigned long long> ulonglong_index{ int_index };
CHECK(ulonglong_index[0] == 0);
}
}
TEST(constructors) TEST(constructors)
{ {
array_view<int, dynamic_range> av(nullptr); array_view<int, dynamic_range> av(nullptr);
@ -1370,7 +1225,7 @@ SUITE(array_view_tests)
int arr[] = {3, 4, 5}; int arr[] = {3, 4, 5};
av1 = arr; av1 = arr;
array_view<array_view_options<const int, unsigned char>, dynamic_range> av2; array_view<const int, dynamic_range> av2;
av2 = av1; av2 = av1;
} }
@ -1380,21 +1235,21 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.first<2>().bounds() == static_bounds<size_t, 2>())); CHECK((av.first<2>().bounds() == static_bounds<2>()));
CHECK(av.first<2>().length() == 2); CHECK(av.first<2>().length() == 2);
CHECK(av.first(2).length() == 2); CHECK(av.first(2).length() == 2);
} }
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.first<0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.first<0>().bounds() == static_bounds<0>()));
CHECK(av.first<0>().length() == 0); CHECK(av.first<0>().length() == 0);
CHECK(av.first(0).length() == 0); CHECK(av.first(0).length() == 0);
} }
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.first<5>().bounds() == static_bounds<size_t, 5>())); CHECK((av.first<5>().bounds() == static_bounds<5>()));
CHECK(av.first<5>().length() == 5); CHECK(av.first<5>().length() == 5);
CHECK(av.first(5).length() == 5); CHECK(av.first(5).length() == 5);
} }
@ -1402,7 +1257,7 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
CHECK(av.first<6>().bounds() == static_bounds<size_t, 6>()); CHECK(av.first<6>().bounds() == static_bounds<6>());
CHECK(av.first<6>().length() == 6); CHECK(av.first<6>().length() == 6);
#endif #endif
CHECK_THROW(av.first(6).length(), fail_fast); CHECK_THROW(av.first(6).length(), fail_fast);
@ -1410,7 +1265,7 @@ SUITE(array_view_tests)
{ {
array_view<int, dynamic_range> av; array_view<int, dynamic_range> av;
CHECK((av.first<0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.first<0>().bounds() == static_bounds<0>()));
CHECK(av.first<0>().length() == 0); CHECK(av.first<0>().length() == 0);
CHECK(av.first(0).length() == 0); CHECK(av.first(0).length() == 0);
} }
@ -1422,21 +1277,21 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.last<2>().bounds() == static_bounds<size_t, 2>())); CHECK((av.last<2>().bounds() == static_bounds<2>()));
CHECK(av.last<2>().length() == 2); CHECK(av.last<2>().length() == 2);
CHECK(av.last(2).length() == 2); CHECK(av.last(2).length() == 2);
} }
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.last<0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.last<0>().bounds() == static_bounds<0>()));
CHECK(av.last<0>().length() == 0); CHECK(av.last<0>().length() == 0);
CHECK(av.last(0).length() == 0); CHECK(av.last(0).length() == 0);
} }
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.last<5>().bounds() == static_bounds<size_t, 5>())); CHECK((av.last<5>().bounds() == static_bounds<5>()));
CHECK(av.last<5>().length() == 5); CHECK(av.last<5>().length() == 5);
CHECK(av.last(5).length() == 5); CHECK(av.last(5).length() == 5);
} }
@ -1445,7 +1300,7 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
CHECK((av.last<6>().bounds() == static_bounds<size_t, 6>())); CHECK((av.last<6>().bounds() == static_bounds<6>()));
CHECK(av.last<6>().length() == 6); CHECK(av.last<6>().length() == 6);
#endif #endif
CHECK_THROW(av.last(6).length(), fail_fast); CHECK_THROW(av.last(6).length(), fail_fast);
@ -1453,7 +1308,7 @@ SUITE(array_view_tests)
{ {
array_view<int, dynamic_range> av; array_view<int, dynamic_range> av;
CHECK((av.last<0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.last<0>().bounds() == static_bounds<0>()));
CHECK(av.last<0>().length() == 0); CHECK(av.last<0>().length() == 0);
CHECK(av.last(0).length() == 0); CHECK(av.last(0).length() == 0);
} }
@ -1462,12 +1317,12 @@ SUITE(array_view_tests)
TEST(custmized_array_view_size) TEST(custmized_array_view_size)
{ {
double (*arr)[3][4] = new double[100][3][4]; double (*arr)[3][4] = new double[100][3][4];
array_view<array_view_options<double, char>, dynamic_range, 3, 4> av1(arr, (char)10); array_view<double, dynamic_range, 3, 4> av1(arr, 10);
struct EffectiveStructure struct EffectiveStructure
{ {
double* v1; double* v1;
char v2; ptrdiff_t v2;
}; };
CHECK(sizeof(av1) == sizeof(EffectiveStructure)); CHECK(sizeof(av1) == sizeof(EffectiveStructure));
@ -1483,7 +1338,7 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.sub<2,2>().bounds() == static_bounds<size_t, 2>())); CHECK((av.sub<2,2>().bounds() == static_bounds<2>()));
CHECK((av.sub<2,2>().length() == 2)); CHECK((av.sub<2,2>().length() == 2));
CHECK(av.sub(2,2).length() == 2); CHECK(av.sub(2,2).length() == 2);
CHECK(av.sub(2,3).length() == 3); CHECK(av.sub(2,3).length() == 3);
@ -1492,14 +1347,14 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.sub<0,0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.sub<0,0>().bounds() == static_bounds<0>()));
CHECK((av.sub<0,0>().length() == 0)); CHECK((av.sub<0,0>().length() == 0));
CHECK(av.sub(0,0).length() == 0); CHECK(av.sub(0,0).length() == 0);
} }
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.sub<0,5>().bounds() == static_bounds<size_t, 5>())); CHECK((av.sub<0,5>().bounds() == static_bounds<5>()));
CHECK((av.sub<0,5>().length() == 5)); CHECK((av.sub<0,5>().length() == 5));
CHECK(av.sub(0,5).length() == 5); CHECK(av.sub(0,5).length() == 5);
CHECK_THROW(av.sub(0,6).length(), fail_fast); CHECK_THROW(av.sub(0,6).length(), fail_fast);
@ -1508,7 +1363,7 @@ SUITE(array_view_tests)
{ {
array_view<int, 5> av = arr; array_view<int, 5> av = arr;
CHECK((av.sub<5,0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.sub<5,0>().bounds() == static_bounds<0>()));
CHECK((av.sub<5, 0>().length() == 0)); CHECK((av.sub<5, 0>().length() == 0));
CHECK(av.sub(5,0).length() == 0); CHECK(av.sub(5,0).length() == 0);
CHECK_THROW(av.sub(6,0).length(), fail_fast); CHECK_THROW(av.sub(6,0).length(), fail_fast);
@ -1516,7 +1371,7 @@ SUITE(array_view_tests)
{ {
array_view<int, dynamic_range> av; array_view<int, dynamic_range> av;
CHECK((av.sub<0,0>().bounds() == static_bounds<size_t, 0>())); CHECK((av.sub<0,0>().bounds() == static_bounds<0>()));
CHECK((av.sub<0,0>().length() == 0)); CHECK((av.sub<0,0>().length() == 0));
CHECK(av.sub(0,0).length() == 0); CHECK(av.sub(0,0).length() == 0);
CHECK_THROW((av.sub<1,0>().length()), fail_fast); CHECK_THROW((av.sub<1,0>().length()), fail_fast);
@ -1564,7 +1419,7 @@ SUITE(array_view_tests)
void AssertContentsMatch(T a1, U a2) void AssertContentsMatch(T a1, U a2)
{ {
CHECK(a1.length() == a2.length()); CHECK(a1.length() == a2.length());
for (size_t i = 0; i < a1.length(); ++i) for (auto i = 0; i < a1.length(); ++i)
CHECK(a1[i] == a2[i]); CHECK(a1[i] == a2[i]);
} }
@ -1726,7 +1581,7 @@ SUITE(array_view_tests)
CHECK(av1 == av2); CHECK(av1 == av2);
array_view<array_view_options<int, char>, 20> av3 = av1.as_array_view(dim<>(20)); array_view<int, 20> av3 = av1.as_array_view(dim<>(20));
CHECK(av3 == av2 && av3 == av1); CHECK(av3 == av2 && av3 == av1);
} }