File: tst_h_wrt_cmp.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 (139 lines) | stat: -rw-r--r-- 4,471 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
/* This is part of the netCDF package.
   Copyright 2018 University Corporation for Atmospheric Research/Unidata
   See COPYRIGHT file for conditions of use.

   Test HDF5 compound types.
   Ed Hartnett
*/

#include "h5_err_macros.h"
#include <hdf5.h>

#define FILE_NAME "tst_h_wrt_cmp.h5"
#define DIM1_LEN 3
#define COMPOUND_NAME "cmp"
#define VAR_NAME "var"

int
main()
{
   hid_t fileid, access_plist, spaceid, typeid;
   hid_t datasetid;
   hsize_t dims[1];
   char dummy[] = "                                 ";
   struct s1
   {
      unsigned char c1;
      double d;
   } data[DIM1_LEN];
   int i;

   /* Initialize our data space. This is required to keep valgrind
    * happy. It makes no difference to the execution of the
    * program. */
   for (i = 0; i < DIM1_LEN; i++)
      memcpy((void *)(&data[i]), (void *)dummy, sizeof(struct s1));

   /* Now init our values. */
   for (i = 0; i < DIM1_LEN; i++)
   {
      data[i].c1 = 126;
      data[i].d = -9999999;
   }

   printf("\n*** Checking HDF5 compound types (even more so).\n");
   printf("*** Checking packing of HDF5 compound types...");
   {
      /* Open file and create group. */
      if ((access_plist = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
      if (H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG)) ERR;
      if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT,
                              access_plist)) < 0) ERR;

      /* Create a simple compound type. */
      if ((typeid = H5Tcreate(H5T_COMPOUND, sizeof(struct s1))) < 0) ERR;
      if (H5Tinsert(typeid, "c1", HOFFSET(struct s1, c1), H5T_NATIVE_UCHAR) < 0) ERR;
      if (H5Tinsert(typeid, "d", HOFFSET(struct s1, d), H5T_NATIVE_DOUBLE) < 0) ERR;
      if (H5Tcommit(fileid, COMPOUND_NAME, typeid) < 0) ERR;

      /* Create a space. */
      dims[0] = DIM1_LEN;
      if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR;

      /* Create a dataset of this compound type. */
      if ((datasetid = H5Dcreate(fileid, VAR_NAME, typeid, spaceid,
                                 H5P_DEFAULT)) < 0) ERR;

      /* Write some data. */
      if (H5Dwrite(datasetid, typeid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                   data) < 0) ERR;

      /* Release all resources. */
      if (H5Pclose(access_plist) < 0) ERR;
      if (H5Tclose(typeid) < 0) ERR;
      if (H5Sclose(spaceid) < 0) ERR;
      if (H5Fclose(fileid) < 0) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** Checking packing of HDF5 compound types...");
   {
      /* Open file. */
      if ((access_plist = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
      if (H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG)) ERR;
      if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDONLY, access_plist)) < 0) ERR;

      /* Open dataset. */
      if ((datasetid = H5Dopen1(fileid, VAR_NAME)) < 0) ERR;

      /* Check space. */
      if ((spaceid = H5Dget_space(datasetid)) < 0) ERR;
      if (H5Sget_simple_extent_ndims(spaceid) != 1) ERR;
      if (H5Sget_simple_extent_npoints(spaceid) != DIM1_LEN) ERR;

      /* Get type. */
      if ((typeid = H5Dget_type(datasetid)) < 0) ERR;

      /* Read the data. */
      if (H5Dread(datasetid, typeid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                  data) < 0) ERR;

      /* Check the data. */
      for (i=0; i<DIM1_LEN; i++)
         if (data[i].c1 != 126 || data[i].d != -9999999) ERR;

      /* Release all resources. */
      if (H5Fclose(fileid) < 0) ERR;
   }
   SUMMARIZE_ERR;
   printf("*** Checking packing of HDF5 compound types...");
   {
      /* Open file. */
      if ((access_plist = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
      if (H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG)) ERR;
      if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDONLY, access_plist)) < 0) ERR;

      /* Open dataset. */
      if ((datasetid = H5Dopen1(fileid, VAR_NAME)) < 0) ERR;

      /* Check space. */
      if ((spaceid = H5Dget_space(datasetid)) < 0) ERR;
      if (H5Sget_simple_extent_ndims(spaceid) != 1) ERR;
      if (H5Sget_simple_extent_npoints(spaceid) != DIM1_LEN) ERR;

      /* Get type. */
      if ((typeid = H5Dget_type(datasetid)) < 0) ERR;

      /* Read the data. */
      if (H5Dread(datasetid, typeid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                  data) < 0) ERR;

      /* Check the data. */
      for (i=0; i<DIM1_LEN; i++)
         if (data[i].c1 != 126 || data[i].d != -9999999) ERR;

      /* Release all resources. */
      if (H5Fclose(fileid) < 0) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}