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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
|
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctst/atttests.c,v 1.18 2006/10/31 16:21:45 ed Exp $
*********************************************************************/
#ifdef _MPW
#define __SEG__ toobig /* under MPW on MacOS, makes it fit */
#endif
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for free() */
#include "netcdf.h"
#include "testcdf.h" /* defines in-memory test cdf structure */
#include "add.h" /* functions to update in-memory netcdf */
#include "error.h"
#include "emalloc.h"
#include "tests.h"
#include "val.h"
#define LEN_OF(array) ((sizeof array) / (sizeof array[0]))
/*
* Test ncattput
* check that new attribute put works in define mode
* check that NC_GLOBAL variable id works
* check that changing type of existing attribute works in define mode
* check that increasing length of attribute works in define mode
* check that changing value of existing attribute works in define mode
* try with bad datatype, should fail
* try with negative length, should fail
* try increasing length of attribute in data mode, should fail
* try putting new attribute in data mode, should fail
* check that changing type of existing attribute works in data mode
* check that decreasing length of attribute works in data mode
* check that changing value of existing attribute works in data mode
* try with bad variable handle, should fail
* try with bad netCDF handle, check error
*/
int
test_ncattput(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattput";
int cdfid; /* netcdf id */
int ndims; /* number of dimensions */
int nvars; /* number of variables */
int ngatts_prev, ngatts; /* number of global attributes */
int xdimid; /* id of unlimited dimension */
int ia, id;
static char byte_vals[] = {'a', 'b'};
static char char_vals[] = "chars";
static short short_vals[] = {-999, 0, 999};
static nclong long_vals[] = {10, 20};
static float float_vals[] = {1.5, 2.5, 3.5 };
static double double_vals[] = {4.5, 5.5, 6.5, 7.5};
/*
* test attributes; it is important for this test that the size
* required for the attribute values increases monotonically.
*/
static struct cdfatt atts[] = {
{___, "att0", NC_BYTE, LEN_OF(byte_vals), (void *) byte_vals},
{___, "att1", NC_CHAR, LEN_OF(char_vals), (void *) char_vals},
{___, "att2", NC_SHORT, LEN_OF(short_vals), (void *) short_vals},
{___, "att3", NC_LONG, LEN_OF(long_vals), (void *) long_vals},
{___, "att4", NC_FLOAT, LEN_OF(float_vals), (void *) float_vals},
{___, "att5", NC_DOUBLE, LEN_OF(double_vals), (void *) double_vals}
};
int na = LEN_OF(atts); /* number of test attributes */
int ww_id; /* variable id */
static struct cdfvar ww = /* new variable */
{"ww", NC_LONG, 1, ___, 0};
static struct cdfatt tmp; /* attribute */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* get count of global attributes */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
ngatts_prev = ngatts;
/* in define mode, add global attributes of every type */
for (ia = 0; ia < na; ia++) {
if (ncattput(cdfid, NC_GLOBAL, atts[ia].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of NC_GLOBAL attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &atts[ia]); /* keep in-memory netcdf updated */
}
/* make sure count of global attributes has been updated */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ngatts != ngatts_prev + na) {
error("%s: number of global = %d, expected %d",
pname, ngatts, ngatts_prev + na);
nerrs++;
}
/* check with ncattinq and ncattget that NC_GLOBAL attributes put OK */
for (ia = 0; ia < na; ia++) {
if (ncattinq(cdfid, NC_GLOBAL, atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of global attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: NC_GLOBAL ncattinq got unexpected type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, NC_GLOBAL, atts[ia].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of NC_GLOBAL attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/* add a variable, then variable attributes of every type */
ww.dims = (int *) emalloc(sizeof(int) * ww.ndims);
for (id = 0; id < ww.ndims; id++)
ww.dims[id] = id;
if ((ww_id = ncvardef(cdfid,
ww.name, ww.type, ww.ndims, ww.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &ww); /* keep in-memory netcdf in sync */
for (ia = 0; ia < na; ia++) {
if (ncattput(cdfid, ww_id,
atts[ia].name, atts[ia].type, atts[ia].len, atts[ia].val)
== -1) {
error("%s: ncattput of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, ww_id, &atts[ia]); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 0; ia < na; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: ncattinq for new attribute got bad type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/*
* check that changing type of existing attribute, increasing
* length of attribute, and changing value of existing attribute
* work OK in define mode.
*/
tmp.name = (char *) emalloc(MAX_NC_NAME);
for (ia = 1; ia < na; ia++) {
if (ncattput(cdfid, ww_id, atts[ia-1].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of larger attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
tmp.var = atts[ia].var;
(void) strcpy (tmp.name, atts[ia-1].name);
tmp.type = atts[ia].type;
tmp.len = atts[ia].len;
tmp.val = atts[ia].val;
add_att(&test, ww_id, &tmp); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 1; ia < na; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia-1].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of larger attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: ncattinq for larger attribute got bad type or len",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia-1].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values after put of larger attrs",
pname);
nerrs++;
}
free (tmp.val);
}
/* try with bad datatype, should fail */
if (ncattput(cdfid, ww_id, "bogus_att1", BAD_TYPE,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput should fail with bad type", pname);
nerrs++;
}
/* try with negative length, should fail */
if (ncattput(cdfid, ww_id, "bogus_att2", atts[0].type,
-1, atts[0].val) != -1) {
error("%s: ncattput should fail with bad length", pname);
nerrs++;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode try increasing length of attribute, should fail */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type,
atts[0].len + 10, atts[0].val) != -1) {
error("%s: ncattput should fail with increased length in data mode",
pname);
nerrs++;
/* reset to correct length for later tests */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput failed to reset length in data mode", pname);
nerrs++;
}
}
/* try creating new attribute in data mode, should fail */
if (ncattput(cdfid, ww_id, "new_name", atts[0].type,
atts[0].len, atts[0].val) != -1) {
error("%s: ncattput of new attribute in data mode should fail",
pname);
ncclose(cdfid); return ++nerrs;
}
/*
* check that changing type of existing attribute, decreasing
* length of attribute, and changing value of existing attribute
* work OK in data mode
*/
for (ia = 0; ia < na - 1; ia++) {
if (ncattput(cdfid, ww_id, atts[ia+1].name, atts[ia].type,
atts[ia].len, atts[ia].val) == -1) {
error("%s: ncattput of smaller attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
tmp.var = atts[ia].var;
(void) strcpy (tmp.name, atts[ia+1].name);
tmp.type = atts[ia].type;
tmp.len = atts[ia].len;
tmp.val = atts[ia].val;
add_att(&test, ww_id, &tmp); /* keep in-memory netcdf updated */
}
/* check with ncattinq and ncattget that variable attributes put OK */
for (ia = 0; ia < na - 1; ia++) {
if (ncattinq(cdfid, ww_id, atts[ia+1].name, &tmp.type, &tmp.len)
== -1) {
error("%s: ncattinq of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (atts[ia].type != tmp.type || atts[ia].len != tmp.len) {
error("%s: VARIABLE ncattinq got bad type or len in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atts[ia].len * nctypelen(atts[ia].type));
if (ncattget(cdfid, ww_id, atts[ia+1].name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atts[ia].val) != 0) {
error("%s: ncattget got bad values in data mode", pname);
nerrs++;
}
free (tmp.val);
}
/* try with bad variable handle, should fail */
if (ncattput(cdfid, test.nvars, atts[0].name, atts[0].type, atts[0].len,
atts[0].val) != -1) {
error("%s: ncattput should fail with bad variable handle", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try with bad netcdf handle, should fail */
if (ncattput(cdfid, ww_id, atts[0].name, atts[0].type, atts[0].len,
atts[0].val) != -1) {
error("%s: ncattput should fail with bad netcdf handle", pname);
ncclose(cdfid); return ++nerrs;
}
free(tmp.name);
free(ww.dims);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncattinq
* check returned values of properly created attributes
* try with nonexisting attribute, check error
* try with bad variable handle, check error
* try with bad netCDF handle, check error
*/
int
test_ncattinq(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattinq";
int cdfid; /* netcdf id */
int ia, id; /* attribute number */
nc_type type;
int len;
int vv_id; /* variable id */
static struct cdfvar vv = /* new variable */
{"vv", NC_SHORT, 2, ___, 0};
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* in data mode, check all attributes against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&type, &len) == -1) {
error("%s: ncattinq failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (type != test.atts[ia].type) {
error("%s: ncattinq returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (len != test.atts[ia].len) {
error("%s: ncattinq returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add a variable */
vv.dims = (int *) emalloc(sizeof(int) * vv.ndims);
for (id = 0; id < vv.ndims; id++)
vv.dims[id] = id; /* assumes vv.ndims <= test.ndims */
if ((vv_id = ncvardef(cdfid, vv.name, vv.type, vv.ndims, vv.dims))
== -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &vv); /* keep in-memory netcdf in sync */
/* try with nonexisting attribute, should fail */
if (ncattinq(cdfid, vv_id, "nonesuch", &type, &len) != -1) {
error("%s: ncattinq should fail with nonexisting attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad variable handle, should fail */
if (ncattinq(cdfid, test.nvars, test.atts[0].name, &type, &len) != -1) {
error("%s: ncattinq should fail with bad variable id", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode check all attributes against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&type, &len) == -1) {
error("%s: ncattinq in define mode failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (type != test.atts[ia].type) {
error("%s: ncattinq in define mode returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (len != test.atts[ia].len) {
error("%s: ncattinq in define mode returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
if (ncattinq(cdfid, NC_GLOBAL, test.atts[0].name, &type, &len) != -1) {
error("%s: ncattinq should fail with bad cdfid", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(vv.dims);
return nerrs;
}
/*
* Test ncattget
* check that NC_GLOBAL variable id works
* check in both modes
* check that proper call worked after ncattput
* try with bad variable handle, check error
* try with nonexisting attribute, check error
* try with bad netCDF handle, check error
*/
int
test_ncattget(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
int cdfid; /* netcdf id */
int ia, id;
static struct cdfatt tmp; /* attribute */
int uu_id; /* variable id */
static struct cdfvar uu = /* variable */
{"uu", NC_LONG, 2, ___, 0};
static nclong uumax = 1000; /* attribute value */
static struct cdfatt vmax = /* attribute */
{___, "valid_max", NC_LONG, 1, (void *) &uumax};
static char pname[] = "test_ncattget";
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* add a variable */
uu.dims = (int *) emalloc(sizeof(int) * uu.ndims);
for (id = 0; id < uu.ndims; id++)
uu.dims[id] = id;
if ((uu_id = ncvardef(cdfid,
uu.name, uu.type, uu.ndims, uu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &uu); /* keep in-memory netcdf in sync */
/* add an attribute */
if (ncattput(cdfid, uu_id,
vmax.name, vmax.type, vmax.len, vmax.val)
== -1) {
error("%s: ncattput of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, uu_id, &vmax); /* keep in-memory netcdf updated */
/* in define mode, check all attributes values against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq in define mode failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (tmp.type != test.atts[ia].type) {
error("%s: ncattinq in define mode returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.len != test.atts[ia].len) {
error("%s: ncattinq in define mode returned wrong len", pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(tmp.len * nctypelen(tmp.type));
if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val)
== -1) {
error("%s: ncattget of variable attribute failed in define mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, test.atts[ia].val) != 0) {
error("%s: ncattget got bad values in define mode", pname);
error(" cdfid=%d, varname=%s, attname=%s, type=%d, len=%d",
cdfid, test.vars[test.atts[ia].var].name,
test.atts[ia].name, test.atts[ia].type, test.atts[ia].len);
(void)fprintf(stderr,"should have got:");
val_out(test.atts[ia].type, test.atts[ia].len,
test.atts[ia].val);
(void)fprintf(stderr," instead got:");
val_out(tmp.type, tmp.len, tmp.val);
nerrs++;
}
free (tmp.val);
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode, check all attributes values against test netcdf */
for (ia = 0; ia < test.natts; ia++) {
if (ncattinq(cdfid, test.atts[ia].var, test.atts[ia].name,
&tmp.type, &tmp.len) == -1) {
error("%s: ncattinq failed", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.type != test.atts[ia].type) {
error("%s: ncattinq returned wrong type", pname);
ncclose(cdfid);
return ++nerrs;
}
if (tmp.len != test.atts[ia].len) {
error("%s: ncattinq returned wrong len", pname);
ncclose(cdfid);
return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(tmp.len * nctypelen(tmp.type));
if (ncattget(cdfid, test.atts[ia].var, test.atts[ia].name, tmp.val)
== -1) {
error("%s: ncattget of variable attribute failed in data mode",
pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, test.atts[ia].val) != 0) {
error("%s: ncattget got bad values in data mode", pname);
error(" cdfid=%d, varname=%s, attname=%s, type=%d, len=%d",
cdfid, test.vars[test.atts[ia].var].name,
test.atts[ia].name, test.atts[ia].type, test.atts[ia].len);
(void)fprintf(stderr,"should have got:");
val_out(test.atts[ia].type, test.atts[ia].len,
test.atts[ia].val);
(void)fprintf(stderr," instead got:");
val_out(tmp.type, tmp.len, tmp.val);
nerrs++;
}
free (tmp.val);
}
/* try with bad variable handle, should fail */
if (ncattget(cdfid, test.nvars, vmax.name, vmax.val) != -1) {
error("%s: ncattget should fail with bad variable handle", pname);
ncclose(cdfid); return ++nerrs;
}
/* try getting non-existent attribute, should fail */
if (ncattget(cdfid, uu_id, "nonesuch", vmax.val) != -1) {
error("%s: ncattget should fail with nonexistent attribute", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try with bad netcdf handle, should fail */
if (ncattget(cdfid, uu_id, vmax.name, vmax.val) != -1) {
error("%s: ncattput should fail with bad netcdf handle", pname);
ncclose(cdfid); return ++nerrs;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(uu.dims);
return nerrs;
}
/*
* Test ncattcopy
* check that NC_GLOBAL variable for source or target works
* check that new attribute put works with target in define mode
* check that old attribute put works with target in data mode
* check that changing type and length of an attribute work OK
* try with same cdfid for source and target, different variables
* try with same cdfid for source and target, same variable
* try with nonexisting attribute, check error
* try with bad source or target netCDF handles, check error
* try with bad source or target variable handle, check error
*/
int
test_ncattcopy(path1, path2)
const char *path1; /* name of input netcdf file to open */
const char *path2; /* name of output netcdf file to create */
{
int nerrs = 0;
static char pname[] = "test_ncattcopy";
int cdfid, cdfid2; /* netcdf id */
int id; /* dimension id */
int tt_id; /* variable id */
static struct cdfvar tt = /* new variable for source netcdf */
{"tt", NC_LONG, 1, ___, 0};
int tu_id, tu2_id; /* variable ids */
static struct cdfvar tu = /* new variable for target netcdf */
{"tu", NC_DOUBLE, 2, ___, 0};
static double double_vals[] = {-1., -2.};
static float float_vals[] = {-1., -2.};
static struct cdfatt att = /* attribute */
{___, "att", NC_DOUBLE, LEN_OF(double_vals), (void *) double_vals};
static struct cdfatt att2 = /* attribute */
{___, "att", NC_FLOAT, LEN_OF(float_vals), (void *) float_vals};
static struct cdfatt tmp; /* attribute */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path1, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed on source", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add a global attribute, a variable and an attribute */
if (ncattput(cdfid, NC_GLOBAL, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att); /* keep in-memory netcdf consistent */
tt.dims = (int *) emalloc(sizeof(int) * tt.ndims);
for (id=0; id < tt.ndims; id++)
tt.dims[0] = id;
if ((tt_id=ncvardef(cdfid, tt.name, tt.type, tt.ndims, tt.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &tt); /* keep in-memory netcdf consistent */
if (ncattput(cdfid, tt_id, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, tt_id, &att); /* keep in-memory netcdf consistent */
tu.dims = (int *) emalloc(sizeof(int) * tu.ndims);
for (id = 0; id < tu.ndims; id++)
tu.dims[id] = id;
if ((tu_id=ncvardef(cdfid, tu.name, tu.type, tu.ndims, tu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &tu); /* keep in-memory netcdf consistent */
if (ncattput(cdfid, tu_id, att.name, att.type, att.len, att.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, tu_id, &att); /* keep in-memory netcdf consistent */
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* first (source) netcdf is in data mode */
/* create second netCDF to copy attributes to */
if ((cdfid2 = nccreate(path2, NC_CLOBBER)) == -1) {
error("%s: nccreate failed", pname);
return ++nerrs;
}
/* create dimensions and variable in second netcdf */
for (id = 0; id < tu.ndims; id++) { /* copy dimensions from source */
if ((tu.dims[id] =ncdimdef(cdfid2, test.dims[id].name,
test.dims[id].size)) == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
}
if ((tu2_id=ncvardef(cdfid2, tu.name, tu.type, tu.ndims, tu.dims)) == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try copying NC_GLOBAL attribute from source to target */
if (ncattcopy(cdfid, NC_GLOBAL, att.name, cdfid2, NC_GLOBAL) == -1) {
error("%s: ncattcopy on NC_GLOBAL attribute '%s' failed",
pname, att.name);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, NC_GLOBAL, att.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att.type != tmp.type || att.len != tmp.len) {
error("%s: NC_GLOBAL ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att.len * nctypelen(att.type));
if (ncattget(cdfid2, NC_GLOBAL, att.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att.val) != 0) {
error("%s: ncattget got bad values after put of NC_GLOBAL attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try copying variable attribute from source to target */
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu2_id) == -1) {
error("%s: ncattcopy failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, tu2_id, att.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att.type != tmp.type || att.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att.len * nctypelen(att.type));
if (ncattget(cdfid2, tu2_id, att.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/*
* check that old attribute put works with target in data mode,
* also checks that changing type and length of an attribute works OK
*/
if (ncendef (cdfid2) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* change attribute to shorter attribute */
if (ncattput(cdfid, NC_GLOBAL, att2.name, att2.type, att2.len, att2.val)
== -1) {
error("%s: ncattput of shorter NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att2); /* keep in-memory netcdf consistent */
/* copy shorter attribute on existing attribute */
if (ncattcopy(cdfid, NC_GLOBAL, att2.name, cdfid2, tu2_id) == -1) {
error("%s: ncattcopy of shorter attribute on old attribute failed",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid2, tu2_id, att2.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att2.type != tmp.type || att2.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att2.len * nctypelen(att2.type));
if (ncattget(cdfid2, tu2_id, att2.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att2.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try copying with same source and target netcdf, different variables */
/* copy shorter attribute on existing attribute */
if (ncattcopy(cdfid, NC_GLOBAL, att2.name, cdfid, tu_id) == -1) {
error("%s: ncattcopy of shorter NC_GLOBAL attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
add_att(&test, tu_id, &att2); /* keep in-memory netcdf consistent */
/* check that copy worked with ncattinq and ncattget */
if (ncattinq(cdfid, tu_id, att2.name, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (att2.type != tmp.type || att2.len != tmp.len) {
error("%s: variable ncattinq got unexpected type or len", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(att2.len * nctypelen(att2.type));
if (ncattget(cdfid, tu_id, att2.name, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, att2.val) != 0) {
error("%s: ncattget got bad values after copy of variable attrs",
pname);
nerrs++;
}
free (tmp.val);
/* try with same cdfid for source and target, same variable */
if (ncattcopy(cdfid, tu_id, att.name, cdfid, tu_id) == -1) {
error("%s: ncattcopy failed with identical source and target",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try with nonexisting attribute, check error */
if (ncattcopy(cdfid, tt_id, "nonesuch", cdfid, tu_id) != -1) {
error("%s: ncattcopy should fail with bad attribute name",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
/* try with bad source or target variable handle, check error */
if (ncattcopy(cdfid, test.nvars, att.name, cdfid, tu_id) != -1) {
error("%s: ncattcopy should fail with bad source variable id",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (ncattcopy(cdfid, tt_id, att.name, cdfid, 2) != -1) {
error("%s: ncattcopy should fail with bad target variable id",
pname);
ncclose(cdfid); ncclose(cdfid2); return ++nerrs;
}
if (ncclose (cdfid2) == -1) {
error("%s: ncclose failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad source or target netCDF handles, check error */
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu_id) != -1) {
error("%s: ncattcopy should fail with bad target netcdf id",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
if (ncattcopy(cdfid, tt_id, att.name, cdfid2, tu_id) != -1) {
error("%s: ncattcopy should fail with bad netcdf id", pname);
nerrs++;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
free(tt.dims);
free(tu.dims);
return nerrs;
}
/*
* Test ncattname
* check that NC_GLOBAL variable id works
* check in both modes
* check that proper call worked after ncattput
* try with bad netCDF handle, check error
* try with bad variable handle, check error
* try with bad attribute number, check error
*/
int
test_ncattname(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattname";
int cdfid; /* netcdf id */
struct cdfatt tmp; /* attributes */
int ia, ib; /* attribute numbers */
int iv; /* variable id */
static short short_vals[] = {3, 4, 5};
static struct cdfatt att = /* attribute */
{___, ___, NC_SHORT, LEN_OF(short_vals), (void *) short_vals};
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: ncredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* for each NC_GLOBAL attribute, get name and compare with expected name */
att.name = (char *) emalloc(MAX_NC_NAME);
ib = 0;
for (ia = 0; ia < test.ngatts; ia++) {
if (ncattname(cdfid, NC_GLOBAL, ia, att.name) == -1) {
error("%s: ncattname failed on global attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next global attribute */
while (ib < test.natts && test.atts[ib].var != NC_GLOBAL)
ib++;
if (ib >= test.natts) {
error("%s: test problem, expected global attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: NC_GLOBAL attribute name `%s' instead of expected `%s'",
pname, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
/* for each variable attribute, get name and compare with expected name */
for (iv = 0; iv < test.nvars; iv++) {
ib = 0;
for (ia = 0; ia < test.vars[iv].natts; ia++) {
if (ncattname(cdfid, iv, ia, att.name) == -1) {
error("%s: ncattname failed on variable attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next attribute */
while (ib < test.natts && test.atts[ib].var != iv)
ib++;
if (ib >= test.natts) {
error("%s: problem in test, expected attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: variable '%s' name `%s' instead of expected `%s'",
pname, test.vars[iv].name, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
}
/* in define mode, add a global attribute */
(void) strcpy(att.name,"attx");
if (ncattput(cdfid, NC_GLOBAL, att.name, att.type, att.len, att.val)
== -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &att); /* keep in-memory netcdf consistent */
/* test that ncattname works immediately after ncattput */
tmp.name = (char *) emalloc(MAX_NC_NAME);
if (ncattname(cdfid, NC_GLOBAL, test.ngatts-1, tmp.name) == -1) {
error("%s: ncattname failed on variable attribute", pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, tmp.name) != 0) {
error("%s: immediate NC_GLOBAL name `%s' instead of expected `%s'",
pname, tmp.name, att.name);
nerrs++;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode */
/* for each NC_GLOBAL attribute, get name and compare with expected name */
ib = 0;
for (ia = 0; ia < test.ngatts; ia++) {
if (ncattname(cdfid, NC_GLOBAL, ia, att.name) == -1) {
error("%s: ncattname failed on global attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next global attribute */
while (ib < test.natts && test.atts[ib].var != NC_GLOBAL)
ib++;
if (ib >= test.natts) {
error("%s: test problem, expected global attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: NC_GLOBAL attribute name `%s' instead of expected `%s'",
pname, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
/* for each variable attribute, get name and compare with expected name */
for (iv = 0; iv < test.nvars; iv++) {
ib = 0;
for (ia = 0; ia < test.vars[iv].natts; ia++) {
if (ncattname(cdfid, iv, ia, att.name) == -1) {
error("%s: ncattname failed on variable attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* find number of next attribute */
while (ib < test.natts && test.atts[ib].var != iv)
ib++;
if (ib >= test.natts) {
error("%s: problem in test, expected attribute not found",
pname);
ncclose(cdfid); return ++nerrs;
}
if (strcmp(att.name, test.atts[ib].name) != 0) {
error("%s: variable '%s' name `%s' instead of expected `%s'",
pname, test.vars[iv].name, att.name, test.atts[ib].name);
nerrs++;
}
ib++;
}
}
/* try with bad variable handle, check error */
if (ncattname(cdfid, test.nvars, 0, att.name) != -1) {
error("%s: ncattname should fail with bad variable handle", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad attribute number, check error */
if (ncattname(cdfid, NC_GLOBAL, -1, att.name) != -1) {
error("%s: ncattname should fail with negative number", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncattname(cdfid, NC_GLOBAL, test.ngatts, att.name) != -1) {
error("%s: ncattname should fail with too-high number", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
nerrs++;
return ++nerrs;
}
/* try with bad netCDF handle, check error */
if (ncattname(cdfid, NC_GLOBAL, 0, att.name) != -1) {
error("%s: ncattname shoul fail with bad cdfid", pname);
nerrs++;
}
free (tmp.name);
free (att.name);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncattrename
* check that proper rename worked with ncattinq, ncattget
* try renaming to existing attribute name, check error
* try with nonexisting attribute, check error
* try with bad variable handle, check error
* try in data mode, check error
* try with bad netCDF handle, check error
*/
int
test_ncattrename(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattrename";
int cdfid; /* netcdf id */
static char newname[] = "shorter";
static char longername[] = "longer_name";
struct cdfatt tmp; /* attributes */
static short short_vals[] = {3, 4, 5};
static struct cdfatt atty = /* attribute */
{___, "long_name", NC_SHORT, LEN_OF(short_vals), (void *) short_vals};
static struct cdfatt attz = /* attribute */
{___, "arggh", NC_SHORT, LEN_OF(short_vals), (void *) short_vals};
int ynum; /* attribute number */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add two attributes */
if (ncattput(cdfid, NC_GLOBAL, atty.name, atty.type, atty.len,
atty.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &atty); /* keep in-memory netcdf in sync */
ynum = test.natts-1; /* number of attribute just put */
if (ncattput(cdfid, NC_GLOBAL, attz.name, attz.type, attz.len,
attz.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &attz); /* keep in-memory netcdf in sync */
/* rename first attribute to shorter name */
if (ncattrename(cdfid, NC_GLOBAL, atty.name, newname) == -1) {
error("%s: ncattrename failed", pname);
ncclose(cdfid); return ++nerrs;
}
(void) strcpy(test.atts[ynum].name, newname); /* keep test consistent */
/* check new name with ncattinq */
if (ncattinq(cdfid, NC_GLOBAL, newname, &tmp.type, &tmp.len) == -1) {
error("%s: ncattinq of renamed attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (atty.type != tmp.type || atty.len != tmp.len) {
error("%s: NC_GLOBAL ncattinq got unexpected type or len", pname);
ncclose(cdfid); return ++nerrs;
}
/* allocate space to hold the attribute value to be retrieved */
tmp.val = emalloc(atty.len * nctypelen(atty.type));
if (ncattget(cdfid, NC_GLOBAL, newname, tmp.val) == -1) {
error("%s: ncattget of variable attribute failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atty.val) != 0) {
error("%s: ncattget got bad values after rename attrs", pname);
nerrs++;
return ++nerrs;
}
if (ncattinq(cdfid, NC_GLOBAL, atty.name, &tmp.type, &tmp.len) != -1) {
error("%s: ncattrename left attribute with old name", pname);
ncclose(cdfid); return ++nerrs;
}
/* try to rename second attribute same as first, should fail */
if (ncattrename(cdfid, NC_GLOBAL, attz.name, newname) != -1) {
error("%s: ncattrename should have failed with used name", pname);
ncclose(cdfid); return ++nerrs;
}
/* try to rename second attribute with a longer name */
if (ncattrename(cdfid, NC_GLOBAL, attz.name, longername) == -1) {
error("%s: ncattrename failed with longer name", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad variable handle, check for failure */
if (ncattrename(cdfid, test.nvars, newname, atty.name) != -1) {
error("%s: ncattrename should have failed on bad variable id", pname);
ncclose(cdfid); return ++nerrs;
}
/* try with bad attribute name, check for failure */
if (ncattrename(cdfid, NC_GLOBAL, "nonesuch", newname) != -1) {
error("%s: ncattrename should have failed on bad attribute name",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode change name to even shorter and check value */
if (ncattrename(cdfid, NC_GLOBAL, newname, "short") == -1) {
error("%s: ncattrename to shorter name failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncattrename(cdfid, NC_GLOBAL, "short", "plugh") == -1) {
error("%s: ncattrename to same length failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncattget(cdfid, NC_GLOBAL, "plugh", tmp.val) == -1) {
error("%s: ncgetatt of renamed attribute failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
if (val_cmp(tmp.type, tmp.len, tmp.val, atty.val) != 0) {
error("%s: ncattget got bad values after data mode rename", pname);
nerrs++;
return ++nerrs;
}
free (tmp.val);
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* should fail, since bad handle */
if (ncattrename(cdfid, NC_GLOBAL, newname, atty.name) != -1) {
error("%s: ncattrename should fail with bad cdfid", pname);
ncclose(cdfid); return ++nerrs;
}
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
/*
* Test ncattdel
* check that proper delete worked
* try with bad netCDF handle, check error
* try with bad variable handle, check error
* try with nonexisting attribute, check error
* try in data mode, check error
*/
int
test_ncattdel(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
static char pname[] = "test_ncattdel";
int cdfid; /* netcdf id */
static short short_vals[] = {-1, -2, -3 };
static struct cdfatt yaa = /* attribute */
{___, "yet_another_attribute", NC_SHORT, LEN_OF(short_vals),
(void *) short_vals};
int id; /* dimension id */
int yav_id; /* variable id */
static struct cdfvar yav = /* new variable for target netcdf */
{"yet_another_variable", NC_DOUBLE, 2, ___, 0};
struct cdfvar vtmp; /* variable */
struct cdfatt atmp; /* attribute */
int ndims; /* number of dimensions */
int nvars; /* number of variables */
int ngatts1, ngatts2; /* number of global attributes */
int natts; /* number of variable attributes */
int xdimid; /* id of unlimited dimension */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened OK, enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in define mode, add global attribute, variable, variable attribute */
if (ncattput(cdfid, NC_GLOBAL, yaa.name, yaa.type, yaa.len, yaa.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &yaa); /* keep in-memory netcdf in sync */
yav.dims = (int *) emalloc(sizeof(int) * yav.ndims);
for (id = 0; id < yav.ndims; id++)
yav.dims[id] = id;
if ((yav_id=ncvardef(cdfid, yav.name, yav.type, yav.ndims, yav.dims))
== -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &yav); /* keep in-memory netcdf consistent */
if (ncattput(cdfid, yav_id, yaa.name, yaa.type, yaa.len, yaa.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, yav_id, &yaa); /* keep in-memory netcdf consistent */
/* get number of global attributes, number of attributes for variable */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts1, &xdimid) == -1) {
error("%s: ncinquire in data mode failed", pname);
ncclose(cdfid); return ++nerrs;
}
vtmp.dims = (int *) emalloc(sizeof(int) * NC_MAX_VAR_DIMS);
vtmp.name = (char *) emalloc(MAX_NC_NAME);
if (ncvarinq(cdfid, yav_id, vtmp.name, &vtmp.type, &vtmp.ndims, vtmp.dims,
&natts) == -1) {
error("%s: ncvarinq failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* delete global attribute and check that it's gone */
if (ncattdel(cdfid, NC_GLOBAL, yaa.name) == -1) {
error("%s: ncattdel failed", pname);
ncclose(cdfid); return ++nerrs;
}
del_att(&test, NC_GLOBAL, &yaa); /* keep in-memory netcdf consistent */
if (ncinquire(cdfid, &ndims, &nvars, &ngatts2, &xdimid) == -1) {
error("%s: ncinquire failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (ngatts2 != ngatts1 - 1) {
error("%s: NC_GLOBAL attribute deleted, but ngatts did not decrement",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncattinq(cdfid, NC_GLOBAL, yaa.name, &atmp.type, &atmp.len) != -1) {
error("%s: ncattinq on deleted NC_GLOBAL attribute should fail", pname);
ncclose(cdfid); return ++nerrs;
}
/* delete variable attribute and check that it's gone */
if (ncattdel(cdfid, yav_id, yaa.name) == -1) {
error("%s: ncattdel failed", pname);
ncclose(cdfid); return ++nerrs;
}
del_att(&test, yav_id, &yaa); /* keep in-memory netcdf consistent */
if (ncvarinq(cdfid, yav_id, vtmp.name, &vtmp.type, &vtmp.ndims,
vtmp.dims, &vtmp.natts) == -1) {
error("%s: ncvarinq failed", pname);
ncclose(cdfid); return ++nerrs;
}
if (vtmp.natts != natts - 1) {
error("%s: NC_GLOBAL attribute deleted, but ngatts did not decrement",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncattinq(cdfid, yav_id, yaa.name, &atmp.type, &atmp.len) != -1) {
error("%s: ncattinq on deleted variable attribute should fail",
pname);
ncclose(cdfid); return ++nerrs;
}
/* re-add global attribute, variable, variable attribute */
if (ncattput(cdfid, NC_GLOBAL, yaa.name, yaa.type, yaa.len, yaa.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, NC_GLOBAL, &yaa); /* keep in-memory netcdf in sync */
if (ncattput(cdfid, yav_id, yaa.name, yaa.type, yaa.len, yaa.val) == -1) {
error("%s: ncattput failed", pname);
ncclose(cdfid); return ++nerrs;
}
add_att(&test, yav_id, &yaa); /* keep in-memory netcdf consistent */
/* try on nonexistent attribute, should fail */
if (ncattdel(cdfid, yav_id, "nonesuch") != -1) {
error("%s: ncattdel should fail on bogus attribute", pname);
ncclose(cdfid); return ++nerrs;
}
/* try on bad variable id, should fail */
if (ncattdel(cdfid, test.nvars, yaa.name) != -1) {
error("%s: ncattdel should fail on bad variable id", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* in data mode, should fail */
if (ncattdel(cdfid, NC_GLOBAL, yaa.name) != -1) {
error("%s: ncattdel in data mode should fail", pname);
ncclose(cdfid); return ++nerrs;
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
/* try on bad netcdf handle, should fail */
if (ncattdel(cdfid, yav_id, yaa.name) != -1) {
error("%s: ncattdel should fail on bad netcdf id", pname);
nerrs++;
}
free(vtmp.dims);
free(vtmp.name);
free(yav.dims);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}
|