forked from Unidata/netcdf-c
/
tst_atts.c
428 lines (360 loc) · 15.2 KB
/
tst_atts.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* This is part of the netCDF package. Copyright 2006-2018 University
Corporation for Atmospheric Research/Unidata. See COPYRIGHT file
for conditions of use.
Test the netCDF-4 attribute code.
Ed Hartnett
*/
#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
#include "nc4internal.h"
/* The data file we will create. */
#define FILE_NAME "tst_atts.nc"
/* Names of attributes. */
#define OLD_NAME "Constantinople"
#define OLD_NAME_2 "Constantinopolis"
#define NEW_NAME "Istanbul"
/* Contents of attributes. */
#define CONTENTS "Lots of people!"
#define CONTENTS_2 "Lots of people!!" /* 1 longer than CONTENTS */
#define CONTENTS_3 "Lots 0f pe0ple!" /* same len as CONTENTS */
#define VAR_NAME "Earth"
/**
WARNING: following should match lists in libsrc4/nc4file.c
*/
/**
* @internal Define the names of attributes to ignore added by the
* HDF5 dimension scale; these attached to variables. They cannot be
* modified thru the netcdf-4 API.
*/
static const char* NC_RESERVED_VARATT_LIST[] = {
NC_ATT_REFERENCE_LIST,
NC_ATT_CLASS,
NC_ATT_DIMENSION_LIST,
NC_ATT_NAME,
NC_ATT_COORDINATES,
NC_DIMID_ATT_NAME,
NULL
};
/**
* @internal Define the names of attributes to ignore because they are
* "hidden" global attributes. They can be read, but not modified thru
* the netcdf-4 API.
*/
static const char* NC_RESERVED_ATT_LIST[] = {
NC_ATT_FORMAT,
NC3_STRICT_ATT_NAME,
NCPROPS,
ISNETCDF4ATT,
SUPERBLOCKATT,
NULL
};
/**
* @internal Define the subset of the reserved list that is readable
* by name only
*/
static const char* NC_RESERVED_SPECIAL_LIST[] = {
ISNETCDF4ATT,
SUPERBLOCKATT,
NCPROPS,
NULL
};
int
main(int argc, char **argv)
{
printf("\n*** Testing netCDF-4 attributes.\n");
printf("*** testing attribute renaming for read-only file...");
{
int ncid;
/* Create a file with an att. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file read-only. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
/* Try to rename the att, but it won't work. */
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, NEW_NAME) != NC_EPERM) ERR;
/* Try to create another att, it also won't work. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME_2, strlen(CONTENTS),
CONTENTS) != NC_EPERM) ERR;
/* Try to delete the att. More failure ensues. */
if (nc_del_att(ncid, NC_GLOBAL, OLD_NAME) != NC_EPERM) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing deleting atts...");
{
int ncid;
int natts;
/* Create a file with two atts. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME_2, 0, NULL)) ERR;
/* These will not work. */
if (nc_del_att(ncid + TEST_VAL_42, NC_GLOBAL, OLD_NAME) != NC_EBADID) ERR;
if (nc_del_att(ncid, TEST_VAL_42, OLD_NAME) != NC_ENOTVAR) ERR;
if (nc_del_att(ncid, NC_GLOBAL, NULL) != NC_EINVAL) ERR;
if (nc_del_att(ncid, NC_GLOBAL, NEW_NAME) != NC_ENOTATT) ERR;
/* End define mode. It redef will be called automatically. */
if (nc_enddef(ncid)) ERR;
/* Delete the attribute. */
if (nc_del_att(ncid, NC_GLOBAL, OLD_NAME)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file. */
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
if (nc_inq_natts(ncid, &natts)) ERR;
if (natts != 1) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing deleting atts classic model...");
{
int ncid;
int natts;
/* Create a file with an att. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
/* End define mode. */
if (nc_enddef(ncid)) ERR;
/* This will not work. */
if (nc_del_att(ncid, NC_GLOBAL, OLD_NAME) != NC_ENOTINDEFINE) ERR;
/* Delete the attribute. Redef is needed since this is a classic
* model file. */
if (nc_redef(ncid)) ERR;
if (nc_del_att(ncid, NC_GLOBAL, OLD_NAME)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file. */
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
if (nc_inq_natts(ncid, &natts)) ERR;
if (natts) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing over-writing atts classic model...");
{
int ncid;
int natts;
char *data_in;
if (!(data_in = malloc(strlen(CONTENTS) + 1))) ERR;
/* Create a file with an att. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLASSIC_MODEL, &ncid)) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
/* End define mode. */
if (nc_enddef(ncid)) ERR;
/* Try and write a new att. Won't work. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME_2, strlen(CONTENTS_2),
CONTENTS_2) != NC_EINDEFINE) ERR;
/* This will not work. Overwriting att must be same length or
* shorter if not in define mode. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS_2),
CONTENTS_2) != NC_EINDEFINE) ERR;
/* Now overwrite the att. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS_3),
CONTENTS_3)) ERR;
/* Delete the attribute. Redef is needed since this is a classic
* model file. This should work but does not. */
if (nc_redef(ncid)) ERR;
if (nc_del_att(ncid, NC_GLOBAL, OLD_NAME)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file. */
if (nc_open(FILE_NAME, 0, &ncid)) ERR;
if (nc_inq_natts(ncid, &natts)) ERR;
/* If delete worked, natts would be 0. */
/* if (natts != 0) ERR; */
if (natts != 1) ERR;
/* Get the attribute. */
if (nc_get_att_text(ncid, NC_GLOBAL, OLD_NAME, data_in)) ERR;
/* if (strncmp(CONTENTS_3, data_in, strlen(CONTENTS))) ERR; */
free(data_in);
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing attribute renaming for a global attribute...");
{
int ncid, attid;
char *data_in;
char too_long_name[NC_MAX_NAME + 2];
char name_in[NC_MAX_NAME + 1];
/* Set up a name that is too long for netCDF. */
memset(too_long_name, 'a', NC_MAX_NAME + 1);
too_long_name[NC_MAX_NAME + 1] = 0;
if (!(data_in = malloc(strlen(CONTENTS) + 1))) ERR;
/* Create a file with an att. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, 0, NULL, NULL)) ERR;
/* These will not work. */
if (nc_put_att_text(ncid + TEST_VAL_42, NC_GLOBAL, OLD_NAME,
strlen(CONTENTS), CONTENTS) != NC_EBADID) ERR;
if (nc_put_att_text(ncid, TEST_VAL_42, OLD_NAME, strlen(CONTENTS),
CONTENTS) != NC_ENOTVAR) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, NULL, strlen(CONTENTS),
CONTENTS) != NC_EBADNAME) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, BAD_NAME, strlen(CONTENTS),
CONTENTS) != NC_EBADNAME) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, too_long_name, strlen(CONTENTS),
CONTENTS) != NC_EBADNAME) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
NULL) != NC_EINVAL) ERR;
{
/* Check that the NC_GLOBAL reserved words are rejected. */
const char** reserved = NC_RESERVED_ATT_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, NC_GLOBAL, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
{
/* Check that the variable reserved words are rejected. */
const char** reserved = NC_RESERVED_VARATT_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, 0, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
{
/* Check that the read-only reserved words are rejected. */
const char** reserved = NC_RESERVED_SPECIAL_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, NC_GLOBAL, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
/* Write the attribute at last. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
/* Write another with different name. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME_2, strlen(CONTENTS),
CONTENTS)) ERR;
/* These will not work. */
if (nc_rename_att(ncid + TEST_VAL_42, NC_GLOBAL, OLD_NAME, NEW_NAME) != NC_EBADID) ERR;
if (nc_rename_att(ncid, TEST_VAL_42, OLD_NAME, NEW_NAME) != NC_ENOTVAR) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, NULL) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, NULL, NEW_NAME) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, NULL, NULL) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, BAD_NAME) != NC_EBADNAME) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, too_long_name) != NC_EMAXNAME) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, OLD_NAME_2) != NC_ENAMEINUSE) ERR;
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
NULL) != NC_EINVAL) ERR;
{
/* Check that the NC_GLOBAL reserved words are rejected. */
const char** reserved = NC_RESERVED_ATT_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, NC_GLOBAL, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
{
/* Check that the variable reserved words are rejected. */
const char** reserved = NC_RESERVED_VARATT_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, 0, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
{
/* Check that the read-only reserved words are rejected. */
const char** reserved = NC_RESERVED_SPECIAL_LIST;
for ( ; *reserved; reserved++)
{
if (nc_put_att_text(ncid, NC_GLOBAL, *reserved, strlen(CONTENTS),
CONTENTS) != NC_ENAMEINUSE) ERR;
}
}
/* Write the attribute at last. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME, strlen(CONTENTS),
CONTENTS)) ERR;
/* Write another with different name. */
if (nc_put_att_text(ncid, NC_GLOBAL, OLD_NAME_2, strlen(CONTENTS),
CONTENTS)) ERR;
/* These will not work. */
if (nc_rename_att(ncid + TEST_VAL_42, NC_GLOBAL, OLD_NAME, NEW_NAME) != NC_EBADID) ERR;
if (nc_rename_att(ncid, TEST_VAL_42, OLD_NAME, NEW_NAME) != NC_ENOTVAR) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, NULL) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, NULL, NEW_NAME) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, NULL, NULL) != NC_EINVAL) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, BAD_NAME) != NC_EBADNAME) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, too_long_name) != NC_EMAXNAME) ERR;
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, OLD_NAME_2) != NC_ENAMEINUSE) ERR;
/* Rename the att. */
if (nc_rename_att(ncid, NC_GLOBAL, OLD_NAME, NEW_NAME)) ERR;
/* These will not work. */
if (nc_inq_attid(ncid + TEST_VAL_42, NC_GLOBAL, NEW_NAME, &attid) != NC_EBADID) ERR;
if (nc_inq_attid(ncid, TEST_VAL_42, NEW_NAME, &attid) != NC_ENOTVAR) ERR;
if (nc_inq_attid(ncid, NC_GLOBAL, NULL, &attid) != NC_EBADNAME) ERR;
/* Check the file. */
if (nc_inq_attid(ncid, NC_GLOBAL, NEW_NAME, &attid)) ERR;
if (attid != 0) ERR;
/* This also works. */
if (nc_inq_attid(ncid, NC_GLOBAL, NEW_NAME, NULL)) ERR;
/* These won't work. */
if (nc_inq_attname(ncid + TEST_VAL_42, NC_GLOBAL, attid, name_in) != NC_EBADID) ERR;
if (nc_inq_attname(ncid, TEST_VAL_42, attid, name_in) != NC_ENOTVAR) ERR;
if (nc_inq_attname(ncid, NC_GLOBAL, -1, name_in) != NC_ENOTATT) ERR;
/* Get the name from the ID. */
if (nc_inq_attname(ncid, NC_GLOBAL, attid, name_in)) ERR;
if (strcmp(name_in, NEW_NAME)) ERR;
/* Also works but does little. */
if (nc_inq_attname(ncid, NC_GLOBAL, attid, NULL)) ERR;
/* These will not work. */
if (nc_get_att_text(ncid + TEST_VAL_42, NC_GLOBAL, NEW_NAME, data_in) != NC_EBADID) ERR;
if (nc_get_att_text(ncid, TEST_VAL_42, NEW_NAME, data_in) != NC_ENOTVAR) ERR;
if (nc_get_att_text(ncid, NC_GLOBAL, NULL, data_in) != NC_EBADNAME) ERR;
/* Get the attribute at last. */
if (nc_get_att_text(ncid, NC_GLOBAL, NEW_NAME, data_in)) ERR;
if (strncmp(CONTENTS, data_in, strlen(CONTENTS))) ERR;
/* This also works. */
if (nc_get_att_text(ncid, NC_GLOBAL, NEW_NAME, NULL)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and check again. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq_attid(ncid, NC_GLOBAL, NEW_NAME, &attid)) ERR;
if (attid != 0) ERR;
if (nc_get_att_text(ncid, NC_GLOBAL, NEW_NAME, data_in)) ERR;
if (strncmp(CONTENTS, data_in, strlen(CONTENTS))) ERR;
if (nc_close(ncid)) ERR;
free(data_in);
}
SUMMARIZE_ERR;
printf("*** testing attribute renaming for a variable attribute...");
{
#define OLD_NAME1 "Constantinople"
#define NEW_NAME1 "Istanbul____________"
#define CONTENTS1 "Lots of people!"
int ncid, attid, varid;
char *data_in;
if (!(data_in = malloc(strlen(CONTENTS1) + 1))) ERR;
/* Create a file with an att. */
if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, 0, NULL, &varid)) ERR;
if (nc_put_att_text(ncid, varid, OLD_NAME1, strlen(CONTENTS1),
CONTENTS1)) ERR;
/* Rename the att. */
if (nc_rename_att(ncid, varid, OLD_NAME1, NEW_NAME1)) ERR;
/* Check the file. */
if (nc_inq_attid(ncid, varid, NEW_NAME1, &attid)) ERR;
if (attid != 0) ERR;
if (nc_get_att_text(ncid, varid, NEW_NAME1, data_in)) ERR;
if (strncmp(CONTENTS1, data_in, strlen(CONTENTS1))) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and check again. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq_attid(ncid, varid, NEW_NAME1, &attid)) ERR;
if (attid != 0) ERR;
if (nc_get_att_text(ncid, varid, NEW_NAME1, data_in)) ERR;
if (strncmp(CONTENTS1, data_in, strlen(CONTENTS1))) ERR;
if (nc_close(ncid)) ERR;
free(data_in);
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}