Skip to content

Commit

Permalink
Clean up the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoemi09 authored and Wentzell committed Apr 24, 2024
1 parent a9d43ec commit d3d104a
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 357 deletions.
4 changes: 2 additions & 2 deletions c++/h5/stl/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ namespace h5 {

// read non-complex data into std::array<std::complex>
if (!lt.has_complex_attribute) {
std::cerr << "WARNING: HDF5 type mismatch while reading into a std::array: " + get_name_of_h5_type(hdf5_type<T>()) + " =! "
+ get_name_of_h5_type(lt.ty) + "\n";
std::cerr << "WARNING: HDF5 type mismatch while reading into a std::array: std::complex<" + get_name_of_h5_type(hdf5_type<T>())
+ "> != " + get_name_of_h5_type(lt.ty) + "\n";
std::array<double, N> tmp{};
h5_read(g, name, tmp);
std::copy(begin(tmp), end(tmp), begin(a));
Expand Down
65 changes: 28 additions & 37 deletions test/c++/h5_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,59 @@
//
// Authors: Nils Wentzell

#include "./test_common.hpp"

#include <gtest/gtest.h>
#include <h5/h5.hpp>

#include <array>
#include <string>
#include <complex>
#include <string>

TEST(H5, Array) {

// write
TEST(H5, ArrayOfBasicTypes) {
// write/read an array of strings and doubles
auto arr_str = std::array<std::string, 2>{"a", "abc"};
auto arr_dbl = std::array<double, 2>{1.0, 2.0};

{
h5::file file{"test_arr.h5", 'w'};
h5::group grp{file};
h5_write(grp, "arr_str", arr_str);
h5_write(grp, "arr_dbl", arr_dbl);
}

// read
std::array<std::string, 2> arr_str_r;
std::array<double, 2> arr_dbl_r{};
h5::write(file, "arr_str", arr_str);
h5::write(file, "arr_dbl", arr_dbl);
}

{
h5::file file{"test_arr.h5", 'r'};
h5::group grp{file};
h5_read(grp, "arr_str", arr_str_r);
h5_read(grp, "arr_dbl", arr_dbl_r);
}

// compare
EXPECT_EQ(arr_str, arr_str_r);
EXPECT_EQ(arr_dbl, arr_dbl_r);
std::array<std::string, 2> arr_str_in;
std::array<double, 2> arr_dbl_in{};
h5::read(file, "arr_str", arr_str_in);
h5::read(file, "arr_dbl", arr_dbl_in);
EXPECT_EQ(arr_str, arr_str_in);
EXPECT_EQ(arr_dbl, arr_dbl_in);
}
}

TEST(H5, ArrayConvert) {

// write
// write an array of ints and doubles and read them as longs and complex doubles
using namespace std::complex_literals;
auto arr_int = std::array<int, 2>{1, 2};
auto arr_dbl = std::array<double, 2>{1.5, 2.5};

{
h5::file file{"test_arr_convert.h5", 'w'};
h5::group grp{file};
h5_write(grp, "arr_int", arr_int);
h5_write(grp, "arr_dbl", arr_dbl);
}

// read
std::array<long, 2> arr_long{};
std::array<dcomplex, 2> arr_cplx;
h5::write(file, "arr_int", arr_int);
h5::write(file, "arr_dbl", arr_dbl);
}

{
h5::file file{"test_arr_convert.h5", 'r'};
h5::group grp{file};
h5_read(grp, "arr_int", arr_long); // Issues Warning
h5_read(grp, "arr_dbl", arr_cplx); // Issues Warning
}

// compare
EXPECT_EQ((std::array{1l, 2l}), arr_long);
using namespace std::literals;
EXPECT_EQ((std::array{1.5 + 0i, 2.5 + 0i}), arr_cplx);
std::array<long, 2> arr_long{};
std::array<std::complex<double>, 2> arr_cplx;
h5::read(file, "arr_int", arr_long); // issues a warning
h5::read(file, "arr_dbl", arr_cplx); // issues a warning

EXPECT_EQ((std::array{1l, 2l}), arr_long);
EXPECT_EQ((std::array{1.5 + 0i, 2.5 + 0i}), arr_cplx);
}
}
101 changes: 101 additions & 0 deletions test/c++/h5_array_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2020-2022 Simons Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors: Thomas Hahn

