Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix xaxis_*_iterator shape and strides types #2747

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions include/xtensor/xaxis_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace xt
using xexpression_type = std::decay_t<CT>;
using size_type = typename xexpression_type::size_type;
using difference_type = typename xexpression_type::difference_type;
using shape_type = typename xexpression_type::shape_type;
using shape_type = std::vector<typename xexpression_type::shape_type::value_type>;
using value_type = xstrided_view<CT, shape_type>;
using reference = std::remove_reference_t<apply_cv_t<CT, value_type>>;
using pointer = xtl::xclosure_pointer<std::remove_reference_t<apply_cv_t<CT, value_type>>>;
Expand Down Expand Up @@ -106,8 +106,8 @@ namespace xt
)
{
using xexpression_type = std::decay_t<CT>;
using shape_type = typename xexpression_type::shape_type;
using strides_type = typename xexpression_type::strides_type;
using shape_type = std::vector<typename xexpression_type::shape_type::value_type>;
using strides_type = std::vector<typename xexpression_type::strides_type::value_type>;

const auto& e_shape = e.shape();
shape_type shape(e_shape.size() - 1);
Expand Down
4 changes: 2 additions & 2 deletions include/xtensor/xaxis_slice_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace xt
using xexpression_type = std::decay_t<CT>;
using size_type = typename xexpression_type::size_type;
using difference_type = typename xexpression_type::difference_type;
using shape_type = typename xexpression_type::shape_type;
using strides_type = typename xexpression_type::strides_type;
using shape_type = std::array<typename xexpression_type::shape_type::value_type, 1>;
using strides_type = std::array<typename xexpression_type::strides_type::value_type, 1>;
using value_type = xstrided_view<CT, shape_type>;
using reference = std::remove_reference_t<apply_cv_t<CT, value_type>>;
using pointer = xtl::xclosure_pointer<std::remove_reference_t<apply_cv_t<CT, value_type>>>;
Expand Down
47 changes: 29 additions & 18 deletions test/test_xaxis_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,35 @@

#include "xtensor/xarray.hpp"
#include "xtensor/xaxis_iterator.hpp"
#include "xtensor/xfixed.hpp"
#include "xtensor/xtensor.hpp"

#include "test_common_macros.hpp"

#define ROW_TYPES \
xarray<int, layout_type::row_major>, xtensor<int, 3, layout_type::row_major>, \
xtensor_fixed<int, xt::xshape<2, 3, 4>, layout_type::row_major>
#define COL_TYPES \
xarray<int, layout_type::column_major>, xtensor<int, 3, layout_type::column_major>, \
xtensor_fixed<int, xt::xshape<2, 3, 4>, layout_type::column_major>
#define ALL_TYPES ROW_TYPES, COL_TYPES

namespace xt
{
using std::size_t;

xarray<int> get_test_array()
template <typename T = xarray<int>>
T get_test_array()
{
xarray<int> res = {
T res = {
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};
return res;
}

TEST(xaxis_iterator, begin)
TEST_CASE_TEMPLATE("xaxis_iterator.begin", T, ALL_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter_begin = axis_begin(a);
EXPECT_EQ(size_t(2), iter_begin->dimension());
EXPECT_EQ(a.shape()[1], iter_begin->shape()[0]);
Expand All @@ -37,9 +48,9 @@ namespace xt
EXPECT_EQ(a(0, 2, 3), (*iter_begin)(2, 3));
}

TEST(xaxis_iterator, increment)
TEST_CASE_TEMPLATE("xaxis_iterator.increment", T, ROW_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter = axis_begin(a);
++iter;

Expand All @@ -52,9 +63,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(2, 3));
}

TEST(xaxis_iterator, end)
TEST_CASE_TEMPLATE("xaxis_iterator.end", T, ALL_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter_begin = axis_begin(a, 1u);
auto iter_end = axis_end(a, 1u);
auto dist = std::distance(iter_begin, iter_end);
Expand All @@ -80,9 +91,9 @@ namespace xt
EXPECT_EQ(iter_begin_row, iter_end_row);
}

TEST(xaxis_iterator, nested)
TEST_CASE_TEMPLATE("xaxis_iterator.nested", T, ROW_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter = axis_begin(a);
++iter;
auto niter = axis_begin(*iter);
Expand All @@ -95,9 +106,9 @@ namespace xt
EXPECT_EQ(a(1, 1, 3), (*niter)(3));
}

TEST(xaxis_iterator, const_array)
TEST_CASE_TEMPLATE("xaxis_iterator.const_array", T, ROW_TYPES)
{
const xarray<int> a = get_test_array();
const T a = get_test_array<T>();
auto iter = axis_begin(a);
++iter;

Expand All @@ -110,9 +121,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(2, 3));
}

TEST(xaxis_iterator, axis_0)
TEST_CASE_TEMPLATE("xaxis_iterator.axis_0", T, ROW_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter = axis_begin(a, 0);

EXPECT_EQ(a(0, 0, 0), (*iter)(0, 0));
Expand Down Expand Up @@ -142,9 +153,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(2, 3));
}

TEST(xaxis_iterator, axis_1)
TEST_CASE_TEMPLATE("xaxis_iterator.axis_1", T, ROW_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter = axis_begin(a, 1u);

EXPECT_EQ(a(0, 0, 0), (*iter)(0, 0));
Expand Down Expand Up @@ -175,9 +186,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(1, 3));
}

