Skip to content

Commit

Permalink
Debug tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willGraham01 committed Jun 2, 2023
1 parent 5db9187 commit e12e1f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
18 changes: 15 additions & 3 deletions tdms/tests/unit/benchmark_scripts/create_hdf5_test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ def create_hdf5_test_file() -> None:
os.mkdir(os.path.dirname(FNAME_TO_CREATE))
file = h5py.File(FNAME_TO_CREATE, "w")

# Create a group under root
read_in_test = file.require_group("read_in_test")

# Create test data to read in
consecutive_numbers = np.arange(0, 12, dtype=float)

# Create a group under root
read_in_test = file.require_group("read_in_test")
# Populate group with test data
read_in_test.create_dataset(
"vector_int", data=consecutive_numbers, shape=(12,), dtype=int
Expand All @@ -33,6 +32,19 @@ def create_hdf5_test_file() -> None:
"tensor_double", data=consecutive_numbers, shape=(2, 3, 2), dtype=float
)

# Create & populate the group that mimics MATLAB empty arrays
# Deliberately include some data here to stress that emptiness is based off the presence of an attribute
flag_as_empty = file.require_group("flag_as_empty")
flag_as_empty.attrs["MATLAB_empty"] = np.array(1, dtype=np.uint8)
flag_as_empty.create_dataset(
"consecutive_numbers", data=consecutive_numbers[0:3], shape=(3,), dtype=float
)

# Create a top-level dataset that will be empty, but the absence of the MATLAB_empty attribute will not mark it as so
file.create_dataset(
"not_marked_empty", data=consecutive_numbers[0:0], shape=(0,), dtype=float
)

file.attrs["file_attribute"] = 1

file.close()
Expand Down
Binary file not shown.
14 changes: 12 additions & 2 deletions tdms/tests/unit/hdf5_io_tests/test_hdf5_base.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <catch2/catch_test_macros.hpp>
#include <spdlog/spdlog.h>

#include "hdf5_io/hdf5_reader.h"
#include "unit_test_utils.h"
Expand All @@ -8,7 +9,9 @@ using tdms_unit_test_data::struct_testdata;

// HDF5Base cannot be instantiated, so we use HDF5Reader just to avoid
// accidental writes.
TEST_CASE("HDF5Base: is_empty()") {
TEST_CASE("HDF5Base: has_MATLAB_empty()") {
spdlog::info("HDF5Base::has_MATLAB_empty()");

HDF5Reader matlab_file(struct_testdata);
HDF5Reader hdf5_file(hdf5_test_file);

Expand All @@ -24,7 +27,7 @@ TEST_CASE("HDF5Base: is_empty()") {
REQUIRE(!matlab_file.has_MATLAB_empty("nonempty_dataset"));
// Point to nonempty group (struct)
REQUIRE(!matlab_file.has_MATLAB_empty("example_struct"));
// Point to nonempty dataset not created by MATLAB (so the attribute should
// Point to nonempty group not created by MATLAB (so the attribute should
// not be flagged)
REQUIRE(!hdf5_file.has_MATLAB_empty("read_in_test"));
}
Expand All @@ -40,4 +43,11 @@ TEST_CASE("HDF5Base: is_empty()") {
REQUIRE_THROWS_AS(hdf5_file.has_MATLAB_empty("file_attribute"),
std::runtime_error);
}

SECTION("False positives") {
// Object has an attribute MATLAB_empty, but still contains data
REQUIRE(hdf5_file.has_MATLAB_empty("flag_as_empty"));
// Object is an empty array, but does not have the MATLAB_empty flag
REQUIRE(!hdf5_file.has_MATLAB_empty("not_marked_empty"));
}
}

0 comments on commit e12e1f0

Please sign in to comment.