1 #include "config.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <netcdf.h>
5 
6 #define ERR { \
7     if (err != NC_NOERR) { \
8         printf("Error at %s line %d: %s\n", __FILE__, __LINE__, \
9                nc_strerror(err)); \
10         nerrs++; \
11     } \
12 }
13 
14 #define EXP_ERR(exp) { \
15     if (err != exp) { \
16         printf("Error at %s:%d: expect %d but got %d\n", \
17                __FILE__, __LINE__, exp,err); \
18         nerrs++; \
19     } \
20 }
21 
22 static int default_format;
23 
24 static int
create_check(char * fname,int cmode,int exp_format)25 create_check(char *fname, int cmode, int exp_format)
26 {
27     int nerrs=0, err, exp_err=NC_NOERR, ncid, format;
28     char *exp_str;
29 
30     switch (exp_format) {
31         case NC_FORMAT_CLASSIC:      exp_str="NC_FORMAT_CLASSIC";      break;
32         case NC_FORMAT_64BIT_OFFSET: exp_str="NC_FORMAT_64BIT_OFFSET"; break;
33         case NC_FORMAT_64BIT_DATA:   exp_str="NC_FORMAT_64BIT_DATA";   break;
34         case NC_FORMAT_NETCDF4:      exp_str="NC_FORMAT_NETCDF4";      break;
35         case NC_FORMAT_NETCDF4_CLASSIC:
36                                      exp_str="NC_FORMAT_NETCDF4_CLASSIC";break;
37         default: break;
38     }
39 
40 #ifndef USE_NETCDF4
41     if (cmode & NC_NETCDF4)
42         exp_err = NC_ENOTBUILT;
43 #endif
44 #ifndef ENABLE_CDF5
45     if (cmode & NC_64BIT_DATA)
46         exp_err = NC_ENOTBUILT;
47 #endif
48 
49     /* create a file */
50     cmode |= NC_CLOBBER;
51     err = nc_create(fname, cmode, &ncid); EXP_ERR(exp_err)
52     if (err == NC_ENOTBUILT) return nerrs;
53 
54     err = nc_close(ncid); ERR
55 
56     /* open the file and check its format */
57     err = nc_open(fname, NC_NOWRITE, &ncid); ERR
58     err = nc_inq_format(ncid, &format); ERR
59     if (format != exp_format) {
60         char *f_str="", *d_str="";
61         switch (format) {
62             case NC_FORMAT_CLASSIC:      f_str = "NC_FORMAT_CLASSIC";
63                                          break;
64             case NC_FORMAT_64BIT_OFFSET: f_str = "NC_FORMAT_64BIT_OFFSET";
65                                          break;
66             case NC_FORMAT_64BIT_DATA:   f_str = "NC_FORMAT_64BIT_DATA";
67                                          break;
68             case NC_FORMAT_NETCDF4:      f_str = "NC_FORMAT_NETCDF4";
69                                          break;
70             case NC_FORMAT_NETCDF4_CLASSIC: f_str = "NC_FORMAT_NETCDF4_CLASSIC";
71                                          break;
72             default: break;
73         }
74         switch (default_format) {
75             case NC_FORMAT_CLASSIC:      d_str = "NC_FORMAT_CLASSIC";
76                                          break;
77             case NC_FORMAT_64BIT_OFFSET: d_str = "NC_FORMAT_64BIT_OFFSET";
78                                          break;
79             case NC_FORMAT_64BIT_DATA:   d_str = "NC_FORMAT_64BIT_DATA";
80                                          break;
81             case NC_FORMAT_NETCDF4:      d_str = "NC_FORMAT_NETCDF4";
82                                          break;
83             case NC_FORMAT_NETCDF4_CLASSIC: d_str = "NC_FORMAT_NETCDF4_CLASSIC";
84                                          break;
85             default: break;
86         }
87 
88         printf("Error at %s line %d: default is %s and expect %s but got %s\n",
89                __FILE__, __LINE__, d_str, exp_str, f_str);
90         nerrs++;
91     }
92     err = nc_close(ncid); ERR
93     return nerrs;
94 }
95 
main(int argc,char * argv[])96 int main(int argc, char *argv[])
97 {
98     char *fname="tst_default_format.nc";
99     int err, nerrs=0, ncid, cmode;
100 
101     if (argc == 2) fname = argv[1];
102 
103     default_format = NC_FORMAT_CLASSIC;
104 
105     /* check illegal cmode */
106     cmode = NC_64BIT_OFFSET | NC_64BIT_DATA;
107     err = nc_create(fname, cmode, &ncid);
108     if (err != NC_EINVAL) {
109         printf("Error at %s line %d: expect NC_EINVAL but got %d\n",
110                __FILE__, __LINE__, err);
111         nerrs++;
112     }
113 
114     /* check illegal cmode */
115     cmode = NC_NETCDF4 | NC_64BIT_OFFSET;
116     err = nc_create(fname, cmode, &ncid);
117     if (err != NC_EINVAL) {
118         printf("Error at %s line %d: expect NC_EINVAL but got %d\n",
119                __FILE__, __LINE__, err);
120         nerrs++;
121     }
122 
123     /* create a file in CDF1 format */
124     cmode = 0;
125     nerrs += create_check(fname, cmode, NC_FORMAT_CLASSIC);
126 
127     /* create a file in CDF2 format */
128     cmode = NC_64BIT_OFFSET;
129     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
130 
131     /* create a file in CDF5 format */
132     cmode = NC_64BIT_DATA;
133     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_DATA);
134 
135     /* set default file format to NC_FORMAT_64BIT_OFFSET ------------------*/
136     default_format = NC_FORMAT_64BIT_OFFSET;
137     err = nc_set_default_format(default_format, NULL); ERR
138 
139     /* create a file in default format */
140     cmode = 0;
141     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
142 
143     /* create a file in CDF5 format (this should ignore default) */
144     cmode = NC_64BIT_DATA;
145     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_DATA);
146 
147 #ifndef ENABLE_CDF5
148     err = nc_set_default_format(NC_FORMAT_64BIT_DATA, NULL); EXP_ERR(NC_ENOTBUILT)
149 #else
150     /* set default file format to NC_FORMAT_64BIT_DATA --------------------*/
151     default_format = NC_FORMAT_64BIT_DATA;
152     err = nc_set_default_format(default_format, NULL); ERR
153 
154     /* create a file in default format */
155     cmode = 0;
156     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_DATA);
157 
158     /* create a file in CDF2 format (this should ignore default) */
159     cmode = NC_64BIT_OFFSET;
160     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
161 #endif
162 
163 #ifndef USE_NETCDF4
164     err = nc_set_default_format(NC_FORMAT_NETCDF4, NULL); EXP_ERR(NC_ENOTBUILT)
165     err = nc_set_default_format(NC_FORMAT_NETCDF4_CLASSIC, NULL); EXP_ERR(NC_ENOTBUILT)
166     nerrs += create_check(fname, NC_NETCDF4, NC_FORMAT_NETCDF4);
167     nerrs += create_check(fname, NC_NETCDF4|NC_CLASSIC_MODEL, NC_FORMAT_NETCDF4_CLASSIC);
168 #else
169     /* set default file format to NC_FORMAT_NETCDF4 -----------------------*/
170     default_format = NC_FORMAT_NETCDF4;
171     err = nc_set_default_format(default_format, NULL); ERR
172 
173     /* create a file in default format */
174     cmode = 0;
175     nerrs += create_check(fname, cmode, NC_FORMAT_NETCDF4);
176 
177     /* create a file in CDF2 format (this should ignore default) */
178     cmode = NC_64BIT_OFFSET;
179     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
180 
181     /* set default file format to NC_FORMAT_NETCDF4_CLASSIC ---------------*/
182     default_format = NC_FORMAT_NETCDF4_CLASSIC;
183     err = nc_set_default_format(default_format, NULL); ERR
184 
185     /* create a file in default format */
186     cmode = 0;
187     nerrs += create_check(fname, cmode, NC_FORMAT_NETCDF4_CLASSIC);
188 
189     /* create a file in NETCDF4 format (this should ignore default) */
190     cmode = NC_NETCDF4;
191     nerrs += create_check(fname, cmode, NC_FORMAT_NETCDF4);
192 
193     /* create a file in CDF2 format (this should ignore default) */
194     cmode = NC_64BIT_OFFSET;
195     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
196 
197     /* set default file format to NC_FORMAT_NETCDF4 -----------------------*/
198     default_format = NC_FORMAT_NETCDF4;
199     err = nc_set_default_format(default_format, NULL); ERR
200 
201     /* create a file in default format */
202     cmode = 0;
203     nerrs += create_check(fname, cmode, NC_FORMAT_NETCDF4);
204 
205     /* create a file in NETCDF4 format (this should ignore default) */
206     cmode = NC_NETCDF4 | NC_CLASSIC_MODEL;
207     nerrs += create_check(fname, cmode, NC_FORMAT_NETCDF4_CLASSIC);
208 
209     /* create a file in CDF2 format (this should ignore default) */
210     cmode = NC_64BIT_OFFSET;
211     nerrs += create_check(fname, cmode, NC_FORMAT_64BIT_OFFSET);
212 #endif
213 
214     return (nerrs > 0);
215 }
216