29 #define BOOST_TEST_MAIN HighFiveTest
31 #include <boost/mpl/list.hpp>
32 #include <boost/test/unit_test.hpp>
35 #include <boost/multi_array.hpp>
42 typedef boost::mpl::list<int,
unsigned int, long,
unsigned long,
unsigned char,
43 char, float, double,
long long,
unsigned long long,
46 typedef boost::mpl::list<int,
unsigned int, long,
unsigned long,
unsigned char,
47 char, float, double, std::string>
50 template <
typename T,
typename Func>
51 void generate2D(T* table,
size_t x,
size_t y, Func& func) {
52 for (
size_t i = 0; i < x; i++) {
53 for (
size_t j = 0; j < y; j++) {
59 template <
typename T,
typename Func>
60 void generate2D(std::vector<std::vector<T>>& vec,
size_t x,
size_t y,
63 for (
size_t i = 0; i < x; i++) {
65 for (
size_t j = 0; j < y; j++) {
96 if (++
_init >= (
'a' + 26))
110 std::string random_string;
111 std::mt19937_64 rgen;
113 std::uniform_int_distribution<int> int_dist(0, 1000);
114 const size_t size_string = int_dist(rgen);
116 random_string.resize(size_string);
117 std::generate(random_string.begin(), random_string.end(), gen);
118 return random_string;
123 const std::string
FILE_NAME(
"h5tutr_dset.h5");
132 std::vector<size_t> dims{4,6};
138 BOOST_CHECK_EQUAL(dataset_exist,
false);
149 BOOST_CHECK_EQUAL(dataset_exist,
true);
169 memset(buffer, 0,
sizeof(
char) * 1024);
170 setvbuf(stderr, buffer, _IOLBF, 1023);
174 File file(
"nonexistent", File::ReadOnly);
177 BOOST_CHECK_EQUAL(buffer[0],
'\0');
182 setvbuf(stderr,
NULL, _IONBF, 0);
186 const std::string
FILE_NAME(
"openmodes.h5");
232 const std::string
FILE_NAME(
"h5_group_test.h5");
234 const std::string CHUNKED_DATASET_NAME(
"chunked_dset");
235 const std::string CHUNKED_DATASET_SMALL_NAME(
"chunked_dset_small");
236 const std::string GROUP_NAME1(
"/group1");
237 const std::string GROUP_NAME2(
"group2");
238 const std::string GROUP_NESTED_NAME(
"group_nested");
245 file.createGroup(GROUP_NAME1);
247 file.createGroup(GROUP_NAME1 +
"/" + GROUP_NESTED_NAME);
249 Group g1 =
file.createGroup(GROUP_NAME2);
254 std::vector<size_t> dims{4, 6};
259 GROUP_NAME1 +
"/" + GROUP_NESTED_NAME +
"/" +
DATASET_NAME,
266 goodChunking.
add(
Chunking(std::vector<hsize_t>{2, 2}));
272 badChunking0.
add(
Chunking(std::vector<hsize_t>{10, 10}));
275 badChunking1.
add(
Chunking(std::vector<hsize_t>{1, 1, 1}));
278 BOOST_CHECK_THROW(
file.createDataSet(CHUNKED_DATASET_NAME, dataspace,
283 BOOST_CHECK_THROW(
file.createDataSet(CHUNKED_DATASET_NAME, dataspace,
289 DataSet dataset_chunked =
file.createDataSet<
float>(
290 CHUNKED_DATASET_NAME, dataspace, goodChunking, cacheConfig);
293 DataSet dataset_chunked_small =
file.createDataSet<
float>(
294 CHUNKED_DATASET_SMALL_NAME, dataspace, goodChunking);
296 dataset_chunked_small.resize({1, 1});
306 GROUP_NAME1 +
"/" + GROUP_NESTED_NAME +
"/" +
DATASET_NAME);
314 DataSet dataset_chunked =
file.getDataSet(CHUNKED_DATASET_NAME,
318 DataSet dataset_chunked_small =
319 file.getDataSet(CHUNKED_DATASET_SMALL_NAME);
326 const std::string
FILE_NAME(
"create_extensible_dataset_example.h5");
328 constexpr
double t1[3][1] = {{2.0}, {2.0}, {4.0}};
329 constexpr
double t2[1][3] = {{4.0, 8.0, 6.0}};
347 dataset.
select({0, 0}, {3, 1}).write(t1);
356 dataset.
select({3, 3}, {1, 3}).write(t2);
372 BOOST_CHECK_EQUAL(4, dims[0]);
373 BOOST_CHECK_EQUAL(6, dims[1]);
375 BOOST_CHECK_EQUAL(t1[0][0],
values[0][0]);
376 BOOST_CHECK_EQUAL(t1[1][0],
values[1][0]);
377 BOOST_CHECK_EQUAL(t1[2][0],
values[2][0]);
379 BOOST_CHECK_EQUAL(t2[0][0],
values[3][3]);
380 BOOST_CHECK_EQUAL(t2[0][1],
values[3][4]);
381 BOOST_CHECK_EQUAL(t2[0][2],
values[3][5]);
386 const std::string
FILE_NAME(
"h5_ref_count_test.h5");
388 const std::string GROUP_NAME1(
"/group1");
389 const std::string GROUP_NAME2(
"/group2");
394 std::unique_ptr<DataSet> d1_ptr;
395 std::unique_ptr<Group> g_ptr;
399 Group g1 =
file.createGroup(GROUP_NAME1);
402 g1 =
file.createGroup(GROUP_NAME2);
405 std::vector<size_t> dims = {10, 10};
412 double values[10][10] = {{0}};
417 d1_ptr.reset(
new DataSet(std::move(d1)));
420 g_ptr.reset(
new Group(g1));
424 DataSet d2(std::move(*d1_ptr));
428 memset(
values, 255, 10 * 10);
432 for (std::size_t i = 0; i < 10; ++i) {
433 for (std::size_t j = 0; j < 10; ++j) {
436 if (i == 5 && j == 0) {
437 BOOST_CHECK_EQUAL(v, 1);
439 BOOST_CHECK_EQUAL(v, 0);
453 const std::string
FILE_NAME(
"h5_list_test.h5");
454 const std::string GROUP_NAME_CORE(
"group_name");
455 const std::string GROUP_NESTED_NAME(
"/group_nested");
462 for (
int i = 0; i < 2; ++i) {
463 std::ostringstream ss;
464 ss <<
"/" << GROUP_NAME_CORE <<
"_" << i;
465 file.createGroup(ss.str());
468 size_t n_elem =
file.getNumberObjects();
469 BOOST_CHECK_EQUAL(2, n_elem);
471 std::vector<std::string> elems =
file.listObjectNames();
472 BOOST_CHECK_EQUAL(2, elems.size());
473 std::vector<std::string> reference_elems;
474 for (
int i = 0; i < 2; ++i) {
475 std::ostringstream ss;
476 ss << GROUP_NAME_CORE <<
"_" << i;
477 reference_elems.push_back(ss.str());
480 BOOST_CHECK_EQUAL_COLLECTIONS(elems.begin(), elems.end(),
481 reference_elems.begin(),
482 reference_elems.end());
486 file.createGroup(GROUP_NESTED_NAME);
487 Group g_nest =
file.getGroup(GROUP_NESTED_NAME);
489 for (
int i = 0; i < 50; ++i) {
490 std::ostringstream ss;
491 ss << GROUP_NAME_CORE <<
"_" << i;
496 BOOST_CHECK_EQUAL(50, n_elem);
499 BOOST_CHECK_EQUAL(50, elems.size());
500 std::vector<std::string> reference_elems;
502 for (
int i = 0; i < 50; ++i) {
503 std::ostringstream ss;
504 ss << GROUP_NAME_CORE <<
"_" << i;
505 reference_elems.push_back(ss.str());
509 std::sort(elems.begin(), elems.end());
510 std::sort(reference_elems.begin(), reference_elems.end());
512 BOOST_CHECK_EQUAL_COLLECTIONS(elems.begin(), elems.end(),
513 reference_elems.begin(),
514 reference_elems.end());
529 BOOST_CHECK(d_var == d_var_test);
530 BOOST_CHECK(d_var != size_var);
533 BOOST_CHECK(size_var_cpy == size_var);
536 BOOST_CHECK(int_var != uint_var);
542 const std::string
FILE_NAME(
"h5tutr_dset.h5");
549 std::vector<size_t> dims{10, 1};
567 const std::string
FILE_NAME(
"h5tutr_space.h5");
574 DataSpace dataspace{std::vector<size_t>{10, 1}};
583 BOOST_CHECK_NE(space.getId(), space2.
getId());
586 BOOST_CHECK_EQUAL(space.getDimensions().size(), 2);
587 BOOST_CHECK_EQUAL(space.getDimensions()[0], 10);
588 BOOST_CHECK_EQUAL(space.getDimensions()[1], 1);
603 std::vector<size_t> space3_ans{8, 9, 10};
605 BOOST_CHECK_EQUAL_COLLECTIONS(space3_res.begin(), space3_res.end(),
606 space3_ans.begin(), space3_ans.end());
609 DataSpace space2(std::vector<size_t>{3, 4});
612 std::vector<size_t> space2_ans{3, 4};
614 BOOST_CHECK_EQUAL_COLLECTIONS(space2_res.begin(), space2_res.end(),
615 space2_ans.begin(), space2_ans.end());
626 std::vector<size_t> space1_ans{7};
628 BOOST_CHECK_EQUAL_COLLECTIONS(space1_res.begin(), space1_res.end(),
629 space1_ans.begin(), space1_ans.end());
636 std::vector<size_t> space3_ans{8, 9, 10};
638 BOOST_CHECK_EQUAL_COLLECTIONS(space3_res.begin(), space3_res.end(),
639 space3_ans.begin(), space3_ans.end());
645 std::vector<size_t> space2_ans{3, 4};
647 BOOST_CHECK_EQUAL_COLLECTIONS(space2_res.begin(), space2_res.end(),
648 space2_ans.begin(), space2_ans.end());
654 std::vector<size_t> space2b_ans{3, 4};
656 BOOST_CHECK_EQUAL_COLLECTIONS(space2b_res.begin(), space2b_res.end(),
657 space2b_ans.begin(), space2b_ans.end());
663 auto first_res =
first.getDimensions();
664 std::vector<hsize_t> first_ans{1,2,3};
666 BOOST_CHECK_EQUAL_COLLECTIONS(first_res.begin(), first_res.end(),
667 first_ans.begin(), first_ans.end());
672 std::vector<hsize_t> second_ans{1,2,3};
674 BOOST_CHECK_EQUAL_COLLECTIONS(second_res.begin(), second_res.end(),
675 second_ans.begin(), second_ans.end());
680 std::vector<hsize_t> third_ans{1,2,3};
682 BOOST_CHECK_EQUAL_COLLECTIONS(third_res.begin(), third_res.end(),
683 third_ans.begin(), third_ans.end());
686 template <
typename T>
689 filename <<
"h5_rw_2d_array_" <<
typeid(T).name() <<
"_test.h5";
691 const size_t x_size = 100;
710 dataset.
write(array);
714 dataset.
read(result);
716 for (
size_t i = 0; i <
x_size; ++i) {
717 for (
size_t j = 0; j <
y_size; ++j) {
718 BOOST_CHECK_EQUAL(result[i][j], array[i][j]);
724 readWrite2DArrayTest<T>();
731 filename <<
"h5_rw_vec_shortcut_test.h5";
733 const size_t x_size = 800;
735 std::vector<int> vec;
737 for(
size_t i=0; i <
x_size; i++)
739 std::string at_contents(
"Contents of string");
741 std::vector<std::vector<int>> my_nested = {{1,2},{3,4}};
750 DataSet ds_int =
file.createDataSet(
"/TmpInt", my_int);
751 DataSet ds_nested =
file.createDataSet(
"/TmpNest", my_nested);
753 std::vector<int> result;
754 dataset.
read(result);
755 BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(),
756 result.begin(), result.end());
760 BOOST_CHECK_EQUAL(read_in, at_contents);
763 ds_int.
read(out_int);
764 BOOST_CHECK_EQUAL(my_int, out_int);
766 decltype(my_nested) out_nested;
767 ds_nested.
read(out_nested);
769 for (
size_t i = 0; i < 2; ++i) {
770 for (
size_t j = 0; j < 2; ++j) {
771 BOOST_CHECK_EQUAL(my_nested[i][j], out_nested[i][j]);
776 template <
typename T>
781 filename <<
"h5_rw_vec_" <<
typeid(T).name() <<
"_test.h5";
783 const size_t x_size = 800;
785 typename std::vector<T> vec;
788 std::generate(vec.begin(), vec.end(), generator);
798 typename std::vector<T> result;
800 dataset.
read(result);
802 BOOST_CHECK_EQUAL(vec.size(),
x_size);
803 BOOST_CHECK_EQUAL(result.size(),
x_size);
805 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(),
806 vec.begin(), vec.end());
809 readWriteVectorTest<T>();
812 template <
typename T>
817 filename <<
"h5_rw_arr_" <<
typeid(T).name() <<
"_test.h5";
819 const size_t x_size = 800;
821 typename std::array<T, x_size> vec;
823 std::generate(vec.begin(), vec.end(), generator);
833 typename std::array<T, x_size> result;
835 dataset.
read(result);
837 BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(),
838 vec.begin(), vec.end());
840 typename std::array<T, 1> tooSmall;
844 readWriteArrayTest<T>();
847 template <
typename T>
852 filename <<
"h5_rw_attribute_vec_" <<
typeid(T).name() <<
"_test.h5";
854 std::srand((
unsigned int)std::time(0));
857 typename std::vector<T> vec;
864 std::generate(vec.begin(), vec.end(), generator);
868 Group g =
file.createGroup(
"dummy_group");
872 BOOST_CHECK_EQUAL(n, 0);
875 BOOST_CHECK_EQUAL(all_attribute_names.size(), 0);
878 BOOST_CHECK_EQUAL(has_attribute,
false);
886 BOOST_CHECK_EQUAL(n2, 1);
889 BOOST_CHECK_EQUAL(has_attribute,
true);
892 BOOST_CHECK_EQUAL(all_attribute_names.size(), 1);
893 BOOST_CHECK_EQUAL(all_attribute_names[0], std::string(
"my_attribute"));
904 typename std::vector<T> result1, result2;
908 file.getGroup(
"dummy_group").getAttribute(
"my_attribute");
909 a1_read.
read(result1);
911 BOOST_CHECK_EQUAL(vec.size(),
x_size);
912 BOOST_CHECK_EQUAL(result1.size(),
x_size);
914 for (
size_t i = 0; i <
x_size; ++i)
915 BOOST_CHECK_EQUAL(result1[i], vec[i]);
917 Attribute a2_read =
file.getDataSet(
"/dummy_group/dummy_dataset")
918 .getAttribute(
"my_attribute_copy");
919 a2_read.
read(result2);
921 BOOST_CHECK_EQUAL(vec.size(),
x_size);
922 BOOST_CHECK_EQUAL(result2.size(),
x_size);
924 for (
size_t i = 0; i <
x_size; ++i)
925 BOOST_CHECK_EQUAL(result2[i], vec[i]);
930 readWriteAttributeVectorTest<T>();
933 template <
typename T>
938 filename <<
"h5_rw_vec_2d_" <<
typeid(T).name() <<
"_test.h5";
943 typename std::vector<std::vector<T>> vec;
958 typename std::vector<std::vector<T>> result;
960 dataset.
read(result);
962 BOOST_CHECK_EQUAL(vec.size(),
x_size);
963 BOOST_CHECK_EQUAL(result.size(),
x_size);
965 BOOST_CHECK_EQUAL(vec[0].size(),
y_size);
966 BOOST_CHECK_EQUAL(result[0].size(),
y_size);
968 for (
size_t i = 0; i <
x_size; ++i) {
969 for (
size_t j = 0; i <
y_size; ++i) {
970 BOOST_CHECK_EQUAL(result[i][j], vec[i][j]);
976 readWriteVector2DTest<T>();
980 std::string
filename =
"datasetOffset.h5";
981 std::string dsetname =
"dset";
986 DataSet ds =
file.createDataSet<
int>(dsetname, DataSpace::From(data));
994 template <
typename T>
995 void MultiArray3DTest() {
996 typedef typename boost::multi_array<T, 3> MultiArray;
999 filename <<
"h5_rw_multiarray_" <<
typeid(T).name() <<
"_test.h5";
1003 MultiArray array(boost::extents[
size_x][
size_y][size_z]);
1006 std::generate(array.data(), array.data() + array.num_elements(), generator);
1009 File file(
filename.str(), File::ReadWrite | File::Create | File::Truncate);
1014 dataset.
write(array);
1019 dataset.
read(result);
1021 for (
size_t i = 0; i <
size_x; ++i) {
1022 for (
size_t j = 0; j <
size_y; ++j) {
1023 for (
size_t k = 0; k < size_z; ++k) {
1024 BOOST_CHECK_EQUAL(array[i][j][k], result[i][j][k]);
1031 MultiArray3DTest<T>();
1034 template <
typename T>
1035 void ublas_matrix_Test() {
1036 typedef typename boost::numeric::ublas::matrix<T> Matrix;
1039 filename <<
"h5_rw_multiarray_" <<
typeid(T).name() <<
"_test.h5";
1047 for (std::size_t i = 0; i < mat.size1(); ++i) {
1048 for (std::size_t j = 0; j < mat.size2(); ++j) {
1049 mat(i, j) = generator();
1054 File file(
filename.str(), File::ReadWrite | File::Create | File::Truncate);
1063 dataset.
read(result);
1065 for (
size_t i = 0; i <
size_x; ++i) {
1066 for (
size_t j = 0; j <
size_y; ++j) {
1067 BOOST_CHECK_EQUAL(mat(i, j), result(i, j));
1073 ublas_matrix_Test<T>();
1078 template <
typename T>
1080 typedef typename std::vector<T> Vector;
1083 filename <<
"h5_rw_select_test_" <<
typeid(T).name() <<
"_test.h5";
1085 const size_t size_x = 10;
1086 const size_t offset_x = 2, count_x = 5;
1093 std::generate(
values.begin(),
values.end(), generator);
1096 File file(
filename.str(), File::ReadWrite | File::Create | File::Truncate);
1109 std::vector<size_t> offset{offset_x};
1110 std::vector<size_t> size{count_x};
1119 BOOST_CHECK_EQUAL(result.size(), 5);
1121 for (
size_t i = 0; i < count_x; ++i) {
1122 BOOST_CHECK_EQUAL(
values[i + offset_x], result[i]);
1130 std::vector<size_t> ids{1,3,4,7};
1139 BOOST_CHECK_EQUAL(result.size(), ids.size());
1141 for (
size_t i = 0; i < ids.size(); ++i) {
1142 const std::size_t
id = ids[i];
1143 BOOST_CHECK_EQUAL(
values[
id], result[i]);
1149 selectionArraySimpleTest<T>();
1152 template <
typename T>
1155 filename <<
"h5_rw_select_column_test_" <<
typeid(T).name() <<
"_test.h5";
1157 const size_t x_size = 10;
1168 File file(
filename.str(), File::ReadWrite | File::Create | File::Truncate);
1181 std::vector<size_t> columns{1, 3, 5};
1190 for (
size_t i = 0; i < 3; ++i)
1191 for (
size_t j = 0; j <
x_size; ++j)
1192 BOOST_CHECK_EQUAL(result[j][i],
values[j][columns[i]]);
1196 columnSelectionTest<T>();
1199 template <
typename T>
1202 filename <<
"h5_rw_attribute_scalar_rw" <<
typeid(T).name() <<
"_test.h5";
1205 File::ReadWrite | File::Create | File::Truncate);
1209 const T attribute_value(generator());
1214 BOOST_CHECK_EQUAL(family_exist,
false);
1218 T out(attribute_value);
1227 BOOST_CHECK_EQUAL(family_exist,
true);
1234 BOOST_CHECK_EQUAL(res, attribute_value);
1239 attribute_scalar_rw<T>();
1244 std::string
filename(
"h5_rw_reg_zero_dim_test.h5");
1252 DataSpace d_null(DataSpace::DataspaceType::datascape_null);
1261 BOOST_CHECK_EQUAL(ndim, 0);
1264 BOOST_CHECK_EQUAL(dims.size(), 0);
1268 template <
typename T>
1271 filename <<
"h5_rw_deflate_" <<
typeid(T).name() <<
"_test.h5";
1273 const size_t x_size = 128;
1274 const size_t y_size = 32;
1275 const size_t x_chunk = 16;
1276 const size_t y_chunk = 16;
1278 const int deflate_level = 9;
1284 File file(
filename.str(), File::ReadWrite | File::Create | File::Truncate);
1293 props.
add(
Chunking(std::vector<hsize_t>{x_chunk, y_chunk}));
1307 dataset.
write(array);
1319 dataset_read.
read(result);
1321 for (
size_t i = 0; i <
x_size; ++i) {
1322 for (
size_t j = 0; i <
y_size; ++i) {
1323 BOOST_CHECK_EQUAL(result[i][j], array[i][j]);
1330 readWriteShuffleDeflateTest<T>();
1336 const std::string
FILE_NAME(
"h5_missmatch1_dset.h5");
1343 std::vector<size_t> dims_a{1,3};
1344 std::vector<size_t> dims_b{3,1};
1347 std::vector<double> some_data{5.0, 6.0, 7.0};
1348 std::vector<double> data_a;
1349 std::vector<double> data_b;
1360 dataset_a.
write(some_data);
1361 dataset_b.
write(some_data);
1369 BOOST_CHECK_EQUAL_COLLECTIONS(
1370 data_a.begin(), data_a.end(),
1371 some_data.begin(), some_data.end());
1373 BOOST_CHECK_EQUAL_COLLECTIONS(
1374 data_b.begin(), data_b.end(),
1375 some_data.begin(), some_data.end());