Skip to content

Commit

Permalink
tests for relay io hnd offset, string, size, tweaks to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed May 12, 2021
1 parent a6c9e99 commit 9d5fb23
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/libs/relay/conduit_relay_io_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@ void
HDF5Handle::write(const Node &node,
const Node &opts)
{
CONDUIT_UNUSED(opts);
// note: wrong mode errors are handled before dispatch to interface

// Options Push / Pop (only needed for write, since hdf5 only supports
Expand All @@ -723,7 +722,7 @@ HDF5Handle::write(const Node &node,
hdf5_set_options(options()["hdf5"]);
}

hdf5_write(node,m_h5_id);
hdf5_write(node,m_h5_id, opts);

if(!prev_options.dtype().is_empty())
{
Expand Down
46 changes: 37 additions & 9 deletions src/libs/relay/conduit_relay_io_hdf5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2507,11 +2507,17 @@ read_hdf5_dataset_into_conduit_node(hid_t hdf5_dset_id,

if (offset < 0)
{
CONDUIT_ERROR("Offset must be non-negative.");
CONDUIT_HDF5_ERROR(ref_path,
"Error reading HDF5 Dataset with options:"
<< opts.to_yaml() <<
"`offset` must be non-negative.");
}
else if (stride < 1)
{
CONDUIT_ERROR("Stride must be greater than zero.");
CONDUIT_HDF5_ERROR(ref_path,
"Error reading HDF5 Dataset with options:"
<< opts.to_yaml() <<
"`stride` must be greater than zero.");
}

// get the number of elements in the dataset given the offset and stride
Expand All @@ -2527,7 +2533,10 @@ read_hdf5_dataset_into_conduit_node(hid_t hdf5_dset_id,
nelems_to_read = opts["size"].to_value();
if (nelems_to_read < 1)
{
CONDUIT_ERROR("Size must be greater than zero.");
CONDUIT_HDF5_ERROR(ref_path,
"Error reading HDF5 Dataset with options:"
<< opts.to_yaml() <<
"`size` must be greater than zero.");
}
}

Expand Down Expand Up @@ -2614,7 +2623,11 @@ read_hdf5_dataset_into_conduit_node(hid_t hdf5_dset_id,
else if( dt.number_of_elements() < 0 )
{
CONDUIT_HDF5_ERROR(ref_path,
"Cannot read dataset with # of elements < 0");
"Error reading HDF5 Dataset with options:"
<< opts.to_yaml()
<< "Cannot read using offset (" << offset << ")"
<< " greater than the number of entries in"
<< " the HDF5 dataset (" << nelems << ")");
}
else if(dest.dtype().is_compact() &&
dest.dtype().compatible(dt) )
Expand Down Expand Up @@ -2651,11 +2664,26 @@ read_hdf5_dataset_into_conduit_node(hid_t hdf5_dset_id,
H5Dclose(dataspace);
}

CONDUIT_CHECK_HDF5_ERROR_WITH_FILE_AND_REF_PATH(h5_status,
hdf5_dset_id,
ref_path,
"Error reading HDF5 Dataset: "
<< hdf5_dset_id);
if(opts.dtype().is_empty())
{
CONDUIT_CHECK_HDF5_ERROR_WITH_FILE_AND_REF_PATH(h5_status,
hdf5_dset_id,
ref_path,
"Error reading HDF5 Dataset: "
<< hdf5_dset_id);
}
else
{
CONDUIT_CHECK_HDF5_ERROR_WITH_FILE_AND_REF_PATH(h5_status,
hdf5_dset_id,
ref_path,
"Error reading HDF5 Dataset: "
<< hdf5_dset_id
<< " with options: "
<< opts.to_yaml()
<< "HDF5 dataset size: "
<< nelems);
}

CONDUIT_CHECK_HDF5_ERROR_WITH_FILE_AND_REF_PATH(H5Tclose(h5_dtype_id),
hdf5_dset_id,
Expand Down
200 changes: 199 additions & 1 deletion src/tests/relay/t_relay_io_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ TEST(conduit_relay_io_handle, test_reuse_handle)
h.open("tout_conduit_relay_io_handle_reopen_1.conduit_bin");
h.write(n);
h.close();

h.open("tout_conduit_relay_io_handle_reopen_2.conduit_bin");
h.write(n);
h.close();
Expand Down Expand Up @@ -529,6 +529,204 @@ TEST(conduit_relay_io_handle, test_hdf5_trunc)
}


//-----------------------------------------------------------------------------
TEST(conduit_relay_io_handle, test_offset_and_stride)
{
Node n_about;
io::about(n_about);

// skip test if hdf5 isn't enabled
if(n_about["protocols/hdf5"].as_string() != "enabled")
return;

std::string tfile_out = "tout_hdf5_io_handle_with_offset.hdf5";
// remove files if they already exist
utils::remove_path_if_exists(tfile_out);

Node n, n_read, n_check, opts, info;
n["data"]= { 0,1,2,3,4,5,6,7,8,9};

io::IOHandle h;
h.open(tfile_out);
h.write(n);

h.read(n_read);
n_read.print();

n_check = n;
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));

// strided read
n_read.reset();
opts.reset();
opts["stride"] = 2;
h.read(n_read,opts);
n_read.print();

n_check.reset();
n_check["data"] = {0,2,4,6,8};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));

