-
Notifications
You must be signed in to change notification settings - Fork 158
/
tests_high_five_base.cpp
2812 lines (2202 loc) · 89.7 KB
/
tests_high_five_base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c), 2017-2019, Blue Brain Project - EPFL
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http:https://www.boost.org/LICENSE_1_0.txt)
*
*/
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <random>
#include <string>
#include <typeinfo>
#include <type_traits>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>
#include <highfive/highfive.hpp>
#include "tests_high_five.hpp"
using namespace HighFive;
using Catch::Matchers::Equals;
TEST_CASE("Basic HighFive tests") {
const std::string file_name("h5tutr_dset.h5");
const std::string dataset_name("dset");
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
CHECK(file.getName() == file_name);
// Create the data space for the dataset.
std::vector<size_t> dims{4, 6};
DataSpace dataspace(dims);
// check if the dataset exist
CHECK(!file.exist(dataset_name + "_double"));
// Create a dataset with double precision floating points
DataSet dataset_double =
file.createDataSet(dataset_name + "_double", dataspace, AtomicType<double>());
CHECK(file.getObjectName(0) == dataset_name + "_double");
{
// check if it exist again
CHECK(file.exist(dataset_name + "_double"));
// and also try to recreate it to the sake of exception testing
SilenceHDF5 silencer;
CHECK_THROWS_AS(file.createDataSet(dataset_name + "_double",
dataspace,
AtomicType<double>()),
DataSetException);
}
DataSet dataset_size_t = file.createDataSet<size_t>(dataset_name + "_size_t", dataspace);
}
TEST_CASE("Test silent HighFive") {
// Setting up a buffer for stderr so we can detect if the stack trace
// was disabled
fflush(stderr);
char buffer[1024];
memset(buffer, 0, sizeof(char) * 1024);
setvbuf(stderr, buffer, _IOLBF, 1023);
try {
SilenceHDF5 silence;
File file("nonexistent", File::ReadOnly);
} catch (const FileException&) {
}
CHECK(buffer[0] == '\0');
// restore the dyn allocated buffer
// or using stderr will segfault when buffer get out of scope
fflush(stderr);
setvbuf(stderr, NULL, _IONBF, 0);
}
TEST_CASE("Test open modes in HighFive") {
const std::string file_name("openmodes.h5");
std::remove(file_name.c_str());
SilenceHDF5 silencer;
// Attempt open file only ReadWrite should fail (wont create)
CHECK_THROWS_AS(File(file_name, File::ReadWrite), FileException);
// But with Create flag should be fine
{ File file(file_name, File::ReadWrite | File::Create); }
// But if its there and exclusive is given, should fail
CHECK_THROWS_AS(File(file_name, File::ReadWrite | File::Excl), FileException);
// ReadWrite and Excl flags are fine together (posix)
std::remove(file_name.c_str());
{ File file(file_name, File::ReadWrite | File::Excl); }
// All three are fine as well (as long as the file does not exist)
std::remove(file_name.c_str());
{ File file(file_name, File::ReadWrite | File::Create | File::Excl); }
// Just a few combinations are incompatible, detected by hdf5lib
CHECK_THROWS_AS(File(file_name, File::Truncate | File::Excl), FileException);
std::remove(file_name.c_str());
CHECK_THROWS_AS(File(file_name, File::Truncate | File::Excl), FileException);
// But in most cases we will truncate and that should always work
{ File file(file_name, File::Truncate); }
std::remove(file_name.c_str());
{ File file(file_name, File::Truncate); }
// Last but not least, defaults should be ok
{ File file(file_name); } // ReadOnly
{ File file(file_name, 0); } // force empty-flags, does open without flags
}
TEST_CASE("Test file version bounds") {
const std::string file_name("h5_version_bounds.h5");
std::remove(file_name.c_str());
{
File file(file_name, File::Truncate);
auto bounds = file.getVersionBounds();
CHECK(bounds.first == H5F_LIBVER_EARLIEST);
CHECK(bounds.second == H5F_LIBVER_LATEST);
}
std::remove(file_name.c_str());
{
FileAccessProps fapl;
fapl.add(FileVersionBounds(H5F_LIBVER_LATEST, H5F_LIBVER_LATEST));
File file(file_name, File::Truncate, fapl);
auto bounds = file.getVersionBounds();
CHECK(bounds.first == H5F_LIBVER_LATEST);
CHECK(bounds.second == H5F_LIBVER_LATEST);
}
}
#if H5_VERSION_GE(1, 10, 1)
TEST_CASE("Test file space strategy") {
const std::string file_name("h5_file_space_strategy.h5");
auto strategies = std::vector<H5F_fspace_strategy_t>{H5F_FSPACE_STRATEGY_FSM_AGGR,
H5F_FSPACE_STRATEGY_AGGR,
H5F_FSPACE_STRATEGY_PAGE,
H5F_FSPACE_STRATEGY_NONE};
for (const auto& strategy: strategies) {
{
FileCreateProps create_props;
create_props.add(FileSpaceStrategy(strategy, true, 0));
File file(file_name, File::Truncate, create_props);
}
{
File file(file_name, File::ReadOnly);
CHECK(file.getFileSpaceStrategy() == strategy);
}
}
}
TEST_CASE("Test file space page size") {
const std::string file_name("h5_file_space_page_size.h5");
hsize_t page_size = 1024;
{
FileCreateProps create_props;
create_props.add(FileSpaceStrategy(H5F_FSPACE_STRATEGY_PAGE, true, 0));
create_props.add(FileSpacePageSize(page_size));
File file(file_name, File::Truncate, create_props);
}
{
File file(file_name, File::ReadOnly);
CHECK(file.getFileSpacePageSize() == page_size);
}
}
#ifndef H5_HAVE_PARALLEL
TEST_CASE("Test page buffer size") {
const std::string file_name("h5_page_buffer_size.h5");
hsize_t page_size = 1024;
{
FileCreateProps create_props;
create_props.add(FileSpaceStrategy(H5F_FSPACE_STRATEGY_PAGE, true, 0));
create_props.add(FileSpacePageSize(page_size));
FileAccessProps access_props;
access_props.add(FileVersionBounds(H5F_LIBVER_V110, H5F_LIBVER_V110));
File file(file_name, File::Truncate, create_props, access_props);
file.createDataSet("x", std::vector<double>{1.0, 2.0, 3.0});
}
{
FileAccessProps access_props;
access_props.add(PageBufferSize(1024));
File file(file_name, File::ReadOnly, access_props);
auto accesses = std::array<unsigned int, 2>{0, 0};
auto hits = std::array<unsigned int, 2>{0, 0};
auto misses = std::array<unsigned int, 2>{0, 0};
auto evictions = std::array<unsigned int, 2>{0, 0};
auto bypasses = std::array<unsigned int, 2>{0, 0};
auto err = H5Fget_page_buffering_stats(file.getId(),
accesses.data(),
hits.data(),
misses.data(),
evictions.data(),
bypasses.data());
REQUIRE(err >= 0);
CHECK(accesses[0] == 0);
CHECK(accesses[1] == 0);
CHECK(bypasses[0] == 0);
CHECK(bypasses[1] == 0);
auto x = file.getDataSet("x").read<std::vector<double>>();
err = H5Fget_page_buffering_stats(file.getId(),
accesses.data(),
hits.data(),
misses.data(),
evictions.data(),
bypasses.data());
REQUIRE(err >= 0);
CHECK(accesses[0] > 0);
CHECK(accesses[1] == 1);
CHECK(bypasses[0] == 0);
CHECK(bypasses[1] == 0);
CHECK(file.getFileSpacePageSize() == page_size);
}
}
#endif
#endif
TEST_CASE("Test metadata block size assignment") {
const std::string file_name("h5_meta_block_size.h5");
std::remove(file_name.c_str());
{
File file(file_name, File::Truncate);
// Default for HDF5
CHECK(file.getMetadataBlockSize() == 2048);
}
std::remove(file_name.c_str());
{
FileAccessProps fapl;
fapl.add(MetadataBlockSize(10240));
File file(file_name, File::Truncate, fapl);
CHECK(file.getMetadataBlockSize() == 10240);
}
}
TEST_CASE("Test group properties") {
const std::string file_name("h5_group_properties.h5");
FileAccessProps fapl;
// When using hdf5 1.10.2 and later, the lower bound may be set to
// H5F_LIBVER_V18
fapl.add(FileVersionBounds(H5F_LIBVER_LATEST, H5F_LIBVER_LATEST));
File file(file_name, File::Truncate, fapl);
GroupCreateProps props;
props.add(EstimatedLinkInfo(10, 60));
auto group = file.createGroup("g", props);
auto sizes = group.getEstimatedLinkInfo();
CHECK(sizes.first == 10);
CHECK(sizes.second == 60);
}
TEST_CASE("Test allocation time") {
const std::string file_name("h5_dataset_alloc_time.h5");
File file(file_name, File::Truncate);
size_t n_elements = 10;
std::vector<double> data(n_elements);
auto dcpl = DataSetCreateProps{};
dcpl.add(AllocationTime(H5D_ALLOC_TIME_EARLY));
auto dataspace = DataSpace::From(data);
auto datatype = create_datatype<decltype(data)::value_type>();
auto dataset = file.createDataSet("dset", dataspace, datatype, dcpl);
auto alloc_size = H5Dget_storage_size(dataset.getId());
CHECK(alloc_size == data.size() * sizeof(decltype(data)::value_type));
}
/*
* Test to ensure legacy support: DataSet used to have a default constructor.
* However, it is not useful to have a DataSet object that does not actually
* refer to a dataset in a file. Hence, the the default constructor was
* deprecated.
* This test is to ensure that the constructor is not accidentally removed and
* thereby break users' code.
*/
TEST_CASE("Test default constructors") {
const std::string file_name("h5_default_ctors.h5");
const std::string dataset_name("dset");
File file(file_name, File::Truncate);
auto ds = file.createDataSet(dataset_name, std::vector<int>{1, 2, 3, 4, 5});
DataSet d2; // expect deprecation warning, as it constructs unsafe object
// d2.getFile(); // runtime error
CHECK(!d2.isValid());
d2 = ds; // copy
CHECK(d2.isValid());
}
TEST_CASE("Test groups and datasets") {
const std::string file_name("h5_group_test.h5");
const std::string dataset_name("dset");
const std::string chunked_dataset_name("chunked_dset");
const std::string chunked_dataset_small_name("chunked_dset_small");
const std::string group_name_1("/group1");
const std::string group_name_2("group2");
const std::string group_nested_name("group_nested");
{
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
// absolute group
file.createGroup(group_name_1);
// nested group absolute
file.createGroup(group_name_1 + "/" + group_nested_name);
// relative group
Group g1 = file.createGroup(group_name_2);
// relative group
Group nested = g1.createGroup(group_nested_name);
// Create the data space for the dataset.
std::vector<size_t> dims{4, 6};
DataSpace dataspace(dims);
DataSet dataset_absolute = file.createDataSet(group_name_1 + "/" + group_nested_name + "/" +
dataset_name,
dataspace,
AtomicType<double>());
DataSet dataset_relative =
nested.createDataSet(dataset_name, dataspace, AtomicType<double>());
DataSetCreateProps goodChunking;
goodChunking.add(Chunking(std::vector<hsize_t>{2, 2}));
DataSetAccessProps cacheConfig;
cacheConfig.add(Caching(13, 1024, 0.5));
// will fail because exceeds dimensions
DataSetCreateProps badChunking0;
badChunking0.add(Chunking(std::vector<hsize_t>{10, 10}));
DataSetCreateProps badChunking1;
badChunking1.add(Chunking(std::vector<hsize_t>{1, 1, 1}));
{
SilenceHDF5 silencer;
CHECK_THROWS_AS(file.createDataSet(chunked_dataset_name,
dataspace,
AtomicType<double>(),
badChunking0),
DataSetException);
CHECK_THROWS_AS(file.createDataSet(chunked_dataset_name,
dataspace,
AtomicType<double>(),
badChunking1),
DataSetException);
}
// here we use the other signature
DataSet dataset_chunked =
file.createDataSet<float>(chunked_dataset_name, dataspace, goodChunking, cacheConfig);
// Here we resize to smaller than the chunking size
DataSet dataset_chunked_small =
file.createDataSet<float>(chunked_dataset_small_name, dataspace, goodChunking);
dataset_chunked_small.resize({1, 1});
}
// read it back
{
File file(file_name, File::ReadOnly);
Group g1 = file.getGroup(group_name_1);
Group g2 = file.getGroup(group_name_2);
Group nested_group2 = g2.getGroup(group_nested_name);
DataSet dataset_absolute = file.getDataSet(group_name_1 + "/" + group_nested_name + "/" +
dataset_name);
CHECK(4 == dataset_absolute.getSpace().getDimensions()[0]);
DataSet dataset_relative = nested_group2.getDataSet(dataset_name);
CHECK(4 == dataset_relative.getSpace().getDimensions()[0]);
DataSetAccessProps accessProps;
accessProps.add(Caching(13, 1024, 0.5));
DataSet dataset_chunked = file.getDataSet(chunked_dataset_name, accessProps);
CHECK(4 == dataset_chunked.getSpace().getDimensions()[0]);
DataSet dataset_chunked_small = file.getDataSet(chunked_dataset_small_name);
CHECK(1 == dataset_chunked_small.getSpace().getDimensions()[0]);
}
}
TEST_CASE("FileSpace") {
const std::string filename = "filespace.h5";
const std::string ds_path = "dataset";
const std::vector<int> data{13, 24, 36};
File file(filename, File::Truncate);
file.createDataSet(ds_path, data);
CHECK(file.getFileSize() > 0);
}
TEST_CASE("FreeSpace (default)") {
const std::string filename = "freespace_default.h5";
const std::string ds_path = "dataset";
const std::vector<int> data{13, 24, 36};
{
File file(filename, File::Truncate);
auto dset = file.createDataSet(ds_path, data);
}
{
File file(filename, File::ReadWrite);
file.unlink(ds_path);
CHECK(file.getFreeSpace() > 0);
CHECK(file.getFreeSpace() < file.getFileSize());
}
}
#if H5_VERSION_GE(1, 10, 1)
TEST_CASE("FreeSpace (tracked)") {
const std::string filename = "freespace_tracked.h5";
const std::string ds_path = "dataset";
const std::vector<int> data{13, 24, 36};
{
FileCreateProps fcp;
fcp.add(FileSpaceStrategy(H5F_FSPACE_STRATEGY_FSM_AGGR, true, 0));
File file(filename, File::Truncate, fcp);
auto dset = file.createDataSet(ds_path, data);
}
{
File file(filename, File::ReadWrite);
file.unlink(ds_path);
#if H5_VERSION_GE(1, 12, 0)
// This fails on 1.10.x but starts working in 1.12.0
CHECK(file.getFreeSpace() > 0);
#endif
CHECK(file.getFreeSpace() < file.getFileSize());
}
{
File file(filename, File::ReadOnly);
CHECK(file.getFreeSpace() > 0);
CHECK(file.getFreeSpace() < file.getFileSize());
}
}
#endif
TEST_CASE("Test extensible datasets") {
const std::string file_name("create_extensible_dataset_example.h5");
const std::string dataset_name("dset");
constexpr long double t1[3][1] = {{2.0l}, {2.0l}, {4.0l}};
constexpr long double t2[1][3] = {{4.0l, 8.0l, 6.0l}};
{
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
// Create a dataspace with initial shape and max shape
DataSpace dataspace = DataSpace({4, 5}, {17, DataSpace::UNLIMITED});
// Use chunking
DataSetCreateProps props;
props.add(Chunking(std::vector<hsize_t>{2, 2}));
// Create the dataset
DataSet dataset =
file.createDataSet(dataset_name, dataspace, AtomicType<long double>(), props);
// Write into the initial part of the dataset
dataset.select({0, 0}, {3, 1}).write(t1);
// Resize the dataset to a larger size
dataset.resize({4, 6});
CHECK(4 == dataset.getSpace().getDimensions()[0]);
CHECK(6 == dataset.getSpace().getDimensions()[1]);
// Write into the new part of the dataset
dataset.select({3, 3}, {1, 3}).write(t2);
SilenceHDF5 silencer;
// Try resize out of bounds
CHECK_THROWS_AS(dataset.resize({18, 1}), DataSetException);
// Try resize invalid dimensions
CHECK_THROWS_AS(dataset.resize({1, 2, 3}), DataSetException);
}
// read it back
{
File file(file_name, File::ReadOnly);
DataSet dataset_absolute = file.getDataSet("/" + dataset_name);
const auto dims = dataset_absolute.getSpace().getDimensions();
long double values[4][6];
dataset_absolute.read(values);
CHECK(4 == dims[0]);
CHECK(6 == dims[1]);
CHECK(t1[0][0] == values[0][0]);
CHECK(t1[1][0] == values[1][0]);
CHECK(t1[2][0] == values[2][0]);
CHECK(t2[0][0] == values[3][3]);
CHECK(t2[0][1] == values[3][4]);
CHECK(t2[0][2] == values[3][5]);
}
}
TEST_CASE("Test reference count") {
const std::string file_name("h5_ref_count_test.h5");
const std::string dataset_name("dset");
const std::string group_name_1("/group1");
const std::string group_name_2("/group2");
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
std::unique_ptr<DataSet> d1_ptr;
std::unique_ptr<Group> g_ptr;
{
// create group
Group g1 = file.createGroup(group_name_1);
// override object
g1 = file.createGroup(group_name_2);
// Create the data space for the dataset.
std::vector<size_t> dims = {10, 10};
DataSpace dataspace(dims);
DataSet d1 =
file.createDataSet(group_name_1 + dataset_name, dataspace, AtomicType<double>());
double values[10][10] = {{0}};
values[5][0] = 1;
d1.write(values);
// force move
d1_ptr.reset(new DataSet(std::move(d1)));
// force copy
g_ptr.reset(new Group(g1));
}
// read it back
{
DataSet d2(std::move(*d1_ptr));
d1_ptr.reset();
double values[10][10];
d2.read(values);
for (std::size_t i = 0; i < 10; ++i) {
for (std::size_t j = 0; j < 10; ++j) {
double v = values[i][j];
if (i == 5 && j == 0) {
REQUIRE(v == 1);
} else {
REQUIRE(v == 0);
}
}
}
// force copy
Group g2 = *g_ptr;
// add a subgroup
g2.createGroup("blabla");
}
}
TEST_CASE("Test simple listings") {
const std::string file_name("h5_list_test.h5");
const std::string group_name_core("group_name");
const std::string group_nested_name("/group_nested");
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
{
// absolute group
for (int i = 0; i < 2; ++i) {
std::ostringstream ss;
ss << "/" << group_name_core << "_" << i;
file.createGroup(ss.str());
}
size_t n_elem = file.getNumberObjects();
CHECK(2 == n_elem);
std::vector<std::string> elems = file.listObjectNames();
CHECK(2 == elems.size());
std::vector<std::string> reference_elems;
for (int i = 0; i < 2; ++i) {
std::ostringstream ss;
ss << group_name_core << "_" << i;
reference_elems.push_back(ss.str());
}
CHECK(elems == reference_elems);
}
{
file.createGroup(group_nested_name);
Group g_nest = file.getGroup(group_nested_name);
for (int i = 0; i < 50; ++i) {
std::ostringstream ss;
ss << group_name_core << "_" << i;
g_nest.createGroup(ss.str());
}
size_t n_elem = g_nest.getNumberObjects();
CHECK(50 == n_elem);
std::vector<std::string> elems = g_nest.listObjectNames();
CHECK(50 == elems.size());
std::vector<std::string> reference_elems;
for (int i = 0; i < 50; ++i) {
std::ostringstream ss;
ss << group_name_core << "_" << i;
reference_elems.push_back(ss.str());
}
// there is no guarantee on the order of the hdf5 index, let's sort it
// to put them in order
std::sort(elems.begin(), elems.end());
std::sort(reference_elems.begin(), reference_elems.end());
CHECK(elems == reference_elems);
}
}
TEST_CASE("StringType") {
SECTION("enshrine-defaults") {
auto fixed_length = FixedLengthStringType(32, StringPadding::SpacePadded);
auto variable_length = VariableLengthStringType();
REQUIRE(fixed_length.getCharacterSet() == CharacterSet::Ascii);
REQUIRE(variable_length.getCharacterSet() == CharacterSet::Ascii);
}
SECTION("fixed-length") {
auto fixed_length =
FixedLengthStringType(32, StringPadding::SpacePadded, CharacterSet::Utf8);
auto string_type = fixed_length.asStringType();
REQUIRE(string_type.getId() == fixed_length.getId());
REQUIRE(string_type.getCharacterSet() == CharacterSet::Utf8);
REQUIRE(string_type.getPadding() == StringPadding::SpacePadded);
REQUIRE(string_type.getSize() == 32);
REQUIRE(!string_type.isVariableStr());
REQUIRE(string_type.isFixedLenStr());
}
SECTION("variable-length") {
auto variable_length = VariableLengthStringType(CharacterSet::Utf8);
auto string_type = variable_length.asStringType();
REQUIRE(string_type.getId() == variable_length.getId());
REQUIRE(string_type.getCharacterSet() == CharacterSet::Utf8);
REQUIRE(string_type.isVariableStr());
REQUIRE(!string_type.isFixedLenStr());
}
SECTION("atomic") {
auto atomic = AtomicType<double>();
REQUIRE_THROWS(atomic.asStringType());
}
}
TEST_CASE("DataTypeEqualTakeBack") {
const std::string file_name("h5tutr_dset.h5");
const std::string dataset_name("dset");
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
// Create the data space for the dataset.
std::vector<size_t> dims{10, 1};
DataSpace dataspace(dims);
// Create a dataset with double precision floating points
DataSet dataset = file.createDataSet<size_t>(dataset_name + "_double", dataspace);
AtomicType<size_t> s;
AtomicType<double> d;
CHECK(s == dataset.getDataType());
CHECK(d != dataset.getDataType());
// Test getAddress and expect deprecation warning
auto addr = dataset.getInfo().getAddress();
CHECK(addr != 0);
}
TEST_CASE("DataSpaceTest") {
const std::string file_name("h5tutr_space.h5");
const std::string dataset_name("dset");
// Create a new file using the default property lists.
File file(file_name, File::ReadWrite | File::Create | File::Truncate);
// Create the data space for the dataset.
DataSpace dataspace{std::vector<size_t>{10, 1}};
// Create a dataset with size_t type
DataSet dataset = file.createDataSet<size_t>(dataset_name, dataspace);
DataSpace space = dataset.getSpace();
DataSpace space2 = dataset.getSpace();
auto space3 = space.clone();
// verify space id are different
CHECK(space.getId() != space2.getId());
CHECK(space.getId() != space3.getId());
// verify space id are consistent
CHECK(space.getDimensions().size() == 2);
CHECK(space.getDimensions()[0] == 10);
CHECK(space.getDimensions()[1] == 1);
}
TEST_CASE("DataSpace::getElementCount") {
SECTION("null") {
auto space = DataSpace(DataSpace::dataspace_null);
CHECK(space.getElementCount() == 0);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_NULL);
}
SECTION("null named ctor") {
auto space = DataSpace::Null();
CHECK(space.getElementCount() == 0);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_NULL);
}
SECTION("scalar") {
auto space = DataSpace(DataSpace::dataspace_scalar);
CHECK(space.getElementCount() == 1);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_SCALAR);
}
SECTION("scalar named ctor") {
auto space = DataSpace::Scalar();
CHECK(space.getElementCount() == 1);
CHECK(detail::h5s_get_simple_extent_type(space.getId()) == H5S_SCALAR);
}
SECTION("simple, empty (1D)") {
auto space = DataSpace(0);
CHECK(space.getElementCount() == 0);
}
SECTION("simple, empty (2D)") {
auto space = DataSpace(0, 0);
CHECK(space.getElementCount() == 0);
}
SECTION("simple, non-empty (2D)") {
auto space = DataSpace(2, 3);
CHECK(space.getElementCount() == 6);
}
SECTION("FromCharArrayStrings") {
char string_array[2][10] = {"123456789", "abcdefghi"};
auto space = DataSpace::FromCharArrayStrings(string_array);
CHECK(space.getElementCount() == 2);
}
}
TEST_CASE("DataSpaceVectorTest") {
// Create 1D shortcut dataspace
DataSpace space(7);
CHECK(space.getDimensions().size() == 1);
CHECK(space.getDimensions()[0] == 7);
// Initializer list (explicit)
DataSpace space3({8, 9, 10});
auto space3_res = space3.getDimensions();
std::vector<size_t> space3_ans{8, 9, 10};
CHECK(space3_res == space3_ans);
// Verify 2D works (note that without the {}, this matches the iterator
// constructor)
DataSpace space2(std::vector<size_t>{3, 4});
auto space2_res = space2.getDimensions();
std::vector<size_t> space2_ans{3, 4};
CHECK(space2_res == space2_ans);
}
TEST_CASE("DataSpaceVariadicTest") {
// Create 1D shortcut dataspace
DataSpace space1{7};
auto space1_res = space1.getDimensions();
std::vector<size_t> space1_ans{7};
CHECK(space1_res == space1_ans);
// Initializer list (explicit)
DataSpace space3{8, 9, 10};
auto space3_res = space3.getDimensions();
std::vector<size_t> space3_ans{8, 9, 10};
CHECK(space3_res == space3_ans);
// Verify 2D works using explicit syntax
DataSpace space2{3, 4};
auto space2_res = space2.getDimensions();
std::vector<size_t> space2_ans{3, 4};
CHECK(space2_res == space2_ans);
// Verify 2D works using old syntax (this used to match the iterator!)
DataSpace space2b(3, 4);
auto space2b_res = space2b.getDimensions();
std::vector<size_t> space2b_ans{3, 4};
CHECK(space2b_res == space2b_ans);
}
TEST_CASE("ChunkingConstructorsTest") {
Chunking first(1, 2, 3);
auto first_res = first.getDimensions();
std::vector<hsize_t> first_ans{1, 2, 3};
CHECK(first_res == first_ans);
Chunking second{1, 2, 3};
auto second_res = second.getDimensions();
std::vector<hsize_t> second_ans{1, 2, 3};
CHECK(second_res == second_ans);
Chunking third({1, 2, 3});
auto third_res = third.getDimensions();
std::vector<hsize_t> third_ans{1, 2, 3};
CHECK(third_res == third_ans);
}
TEST_CASE("HighFiveReadWriteShortcut") {
std::ostringstream filename;
filename << "h5_rw_vec_shortcut_test.h5";
const unsigned x_size = 800;
const std::string dataset_name("dset");
std::vector<unsigned> vec;
vec.resize(x_size);
for (unsigned i = 0; i < x_size; i++) {
vec[i] = i * 2;
}
std::string at_contents("Contents of string");
int my_int = 3;
std::vector<std::vector<int>> my_nested = {{1, 2}, {3, 4}};
// Create a new file using the default property lists.
File file(filename.str(), File::ReadWrite | File::Create | File::Truncate);
// Create a dataset with int points
DataSet dataset = file.createDataSet(dataset_name, vec);
dataset.createAttribute("str", at_contents);
DataSet ds_int = file.createDataSet("/TmpInt", my_int);
DataSet ds_nested = file.createDataSet("/TmpNest", my_nested);
std::vector<unsigned> result;
dataset.read(result);
CHECK_THAT(vec, Equals(result));
std::string read_in;
dataset.getAttribute("str").read(read_in);
CHECK(read_in == at_contents);
int out_int = 0;
ds_int.read(out_int);
CHECK(my_int == out_int);
decltype(my_nested) out_nested;
ds_nested.read(out_nested);
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 2; ++j) {
REQUIRE(my_nested[i][j] == out_nested[i][j]);
}
}
// Plain c arrays. 1D
SECTION("int-c-array") {
int int_c_array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
DataSet ds_int2 = file.createDataSet("/TmpCArrayInt", int_c_array);
decltype(int_c_array) int_c_array_out;
ds_int2.read(int_c_array_out);
for (size_t i = 0; i < 10; ++i) {
REQUIRE(int_c_array[i] == int_c_array_out[i]);
}
}
// Plain c arrays. 2D
SECTION("char-c-array") {
char char_c_2darray[][3] = {"aa", "bb", "cc", "12"};
DataSet ds_char2 = file.createDataSet("/TmpCArray2dchar", char_c_2darray);
decltype(char_c_2darray) char_c_2darray_out;
ds_char2.read(char_c_2darray_out);
for (size_t i = 0; i < 4; ++i) {
for (size_t j = 0; j < 3; ++j) {
REQUIRE(char_c_2darray[i][j] == char_c_2darray_out[i][j]);
}
}
}
}
template <typename T>
void readWriteAttributeVectorTest() {
std::ostringstream filename;
filename << "h5_rw_attribute_vec_" << typeNameHelper<T>() << "_test.h5";
std::srand((unsigned) std::time(0));
const size_t x_size = 25;
const std::string dataset_name("dset");
typename std::vector<T> vec;
// Create a new file using the default property lists.
File file(filename.str(), File::ReadWrite | File::Create | File::Truncate);
vec.resize(x_size);
ContentGenerate<T> generator;
std::generate(vec.begin(), vec.end(), generator);
{
// Create a dummy group to annotate with an attribute
Group g = file.createGroup("dummy_group");
// check that no attributes are there
std::size_t n = g.getNumberAttributes();
CHECK(n == 0);
std::vector<std::string> all_attribute_names = g.listAttributeNames();
CHECK(all_attribute_names.size() == 0);