From 6c921e148428160d89855668e6a8acf9bf20b718 Mon Sep 17 00:00:00 2001 From: Owen Green Date: Mon, 14 Feb 2022 14:50:09 +0000 Subject: [PATCH] `SliceIterator::end()` fix (#97) * fix problem with 0-size slices and end() sentinels * adds basic test for slice ietrator fix --- include/data/FluidTensor_Support.hpp | 9 ++++----- tests/data/TestFluidTensorSupport.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/data/FluidTensor_Support.hpp b/include/data/FluidTensor_Support.hpp index 331f5de1b..d611d6b81 100644 --- a/include/data/FluidTensor_Support.hpp +++ b/include/data/FluidTensor_Support.hpp @@ -166,11 +166,10 @@ struct SliceIterator mIndexes[0] = s.extents[0]; - // size = s.size; - if (!s.transposed) // Not transposed - endoffset = s.strides[0] * s.extents[0]; - else // transposed - endoffset = s.strides[N - 1] * s.extents[N - 1]; + if (!s.transposed) + endoffset = s.strides[0] * s.size ; + else + endoffset = s.strides[N - 1] * s.size; mPtr = end ? base + s.start + endoffset : base + s.start; } diff --git a/tests/data/TestFluidTensorSupport.cpp b/tests/data/TestFluidTensorSupport.cpp index 4059b8a78..e6ef3aae2 100644 --- a/tests/data/TestFluidTensorSupport.cpp +++ b/tests/data/TestFluidTensorSupport.cpp @@ -184,3 +184,29 @@ TEST_CASE("SliceIterator does strided iteration","[FluidTensorSupport]"){ REQUIRE(std::equal(colIterator,end,colData.begin())); } } + + + +TEST_CASE("SliceIterator end() does sensible things","[FluidTensorSupport]"){ + + std::array data{0,1,2,3,4,5,6,7,8,9}; + fluid::FluidTensorView view{data.data(),0,1,10}; + auto size = GENERATE(0,1,2,3,4,5,6,7,8,9); + + using Iterator = fluid::impl::SliceIterator; + FluidTensorSlice<1> s(size); + + auto colIterator = Iterator(s,data.data()); + auto end = Iterator(s,data.data(), true); + size_t distance=std::distance(colIterator,end); + + CHECK(size == distance); + + auto slice = view(Slice(0),Slice(0,size)); + distance=std::distance(slice.begin(),slice.end()); + CHECK(size == distance); + + + + +}