File: tst_h_enums.c

package info (click to toggle)
netcdf 1:4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (294 lines) | stat: -rw-r--r-- 9,662 bytes parent folder | download | duplicates (3)
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
/* This is part of the netCDF package.  Copyright 2018 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   This program excersizes HDF5 variable length array code.

   $Id: tst_h_enums.c,v 1.14 2010/06/01 15:34:51 ed Exp $
*/
#include "h5_err_macros.h"
#include <hdf5.h>
#include "config.h"

#define FILE_NAME "tst_h_enums.h5"
#define DIM1_LEN 12
#define ATT_NAME "Browings_reverse_enumerations"
#define SIZE 9
#define GRP_NAME "Browning"
#define NUM_VALS 12
#define STR_LEN 255

/* This seems like a good sonnet for enumeration:

How do I love thee? Let me count the ways.
I love thee to the depth and breadth and height
My soul can reach, when feeling out of sight
For the ends of Being and ideal Grace.
I love thee to the level of everyday's
Most quiet need, by sun and candle-light.
I love thee freely, as men strive for Right;
I love thee purely, as they turn from Praise.
I love thee with a passion put to use
In my old griefs, and with my childhood's faith.
I love thee with a love I seemed to lose
With my lost saints, --- I love thee with the breath,
Smiles, tears, of all my life! --- and, if God choose,
I shall but love thee better after death.

(I gotta say, that's one dorky poem. - Ed)
*/

int
main()
{
   printf("\n*** Checking HDF5 enum types.\n");
   printf("*** Checking simple HDF5 enum type...");
   {
      hid_t fileid, grpid, spaceid, typeid, attid;
      hsize_t dims[1] = {DIM1_LEN};
      short data[DIM1_LEN];
      short data_in[DIM1_LEN];
      int i;
      short val[NUM_VALS];
      char love_how[NUM_VALS][STR_LEN + 1] = {"Depth", "Breadth",
						  "Height", "Level",
						  "Freely", "Purely",
						  "Passionately", "Lost",
						  "Breath", "Smiles",
						  "Tears", "After Death"};
/*      H5T_class_t type_class;*/
      size_t size;
      int num_members;
      short the_value;
      char *member_name;
      htri_t types_equal;
      hid_t base_hdf_typeid;


      for (i=0; i < NUM_VALS; i++)
	 val[i] = i*2;
      for (i=0; i < DIM1_LEN; i++)
	 data[i] = i*2;

      /* Open file. */
      if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
			      H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR;

      /* Create enum type. */
      /* Both methods do the same thing, but Quincey says to prefer
       * H5Tcreate_enum. */
      /*if ((typeid =  H5Tcreate(H5T_ENUM, sizeof(short))) < 0) ERR;*/
      if ((typeid =  H5Tenum_create(H5T_NATIVE_SHORT)) < 0) ERR;

      /* Insert some values. */
      for (i=0; i<NUM_VALS; i++)
	 if (H5Tenum_insert(typeid, love_how[i], &val[i]) < 0) ERR;

      /* Write an attribute of this type. */
      if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
      if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
			     H5P_DEFAULT)) < 0) ERR;
      if (H5Awrite(attid, typeid, data) < 0) ERR;

      /* Close everything. */
      if (H5Aclose(attid) < 0 ||
	  H5Tclose(typeid) < 0 ||
	  H5Gclose(grpid) < 0 ||
	  H5Fclose(fileid) < 0) ERR;

      /* Reopen the file. */
      if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR,
			    H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR;

      /* Check the attribute's type. */
      if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
      if ((typeid = H5Aget_type(attid)) < 0) ERR;
