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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
|
/*
* Copyright (C) 2001 National Center for Supercomputing Applications
* All rights reserved.
*
*/
/***********************************************************
*
* Test program: dsets
*
* Test the dataset interface
*
*************************************************************/
#include <iostream>
#include "H5Cpp.h"
#include "h5test.h"
#include "testhdf5.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const char *FILENAME[] = {
"dataset",
NULL
};
#define DSET_DEFAULT_NAME "default"
#define DSET_CHUNKED_NAME "chunked"
#define DSET_SIMPLE_IO_NAME "simple_io"
#define DSET_TCONV_NAME "tconv"
#define DSET_COMPRESS_NAME "compressed"
#define DSET_BOGUS_NAME "bogus"
#define H5Z_BOGUS 305
/*-------------------------------------------------------------------------
* Function: test_create
*
* Purpose: Attempts to create a dataset.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* Friday, January 5, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_create( H5File& file)
{
TESTING("create, open, close");
try {
/* Create the data space */
hsize_t dims[2];
dims[0] = 256;
dims[1] = 512;
DataSpace space (2, dims, NULL);
/*
* Create a dataset using the default dataset creation properties.
* We're not sure what they are, so we won't check.
*/
DataSet *dataset = new DataSet (file.createDataSet
(DSET_DEFAULT_NAME, PredType::NATIVE_DOUBLE, space));
/* Close the dataset */
delete dataset;
/* Add a comment to the dataset */
file.setComment (DSET_DEFAULT_NAME, "This is a dataset");
/*
* Try creating a dataset that already exists. This should fail since a
* dataset can only be created once. If an exception is not thrown
* for this action by createDataSet, then display failure information
* and jump to label error: to return.
*/
try {
dataset = new DataSet (file.createDataSet
(DSET_DEFAULT_NAME, PredType::NATIVE_DOUBLE, space));
// continuation here, that means no exception has been thrown
H5_FAILED();
cout << " Library allowed overwrite of existing dataset." << endl;
goto error;
}
catch (FileIException E ) // catching invalid creating dataset
{
// Exception is expected. Do nothing here.
}
/*
* Open the dataset we created above and then close it. This is how
* existing datasets are accessed.
*/
dataset = new DataSet (file.openDataSet (DSET_DEFAULT_NAME));
delete dataset;
/*
* Try opening a non-existent dataset. This should fail so if an
* exception is not thrown for this action by openDataSet, then
* display failure information and jump to label error: to return.
*/
try {
dataset = new DataSet (file.openDataSet( "does_not_exist" ));
// continuation here, that means no exception has been thrown
H5_FAILED();
cout << " Opened a non-existent dataset." << endl;
goto error;
}
catch (FileIException E ) // catching creating non-existent dataset
{
// Exception is expected. Do nothing here.
}
/*
* Create a new dataset that uses chunked storage instead of the default
* layout.
*/
DSetCreatPropList create_parms;
hsize_t csize[2];
csize[0] = 5;
csize[1] = 100;
create_parms.setChunk( 2, csize );
dataset = new DataSet (file.createDataSet
(DSET_CHUNKED_NAME, PredType::NATIVE_DOUBLE, space, create_parms));
// Note: this one has no error message in C when failure occurs?
/*
* Close the chunked dataset.
*/
delete dataset;
PASSED();
return 0;
} // outer most try block
// catch all dataset, file, space, plist exceptions
catch (Exception E) { goto error; }
error:
return -1;
}
/*-------------------------------------------------------------------------
* Function: check_values
*
* Purpose: Checks a read value against the written value. If they are
* different, the function will
* print out a message and the different values. This function
* is made to reuse the code segment that is used in various
* places throughout test_compression and in test_simple_io.
* Where the C version
* of this code segment "goto error," this function will
* return -1, so that the caller can "goto error."
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C code segment for checking values)
* Friday, February 6, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static int
check_values (hsize_t i, hsize_t j, int apoint, int acheck)
{
if (apoint != acheck)
{
H5_FAILED();
cout << " Read different values than written.\n" << endl;
cout << " At index " << (unsigned long)i << "," <<
(unsigned long)j << endl;
return -1;
}
return 0;
} // check_values
/*-------------------------------------------------------------------------
* Function: test_simple_io
*
* Purpose: Tests simple I/O. That is, reading and writing a complete
* multi-dimensional array without data type or data space
* conversions, without compression, and stored contiguously.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* Friday, January 5, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_simple_io( H5File& file)
{
TESTING("simple I/O");
int points[100][200];
int check[100][200];
int i, j, n;
/* Initialize the dataset */
for (i = n = 0; i < 100; i++)
{
for (j = 0; j < 200; j++) {
points[i][j] = n++;
}
}
char* tconv_buf = new char [1000];
try
{
/* Create the data space */
hsize_t dims[2];
dims[0] = 100;
dims[1] = 200;
DataSpace space (2, dims, NULL);
/* Create a small conversion buffer to test strip mining */
DSetMemXferPropList xfer;
//if (H5Pset_buffer (xfer, 1000, tconv_buf, NULL)<0) goto error;
xfer.setBuffer (1000, tconv_buf, NULL);
/* Create the dataset */
DataSet dataset (file.createDataSet (DSET_SIMPLE_IO_NAME, PredType::NATIVE_INT, space));
/* Write the data to the dataset */
dataset.write ((void*) points, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Read the dataset back */
dataset.read ((void*) check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Check that the values read are the same as the values written */
for (i = 0; i < 100; i++)
for (j = 0; j < 200; j++)
{
int status = check_values (i, j, points[i][j], check[i][j]);
if (status == -1) goto error;
}
delete [] tconv_buf;
PASSED();
return 0;
} // end try
// catch all dataset, space, plist exceptions
catch (Exception E) { goto error; }
error:
// cleaning up
if (tconv_buf)
delete [] tconv_buf;
return -1;
}
/*-------------------------------------------------------------------------
* Function: test_tconv
*
* Purpose: Test some simple data type conversion stuff.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* Friday, January 5, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_tconv( H5File& file)
{
// Prepare buffers for input/output
char *out=NULL, *in=NULL;
out = new char [4*1000000];
// assert (out); - should use exception handler for new - BMR
in = new char [4*1000000];
//assert (in);
TESTING("data type conversion");
/* Initialize the dataset */
for (int i = 0; i < 1000000; i++) {
out[i*4+0] = 0x11;
out[i*4+1] = 0x22;
out[i*4+2] = 0x33;
out[i*4+3] = 0x44;
}
try
{
/* Create the data space */
hsize_t dims[1];
dims[0] = 1000000;
DataSpace space (1, dims, NULL);
/* Create the data set */
DataSet dataset (file.createDataSet (DSET_TCONV_NAME, PredType::STD_I32LE, space));
/* Write the data to the dataset */
dataset.write ((void*) out, PredType::STD_I32LE);
/* Read data with byte order conversion */
dataset.read ((void*) in, PredType::STD_I32BE);
/* Check */
for (int i = 0; i < 1000000; i++) {
if (in[4*i+0]!=out[4*i+3] ||
in[4*i+1]!=out[4*i+2] ||
in[4*i+2]!=out[4*i+1] ||
in[4*i+3]!=out[4*i+0])
{
H5_FAILED();
cout << " Read with byte order conversion failed." << endl;
goto error;
}
}
delete [] out;
delete [] in;
cout << " PASSED" << endl;
return 0;
} // end try
// catch all dataset and space exceptions
catch (Exception E) { goto error; }
error:
delete [] out;
delete [] in;
return -1;
}
/*-------------------------------------------------------------------------
* Function: bogus
*
* Purpose: A bogus compression method that doesn't do anything.
*
* Return: Success: Data chunk size
*
* Failure: 0
*
* Programmer: Robb Matzke
* Tuesday, April 21, 1998
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static size_t
/*bogus(unsigned int UNUSED flags, size_t UNUSED cd_nelmts,
const unsigned int UNUSED cd_values[], size_t nbytes,
size_t UNUSED *buf_size, void UNUSED **buf)
BMR: removed UNUSED for now until asking Q. or R. to pass compilation*/
bogus(unsigned int flags, size_t cd_nelmts,
const unsigned int cd_values[], size_t nbytes,
size_t *buf_size, void **buf)
{
return nbytes;
}
/*-------------------------------------------------------------------------
* Function: test_compression
*
* Purpose: Tests dataset compression. If compression is requested when
* it hasn't been compiled into the library (such as when
* updating an existing compressed dataset) then data is sent to
* the file uncompressed but no errors are returned.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* Friday, January 5, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_compression(H5File& file)
{
const char *not_supported;
not_supported = " Deflate compression is not supported.\n"
" The zlib was not found when hdf5 was configured.";
TESTING("compression (setup)");
int points[100][200];
int check[100][200];
hsize_t i, j, n;
/* Initialize the dataset */
for (i = n = 0; i < 100; i++)
{
for (j = 0; j < 200; j++) {
points[i][j] = n++;
}
}
char* tconv_buf = new char [1000];
try
{
const hsize_t size[2] = {100, 200};
/* Create the data space */
DataSpace space1(2, size, NULL);
/*
* Create a small conversion buffer to test strip mining. We
* might as well test all we can!
*/
DSetMemXferPropList xfer;
xfer.setBuffer (1000, tconv_buf, NULL);
/* Use chunked storage with compression */
DSetCreatPropList dscreatplist;
const hsize_t chunk_size[2] = {2, 25};
dscreatplist.setChunk (2, chunk_size);
dscreatplist.setDeflate (6);
/* Create the dataset */
DataSet* dataset = new DataSet (file.createDataSet
(DSET_COMPRESS_NAME, PredType::NATIVE_INT, space1, dscreatplist));
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 1: Read uninitialized data. It should be zero.
*----------------------------------------------------------------------
*/
TESTING("compression (uninitialized read)");
dataset->read ((void*) check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
for (i=0; i<size[0]; i++) {
for (j=0; j<size[1]; j++) {
if (0!=check[i][j]) {
H5_FAILED();
cout << " Read a non-zero value." << endl;
cout << " At index " << (unsigned long)i << "," <<
(unsigned long)j << endl;
goto error;
}
}
}
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 2: Test compression by setting up a chunked dataset and writing
* to it.
*----------------------------------------------------------------------
*/
TESTING("compression (write)");
for (i=n=0; i<size[0]; i++)
{
for (j=0; j<size[1]; j++)
{
points[i][j] = n++;
}
}
//if (H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, xfer, points)<0) goto error;
dataset->write ((void*) points, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 3: Try to read the data we just wrote.
*----------------------------------------------------------------------
*/
TESTING("compression (read)");
/* Read the dataset back */
dataset->read ((void*)check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Check that the values read are the same as the values written */
for (i = 0; i < size[0]; i++)
for (j = 0; j < size[1]; j++)
{
int status = check_values (i, j, points[i][j], check[i][j]);
if (status == -1) goto error;
}
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 4: Write new data over the top of the old data. The new data is
* random thus not very compressible, and will cause the chunks to move
* around as they grow. We only change values for the left half of the
* dataset although we rewrite the whole thing.
*----------------------------------------------------------------------
*/
TESTING("compression (modify)");
for (i=0; i<size[0]; i++)
{
for (j=0; j<size[1]/2; j++)
{
points[i][j] = rand ();
}
}
dataset->write ((void*)points, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Read the dataset back and check it */
dataset->read ((void*)check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Check that the values read are the same as the values written */
for (i = 0; i < size[0]; i++)
for (j = 0; j < size[1]; j++)
{
int status = check_values (i, j, points[i][j], check[i][j]);
if (status == -1) goto error;
}
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 5: Close the dataset and then open it and read it again. This
* insures that the compression message is picked up properly from the
* object header.
*----------------------------------------------------------------------
*/
TESTING("compression (re-open)");
delete dataset;
//if ((dataset = H5Dopen (file, DSET_COMPRESS_NAME))<0) goto error;
dataset = new DataSet (file.openDataSet (DSET_COMPRESS_NAME));
dataset->read ((void*)check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Check that the values read are the same as the values written */
for (i = 0; i < size[0]; i++)
for (j = 0; j < size[1]; j++)
{
int status = check_values (i, j, points[i][j], check[i][j]);
if (status == -1) goto error;
}
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 6: Test partial I/O by writing to and then reading from a
* hyperslab of the dataset. The hyperslab does not line up on chunk
* boundaries (we know that case already works from above tests).
*----------------------------------------------------------------------
*/
TESTING("compression (partial I/O)");
const hsize_t hs_size[2] = {4, 50};
const hssize_t hs_offset[2] = {7, 30};
for (i = 0; i < hs_size[0]; i++) {
for (j = 0; j < hs_size[1]; j++) {
points[hs_offset[0]+i][hs_offset[1]+j] = rand ();
}
}
space1.selectHyperslab( H5S_SELECT_SET, hs_size, hs_offset );
dataset->write ((void*)points, PredType::NATIVE_INT, space1, space1, xfer);
dataset->read ((void*)check, PredType::NATIVE_INT, space1, space1, xfer);
/* Check that the values read are the same as the values written */
for (i=0; i<hs_size[0]; i++) {
for (j=0; j<hs_size[1]; j++) {
if (points[hs_offset[0]+i][hs_offset[1]+j] !=
check[hs_offset[0]+i][hs_offset[1]+j]) {
H5_FAILED();
cout << " Read different values than written.\n" << endl;
cout << " At index " << (unsigned long)(hs_offset[0]+i) <<
"," << (unsigned long)(hs_offset[1]+j) << endl;
cout << " At original: " << (int)points[hs_offset[0]+i][hs_offset[1]+j] << endl;
cout << " At returned: " << (int)check[hs_offset[0]+i][hs_offset[1]+j] << endl;
goto error;
}
}
}
#if defined(H5_HAVE_COMPRESS2) && defined(H5_HAVE_ZLIB_H) && defined(H5_HAVE_LIBZ)
PASSED();
#else
SKIPPED();
cout << not_supported << endl;
#endif
/*----------------------------------------------------------------------
* STEP 7: Register an application-defined compression method and use it
* to write and then read the dataset.
*----------------------------------------------------------------------
*/
TESTING("compression (app-defined method)");
// BMR: not sure how to handle this yet
if (H5Zregister (H5Z_BOGUS, DSET_BOGUS_NAME, bogus)<0) goto error;
if (H5Pset_filter (dscreatplist.getId(), H5Z_BOGUS, 0, 0, NULL)<0) goto error;
dscreatplist.setFilter (H5Z_BOGUS, 0, 0, NULL);
delete dataset;
DataSpace space2 (2, size, NULL);
dataset = new DataSet (file.createDataSet (DSET_BOGUS_NAME, PredType::NATIVE_INT, space2, dscreatplist));
dataset->write ((void*)points, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
dataset->read ((void*)check, PredType::NATIVE_INT, DataSpace::ALL, DataSpace::ALL, xfer);
/* Check that the values read are the same as the values written */
for (i = 0; i < size[0]; i++)
for (j = 0; j < size[1]; j++)
{
int status = check_values (i, j, points[i][j], check[i][j]);
if (status == -1) goto error;
}
PASSED();
/*----------------------------------------------------------------------
* Cleanup
*----------------------------------------------------------------------
*/
delete dataset;
delete [] tconv_buf;
return 0;
} // end try
// catch all dataset, file, space, and plist exceptions
catch (Exception E) { goto error; }
error:
// cleaning up
if (tconv_buf)
delete [] tconv_buf;
return -1;
}
/*-------------------------------------------------------------------------
* Function: test_multiopen
*
* Purpose: Tests that a bug no longer exists. If a dataset is opened
* twice and one of the handles is used to extend the dataset,
* then the other handle should return the new size when
* queried.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* Saturday, February 17, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_multiopen (H5File& file)
{
TESTING("multi-open with extending");
try {
// Create a dataset creation property list
DSetCreatPropList dcpl;
// Set chunk size to given size
hsize_t cur_size[1] = {10};
dcpl.setChunk (1, cur_size);
// Create a simple data space with unlimited size
static hsize_t max_size[1] = {H5S_UNLIMITED};
DataSpace* space = new DataSpace (1, cur_size, max_size);
// Create first dataset
DataSet dset1 = file.createDataSet ("multiopen", PredType::NATIVE_INT, *space, dcpl);
//BMR: Quincey said Rob gave a tweak to have dataset being the first
// argument in this call but actually shouldn't be valid, so just
// ignore the argument dset1. Just open the first dataset again
// from the file to another DataSet object.
// if ((dset2=H5Dopen (dset1, "."))<0) goto error;
DataSet dset2 = file.openDataSet ("multiopen");
// Relieve the dataspace
delete space;
// Extend the dimensionality of the first dataset
cur_size[0] = 20;
dset1.extend (cur_size);
/* Get the size from the second handle */
//if ((space = H5Dget_space (dset2))<0) goto error;
space = new DataSpace (dset2.getSpace());
hsize_t tmp_size[1];
//if (H5Sget_simple_extent_dims (space, tmp_size, NULL)<0) goto error;
space->getSimpleExtentDims (tmp_size);
if (cur_size[0]!=tmp_size[0])
{
H5_FAILED();
cout << " Got " << (int)tmp_size[0] << " instead of "
<< (int)cur_size[0] << "!" << endl;
delete space;
goto error;
}
delete space;
PASSED();
return 0;
} // end try block
// catch all dataset, file, space, and plist exceptions
catch (Exception E)
{ goto error; }
error:
return -1;
}
/*-------------------------------------------------------------------------
* Function: test_types
*
* Purpose: Make some datasets with various types so we can test h5ls.
*
* Return: Success: 0
*
* Failure: -1
*
* Programmer: Binh-Minh Ribler (using C version)
* February 17, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static herr_t
test_types(H5File& file)
{
size_t i;
TESTING("various datatypes");
try {
// Create a group in the file that was passed in from the caller
Group grp = file.createGroup ("typetests");
/* bitfield_1 */
unsigned char buf[32];
hsize_t nelmts = sizeof(buf);
DataType type;
try { // block of bitfield_1
// test copying a predefined type
type.copy (PredType::STD_B8LE);
// Test copying a user-defined type using DataType::copy
DataType copied_type;
copied_type.copy(type);
// Test copying a user-defined type using DataType::operator=
DataType another_copied_type;
another_copied_type = type;
// Test copying a user-defined int type using DataType::operator=
IntType orig_int(PredType::STD_B8LE);
DataType generic_type;
generic_type = orig_int;
// Test copying an integer predefined type
IntType new_int_type(PredType::STD_B8LE);
// Test copying an int predefined type using DataType::operator=
IntType another_int_type;
another_int_type = new_int_type;
DataSpace space (1, &nelmts);
DataSet* dset = new DataSet(grp.createDataSet("bitfield_1", type, space));
// Fill buffer
for (i=0; i<sizeof buf; i++)
buf[i] = (unsigned char)0xff ^ (unsigned char)i;
// Write data from buf using all default dataspaces and property
// list; if writing fails, deallocate dset and return.
try { dset->write (buf, type); }
catch(DataSetIException E)
{
delete dset;
goto error;
}
delete dset;
} // end try block of bitfield_1
// catch exceptions thrown in try block of bitfield_1
catch (Exception E) {
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
/* bitfield_2 */
nelmts = sizeof(buf)/2;
try { // bitfield_2 block
type.copy (PredType::STD_B16LE);
DataSpace space (1, &nelmts);
DataSet* dset = new DataSet(grp.createDataSet("bitfield_2", type, space));
// Fill buffer
for (i=0; i<sizeof(buf); i++)
buf[i] = (unsigned char)0xff ^ (unsigned char)i;
// Write data from buf using all default dataspaces and property
// list; if writing fails, deallocate dset and return.
try { dset->write (buf, type); }
catch(DataSetIException E)
{
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
delete dset;
goto error;
}
delete dset;
} // end try block of bitfield_2
// catch exceptions thrown in try block of bitfield_2
catch (Exception E) {
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
/* opaque_1 */
DataType* optype = new DataType(H5T_OPAQUE, 1);
try { // opaque_1 block
nelmts = sizeof(buf);
DataSpace space (1, &nelmts);
optype->setTag ("testing 1-byte opaque type");
DataSet* dset = new DataSet(grp.createDataSet("opaque_1", *optype, space));
// Fill buffer
for (i=0; i<sizeof buf; i++)
buf[i] = (unsigned char)0xff ^ (unsigned char)i;
// Write data from buf using all default dataspaces and property
// list; if writing fails, deallocate dset and return.
try { dset->write (buf, *optype); }
catch(DataSetIException E)
{
delete dset;
goto error;
}
delete dset;
delete optype;
} // end try block of opaque_1
// catch exceptions thrown in try block of opaque_1
catch (DataSetIException E) {
delete optype;
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
catch (Exception E) {
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
/* opaque_2 */
try { // block opaque_2
nelmts = sizeof(buf)/4;
DataSpace space (1, &nelmts);
optype = new DataType(H5T_OPAQUE, 4);
optype->setTag ("testing 4-byte opaque type");
DataSet* dset = new DataSet(grp.createDataSet("opaque_2", *optype, space));
// Fill buffer
for (i=0; i<sizeof(buf); i++)
buf[i] = (unsigned char)0xff ^ (unsigned char)i;
// Write data from buf using all default dataspaces and property
// list; if writing fails, deallocate dset and return.
try { dset->write (buf, *optype); }
catch(DataSetIException E)
{
delete dset;
goto error;
}
delete dset;
delete optype;
} //end try block of opaque_2
catch (DataSetIException E) {
delete optype;
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
catch (Exception E) {
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
PASSED();
return 0;
} // end top try block
catch (Exception E) { // Group and DataType exceptions
cout << "Failure in " << E.getFuncName() << " - "
<< E.getDetailMsg() << endl;
goto error;
}
error:
return -1;
}
/*-------------------------------------------------------------------------
* Function: test_report
*
* Purpose: Prints out the number of errors for dataset tests if there
* were any failures occurred. If no failure, test_report
* prints out the "All dataset tests passed" message
*
* Return: if any failure has occurred: 1
*
* if no failure occurs: 0
*
* Programmer: Binh-Minh Ribler (using C code segment for reporting tests)
* Friday, February 6, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
int test_report( int nerrors )
{
if (nerrors)
{
nerrors = MAX(1, nerrors);
if (1 == nerrors)
cout << "***** " << nerrors << " DATASET TEST"
<< " FAILED! *****" << endl;
else
cout << "***** " << nerrors << " DATASET TESTS"
<< " FAILED! *****" << endl;
return 1;
}
else
{
cout << "All dataset tests passed." << endl;
return 0;
}
}
/*-------------------------------------------------------------------------
* Function: main
*
* Purpose: Tests the dataset interface (H5D)
*
* Return: Success: exit(0)
*
* Failure: exit(1)
*
* Programmer: Binh-Minh Ribler (using C version)
* Friday, January 5, 2001
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
static hid_t tempid = H5T_STD_B8LE;
// the following statement caused seg. fault, need to check into - BMR
//static PredType temp = PredType::STD_B8LE;
// because PredType::STD_B8LE was not defined yet, so ref_counter is nil
// and it'll fail when the ref_counter is being incremented
// this can be a problem; must check ref_counter before increment or decrement
int
main(void)
{
h5_reset(); // in h5test.c, resets the library by closing it
hid_t fapl_id;
fapl_id = h5_fileaccess(); // in h5test.c, returns a file access template
#if 0
/* BMR: leave paralell stuff out! */
{
// Turn off raw data cache
int mdc_nelmts;
(H5Pget_cache(fapl_id, &mdc_nelmts, NULL, NULL, NULL)<0) goto error;
(H5Pset_cache(fapl_id, mdc_nelmts, 0, 0, 0.0)<0) goto error;
}
#endif
char filename[1024];
h5_fixname(FILENAME[0], fapl_id, filename, sizeof filename);
int nerrors=0; // keep track of number of failures occurr
try
{
// Turn of the auto-printing when failure occurs so that we can
// handle the errors appropriately since sometime failures are
// caused deliberately and expected.
Exception::dontPrint();
// Use the file access template id to create a file access prop.
// list object to pass in H5File::H5File
FileAccPropList* fapl = new FileAccPropList(fapl_id);
H5File file( filename, H5F_ACC_TRUNC, FileCreatPropList::DEFAULT, *fapl );
/* Cause the library to emit initial messages */
Group grp = file.createGroup( "emit diagnostics", 0);
grp.setComment( ".", "Causes diagnostic messages to be emitted");
nerrors += test_create(file)<0 ?1:0;
nerrors += test_simple_io(file)<0 ?1:0;
nerrors += test_tconv(file)<0 ?1:0;
nerrors += test_compression(file)<0 ?1:0;
nerrors += test_multiopen (file)<0 ?1:0;
nerrors += test_types(file)<0 ?1:0;
// BMR: this is a very unattractive workaround approach, but I used
// it for now, since I want to use already existing utility code from
// the C tests. h5_cleanup calls H5Pclose to close the fapl_id while
// the object 'fapl' is still opened. So, I dynamically allocated
// 'fapl' then deleted it before h5_cleanup, but after incrementing
// 'fapl's ref counter so that the prop list id will not be closed
// when 'fapl' is destroyed. Why did I need to create a
// FileCreatPropList object while I already had the prop list id?
// Because I need to pass it into the constructor H5File::H5File.
// We're trying to avoid introducing id's into the C++ API.
fapl->incRefCount();
delete fapl;
h5_cleanup(FILENAME, fapl_id);
return( test_report( nerrors ));
}
catch (Exception E)
{
return( test_report( nerrors ));
}
}
|