summaryrefslogtreecommitdiffstats
path: root/test/th5d.c
blob: 39e0f3549a20db2bf3b32f3c015f5b4fbc13e8d2 (plain)
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
/****************************************************************************
 * NCSA HDF                                                                 *
 * Software Development Group                                               *
 * National Center for Supercomputing Applications                          *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf/COPYING file.                                                        *
 *                                                                          *
 ****************************************************************************/

#ifdef RCSID
static char RcsId[] = "$Revision$";
#endif

/* $Id$ */

/***********************************************************
*
* Test program:  th5d
*
* Test the basic dataset functionality
*
*************************************************************/

#include <testhdf5.h>

#include <H5private.h>
#include <H5Bprivate.h>
#include <H5Mprivate.h>
#include <H5Dprivate.h>

#define FILE   "th5d1.h5"

/* 3-D dataset with fixed dimensions */
#define SPACE1_NAME  "Space1"
#define SPACE1_RANK     3
#define SPACE1_DIM1     3
#define SPACE1_DIM2     15
#define SPACE1_DIM3     13

/* 32-bit big-endian integer type */
#define TYPE1_NAME  "Type1"
#define TYPE1_BASE  H5T_INT
#define TYPE1_LEN   4
#define TYPE1_ARCH  H5T_BIGENDIAN

/* 3-d 32-bit integer dataset */
#define DATA1_NAME  "Data1"
int32 data1[SPACE1_DIM1][SPACE1_DIM2][SPACE1_DIM3];

/* 4-D dataset with fixed dimensions */
#define SPACE2_NAME  "Space2"
#define SPACE2_RANK     4
#define SPACE2_DIM1     3
#define SPACE2_DIM2     15
#define SPACE2_DIM3     13
#define SPACE2_DIM4     7 

/* 64-bit big-endian floating-point type */
#define TYPE2_NAME  "Type2"
#define TYPE2_BASE  H5T_FLOAT
#define TYPE2_LEN   8
#define TYPE2_ARCH  H5T_BIGENDIAN

/* 4-d 64-bit floating-point dataset */
#define DATA2_NAME  "Data2"
float64 data2[SPACE2_DIM1][SPACE2_DIM2][SPACE2_DIM3][SPACE2_DIM4];

/****************************************************************
**
**  init_data(): Initialize data arrays.
** 
****************************************************************/
static void init_data(void)
{
    int32 *ip;
    float64 *fp;
    uintn u;

    for(ip=(int32 *)data1, u=0; u<(SPACE1_DIM1*SPACE1_DIM2*SPACE1_DIM3); u++)
        *ip++=(int32)u*2;

    for(fp=(float64 *)data2, u=0; u<(SPACE2_DIM1*SPACE2_DIM2*SPACE2_DIM3*SPACE2_DIM4); u++)
        *fp++=((float64)u/4.0);
}