/*      if ((type_class = H5Tget_class(typeid)) != H5T_ENUM) ERR;*/
      num_members = H5Tget_nmembers(typeid);
      if (num_members != NUM_VALS) ERR;
      size = H5Tget_size(typeid);
      if (size != 2) ERR;
      if ((base_hdf_typeid = H5Tget_super(typeid)) < 0) ERR;
      if ((types_equal = H5Tequal(base_hdf_typeid, H5T_NATIVE_SHORT)) < 0) ERR;
      if (!types_equal) ERR;

      /* Check each value and number in the enum. */
      for (i=0; i < NUM_VALS; i++)
      {
	 if (H5Tget_member_value(typeid, i, &the_value) < 0) ERR;
	 if (the_value != val[i]) ERR;
	 member_name = H5Tget_member_name(typeid, i);
	 if (strcmp(member_name, love_how[i])) ERR;
#ifdef HAVE_H5FREE_MEMORY
	 H5free_memory(member_name);
#else
     free(member_name);
#endif
      }

      /* Now read the data in the attribute and make sure it's what we
       * expect it to be. */
      if (H5Aread(attid, typeid, data_in) < 0) ERR;
      for (i=0; i < DIM1_LEN; i++)
	 if (data_in[i] != data[i]) ERR;

      /* Close it all again. */
      if (H5Aclose(attid) < 0 ||
	  H5Tclose(typeid) < 0 ||
	  H5Gclose(grpid) < 0 ||
	  H5Fclose(fileid) < 0) ERR;
   }
   SUMMARIZE_ERR;

   printf("*** Checking HDF5 enum type missing values...");
   {
#define NUM_LANG 4
#define VAR_LANG_NAME "Programming_Language"
#define FV_NAME "_FillValue"
#define GRP_NAME2 "NetCDF_Programming"

      hid_t datasetid, mem_spaceid, fileid, grpid, spaceid, typeid, plistid;
      hid_t file_spaceid, attid, att_spaceid;
      hsize_t dims[1] = {DIM1_LEN};
      short data_in[DIM1_LEN];
      int i;
      short val[NUM_LANG];
      char lang[NUM_LANG][STR_LEN + 1] = {"C", "Fortran", "C++", "MISSING"};
      enum langs {CLANG=0, Fortran=1, CPP=2, MISSING=255};
      short the_value, fill_value = MISSING, data_point = CLANG;
      hsize_t start[1] = {1}, count[1] = {1};
      int num_members;
      size_t size;
      hid_t base_hdf_typeid;
/*      H5T_class_t type_class;*/
      char *member_name;
      htri_t types_equal;

      val[0] = CLANG;
      val[1] = Fortran;
      val[2] = CPP;
      val[3] = MISSING;

      /* Open file. */
      if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
			      H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gcreate(fileid, GRP_NAME2, 0)) < 0) ERR;

      /* Create enum type. */
      if ((typeid =  H5Tenum_create(H5T_NATIVE_SHORT)) < 0) ERR;

      /* Insert some values. */
      for (i=0; i<NUM_LANG; i++)
	 if (H5Tenum_insert(typeid, lang[i], &val[i]) < 0) ERR;

      /* Create a dataset of this enum type, with fill value. */
      if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
      if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
      if (H5Pset_fill_value(plistid, typeid, &fill_value) < 0) ERR;
      if ((datasetid = H5Dcreate(grpid, VAR_LANG_NAME, typeid,
				 spaceid, plistid)) < 0) ERR;

      /* Create a netCDFstyle _FillValue attribute, though it will be
       * ignored by HDF5. */
      if ((att_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
      if ((attid = H5Acreate(grpid, FV_NAME, typeid, att_spaceid,
			     H5P_DEFAULT)) < 0) ERR;
      if (H5Awrite(attid, typeid, &fill_value) < 0) ERR;

      /* Write one value, the rest will end up set to MISSING. */
      if ((mem_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
      if ((file_spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR;
      if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
			      start, NULL, count, NULL) < 0) ERR;
      if (H5Dwrite(datasetid, typeid, mem_spaceid, file_spaceid,
		   H5P_DEFAULT, &data_point) < 0) ERR;

      /* Close everything. */
      if (H5Sclose(spaceid) < 0 ||
	  H5Sclose(mem_spaceid) < 0 ||
	  H5Aclose(attid) < 0 ||
	  H5Sclose(file_spaceid) < 0 ||
	  H5Dclose(datasetid) < 0 ||
	  H5Pclose(plistid) < 0 ||
	  H5Tclose(typeid) < 0 ||
	  H5Gclose(grpid) < 0 ||
	  H5Fclose(fileid) < 0) ERR;

      /* Reopen the file. */
      if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
      if ((grpid = H5Gopen(fileid, GRP_NAME2)) < 0) ERR;

      /* Check the variable's type. */
      if ((datasetid = H5Dopen1(grpid, VAR_LANG_NAME)) < 0) ERR;
      if ((typeid = H5Dget_type(datasetid)) < 0) ERR;
/*      if ((type_class = H5Tget_class(typeid)) != H5T_ENUM) ERR;*/
      num_members = H5Tget_nmembers(typeid);
      if (num_members != NUM_LANG) ERR;
      size = H5Tget_size(typeid);
      if (size != sizeof(short)) ERR;
      if ((base_hdf_typeid = H5Tget_super(typeid)) < 0) ERR;
      if ((types_equal = H5Tequal(base_hdf_typeid, H5T_NATIVE_SHORT)) < 0) ERR;
      if (!types_equal) ERR;

      /* Check each value and number in the enum. */
      for (i=0; i < NUM_LANG; i++)
      {
	 if (H5Tget_member_value(typeid, i, &the_value) < 0) ERR;
	 if (the_value != val[i]) ERR;
	 member_name = H5Tget_member_name(typeid, i);
	 if (strcmp(member_name, lang[i])) ERR;
#ifdef HAVE_H5FREE_MEMORY
	 H5free_memory(member_name);
#else
     free(member_name);
#endif
      }

      /* Now read the data in the dataset and make sure it's what we
       * expect it to be. */
      if (H5Dread(datasetid, typeid, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_in) < 0) ERR;
      for (i=0; i < DIM1_LEN; i++)
	 if (i == 1)
	 {
	    if (data_in[i] != data_point) ERR;
	 }
	 else
	    if (data_in[i] != MISSING) ERR;

      /* Close it all again. */
      if (H5Dclose(datasetid) < 0 ||
	  H5Tclose(typeid) < 0 ||
	  H5Gclose(grpid) < 0 ||
	  H5Fclose(fileid) < 0) ERR;
   }
   SUMMARIZE_ERR;
/*    printf("*** Checking HDF5 enum interuptus..."); */
/*    { */
/* #define GRP_NAME3 "STOP!" */

/*       hid_t fileid, grpid,  typeid; */

/*       /\* Open file. *\/ */
/*       if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,  */
/* 			      H5P_DEFAULT)) < 0) ERR; */
/*       if ((grpid = H5Gcreate(fileid, GRP_NAME3, 0)) < 0) ERR; */

/*       /\* Create enum type. *\/ */
/*       if ((typeid =  H5Tenum_create(H5T_NATIVE_SHORT)) < 0) ERR; */

/*       /\* Commit the type. This doesn't work, because no members were */
/*        * specified. *\/ */
/*       if (!H5Tcommit(grpid, "name", typeid) < 0) ERR; */

/*       /\* Close everything. *\/ */
/*       if (H5Tclose(typeid) < 0 || */
/* 	  H5Gclose(grpid) < 0 || */
/* 	  H5Fclose(fileid) < 0) ERR;  */

/*    } */
/*    SUMMARIZE_ERR; */

   FINAL_RESULTS;
}