Commit 1342834d authored by gbalduzz's avatar gbalduzz
Browse files

test vector of strings.

parent eef2eb33
Loading
Loading
Loading
Loading
+5 −11
Original line number Diff line number Diff line
@@ -82,7 +82,6 @@ bool HDF5Reader::execute(std::string name,

bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) {
  open_group(name);
  open_group("data");
  bool success = true;

  try {
@@ -91,18 +90,13 @@ bool HDF5Reader::execute(std::string name, std::vector<std::string>& value) {

    value.resize(size);

    for (size_t l = 0; l < value.size(); l++) {
      std::stringstream ss;
      ss << get_path() << "/" << l;

      H5::DataSet dataset = my_file->openDataSet(ss.str().c_str());

      value[l].resize(dataset.getInMemDataSize(), 'a');
    open_group("data");

      H5::DataSpace dataspace = dataset.getSpace();
    for (size_t l = 0; l < value.size(); l++) {
      open_group(std::to_string(l));
      execute(std::to_string(l), value[l]);

      H5Dread(dataset.getId(), HDF5_TYPE<char>::get(), dataspace.getId(), H5S_ALL, H5P_DEFAULT,
              &value[l][0]);
      close_group();
    }
  }
  catch (const H5::FileIException& err) {
+12 −50
Original line number Diff line number Diff line
@@ -47,9 +47,12 @@ void HDF5Writer::open_file(std::string file_name, bool overwrite) {
}

void HDF5Writer::close_file() {
  if (file_) {
    // file_->flush(H5F_SCOPE_LOCAL);
    file_->close();
    file_.release();
  }
}

void HDF5Writer::open_group(std::string name) {
  my_paths_.push_back(name);
@@ -80,68 +83,27 @@ std::string HDF5Writer::get_path() {
void HDF5Writer::execute(const std::string& name,
                         const std::string& value)  //, H5File& file, std::string path)
{
  if (value.size() > 0) {
    H5::H5File& file = (*file_);
    std::string path = get_path();

    hsize_t dims[1];

    H5::DataSet* dataset = NULL;
    H5::DataSpace* dataspace = NULL;

    {
      dims[0] = value.size();
      dataspace = new H5::DataSpace(1, dims);

      std::string full_name = path + "/" + name;
      dataset = new H5::DataSet(
          file.createDataSet(full_name.c_str(), HDF5_TYPE<char>::get_PredType(), *dataspace));
  std::string full_name = get_path() + '/' + name;

      H5Dwrite(dataset->getId(), HDF5_TYPE<char>::get(), dataspace->getId(), H5S_ALL, H5P_DEFAULT,
               &value[0]);
    }

    delete dataset;
    delete dataspace;
  }
  write(full_name, std::vector<hsize_t>{value.size()}, HDF5_TYPE<char>::get_PredType(), value.data());
}

void HDF5Writer::execute(const std::string& name,
                         const std::vector<std::string>& value)  //, H5File& file, std::string path)
{
  if (value.size() > 0) {
    H5::H5File& file = (*file_);

    open_group(name);

    execute("size", value.size());  //, file, new_path);
    execute("size", static_cast<int>(value.size()));

    open_group("data");

    hsize_t dims[1];

    H5::DataSet* dataset = NULL;
    H5::DataSpace* dataspace = NULL;

    for (size_t l = 0; l < value.size(); l++) {
      dims[0] = value[l].size();
      dataspace = new H5::DataSpace(1, dims);

      std::stringstream ss;
      ss << get_path() << "/" << l;

      dataset = new H5::DataSet(
          file.createDataSet(ss.str().c_str(), HDF5_TYPE<char>::get_PredType(), *dataspace));

      H5Dwrite(dataset->getId(), HDF5_TYPE<char>::get(), dataspace->getId(), H5S_ALL, H5P_DEFAULT,
               &(value[l][0]));
    for (int i = 0; i < value.size(); ++i) {
      open_group(std::to_string(i));
      execute(std::to_string(i), value[i]);
      close_group();
    }

    close_group();

    delete dataset;
    delete dataspace;

    close_group();
  }
}
+18 −0
Original line number Diff line number Diff line
@@ -95,6 +95,24 @@ TEST(HDF5ReaderWriterTest, VectorReadWrite) {
  reader.close_file();
}

TEST(HDF5ReaderWriterTest, VectorOfStringsReadWrite) {
  std::vector<std::string> s1{"foo", "bar", "baz"};

  // Create test file.
  dca::io::HDF5Writer writer;
  writer.open_file("test.hdf5");
  writer.execute("strings", s1);
  writer.close_file();

  // Read test file.
  dca::io::HDF5Reader reader;
  reader.open_file("test.hdf5");

  std::vector<std::string> s2;
  reader.execute("strings", s2);
  EXPECT_EQ(s1, s2);
}

template <typename Scalar>
class HDF5ReaderWriterTest : public ::testing::Test {};
using TestTypes = ::testing::Types<std::complex<double>, float>;