Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions include/xtensor/misc/xmanipulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,17 +1052,15 @@ namespace xt
{
auto cpy = empty_like(e);
const auto& shape = cpy.shape();
std::size_t saxis = static_cast<std::size_t>(axis);
if (axis < 0)
{
axis += std::ptrdiff_t(cpy.dimension());
}
const auto dim = cpy.dimension();

if (saxis >= cpy.dimension() || axis < 0)
if (axis < -static_cast<std::ptrdiff_t>(dim) || axis >= static_cast<std::ptrdiff_t>(dim))
{
XTENSOR_THROW(std::runtime_error, "axis is no within shape dimension.");
XTENSOR_THROW(std::runtime_error, "axis is not within shape dimension.");
}

std::size_t saxis = normalize_axis(dim, axis);

const auto axis_dim = static_cast<std::ptrdiff_t>(shape[saxis]);
while (shift < 0)
{
Expand Down
12 changes: 12 additions & 0 deletions test/test_xmanipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,18 @@ namespace xt

xarray<double> expected8 = {{{3, 1, 2}}, {{6, 4, 5}}, {{9, 7, 8}}};
ASSERT_EQ(expected8, xt::roll(e2, -2, /*axis*/ 2));

EXPECT_THROW(xt::roll(e2, 1, /*axis*/ 3), std::runtime_error);
EXPECT_THROW(xt::roll(e2, 1, /*axis*/ -4), std::runtime_error);

xarray<double> expected9 = {{{3, 1, 2}}, {{6, 4, 5}}, {{9, 7, 8}}};
ASSERT_EQ(expected9, xt::roll(e2, -2, /*axis*/ -1));

xarray<double> expected10 = {{{1, 2, 3}}, {{4, 5, 6}}, {{7, 8, 9}}};
ASSERT_EQ(expected10, xt::roll(e2, -2, /*axis*/ -2));

xarray<double> expected11 = {{{4, 5, 6}}, {{7, 8, 9}}, {{1, 2, 3}}};
ASSERT_EQ(expected11, xt::roll(e2, 2, /*axis*/ -3));
}

TEST(xmanipulation, repeat_all_elements_of_axis_0_of_int_array_2_times)
Expand Down