1 /* This is part of the netCDF package.
2    Copyright 2005 University Corporation for Atmospheric Research/Unidata
3     See COPYRIGHT file for conditions of use.
4 
5    Test netcdf-4 variables.
6    Ed Hartnett
7 */
8 
9 #include <nc_tests.h>
10 #include "err_macros.h"
11 #include "netcdf.h"
12 #include "netcdf_f.h"
13 
14 #define FILE_NAME "tst_vars2.nc"
15 #define NUM_DIMS 1
16 #define NUM_VARS 3
17 #define DIM1_LEN NC_UNLIMITED
18 #define DIM1_NAME "Hoplites_Engaged"
19 #define VAR_NAME "Battle_of_Marathon"
20 #define LOSSES_NAME "Miltiades_Losses"
21 #define NDIMS1 1
22 #define MAX_CNUM 4
23 
24 int
main(int argc,char ** argv)25 main(int argc, char **argv)
26 {
27    int ncid, dimids[NUM_DIMS];
28    int varid;
29    int nvars_in, varids_in[NUM_VARS] = {0};
30    signed char fill_value = 42, fill_value_in;
31    nc_type xtype_in;
32    size_t len_in;
33    char name_in[NC_MAX_NAME + 1];
34    int attnum_in;
35    int cnum;
36    char too_long_name[NC_MAX_NAME + 2];
37 
38    /* Set up a name that is too long for netCDF. */
39    memset(too_long_name, 'a', NC_MAX_NAME + 1);
40    too_long_name[NC_MAX_NAME + 1] = 0;
41 
42    printf("\n*** Testing netcdf-4 variable functions, even more.\n");
43    for (cnum = 0; cnum < MAX_CNUM; cnum++)
44    {
45       int cmode = 0;
46 
47       switch(cnum)
48       {
49          case 0:
50             printf("*** Testing with classic format:\n");
51             cmode = 0;
52             break;
53          case 1:
54             printf("*** Testing with 64-bit offset format:\n");
55             cmode = NC_64BIT_OFFSET;
56             break;
57          case 2:
58             printf("*** Testing with HDF5:\n");
59             cmode = NC_NETCDF4|NC_CLOBBER;
60             break;
61          case 3:
62             printf("*** Testing with HDF5, netCDF Classic Model:\n");
63             cmode = NC_CLASSIC_MODEL | NC_NETCDF4;
64 	    break;
65 	 default:
66 	    return 1;
67       }
68 
69 #define FILE_NAME2 "tst_vars2_latefill.nc"
70       printf("**** testing simple fill value attribute creation...");
71       {
72          int schar_data = 0;
73          size_t index[1] = {0};
74          int expected_ret;
75          int dimid;
76 
77          /* Determined the expected result of setting fill value
78           * late. For historical reasons this is allowed for classic
79           * and 64-bit offset formats, but should never be done. */
80          if (cmode == 0 || cmode == NC_64BIT_OFFSET)
81             expected_ret = NC_NOERR;
82          else
83             expected_ret = NC_ELATEFILL;
84 
85          /* Create a netcdf-4 file with one scalar var. Add fill
86           * value. */
87          if (nc_create(FILE_NAME2, cmode, &ncid)) ERR;
88          if (nc_def_dim(ncid, VAR_NAME, TEST_VAL_42, &dimid)) ERR;
89          if (nc_def_var(ncid, VAR_NAME, NC_BYTE, 1, &dimid, &varid)) ERR;
90          if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR;
91          if (nc_enddef(ncid)) ERR;
92          if (nc_put_var1(ncid, varid, index, &schar_data)) ERR;
93          if (nc_redef(ncid)) ERR;
94          if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1,
95                               &fill_value) != expected_ret) ERR;
96          if (nc_close(ncid)) ERR;
97 
98          /* Open the file and check. */
99          if (nc_open(FILE_NAME2, NC_WRITE, &ncid)) ERR;
100          if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
101          if (nvars_in != 1 || varids_in[0] != 0) ERR;
102          if (nc_inq_varname(ncid, 0, name_in)) ERR;
103          if (strcmp(name_in, VAR_NAME)) ERR;
104          if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR;
105          if (xtype_in != NC_BYTE || len_in != 1) ERR;
106          if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR;
107          if (fill_value_in != fill_value) ERR;
108          if (nc_close(ncid)) ERR;
109       }
110 
111       SUMMARIZE_ERR;
112       printf("**** testing simple fill value with data read...");
113       {
114          size_t start[NUM_DIMS], count[NUM_DIMS];
115          signed char data = 99, data_in;
116 
117          /* Create a netcdf-4 file with one unlimited dim and one
118           * var. Add fill value. */
119          if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
120          if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
121          if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR;
122          if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR;
123          if (nc_enddef(ncid)) ERR;
124 
125          /* Write the second record. */
126          start[0] = 1;
127          count[0] = 1;
128          if (nc_put_vara_schar(ncid, varid, start, count, &data)) ERR;
129 
130          /* Read the first record, it should be the fill value. */
131          start[0] = 0;
132          if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR;
133          if (data_in != fill_value) ERR;
134 
135          /* Read the second record, it should be the value we just wrote
136           * there. */
137          start[0] = 1;
138          if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR;
139          if (data_in != data) ERR;
140 
141          /* Close up. */
142          if (nc_close(ncid)) ERR;
143 
144          /* Open the file and check. */
145          if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
146 
147          /* This will not work because file was opened read-only. */
148           if (nc_rename_var(ncid, 0, "something_very_new") != NC_EPERM) ERR;
149 
150          /* Check metadata. */
151          if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
152          if (nvars_in != 1 || varids_in[0] != 0) ERR;
153          if (nc_inq_varname(ncid, 0, name_in)) ERR;
154          if (strcmp(name_in, VAR_NAME)) ERR;
155 
156          /* Check fill value att. */
157          if (nc_inq_att(ncid, varid, _FillValue, &xtype_in, &len_in)) ERR;
158          if (xtype_in != NC_BYTE || len_in != 1) ERR;
159          if (nc_get_att(ncid, varid, _FillValue, &fill_value_in)) ERR;
160          if (fill_value_in != fill_value) ERR;
161 
162          /* Read the first record, it should be the fill value. */
163          start[0] = 0;
164          if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR;
165          if (data_in != fill_value) ERR;
166 
167          /* Read the second record, it should be the value we just wrote
168           * there. */
169          start[0] = 1;
170          if (nc_get_vara_schar(ncid, varid, start, count, &data_in)) ERR;
171          if (data_in != data) ERR;
172 
173          if (nc_close(ncid)) ERR;
174       }
175 
176       SUMMARIZE_ERR;
177       printf("**** testing fill value with one other attribute...");
178 
179       {
180          int losses_value = 192, losses_value_in;
181 
182          /* Create a netcdf-4 file with one dim and one var. Add another
183           * attribute, then fill value. */
184          if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
185          if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
186          if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR;
187          if (nc_put_att_int(ncid, varid, LOSSES_NAME, NC_INT, 1, &losses_value)) ERR;
188          if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR;
189          if (nc_close(ncid)) ERR;
190 
191          /* Open the file and check. */
192          if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
193          if (nc_inq_att(ncid, 0, LOSSES_NAME, &xtype_in, &len_in)) ERR;
194          if (xtype_in != NC_INT || len_in != 1) ERR;
195          if (nc_get_att(ncid, 0, LOSSES_NAME, &losses_value_in)) ERR;
196          if (losses_value_in != losses_value) ERR;
197          if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR;
198          if (xtype_in != NC_BYTE || len_in != 1) ERR;
199          if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR;
200          if (fill_value_in != fill_value) ERR;
201          if (nc_inq_attid(ncid, 0, LOSSES_NAME, &attnum_in)) ERR;
202          if (attnum_in != 0) ERR;
203          if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR;
204          if (attnum_in != 1) ERR;
205          if (nc_close(ncid)) ERR;
206       }
207 
208       SUMMARIZE_ERR;
209       printf("**** testing fill value with three other attributes...");
210       {
211 #define NUM_LEADERS 3
212          char leader[NUM_LEADERS][NC_MAX_NAME + 1] = {"hair_length_of_strategoi",
213                                                       "hair_length_of_Miltiades",
214                                                       "hair_length_of_Darius_I"};
215          short hair_length[NUM_LEADERS] = {3, 11, 4};
216          short short_in;
217          int a;
218 
219          /* Create a netcdf file with one dim and one var. Add 3
220           * attributes, then fill value. */
221          if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
222          if (nc_def_dim(ncid, DIM1_NAME, DIM1_LEN, &dimids[0])) ERR;
223          if (nc_def_var(ncid, VAR_NAME, NC_BYTE, NUM_DIMS, dimids, &varid)) ERR;
224          for (a = 0; a < NUM_LEADERS; a++)
225             if (nc_put_att_short(ncid, varid, leader[a], NC_SHORT, 1, &hair_length[a])) ERR;
226          if (nc_put_att_schar(ncid, varid, _FillValue, NC_BYTE, 1, &fill_value)) ERR;
227          if (nc_close(ncid)) ERR;
228 
229          /* Open the file. */
230          if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
231 
232          /* Check our three hair-related attributes. */
233          for (a = 0; a < NUM_LEADERS; a++)
234          {
235             if (nc_inq_att(ncid, 0, leader[a], &xtype_in, &len_in)) ERR;
236             if (xtype_in != NC_SHORT || len_in != 1) ERR;
237             if (nc_get_att(ncid, 0, leader[a], &short_in)) ERR;
238             if (short_in != hair_length[a]) ERR;
239             if (nc_inq_attid(ncid, 0, leader[a], &attnum_in)) ERR;
240             if (attnum_in != a) ERR;
241          }
242 
243          /* Check our fill value attribute. */
244          if (nc_inq_att(ncid, 0, _FillValue, &xtype_in, &len_in)) ERR;
245          if (xtype_in != NC_BYTE || len_in != 1) ERR;
246          if (nc_get_att(ncid, 0, _FillValue, &fill_value_in)) ERR;
247          if (fill_value_in != fill_value) ERR;
248 
249          if (nc_close(ncid)) ERR;
250       }
251 
252       SUMMARIZE_ERR;
253       printf("**** testing fill value with simple example...");
254       {
255 /* Dims stuff. */
256 #define NDIMS 3
257 #define VAR_DIMS 3
258 #define DIM_A "dim1"
259 #define DIM_A_LEN 4
260 #define DIM_B "dim2"
261 #define DIM_B_LEN 3
262 #define DIM_C "dim3"
263 #define DIM_C_LEN NC_UNLIMITED
264 
265 /* Var stuff. */
266 #define CXX_VAR_NAME "P"
267 
268 /* Att stuff. */
269 #define NUM_ATTS 4
270 #define LONG_NAME "long_name"
271 #define PRES_MAX_WIND "pressure at maximum wind"
272 #define UNITS "units"
273 #define HECTOPASCALS "hectopascals"
274 
275          int dimid[NDIMS], var_dimids[VAR_DIMS] = {2, 1, 0};
276          float fill_value = -9999.0f;
277          char long_name[] = PRES_MAX_WIND;
278 
279          if (nc_create(FILE_NAME, cmode, &ncid)) ERR;
280 
281          /* Create dims. */
282          if (nc_def_dim(ncid, DIM_A, DIM_A_LEN, &dimid[0])) ERR;
283          if (nc_def_dim (ncid, DIM_B, DIM_B_LEN, &dimid[1])) ERR;
284          if (nc_def_dim(ncid, DIM_C, DIM_C_LEN, &dimid[2])) ERR;
285 
286          /* Create var. */
287          if (nc_def_var(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS,
288                         var_dimids, &varid)) ERR;
289          if (varid) ERR;
290 
291          if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1,
292                         long_name)) ERR;
293          if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1,
294                         UNITS)) ERR;
295 
296          /* Check to ensure the atts have their expected attnums. */
297          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
298          if (attnum_in != 0) ERR;
299          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
300          if (attnum_in != 1) ERR;
301 
302          /* Now add a fill value. This will acutually cause HDF5 to
303           * destroy the dataset and recreate it, recreating also the
304           * three attributes that are attached to it. */
305          if (nc_put_att(ncid, varid, _FillValue, NC_FLOAT,
306                         1, &fill_value)) ERR;
307 
308          /* Check to ensure the atts have their expected attnums. */
309          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
310          if (attnum_in != 0) ERR;
311          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
312          if (attnum_in != 1) ERR;
313 
314          if (nc_close(ncid)) ERR;
315 
316          /* Open the file and check. */
317          if (nc_open(FILE_NAME, 0, &ncid)) ERR;
318          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
319          if (attnum_in != 0) ERR;
320          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
321          if (attnum_in != 1) ERR;
322          if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR;
323          if (attnum_in != 2) ERR;
324 
325          if (nc_close(ncid)) ERR;
326       }
327       SUMMARIZE_ERR;
328 
329 #ifndef NO_NETCDF_2
330       /* The following test is an attempt to recreate a problem occurring
331          in the cxx tests. The file is created in c++ in nctsts.cpp. */
332       printf("**** testing fill value with example from cxx tests in v2 api...");
333       {
334 /* Dims stuff. */
335 #define NDIMS_1 4
336 #define VAR_DIMS 3
337 #define LAT "lat"
338 #define LAT_LEN 4
339 #define LON "lon"
340 #define LON_LEN 3
341 #define FRTIMED "frtimed"
342 #define FRTIMED_LEN NC_UNLIMITED
343 #define TIMELEN "timelen"
344 #define TIMELEN_LEN 20
345 
346 /* Var stuff. */
347 #define CXX_VAR_NAME "P"
348 
349 /* Att stuff. */
350 #define NUM_ATTS 4
351 #define LONG_NAME "long_name"
352 #define UNITS "units"
353 
354          int dimid[NDIMS_1], var_dimids[VAR_DIMS] = {2, 0, 1};
355          float fill_value = -9999.0f;
356          char long_name[] = PRES_MAX_WIND;
357          int i;
358 
359          ncid = nccreate(FILE_NAME, NC_NETCDF4);
360 
361          /* Create dims. */
362          dimid[0] = ncdimdef(ncid, LAT, LAT_LEN);
363          dimid[1] = ncdimdef(ncid, LON, LON_LEN);
364          dimid[2] = ncdimdef(ncid, FRTIMED, FRTIMED_LEN);
365          dimid[3] = ncdimdef(ncid, TIMELEN, TIMELEN_LEN);
366 
367          /* Just check our dimids to see that they are correct. */
368          for (i = 0; i < NDIMS_1; i++)
369             if (dimid[i] != i) ERR;
370 
371          /* Create var. */
372          varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS, var_dimids);
373          if (varid) ERR;
374 
375          /* Add three atts to the var, long_name, units, and
376           * valid_range. */
377          if (nc_put_att(ncid, varid, LONG_NAME, NC_CHAR, strlen(long_name) + 1,
378                         long_name)) ERR;
379          if (nc_put_att(ncid, varid, UNITS, NC_CHAR, strlen(UNITS) + 1,
380                         UNITS)) ERR;
381 
382          /* Check to ensure the atts have their expected attnums. */
383          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
384          if (attnum_in != 0) ERR;
385          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
386          if (attnum_in != 1) ERR;
387 
388          /* Now add a fill value. This will acutually cause HDF5 to
389           * destroy the dataset and recreate it, recreating also the
390           * three attributes that are attached to it. */
391 	 ncattput(ncid, varid, _FillValue, NC_FLOAT, 1, &fill_value);
392 
393          /* Check to ensure the atts have their expected attnums. */
394          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
395          if (attnum_in != 0) ERR;
396          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
397          if (attnum_in != 1) ERR;
398          if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR;
399          if (attnum_in != 2) ERR;
400 
401          ncclose(ncid);
402 
403          /* Open the file and check. */
404          ncid = ncopen(FILE_NAME, 0);
405          if (nc_inq_attid(ncid, 0, LONG_NAME, &attnum_in)) ERR;
406          if (attnum_in != 0) ERR;
407          if (nc_inq_attid(ncid, 0, UNITS, &attnum_in)) ERR;
408          if (attnum_in != 1) ERR;
409          if (nc_inq_attid(ncid, 0, _FillValue, &attnum_in)) ERR;
410          if (attnum_in != 2) ERR;
411          ncclose(ncid);
412       }
413       SUMMARIZE_ERR;
414 #endif /* NO_NETCDF_2 */
415    }
416 
417    printf("**** testing create order varids...");
418 
419 #define UNITS "units"
420 #define DIMNAME "x"
421 #define VARNAME "data"
422    {
423       /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */
424       int ncid, dimid, varid, xvarid;
425       char units[] = "zlotys";
426 
427       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
428       if (nc_def_dim(ncid, DIMNAME, 1, &dimid)) ERR;
429       if (nc_enddef(ncid)) ERR;
430       if (nc_redef(ncid)) ERR;
431 
432       /* Check that these netCDF-4 things will fail on this classic
433        * model file. */
434       if (nc_def_var(ncid, DIMNAME, NC_UINT, 1, &dimid, &xvarid) != NC_ESTRICTNC3) ERR;
435       if (nc_def_var(ncid, DIMNAME, NC_INT, NC_MAX_VAR_DIMS + 1, &dimid,
436                      &xvarid) != NC_EMAXDIMS) ERR;
437       if (nc_enddef(ncid)) ERR;
438       if (nc_def_var(ncid, DIMNAME, NC_INT, 1, &dimid, &xvarid) != NC_ENOTINDEFINE) ERR;
439       if (nc_redef(ncid)) ERR;
440 
441       /* Define the variable for the test. */
442       if (nc_def_var(ncid, DIMNAME, NC_INT, 1, &dimid, &xvarid)) ERR;
443       if (nc_put_att_text(ncid, xvarid, UNITS, strlen(units), units)) ERR;
444       if (nc_def_var(ncid, VARNAME, NC_INT, 1, &dimid, &varid)) ERR;
445       if (nc_close(ncid)) ERR;
446 
447       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
448       if (nc_close(ncid)) ERR;
449    }
450 
451    SUMMARIZE_ERR;
452 #define RANK_wind 1
453    printf("**** testing simple variable renaming...");
454    {
455       /* This test contributed by Jeff Whitaker of NOAA - Thanks Jeff! */
456       int  ncid, lat_dim, time_dim, lon_dim, wind_id;
457       size_t lat_len = 73, time_len = 10, lon_len = 145;
458       int cdf_goober[1];
459 
460 /*      if (nc_set_default_format(NC_FORMAT_NETCDF4, NULL)) ERR;*/
461       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
462 
463       /* define dimensions */
464       if (nc_def_dim(ncid, "a", lon_len, &lon_dim)) ERR;
465       if (nc_def_dim(ncid, "b", lat_len, &lat_dim)) ERR;
466       if (nc_def_dim(ncid, "c", time_len, &time_dim)) ERR;
467 
468       if (nc_put_att_text(ncid, NC_GLOBAL, "a", 3, "bar")) ERR;
469       cdf_goober[0] = 2;
470       if (nc_put_att_int(ncid, NC_GLOBAL, "b", NC_INT, 1, cdf_goober)) ERR;
471 
472       /* define variables */
473       if (nc_def_var(ncid, "aa", NC_FLOAT, RANK_wind, &lon_dim, &wind_id)) ERR;
474       if (nc_close(ncid)) ERR;
475 
476       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
477 
478       /* These won't work. */
479       if (nc_rename_var(ncid + TEST_VAL_42, 0, "az") != NC_EBADID) ERR;
480       if (nc_rename_var(ncid + MILLION, 0, "az") != NC_EBADID) ERR;
481 
482       /* Rename the var. */
483       if (nc_rename_var(ncid, 0, "az")) ERR;
484       if (nc_close(ncid)) ERR;
485    }
486 
487    SUMMARIZE_ERR;
488    printf("**** testing dimension and variable renaming...");
489    {
490       /* This test based on code contributed by Jeff Whitaker of NOAA
491        * - Thanks Jeff! */
492       int  ncid, lat_dim, time_dim, lon_dim, wind_id, temp2_id;
493       size_t lat_len = 73, time_len = 10, lon_len = 145;
494       int wind_dims[RANK_wind], wind_slobber[1], cdf_goober[1];
495 
496       if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
497 
498       /* define dimensions */
499       if (nc_def_dim(ncid, "lon", lon_len, &lon_dim)) ERR;
500       if (nc_def_dim(ncid, "lat", lat_len, &lat_dim)) ERR;
501       if (nc_def_dim(ncid, "time", time_len, &time_dim)) ERR;
502 
503       if (nc_put_att_text(ncid, NC_GLOBAL, "foo", 3, "bar")) ERR;
504       cdf_goober[0] = 2;
505       if (nc_put_att_int(ncid, NC_GLOBAL, "goober", NC_INT, 1, cdf_goober)) ERR;
506 
507       /* define variables */
508       wind_dims[0] = lon_dim;
509       if (nc_def_var(ncid, "temp", NC_FLOAT, RANK_wind, wind_dims, &wind_id)) ERR;
510       if (nc_def_var(ncid, "temp2", NC_FLOAT, RANK_wind, wind_dims, &temp2_id)) ERR;
511 
512       if (nc_put_att_text(ncid, wind_id, "bar", 3, "foo")) ERR;
513       wind_slobber[0] = 3;
514       if (nc_put_att_int(ncid, wind_id, "slobber", NC_INT, 1, wind_slobber)) ERR;
515       if (nc_close(ncid)) ERR;
516 
517       /* re-open dataset*/
518       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
519       if (nc_inq_dimid(ncid, "lon", &lon_dim)) ERR;
520 
521       /* THese won't work due to bad params. */
522       if (nc_rename_dim(ncid + MILLION, lon_dim, "longitude") != NC_EBADID) ERR;
523       if (nc_rename_dim(ncid + TEST_VAL_42, lon_dim, "longitude") != NC_EBADID) ERR;
524       if (nc_rename_dim(ncid, lon_dim, NULL) != NC_EINVAL) ERR;
525 
526       /* rename dimension */
527       if (nc_rename_dim(ncid, lon_dim, "longitude")) ERR;
528 
529       /* These will fail due to bad params. */
530       if (nc_inq_varid(ncid + MILLION, "temp", &wind_id) != NC_EBADID) ERR;
531       if (nc_inq_varid(ncid + TEST_VAL_42, "temp", &wind_id) != NC_EBADID) ERR;
532       if (nc_inq_varid(ncid, NULL, &wind_id) != NC_EINVAL) ERR;
533       if (nc_inq_varid(ncid, "not_a_real_name", &wind_id) != NC_ENOTVAR) ERR;
534       if (nc_inq_varid(ncid, BAD_NAME, &wind_id) != NC_ENOTVAR) ERR;
535       if (nc_inq_varid(ncid, too_long_name, &wind_id) != NC_EMAXNAME) ERR;
536 
537       /* Now get the variable ID. */
538       if (nc_inq_varid(ncid, "temp", &wind_id)) ERR;
539 
540       /* THis also works, pointlessly. */
541       if (nc_inq_varid(ncid, "temp", NULL)) ERR;
542 
543       /* These won't work due to bad parameters. */
544       if (nc_rename_var(ncid + MILLION, wind_id, "wind") != NC_EBADID) ERR;
545       if (nc_rename_var(ncid, wind_id + TEST_VAL_42, "wind") != NC_ENOTVAR) ERR;
546       if (nc_rename_var(ncid, -TEST_VAL_42, "wind") != NC_ENOTVAR) ERR;
547       if (nc_rename_var(ncid, wind_id, BAD_NAME) != NC_EBADNAME) ERR;
548       if (nc_rename_var(ncid, wind_id, too_long_name) != NC_EMAXNAME) ERR;
549       if (nc_rename_var(ncid, wind_id, "temp2") != NC_ENAMEINUSE) ERR;
550       if (nc_rename_var(ncid, wind_id, "windy") != NC_ENOTINDEFINE) ERR;
551       if (nc_rename_var(ncid, wind_id, NULL) != NC_EINVAL) ERR;
552 
553       /* rename variable */
554       if (nc_rename_var(ncid, wind_id, "wind")) ERR;
555 
556       /* Enter define mode and rename it to something longer. */
557       if (nc_redef(ncid)) ERR;
558       if (nc_rename_var(ncid, wind_id, "windy")) ERR;
559       if (nc_inq_varid(ncid, "windy", &wind_id)) ERR;
560       if (nc_close(ncid)) ERR;
561 
562       /* Try again without classic. */
563       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
564 
565       /* define dimension */
566       if (nc_def_dim(ncid, "lon", lon_len, &lon_dim)) ERR;
567 
568       /* define variable */
569       wind_dims[0] = lon_dim;
570       if (nc_def_var(ncid, "temp", NC_FLOAT, RANK_wind, wind_dims, &wind_id)) ERR;
571       if (nc_enddef(ncid)) ERR;
572       if (nc_rename_var(ncid, wind_id, "windy")) ERR;
573       if (nc_close(ncid)) ERR;
574 
575    }
576    SUMMARIZE_ERR;
577 
578 #ifndef NO_NETCDF_2
579 #define VAR_DIMS2 2
580    printf("*** testing 2D array of NC_FLOAT with v2 API...");
581    {
582       int dimid[VAR_DIMS2];
583       int ndims, nvars, natts, recdim;
584 
585       ncid = nccreate(FILE_NAME, NC_NETCDF4);
586 
587       /* Create dims. */
588       dimid[0] = ncdimdef(ncid, LAT, LAT_LEN);
589       dimid[1] = ncdimdef(ncid, LON, LON_LEN);
590 
591       /* Create var. */
592       varid = ncvardef(ncid, CXX_VAR_NAME, NC_FLOAT, VAR_DIMS2, dimid);
593       if (varid != 0) ERR;
594 
595       ncclose(ncid);
596 
597       /* Open the file and check. */
598       ncid = ncopen(FILE_NAME, 0);
599       ncinquire (ncid, &ndims, &nvars, &natts, &recdim);
600       if (nvars != 1 || ndims != 2 || natts != 0 || recdim != -1) ERR;
601       ncclose(ncid);
602    }
603    SUMMARIZE_ERR;
604 #endif /* NO_NETCDF_2 */
605 
606 #define NDIMS 3
607 #define NNAMES 4
608 #define NLINES 13
609    printf("**** testing funny names for netCDF-4...");
610    {
611       int  ncid, wind_id;
612       size_t len[NDIMS] = {7, 3, 1};
613       int dimids[NDIMS], dimids_in[NDIMS], ndims_in;
614       char funny_name[NNAMES][NC_MAX_NAME] = {"\a\t", "\f\n", "\r\v", "\b"};
615       char serious_name[NNAMES][NC_MAX_NAME] = {"name1", "name2", "name3", "name4"};
616       char name_in[NC_MAX_NAME + 1];
617       char *speech[NLINES] = {"who would fardels bear, ",
618 			      "To grunt and sweat under a weary life, ",
619 			      "But that the dread of something after death, ",
620 			      "The undiscover'd country from whose bourn ",
621 			      "No traveller returns, puzzles the will ",
622 			      "And makes us rather bear those ills we have ",
623 			      "Than fly to others that we know not of? ",
624 			      "Thus conscience does make cowards of us all; ",
625 			      "And thus the native hue of resolution ",
626 			      "Is sicklied o'er with the pale cast of thought, ",
627 			      "And enterprises of great pith and moment ",
628 			      "With this regard their currents turn awry, ",
629 			      "And lose the name of action."};
630       char *speech_in[NLINES];
631       int i;
632       unsigned short nlines = NLINES;
633       unsigned int nlines_in;
634 
635       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
636 
637       /* Define dimensions. Funny names are rejected, serious names work. */
638       for (i = 0; i < NDIMS; i++)
639       	 if (nc_def_dim(ncid, funny_name[i], len[i], &dimids[i]) != NC_EBADNAME) ERR;
640       for (i = 0; i < NDIMS; i++)
641       	 if (nc_def_dim(ncid, serious_name[i], len[i], &dimids[i])) ERR;
642 
643       /* Write some global atts. Funny names are rejected, serious names work. */
644       if (nc_put_att_string(ncid, NC_GLOBAL, funny_name[0], NLINES,
645 			    (const char **)speech) != NC_EBADNAME) ERR;
646       if (nc_put_att_ushort(ncid, NC_GLOBAL, funny_name[1], NC_UINT, 1, &nlines) != NC_EBADNAME) ERR;
647       if (nc_put_att_string(ncid, NC_GLOBAL, serious_name[0], NLINES,
648 			    (const char **)speech)) ERR;
649       if (nc_put_att_ushort(ncid, NC_GLOBAL, serious_name[1], NC_UINT, 1, &nlines)) ERR;
650 
651       /* Define variables. Funny name fails, seriousness wins the day! */
652       if (nc_def_var(ncid, funny_name[3], NC_INT64, NDIMS, dimids, &wind_id) != NC_EBADNAME) ERR;
653       if (nc_def_var(ncid, serious_name[3], NC_INT64, NDIMS, dimids, &wind_id)) ERR;
654 
655       if (nc_close(ncid)) ERR;
656 
657       /* Open the file and check. */
658       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
659       if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR;
660       if (ndims_in != NDIMS) ERR;
661       for (i = 0; i < NDIMS; i++)
662       {
663       	 if (dimids_in[i] != i) ERR;
664       	 if (nc_inq_dimname(ncid, i, name_in)) ERR;
665       	 if (strcmp(name_in, serious_name[i])) ERR;
666       }
667 
668       if (nc_get_att_string(ncid, NC_GLOBAL, serious_name[0], (char **)speech_in)) ERR;
669       for (i = 0; i < NLINES; i++)
670       	 if (strcmp(speech_in[i], speech[i])) ERR;
671       if (nc_get_att_uint(ncid, NC_GLOBAL, serious_name[1], &nlines_in)) ERR;
672       if (nlines_in != NLINES) ERR;
673       if (nc_free_string(NLINES, (char **)speech_in)) ERR;
674       if (nc_inq_varname(ncid, 0, name_in)) ERR;
675       if (strcmp(name_in, serious_name[3])) ERR;
676       if (nc_close(ncid)) ERR;
677    }
678    SUMMARIZE_ERR;
679    printf("**** testing endianness...");
680 
681 #define NDIMS4 1
682 #define DIM4_NAME "Joe"
683 #define VAR_NAME4 "Ed"
684 #define DIM4_LEN 10
685    {
686       int dimids[NDIMS4], dimids_in[NDIMS4];
687       int varid, varid1;
688       int ndims, nvars, natts, unlimdimid;
689       nc_type xtype_in;
690       char name_in[NC_MAX_NAME + 1];
691       int data[DIM4_LEN], data_in[DIM4_LEN];
692       int endian_in;
693       int i;
694 
695       for (i = 0; i < DIM4_LEN; i++)
696          data[i] = i;
697 
698       /* Create a netcdf-4 file with one dim and one var. */
699       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
700       if (nc_def_dim(ncid, DIM4_NAME, DIM4_LEN, &dimids[0])) ERR;
701       if (dimids[0] != 0) ERR;
702       if (nc_def_var(ncid, VAR_NAME4, NC_INT, NDIMS4, dimids, &varid)) ERR;
703       if (nc_def_var_endian(ncid, varid, NC_ENDIAN_BIG)) ERR;
704       if (varid != 0) ERR;
705       if (nc_put_var_int(ncid, varid, data)) ERR;
706 
707       /* Check stuff. */
708       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
709       if (ndims != NDIMS4 || nvars != 1 || natts != 0 ||
710           unlimdimid != -1) ERR;
711       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
712       if (nvars != 1) ERR;
713       if (varids_in[0] != 0) ERR;
714 
715       /* Test some bad parameter values. */
716       if (nc_inq_var(ncid + MILLION, 0, name_in, &xtype_in, &ndims,
717                      dimids_in, &natts) != NC_EBADID) ERR;
718       if (nc_inq_var(ncid + TEST_VAL_42, 0, name_in, &xtype_in, &ndims,
719                      dimids_in, &natts) != NC_EBADID) ERR;
720       if (nc_inq_var(ncid, -TEST_VAL_42, name_in, &xtype_in, &ndims,
721                      dimids_in, &natts) != NC_ENOTVAR) ERR;
722       if (nc_inq_var(ncid, 1, name_in, &xtype_in, &ndims,
723                      dimids_in, &natts) != NC_ENOTVAR) ERR;
724       if (nc_inq_var(ncid, TEST_VAL_42, name_in, &xtype_in, &ndims,
725                      dimids_in, &natts) != NC_ENOTVAR) ERR;
726 
727       /* Now pass correct parameters. */
728       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims,
729                      dimids_in, &natts)) ERR;
730       if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT ||
731           ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR;
732       if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR;
733       if (endian_in != NC_ENDIAN_BIG) ERR;
734 
735       /* This also works, uselessly. */
736       if (nc_inq_var(ncid, 0, name_in, NULL, NULL, NULL, NULL)) ERR;
737 
738       if (nc_close(ncid)) ERR;
739 
740       /* Open the file and check the same stuff. */
741       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
742 
743       /* This won't work. */
744       if (nc_def_var(ncid, "this_wont_work", NC_BYTE, NDIMS4, dimids,
745                      &varid1) != NC_EPERM) ERR;
746 
747       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
748       if (ndims != NDIMS4 || nvars != 1 || natts != 0 ||
749           unlimdimid != -1) ERR;
750       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
751       if (nvars != 1) ERR;
752       if (varids_in[0] != 0) ERR;
753       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims,
754                      dimids_in, &natts)) ERR;
755       if (strcmp(name_in, VAR_NAME4) || xtype_in != NC_INT ||
756           ndims != 1 || natts != 0 || dimids_in[0] != 0) ERR;
757       if (nc_inq_var_endian(ncid, 0, &endian_in)) ERR;
758       if (endian_in != NC_ENDIAN_BIG) ERR;
759       if (nc_get_var_int(ncid, varid, data_in)) ERR;
760       for (i = 0; i < DIM4_LEN; i++)
761 	 if (data[i] != data_in[i]) ERR;
762 
763       if (nc_close(ncid)) ERR;
764    }
765 
766    SUMMARIZE_ERR;
767    printf("**** testing chunking and the chunk cache...");
768    {
769 #define NDIMS5 1
770 #define DIM5_NAME "D5"
771 #define VAR_NAME5 "V5"
772 #define VAR_NAME5_1 "V5_1"
773 #define VAR_NAME5_2 "V5_2"
774 #define VAR_NAME5_3 "V5_3"
775 #define VAR_NAME5_4 "V5_4"
776 #define DIM5_LEN 1000
777 #define CACHE_SIZE 32000000
778 #define CACHE_NELEMS 1009
779 #define CACHE_PREEMPTION .75
780 #define CACHE_SIZE2 64000000
781 #define CACHE_NELEMS2 2000
782 #define CACHE_PREEMPTION2 .50
783 #define NVAR4 5
784 
785       int dimids[NDIMS5], dimids_in[NDIMS5];
786       int varid, varid1, varid2, varid3, varid4;
787       int varids_in4[NVAR4];
788       int ndims, nvars, natts, unlimdimid;
789       nc_type xtype_in;
790       char name_in[NC_MAX_NAME + 1];
791       int data[DIM5_LEN], data_in[DIM5_LEN];
792       size_t chunksize[NDIMS5] = {5};
793       size_t bad_chunksize[NDIMS5] = {-5}; /* Converted to large pos number since size_t is unsigned. */
794       size_t large_chunksize[NDIMS5] = {(size_t)NC_MAX_INT + (size_t)1}; /* Too big for inq_var_chunking_ints(). */
795       size_t chunksize_in[NDIMS5];
796       int chunksize_int[NDIMS5];
797       int chunksize_int_in[NDIMS5];
798       int storage_in;
799       size_t cache_size_in, cache_nelems_in;
800       float cache_preemption_in;
801       int cache_size_int_in, cache_nelems_int_in;
802       int cache_preemption_int_in;
803       int cache_size_int_default, cache_nelems_int_default;
804       int cache_preemption_int_default;
805       int i, d;
806 
807       for (i = 0; i < DIM5_LEN; i++)
808          data[i] = i;
809 
810       /* Create a netcdf-4 file with one dim and one var. */
811       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
812       if (nc_def_dim(ncid, DIM5_NAME, DIM5_LEN, &dimids[0])) ERR;
813       if (dimids[0] != 0) ERR;
814 
815       /* Define the variable. */
816       if (nc_def_var(ncid, VAR_NAME5, NC_INT, NDIMS5, dimids, &varid)) ERR;
817 
818       /* These will fail due to bad parameters. */
819       if (nc_def_var_chunking(ncid + MILLION, varid, NC_CHUNKED,
820                               chunksize) != NC_EBADID) ERR;
821       if (nc_def_var_chunking(ncid + TEST_VAL_42, varid, NC_CHUNKED,
822                               chunksize) != NC_EBADID) ERR;
823       if (nc_def_var_chunking(ncid, varid + TEST_VAL_42, NC_CHUNKED,
824                               chunksize) != NC_ENOTVAR) ERR;
825       if (nc_def_var_chunking(ncid, varid + 1, NC_CHUNKED,
826                               chunksize) != NC_ENOTVAR) ERR;
827       if (nc_def_var_chunking(ncid, -1, NC_CHUNKED,
828                               chunksize) != NC_ENOTVAR) ERR;
829       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, bad_chunksize) !=
830           NC_EBADCHUNK) ERR;
831 
832       /* Define the chunking. */
833       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunksize)) ERR;
834 
835       /* Try to set var cache with bad parameters. They will be
836        * rejected. */
837       if (nc_set_var_chunk_cache(ncid + MILLION, varid, CACHE_SIZE, CACHE_NELEMS,
838                                  CACHE_PREEMPTION) != NC_EBADID) ERR;
839       if (nc_set_var_chunk_cache(ncid + 1, varid, CACHE_SIZE, CACHE_NELEMS,
840                                  CACHE_PREEMPTION) != NC_EBADID) ERR;
841       if (nc_set_var_chunk_cache(ncid, varid + TEST_VAL_42, CACHE_SIZE, CACHE_NELEMS,
842                                  CACHE_PREEMPTION) != NC_ENOTVAR) ERR;
843       if (nc_set_var_chunk_cache(ncid, -TEST_VAL_42, CACHE_SIZE, CACHE_NELEMS,
844                                  CACHE_PREEMPTION) != NC_ENOTVAR) ERR;
845       if (nc_set_var_chunk_cache(ncid, varid + 1, CACHE_SIZE, CACHE_NELEMS,
846                                  CACHE_PREEMPTION) != NC_ENOTVAR) ERR;
847       if (nc_set_var_chunk_cache(ncid, varid, CACHE_SIZE, CACHE_NELEMS,
848                                  CACHE_PREEMPTION + TEST_VAL_42) != NC_EINVAL) ERR;
849       if (nc_set_var_chunk_cache(ncid, varid, CACHE_SIZE, CACHE_NELEMS,
850                                  CACHE_PREEMPTION - TEST_VAL_42) != NC_EINVAL) ERR;
851 
852       /* Set the cache. */
853       if (nc_set_var_chunk_cache(ncid, varid, CACHE_SIZE, CACHE_NELEMS, CACHE_PREEMPTION)) ERR;
854       if (nc_put_var_int(ncid, varid, data)) ERR;
855 
856       /* Check stuff. */
857       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
858       if (ndims != NDIMS5 || nvars != 1 || natts != 0 ||
859           unlimdimid != -1) ERR;
860       if (nc_inq_varids(ncid, &nvars, varids_in4)) ERR;
861       if (nvars != 1) ERR;
862       if (varids_in4[0] != 0) ERR;
863       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
864       if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
865 	  dimids_in[0] != 0) ERR;
866       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
867       for (d = 0; d < NDIMS5; d++)
868 	 if (chunksize[d] != chunksize_in[d]) ERR;
869       if (storage_in != NC_CHUNKED) ERR;
870       if (nc_get_var_int(ncid, varid, data_in)) ERR;
871       for (i = 0; i < DIM5_LEN; i++)
872          if (data[i] != data_in[i])
873 	    ERR_RET;
874 
875       /* These will not work due to bad parameters. */
876       if (nc_inq_var_chunking_ints(ncid + MILLION, 0, &storage_in,
877                                    chunksize_int_in) != NC_EBADID) ERR;
878       if (nc_inq_var_chunking_ints(ncid + TEST_VAL_42, 0, &storage_in,
879                                    chunksize_int_in) != NC_EBADID) ERR;
880       if (nc_inq_var_chunking_ints(ncid, -1, &storage_in,
881                                    chunksize_int_in) != NC_ENOTVAR) ERR;
882       if (nc_inq_var_chunking_ints(ncid, varid + 1, &storage_in,
883                                    chunksize_int_in) != NC_ENOTVAR) ERR;
884       if (nc_inq_var_chunking_ints(ncid, varid + TEST_VAL_42, &storage_in,
885                                    chunksize_int_in) != NC_ENOTVAR) ERR;
886 
887       /* Now check with the fortran versions of the var_chunking. */
888       if (nc_inq_var_chunking_ints(ncid, 0, &storage_in, chunksize_int_in)) ERR;
889       if (storage_in != NC_CHUNKED) ERR;
890       for (d = 0; d < NDIMS5; d++)
891 	 if (chunksize_int_in[d] != chunksize[d]) ERR;
892       for (d = 0; d < NDIMS5; d++)
893          chunksize_int[d] = chunksize[d] * 2;
894       if (nc_inq_var_chunking_ints(ncid, 0, &storage_in, NULL)) ERR;
895       if (storage_in != NC_CHUNKED) ERR;
896 
897       /* Check that some bad parameter values are rejected properly. */
898       if (nc_def_var_chunking_ints(ncid + MILLION, varid, NC_CHUNKED,
899                                    chunksize_int) != NC_EBADID) ERR;
900       if (nc_def_var_chunking_ints(ncid + TEST_VAL_42, varid, NC_CHUNKED,
901                                    chunksize_int) != NC_EBADID) ERR;
902       if (nc_def_var_chunking_ints(ncid, -1, NC_CHUNKED,
903                                    chunksize_int) != NC_ENOTVAR) ERR;
904       if (nc_def_var_chunking_ints(ncid, varid + 1, NC_CHUNKED,
905                                    chunksize_int) != NC_ENOTVAR) ERR;
906       if (nc_def_var_chunking_ints(ncid, varid + TEST_VAL_42, NC_CHUNKED,
907                                    chunksize_int) != NC_ENOTVAR) ERR;
908 
909       if (nc_def_var_chunking_ints(ncid, varid, NC_CHUNKED, chunksize_int) != NC_ELATEDEF) ERR;
910       if (nc_redef(ncid)) ERR;
911       if (nc_def_var(ncid, VAR_NAME5_1, NC_INT, NDIMS5, dimids, &varid1)) ERR;
912       if (nc_def_var(ncid, VAR_NAME5_2, NC_INT, 0, NULL, &varid2)) ERR;
913       if (nc_def_var(ncid, VAR_NAME5_3, NC_INT, 0, NULL, &varid3)) ERR;
914       if (nc_def_var(ncid, VAR_NAME5_4, NC_INT, NDIMS5, dimids, &varid4)) ERR;
915       if (nc_def_var_chunking(ncid, varid2, NC_CHUNKED, chunksize)) ERR;
916       if (nc_def_var_chunking(ncid, varid3, NC_CONTIGUOUS, NULL)) ERR;
917       if (nc_def_var_chunking(ncid, varid4, NC_CHUNKED, large_chunksize) != NC_EBADCHUNK) ERR;
918       if (nc_def_var_chunking_ints(ncid, varid2, NC_CHUNKED, chunksize_int)) ERR;
919       if (nc_def_var_chunking_ints(ncid, varid1, NC_CHUNKED, chunksize_int)) ERR;
920       if (nc_inq_var_chunking_ints(ncid, varid2, NULL, chunksize_int_in)) ERR;
921       if (nc_inq_var_chunking_ints(ncid, varid1, NULL, chunksize_int_in)) ERR;
922       for (d = 0; d < NDIMS5; d++)
923 	 if (chunksize_int_in[d] != chunksize[d] * 2) ERR;
924       if (nc_inq_var_chunking_ints(ncid, varid1, &storage_in, NULL)) ERR;
925       if (storage_in != NC_CHUNKED) ERR;
926       if (nc_inq_var_chunking_ints(ncid, varid2, NULL, chunksize_int_in)) ERR;
927       if (nc_inq_var_chunking_ints(ncid, varid3, &storage_in, NULL)) ERR;
928       if (storage_in != NC_CONTIGUOUS) ERR;
929       if (nc_inq_var_chunking_ints(ncid, varid3, &storage_in, chunksize_int_in)) ERR;
930       if (storage_in != NC_CONTIGUOUS) ERR;
931 
932       /* Check that some bad parameter values are rejected properly. */
933       if (nc_get_var_chunk_cache(ncid + MILLION, varid, &cache_size_in, &cache_nelems_in,
934 				 &cache_preemption_in) != NC_EBADID) ERR;
935       if (nc_get_var_chunk_cache(ncid + 1, -TEST_VAL_42, &cache_size_in, &cache_nelems_in,
936 				 &cache_preemption_in) != NC_EBADID) ERR;
937       if (nc_get_var_chunk_cache(ncid, varid + TEST_VAL_42, &cache_size_in, &cache_nelems_in,
938 				 &cache_preemption_in) != NC_ENOTVAR) ERR;
939       if (nc_get_var_chunk_cache(ncid, varid4 + 1, &cache_size_in, &cache_nelems_in,
940 				 &cache_preemption_in) != NC_ENOTVAR) ERR;
941       if (nc_get_var_chunk_cache(ncid, -TEST_VAL_42, &cache_size_in, &cache_nelems_in,
942 				 &cache_preemption_in) != NC_ENOTVAR) ERR;
943 
944       /* Get the var chunk cache settings. */
945       if (nc_get_var_chunk_cache(ncid, varid, &cache_size_in, &cache_nelems_in,
946 				 &cache_preemption_in)) ERR;
947       if (cache_size_in != CACHE_SIZE || cache_nelems_in != CACHE_NELEMS ||
948 	  cache_preemption_in != CACHE_PREEMPTION) ERR;
949       /* THis should also work, pointlessly. */
950       if (nc_get_var_chunk_cache(ncid, varid, NULL, NULL, NULL)) ERR;
951 
952       /* Check the _int version of this function, used by the F77 API. */
953       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_in, &cache_nelems_int_in,
954                                       &cache_preemption_int_in)) ERR;
955       if (cache_size_int_in != CACHE_SIZE / MEGABYTE) ERR;
956       if (cache_nelems_int_in != CACHE_NELEMS) ERR;
957       if (cache_preemption_int_in != (int)(CACHE_PREEMPTION * 100)) ERR;
958       /* THis should also work, pointlessly. */
959       if (nc_get_var_chunk_cache_ints(ncid, varid, NULL, NULL, NULL)) ERR;
960 
961       if (nc_close(ncid)) ERR;
962 
963       /* Open the file and check the same stuff. */
964       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
965 
966       /* Check stuff. */
967       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
968       if (ndims != NDIMS5 || nvars != NVAR4 || natts != 0 ||
969           unlimdimid != -1) ERR;
970       if (nc_inq_varids(ncid, &nvars, varids_in4)) ERR;
971       if (nvars != NVAR4) ERR;
972       if (varids_in4[0] != 0 || varids_in4[1] != 1) ERR;
973       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
974       if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
975 	  dimids_in[0] != 0) ERR;
976       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
977       for (d = 0; d < NDIMS5; d++)
978 	 if (chunksize[d] != chunksize_in[d]) ERR;
979       if (storage_in != NC_CHUNKED) ERR;
980       if (nc_get_var_int(ncid, varid, data_in)) ERR;
981       for (i = 0; i < DIM5_LEN; i++)
982          if (data[i] != data_in[i])
983 	    ERR_RET;
984 
985       /* Use the _int function to change the var chunk cache settings. */
986       if (nc_set_var_chunk_cache_ints(ncid, varid, CACHE_SIZE2 / MEGABYTE, CACHE_NELEMS2,
987                                       (int)(CACHE_PREEMPTION2 * 100))) ERR;
988 
989       /* These will fail due to bad ncid and group ID. */
990       if (nc_get_var_chunk_cache_ints(ncid + MILLION, varid, &cache_size_int_in, &cache_nelems_int_in,
991                                       &cache_preemption_int_in) != NC_EBADID) ERR;
992       if (nc_get_var_chunk_cache_ints(ncid + TEST_VAL_42, varid, &cache_size_int_in, &cache_nelems_int_in,
993                                       &cache_preemption_int_in) != NC_EBADID) ERR;
994 
995       /* Now get the settings. */
996       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_in, &cache_nelems_int_in,
997                                       &cache_preemption_int_in)) ERR;
998       if (cache_size_int_in != CACHE_SIZE2 / MEGABYTE || cache_nelems_int_in != CACHE_NELEMS2 ||
999           cache_preemption_int_in != (int)(CACHE_PREEMPTION2 * 100)) ERR;
1000 
1001       /* Passing negative values to the _int function causes them to
1002        * be ignored and a default setting used. Set all to negative to
1003        * get defaults.. */
1004       if (nc_set_var_chunk_cache_ints(ncid, varid, -CACHE_SIZE / MEGABYTE, -CACHE_NELEMS2,
1005                                       -(int)(CACHE_PREEMPTION2 * 100))) ERR;
1006       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_default, &cache_nelems_int_default,
1007                                       &cache_preemption_int_default)) ERR;
1008 
1009       /* Now set the size only. */
1010       if (nc_set_var_chunk_cache_ints(ncid, varid, CACHE_SIZE / MEGABYTE, -CACHE_NELEMS2,
1011                                       -(int)(CACHE_PREEMPTION2 * 100))) ERR;
1012       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_in, &cache_nelems_int_in,
1013                                       &cache_preemption_int_in)) ERR;
1014       if (cache_size_int_in != CACHE_SIZE / MEGABYTE || cache_nelems_int_in != cache_nelems_int_default ||
1015           cache_preemption_int_in != cache_preemption_int_default) ERR;
1016       /* Now set the nelems only. */
1017       if (nc_set_var_chunk_cache_ints(ncid, varid, -CACHE_SIZE / MEGABYTE, CACHE_NELEMS,
1018                                       -(int)(CACHE_PREEMPTION2 * 100))) ERR;
1019       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_in, &cache_nelems_int_in,
1020                                       &cache_preemption_int_in)) ERR;
1021       if (cache_size_int_in != cache_size_int_default || cache_nelems_int_in != CACHE_NELEMS ||
1022           cache_preemption_int_in != cache_preemption_int_default) ERR;
1023       /* Now set the preemption only. */
1024       if (nc_set_var_chunk_cache_ints(ncid, varid, -CACHE_SIZE / MEGABYTE, -CACHE_NELEMS,
1025                                       (int)(CACHE_PREEMPTION2 * 100))) ERR;
1026       if (nc_get_var_chunk_cache_ints(ncid, varid, &cache_size_int_in, &cache_nelems_int_in,
1027                                       &cache_preemption_int_in)) ERR;
1028       if (cache_size_int_in != cache_size_int_default || cache_nelems_int_in != cache_nelems_int_default ||
1029           cache_preemption_int_in != (int)(CACHE_PREEMPTION2 * 100)) ERR;
1030 
1031       if (nc_close(ncid)) ERR;
1032    }
1033 
1034    SUMMARIZE_ERR;
1035    printf("**** testing netCDF-4 functions on netCDF-3 files...");
1036    {
1037       int dimids[NDIMS5], dimids_in[NDIMS5];
1038       int varid;
1039       int ndims, nvars, natts, unlimdimid;
1040       nc_type xtype_in;
1041       char name_in[NC_MAX_NAME + 1];
1042       int data[DIM5_LEN], data_in[DIM5_LEN];
1043       size_t chunksize[NDIMS5] = {5};
1044       size_t chunksize_in[NDIMS5];
1045       int storage_in;
1046       size_t cache_size_in, cache_nelems_in;
1047       float cache_preemption_in;
1048       int i;
1049 
1050       for (i = 0; i < DIM5_LEN; i++)
1051          data[i] = i;
1052 
1053       /* Create a netcdf classic file with one dim and one var. */
1054       if (nc_create(FILE_NAME, 0, &ncid)) ERR;
1055       if (nc_def_dim(ncid, DIM5_NAME, DIM5_LEN, &dimids[0])) ERR;
1056       if (dimids[0] != 0) ERR;
1057       if (nc_def_var(ncid, VAR_NAME5, NC_INT, NDIMS5, dimids, &varid)) ERR;
1058 
1059       /* These will return error. */
1060       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunksize) != NC_ENOTNC4) ERR;
1061       if (nc_set_var_chunk_cache(ncid, varid, CACHE_SIZE, CACHE_NELEMS,
1062                                  CACHE_PREEMPTION) != NC_ENOTNC4) ERR;
1063 
1064       if (nc_enddef(ncid)) ERR;
1065       if (nc_put_var_int(ncid, varid, data)) ERR;
1066 
1067       /* Check stuff. */
1068       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1069       if (ndims != NDIMS5 || nvars != 1 || natts != 0 ||
1070           unlimdimid != -1) ERR;
1071       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1072       if (nvars != 1) ERR;
1073       if (varids_in[0] != 0) ERR;
1074       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1075       if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
1076 	  dimids_in[0] != 0) ERR;
1077 
1078       /* This call fails. */
1079       if (nc_get_var_chunk_cache(ncid, varid, &cache_size_in, &cache_nelems_in,
1080 				 &cache_preemption_in) != NC_ENOTNC4) ERR;
1081 
1082       /* This call passes but does nothing. */
1083       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1084 
1085       if (nc_get_var_int(ncid, varid, data_in)) ERR;
1086       for (i = 0; i < DIM5_LEN; i++)
1087          if (data[i] != data_in[i])
1088 	    ERR_RET;
1089 
1090       if (nc_close(ncid)) ERR;
1091 
1092       /* Open the file and check the same stuff. */
1093       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
1094 
1095       /* Check stuff. */
1096       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1097       if (ndims != NDIMS5 || nvars != 1 || natts != 0 ||
1098           unlimdimid != -1) ERR;
1099       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1100       if (nvars != 1) ERR;
1101       if (varids_in[0] != 0) ERR;
1102       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1103       if (strcmp(name_in, VAR_NAME5) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
1104 	  dimids_in[0] != 0) ERR;
1105 
1106       /* This call fails. */
1107       if (nc_get_var_chunk_cache(ncid, varid, &cache_size_in, &cache_nelems_in,
1108 				 &cache_preemption_in) != NC_ENOTNC4) ERR;
1109 
1110       /* This call passes but does nothing. */
1111       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1112 
1113       if (nc_get_var_int(ncid, varid, data_in)) ERR;
1114       for (i = 0; i < DIM5_LEN; i++)
1115          if (data[i] != data_in[i])
1116 	    ERR_RET;
1117       if (nc_close(ncid)) ERR;
1118    }
1119 
1120    SUMMARIZE_ERR;
1121    printf("**** testing contiguous storage...");
1122    {
1123 #define DIM6_NAME "D5"
1124 #define VAR_NAME6 "V5"
1125 #define DIM6_LEN 100
1126 
1127       int dimids[NDIMS1], dimids_in[NDIMS1];
1128       int varid;
1129       int ndims, nvars, natts, unlimdimid;
1130       nc_type xtype_in;
1131       char name_in[NC_MAX_NAME + 1];
1132       int data[DIM6_LEN], data_in[DIM6_LEN];
1133       size_t chunksize_in[NDIMS1];
1134       int storage_in;
1135       int i;
1136 
1137       for (i = 0; i < DIM6_LEN; i++)
1138          data[i] = i;
1139 
1140       /* Create a netcdf-4 file with one dim and one var. */
1141       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1142       if (nc_def_dim(ncid, DIM6_NAME, DIM6_LEN, &dimids[0])) ERR;
1143       if (dimids[0] != 0) ERR;
1144       if (nc_def_var(ncid, VAR_NAME6, NC_INT, NDIMS1, dimids, &varid)) ERR;
1145       if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL)) ERR;
1146       if (nc_put_var_int(ncid, varid, data)) ERR;
1147 
1148       /* Check stuff. */
1149       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1150       if (ndims != NDIMS1 || nvars != 1 || natts != 0 ||
1151           unlimdimid != -1) ERR;
1152       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1153       if (nvars != 1) ERR;
1154       if (varids_in[0] != 0) ERR;
1155       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1156       if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
1157 	  dimids_in[0] != 0) ERR;
1158       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1159       if (storage_in != NC_CONTIGUOUS) ERR;
1160       if (nc_get_var_int(ncid, varid, data_in)) ERR;
1161       for (i = 0; i < DIM6_LEN; i++)
1162          if (data_in[i] != data[i])
1163 	    ERR_RET;
1164       if (nc_close(ncid)) ERR;
1165 
1166       /* Open the file and check the same stuff. */
1167       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
1168 
1169       /* Check stuff. */
1170       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1171       if (ndims != NDIMS1 || nvars != 1 || natts != 0 ||
1172           unlimdimid != -1) ERR;
1173       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1174       if (nvars != 1) ERR;
1175       if (varids_in[0] != 0) ERR;
1176       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1177       if (strcmp(name_in, VAR_NAME6) || xtype_in != NC_INT || ndims != 1 || natts != 0 ||
1178 	  dimids_in[0] != 0) ERR;
1179       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1180       if (storage_in != NC_CONTIGUOUS) ERR;
1181       if (nc_get_var_int(ncid, varid, data_in)) ERR;
1182       for (i = 0; i < DIM6_LEN; i++)
1183          if (data[i] != data_in[i])
1184 	    ERR_RET;
1185       if (nc_close(ncid)) ERR;
1186    }
1187 
1188    SUMMARIZE_ERR;
1189    printf("**** testing extreme numbers dude...");
1190    {
1191 #define VAR_NAME7 "V5"
1192 #define DIM6_LEN 100
1193 
1194       int varid;
1195       int ndims, nvars, natts, unlimdimid;
1196       nc_type xtype_in;
1197       char name_in[NC_MAX_NAME + 1];
1198 /*      unsigned long long data = 9223372036854775807ull, data_in;*/
1199       unsigned long long data = 9223372036854775817ull, data_in;
1200 
1201       /* Create a netcdf-4 file with scalar var. */
1202       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1203       if (nc_def_var(ncid, VAR_NAME7, NC_UINT64, 0, NULL, &varid)) ERR;
1204       if (nc_put_var_ulonglong(ncid, varid, &data)) ERR;
1205 
1206       /* Check stuff. */
1207       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1208       if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
1209       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1210       if (nvars != 1 || varids_in[0] != 0) ERR;
1211       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR;
1212       if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR;
1213       if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR;
1214       if (data_in != data) ERR;
1215       if (nc_close(ncid)) ERR;
1216 
1217       /* Open the file and check the same stuff. */
1218       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
1219       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1220       if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
1221       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1222       if (nvars != 1 || varids_in[0] != 0) ERR;
1223       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, NULL, &natts)) ERR;
1224       if (strcmp(name_in, VAR_NAME7) || xtype_in != NC_UINT64 || ndims != 0 || natts != 0) ERR;
1225       if (nc_get_var_ulonglong(ncid, varid, &data_in)) ERR;
1226       if (data_in != data) ERR;
1227       if (nc_close(ncid)) ERR;
1228    }
1229 
1230    SUMMARIZE_ERR;
1231    printf("**** testing error codes for name clashes...");
1232    {
1233 #define GENERIC_NAME "bob"
1234       int ncid, varid, numgrps, ntypes;
1235 
1236       /* Create a netcdf-4 file with one var. */
1237       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1238       if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid)) ERR;
1239 
1240       /* These don'e work, because the name is already in use. Make
1241        * sure the correct error is returned. */
1242       if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR;
1243       if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR;
1244 
1245       /* Close it. */
1246       if (nc_close(ncid)) ERR;
1247 
1248       /* Open the file and check. */
1249       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
1250       if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
1251       if (nvars_in != 1 || varids_in[0] != 0) ERR;
1252       if (nc_inq_varname(ncid, 0, name_in)) ERR;
1253       if (strcmp(name_in, GENERIC_NAME)) ERR;
1254       if (nc_inq_grps(ncid, &numgrps, NULL)) ERR;
1255       if (numgrps) ERR;
1256       if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR;
1257       if (ntypes) ERR;
1258       if (nc_close(ncid)) ERR;
1259    }
1260 
1261    SUMMARIZE_ERR;
1262    printf("**** testing error codes for name clashes some more...");
1263 
1264    {
1265 #define GENERIC_NAME "bob"
1266       int ncid, varid, numgrps, ntypes;
1267 
1268       /* Create a netcdf-4 file with one type. */
1269       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1270       if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL)) ERR;
1271 
1272       /* These don'e work, because the name is already in use. Make
1273        * sure the correct error is returned. */
1274       if (nc_def_grp(ncid, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR;
1275       if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR;
1276 
1277       /* Close it. */
1278       if (nc_close(ncid)) ERR;
1279 
1280       /* Open the file and check. */
1281       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
1282       if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
1283       if (nvars_in) ERR;
1284       if (nc_inq_grps(ncid, &numgrps, NULL)) ERR;
1285       if (numgrps) ERR;
1286       if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR;
1287       if (ntypes != 1) ERR;
1288       if (nc_close(ncid)) ERR;
1289    }
1290    SUMMARIZE_ERR;
1291    printf("**** testing error codes for name clashes even more...");
1292 
1293    {
1294 #define GENERIC_NAME "bob"
1295       int ncid, varid, numgrps, ntypes;
1296 
1297       /* Create a netcdf-4 file with one group. */
1298       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1299       if (nc_def_grp(ncid, GENERIC_NAME, NULL)) ERR;
1300 
1301       /* These don'e work, because the name is already in use. Make
1302        * sure the correct error is returned. */
1303       if (nc_def_opaque(ncid, 1, GENERIC_NAME, NULL) != NC_ENAMEINUSE) ERR;
1304       if (nc_def_var(ncid, GENERIC_NAME, NC_BYTE, 0, NULL, &varid) != NC_ENAMEINUSE) ERR;
1305 
1306       /* Close it. */
1307       if (nc_close(ncid)) ERR;
1308 
1309       /* Open the file and check. */
1310       if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
1311       if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR;
1312       if (nvars_in) ERR;
1313       if (nc_inq_grps(ncid, &numgrps, NULL)) ERR;
1314       if (numgrps != 1) ERR;
1315       if (nc_inq_typeids(ncid, &ntypes, NULL)) ERR;
1316       if (ntypes) ERR;
1317       if (nc_close(ncid)) ERR;
1318    }
1319    SUMMARIZE_ERR;
1320    printf("**** testing error code for too-large chunks...");
1321    {
1322 #define NDIMS17 2
1323 #define DIM17_NAME "personality"
1324 #define DIM17_NAME_2 "good_looks"
1325 #define VAR_NAME17 "ed"
1326 #define DIM17_LEN 2147483644 /* max dimension size - 2GB - 4. */
1327 #define DIM17_2_LEN 1000
1328 
1329       int dimids[NDIMS17], dimids_in[NDIMS17];
1330       int varid;
1331       int ndims, nvars, natts, unlimdimid;
1332       nc_type xtype_in;
1333       char name_in[NC_MAX_NAME + 1];
1334       size_t chunksize[NDIMS17] = {5, 5};
1335       size_t bad_chunksize[NDIMS17] = {5, DIM17_LEN};
1336       size_t chunksize_in[NDIMS17];
1337       int storage_in;
1338       int d;
1339 
1340       /* Create a netcdf-4 file with two dims and one var. */
1341       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1342       if (nc_def_dim(ncid, DIM17_NAME, DIM17_LEN, &dimids[0])) ERR;
1343       if (nc_def_dim(ncid, DIM17_NAME_2, DIM17_2_LEN, &dimids[1])) ERR;
1344       if (dimids[0] != 0 || dimids[1] != 1) ERR;
1345       if (nc_def_var(ncid, VAR_NAME17, NC_UINT64, NDIMS17, dimids, &varid)) ERR;
1346       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, bad_chunksize) != NC_EBADCHUNK) ERR;
1347       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunksize)) ERR;
1348 
1349       /* Check stuff. */
1350       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1351       if (ndims != NDIMS17 || nvars != 1 || natts != 0 ||
1352           unlimdimid != -1) ERR;
1353       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1354       if (nvars != 1) ERR;
1355       if (varids_in[0] != 0) ERR;
1356       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1357       if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 ||
1358 	  dimids_in[0] != 0 || dimids_in[1] != 1) ERR;
1359       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1360       for (d = 0; d < NDIMS17; d++)
1361 	 if (chunksize[d] != chunksize_in[d]) ERR;
1362       if (storage_in != NC_CHUNKED) ERR;
1363       if (nc_close(ncid)) ERR;
1364 
1365       /* Open the file and check the same stuff. */
1366       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
1367 
1368       /* Check stuff. */
1369       if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
1370       if (ndims != NDIMS17 || nvars != 1 || natts != 0 ||
1371           unlimdimid != -1) ERR;
1372       if (nc_inq_varids(ncid, &nvars, varids_in)) ERR;
1373       if (nvars != 1) ERR;
1374       if (varids_in[0] != 0) ERR;
1375       if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims, dimids_in, &natts)) ERR;
1376       if (strcmp(name_in, VAR_NAME17) || xtype_in != NC_UINT64 || ndims != 2 || natts != 0 ||
1377 	  dimids_in[0] != 0 || dimids_in[1] != 1) ERR;
1378       if (nc_inq_var_chunking(ncid, 0, &storage_in, chunksize_in)) ERR;
1379       for (d = 0; d < NDIMS17; d++)
1380 	 if (chunksize[d] != chunksize_in[d]) ERR;
1381       if (storage_in != NC_CHUNKED) ERR;
1382       if (nc_close(ncid)) ERR;
1383    }
1384    SUMMARIZE_ERR;
1385 #define DIM8_NAME "num_monkeys"
1386 #define DIM9_NAME "num_coconuts"
1387 #define DIM9_LEN 10
1388 #define VAR_NAME8 "John_Clayton"
1389 #define VAR_NAME9 "Lord_Greystoke"
1390 #define VAR_NAME10 "Jane_Porter"
1391    printf("**** testing that contiguous storage can't be turned on for vars with unlimited dims or filters...");
1392    {
1393       int ncid;
1394       int dimids[NDIMS1];
1395       int varid, varid2;
1396       size_t chunksize_in[NDIMS1];
1397       int storage_in;
1398 
1399       /* Create a netcdf-4 file with one dim and some vars. */
1400       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1401       if (nc_def_dim(ncid, DIM8_NAME, NC_UNLIMITED, &dimids[0])) ERR;
1402       if (nc_def_var(ncid, VAR_NAME8, NC_INT, NDIMS1, dimids, &varid)) ERR;
1403       if (nc_def_dim(ncid, DIM9_NAME, DIM9_LEN, &dimids[0])) ERR;
1404       if (nc_def_var(ncid, VAR_NAME9, NC_INT, NDIMS1, dimids, &varid2)) ERR;
1405 
1406       /* These will fail due to bad parameters. */
1407       if (nc_def_var_deflate(ncid, varid2, 0, 1,
1408                              NC_MIN_DEFLATE_LEVEL - 1) != NC_EINVAL) ERR;
1409       if (nc_def_var_deflate(ncid, varid2, 0, 1,
1410                              NC_MAX_DEFLATE_LEVEL + 1) != NC_EINVAL) ERR;
1411 
1412       /* This will work. */
1413       if (nc_def_var_deflate(ncid, varid2, 0, 1, 4)) ERR;
1414 
1415       /* This won't work because of the umlimited dimension. */
1416       if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1417 
1418       /* This won't work because of the deflate filter. */
1419       if (nc_def_var_chunking(ncid, varid2, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1420 
1421       /* Storage must be chunked because of unlimited dimension and
1422        * the deflate filter. */
1423       if (nc_inq_var_chunking(ncid, varid, &storage_in, chunksize_in)) ERR;
1424       if (storage_in != NC_CHUNKED) ERR;
1425       if (nc_inq_var_chunking(ncid, varid2, &storage_in, chunksize_in)) ERR;
1426       if (storage_in != NC_CHUNKED) ERR;
1427       if (nc_close(ncid)) ERR;
1428 
1429    }
1430    SUMMARIZE_ERR;
1431    printf("**** testing error conditions on nc_def_var functions...");
1432    {
1433       int ncid;
1434       int dimids[NDIMS1];
1435       int bad_dimids[NDIMS1] = {42};
1436       int varid;
1437       int varid_scalar;
1438       int num_models = 2;
1439       int m;
1440       int mode = NC_NETCDF4;
1441 
1442       /* Test without and with classic model. */
1443       for (m = 0; m < num_models; m++)
1444       {
1445          int contiguous_in;
1446          size_t chunksizes_in[NDIMS1];
1447          int shuffle_in, deflate_in, deflate_level_in;
1448 
1449          if (m)
1450             mode |= NC_CLASSIC_MODEL;
1451 
1452          /* Create a netcdf-4 file. */
1453          if (nc_create(FILE_NAME, mode, &ncid)) ERR;
1454          if (nc_def_dim(ncid, DIM8_NAME, TEST_VAL_42, &dimids[0])) ERR;
1455 
1456          /* This won't work. */
1457          if (nc_def_var(ncid, VAR_NAME8, NC_INT, NDIMS1, bad_dimids,
1458                         &varid) != NC_EBADDIM) ERR;
1459 
1460          /* This will work. */
1461          if (nc_def_var(ncid, VAR_NAME8, NC_INT, NDIMS1, dimids, &varid)) ERR;
1462          if (nc_def_var(ncid, VAR_NAME10, NC_INT, 0, NULL, &varid_scalar)) ERR;
1463 
1464          /* Set the var to contiguous. */
1465          if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL)) ERR;
1466 
1467          /* Now defalte will change the var to chunked. */
1468          if (nc_def_var_deflate(ncid, varid, 0, 1, 4)) ERR;
1469          if (nc_inq_var_chunking(ncid, varid, &contiguous_in, chunksizes_in)) ERR;
1470          if (contiguous_in) ERR;
1471 
1472          /* Now I can't turn contiguous on, because deflate is on. */
1473          if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1474 
1475          /* Turn off deflation. */
1476          if (nc_def_var_deflate(ncid, varid, 0, 0, 0)) ERR;
1477          if (nc_inq_var_deflate(ncid, varid, &shuffle_in, &deflate_in, &deflate_level_in)) ERR;
1478          if (shuffle_in || deflate_in) ERR;
1479          if (nc_inq_var_deflate(ncid, varid, NULL, NULL, NULL)) ERR;
1480 
1481          /* Deflate is ignored for scalar. */
1482          if (nc_def_var_deflate(ncid, varid_scalar, 0, 1, 4)) ERR;
1483          if (nc_inq_var_deflate(ncid, varid, &shuffle_in, &deflate_in, &deflate_level_in)) ERR;
1484          if (shuffle_in || deflate_in) ERR;
1485 
1486          /* Turn on shuffle. */
1487          if (nc_def_var_deflate(ncid, varid, 1, 0, 0)) ERR;
1488 
1489          /* Now I can't turn contiguous on, because shuffle is on. */
1490          if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1491 
1492          /* Turn off shuffle. */
1493          if (nc_def_var_deflate(ncid, varid, 0, 0, 0)) ERR;
1494 
1495          /* Turn on fletcher32. */
1496          if (nc_def_var_fletcher32(ncid, varid, 1)) ERR;
1497 
1498          /* Now I can't turn contiguous on, because fletcher32 is on. */
1499          if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1500 
1501          /* Turn off fletcher32. */
1502          if (nc_def_var_fletcher32(ncid, varid, 0)) ERR;
1503 
1504          /* Now I can make it contiguous again. */
1505          if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL)) ERR;
1506          if (nc_close(ncid)) ERR;
1507       }
1508    }
1509    SUMMARIZE_ERR;
1510 #define DIM8_NAME "num_monkeys"
1511 #define DIM9_NAME "num_coconuts"
1512 #define DIM9_LEN 10
1513 #define VAR_NAME8 "John_Clayton"
1514 #define VAR_NAME9 "Lord_Greystoke"
1515    printf("**** testing that contiguous storage can't be turned on for vars with unlimited dims or filters...");
1516    {
1517       int ncid;
1518       int dimids[NDIMS1];
1519       int varid, varid2;
1520       size_t chunksize_in[NDIMS1];
1521       int storage_in;
1522 
1523       /* Create a netcdf-4 file with one dim and some vars. */
1524       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1525       if (nc_def_dim(ncid, DIM8_NAME, NC_UNLIMITED, &dimids[0])) ERR;
1526       if (nc_def_var(ncid, VAR_NAME8, NC_INT, NDIMS1, dimids, &varid)) ERR;
1527       if (nc_def_dim(ncid, DIM9_NAME, DIM9_LEN, &dimids[0])) ERR;
1528       if (nc_def_var(ncid, VAR_NAME9, NC_INT, NDIMS1, dimids, &varid2)) ERR;
1529       if (nc_def_var_deflate(ncid, varid2, 0, 1, 4)) ERR;
1530 
1531       /* This won't work because of the umlimited dimension. */
1532       if (nc_def_var_chunking(ncid, varid, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1533 
1534       /* This won't work because of the deflate filter. */
1535       if (nc_def_var_chunking(ncid, varid2, NC_CONTIGUOUS, NULL) != NC_EINVAL) ERR;
1536 
1537       /* Storage must be chunked because of unlimited dimension and
1538        * the deflate filter. */
1539       if (nc_inq_var_chunking(ncid, varid, &storage_in, chunksize_in)) ERR;
1540       if (storage_in != NC_CHUNKED) ERR;
1541       if (nc_inq_var_chunking(ncid, varid2, &storage_in, chunksize_in)) ERR;
1542       if (storage_in != NC_CHUNKED) ERR;
1543       if (nc_close(ncid)) ERR;
1544 
1545    }
1546    SUMMARIZE_ERR;
1547 #define DIM10_NAME "num_monkeys"
1548 #define DIM11_NAME "num_hats"
1549 #define VAR_NAME11 "Silly_Sally"
1550 #define NDIM2 2
1551    printf("**** testing very large chunksizes...");
1552    {
1553       int ncid;
1554       int dimid[NDIM2];
1555       int varid;
1556       size_t chunksize[NDIM2] = {1, (size_t)NC_MAX_INT + (size_t)1};
1557       size_t chunksize_in[NDIM2];
1558       int chunksize_int_in[NDIM2];
1559       int storage_in;
1560 
1561       /* Create a netcdf-4 file. */
1562       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
1563       if (nc_def_dim(ncid, DIM10_NAME, NC_UNLIMITED, &dimid[0])) ERR;
1564       if (nc_def_dim(ncid, DIM11_NAME, NC_UNLIMITED, &dimid[1])) ERR;
1565       if (nc_def_var(ncid, VAR_NAME11, NC_BYTE, NDIM2, dimid, &varid)) ERR;
1566       if (nc_inq_var_chunking(ncid, varid, &storage_in, chunksize_in)) ERR;
1567       if (storage_in != NC_CHUNKED) ERR;
1568 
1569       /* Set a large chunksize. */
1570       if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunksize)) ERR;
1571       if (nc_inq_var_chunking(ncid, varid, &storage_in, chunksize_in)) ERR;
1572       if (storage_in != NC_CHUNKED) ERR;
1573       if (chunksize_in[0] != chunksize[0] || chunksize_in[1] != chunksize[1]) ERR;
1574       if (nc_inq_var_chunking_ints(ncid, varid, &storage_in, chunksize_int_in) != NC_ERANGE) ERR;
1575 
1576       /* Close the file. */
1577       if (nc_close(ncid)) ERR;
1578 
1579    }
1580    SUMMARIZE_ERR;
1581    FINAL_RESULTS;
1582 }
1583