#include <gtest/gtest.h>
#include <h5/h5.hpp>

#include <numeric>
#include <vector>

// not working and probably should not work
// TEST(H5, GetLtotAndStridesH5) {
// // test the get_L_tot_and_strides_h5 function for a 10x10 array with every 2nd element in the view
// h5::v_t arr_shape{10, 10};
// h5::v_t view_shape{5, 5};
// long view_size = 25;
// std::vector<long> np_strides{20, 2};
// h5::v_t h5_strides{2, 2};

// auto [L_tot, strides] = h5::array_interface::get_L_tot_and_strides_h5(np_strides.data(), 2, view_size);
// EXPECT_EQ(L_tot, arr_shape);
// EXPECT_EQ(strides, h5_strides);
// }

// not working, should definitely work
// TEST(H5, GetLtotAndStridesH5ContiguousView) {
// // test the get_L_tot_and_strides_h5 function for a 10x10 array with every element in the view
// h5::v_t arr_shape{10, 10};
// h5::v_t view_shape{10, 10};
// long view_size = 100;
// std::vector<long> np_strides{10, 1};
// h5::v_t h5_strides{1, 1};

// auto [L_tot, strides] = h5::array_interface::get_L_tot_and_strides_h5(np_strides.data(), 2, view_size);
// EXPECT_EQ(L_tot, arr_shape);
// EXPECT_EQ(strides, h5_strides);
// }

TEST(H5, ArrayInterface2DArray) {
// write a 5x5 array and read a 5x3 array (every other column)
std::vector<int> data(25, 0);
std::iota(data.begin(), data.end(), 0);

// create an h5_array_view of rank 2 with dimensions 5x5 of the original data
int rank = 2;
int rows_w = 5;
int cols_w = 5;
h5::array_interface::h5_array_view view(h5::hdf5_type<int>(), (void *)data.data(), rank, false);
view.slab.count[0] = rows_w;
view.slab.count[1] = cols_w;
view.L_tot[0] = rows_w;
view.L_tot[1] = cols_w;

// create file in read/write mode
h5::file file("2d_array.h5", 'w');

// write h5_array_view to file
h5::array_interface::write(file, "view", view, false);

// reserve memory for reading
std::vector<int> data_in(15, 0);

// create an h5_array_view or rank 2 with dimensions 5x3 of the read memory
int rows_in = 5;
int cols_in = 3;
h5::array_interface::h5_array_view view_in(h5::hdf5_type<int>(), (void *)data_in.data(), rank, false);
view_in.slab.count[0] = rows_in;
view_in.slab.count[1] = cols_in;
view_in.L_tot[0] = rows_in;
view_in.L_tot[1] = cols_in;

// create an hyperslab to select the data to be read from the file (every other column -> stride in second dimension is 2)
h5::array_interface::hyperslab slab_in(rank, false);
slab_in.count[0] = rows_in;
slab_in.count[1] = cols_in;
slab_in.stride[0] = 1;
slab_in.stride[1] = 2;

// get h5_lengths_type from the dataset in the file
auto lengths_type = h5::array_interface::get_h5_lengths_type(file, "view");

// read data from file
h5::array_interface::read(file, "view", view_in, lengths_type, slab_in);

// check results
for (int i = 0; i < rows_in; ++i) {
for (int j = 0; j < cols_in; ++j) { EXPECT_EQ(data_in[i * cols_in + j], data[i * cols_w + j * 2]); }
}
}
45 changes: 21 additions & 24 deletions test/c++/h5_complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,63 @@
//
// Authors: Nils Wentzell

#include "./test_common.hpp"

#include <gtest/gtest.h>
#include <h5/h5.hpp>

#include <array>
#include <complex>

