File: tst_converts2.c

package info (click to toggle)
netcdf 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • 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 (226 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (7)
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
/* This is part of the netCDF package.
   Copyright 2018 University Corporation for Atmospheric Research/Unidata
   See COPYRIGHT file for conditions of use.

   Test even more data conversions.

   $Id: tst_converts2.c,v 1.14 2010/06/01 15:34:51 ed Exp $
*/

#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"
#include <math.h>

#define FILE_NAME "tst_converts2.nc"
#define VAR_NAME "Monkey"
#define DIM_NAME "n"
#define DIM_LEN 5

int
main(int argc, char **argv)
{
   int ncid, varid;
   signed char schar_in, schar = -2;
   char var_name[NC_MAX_NAME+1];
   int ndims, natts, int_in;
   long long_in;
   nc_type var_type;

   printf("\n*** Testing more netcdf-4 data conversion.\n");
   printf ("*** Testing NC_BYTE conversions...");
   {
      /* Write a scalar NC_BYTE with value -2. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_BYTE, 0, NULL, &varid)) ERR;
      if (nc_put_var_schar(ncid, varid, &schar)) ERR;
      if (nc_close(ncid)) ERR;

      /* Now open the file and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, NULL, &natts)) ERR;
      if (strcmp(var_name, VAR_NAME) || natts !=0 || ndims != 0 ||
	  var_type != NC_BYTE) ERR;
      if (nc_get_var_schar(ncid, varid, &schar_in)) ERR;
      if (schar_in != schar) ERR;
      if (nc_get_var_int(ncid, varid, &int_in)) ERR;
      if (int_in != schar) ERR;
      if (nc_get_var_long(ncid, varid, &long_in)) ERR;
      if (long_in != schar) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf ("*** Testing NC_USHORT conversions...");
   {
      /* Write a scalar NC_USHORT, converted from various types, testing a bug fix. */
       unsigned short usval = 65535;
       int ival = 65535;
       long lval = 65535;
       float fval = 65535;
       double dval = 65535;
       int dimid;
       size_t coord[1];
       unsigned short ushort_in;
       int int_in;
       long long_in;
       float float_in;
       double double_in;

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_USHORT, 1, &dimid, &varid)) ERR;
      coord[0] = 0;
      if (nc_put_var1_ushort(ncid, varid, coord, &usval)) ERR;
      coord[0] = 1;
      if (nc_put_var1_int(ncid, varid, coord, &ival)) ERR;
      coord[0] = 2;
      if (nc_put_var1_float(ncid, varid, coord, &fval)) ERR;
      coord[0] = 3;
      if (nc_put_var1_double(ncid, varid, coord, &dval)) ERR;
      coord[0] = 4;
      if (nc_put_var1_long(ncid, varid, coord, &lval)) ERR;

      if (nc_close(ncid)) ERR;

      /* Now open the file and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, NULL, &natts)) ERR;
      if (strcmp(var_name, VAR_NAME) || natts !=0 || ndims != 1 ||
	  var_type != NC_USHORT) ERR;
      coord[0] = 0;
      if (nc_get_var1_ushort(ncid, varid, coord, &ushort_in)) ERR;
      if (ushort_in != usval) ERR;
      coord[0] = 1;
      if (nc_get_var1_int(ncid, varid, coord, &int_in)) ERR;
      if (int_in != ival) ERR;
      coord[0] = 2;
      if (nc_get_var1_float(ncid, varid, coord, &float_in)) ERR;
      if (float_in != fval) ERR;
      coord[0] = 3;
      if (nc_get_var1_double(ncid, varid, coord, &double_in)) ERR;
      if (double_in != dval) ERR;
      coord[0] = 4;
      if (nc_get_var1_long(ncid, varid, coord, &long_in)) ERR;
      if (long_in != lval) ERR;

      /* This should fail. */
      coord[0] = 5;
      if (nc_get_var1_ushort(ncid, varid, coord,
			     &ushort_in) != NC_EINVALCOORDS) ERR;

      if (nc_close(ncid)) ERR;
   }
   {
      /* Write a scalar NC_USHORT that's not default fill value,
       * converted from various types. */
       unsigned short usval = 65534;
       int ival = 65534;
       long lval = 65534;
       float fval = 65534;
       double dval = 65534;
       int dimid;
       size_t coord[1];
       unsigned short ushort_in;
       int int_in;
       long long_in;
       float float_in;
       double double_in;

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_USHORT, 1, &dimid, &varid)) ERR;
      coord[0] = 0;
      if (nc_put_var1_ushort(ncid, varid, coord, &usval)) ERR;
      coord[0] = 1;
      if (nc_put_var1_int(ncid, varid, coord, &ival)) ERR;
      coord[0] = 2;
      if (nc_put_var1_float(ncid, varid, coord, &fval)) ERR;
      coord[0] = 3;
      if (nc_put_var1_double(ncid, varid, coord, &dval)) ERR;
      coord[0] = 4;
      if (nc_put_var1_long(ncid, varid, coord, &lval)) ERR;

      if (nc_close(ncid)) ERR;

      /* Now open the file and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, NULL, &natts)) ERR;
      if (strcmp(var_name, VAR_NAME) || natts !=0 || ndims != 1 ||
	  var_type != NC_USHORT) ERR;
      coord[0] = 0;
      if (nc_get_var1_ushort(ncid, varid, coord, &ushort_in)) ERR;
      if (ushort_in != usval) ERR;
      coord[0] = 1;
      if (nc_get_var1_int(ncid, varid, coord, &int_in)) ERR;
      if (int_in != ival) ERR;
      coord[0] = 2;
      if (nc_get_var1_float(ncid, varid, coord, &float_in)) ERR;
      if (float_in != fval) ERR;
      coord[0] = 3;
      if (nc_get_var1_double(ncid, varid, coord, &double_in)) ERR;
      if (double_in != dval) ERR;
      coord[0] = 4;
      if (nc_get_var1_long(ncid, varid, coord, &long_in)) ERR;
      if (long_in != lval) ERR;

      /* This should fail. */
      coord[0] = 5;
      if (nc_get_var1_ushort(ncid, varid, coord,
			     &ushort_in) != NC_EINVALCOORDS) ERR;

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf ("*** Testing MAX_INT conversions...");
   {
#define X_INT_MAX	2147483647 /* from libsrc4/nc4internal.h */
      int ivalue = X_INT_MAX, ivalue_in;
      unsigned char uchar_in;
      unsigned int uivalue_in;
      short svalue_in;
      unsigned short usvalue_in;
      long long int64_in;
      unsigned long long uint64_in;
      float float_in;
      double double_in;

      /* Write a scalar NC_INT with value X_MAX_INT. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_var(ncid, VAR_NAME, NC_INT, 0, NULL, &varid)) ERR;
      if (nc_put_var_int(ncid, varid, &ivalue)) ERR;
      if (nc_close(ncid)) ERR;

      /* Now open the file and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_var(ncid, 0, var_name, &var_type, &ndims, NULL, &natts)) ERR;
      if (strcmp(var_name, VAR_NAME) || natts !=0 || ndims != 0 ||
	  var_type != NC_INT) ERR;
      if (nc_get_var_schar(ncid, varid, &schar_in) != NC_ERANGE) ERR;
      if (schar_in != (signed char)ivalue) ERR;
      if (nc_get_var_uchar(ncid, varid, &uchar_in) != NC_ERANGE) ERR;
      if (uchar_in != (unsigned char)ivalue) ERR;
      if (nc_get_var_short(ncid, varid, &svalue_in) != NC_ERANGE) ERR;
      if (svalue_in != (short)ivalue) ERR;
      if (nc_get_var_ushort(ncid, varid, &usvalue_in) != NC_ERANGE) ERR;
      if (usvalue_in != (unsigned short)ivalue) ERR;
      if (nc_get_var_int(ncid, varid, &ivalue_in)) ERR;
      if (ivalue_in != ivalue) ERR;
      if (nc_get_var_uint(ncid, varid, &uivalue_in)) ERR;
      if (uivalue_in != (unsigned int)ivalue) ERR;
      if (nc_get_var_long(ncid, varid, &long_in)) ERR;
      if (long_in != ivalue) ERR;
      if (nc_get_var_longlong(ncid, varid, &int64_in)) ERR;
      if (int64_in != ivalue) ERR;
      if (nc_get_var_ulonglong(ncid, varid, &uint64_in)) ERR;
      if (uint64_in != ivalue) ERR;
      if (nc_get_var_float(ncid, varid, &float_in)) ERR;

      if(fabs( (float_in-X_INT_MAX) - ((float)ivalue - X_INT_MAX)) > 1) ERR;

      if (nc_get_var_double(ncid, varid, &double_in)) ERR;
      if (double_in != (double)ivalue) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}