Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicates of unifying test API. #990

Merged
merged 5 commits into from
May 7, 2024
Merged
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
163 changes: 54 additions & 109 deletions tests/unit/tests_high_five_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <highfive/highfive.hpp>
#include "tests_high_five.hpp"
#include "create_traits.hpp"

#ifdef HIGHFIVE_TEST_BOOST
#include <highfive/boost.hpp>
Expand Down Expand Up @@ -1717,28 +1718,6 @@ void check_empty_dimensions(const std::vector<size_t>& dims) {
check_empty_dimensions(input_data, dims);
}

struct ReadWriteAttribute {
template <class Container>
static void create(HighFive::File& file, const std::string& name, const Container& container) {
file.createAttribute(name, container);
}

static HighFive::Attribute get(HighFive::File& file, const std::string& name) {
return file.getAttribute(name);
}
};

struct ReadWriteDataSet {
template <class Container>
static void create(HighFive::File& file, const std::string& name, const Container& container) {
file.createDataSet(name, container);
}

static HighFive::DataSet get(HighFive::File& file, const std::string& name) {
return file.getDataSet(name);
}
};

template <class ReadWriteInterface, class CreateContainer>
void check_empty_read_write_cycle(const std::vector<size_t>& dims) {
using container_type = typename CreateContainer::container_type;
Expand Down Expand Up @@ -1781,12 +1760,12 @@ void check_empty_read_write_cycle(const std::vector<size_t>& dims) {

template <class CreateContainer>
void check_empty_dataset(const std::vector<size_t>& dims) {
check_empty_read_write_cycle<ReadWriteDataSet, CreateContainer>(dims);
check_empty_read_write_cycle<testing::DataSetCreateTraits, CreateContainer>(dims);
}

template <class CreateContainer>
void check_empty_attribute(const std::vector<size_t>& dims) {
check_empty_read_write_cycle<ReadWriteAttribute, CreateContainer>(dims);
check_empty_read_write_cycle<testing::AttributeCreateTraits, CreateContainer>(dims);
}

template <class CreateContainer>
Expand Down Expand Up @@ -2284,56 +2263,8 @@ TEST_CASE("DirectWriteBool") {
}


class ForwardToAttribute {
public:
ForwardToAttribute(const HighFive::File& file)
: _file(file) {}

template <class T>
HighFive::Attribute create(const std::string& name, const T& value) {
return _file.createAttribute(name, value);
}

HighFive::Attribute create(const std::string& name,
const HighFive::DataSpace filespace,
const HighFive::DataType& datatype) {
return _file.createAttribute(name, filespace, datatype);
}

HighFive::Attribute get(const std::string& name) {
return _file.getAttribute(name);
}

private:
HighFive::File _file;
};

class ForwardToDataSet {
public:
ForwardToDataSet(const HighFive::File& file)
: _file(file) {}

template <class T>
HighFive::DataSet create(const std::string& name, const T& value) {
return _file.createDataSet(name, value);
}

HighFive::DataSet create(const std::string& name,
const HighFive::DataSpace filespace,
const HighFive::DataType& datatype) {
return _file.createDataSet(name, filespace, datatype);
}

HighFive::DataSet get(const std::string& name) {
return _file.getDataSet(name);
}

private:
HighFive::File _file;
};

template <class Proxy>
void check_single_string(Proxy proxy, size_t string_length) {
template <class CreateTraits>
void check_single_string(File file, size_t string_length) {
auto value = std::string(string_length, 'o');
auto dataspace = DataSpace::From(value);

Expand All @@ -2348,42 +2279,49 @@ void check_single_string(Proxy proxy, size_t string_length) {
auto variable_length = VariableLengthStringType();

SECTION("automatic") {
proxy.create("auto", value);
REQUIRE(proxy.get("auto").template read<std::string>() == value);
auto obj = CreateTraits::create(file, "auto", value);
REQUIRE(obj.template read<std::string>() == value);
}

SECTION("fixed length") {
proxy.create("fixed", dataspace, fixed_length).write(value);
REQUIRE(proxy.get("fixed").template read<std::string>() == value);
auto obj = CreateTraits::create(file, "fixed", dataspace, fixed_length);
obj.write(value);
REQUIRE(obj.template read<std::string>() == value);
}

SECTION("overlength null-terminated") {
proxy.create("overlength_nullterm", dataspace, overlength_nullterm).write(value);
REQUIRE(proxy.get("overlength_nullterm").template read<std::string>() == value);
auto obj =
CreateTraits::create(file, "overlength_nullterm", dataspace, overlength_nullterm);
obj.write(value);
REQUIRE(obj.template read<std::string>() == value);
}

SECTION("overlength null-padded") {
proxy.create("overlength_nullpad", dataspace, overlength_nullpad).write(value);
auto obj = CreateTraits::create(file, "overlength_nullpad", dataspace, overlength_nullpad);
obj.write(value);
auto expected = std::string(n_chars_overlength, '\0');
expected.replace(0, value.size(), value.data());
REQUIRE(proxy.get("overlength_nullpad").template read<std::string>() == expected);
REQUIRE(obj.template read<std::string>() == expected);
}

SECTION("overlength space-padded") {
proxy.create("overlength_spacepad", dataspace, overlength_spacepad).write(value);
auto obj =
CreateTraits::create(file, "overlength_spacepad", dataspace, overlength_spacepad);
obj.write(value);
auto expected = std::string(n_chars_overlength, ' ');
expected.replace(0, value.size(), value.data());
REQUIRE(proxy.get("overlength_spacepad").template read<std::string>() == expected);
REQUIRE(obj.template read<std::string>() == expected);
}

SECTION("variable length") {
proxy.create("variable", dataspace, variable_length).write(value);
REQUIRE(proxy.get("variable").template read<std::string>() == value);
auto obj = CreateTraits::create(file, "variable", dataspace, variable_length);
obj.write(value);
REQUIRE(obj.template read<std::string>() == value);
}
}

template <class Proxy>
void check_multiple_string(Proxy proxy, size_t string_length) {
template <class CreateTraits>
void check_multiple_string(File file, size_t string_length) {
using value_t = std::vector<std::string>;
auto value = value_t{std::string(string_length, 'o'), std::string(string_length, 'x')};

Expand All @@ -2407,13 +2345,14 @@ void check_multiple_string(Proxy proxy, size_t string_length) {
};

SECTION("automatic") {
proxy.create("auto", value);
check(proxy.get("auto").template read<value_t>(), value);
auto obj = CreateTraits::create(file, "auto", value);
check(obj.template read<value_t>(), value);
}

SECTION("variable length") {
proxy.create("variable", dataspace, variable_length).write(value);
check(proxy.get("variable").template read<value_t>(), value);
auto obj = CreateTraits::create(file, "variable", dataspace, variable_length);
obj.write(value);
check(obj.template read<value_t>(), value);
}

auto make_padded_reference = [&](char pad, size_t n) {
Expand All @@ -2428,22 +2367,25 @@ void check_multiple_string(Proxy proxy, size_t string_length) {
auto check_fixed_length = [&](const std::string& label, size_t length) {
SECTION(label + " null-terminated") {
auto datatype = FixedLengthStringType(length + 1, StringPadding::NullTerminated);
proxy.create(label + "_nullterm", dataspace, datatype).write(value);
check(proxy.get(label + "_nullterm").template read<value_t>(), value);
auto obj = CreateTraits::create(file, label + "_nullterm", dataspace, datatype);
obj.write(value);
check(obj.template read<value_t>(), value);
}

SECTION(label + " null-padded") {
auto datatype = FixedLengthStringType(length, StringPadding::NullPadded);
proxy.create(label + "_nullpad", dataspace, datatype).write(value);
auto obj = CreateTraits::create(file, label + "_nullpad", dataspace, datatype);
obj.write(value);
auto expected = make_padded_reference('\0', length);
check(proxy.get(label + "_nullpad").template read<value_t>(), expected);
check(obj.template read<value_t>(), expected);
}

SECTION(label + " space-padded") {
auto datatype = FixedLengthStringType(length, StringPadding::SpacePadded);
proxy.create(label + "_spacepad", dataspace, datatype).write(value);
auto obj = CreateTraits::create(file, label + "_spacepad", dataspace, datatype);
obj.write(value);
auto expected = make_padded_reference(' ', length);
check(proxy.get(label + "_spacepad").template read<value_t>(), expected);
check(obj.template read<value_t>(), expected);
}
};

Expand All @@ -2453,58 +2395,61 @@ void check_multiple_string(Proxy proxy, size_t string_length) {

SECTION("underlength null-terminated") {
auto datatype = FixedLengthStringType(string_length, StringPadding::NullTerminated);
REQUIRE_THROWS(proxy.create("underlength_nullterm", dataspace, datatype).write(value));
auto obj = CreateTraits::create(file, "underlength_nullterm", dataspace, datatype);
REQUIRE_THROWS(obj.write(value));
}

SECTION("underlength nullpad") {
auto datatype = FixedLengthStringType(string_length - 1, StringPadding::NullPadded);
REQUIRE_THROWS(proxy.create("underlength_nullpad", dataspace, datatype).write(value));
auto obj = CreateTraits::create(file, "underlength_nullpad", dataspace, datatype);
REQUIRE_THROWS(obj.write(value));
}

SECTION("underlength spacepad") {
auto datatype = FixedLengthStringType(string_length - 1, StringPadding::NullTerminated);
REQUIRE_THROWS(proxy.create("underlength_spacepad", dataspace, datatype).write(value));
auto obj = CreateTraits::create(file, "underlength_spacepad", dataspace, datatype);
REQUIRE_THROWS(obj.write(value));
}
}

TEST_CASE("HighFiveSTDString (dataset, single, short)") {
File file("std_string_dataset_single_short.h5", File::Truncate);
check_single_string(ForwardToDataSet(file), 3);
check_single_string<testing::DataSetCreateTraits>(file, 3);
}

TEST_CASE("HighFiveSTDString (attribute, single, short)") {
File file("std_string_attribute_single_short.h5", File::Truncate);
check_single_string(ForwardToAttribute(file), 3);
check_single_string<testing::AttributeCreateTraits>(file, 3);
}

TEST_CASE("HighFiveSTDString (dataset, single, long)") {
File file("std_string_dataset_single_long.h5", File::Truncate);
check_single_string(ForwardToDataSet(file), 256);
check_single_string<testing::DataSetCreateTraits>(file, 256);
}

TEST_CASE("HighFiveSTDString (attribute, single, long)") {
File file("std_string_attribute_single_long.h5", File::Truncate);
check_single_string(ForwardToAttribute(file), 256);
check_single_string<testing::AttributeCreateTraits>(file, 256);
}

TEST_CASE("HighFiveSTDString (dataset, multiple, short)") {
File file("std_string_dataset_multiple_short.h5", File::Truncate);
check_multiple_string(ForwardToDataSet(file), 3);
check_multiple_string<testing::DataSetCreateTraits>(file, 3);
}

TEST_CASE("HighFiveSTDString (attribute, multiple, short)") {
File file("std_string_attribute_multiple_short.h5", File::Truncate);
check_multiple_string(ForwardToAttribute(file), 3);
check_multiple_string<testing::AttributeCreateTraits>(file, 3);
}

TEST_CASE("HighFiveSTDString (dataset, multiple, long)") {
File file("std_string_dataset_multiple_long.h5", File::Truncate);
check_multiple_string(ForwardToDataSet(file), 256);
check_multiple_string<testing::DataSetCreateTraits>(file, 256);
}

TEST_CASE("HighFiveSTDString (attribute, multiple, long)") {
File file("std_string_attribute_multiple_long.h5", File::Truncate);
check_multiple_string(ForwardToAttribute(file), 256);
matz-e marked this conversation as resolved.
Show resolved Hide resolved
check_multiple_string<testing::AttributeCreateTraits>(file, 256);
}

TEST_CASE("HighFiveFixedString") {
Expand Down
Loading