// offset write
n = {-1,-1,-1,-1,-1};
opts.reset();
opts["offset"] = 5;
h.write(n,"data",opts);

n_read.reset();
h.read(n_read);
n_read.print();

n_check.reset();
n_check["data"] = {0,1,2,3,4,-1,-1,-1,-1,-1};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// read the first part of the seq
opts.reset();
opts["size"] = 5;
n_read.reset();
h.read("data",n_read,opts);
n_read.print();

n_check.reset();
n_check = {0,1,2,3,4};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));

// read the second part of the seq (-1's)
opts.reset();
opts["offset"] = 5;
n_read.reset();
h.read("data",n_read,opts);
n_read.print();

n_check.reset();
n_check = {-1,-1,-1,-1,-1};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// strided write
n = {1,1,1,1,1};
opts.reset();
opts["stride"] = 2;
h.write(n,"data",opts);

// strided +offset write
n = {2,2,2,2,2};
opts.reset();
opts["offset"] = 1;
opts["stride"] = 2;
h.write(n,"data",opts);

n_read.reset();
h.read(n_read);
n_read.print();

n_check.reset();
n_check["data"] = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// read the 1's
opts.reset();
opts["offset"] = 0;
opts["stride"] = 2;
n_read.reset();
h.read("data",n_read,opts);
n_read.print();

n_check.reset();
n_check = {1, 1, 1, 1, 1};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// read the 2's
opts.reset();
opts["offset"] = 1;
opts["stride"] = 2;
n_read.reset();
h.read("data",n_read,opts);
n_read.print();


n_check.reset();
n_check = {2, 2, 2, 2, 2};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// read subset of the 2's
opts.reset();
opts["offset"] = 1;
opts["stride"] = 2;
opts["size"] = 2;
n_read.reset();
h.read("data",n_read,opts);
n_read.print();

n_check.reset();
n_check = {2, 2};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));

// huge stride, this will only read the first entry
n_read.reset();
opts.reset();
opts["stride"] = 1000;
h.read(n_read,opts);
n_read.print();

n_check.reset();
n_check["data"] = {1};
// expect no diff
EXPECT_FALSE(n_read.diff(n_check,info));


// now some error conditions:

// neg size
n_read.reset();
opts.reset();
opts["size"] = -100;
EXPECT_THROW(h.read(n_read,opts),conduit::Error);


// neg stride
n_read.reset();
opts.reset();
opts["stride"] = -1;
EXPECT_THROW(h.read(n_read,opts),conduit::Error);

// neg offset
n_read.reset();
opts.reset();
opts["offset"] = -1;
EXPECT_THROW(h.read(n_read,opts),conduit::Error);

// // huge size
n_read.reset();
opts.reset();
opts["size"] = 1000;
EXPECT_THROW(h.read(n_read,opts),conduit::Error);

// huge offset
n_read.reset();
opts.reset();
opts["offset"] = 1000;
EXPECT_THROW(h.read(n_read,opts),conduit::Error);

}




//-----------------------------------------------------------------------------
TEST(conduit_relay_io_handle, test_empty_path_as_root)
{
Expand Down

0 comments on commit 9d5fb23

Please sign in to comment.