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
|
/* This is part of the netCDF package.
Copyright 2005 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT file for conditions of use.
Test netcdf-4 compound type feature.
Ed Hartnett
*/
#include <config.h>
#include <stdlib.h>
#include <nc_tests.h>
#include "err_macros.h"
#define FILE_NAME "tst_compounds.nc"
#define SVC_REC "Service_Record"
#define BATTLES_WITH_KLINGONS "Number_of_Battles_with_Klingons"
#define DATES_WITH_ALIENS "Dates_with_Alien_Hotties"
#define STARDATE "Stardate"
#define RANK 1
#define DIM_LEN 3
#define SERVICE_RECORD "Kirk_Service_Record"
int
main(int argc, char **argv)
{
printf("\n*** Testing netcdf-4 user defined type functions.\n");
printf("*** testing simple compound scalar variable create...");
{
int ncid, typeid, varid;
size_t nfields;
int ndims, nvars, natts, unlimdimid;
char name[NC_MAX_NAME + 1];
size_t size;
nc_type xtype, field_xtype;
int dimids[] = {0}, fieldid;
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
struct s1
{
int i1;
int i2;
};
struct s1 data, data_in;
/* Create some phony data. */
data.i1 = 5;
data.i2 = 10;
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
if (nc_inq_compound(ncid, typeid, name, &size, &nfields)) ERR;
if (size != sizeof(struct s1) || strcmp(name, SVC_REC) || nfields) ERR;
if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS,
NC_COMPOUND_OFFSET(struct s1, i1), NC_INT)) ERR;
if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS,
NC_COMPOUND_OFFSET(struct s1, i2), NC_INT)) ERR;
/* This won't work due to bad typeid. */
if (nc_def_var(ncid, SERVICE_RECORD, typeid + TEST_VAL_42, 0, NULL,
&varid) != NC_EBADTYPE) ERR;
/* Define the variable. */
if (nc_def_var(ncid, SERVICE_RECORD, typeid, 0, NULL, &varid)) ERR;
if (nc_put_var(ncid, varid, &data)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a peek. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and take a more detailed look at the compound
* type. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
if (strcmp(name, SERVICE_RECORD) || ndims != 0 || natts != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 2 || size != sizeof(struct s1) || strcmp(name, SVC_REC)) ERR;
if (nc_inq_compound_name(ncid, xtype, name)) ERR;
if (strcmp(name, SVC_REC)) ERR;
if (nc_inq_compound_size(ncid, xtype, &size)) ERR;
if (size != sizeof(struct s1)) ERR;
if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR;
if (nfields != 2) ERR;
if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (field_ndims) ERR;
if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || (field_xtype != NC_INT || field_ndims != 0)) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR;
if (nc_inq_compound_fieldname(ncid, xtype, 1, name)) ERR;
if (strcmp(name, DATES_WITH_ALIENS)) ERR;
if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR;
if (fieldid != 0) ERR;
if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR;
if (offset != 4) ERR;
if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR;
if (field_xtype != NC_INT) ERR;
if (nc_get_var(ncid, varid, &data_in)) ERR;
if (data.i1 != data_in.i1 || data.i2 != data_in.i2) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing simple compound variable create...");
{
int ncid, typeid, varid;
size_t nfields;
int dimid;
int ndims, nvars, natts, unlimdimid;
char name[NC_MAX_NAME + 1];
size_t size;
nc_type xtype, field_xtype;
int dimids[] = {0}, fieldid;
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
int i;
struct s1
{
int i1;
int i2;
};
struct s1 data[DIM_LEN], data_in[DIM_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct s1), DIM_LEN)))
return NC_ENOMEM;
memcpy((void *)data, (void *)dummy, sizeof(struct s1) * DIM_LEN);
free(dummy);
/* Create some phony data. */
for (i = 0; i < DIM_LEN; i++)
{
data[i].i1 = 5;
data[i].i2 = 10;
}
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
if (nc_inq_compound(ncid, typeid, name, &size, &nfields)) ERR;
if (size != sizeof(struct s1) || strcmp(name, SVC_REC) || nfields) ERR;
if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS,
NC_COMPOUND_OFFSET(struct s1, i1), NC_INT)) ERR;
if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS,
NC_COMPOUND_OFFSET(struct s1, i2), NC_INT)) ERR;
if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, SERVICE_RECORD, typeid, 1, dimids, &varid)) ERR;
if (nc_put_var(ncid, varid, data)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a peek. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and take a more detailed look at the compound
* type. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
if (strcmp(name, SERVICE_RECORD) || ndims != 1 || natts != 0 || dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 2 || size != sizeof(struct s1) || strcmp(name, SVC_REC)) ERR;
if (nc_inq_compound_name(ncid, xtype, name)) ERR;
if (strcmp(name, SVC_REC)) ERR;
if (nc_inq_compound_size(ncid, xtype, &size)) ERR;
if (size != sizeof(struct s1)) ERR;
if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR;
if (nfields != 2) ERR;
if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (field_ndims) ERR;
if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || (field_xtype != NC_INT || field_ndims != 0)) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR;
if (nc_inq_compound_fieldname(ncid, xtype, 1, name)) ERR;
if (strcmp(name, DATES_WITH_ALIENS)) ERR;
if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR;
if (fieldid != 0) ERR;
if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR;
if (offset != 4) ERR;
if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR;
if (field_xtype != NC_INT) ERR;
if (nc_get_var(ncid, varid, data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
if (data[i].i1 != data_in[i].i1 || data[i].i2 != data_in[i].i2) ERR;
{ /* Test if we can read/write with get_vars */
# define STRIDE_S 2
# define DIM_LEN_S ((1+DIM_LEN) / STRIDE_S)
size_t start[RANK] = {0};
size_t edges[RANK] = {2};
ptrdiff_t stride[RANK] = {2};
struct s1 datas[DIM_LEN_S];
memcpy(datas,data,sizeof(datas));
datas[0].i1 = 13; datas[0].i2 = 23;
datas[1].i1 = 111; datas[1].i2 = 222;
memset(data,0,sizeof(data));
if (nc_get_var(ncid, varid, data)) ERR;
if (nc_put_vars(ncid, varid, start, edges, stride, datas)) ERR;
memset(data,0,sizeof(data));
if (nc_get_var(ncid, varid, data)) ERR;
for (i=0; i<DIM_LEN_S; i++) { /* verify the write */
if(data[i*STRIDE_S].i1 != datas[i].i1 || data[i*STRIDE_S].i2 != datas[i].i2) ERR;
}
/* Do a strided read */
memset(data,0,sizeof(data));
if (nc_get_vars(ncid, varid, start, edges, stride, data)) ERR;
for (i=0; i<DIM_LEN_S; i++) {
if(data[i].i1 != datas[i].i1 || data[i].i2 != datas[i].i2) ERR;
}
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing REALLY simple compound variable create...");
{
int ncid, typeid, varid;
int ndims, nvars, natts, unlimdimid;
nc_type xtype;
size_t size_in, nfields_in;
char name_in[NC_MAX_NAME + 1];
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(int), SVC_REC, &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS, 0, NC_INT)) ERR;
if (nc_def_var(ncid, SERVICE_RECORD, typeid, 0, NULL, &varid)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a peek. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
if (ndims != 0 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR;
if (nc_inq_vartype(ncid, 0, &xtype)) ERR;
if (nc_inq_compound(ncid, xtype, name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, SVC_REC) || size_in != sizeof(int) || nfields_in != 1) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing simple compound attribute create...");
{
int ncid, typeid;
size_t nfields;
int ndims, nvars, natts, unlimdimid;
char name[NC_MAX_NAME + 1];
size_t size, len;
nc_type xtype, field_xtype;
int fieldid;
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
int i;
struct s1
{
int i1;
int i2;
};
struct s1 data[DIM_LEN], data_in[DIM_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct s1), DIM_LEN)))
return NC_ENOMEM;
memcpy((void *)data, (void *)dummy, sizeof(struct s1) * DIM_LEN);
free(dummy);
/* Create some phony data. */
for (i=0; i<DIM_LEN; i++)
{
data[i].i1 = 5;
data[i].i2 = 10;
}
/* Create a file with a global attribute of compound type. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS,
NC_COMPOUND_OFFSET(struct s1, i1), NC_INT)) ERR;
if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS,
NC_COMPOUND_OFFSET(struct s1, i2), NC_INT)) ERR;
if (nc_put_att(ncid, NC_GLOBAL, SERVICE_RECORD, typeid, 3, data)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a peek. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and take a more detailed look at the compound
* type. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_get_att(ncid, NC_GLOBAL, SERVICE_RECORD, data_in)) ERR;
for (i = 0; i < DIM_LEN; i++)
if (data[i].i1 != data_in[i].i1 || data[i].i2 != data_in[i].i2) ERR;;
if (nc_inq_att(ncid, NC_GLOBAL, SERVICE_RECORD, &xtype, &len)) ERR;
if (len != 3) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 2 || size != 8 || strcmp(name, SVC_REC)) ERR;
if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR;
if (nfields != 2) ERR;
if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || field_xtype != NC_INT) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR;
if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR;
if (fieldid != 0) ERR;
if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR;
if (offset != 4) ERR;
if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR;
if (field_xtype != NC_INT) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound variable with new atomic types...");
{
int ncid, typeid, dimid, varid;
size_t nfields;
int ndims, natts;
char name[NC_MAX_NAME + 1];
size_t size;
nc_type xtype, field_xtype;
int dimids[] = {0};
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
int i;
/* StarFleet Medical Record. */
struct sf_med_rec
{
unsigned char num_heads;
unsigned short num_arms;
unsigned int num_toes;
long long ago; /* in a galaxy far far away... */
unsigned long long num_hairs;
};
struct sf_med_rec med_data_out[DIM_LEN], med_data_in[DIM_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct sf_med_rec), DIM_LEN)))
return NC_ENOMEM;
memcpy((void *)med_data_out, (void *)dummy, sizeof(struct sf_med_rec) * DIM_LEN);
free(dummy);
/* Create some phony data. */
for (i=0; i<DIM_LEN; i++)
{
/* medical data */
med_data_out[i].num_heads = 254;
med_data_out[i].num_arms = NC_FILL_USHORT - 1;
med_data_out[i].num_toes = NC_FILL_UINT - 1;
med_data_out[i].ago = NC_FILL_INT64 + 1;
med_data_out[i].num_hairs = NC_FILL_UINT64 - 1;
}
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct sf_med_rec), "SFMedRec", &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, "num_heads",
NC_COMPOUND_OFFSET(struct sf_med_rec, num_heads), NC_UBYTE)) ERR;
if (nc_insert_compound(ncid, typeid, "num_arms",
NC_COMPOUND_OFFSET(struct sf_med_rec, num_arms), NC_USHORT)) ERR;
if (nc_insert_compound(ncid, typeid, "num_toes",
NC_COMPOUND_OFFSET(struct sf_med_rec, num_toes), NC_UINT)) ERR;
if (nc_insert_compound(ncid, typeid, "ago",
NC_COMPOUND_OFFSET(struct sf_med_rec, ago), NC_INT64)) ERR;
if (nc_insert_compound(ncid, typeid, "num_hairs",
NC_COMPOUND_OFFSET(struct sf_med_rec, num_hairs), NC_UINT64)) ERR;
if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, "starbase_13", typeid, 1, dimids, &varid)) ERR;
if (nc_put_var(ncid, varid, med_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a look. */
{
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
if (strcmp(name, "starbase_13") || ndims != 1 || natts != 0 || dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 5 || size != sizeof(struct sf_med_rec) || strcmp(name, "SFMedRec")) ERR;
if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "num_heads") || offset != 0 || field_xtype != NC_UBYTE) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "num_arms") || offset != NC_COMPOUND_OFFSET(struct sf_med_rec, num_arms) ||
field_xtype != NC_USHORT) ERR;
if (nc_inq_compound_field(ncid, xtype, 2, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "num_toes") || offset != NC_COMPOUND_OFFSET(struct sf_med_rec, num_toes) ||
field_xtype != NC_UINT) ERR;
if (nc_inq_compound_field(ncid, xtype, 3, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "ago") || offset != NC_COMPOUND_OFFSET(struct sf_med_rec, ago) ||
field_xtype != NC_INT64) ERR;
if (nc_inq_compound_field(ncid, xtype, 4, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "num_hairs") || offset != NC_COMPOUND_OFFSET(struct sf_med_rec, num_hairs) ||
field_xtype != NC_UINT64) ERR;
if (nc_get_var(ncid, varid, med_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
if (med_data_in[i].num_heads != med_data_out[i].num_heads ||
med_data_in[i].num_arms != med_data_out[i].num_arms ||
med_data_in[i].num_toes != med_data_out[i].num_toes ||
med_data_in[i].ago != med_data_out[i].ago ||
med_data_in[i].num_hairs != med_data_out[i].num_hairs) ERR;
if (nc_close(ncid)) ERR;
}
}
SUMMARIZE_ERR;
printf("*** testing compound variable containing an array of ints...");
{
#define NUM_DIMENSIONS 7
int ncid, typeid, varid;
size_t nfields;
int dimid;
int ndims, natts;
char name[NC_MAX_NAME + 1];
size_t size;
nc_type xtype;
int dimids[] = {0};
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
nc_type field_typeid;
int dim_sizes[] = {NUM_DIMENSIONS};
int i, j;
/* Since some aliens exists in different, or more than one,
* dimensions, StarFleet keeps track of the dimensional
* abilities of everyone on 7 dimensions. */
struct dim_rec
{
int starfleet_id;
int abilities[NUM_DIMENSIONS];
};
struct dim_rec dim_data_out[DIM_LEN], dim_data_in[DIM_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct dim_rec), DIM_LEN)))
return NC_ENOMEM;
memcpy((void *)dim_data_out, (void *)dummy, sizeof(struct dim_rec) * DIM_LEN);
free(dummy);
/* Create some phony data. */
for (i=0; i<DIM_LEN; i++)
{
dim_data_out[i].starfleet_id = i;
for (j = 0; j < NUM_DIMENSIONS; j++)
dim_data_out[i].abilities[j] = j;
}
/* Create a file with a compound type which contains an array of
* int. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct dim_rec), "SFDimRec", &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, "starfleet_id",
NC_COMPOUND_OFFSET(struct dim_rec, starfleet_id), NC_INT)) ERR;
if (nc_insert_array_compound(ncid, typeid, "abilities",
NC_COMPOUND_OFFSET(struct dim_rec, abilities), NC_INT, 1, dim_sizes)) ERR;
if (nc_inq_compound_field(ncid, typeid, 1, name, &offset, &field_typeid,
&field_ndims, field_sizes)) ERR;
if (strcmp(name, "abilities") || offset != 4 || field_typeid != NC_INT ||
field_ndims != 1 || field_sizes[0] != dim_sizes[0]) ERR;
if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
if (nc_def_var(ncid, "dimension_data", typeid, 1, dimids, &varid)) ERR;
if (nc_put_var(ncid, varid, dim_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Open the file and take a look. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR;
if (strcmp(name, "dimension_data") || ndims != 1 || natts != 0 || dimids[0] != 0) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 2 || size != sizeof(struct dim_rec) || strcmp(name, "SFDimRec")) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_typeid,
&field_ndims, field_sizes)) ERR;
if (strcmp(name, "abilities") || offset != 4 || field_typeid != NC_INT ||
field_ndims != 1 || field_sizes[0] != NUM_DIMENSIONS) ERR;
if (nc_get_var(ncid, varid, dim_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
{
if (dim_data_in[i].starfleet_id != dim_data_out[i].starfleet_id) ERR;
for (j = 0; j < NUM_DIMENSIONS; j++)
if (dim_data_in[i].abilities[j] != dim_data_out[i].abilities[j]) ERR;
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound var containing compound type...");
{
int ncid;
size_t nfields;
char name[NC_MAX_NAME + 1];
size_t size, len;
nc_type xtype, field_xtype;
int field_ndims, field_sizes[NC_TESTS_MAX_DIMS];
size_t offset;
nc_type svc_recid, hr_recid;
int dim_sizes[] = {NC_MAX_NAME + 1};
int i;
/* The following structs are written and read as compound types by
* the tests below. */
struct s1
{
int i1;
int i2;
};
struct s1 data[DIM_LEN];
/* StarFleet Human Resources Department has data records for all
* employees. */
struct hr_rec
{
int starfleet_id;
struct s1 svc_rec;
char name[NC_MAX_NAME + 1];
float max_temp, min_temp; /* temperature range */
double percent_transporter_errosion;
};
struct hr_rec hr_data_out[DIM_LEN], hr_data_in[DIM_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct hr_rec), DIM_LEN)))
return NC_ENOMEM;
memcpy((void *)hr_data_out, (void *)dummy, sizeof(struct hr_rec) * DIM_LEN);
free(dummy);
/* Create some phony data. */
for (i=0; i<DIM_LEN; i++)
{
data[i].i1 = 5;
data[i].i2 = 10;
/* hr data */
hr_data_out[i].starfleet_id = i;
hr_data_out[i].svc_rec = data[i];
if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR;
hr_data_out[i].max_temp = 99.99;
hr_data_out[i].min_temp = -9.99;
hr_data_out[i].percent_transporter_errosion = .030303;
}
/* Create a file with a nested compound type attribute and variable. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Define the inner type first. */
if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &svc_recid)) ERR;
if (nc_insert_compound(ncid, svc_recid, BATTLES_WITH_KLINGONS,
NC_COMPOUND_OFFSET(struct s1, i1), NC_INT)) ERR;
if (nc_insert_compound(ncid, svc_recid, DATES_WITH_ALIENS,
NC_COMPOUND_OFFSET(struct s1, i2), NC_INT)) ERR;
/* Now define the containing type. */
if (nc_def_compound(ncid, sizeof(struct hr_rec), "SF_HR_Record", &hr_recid)) ERR;
if (nc_insert_compound(ncid, hr_recid, "StarFleet_ID",
NC_COMPOUND_OFFSET(struct hr_rec, starfleet_id), NC_INT)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Service_Record",
NC_COMPOUND_OFFSET(struct hr_rec, svc_rec), svc_recid)) ERR;
if (nc_insert_array_compound(ncid, hr_recid, "Name",
NC_COMPOUND_OFFSET(struct hr_rec, name), NC_CHAR, 1, dim_sizes)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Max_Temp",
NC_COMPOUND_OFFSET(struct hr_rec, max_temp), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Min_Temp",
NC_COMPOUND_OFFSET(struct hr_rec, min_temp), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Percent_Transporter_Erosian",
NC_COMPOUND_OFFSET(struct hr_rec, percent_transporter_errosion),
NC_DOUBLE)) ERR;
/* Write it as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, "HR_Records", hr_recid, DIM_LEN,
hr_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Read the att and check values. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_get_att(ncid, NC_GLOBAL, "HR_Records", hr_data_in)) ERR;
for (i=0; i<DIM_LEN; i++)
{
if (hr_data_in[i].starfleet_id != hr_data_out[i].starfleet_id ||
hr_data_in[i].svc_rec.i1 != hr_data_out[i].svc_rec.i1 ||
hr_data_in[i].svc_rec.i2 != hr_data_out[i].svc_rec.i2 ||
strcmp(hr_data_in[i].name, hr_data_out[i].name) ||
hr_data_in[i].max_temp != hr_data_out[i].max_temp ||
hr_data_in[i].min_temp != hr_data_out[i].min_temp ||
hr_data_in[i].percent_transporter_errosion !=
hr_data_out[i].percent_transporter_errosion) ERR;
}
/* Use the inq functions to learn about nested compound type. */
if (nc_inq_att(ncid, NC_GLOBAL, "HR_Records", &xtype, &len)) ERR;
if (len != DIM_LEN) ERR;
if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR;
if (nfields != 6 || size != sizeof(struct hr_rec) || strcmp(name, "SF_HR_Record")) ERR;
if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "StarFleet_ID") || offset != 0 || field_xtype != NC_INT) ERR;
if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR;
if (strcmp(name, "Service_Record") || offset != NC_COMPOUND_OFFSET(struct hr_rec, svc_rec)) ERR;
/* Check the internal compound type. */
/* Finish checking the containing compound type. */
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** creating compound test file...");
{
#define TYPE6_NAME "obs_t"
#define DIM6_NAME "n"
#define DIM6_LEN 3
#define VAR6_NAME "obs"
#define VAR6_RANK 1
#define ATT6_NAME "_FillValue"
#define ATT6_LEN 1
int ncid;
int dimid, varid;
nc_type typeid;
char name_in[NC_MAX_NAME+1];
int i;
int var_dims[VAR6_RANK];
typedef struct obs_t {
char day ;
short elev;
int count;
float relhum;
double time;
} obs_t ;
obs_t obsdata[DIM6_LEN];
char *dummy;
obs_t missing_val;
obs_t val_in;
size_t size_in;
size_t nfields_in;
nc_type class_in;
int ntypes;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct obs_t), DIM6_LEN)))
return NC_ENOMEM;
memcpy((void *)obsdata, (void *)dummy, sizeof(struct obs_t) * DIM6_LEN);
memcpy((void *)&missing_val, (void *)dummy, sizeof(struct obs_t));
free(dummy);
/* Initialize data. */
for (i = 0; i < DIM6_LEN; i++)
{
obsdata[i].day = 15 * i + 1;
obsdata[i].elev = 2 * i + 1;
obsdata[i].count = 2 * i + 1;
obsdata[i].relhum = 2.0 * i + 1;
obsdata[i].time = 2.0 * i + 1;
}
missing_val.day = 99;
missing_val.elev = 99;
missing_val.count = 99;
missing_val.relhum = 99.;
missing_val.time = 99.;
if (nc_create(FILE_NAME, NC_CLOBBER | NC_NETCDF4, &ncid)) ERR;
/* Create a compound type. */
if (nc_def_compound(ncid, sizeof(obs_t), TYPE6_NAME, &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, "day", NC_COMPOUND_OFFSET(struct obs_t, day),
NC_BYTE)) ERR;
if (nc_insert_compound(ncid, typeid, "elev", NC_COMPOUND_OFFSET(struct obs_t, elev),
NC_SHORT)) ERR;
if (nc_insert_compound(ncid, typeid, "count", NC_COMPOUND_OFFSET(struct obs_t, count),
NC_INT)) ERR;
if (nc_insert_compound(ncid, typeid, "relhum", NC_COMPOUND_OFFSET(struct obs_t, relhum),
NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, typeid, "time", NC_COMPOUND_OFFSET(struct obs_t, time),
NC_DOUBLE)) ERR;
/* Declare a dimension for number of obs */
if (nc_def_dim(ncid, DIM6_NAME, DIM6_LEN, &dimid)) ERR;
/* Declare a variable of the compound type */
var_dims[0] = dimid;
if (nc_def_var(ncid, VAR6_NAME, typeid, VAR6_RANK, var_dims, &varid)) ERR;
/* Create and write a variable attribute of the compound type */
if (nc_put_att(ncid, varid, ATT6_NAME, typeid, ATT6_LEN, (void *) &missing_val)) ERR;
if (nc_enddef(ncid)) ERR;
/* Store data, writing all values in one call */
if(nc_put_var(ncid, varid, obsdata)) ERR;
/* Write the file. */
if (nc_close(ncid)) ERR;
/* Check it out. */
/* Reopen the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq_typeids(ncid, &ntypes, &typeid)) ERR;
if (ntypes != 1) ERR;
/* Get info with the generic inquire for user-defined types */
if (nc_inq_user_type(ncid, typeid, name_in, NULL, NULL,
NULL, &class_in)) ERR;
if (strcmp(name_in, TYPE6_NAME) ||
class_in != NC_COMPOUND) ERR;
/* Get the same info with the compound-specific inquire function */
if (nc_inq_compound(ncid, typeid, name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, TYPE6_NAME) ||
size_in != sizeof(obs_t) ||
nfields_in != 5) ERR;
if (nc_inq_varid(ncid, VAR6_NAME, &varid)) ERR;
/* Read in attribute value and check it */
if (nc_get_att(ncid, varid, ATT6_NAME, &val_in)) ERR;
if (val_in.day != missing_val.day ||
val_in.elev != missing_val.elev ||
val_in.count != missing_val.count ||
val_in.relhum != missing_val.relhum) ERR;
/* Read in each value and check */
for (i = 0; i < DIM6_LEN; i++) {
size_t index[VAR6_RANK];
index[0] = i;
if (nc_get_var1(ncid, varid, index, (void *) &val_in)) ERR;
/* TODO: check values */
if (val_in.day != obsdata[i].day) ERR;
if (val_in.elev != obsdata[i].elev) ERR;
if (val_in.count != obsdata[i].count) ERR;
if (val_in.relhum != obsdata[i].relhum) ERR;
if (val_in.time != obsdata[i].time) ERR;
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Now opening the ref file for this...");
{
#define REF_FILE_NAME "ref_tst_compounds.nc"
int ncid;
int varid;
nc_type typeid;
char name_in[NC_MAX_NAME+1];
int i;
typedef struct obs_t {
char day ;
short elev;
int count;
float relhum;
double time;
} obs_t ;
obs_t obsdata[DIM6_LEN] = {
{15, 2, 1, 0.5, 3600.01},
{-99, -99, -99, -99.0f, -99.0},
{20, 6, 3, 0.75, 5000.01}
};
obs_t missing_val = {-99, -99, -99, -99, -99};
obs_t val_in;
size_t size_in;
size_t nfields_in;
nc_type class_in;
int ntypes;
char file_in[NC_MAX_NAME * 2];
if (getenv("srcdir"))
{
strcpy(file_in, getenv("srcdir"));
strcat(file_in, "/");
strcat(file_in, REF_FILE_NAME);
}
else
strcpy(file_in, REF_FILE_NAME);
/* Reopen the file. */
if (nc_open(file_in, NC_NOWRITE, &ncid)) ERR;
if (nc_inq_typeids(ncid, &ntypes, &typeid)) ERR;
if (ntypes != 1) ERR;
/* Get info with the generic inquire for user-defined types */
if (nc_inq_user_type(ncid, typeid, name_in, NULL, NULL,
NULL, &class_in)) ERR;
if (strcmp(name_in, TYPE6_NAME) ||
class_in != NC_COMPOUND) ERR;
/* Get the same info with the compound-specific inquire function */
if (nc_inq_compound(ncid, typeid, name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, TYPE6_NAME) || size_in != sizeof(obs_t) ||
nfields_in != 5) ERR;
if (nc_inq_varid(ncid, VAR6_NAME, &varid)) ERR;
/* Read in attribute value and check it */
if (nc_get_att(ncid, varid, ATT6_NAME, &val_in)) ERR;
if (val_in.day != missing_val.day ||
val_in.elev != missing_val.elev ||
val_in.count != missing_val.count ||
val_in.relhum != missing_val.relhum) ERR;
/* Read in each value and check */
for (i = 0; i < DIM6_LEN; i++) {
size_t index[VAR6_RANK];
index[0] = i;
if (nc_get_var1(ncid, varid, index, (void *) &val_in)) ERR;
/* TODO: check values */
if (val_in.day != obsdata[i].day) ERR;
if (val_in.elev != obsdata[i].elev) ERR;
if (val_in.count != obsdata[i].count) ERR;
if (val_in.relhum != obsdata[i].relhum) ERR;
if (val_in.time != obsdata[i].time) ERR;
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound var containing char array...");
{
#define DIM1_LEN 1
int ncid;
size_t len;
nc_type xtype;
nc_type hr_recid;
int dim_sizes[] = {NC_MAX_NAME + 1};
int i;
/* StarFleet Human Resources Department has data records for all
* employees. */
struct hr_rec
{
char name[NC_MAX_NAME + 1];
float max_temp;
};
struct hr_rec hr_data_out[DIM1_LEN], hr_data_in[DIM1_LEN];
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct hr_rec), DIM1_LEN)))
return NC_ENOMEM;
memcpy((void *)hr_data_out, (void *)dummy, sizeof(struct hr_rec) * DIM1_LEN);
free(dummy);
/* Create some phony data. */
for (i = 0; i < DIM1_LEN; i++)
{
if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR;
hr_data_out[i].max_temp = 99.99;
}
/* Create a file with a nested compound type attribute and variable. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Now define the compound type. */
/*printf("sizeof(struct hr_rec)=%d\n", sizeof(struct hr_rec));*/
if (nc_def_compound(ncid, sizeof(struct hr_rec), "SF_HR_Record", &hr_recid)) ERR;
if (nc_insert_array_compound(ncid, hr_recid, "Name",
NC_COMPOUND_OFFSET(struct hr_rec, name), NC_CHAR, 1, dim_sizes)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Max_Temp",
NC_COMPOUND_OFFSET(struct hr_rec, max_temp), NC_FLOAT)) ERR;
/* Write it as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, "HR_Records", hr_recid, DIM1_LEN, hr_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Read the att and check values. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_get_att(ncid, NC_GLOBAL, "HR_Records", hr_data_in)) ERR;
for (i=0; i<DIM1_LEN; i++)
if (strcmp(hr_data_in[i].name, hr_data_out[i].name) ||
hr_data_in[i].max_temp != hr_data_out[i].max_temp) ERR;
/* Use the inq functions to learn about the compound type. */
if (nc_inq_att(ncid, NC_GLOBAL, "HR_Records", &xtype, &len)) ERR;
if (len != DIM1_LEN) ERR;
/* Finish checking the containing compound type. */
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound var containing byte array...");
{
#define DIM1_LEN 1
#define ARRAY_LEN (NC_MAX_NAME + 1)
int ncid;
size_t len;
nc_type xtype;
nc_type hr_recid;
int dim_sizes[] = {ARRAY_LEN};
int i, j;
char *dummy;
/* StarFleet Human Resources Department has data records for all
* employees. */
struct hr_rec
{
char name[ARRAY_LEN];
float max_temp;
};
struct hr_rec hr_data_out[DIM1_LEN], hr_data_in[DIM1_LEN];
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct hr_rec), DIM1_LEN)))
return NC_ENOMEM;
memcpy((void *)hr_data_out, (void *)dummy, sizeof(struct hr_rec) * DIM1_LEN);
free(dummy);
/* Create some phony data. */
for (i = 0; i < DIM1_LEN; i++)
{
hr_data_out[i].max_temp = 99.99;
for (j = 0; j < ARRAY_LEN; j++)
hr_data_out[i].name[j] = j;
}
/* Create a file with a nested compound type attribute and variable. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Now define the compound type. */
/*printf("sizeof(struct hr_rec)=%d\n", sizeof(struct hr_rec));*/
if (nc_def_compound(ncid, sizeof(struct hr_rec), "SF_HR_Record", &hr_recid)) ERR;
if (nc_insert_array_compound(ncid, hr_recid, "Name",
NC_COMPOUND_OFFSET(struct hr_rec, name), NC_UBYTE, 1, dim_sizes)) ERR;
if (nc_insert_compound(ncid, hr_recid, "Max_Temp",
NC_COMPOUND_OFFSET(struct hr_rec, max_temp), NC_FLOAT)) ERR;
/* Write it as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, "HR_Records", hr_recid, DIM1_LEN, hr_data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Read the att and check values. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_get_att(ncid, NC_GLOBAL, "HR_Records", hr_data_in)) ERR;
for (i = 0; i < DIM1_LEN; i++)
{
if (hr_data_in[i].max_temp != hr_data_out[i].max_temp) ERR;
for (j = 0; j < ARRAY_LEN; j++)
if (hr_data_in[i].name[j] != hr_data_out[i].name[j]) ERR;
}
/* Use the inq functions to learn about the compound type. */
if (nc_inq_att(ncid, NC_GLOBAL, "HR_Records", &xtype, &len)) ERR;
if (len != DIM1_LEN) ERR;
/* Finish checking the containing compound type. */
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing with user-contributed code...");
{
#define DATA_LEN 1
int ncid, typeid, varid, dimid;
int dimids[] = {0};
struct s1
{
short i;
long long j;
};
struct s1 data_out[DATA_LEN], data_in[DATA_LEN];
int idx;
char *dummy;
/* REALLY initialize the data (even the gaps in the structs). This
* is only needed to pass valgrind. */
if (!(dummy = calloc(sizeof(struct s1), DATA_LEN)))
return NC_ENOMEM;
memcpy((void *)data_out, (void *)dummy, sizeof(struct s1) * DATA_LEN);
free(dummy);
/* Create some phony data. */
for (idx = 0; idx < DATA_LEN; idx++)
{
data_out[idx].i = 20000;
data_out[idx].j = 300000;
}
/* Create a file with a compound type. Write a little data. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_compound(ncid, sizeof(struct s1), "cmp1", &typeid)) ERR;
if (nc_insert_compound(ncid, typeid, "i",
NC_COMPOUND_OFFSET(struct s1, i), NC_SHORT)) ERR;
if (nc_insert_compound(ncid, typeid, "j",
NC_COMPOUND_OFFSET(struct s1, j), NC_INT64)) ERR;
if (nc_def_dim(ncid, "phony_dim", 1, &dimid)) ERR;
if (nc_def_var(ncid, "phony_var", typeid, 1, dimids, &varid)) ERR;
if (nc_put_var(ncid, varid, data_out)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen the file and check it. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_get_var(ncid, 0, data_in)) ERR;
for (idx = 0; idx < DATA_LEN; idx++)
{
if (data_in[idx].i != data_out[idx].i ||
data_in[idx].j != data_out[idx].j) ERR;
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}
|