Skip to content

Commit ca4c2e6

Browse files
authored
Merge pull request #2361 from JohanMabille/resize
Added assertion in resize method
2 parents 3abb74c + 908229e commit ca4c2e6

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

include/xtensor/xcontainer.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,18 @@ namespace xt
898898
(void) size;
899899
XTENSOR_ASSERT_MSG(c.size() == size, "Trying to resize const data container with wrong size.");
900900
}
901+
902+
template <class S, class T>
903+
constexpr bool check_resize_dimension(const S&, const T&)
904+
{
905+
return true;
906+
}
907+
908+
template <class T, size_t N, class S>
909+
constexpr bool check_resize_dimension(const std::array<T, N>&, const S& s)
910+
{
911+
return N == s.size();
912+
}
901913
}
902914

903915
/**
@@ -911,6 +923,8 @@ namespace xt
911923
template <class S>
912924
inline void xstrided_container<D>::resize(S&& shape, bool force)
913925
{
926+
XTENSOR_ASSERT_MSG(detail::check_resize_dimension(m_shape, shape),
927+
"cannot change the number of dimensions of xtensor")
914928
std::size_t dim = shape.size();
915929
if (m_shape.size() != dim || !std::equal(std::begin(shape), std::end(shape), std::begin(m_shape)) || force)
916930
{
@@ -938,6 +952,8 @@ namespace xt
938952
template <class S>
939953
inline void xstrided_container<D>::resize(S&& shape, layout_type l)
940954
{
955+
XTENSOR_ASSERT_MSG(detail::check_resize_dimension(m_shape, shape),
956+
"cannot change the number of dimensions of xtensor")
941957
if (base_type::static_layout != layout_type::dynamic && l != base_type::static_layout)
942958
{
943959
XTENSOR_THROW(std::runtime_error, "Cannot change layout_type if template parameter not layout_type::dynamic.");
@@ -957,6 +973,8 @@ namespace xt
957973
template <class S>
958974
inline void xstrided_container<D>::resize(S&& shape, const strides_type& strides)
959975
{
976+
XTENSOR_ASSERT_MSG(detail::check_resize_dimension(m_shape, shape),
977+
"cannot change the number of dimensions of xtensor")
960978
if (base_type::static_layout != layout_type::dynamic)
961979
{
962980
XTENSOR_THROW(std::runtime_error,

include/xtensor/xexception.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace xt
208208
{
209209
XTENSOR_THROW(std::out_of_range,
210210
"Number of arguments (" + std::to_string(sizeof...(Args)) +
211-
") us greater than the number of dimensions (" +
211+
") is greater than the number of dimensions (" +
212212
std::to_string(shape.size()) + ")");
213213
}
214214
}

test/test_xarray.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,14 @@ namespace xt
147147
TEST(xarray, resize)
148148
{
149149
xarray_dynamic a;
150+
std::vector<size_t> shape = {2, 2};
151+
xarray_dynamic::strides_type strides = {2, 1};
150152
test_resize(a);
153+
#ifdef XTENSOR_ENABLE_ASSERT
154+
EXPECT_NO_THROW(a.resize(shape));
155+
EXPECT_NO_THROW(a.resize(shape, layout_type::row_major));
156+
EXPECT_NO_THROW(a.resize(shape, strides));
157+
#endif
151158
}
152159

153160
TEST(xarray, reshape)

test/test_xtensor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ namespace xt
171171
{
172172
xtensor_dynamic a;
173173
test_resize<xtensor_dynamic, storage_type>(a);
174+
175+
#ifdef XTENSOR_ENABLE_ASSERT
176+
xtensor_dynamic b;
177+
std::vector<size_t> s = { 2u, 2u };
178+
xtensor_dynamic::strides_type strides = {2u, 1u};
179+
EXPECT_THROW(b.resize(s), std::runtime_error);
180+
EXPECT_THROW(b.resize(s, layout_type::dynamic), std::runtime_error);
181+
EXPECT_THROW(b.resize(s, strides), std::runtime_error);
182+
#endif
174183
}
175184

176185
TEST(xtensor, reshape)

0 commit comments

Comments
 (0)