1 /* This is part of the netCDF package.
2 Copyright 2018 University Corporation for Atmospheric Research/Unidata
3 See COPYRIGHT file for conditions of use.
4
5 Test dim rename that is causing problems with v2 API.
6 */
7
8 #include <nc_tests.h>
9 #include "err_macros.h"
10
11 #define FILE_NAME "tst_rename.nc"
12
13 void
check_err(const int stat,const int line,const char * file)14 check_err(const int stat, const int line, const char *file) {
15 if (stat != NC_NOERR) {
16 (void)fprintf(stderr,"line %d of %s: %s\n", line, file, nc_strerror(stat));
17 fflush(stderr);
18 exit(1);
19 }
20 }
21
22 int
create_file()23 create_file()
24 {
25 int stat; /* return status */
26 int ncid; /* netCDF id */
27
28 /* dimension ids */
29 int ii_dim;
30 int jj_dim;
31 int kk_dim;
32 int i1_dim;
33 int i2_dim;
34 int i3_dim;
35 int rec_dim;
36 int ll_dim;
37 int mm_dim;
38 int nn_dim;
39
40 /* dimension lengths */
41 size_t ii_len = 4;
42 size_t jj_len = 3;
43 size_t kk_len = 3;
44 size_t i1_len = 5;
45 size_t i2_len = 3;
46 size_t i3_len = 7;
47 size_t rec_len = NC_UNLIMITED;
48 size_t ll_len = 3;
49 size_t mm_len = 1;
50 size_t nn_len = 1;
51
52 /* variable ids */
53 int aa_id;
54 int bb_id;
55 int cc_id;
56 int cd_id;
57 int ce_id;
58 int dd_id;
59
60 /* rank (number of dimensions) for each variable */
61 # define RANK_aa 1
62 # define RANK_bb 2
63 # define RANK_cc 1
64 # define RANK_cd 2
65 # define RANK_ce 3
66 # define RANK_dd 1
67
68 /* variable shapes */
69 int aa_dims[RANK_aa];
70 int bb_dims[RANK_bb];
71 int cc_dims[RANK_cc];
72 int cd_dims[RANK_cd];
73 int ce_dims[RANK_ce];
74 int dd_dims[RANK_dd];
75
76 /* enter define mode */
77 stat = nc_create(FILE_NAME, NC_CLOBBER, &ncid);
78 check_err(stat,__LINE__,__FILE__);
79
80 /* define dimensions */
81 stat = nc_def_dim(ncid, "ii", ii_len, &ii_dim);
82 check_err(stat,__LINE__,__FILE__);
83 stat = nc_def_dim(ncid, "jj", jj_len, &jj_dim);
84 check_err(stat,__LINE__,__FILE__);
85 stat = nc_def_dim(ncid, "kk", kk_len, &kk_dim);
86 check_err(stat,__LINE__,__FILE__);
87 stat = nc_def_dim(ncid, "i1", i1_len, &i1_dim);
88 check_err(stat,__LINE__,__FILE__);
89 stat = nc_def_dim(ncid, "i2", i2_len, &i2_dim);
90 check_err(stat,__LINE__,__FILE__);
91 stat = nc_def_dim(ncid, "i3", i3_len, &i3_dim);
92 check_err(stat,__LINE__,__FILE__);
93 stat = nc_def_dim(ncid, "rec", rec_len, &rec_dim);
94 check_err(stat,__LINE__,__FILE__);
95 stat = nc_def_dim(ncid, "ll", ll_len, &ll_dim);
96 check_err(stat,__LINE__,__FILE__);
97 stat = nc_def_dim(ncid, "mm", mm_len, &mm_dim);
98 check_err(stat,__LINE__,__FILE__);
99 stat = nc_def_dim(ncid, "nn", nn_len, &nn_dim);
100 check_err(stat,__LINE__,__FILE__);
101
102 /* define variables */
103
104 aa_dims[0] = ii_dim;
105 stat = nc_def_var(ncid, "aa", NC_INT, RANK_aa, aa_dims, &aa_id);
106 check_err(stat,__LINE__,__FILE__);
107
108 bb_dims[0] = kk_dim;
109 bb_dims[1] = jj_dim;
110 stat = nc_def_var(ncid, "bb", NC_INT, RANK_bb, bb_dims, &bb_id);
111 check_err(stat,__LINE__,__FILE__);
112
113 cc_dims[0] = rec_dim;
114 stat = nc_def_var(ncid, "cc", NC_INT, RANK_cc, cc_dims, &cc_id);
115 check_err(stat,__LINE__,__FILE__);
116
117 cd_dims[0] = rec_dim;
118 cd_dims[1] = i2_dim;
119 stat = nc_def_var(ncid, "cd", NC_SHORT, RANK_cd, cd_dims, &cd_id);
120 check_err(stat,__LINE__,__FILE__);
121
122 ce_dims[0] = rec_dim;
123 ce_dims[1] = i2_dim;
124 ce_dims[2] = i3_dim;
125 stat = nc_def_var(ncid, "ce", NC_FLOAT, RANK_ce, ce_dims, &ce_id);
126 check_err(stat,__LINE__,__FILE__);
127
128 dd_dims[0] = ll_dim;
129 stat = nc_def_var(ncid, "dd", NC_SHORT, RANK_dd, dd_dims, &dd_id);
130 check_err(stat,__LINE__,__FILE__);
131
132 /* assign global attributes */
133 { /* title */
134 stat = nc_put_att_text(ncid, NC_GLOBAL, "title", 11, "test netcdf");
135 check_err(stat,__LINE__,__FILE__);
136 }
137
138
139 /* assign per-variable attributes */
140 { /* units */
141 stat = nc_put_att_text(ncid, aa_id, "units", 8, "furlongs");
142 check_err(stat,__LINE__,__FILE__);
143 }
144 { /* valid_range */
145 static const float bb_valid_range_att[2] = {0, 100} ;
146 stat = nc_put_att_float(ncid, bb_id, "valid_range", NC_FLOAT, 2, bb_valid_range_att);
147 check_err(stat,__LINE__,__FILE__);
148 }
149 { /* units */
150 stat = nc_put_att_text(ncid, cc_id, "units", 5, "moles");
151 check_err(stat,__LINE__,__FILE__);
152 }
153 { /* units */
154 stat = nc_put_att_text(ncid, cd_id, "units", 5, "moles");
155 check_err(stat,__LINE__,__FILE__);
156 }
157 { /* units */
158 stat = nc_put_att_text(ncid, ce_id, "units", 5, "moles");
159 check_err(stat,__LINE__,__FILE__);
160 }
161 { /* fill_value */
162 static const short dd_fill_value_att[1] = {-999} ;
163 stat = nc_put_att_short(ncid, dd_id, "fill_value", NC_SHORT, 1, dd_fill_value_att);
164 check_err(stat,__LINE__,__FILE__);
165 }
166
167
168 /* leave define mode */
169 stat = nc_enddef (ncid);
170 check_err(stat,__LINE__,__FILE__);
171
172 /* assign variable data */
173 {
174 int aa_data[4] = {-2147483647, -2147483647, -2147483647, -2147483647} ;
175 size_t aa_startset[1] = {0} ;
176 size_t aa_countset[1] = {4} ;
177 stat = nc_put_vara(ncid, aa_id, aa_startset, aa_countset, aa_data);
178 check_err(stat,__LINE__,__FILE__);
179 }
180
181 {
182 int bb_data[9] = {-2147483647, -2147483647, -2147483647, -2147483647, -2147483647, -2147483647, -2147483647, -2147483647, -2147483647} ;
183 size_t bb_startset[2] = {0, 0} ;
184 size_t bb_countset[2] = {3, 3} ;
185 stat = nc_put_vara(ncid, bb_id, bb_startset, bb_countset, bb_data);
186 check_err(stat,__LINE__,__FILE__);
187 }
188
189 {
190 short dd_data[3] = {1, 2, -32767} ;
191 size_t dd_startset[1] = {0} ;
192 size_t dd_countset[1] = {3} ;
193 stat = nc_put_vara(ncid, dd_id, dd_startset, dd_countset, dd_data);
194 check_err(stat,__LINE__,__FILE__);
195 }
196
197
198 stat = nc_close(ncid);
199 check_err(stat,__LINE__,__FILE__);
200 return 0;
201 }
202
203 int
main(int argc,char ** argv)204 main(int argc, char **argv)
205 {
206 printf("\n*** Testing v3/v4 API versions of some v2 tests.\n");
207 printf("*** testing simple dim rename...");
208 {
209 #define PP1 "pp"
210 #define PP1_SIZE 7
211 #define P1_NAME "p"
212
213 int ncid, pp_dimid, dimid_in;
214
215 /* Create a file with one dimension. */
216 if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR;
217 if (nc_def_dim(ncid, PP1, PP1_SIZE, &pp_dimid)) ERR;
218
219 /* Renaming to shorter name is possible in data mode... */
220 if (nc_enddef(ncid)) ERR;
221 if (nc_rename_dim(ncid, pp_dimid, P1_NAME)) ERR;
222 if (nc_inq_dimid(ncid, P1_NAME, &dimid_in)) ERR;
223 if (dimid_in != pp_dimid) ERR;
224 if (nc_close(ncid)) ERR;
225 }
226 SUMMARIZE_ERR;
227 printf("*** testing dim rename from nctest...");
228 {
229 #define PP "pp"
230 #define PP_SIZE 7
231 #define QQ "qq"
232 #define QQ_SIZE 10
233 #define NEW_NAME "new_name"
234 #define ANOTHER_NAME "another_name"
235 #define P_NAME "p"
236
237 int ncid, pp_dimid, qq_dimid, dimid_in;
238 char name_in[NC_MAX_NAME + 1];
239
240 /* Create the same file as nctest.c does. */
241 if (create_file()) ERR;
242
243 /* Open it and test renames of dimensions. */
244 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
245 if (nc_redef(ncid)) ERR;
246 if (nc_def_dim(ncid, PP, PP_SIZE, &pp_dimid)) ERR;
247 if (nc_def_dim(ncid, QQ, QQ_SIZE, &qq_dimid)) ERR;
248 if (nc_rename_dim(ncid, pp_dimid, NEW_NAME)) ERR;
249 if (nc_inq_dimname(ncid, pp_dimid, name_in)) ERR;
250 if (strcmp(NEW_NAME, name_in)) ERR;
251 if (nc_rename_dim(ncid, pp_dimid, QQ) != NC_ENAMEINUSE) ERR;
252 if (nc_rename_dim(ncid, -1, ANOTHER_NAME) != NC_EBADDIM) ERR;
253 if (nc_rename_dim(ncid, 12, ANOTHER_NAME) != NC_EBADDIM) ERR;
254 if (nc_enddef(ncid)) ERR;
255 if (nc_rename_dim(ncid, pp_dimid, P_NAME)) ERR;
256 if (nc_inq_dimid(ncid, P_NAME, &dimid_in)) ERR;
257 if (dimid_in != pp_dimid) ERR;
258 if (nc_inq_dimid(ncid, P_NAME, NULL)) ERR;
259 if (nc_close(ncid)) ERR;
260 }
261 SUMMARIZE_ERR;
262 FINAL_RESULTS;
263 }
264