File: tst_h4_lendian.c

package info (click to toggle)
netcdf 1%3A4.4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96,828 kB
  • ctags: 15,369
  • sloc: ansic: 163,650; sh: 9,294; yacc: 2,457; makefile: 1,208; lex: 1,161; xml: 173; f90: 7; fortran: 6; awk: 2
file content (169 lines) | stat: -rw-r--r-- 4,652 bytes parent folder | download
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
/*! Testing for proper read of little-endian variables in an hdf4 file.
 *
 * Added to debug issue NCF-332. Based on code submitted by
 * https://github.com/Unidata/netcdf-c/issues/113.
 */

#include <stdio.h>
#include <config.h>
#include <unistd.h>
#include <nc_tests.h>
#include "err_macros.h"
#include <hdf5.h>
#include <H5DSpublic.h>
#include "mfhdf.h"

#define DIM1 5
#define DIM0 5
#define RANK 2
#define FILENAME "tst_h4_lendian.h4"
#define SDSNAME "data"

int read_hdf_file(int dtype) {

  int ncid = 0;
  int le_int16_varid = 0;
  int retval = 0;
  int ed = 0;

  printf("\to Reading hdf4 file with a little-endian datatype %d\n",dtype);

  printf("\t\to Opening file....\t\t\t\t\t");
  retval = nc_open(FILENAME, NC_NETCDF4 | NC_NOWRITE, &ncid);
  if(retval) {printf("Failure [%d]\n",retval); return retval;}
  else {printf("Success\n");}

  printf("\t\to Getting varid....\t\t\t\t\t");
  retval = nc_inq_varid(ncid,SDSNAME,&le_int16_varid);
  if(retval) {printf("Failure [%d]\n",retval); return retval;}
  else {printf("Success\n");}

  printf("\t\to Querying endianness of the variable....\t\t");
  retval = nc_inq_var_endian(ncid,le_int16_varid,&ed);
  if(retval) {printf("Failure [%d]\n",retval); return retval;}
  else {printf("Success\n");}

  printf("\t\to Checking that endianness is NC_ENDIAN_LITTLE....\t");
  if (ed == NC_ENDIAN_LITTLE) printf("Success\n");
  else {printf("Failure [%d]\n\n",ed); nc_close(ncid); return -1;}

  printf("\t\to Closing file....\t\t\t\t\t");
  retval = nc_close(ncid);
  if(retval) {printf("Failure [%d]\n\n",retval); return retval;}
  else {printf("Success\n\n");}

  return 0;
}

int create_hdf_file(int dtype) {

    int32 sd_id, sds_id, istat, sd_index;
    int32 dims[2], start[2], edges[2], rank;
    int16 array_data[DIM0][DIM1];
    intn i, j, count;

    start[0] = 0;
    start[1] = 0;
    edges[0] = DIM1;
    edges[1] = DIM0;

    /* populate data array */
    count = 0;
    for (j = 0; j < DIM0; j++)
      {
        for (i = 0; i < DIM1; i++)
          array_data[j][i] = count++;
      }

    printf("\to Creating hdf4 file with little-endian datatype %d....\t",dtype);

    sd_id = SDstart(FILENAME, DFACC_CREATE);
    /* sds_id = SDcreate(sd_id, SDSNAME, DFNT_LITEND|dtype, RANK, edges); */
    sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges);

    istat = SDendaccess(sds_id);
    if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;}

    istat = SDend(sd_id);
    if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;}

    sd_id = SDstart(FILENAME, DFACC_WRITE);

    sd_index = 0;
    sds_id = SDselect(sd_id, sd_index);

    istat = SDwritedata(sds_id, start, NULL, edges, (VOIDP)array_data);
    if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;}

    istat = SDendaccess(sds_id);
    if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;}

    istat = SDend(sd_id);
    if(istat) {printf("Failure %d\n", istat); return istat;}

    printf("Success\n");
    return 0;
}


int test_read_write(int dtype) {

  int res = 0;

  res = create_hdf_file(dtype);
  if(res) {unlink(FILENAME); return res;}

  res = read_hdf_file(dtype);

  unlink(FILENAME);
  return res;
}

/*! Standard main function.
 *
 */
int main() {

  int res = 0;

  printf("Test reading from an hdf4 file with a little-endian datatype.\n");

  /* True Positives. */
  res = test_read_write(DFNT_LINT8);
  res = test_read_write(DFNT_LUINT8);
  res = test_read_write(DFNT_LINT16);
  res = test_read_write(DFNT_LUINT16);
  res = test_read_write(DFNT_LINT32);
  res = test_read_write(DFNT_LUINT32);
  res = test_read_write(DFNT_LFLOAT32);
  res = test_read_write(DFNT_LFLOAT64);

  /* True Negatives. */
  printf("\t**** Testing for True Negatives. THESE SHOULD FAIL.****\n\n");
  res = test_read_write(DFNT_INT8);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_UINT8);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_INT16);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_UINT16);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_INT32);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_UINT32);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_FLOAT32);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  res = test_read_write(DFNT_FLOAT64);
  if(!res) {printf("Should have failed. Error!\n"); return -1;}

  printf("Finished.\n");
  return 0;
}