Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/green/ndarray/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace green::ndarray {
template <typename shape_type>
explicit ndarray(T* data, const shape_type& shape) :
shape_(get_shape(shape)), strides_(strides_for_shape(shape)), size_(size_for_shape(shape)), offset_(0),
storage_(data, size_) {
storage_(data, sizeof(T) * size_) {
if (data) set_value(0.0);
}

Expand Down
16 changes: 16 additions & 0 deletions test/ndarray_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ TEST_CASE("NDArrayTest") {
std::equal(array.begin(), array.end(), array_ref2.begin(), [](double a, double b) { return std::abs(a - b) < 1e-12; }));
}

SECTION("Check storage size consistensy") {
std::vector<double> x(200);
// make shape consistent with the vector size
std::array<size_t, 3> shape{20, 5, 2};
// create ndarray referencing data in x
ndarray::ndarray<double, 3> array_ref(x.data(), shape);
// check that size assigned in storage is consistent with actual size of data
REQUIRE(array_ref.storage().data().size == x.size() * sizeof(decltype(x)::value_type));
// create a complex view of an array
auto carray_ref = array_ref.view<std::complex<double>>();
// check that new array points to the same memory region
REQUIRE(array_ref.storage().data().ptr == carray_ref.storage().data().ptr);
// check that new array storage size is still correct
REQUIRE(carray_ref.storage().data().size == x.size() * sizeof(decltype(x)::value_type));
}

SECTION("Slice") {
ndarray::ndarray<double, 5> array(1, 2, 3, 4, 5);
initialize_array(array);
Expand Down