Commit a815b6d5 authored by Paal Kvamme's avatar Paal Kvamme
Browse files

Copying a region into a scalar buffer is not a good idea.

parent 9402da40
......@@ -810,6 +810,9 @@ DataBufferNd<T,NDim>::copySubset(const ndsize_t& srcorig, const self_type& src,
const ndsize_t& dstorig, self_type& dst,
const ndsize_t& cpyorig, const ndsize_t& cpysize)
{
if (dst.isScalar())
throw OpenZGY::Errors::ZgyInternalError("Attempted copy to scalar buffer.");
if (src.isScalar())
CopyScalar(NDim,
srcorig.data(), src._size.data(), src.scalarValue(),
......@@ -827,6 +830,9 @@ void
DataBufferNd<T,NDim>::copySubset(const ndsize_t& srcorig, const self_type& src,
const ndsize_t& dstorig, self_type& dst)
{
if (dst.isScalar())
throw OpenZGY::Errors::ZgyInternalError("Attempted copy to scalar buffer.");
if (src.isScalar())
CopyScalar(NDim,
srcorig.data(), src._size.data(), src.scalarValue(),
......
......@@ -24,6 +24,7 @@
using namespace OpenZGY;
using namespace InternalZGY;
using Test_Utils::must_throw;
template<typename T>
static void
......@@ -332,6 +333,22 @@ test_databuffer_copyfrom()
TEST_CHECK(countequal(3,target.data(),target.totalsize()) == 2*5*10-1);
}
static void
test_databuffer_copytoscalar()
{
const std::array<std::int64_t,3>zero{0, 0, 0};
const std::array<std::int64_t,3>srcsize{100, 90, 110};
const std::array<std::int64_t,3>dstsize{100, 90, 120};
DataBufferNd<float,3> source(srcsize);
DataBufferNd<float,3> target(42, dstsize);
source.fill(0);
source.data()[0] = 1;
source.data()[100*90*110 - 1] = 2;
must_throw("copy to scalar buffer", [&](){
target.copyFrom(&source, zero.data(), zero.data(), nullptr, nullptr);
});
}
template<typename T>
static void
test_databuffer_tostring()
......@@ -423,6 +440,7 @@ namespace {
Register()
{
register_test("databuffer.range", test_databuffer_range);
register_test("databuffer.copytoscalar", test_databuffer_copytoscalar);
#if 1
register_test("databuffer.all<f>", test_databuffer_all<float>);
register_test("databuffer.all<i>", test_databuffer_all<std::int32_t>);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment