From a7fa92f61a96404f2dc5d099465d9e94c53edc47 Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Thu, 9 May 2024 14:05:42 +0200 Subject: [PATCH] Test broadcasting scalars. --- tests/unit/tests_high_five_base.cpp | 121 ++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/tests/unit/tests_high_five_base.cpp b/tests/unit/tests_high_five_base.cpp index 6ef2add53..f52c2dcb8 100644 --- a/tests/unit/tests_high_five_base.cpp +++ b/tests/unit/tests_high_five_base.cpp @@ -1427,6 +1427,66 @@ TEMPLATE_LIST_TEST_CASE("ReadWriteSzip", "[template]", dataset_test_types) { } } +template +void check_broadcast_scalar_memspace(File& file, + const std::string& name, + const std::vector& dims) { + auto datatype = create_datatype(); + auto obj = CreateTraits::create(file, name, DataSpace(dims), datatype); + + double expected = 3.0; + obj.write(expected); + + auto actual = obj.template read(); + CHECK(actual == expected); +} + +TEST_CASE("Broadcast scalar memspace, dset") { + File file("h5_broadcast_scalar_memspace_dset.h5", File::Truncate); + + SECTION("[1]") { + check_broadcast_scalar_memspace(file, "dset", {1}); + } + + SECTION("[1, 1, 1]") { + check_broadcast_scalar_memspace(file, "dset", {1, 1, 1}); + } +} + +TEST_CASE("Broadcast scalar memspace, attr") { + File file("h5_broadcast_scalar_memspace_attr.h5", File::Truncate); + + SECTION("[1]") { + check_broadcast_scalar_memspace(file, "attr", {1}); + } + + SECTION("[1, 1, 1]") { + check_broadcast_scalar_memspace(file, "attr", {1, 1, 1}); + } +} + +template +void check_broadcast_scalar_filespace(File& file, const std::string& name) { + auto datatype = create_datatype(); + auto obj = CreateTraits::create(file, name, DataSpace::Scalar(), datatype); + + auto value = std::vector{3.0}; + + REQUIRE_THROWS(obj.write(value)); + REQUIRE_THROWS(obj.template read>()); + REQUIRE_THROWS(obj.read(value)); +} + +TEST_CASE("Broadcast scalar filespace, dset") { + File file("h5_broadcast_scalar_filespace_dset.h5", File::Truncate); + check_broadcast_scalar_filespace(file, "dset"); +} + +TEST_CASE("Broadcast scalar filespace, attr") { + File file("h5_broadcast_scalar_filespace_attr.h5", File::Truncate); + check_broadcast_scalar_filespace(file, "attr"); +} + TEST_CASE("squeeze") { CHECK(detail::squeeze({}, {}) == std::vector{}); CHECK(detail::squeeze({3, 1, 1}, {}) == std::vector{3, 1, 1}); @@ -1439,9 +1499,7 @@ TEST_CASE("squeeze") { } template -void check_modify_mem_space(File& file) { - const std::string name = "dset"; - +void check_modify_memspace(File& file, const std::string& name) { auto expected_values = std::vector{1.0, 2.0, 3.0}; auto values = std::vector>{expected_values}; @@ -1465,14 +1523,63 @@ void check_modify_mem_space(File& file) { } } -TEST_CASE("Modify Mem Space, attr") { +TEST_CASE("Modify MemSpace, dset") { File file("h5_modify_memspace_dset.h5", File::Truncate); - check_modify_mem_space(file); + check_modify_memspace(file, "dset"); } -TEST_CASE("Modify Mem Space, dset") { +TEST_CASE("Modify MemSpace, attr") { File file("h5_modify_memspace_attr.h5", File::Truncate); - check_modify_mem_space(file); + check_modify_memspace(file, "attr"); +} + +template +void check_modify_scalar_filespace(File& file, const std::string& name) { + auto expected_value = 3.0; + + auto obj = CreateTraits::create(file, name, expected_value); + SECTION("reshape") { + auto actual_values = obj.reshapeMemSpace({1}).template read>(); + + REQUIRE(actual_values.size() == 1); + REQUIRE(actual_values[0] == expected_value); + } +} + +TEST_CASE("Modify Scalar FileSpace, dset") { + File file("h5_modify_scalar_filespace_dset.h5", File::Truncate); + check_modify_scalar_filespace(file, "dset"); +} + +TEST_CASE("Modify Scalar FileSpace, attr") { + File file("h5_modify_scalar_filespace_attr.h5", File::Truncate); + check_modify_scalar_filespace(file, "attr"); +} + +template +void check_modify_scalar_memspace(File& file, const std::string& name) { + auto expected_value = std::vector{3.0}; + + auto obj = CreateTraits::create(file, name, expected_value); + SECTION("squeeze") { + auto actual_value = obj.squeezeMemSpace({0}).template read(); + REQUIRE(actual_value == expected_value[0]); + } + + SECTION("reshape") { + auto actual_value = obj.reshapeMemSpace({}).template read(); + REQUIRE(actual_value == expected_value[0]); + } +} + +TEST_CASE("Modify Scalar MemSpace, dset") { + File file("h5_modify_scalar_memspace_dset.h5", File::Truncate); + check_modify_scalar_memspace(file, "dset"); +} + +TEST_CASE("Modify Scalar MemSpace, attr") { + File file("h5_modify_scalar_memspace_attr.h5", File::Truncate); + check_modify_scalar_memspace(file, "attr"); }