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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF. The full HDF copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdlib.h>
#include <string.h>
/* Need to pick up H4_HAVE_SYS_STAT_H from mfhdf.h */
#include "hdf_priv.h"
#include "mfhdf.h"
#ifdef H4_HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef H4_HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include "hdftest.h"
#define EXTTST "exttst.hdf" /* main file for external file test */
#define EXTFILE "SD_external_file" /* file to contain external data */
#define EXTFILE1 "SD_external_file 2" /* file to contain external data */
#define EXTSDS "ExternalDataSet" /* data set written with external data right after creation */
#define EXTSDS2 "ExternalDataSet 2" /* data set first empty then written with external data */
#define WRAPSDS "WrapperDataSet" /* data set pointing to external data */
#define NOEXTSDS "NoExternalDataSet" /* data set with data in main file */
#define EXTFILE2 "ExternalSDSexisting" /* data set having data */
#define EXTFILE3 "ShouldNotHappen" /* data set already is external */
#define OFFSET 24
#define NUM_SDS 4
#define SDS1 "Dataset 1"
#define SDS2 "Dataset 2"
#define SDS3 "Dataset 3"
#define SDS4 "Dataset 4"
#define RANK3 3
#define X_LENGTH 4
#define Y_LENGTH 5
#define Z_LENGTH 6
#define RANK2 2
#define DIM1 5
#define DIM2 5
void verify_data(int32 sd_id, int32 sds_ind);
/* Same set of data for every 3-dim data set. Initialized in test_external(). */
int32 written_data[Z_LENGTH][Y_LENGTH][X_LENGTH];
/* Appended data or hyperslab */
int32 ap_data[1][Y_LENGTH][X_LENGTH];
/********************************************************************
Name: test_setexternal() - tests basic functionalities in storing
data in an external file
Description:
This function tests three scenarios:
- Data written in main file then moved to external file and modified
- Data in external file is pointed to by a "wrapper" data set
- Empty data set is written with data in the external file
The main contents include:
- Data written in main file then moved and modified
+ create and write the entire data set in the main file
+ move the data to the external file with SDsetexternalfile
+ modify this external data
- Data in external file pointed to by a "wrapper" data set
+ create a data set in the main file, i.e., the wrapper data set
+ have the wrapper pointed to part of the external data that
belongs to the external data set above
+ read the wrapper's data and verify
- Empty data set is written with data in the external file
+ create a data set in the main file and close it
+ re-open the data set
+ promote it to external data set, i.e., SDsetexternalfile
+ write data to the data set
Return value:
The number of errors occurred in this routine.
*********************************************************************/
static int
test_setexternal()
{
int32 sd_id, sds_id;
int32 start[2], edges[2], dimsizes[2], nt, offset;
int32 idata[DIM1 * DIM2];
int ii;
intn status;
intn num_errs = 0; /* number of errors in compression test so far */
/* Create an HDF file */
sd_id = SDstart(EXTTST, DFACC_CREATE);
CHECK(sd_id, FAIL, "SDstart");
/* Create a data set in the HDF file */
nt = DFNT_INT32 | DFNT_NATIVE;
dimsizes[0] = DIM1;
dimsizes[1] = DIM2;
sds_id = SDcreate(sd_id, EXTSDS, nt, RANK2, dimsizes);
CHECK(sds_id, FAIL, "SDcreate: Failed to create a new data set 'ExternalDataSet' for external promotion");
/* Initialize data to write out */
for (ii = 0; ii < dimsizes[0] * dimsizes[1]; ii++)
idata[ii] = ii;
/* Write data to the entire data set */
start[0] = start[1] = 0;
edges[0] = dimsizes[0];
edges[1] = dimsizes[1];
status = SDwritedata(sds_id, start, NULL, edges, (void *)idata);
CHECK(status, FAIL, "SDwritedata");
/* Promote the data set to an external data set by storing its data in
the external file EXTFILE */
status = SDsetexternalfile(sds_id, EXTFILE, 0);
CHECK(status, FAIL, "SDsetexternalfile");
for (ii = 0; ii < 3 * dimsizes[1]; ii++)
idata[ii] = ii * 10;
/* Write data to part of the newly promoted data set which now contains
data in the external file */
start[0] = start[1] = 0;
edges[0] = 3;
edges[1] = dimsizes[1];
status = SDwritedata(sds_id, start, NULL, edges, (void *)idata);
CHECK(status, FAIL, "SDwritedata");
/* End access to the data set */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
/* Need to close to flush external info to the HDF file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Open the HDF file again */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(sd_id, FAIL, "SDstart (again)");
/* Create a data set in the HDF file */
dimsizes[0] = 3;
dimsizes[1] = 3;
sds_id = SDcreate(sd_id, WRAPSDS, nt, 2, dimsizes);
CHECK(sds_id, FAIL, "SDcreate:Failed to create a new data set('WrapperDataSet') for external wrapping");
/* Promote the regular data set to a "wrapper" one by making it point to
the actual data in the external file 'extfile.hdf'.
Note that only a subset of the existing data (which belongs to the
previous data set, EXTSDS) is pointed to by the "wrapper"
data set. The subset is specified by dimsizes array */
offset = DFKNTsize(nt) * 2;
status = SDsetexternalfile(sds_id, EXTFILE, offset);
CHECK(status, FAIL, "SDsetexternalfile");
/* End access to the data set */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
/* Need to close to flush external info to the HDF, or main, file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Open the HDF file again */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(sd_id, FAIL, "SDstart (again)");
/* Select the named data set, id is checked by callee */
sds_id = get_SDSbyName(sd_id, WRAPSDS);
/* Read and verify data via the "wrapper" data set */
{
int32 odata[9];
/* Read data back from this "wrapper" data set */
start[0] = start[1] = 0;
edges[0] = 3;
edges[1] = 3;
status = SDreaddata(sds_id, start, NULL, edges, (void *)odata);
CHECK(status, FAIL, "SDreaddata");
/* Verify data read back in */
for (ii = 0; ii < edges[0] * edges[1]; ii++) {
if (odata[ii] != (ii + 2) * 10) {
fprintf(stderr, "Bogus val in loc %d in wrapper dset want %d got %ld\n", ii, (ii + 2) * 10,
(long)odata[ii]);
num_errs++;
}
}
}
/* End access to the wrapper data set */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
/* Create an empty data set then write external data to it */
/* Create data set EXTSDS2 */
nt = DFNT_INT32 | DFNT_NATIVE;
dimsizes[0] = X_LENGTH;
dimsizes[1] = Y_LENGTH;
sds_id = SDcreate(sd_id, EXTSDS2, nt, 2, dimsizes);
CHECK(sds_id, FAIL,
"SDcreate: Failed to create a new data set for testing writing external data to an empty data set");
/* Close data sets */
status = SDendaccess(sds_id);
/* Re-open the named data set, id is checked by callee */
sds_id = get_SDSbyName(sd_id, EXTSDS2);
/* Make this data set to have external data in a new file */
status = SDsetexternalfile(sds_id, EXTFILE1, 0);
/* initialize data to write out */
for (ii = 0; ii < dimsizes[0] * dimsizes[1]; ii++)
idata[ii] = ii;
/* Write data to all of data set EXTSDS2 in the file EXTFILE1 */
start[0] = start[1] = 0;
edges[0] = dimsizes[0];
edges[1] = dimsizes[1];
status = SDwritedata(sds_id, start, NULL, edges, (void *)idata);
CHECK(status, FAIL, "SDwritedata");
/* Close data sets */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
/* Close HDF file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_setexternal() */
/********************************************************************
Name: test_getexternal() - tests getting external file info on
various data sets
Description:
The main contents include:
- Create and write a data set in the main file only, i.e., non-external
data set
- Get and verify external file info for external data set and
wrapper data set
- Verifying that there is no external file info from the non-external
data set
Return value:
The number of errors occurred in this routine.
*********************************************************************/
static int
test_getexternal()
{
int32 sd_id, sds_id, noextsds;
intn name_len = 0;
char *extfile_name;
int32 offset = 0, length = 0;
int32 start[2], edges[2], dimsizes[2], nt;
int32 idata[DIM1 * DIM2];
int ii;
intn num_errs = 0; /* number of errors in compression test so far */
intn status = SUCCEED;
/* Open file 'exttst.hdf' again */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(sd_id, FAIL, "SDstart (again)");
/* Create and write a data set in the main file */
/* Create data set NOEXTSDS */
nt = DFNT_INT32 | DFNT_NATIVE;
dimsizes[0] = DIM1;
dimsizes[1] = DIM2;
noextsds = SDcreate(sd_id, NOEXTSDS, nt, 2, dimsizes);
CHECK(noextsds, FAIL,
"SDcreate: Failed to create a new data set 'NoExternalDataSet' for testing SDSgetexternalfile on a "
"non-external element");
/* initialize data to write out */
for (ii = 0; ii < 25; ii++)
idata[ii] = ii;
/* Write data to all of data set NOEXTSDS in main file */
start[0] = start[1] = 0;
edges[0] = edges[1] = DIM1;
status = SDwritedata(noextsds, start, NULL, edges, (void *)idata);
CHECK(status, FAIL, NOEXTSDS);
CHECK(status, FAIL, "SDwritedata");
/* Close data sets */
status = SDendaccess(noextsds);
CHECK(status, FAIL, "SDendaccess");
/*
* Test getting external info on an external data set; should return the
* external file information successfully.
*/
/* Get access to the data set named EXTSDS, id is checked by callee */
sds_id = get_SDSbyName(sd_id, EXTSDS);
/* Call SDgetexternalfile the first time passing in 0 for external
file name length to get the actual length - SDgetexternalfile is
deprecated as of 4.2.7 because it missed the length argument */
{ /* deprecated */
name_len = SDgetexternalfile(sds_id, 0, NULL, NULL);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalfile");
extfile_name = (char *)malloc(sizeof(char *) * (name_len + 1));
CHECK_ALLOC(extfile_name, "extfile_name", "SDgetexternalfile");
memset(extfile_name, '\0', name_len + 1);
/* Call SDgetexternalfile again and get the external file info */
name_len = SDgetexternalfile(sds_id, name_len + 1, extfile_name, &offset);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalfile");
VERIFY_CHAR(EXTFILE, extfile_name, "SDgetexternalfile");
free(extfile_name);
}
/* Call SDgetexternalinfo the first time passing in 0 for external
file name length to get the actual length */
name_len = SDgetexternalinfo(sds_id, 0, NULL, NULL, NULL);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalinfo");
/* Test passing in NULL pointer for external file name buffer, should
fail gracefully */
{
char *null_buffer = NULL;
intn ret_code = 0;
ret_code = SDgetexternalinfo(sds_id, name_len + 1, null_buffer, &offset, &length);
VERIFY(ret_code, FAIL, "SDgetexternalinfo");
}
/* Prepare buffer for external file name */
extfile_name = (char *)malloc(sizeof(char *) * (name_len + 1));
CHECK_ALLOC(extfile_name, "extfile_name", "test_getexternal");
memset(extfile_name, '\0', name_len + 1);
/* Call SDgetexternalinfo again and get the external file info */
name_len = SDgetexternalinfo(sds_id, name_len + 1, extfile_name, &offset, &length);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalinfo");
VERIFY_CHAR(EXTFILE, extfile_name, "SDgetexternalinfo");
/* Test passing in smaller buffer for external file name than actual;
name should be truncated */
{
char *short_name = (char *)malloc(sizeof(char *) * (name_len));
CHECK_ALLOC(short_name, "short_name", "test_getexternal");
memset(short_name, '\0', name_len);
strncpy(short_name, EXTFILE, name_len - 2);
memset(extfile_name, '\0', name_len);
/* Call SDgetexternalinfo again with smaller buffer size and verify
that SDgetexternalinfo reads the name truncated to the given
buffer size*/
name_len = SDgetexternalinfo(sds_id, name_len - 2, extfile_name, &offset, &length);
VERIFY(name_len, (intn)strlen(extfile_name), "SDgetexternalinfo");
VERIFY_CHAR(short_name, extfile_name, "SDgetexternalinfo");
free(short_name);
}
free(extfile_name);
/* Close the data set */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
/*
* Test getting external info on a wrapper data set; should return the
* external file information successfully.
*/
/* Get access to the data set named WRAPSDS, id is checked by callee */
sds_id = get_SDSbyName(sd_id, WRAPSDS);
/* Call SDgetexternalinfo the first time passing in 0 for external
file name length to get the actual length */
name_len = SDgetexternalinfo(sds_id, 0, NULL, NULL, NULL);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalinfo");
/* Test passing in NULL pointer for external file name buffer, should
fail gracefully */
{
char *null_buffer = NULL;
intn ret_code = 0;
ret_code = SDgetexternalinfo(sds_id, name_len + 1, null_buffer, &offset, &length);
VERIFY(ret_code, FAIL, "SDgetexternalinfo");
}
extfile_name = (char *)malloc(sizeof(char *) * (name_len + 1));
CHECK_ALLOC(extfile_name, "extfile_name", "test_getexternal");
memset(extfile_name, '\0', name_len + 1);
/* Call SDgetexternalinfo again and get the external file info */
name_len = SDgetexternalinfo(sds_id, name_len + 1, extfile_name, &offset, &length);
VERIFY(name_len, (intn)strlen(EXTFILE), "SDgetexternalinfo");
VERIFY_CHAR(EXTFILE, extfile_name, "SDgetexternalinfo");
free(extfile_name);
/*
* Test getting external info on a non-external data set; should return
* no external file information
*/
/* Get access to the data set named NOEXTSDS, id is checked by callee */
noextsds = get_SDSbyName(sd_id, NOEXTSDS);
/* Call SDgetexternalinfo on the SDS that doesn't have external
element, should return 0 for length of external file name */
name_len = SDgetexternalinfo(noextsds, 0, NULL, NULL, NULL);
VERIFY(name_len, 0, "SDgetexternalinfo");
status = SDendaccess(noextsds);
CHECK(status, FAIL, "SDendaccess");
/* Close file 'exttst.hdf' */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_getexternal() */
/********************************************************************
Name: test_mult_setexternal() - tests setting external multiple times
Description:
The main contents include:
- Create and write an external data set to FILE_NAME, then close
it and the file.
- Re-open the file
- Call SDsetexternal on the data set, which is already an external SDS.
This is to make sure HDFFR-1516 is fixed.
- Re-open the file and verify data of each data set.
Return value:
The number of errors occurred in this routine.
*********************************************************************/
int
test_mult_setexternal()
{
int32 sd_id, sds1_id;
int32 dim_sizes[3];
int32 size_written = 0;
char *extfile_name = NULL;
intn name_len = 0;
intn status = SUCCEED;
intn num_errs = 0; /* number of errors in compression test so far */
/* Create the file and initialize the SD interface */
sd_id = SDstart(EXTTST, DFACC_CREATE);
CHECK(status, FAIL, "SDstart");
dim_sizes[0] = Z_LENGTH;
dim_sizes[1] = Y_LENGTH;
dim_sizes[2] = X_LENGTH;
/* Create data set SDS1 and write data to the external file; the returned
value is the size of the data had been written for this sds */
size_written =
make_Ext3D_SDS(sd_id, SDS1, DFNT_INT32, 3, dim_sizes, (void *)written_data, OFFSET, EXTFILE2);
CHECK(size_written, FAIL, "make_Ext3D_SDS");
/* Close the file to flush */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Re-open the file */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(status, FAIL, "SDstart");
/* Move data from an external data set, SDS1, into the external file again.
This simulates the situation of the example being run more than once,
causing failure in daily test. This action should have no effect now.
(HDFFR-1521) */
/* Select the named data set, id is checked by callee */
sds1_id = get_SDSbyName(sd_id, SDS1);
/* Try to move it to the external file again; should neither fail, nor have
any effect. External file name should still be EXTFILE2 */
status = SDsetexternalfile(sds1_id, EXTFILE3, OFFSET);
if (status < 0)
fprintf(stderr, "SDsetexternalfile still fail when called more than once on an SDS\n");
/* Verify that external file still is EXTFILE2, and not EXTFILE3 */
/* Call SDgetexternalinfo the first time passing in 0 for external
file name length to get the actual length */
name_len = SDgetexternalinfo(sds1_id, 0, NULL, NULL, NULL);
if (name_len <= 0)
fprintf(stderr, "SDsetexternalfile should return length greater than 0\n");
/* Prepare buffer for external file name */
extfile_name = malloc(sizeof(char *) * (name_len + 1));
CHECK_ALLOC(extfile_name, "extfile_name", "test_getexternal");
memset(extfile_name, '\0', name_len + 1);
/* Call SDgetexternalinfo again and get the external file info */
name_len = SDgetexternalinfo(sds1_id, name_len + 1, extfile_name, NULL, NULL);
VERIFY(name_len, (intn)strlen(EXTFILE2), "SDgetexternalinfo");
VERIFY_CHAR(EXTFILE2, extfile_name, "SDgetexternalinfo");
/* Close the data set and the file */
status = SDendaccess(sds1_id);
CHECK(status, FAIL, "SDendaccess SDS1");
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Re-open the file to verify written data */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(status, FAIL, "SDstart");
/* Read data of the data set and verify against the original */
verify_data(sd_id, 0);
free(extfile_name);
/* Close the file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_mult_setexternal() */
/********************************************************************
Name: test_special_combos() - tests combining other specialness with
external data feature.
Description:
The main contents include:
- Open the file from test_mult_setexternal
- Create and write two unlimited-dimension data sets, SDS2 and SDS3
- Append data to the first unlimited-dimension data set, SDS2, to
create linked-block element.
- Move data of the first unlimited-dim data set, SDS2, to the external
file to test the combination of SPECIAL_LINKED and SPECIAL_EXT.
- Move data of SDS3 to the external file to test unlimited and
SPECIAL_EXT without linked-block element.
- Make a compressed data set, SDS4, in the main file.
- Attempt to move data of SDS4 to the external file should fail, because
SPECIAL_COMP and SPECIAL_EXT combo is not supported.
- Close everything.
- Re-open the file and verify data of each data set in the file.
Return value:
The number of errors occurred in this routine.
*********************************************************************/
int
test_special_combos()
{
int32 sd_id, sds2_id, sds3_id, sds4_id;
int32 num_sds = 0, num_attrs = 0;
int32 ap_start[3], ap_edges[3], dim_sizes[3];
int32 sds2_size = 0, sds3_size = 0, sds4_size = 0, size_written = 0;
intn status = 0;
int ii, jj, kk;
intn num_errs = 0; /* number of errors in compression test so far */
/* Create the file and initialize the SD interface */
sd_id = SDstart(EXTTST, DFACC_CREATE);
CHECK(status, FAIL, "SDstart");
dim_sizes[0] = SD_UNLIMITED;
dim_sizes[1] = Y_LENGTH;
dim_sizes[2] = X_LENGTH;
/* Create and write two unlimited-dimension data sets, SDS2 and SDS3,
in the main file. Z_LENGTH is passed for unlimited dimension. */
sds2_size = make_SDS(sd_id, SDS2, DFNT_INT32, 3, dim_sizes, Z_LENGTH, (void *)written_data);
sds3_size = make_SDS(sd_id, SDS3, DFNT_INT32, 3, dim_sizes, Z_LENGTH, (void *)written_data);
/* Close the file to flush */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Re-open the file */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(status, FAIL, "SDstart");
/* Start appending data at the end of the unlimited dimension */
ap_start[0] = Z_LENGTH;
ap_start[1] = ap_start[2] = 0;
ap_edges[0] = 1;
ap_edges[1] = Y_LENGTH;
ap_edges[2] = X_LENGTH;
/* Data initialization, a hyperslab 1xY_LENGTHxX_LENGTH */
for (kk = 0; kk < ap_edges[0]; kk++)
for (jj = 0; jj < ap_edges[1]; jj++)
for (ii = 0; ii < ap_edges[2]; ii++)
ap_data[kk][jj][ii] = (ii + 1) + (jj + 1) + (kk + 1);
/* Append data to the unlimited-dimension data set SDS2. This should */
/* produce a linked-block element, because SDS3 had been written */
sds2_size = append_Data2SDS(sd_id, SDS2, ap_start, ap_edges, (void *)ap_data);
CHECK(sds2_size, FAIL, "append_Data2SDS");
/* Select the named data set, id is checked by callee */
sds2_id = get_SDSbyName(sd_id, SDS2);
/* Now, move SDS2's data to the external file, then check its size
This tests the combo: SPECIAL_LINKED and SPECIAL_EXT. 1600 is just a
random number that is more than enough to go pass the existing data.
This action verifies the fix in HDFFR-1516. SDsetexternalfile does not
fail. Note that there is no test for the case where calling
SDsetexternalfile on an external SDS failed, because SDsetexternalfile is
modified to have no effect if it is called more than once on an SDS, see
HDFFR-1521. */
status = SDsetexternalfile(sds2_id, EXTFILE2, 1600);
CHECK(status, FAIL, "SDsetexternalfile");
/* Verify data size */
status = verify_datasize(sds2_id, sds2_size, SDS2);
CHECK(status, FAIL, "verify_datasize");
/* Move data of an existing contiguous data set to the external file */
/* Select the named data set, id is checked by callee */
sds3_id = get_SDSbyName(sd_id, SDS3);
/* Move SDS3's data to the external file, then check its size. This also */
/* tests moving an existing unlimited-dimension data set to external file */
status = SDsetexternalfile(sds3_id, EXTFILE2, 2500); /* random spot */
CHECK(status, FAIL, "SDsetexternalfile");
status = verify_datasize(sds3_id, sds3_size, SDS3);
CHECK(status, FAIL, "verify_datasize");
/* Attempt to move a compressed data set to an external file; should fail */
/* Create and write to a compressed data set */
sds4_size = make_SDS(sd_id, SDS4, DFNT_INT32, 3, dim_sizes, Z_LENGTH, (void *)written_data);
/* Select the named data set, id is checked by callee */
sds4_id = get_SDSbyName(sd_id, SDS4);
/* Try to move SDS4's data to the external file, should fail because HDF4
doesn't support compressed and external data together */
status = SDsetexternalfile(sds4_id, EXTFILE2, 4000); /* random spot */
CHECK(status, FAIL, "SDsetexternalfile");
/* Check the data length of each data set */
status = verify_datasize(sds2_id, sds2_size, SDS2);
CHECK(status, FAIL, "verify_datasize");
status = verify_datasize(sds3_id, sds3_size, SDS3);
CHECK(status, FAIL, "verify_datasize");
status = verify_datasize(sds4_id, sds4_size, SDS4);
CHECK(status, FAIL, "verify_datasize");
status = SDendaccess(sds2_id);
CHECK(status, FAIL, "SDendaccess SDS2");
status = SDendaccess(sds3_id);
CHECK(status, FAIL, "SDendaccess SDS3");
status = SDendaccess(sds4_id);
CHECK(status, FAIL, "SDendaccess SDS4");
/* Close the file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Re-open the file to verify written data */
sd_id = SDstart(EXTTST, DFACC_RDWR);
CHECK(status, FAIL, "SDstart");
status = SDfileinfo(sd_id, &num_sds, &num_attrs);
CHECK(status, FAIL, "SDfileinfo");
/* Read data of each data sets and verify against the original */
for (ii = 0; ii < num_sds; ii++) {
verify_data(sd_id, ii);
}
/* Close the file */
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_special_combos() */
/********************************************************************
Name: test_change_extdir() - tests re-setting external directory
with valid and invalid values.
Description:
The main contents include:
- Create an HDF file and a dataset in it
- Set create external directory to the source test directory
- Promote the dataset to an external element
- Try setting the external directory to various values and verify
the behavior of SDreaddata each time
- Remove the data file and the external file
Return value:
The number of errors occurred in this routine.
*********************************************************************/
#define MAIN_FILE "tmainfile.hdf" /* file where the SDS is created */
#define EXT_FILE "textfile.txt" /* file where the external data is stored */
#define SDS_NAME "ExternalDS" /* dataset will have data in an external file */
#define RANK 1
#define TMP_DIR "EXT_tempdir/" /* temporary dir to create the external file in */
static int
test_change_extdir(void)
{
int32 sd_id;
int32 sds_id;
float sds_data[] = {0.1f, 2.3f, 4.5f, 6.7f, 8.9f};
float sds1_out[5];
int32 start = 0, stride = 1, edge;
int32 dimsize[RANK];
char dir_name[MAX_PATH_LEN]; /* directory from srcdir */
size_t dir_name_len;
char *another_path = NULL; /* another path to test reading external file */
char *temp_dir = NULL; /* temp dir to create the external file in */
char *created_file_path = NULL; /* path to the created external file */
int32 sds_index;
int command_ret = 0; /* retvalue from system commands */
intn status = 0;
intn num_errs = 0; /* number of errors in compression test so far */
status = make_sourcepath(dir_name, MAX_PATH_LEN);
CHECK(status, FAIL, "make_sourcepath");
/* When srcdir is not available, make up a directory to create the external
file to cause subsequent reads to look outside of the current directory */
if (!strcmp(dir_name, "./")) {
/* Facilitate the removal of the temporary directory later */
temp_dir = (char *)calloc(strlen(TMP_DIR) + 1, sizeof(char));
CHECK_ALLOC(temp_dir, "temp_dir", "test_change_extdir");
strcpy(temp_dir, TMP_DIR);
#if defined H4_HAVE_WIN32_API
command_ret = mkdir(temp_dir);
#else
command_ret = mkdir(temp_dir, 0755);
#endif
CHECK(command_ret, (-1), "mkdir temp_dir");
strcat(dir_name, temp_dir);
}
/* Create the main file */
sd_id = SDstart(MAIN_FILE, DFACC_CREATE);
CHECK(sd_id, FAIL, "SDstart");
/* Set the directory to put the external file in */
status = HXsetcreatedir(dir_name);
CHECK(status, FAIL, "HXsetcreatedir");
/* Create a one-dim dataset named SDS_NAME */
dimsize[0] = 5;
edge = dimsize[0];
sds_id = SDcreate(sd_id, SDS_NAME, DFNT_FLOAT32, RANK, dimsize);
CHECK(sds_id, FAIL, "SDcreate");
/* Promote the data set to an external data set by storing its data in
the external file EXT_FILE */
status = SDsetexternalfile(sds_id, EXT_FILE, 0);
CHECK(status, FAIL, "SDsetexternalfile");
/* Write to the dataset, the data will be stored in the external file EXT_FILE */
status = SDwritedata(sds_id, &start, &stride, &edge, sds_data);
CHECK(status, FAIL, "SDwritedata");
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Open the file to read */
sd_id = SDstart(MAIN_FILE, DFACC_READ);
CHECK(sd_id, FAIL, "SDstart");
/* Determine the index of the dataset */
sds_index = SDnametoindex(sd_id, SDS_NAME);
CHECK(sds_index, FAIL, "SDnametoindex");
/* Dataset identifier */
sds_id = SDselect(sd_id, sds_index);
CHECK(sds_id, FAIL, "SDselect");
/* Unset the target directory and read the variable */
status = HXsetdir(NULL);
CHECK(status, FAIL, "HXsetdir NULL");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, FAIL, "SDreaddata");
/* Set the target directory to the location of the external file and read the dataset */
status = HXsetdir(dir_name);
CHECK(status, FAIL, "HXsetdir dir_name");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, SUCCEED, "SDreaddata");
/* Unset the target directory again and read the variable */
status = HXsetdir(".");
CHECK(status, FAIL, "HXsetdir .");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, FAIL, "SDreaddata");
dir_name_len = strlen(dir_name);
another_path = (char *)malloc(sizeof(char) * dir_name_len + 20);
strcpy(another_path, dir_name);
strcat(another_path, "non-existing-dir");
status = HXsetdir(another_path);
CHECK(status, FAIL, "HXsetdir another_path");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, FAIL, "SDreaddata");
free(another_path);
status = HXsetdir(dir_name);
CHECK(status, FAIL, "HXsetdir dir_name");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, SUCCEED, "SDreaddata");
status = HXsetdir(NULL);
CHECK(status, FAIL, "HXsetdir NULL");
status = SDreaddata(sds_id, &start, &stride, &edge, sds1_out);
VERIFY(status, FAIL, "SDreaddata");
/* Terminates access to the SD interface and closes the file */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Remove external data file */
created_file_path = (char *)malloc(strlen(dir_name) + strlen(EXT_FILE) + 1);
CHECK_ALLOC(created_file_path, "created_file_path", "test_change_extdir");
strcpy(created_file_path, dir_name);
strcat(created_file_path, EXT_FILE);
command_ret = remove(created_file_path);
CHECK(command_ret, FAIL, "remove created_file_path");
free(created_file_path);
/* Remove hdf file */
command_ret = remove(MAIN_FILE);
CHECK(command_ret, FAIL, "remove MAIN_FILE");
/* Remove temporary directory used in case no src_dir */
if (temp_dir) {
command_ret = rmdir(temp_dir);
CHECK(command_ret, (-1), "remove temp_dir");
free(temp_dir);
}
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_change_extdir */
/********************************************************************
Name: test_HDFFR_1609() - tests writing after creating external file
and without specifying external dir
Description:
The main contents include:
- HXsetdir to a location other than the directory where the external file
is going to be created
- Create a new hdf file and a new dataset
- Set create external directory to the source test directory
- Create the external file by calling SDsetexternalfile
- Write to the dataset with external data, should succeed (this failed before the fix)
- Remove the data file and the external file
Return value:
The number of errors occurred in this routine.
*********************************************************************/
static int
test_HDFFR_1609(void)
{
int32 sd_id;
int32 sds_id;
float sds_data[] = {0.1f, 2.3f, 4.5f, 6.7f, 8.9f};
int32 start = 0, stride = 1, edge;
int32 dimsize[RANK];
char dir_name[MAX_PATH_LEN]; /* directory from srcdir */
char *temp_dir = NULL; /* temp dir to create the external file in */
char *created_file_path = NULL; /* path to the created external file */
int command_ret = 0; /* retvalue from system commands */
intn status = 0;
intn num_errs = 0; /* number of errors in compression test so far */
status = make_sourcepath(dir_name, MAX_PATH_LEN);
CHECK(status, FAIL, "make_sourcepath");
/* When srcdir is not available, make up a directory to create the external
file to cause subsequent reads to look outside of the current directory */
if (!strcmp(dir_name, "./")) {
/* Facilitate the removal of the temporary directory later */
temp_dir = (char *)calloc(strlen(TMP_DIR) + 1, sizeof(char));
CHECK_ALLOC(temp_dir, "temp_dir", "test_HDFFR_1609");
strcpy(temp_dir, TMP_DIR);
#if defined H4_HAVE_WIN32_API
command_ret = mkdir(temp_dir);
#else
command_ret = mkdir(temp_dir, 0755);
#endif
CHECK(command_ret, (-1), "mkdir temp_dir");
strcat(dir_name, temp_dir);
}
/* Unset the target directory and read the variable */
status = HXsetdir(NULL);
CHECK(status, FAIL, "HXsetdir");
/* Create the main file */
sd_id = SDstart(MAIN_FILE, DFACC_CREATE);
CHECK(sd_id, FAIL, "SDstart");
/* Set the directory to put the external file in */
status = HXsetcreatedir(dir_name);
CHECK(status, FAIL, "HXsetcreatedir");
/* Create a one-dim dataset named SDS_NAME */
dimsize[0] = 5;
edge = dimsize[0];
sds_id = SDcreate(sd_id, SDS_NAME, DFNT_FLOAT32, RANK, dimsize);
CHECK(sds_id, FAIL, "SDcreate");
/* Promote the data set to an external data set by storing its data in
the external file EXT_FILE */
status = SDsetexternalfile(sds_id, EXT_FILE, 0);
CHECK(status, FAIL, "SDsetexternalfile");
/* Write to the dataset, the data will be stored in the external file EXT_FILE */
status = SDwritedata(sds_id, &start, &stride, &edge, sds_data);
CHECK(status, FAIL, "SDwritedata");
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
status = SDend(sd_id);
CHECK(status, FAIL, "SDend");
/* Remove external data file */
created_file_path = (char *)malloc(strlen(dir_name) + strlen(EXT_FILE) + 1);
CHECK_ALLOC(created_file_path, "created_file_path", "test_HDFFR_1609");
strcpy(created_file_path, dir_name);
strcat(created_file_path, EXT_FILE);
command_ret = remove(created_file_path);
CHECK(command_ret, FAIL, "remove created_file_path");
free(created_file_path);
/* Remove hdf file */
command_ret = remove(MAIN_FILE);
CHECK(command_ret, FAIL, "remove MAIN_FILE");
/* Remove temporary directory used in case no src_dir */
if (temp_dir) {
command_ret = rmdir(temp_dir);
CHECK(command_ret, (-1), "remove temp_dir");
free(temp_dir);
}
/* Return the number of errors that's been kept track of so far */
return num_errs;
} /* test_HDFFR_1609 */
/* Test driver for testing external file functions */
extern int
test_external()
{
int ii, jj, kk;
intn num_errs = 0; /* number of errors */
/* Data initialization */
for (kk = 0; kk < Z_LENGTH; kk++)
for (jj = 0; jj < Y_LENGTH; jj++)
for (ii = 0; ii < X_LENGTH; ii++)
written_data[kk][jj][ii] = (ii + 1) + (jj + 1) + (kk + 1);
/* Output message about test being performed */
TESTING("external file functions (texternal.c)");
/* Test SDsetexternalfile basic functionality (modified from hdftest.c) */
num_errs = num_errs + test_setexternal();
/* Test SDgetexternalfile basic functionality */
num_errs = num_errs + test_getexternal();
/* Test calling SDsetexternalfile repeatedly */
num_errs = num_errs + test_mult_setexternal();
/* Test multiple specialness */
num_errs = num_errs + test_special_combos();
/* Test reading external elements with multiple HXsetdir calls */
num_errs = num_errs + test_change_extdir();
/* Test HDFFR_1609 fix */
num_errs = num_errs + test_HDFFR_1609();
if (num_errs == 0)
PASSED();
else
H4_FAILED();
/* Return the number of errors that's been kept track of so far */
return num_errs;
}
/*********************** Local utility functions ***********************/
/********************************************************************
Name: verify_data() - Verifies the written data, given the SDS' index.
Description:
Calls SDselect, SDgetinfo, and SDreaddata to verify the sds_ind'th
data against the original buffer.
Return value:
None.
*********************************************************************/
void
verify_data(int32 sd_id, int32 sds_ind)
{
int32 sds_id;
int32 *ptr;
char name[80];
int32 data_size, rank1;
int32 start[3], edges[3], dims[3];
intn status;
int32 *outdata = NULL, num_elems;
intn num_errs = 0; /* number of errors in compression test so far */
int32 data_wappended[Z_LENGTH + 1][Y_LENGTH]
[X_LENGTH]; /* Buffer for first written data + appended data */
/* Select the data set. */
sds_id = SDselect(sd_id, sds_ind);
CHECK(sds_id, FAIL, "SDselect");
/* Set the parameters start and edges to read */
edges[1] = Y_LENGTH;
edges[2] = X_LENGTH;
start[0] = start[1] = start[2] = 0;
/* Get the name of the data set */
status = SDgetinfo(sds_id, name, &rank1, dims, NULL, NULL);
CHECK(status, FAIL, "SDgetinfo");
/* The data set SDS2 has appended data so the written data is different
from the rest of the data sets in the file */
if (!strncmp(name, SDS2, strlen(SDS2))) {
/* Number of elements in first written data + appended data */
num_elems = Z_LENGTH * Y_LENGTH * X_LENGTH + 1 * Y_LENGTH * X_LENGTH;
/* Copy buffer of first written data to data_wappended */
memcpy(data_wappended, written_data, (Z_LENGTH * Y_LENGTH * X_LENGTH) * sizeof(int));
/* Forward to the end of first written data */
ptr = &data_wappended[Z_LENGTH][0][0];
/* Copy appended data to data_wappended */
memcpy(ptr, ap_data, (1 * Y_LENGTH * X_LENGTH) * sizeof(int));
/* Back to the beginning of data_wappended */
ptr = &data_wappended[0][0][0];
/* Size of data written including appended data */
data_size = ((Z_LENGTH + 1) * Y_LENGTH * X_LENGTH) * sizeof(int);
edges[0] = Z_LENGTH + 1;
} /* with appended data */
/* Everyone else */
else {
/* Point to written data buffer */
ptr = &written_data[0][0][0];
/* Number of elements */
num_elems = Z_LENGTH * Y_LENGTH * X_LENGTH;
/* Size of data written */
data_size = num_elems * sizeof(int);
edges[0] = Z_LENGTH;
}
/* Allocate buffer for reading, after establishing the data size */
outdata = (int32 *)malloc(data_size);
CHECK_ALLOC(outdata, "outdata", "verify_data");
/* Read the entire sds and verify that the data is as the original buffer */
status = SDreaddata(sds_id, start, NULL, edges, (void *)outdata);
CHECK(status, FAIL, "SDreaddata");
/* Verify that data is correct comparing against the written data */
{
int num;
int32 *out = outdata;
for (num = 0; num < num_elems; num++, ptr++, out++) {
if (*ptr != *out) {
fprintf(stderr, "Data read (%d) is different than written (%d) for SDS #%d, name = %s\n",
*out, *ptr, sds_ind, name);
}
}
}
/* Release resource */
free(outdata);
/* Terminate access to the data set, SD interface, and file. */
status = SDendaccess(sds_id);
CHECK(status, FAIL, "SDendaccess");
} /* verify_data */
|