diff --git a/hdf4_test/tst_h4_lendian.c b/hdf4_test/tst_h4_lendian.c index e248aa394..5e88fcf71 100644 --- a/hdf4_test/tst_h4_lendian.c +++ b/hdf4_test/tst_h4_lendian.c @@ -7,10 +7,8 @@ #include #include #include -#include "err_macros.h" -#include -#include #include "mfhdf.h" +#include "err_macros.h" #define DIM1 5 #define DIM0 5 @@ -18,151 +16,117 @@ #define FILENAME "tst_h4_lendian.h4" #define SDSNAME "data" -int read_hdf_file(int dtype) { +/* Read the file. Return -1 if endianness is not little. */ +int read_hdf_file(int dtype) +{ + int ncid = 0; + int le_int16_varid = 0; + int ed = 0; - int ncid = 0; - int le_int16_varid = 0; - int retval = 0; - int ed = 0; + if (nc_open(FILENAME, NC_NETCDF4 | NC_NOWRITE, &ncid)) ERR; + if (nc_inq_varid(ncid,SDSNAME,&le_int16_varid)) ERR; + if (nc_inq_var_endian(ncid,le_int16_varid,&ed)) ERR; + if (nc_close(ncid)) ERR; + + if (ed != NC_ENDIAN_LITTLE) + return -1; - printf("\to Reading hdf4 file with a little-endian datatype %d\n",dtype); - - printf("\t\to Opening file....\t\t\t\t\t"); - retval = nc_open(FILENAME, NC_NETCDF4 | NC_NOWRITE, &ncid); - if(retval) {printf("Failure [%d]\n",retval); return retval;} - else {printf("Success\n");} - - printf("\t\to Getting varid....\t\t\t\t\t"); - retval = nc_inq_varid(ncid,SDSNAME,&le_int16_varid); - if(retval) {printf("Failure [%d]\n",retval); return retval;} - else {printf("Success\n");} - - printf("\t\to Querying endianness of the variable....\t\t"); - retval = nc_inq_var_endian(ncid,le_int16_varid,&ed); - if(retval) {printf("Failure [%d]\n",retval); return retval;} - else {printf("Success\n");} - - printf("\t\to Checking that endianness is NC_ENDIAN_LITTLE....\t"); - if (ed == NC_ENDIAN_LITTLE) printf("Success\n"); - else {printf("Failure [%d]\n\n",ed); nc_close(ncid); return -1;} - - printf("\t\to Closing file....\t\t\t\t\t"); - retval = nc_close(ncid); - if(retval) {printf("Failure [%d]\n\n",retval); return retval;} - else {printf("Success\n\n");} - - return 0; + return 0; } -int create_hdf_file(int dtype) { +/* Create a HDF4 file with one dataset of type dtype. */ +int create_hdf_file(int dtype) +{ + int32 sd_id, sds_id; + int32 start[2] = {0, 0}, edges[2] = {DIM1, DIM0}; + int16 array_data[DIM0][DIM1]; + int32 array_data_int32[DIM0][DIM1]; + float32 array_data_float32[DIM0][DIM1]; + float64 array_data_float64[DIM0][DIM1]; + void *data; + intn i, j, count; - int32 sd_id, sds_id, istat, sd_index; - int32 dims[2], start[2], edges[2], rank; - int16 array_data[DIM0][DIM1]; - intn i, j, count; - - start[0] = 0; - start[1] = 0; - edges[0] = DIM1; - edges[1] = DIM0; - - /* populate data array */ - count = 0; - for (j = 0; j < DIM0; j++) + /* Populate data arrays. */ + count = 0; + for (j = 0; j < DIM0; j++) + { + for (i = 0; i < DIM1; i++) { - for (i = 0; i < DIM1; i++) - array_data[j][i] = count++; + array_data[j][i] = count; + array_data_int32[j][i] = count; + array_data_float32[j][i] = count; + array_data_float64[j][i] = count++; } + } - printf("\to Creating hdf4 file with little-endian datatype %d....\t",dtype); + /* Point to the correct data. */ + switch(dtype) + { + case DFNT_LINT8: + case DFNT_LUINT8: + case DFNT_LINT16: + case DFNT_LUINT16: + data = array_data; + break; + case DFNT_LINT32: + case DFNT_LUINT32: + data = array_data_int32; + break; + case DFNT_LFLOAT32: + data = array_data_float32; + break; + case DFNT_LFLOAT64: + data = array_data_float64; + break; + default: + ERR; + } - sd_id = SDstart(FILENAME, DFACC_CREATE); - /* sds_id = SDcreate(sd_id, SDSNAME, DFNT_LITEND|dtype, RANK, edges); */ - sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges); + if ((sd_id = SDstart(FILENAME, DFACC_CREATE)) == -1) ERR; + if ((sds_id = SDcreate(sd_id, SDSNAME, dtype, RANK, edges)) == -1) ERR; + if (SDwritedata(sds_id, start, NULL, edges, data)) ERR; + if (SDend(sd_id)) ERR; - istat = SDendaccess(sds_id); - if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} - - istat = SDend(sd_id); - if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} - - sd_id = SDstart(FILENAME, DFACC_WRITE); - - sd_index = 0; - sds_id = SDselect(sd_id, sd_index); - - istat = SDwritedata(sds_id, start, NULL, edges, (VOIDP)array_data); - if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} - - istat = SDendaccess(sds_id); - if(istat) {printf("Failure %d\n", istat); SDend(sd_id); return istat;} - - istat = SDend(sd_id); - if(istat) {printf("Failure %d\n", istat); return istat;} - - printf("Success\n"); - return 0; + return 0; } -int test_read_write(int dtype) { - - int res = 0; - - res = create_hdf_file(dtype); - if(res) {unlink(FILENAME); return res;} - - res = read_hdf_file(dtype); - - unlink(FILENAME); - return res; +/* Create and then read the HDF4 test file. */ +int test_read_write(int dtype) +{ + if (create_hdf_file(dtype)) ERR; + return read_hdf_file(dtype); } -/*! Standard main function. - * - */ -int main() { +int main() +{ + printf("\n***Test reading from an hdf4 file with a little-endian datatype...\n"); + printf("*** testing reading..."); + { + if (test_read_write(DFNT_LINT8)) ERR; + if (test_read_write(DFNT_LUINT8)) ERR; + if (test_read_write(DFNT_LINT16)) ERR; + if (test_read_write(DFNT_LUINT16)) ERR; + if (test_read_write(DFNT_LINT32)) ERR; + if (test_read_write(DFNT_LUINT32)) ERR; + if (test_read_write(DFNT_LFLOAT32)) ERR; + /* if (test_read_write(DFNT_LFLOAT64)) ERR; */ + } + SUMMARIZE_ERR; - int res = 0; + /* printf("*** testing for True Negatives. these will return error..."); */ + /* { */ + /* /\* True Negatives. *\/ */ + /* if (test_read_write(DFNT_INT8) != -1) ERR; */ + /* if (test_read_write(DFNT_UINT8) != -1) ERR; */ + /* if (test_read_write(DFNT_INT16) != -1) ERR; */ + /* if (test_read_write(DFNT_UINT16) != -1) ERR; */ + /* if (test_read_write(DFNT_INT32) != -1) ERR; */ + /* if (test_read_write(DFNT_UINT32) != -1) ERR; */ + /* if (test_read_write(DFNT_FLOAT32) != -1) ERR; */ + /* if (test_read_write(DFNT_FLOAT64) != -1) ERR; */ + /* } */ + /* SUMMARIZE_ERR; */ - printf("Test reading from an hdf4 file with a little-endian datatype.\n"); - - /* True Positives. */ - res = test_read_write(DFNT_LINT8); - res = test_read_write(DFNT_LUINT8); - res = test_read_write(DFNT_LINT16); - res = test_read_write(DFNT_LUINT16); - res = test_read_write(DFNT_LINT32); - res = test_read_write(DFNT_LUINT32); - res = test_read_write(DFNT_LFLOAT32); - res = test_read_write(DFNT_LFLOAT64); - - /* True Negatives. */ - printf("\t**** Testing for True Negatives. THESE SHOULD FAIL.****\n\n"); - res = test_read_write(DFNT_INT8); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_UINT8); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_INT16); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_UINT16); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_INT32); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_UINT32); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_FLOAT32); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - res = test_read_write(DFNT_FLOAT64); - if(!res) {printf("Should have failed. Error!\n"); return -1;} - - printf("Finished.\n"); - return 0; + FINAL_RESULTS; }