/****************************************************************
**
**  test_h5d_basic_write(): Test basic H5D (dataset) writing code.
** 
****************************************************************/
static void test_h5d_basic_write(void)
{
    hid_t fid1;   /* HDF5 File IDs */
    hid_t did1;   /* Dataset ID */
    hid_t tid1;   /* Datatype ID */
    hid_t sid1;   /* Dataspace ID */
    uint32 dims1[]={SPACE1_DIM1,SPACE1_DIM2,SPACE1_DIM3};   /* dataspace dim sizes */
    hid_t did2;   /* Dataset ID */
    hid_t tid2;   /* Datatype ID */
    hid_t sid2;   /* Dataspace ID */
    uint32 dims2[]={SPACE2_DIM1,SPACE2_DIM2,SPACE2_DIM3,SPACE2_DIM4};   /* dataspace dim sizes */
    herr_t ret;         /* Generic return value */

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Datatype Manipulation\n"));

    /* Create file */
    fid1=H5Fcreate(FILE,H5ACC_OVERWRITE,0,0);
    CHECK(fid1,FAIL,"H5Fcreate");

    sid1=H5Mcreate(fid1,H5_DATASPACE,SPACE1_NAME);
    CHECK(sid1,FAIL,"H5Mcreate");

    ret=H5Pset_space(sid1,SPACE1_RANK,dims1);
    CHECK(ret,FAIL,"H5Pset_space");
    
    tid1=H5Mcreate(fid1,H5_DATATYPE,TYPE1_NAME);
    CHECK(tid1,FAIL,"H5Mcreate");
    
    ret=H5Tset_type(tid1,TYPE1_BASE,TYPE1_LEN,TYPE1_ARCH);
    CHECK(ret,FAIL,"H5Tset_type");
    
    did1=H5Mcreate(fid1,H5_DATASET,DATA1_NAME);
    CHECK(did1,FAIL,"H5Mcreate");

    ret=H5Dset_info(did1,tid1,sid1);
    CHECK(ret,FAIL,"H5Dset_info");

    ret=H5Dwrite(did1,H5P_SCALAR,data1);
    CHECK(ret,FAIL,"H5Dwrite");
    
    ret=H5Mrelease(did1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(tid1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    /* Write second dataset out */
    sid2=H5Mcreate(fid1,H5_DATASPACE,SPACE2_NAME);
    CHECK(sid1,FAIL,"H5Mcreate");

    ret=H5Pset_space(sid2,SPACE2_RANK,dims2);
    CHECK(ret,FAIL,"H5Pset_space");
    
    tid2=H5Mcreate(fid1,H5_DATATYPE,TYPE2_NAME);
    CHECK(tid2,FAIL,"H5Mcreate");
    
    ret=H5Tset_type(tid2,TYPE2_BASE,TYPE2_LEN,TYPE2_ARCH);
    CHECK(ret,FAIL,"H5Tset_type");
    
    did2=H5Mcreate(fid1,H5_DATASET,DATA2_NAME);
    CHECK(sid2,FAIL,"H5Mcreate");

    ret=H5Dset_info(did2,tid2,sid2);
    CHECK(ret,FAIL,"H5Dset_info");

    ret=H5Dwrite(did2,H5P_SCALAR,data2);
    CHECK(ret,FAIL,"H5Dwrite");
    
    ret=H5Mrelease(did2);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(tid2);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(sid2);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(sid1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    /* Close first file */
    ret=H5Fclose(fid1);
    CHECK(ret,FAIL,"H5Fclose");
}   /* test_h5d_basic_write() */

/****************************************************************
**
**  test_h5d_basic_read(): Test basic H5D (dataset) reading code.
** 
****************************************************************/
static void test_h5d_basic_read(void)
{
    hid_t fid1;   /* HDF5 File IDs */
#if 0
    hid_t oid1;   /* Dataset OID */
#endif
    hid_t did1;   /* Dataset ID */
    hid_t tid1;   /* Datatype ID */
    hid_t sid1;   /* Dataspace ID */
    void *buf;      /* space for the buffer read */
    uint32 buf_size;    /* size of the buffer to read */
    hid_t type;   /* Datatype's base type */
    uint8 len, arch;    /* Datatype's length and architecture */
    uintn n;            /* number of dataspace elements */
    herr_t ret;         /* Generic return value */

    /* Output message about test being performed */
    MESSAGE(5, ("Testing Datatype Manipulation\n"));

    /* Create file */
    fid1=H5Fopen(FILE,0,0);
    CHECK(fid1,FAIL,"H5Fopen");

    did1 = H5Mfind_name (fid1, H5_DATASET,  DATA1_NAME);
    CHECK (did1, FAIL, "H5Mfind_name");

    ret=H5Dget_info(did1,&tid1,&sid1);
    CHECK(ret,FAIL,"H5Pget_info");
    
    ret=H5Tis_atomic(tid1);
    VERIFY(ret,BTRUE,"H5Tis_atomic");
    
    ret=H5Tget_type(tid1,&type,&len,&arch);
    CHECK(ret,FAIL,"H5Tget_type");
    VERIFY(type,TYPE1_BASE,"H5Tget_type");
    VERIFY(len,TYPE1_LEN,"H5Tget_type");
    VERIFY(arch,TYPE1_ARCH,"H5Tget_type");

    n=H5Pnelem(sid1);
    CHECK(n,UFAIL,"H5Pnelem");
    VERIFY(n,SPACE1_DIM1*SPACE1_DIM2*SPACE1_DIM3,"H5Pnelem");
    
    buf_size=H5Tsize(tid1,len,arch,BTRUE)*n;
    buf=HDmalloc(buf_size);
    ret=H5Dread(did1,H5P_SCALAR,buf);
    CHECK(ret,FAIL,"H5Dread");
    VERIFY(HDmemcmp(buf,data1,buf_size),0,"H5Dread");
    HDfree(buf);
    
    ret=H5Mrelease(tid1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(sid1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    ret=H5Mrelease(did1);
    CHECK(ret,FAIL,"H5Mrelease");
    
    /* Close first file */
    ret=H5Fclose(fid1);
    CHECK(ret,FAIL,"H5Fclose");
}   /* test_h5d_basic_read() */


/****************************************************************
**
**  test_h5d(): Main H5D (dataset) testing routine.
** 
****************************************************************/
void test_h5d(void)
{
    /* Output message about test being performed */
    MESSAGE(5, ("Testing datasets\n"));

    init_data();                 /* initialize data arrays */
    test_h5d_basic_write();      /* Test basic H5D writing code */
    test_h5d_basic_read();       /* Test basic H5D reading code */
}   /* test_h5d() */