Skip to content

Commit

Permalink
Added tests for the new varr_t and vstr_t types
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAce committed Oct 2, 2022
1 parent e2e3f39 commit 748d2e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
44 changes: 20 additions & 24 deletions tests/test-readWrite.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@

#include <h5pp/h5pp.h>
#include <iostream>

/*! \brief Prints the content of a vector nicely */
template<typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
if(!v.empty()) {
out << "[ ";
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " "));
out << "]";
}
return out;
}

template<typename T>
struct has_scalar2 {
Expand Down Expand Up @@ -102,10 +90,18 @@ void test_h5pp(h5pp::File &file, const WriteType &writeData, std::string_view ds
for(int i = 0; i < writeData.dimension(0); i++)
for(int j = 0; j < writeData.dimension(1); j++)
for(int k = 0; k < writeData.dimension(2); k++) {
for(int l = 0; l < writeData.dimension(3); l++){
for(int l = 0; l < writeData.dimension(3); l++) {
auto w = writeData(i, j, k, l);
auto r = readData(i, j, k, l);
h5pp::print("[{} {} {} {}]: {} + i{} == {} + i{}", i, j, k, l, std::real(w), std::imag(w), std::real(r), std::imag(r));
h5pp::print("[{} {} {} {}]: {} + i{} == {} + i{}",
i,
j,
k,
l,
std::real(w),
std::imag(w),
std::real(r),
std::imag(r));
}

h5pp::print("\n");
Expand All @@ -125,12 +121,8 @@ void test_h5pp(h5pp::File &file, const WriteType &writeData, std::string_view ds
#endif
else {
if(writeData != readData) {
std::cerr << "Wrote: \n"
<< writeData << "\n"
<< "Read: \n"
<< readData << std::endl;
// if constexpr(h5pp::type::sfinae::is_streamable_v<WriteType> and h5pp::type::sfinae::is_streamable_v<ReadType>)

h5pp::print("Wrote: \n{}\n", writeData);
h5pp::print("Read: \n{}\n", readData);
throw std::runtime_error("Data mismatch: Write != Read");
}
}
Expand All @@ -149,7 +141,7 @@ void test_h5pp(h5pp::File &file, const WriteType *writeData, const DimsType &dim
for(size_t i = 0; i < size; i++) {
if(writeData[i] != readData[i]) {
for(size_t j = 0; j < size; j++) {
std::cerr << "Wrote [" << j << "]: " << writeData[j] << " | Read [" << j << "]: " << readData[j] << std::endl;
h5pp::print("Wrote [{}]: {} | Read [{}]: {}", j, writeData[j], j, readData[j]);
}
throw std::runtime_error("Data mismatch: Write != Read");
}
Expand All @@ -175,7 +167,7 @@ int main() {
std::string stringDummy = "Dummy string with spaces";
std::complex<float> cplxFloat(1, 1);
std::vector<double> vectorDouble = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 1.0};
1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 1.0};
std::vector<cplx> vectorComplex = {{-0.191154, 0.326211}, {0.964728, -0.712335}, {-0.0351791, -0.10264}, {0.177544, 0.99999}};
auto *cStyleDoubleArray = new double[10];
for(size_t i = 0; i < 10; i++) cStyleDoubleArray[i] = static_cast<double>(i);
Expand Down Expand Up @@ -205,6 +197,9 @@ int main() {
field3vector[i].z = 200.9 * d;
}

h5pp::varr_t<double> vlenDouble = {1.0, 2.0, 3.0, 4.0};
std::vector<h5pp::varr_t<double>> vectorVlenDouble = {{1.0}, {2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0, 10.0}};

#ifdef H5PP_USE_EIGEN3
Eigen::MatrixXd matrixDouble = Eigen::MatrixXd::Random(3, 2);
Eigen::Matrix<size_t, 3, 2, Eigen::RowMajor> matrixSizeTRowMajor = Eigen::Matrix<size_t, 3, 2, Eigen::RowMajor>::Random(3, 2);
Expand Down Expand Up @@ -235,11 +230,12 @@ int main() {
test_h5pp(file, field2vector, "field2vector");
test_h5pp(file, field3vector, "field3vector");

test_h5pp(file, vlenDouble, "vlenDouble");
test_h5pp(file, vectorVlenDouble, "vectorVlenDouble");

// Read data as std::vector<std::byte>
auto vectorReadBytes = file.readDataset<std::vector<std::byte>>("vectorDouble");



#ifdef H5PP_USE_EIGEN3
test_h5pp(file, matrixDouble, "matrixDouble");
test_h5pp(file, matrixSizeTRowMajor, "matrixSizeTRowMajor");
Expand Down
14 changes: 14 additions & 0 deletions tests/test-readWriteText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,19 @@ int main() {
if(stringView != stringViewRead)
throw std::runtime_error(h5pp::format("string view mismatch: [{}] != [{}]", stringView, stringViewRead));


// Test vstr_t for variable-length text
h5pp::vstr_t vlenString = "This is a variable-length string";
file.writeDataset(vlenString, "vlenString");
auto vlenStringRead = file.readDataset<h5pp::vstr_t>("vlenString");
if(vlenString != vlenStringRead)
throw std::runtime_error(h5pp::format("vlen string mismatch: [{}] != [{}]", vlenString, vlenStringRead));

std::vector<h5pp::vstr_t> vectorVlenString = {{"This is "},{"a variable-length string vector"}};
file.writeDataset(vectorVlenString, "vectorVlenString");
auto vectorVlenStringRead = file.readDataset<std::vector<h5pp::vstr_t>>("vectorVlenString");
if(vectorVlenString != vectorVlenStringRead)
throw std::runtime_error(h5pp::format("vector vlen string mismatch: [{}] != [{}]", vectorVlenString, vectorVlenStringRead));

return 0;
}
8 changes: 8 additions & 0 deletions tests/test-simpleWrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ int main() {
std::vector<std::complex<int>> vectorComplexInt(10, std::complex<int>(42, 7));
std::vector<std::complex<double>> vectorComplexDouble(10, {10.0, 5.0});

h5pp::varr_t<double> vlenDouble = std::vector<double>(10, 42.0);
std::vector<h5pp::varr_t<double>> vectorVlenDouble;
for(size_t i = 0; i < 10; i++) vectorVlenDouble.emplace_back(std::vector<double>(i, static_cast<double>(i)));

struct Field2 {
double x;
double y;
Expand Down Expand Up @@ -64,6 +68,10 @@ int main() {
file.writeDataset(vectorDouble.data(), "simpleWriteGroup/vectorDouble", vectorDouble.size());
file.writeDataset(vectorInt.data(), "simpleWriteGroup/vectorInt", vectorInt.size());

// Test variable-length data
file.writeDataset(vlenDouble, "simpleWriteGroup/vlenDouble");
file.writeDataset(vectorVlenDouble, "simpleWriteGroup/vectorVlenDouble");

#ifdef H5PP_USE_EIGEN3
Eigen::MatrixXi matrixInt(2, 2);
Eigen::MatrixXd matrixDouble(2, 2);
Expand Down

0 comments on commit 748d2e0

Please sign in to comment.