// clang-format off
TEST(H5, ComplexBkwd){
TEST(H5, ComplexBackwardCompatibility) {
// write and read a complex number the old way
std::complex<double> z{1.0, 2.0};

{
h5::file file("complex_old.h5", 'w');
h5::group top(file);

auto g = top.create_group("cplx");

double r = 1.0, i = 2.0;
h5_write(g, "r", r);
h5_write(g, "i", i);
h5_write(g, "r", z.real());
h5_write(g, "i", z.imag());
}

{
h5::file file("complex_old.h5", 'r');

std::complex<double> c;
h5_read(file, "cplx", c);
std::complex<double> z_in;
h5_read(file, "cplx", z_in);

dcomplex exact = {1.0, 2.0};
EXPECT_EQ(c, exact);
EXPECT_EQ(z, z_in);
}
};

TEST(H5, ComplexCompound){

std::array<h5::dcplx_t, 4> arr = { h5::dcplx_t{0.0, 0.0}, h5::dcplx_t{0.0, 1.0}, h5::dcplx_t{1.0, 0.0}, h5::dcplx_t{1.0, 1.0} };
auto scal = h5::dcplx_t{2.0, 2.0};
TEST(H5, ComplexCompoundType) {
// write an array of h5::dxplx_t and read it into an array of std::complex<double>
std::array<h5::dcplx_t, 4> arr = {h5::dcplx_t{0.0, 0.0}, h5::dcplx_t{0.0, 1.0}, h5::dcplx_t{1.0, 0.0}, h5::dcplx_t{1.0, 1.0}};
h5::dcplx_t z{2.0, 2.0};

{
h5::file file("complex_compound.h5", 'w');

h5_write(file, "cplx_arr", arr);
h5_write(file, "cplx_scal", scal);
h5_write(file, "cplx_scal", z);
}

{
h5::file file("complex_compound.h5", 'r');

std::array<std::complex<double>, 4> arr_in;
std::complex<double> scal_in;
std::complex<double> z_in;

h5_read(file, "cplx_arr", arr_in);
h5_read(file, "cplx_scal", scal_in);
h5_read(file, "cplx_scal", z_in);

for(int i = 0; i < arr.size(); ++i){
for (int i = 0; i < arr.size(); ++i) {
EXPECT_EQ(arr_in[i].real(), arr[i].r);
EXPECT_EQ(arr_in[i].imag(), arr[i].i);
}

EXPECT_EQ(scal_in.real(), scal.r);
EXPECT_EQ(scal_in.imag(), scal.i);
EXPECT_EQ(z_in.real(), z.r);
EXPECT_EQ(z_in.imag(), z.i);
}

};

// clang-format on
70 changes: 70 additions & 0 deletions test/c++/h5_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2020 Simons Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors: Thomas Hahn

#include <gtest/gtest.h>
#include <h5/h5.hpp>

#include <filesystem>
#include <string>

TEST(H5, FileOperations) {
// test various ways to construct a file
std::string fname{"file.h5"};

// create file in write mode
{
h5::file file(fname, 'w');
EXPECT_TRUE(file.is_valid());
EXPECT_EQ(file.name(), fname);
}

// overwrite file in write mode
{
h5::file file(fname, 'w');
EXPECT_TRUE(file.is_valid());
}

// open file in read mode
{
h5::file file(fname, 'r');
EXPECT_TRUE(file.is_valid());
}

// open file in append mode
{
h5::file file(fname, 'a');
EXPECT_TRUE(file.is_valid());
}

// create file in append mode
{
EXPECT_TRUE(std::filesystem::remove(fname));
h5::file file(fname, 'a');
EXPECT_TRUE(file.is_valid());
EXPECT_EQ(file.name(), fname);
}

// create a new file with H5F_ACC_EXCL flag
{
EXPECT_TRUE(std::filesystem::remove(fname));
h5::file file(fname, 'e');
EXPECT_TRUE(file.is_valid());
EXPECT_EQ(file.name(), fname);
}

// try to create file with H5F_ACC_EXCL flag which already exists
{ EXPECT_THROW(h5::file file(fname, 'e'), std::runtime_error); }
};
Loading

0 comments on commit d3d104a

Please sign in to comment.