diff --git a/include/xtensor-python/pycontainer.hpp b/include/xtensor-python/pycontainer.hpp index 1617d03..d0a1a1e 100644 --- a/include/xtensor-python/pycontainer.hpp +++ b/include/xtensor-python/pycontainer.hpp @@ -473,7 +473,11 @@ namespace xt template inline bool pycontainer::is_contiguous() const noexcept { - if (PyArray_CHKFLAGS(python_array(), NPY_ARRAY_C_CONTIGUOUS)) + if (this->strides().size() == 0) + { + return true; + } + else if (PyArray_CHKFLAGS(python_array(), NPY_ARRAY_C_CONTIGUOUS)) { return 1 == this->strides().back(); } diff --git a/test/test_pyarray.cpp b/test/test_pyarray.cpp index 3f509e3..6406de6 100644 --- a/test/test_pyarray.cpp +++ b/test/test_pyarray.cpp @@ -38,12 +38,12 @@ namespace xt TEST(pyarray, initializer_constructor) { pyarray r - {{{ 0, 1, 2}, - { 3, 4, 5}, - { 6, 7, 8}}, - {{ 9, 10, 11}, - {12, 13, 14}, - {15, 16, 17}}}; + {{{ 0, 1, 2}, + { 3, 4, 5}, + { 6, 7, 8}}, + {{ 9, 10, 11}, + {12, 13, 14}, + {15, 16, 17}}}; EXPECT_EQ(r.layout(), xt::layout_type::row_major); EXPECT_EQ(r.dimension(), 3); @@ -51,12 +51,12 @@ namespace xt EXPECT_EQ(r.shape()[0], 2); pyarray c - {{{ 0, 1, 2}, - { 3, 4, 5}, - { 6, 7, 8}}, - {{ 9, 10, 11}, - {12, 13, 14}, - {15, 16, 17}}}; + {{{ 0, 1, 2}, + { 3, 4, 5}, + { 6, 7, 8}}, + {{ 9, 10, 11}, + {12, 13, 14}, + {15, 16, 17}}}; EXPECT_EQ(c.layout(), xt::layout_type::column_major); EXPECT_EQ(c.dimension(), 3); @@ -64,12 +64,12 @@ namespace xt EXPECT_EQ(c.shape()[0], 2); pyarray d - {{{ 0, 1, 2}, - { 3, 4, 5}, - { 6, 7, 8}}, - {{ 9, 10, 11}, - {12, 13, 14}, - {15, 16, 17}}}; + {{{ 0, 1, 2}, + { 3, 4, 5}, + { 6, 7, 8}}, + {{ 9, 10, 11}, + {12, 13, 14}, + {15, 16, 17}}}; EXPECT_EQ(d.layout(), xt::layout_type::row_major); EXPECT_EQ(d.dimension(), 3); @@ -77,6 +77,29 @@ namespace xt EXPECT_EQ(d.shape()[0], 2); } + TEST(pyarray, expression) + { + pyarray a = xt::empty({}); + + EXPECT_EQ(a.layout(), xt::layout_type::row_major); + EXPECT_EQ(a.dimension(), 0); + EXPECT_EQ(a.size(), 1); + + pyarray b = xt::empty({5}); + + EXPECT_EQ(b.layout(), xt::layout_type::row_major); + EXPECT_EQ(b.dimension(), 1); + EXPECT_EQ(b.size(), 5); + + pyarray c = xt::empty({5, 3}); + + EXPECT_EQ(c.layout(), xt::layout_type::row_major); + EXPECT_EQ(c.dimension(), 2); + EXPECT_EQ(c.size(), 15); + EXPECT_EQ(c.shape(0), 5); + EXPECT_EQ(c.shape(1), 3); + } + TEST(pyarray, shaped_constructor) { { @@ -86,7 +109,7 @@ namespace xt compare_shape(ra, rm); EXPECT_EQ(layout_type::row_major, ra.layout()); } - + { SCOPED_TRACE("column_major constructor"); column_major_result<> cm; @@ -150,7 +173,7 @@ namespace xt central_major_result<> res; int value = 2; pyarray a(res.m_shape, res.m_strides, value); - + { SCOPED_TRACE("copy constructor"); pyarray b(a); @@ -277,7 +300,7 @@ namespace xt EXPECT_EQ(2, a1(1)); EXPECT_EQ(4, a2(1, 1)); } - + TEST(pyarray, zerod) { pyarray a;