TEST(xaxis_iterator, axis_2)
TEST_CASE_TEMPLATE("xaxis_iterator.axis_2", T, ROW_TYPES)
{
xarray<int> a = get_test_array();
T a = get_test_array<T>();
auto iter = axis_begin(a, 2u);

EXPECT_EQ(a(0, 0, 0), (*iter)(0, 0));
Expand Down
66 changes: 34 additions & 32 deletions test/test_xaxis_slice_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,60 @@

#include "xtensor/xarray.hpp"
#include "xtensor/xaxis_slice_iterator.hpp"
#include "xtensor/xfixed.hpp"
#include "xtensor/xtensor.hpp"

#include "test_common_macros.hpp"

#define ROW_TYPES \
xarray<int, layout_type::row_major>, xtensor<int, 3, layout_type::row_major>, \
xtensor_fixed<int, xt::xshape<2, 3, 4>, layout_type::row_major>
#define COL_TYPES \
xarray<int, layout_type::column_major>, xtensor<int, 3, layout_type::column_major>, \
xtensor_fixed<int, xt::xshape<2, 3, 4>, layout_type::column_major>
#define ALL_TYPES ROW_TYPES, COL_TYPES

namespace xt
{
using std::size_t;
constexpr auto _col = layout_type::column_major;

xarray<int> get_slice_test_array()
template <typename T = xarray<int>>
T get_slice_test_array()
{
xarray<int> res = {
T res = {
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};
return res;
}

TEST(xaxis_slice_iterator, begin)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.begin", T, ALL_TYPES)
{
xarray<int> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter_begin = axis_slice_begin(a, 0);
EXPECT_EQ(size_t(1), iter_begin->dimension());
EXPECT_EQ(a.shape()[0], iter_begin->shape()[0]);
EXPECT_EQ(a(0, 0, 0), (*iter_begin)(0));
EXPECT_EQ(a(1, 0, 0), (*iter_begin)(1));
}

TEST(xaxis_slice_iterator, end)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.end", T, ALL_TYPES)
{
xarray<int> a = get_slice_test_array();
xarray<int, layout_type::column_major> a_col = get_slice_test_array();
T a = get_slice_test_array<T>();

auto dist = std::distance(axis_slice_begin(a, 0), axis_slice_end(a, 0));
EXPECT_EQ(12, dist);

dist = std::distance(axis_slice_begin(a_col), axis_slice_end(a_col));
EXPECT_EQ(12, dist);

dist = std::distance(axis_slice_begin(a, 1), axis_slice_end(a, 1));
EXPECT_EQ(8, dist);

dist = std::distance(axis_slice_begin(a_col, 1), axis_slice_end(a_col, 1));
EXPECT_EQ(8, dist);

dist = std::distance(axis_slice_begin(a, 2), axis_slice_end(a, 2));
EXPECT_EQ(6, dist);

dist = std::distance(axis_slice_begin(a_col, 2), axis_slice_end(a_col, 2));
EXPECT_EQ(6, dist);
}

TEST(xaxis_slice_iterator, increment)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.increment", T, ROW_TYPES)
{
xarray<int, layout_type::row_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, 0);
++iter;

Expand All @@ -71,9 +73,9 @@ namespace xt
EXPECT_EQ(a(1, 0, 1), (*iter)(1));
}

TEST(xaxis_slice_iterator, const_array)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.const_array", T, ROW_TYPES)
{
const xarray<int, layout_type::row_major> a = get_slice_test_array();
const T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, 2);
++iter;

Expand All @@ -86,9 +88,9 @@ namespace xt
EXPECT_EQ(a(0, 1, 3), (*iter)(3));
}

TEST(xaxis_slice_iterator, axis_0)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_0", T, ROW_TYPES)
{
xarray<int, layout_type::row_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(0));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down Expand Up @@ -128,9 +130,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(1));
}

TEST(xaxis_slice_iterator, axis_0_col)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_0_col", T, COL_TYPES)
{
xarray<int, layout_type::column_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(0));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down Expand Up @@ -170,9 +172,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(1));
}

TEST(xaxis_slice_iterator, axis_1)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_1", T, ROW_TYPES)
{
xarray<int, layout_type::row_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(1));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down Expand Up @@ -208,9 +210,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(2));
}

TEST(xaxis_slice_iterator, axis_1_col)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_1_col", T, COL_TYPES)
{
xarray<int, layout_type::column_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(1));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down Expand Up @@ -246,9 +248,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(2));
}

TEST(xaxis_slice_iterator, axis_2)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_2", T, ROW_TYPES)
{
xarray<int, layout_type::row_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(2));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down Expand Up @@ -282,9 +284,9 @@ namespace xt
EXPECT_EQ(a(1, 2, 3), (*iter)(3));
}

TEST(xaxis_slice_iterator, axis_2_col)
TEST_CASE_TEMPLATE("xaxis_slice_iterator.axis_2_col", T, COL_TYPES)
{
xarray<int, layout_type::column_major> a = get_slice_test_array();
T a = get_slice_test_array<T>();
auto iter = axis_slice_begin(a, size_t(2));

EXPECT_EQ(a(0, 0, 0), (*iter)(0));
Expand Down