/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 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/HDF5/releases. * * If you do not have access to either file, you may request a copy from * * help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include "h5hltest.h" #include "H5srcdir.h" #include "H5TBpublic.h" #define TEST_FILE_BE "test_table_be.h5" #define TEST_FILE_LE "test_table_le.h5" #define TEST_FILE_CRAY "test_table_cray.h5" /*------------------------------------------------------------------------- * Table API test * * Functions tested: * * H5TBmake_table * H5TBread_table * H5TBwrite_records * H5TBread_records * H5TBappend_records * H5TBinsert_record * H5TBdelete_record * H5TBcombine_tables * H5TBwrite_fields_name * H5TBread_fields_name * H5TBwrite_fields_index * H5TBinsert_field * H5TBdelete_field * H5TBget_table_info * H5TBget_field_info * *------------------------------------------------------------------------- */ #define TITLE "Title" #define NFIELDS 5 #define NRECORDS 8 #define NRECORDS_ADD 3 /*------------------------------------------------------------------------- * structure used for all tests, a particle with properties *------------------------------------------------------------------------- */ typedef struct particle_t { char name[16]; long longi; float pressure; double temperature; int lati; } particle_t; /*------------------------------------------------------------------------- * a subset of particle_t, with latitude and longitude fields *------------------------------------------------------------------------- */ typedef struct position_t { long longi; int lati; } position_t; /*------------------------------------------------------------------------- * a subset of particle_t, with name and pressure fields *------------------------------------------------------------------------- */ typedef struct namepressure_t { char name[16]; float pressure; } namepressure_t; /*------------------------------------------------------------------------- * an extended particle, used in the insert field test *------------------------------------------------------------------------- */ typedef struct particle2_t { char name[16]; long longi; float pressure; double temperature; int lati; int new_field; } particle2_t; /*------------------------------------------------------------------------- * a particle with one field less, used in the delete field test *------------------------------------------------------------------------- */ typedef struct particle3_t { char name[16]; long longi; double temperature; int lati; } particle3_t; /*------------------------------------------------------------------------- * a particle, used in the delete field test differing memory layout *------------------------------------------------------------------------- */ /* Push current alignment rule forcing 4-byte alignment boundary * to the internal stack ... */ #pragma pack(push,4) typedef struct particle4_t { uint32_t state; double posx; double posy; float atx[3]; float aty[3]; float rro[2]; } particle4_t; /* * ... and restore original alignment rules from stack */ #pragma pack(pop) /*------------------------------------------------------------------------- * function to open an HDF5 file and return its file identifier *------------------------------------------------------------------------- */ static hid_t h5file_open(const char *fname, unsigned flags) { hid_t fid; /* identifier for the file */ const char *data_file = H5_get_srcdir_filename(fname); /* open */ if ((fid = H5Fopen(data_file,flags,H5P_DEFAULT))<0) { HDfprintf(stderr,"Error: Cannot open file <%s>\n",data_file ); HDexit(1); } return fid; } /*------------------------------------------------------------------------- * function that compares one particle *------------------------------------------------------------------------- */ static int cmp_par(hsize_t i, hsize_t j, particle_t *rbuf, particle_t *wbuf ) { if ( ( HDstrcmp( rbuf[i].name, wbuf[j].name ) != 0 ) || rbuf[i].lati != wbuf[j].lati || rbuf[i].longi != wbuf[j].longi || !H5_FLT_ABS_EQUAL(rbuf[i].pressure,wbuf[j].pressure) || !H5_DBL_ABS_EQUAL(rbuf[i].temperature,wbuf[j].temperature) ) { HDfprintf(stderr,"read and write buffers have differences\n"); HDfprintf(stderr,"%s %ld %f %f %d\n", rbuf[i].name,rbuf[i].longi,(double)rbuf[i].pressure,rbuf[i].temperature,rbuf[i].lati); HDfprintf(stderr,"%s %ld %f %f %d\n", wbuf[j].name,wbuf[j].longi,(double)wbuf[j].pressure,wbuf[j].temperature,wbuf[j].lati); return -1; } return 0; } /*------------------------------------------------------------------------- * function to compare deleted records *------------------------------------------------------------------------- */ static int compare_deleted(hsize_t rrecords, hsize_t dstart, hsize_t drecords, particle_t *rbuf, particle_t *wbuf) { hsize_t i,j; for( i=0; i= istart && i < istart + irecords) { j = i - istart; if (cmp_par(i,j,rbuf,ibuf)<0) goto out; } else if ( i >= istart + irecords && i < 10 ) { j = i - irecords; if (cmp_par(i,j,rbuf,wbuf)<0) goto out; } else { j = i - 10; if (cmp_par(i,j,rbuf,abuf)<0) goto out; } } PASSED(); } /*------------------------------------------------------------------------- * * Functions tested: * * H5TBdelete_record * H5TBread_records * *------------------------------------------------------------------------- */ if (do_write) { HL_TESTING2("deleting records"); /*------------------------------------------------------------------------- * Create a table * pos = 0 1 2 3 4 5 6 7 * data= 0 1 2 3 4 5 6 7 *------------------------------------------------------------------------- */ for( i=0; i= 2 && i <= 4 ) { if ( rbuf[i].lati != position_in[i-NRECORDS_ADD+1].lati || rbuf[i].longi != position_in[i-NRECORDS_ADD+1].longi || !H5_FLT_ABS_EQUAL(rbuf[i].pressure,pressure_in[i-NRECORDS_ADD+1]) ) { HDfprintf(stderr,"%ld %f %d\n", rbuf[i].longi,(double)rbuf[i].pressure,rbuf[i].lati); HDfprintf(stderr,"%ld %f %d\n", position_in[i].longi,(double)pressure_in[i],position_in[i].lati); goto out; } } } } PASSED(); } /*write*/ /*------------------------------------------------------------------------- * * Functions tested: * * H5TBread_fields_name * *------------------------------------------------------------------------- */ HL_TESTING2("reading fields by name"); /*------------------------------------------------------------------------- * write and read the "Pressure" field *------------------------------------------------------------------------- */ if (do_write) { if (H5TBmake_table(TITLE,fid,"table10",FIELDS,RECORDS,type_size_mem, field_names,field_offset,field_type, chunk_size,fill1,compress,0)<0) goto out; /* write the pressure field to all the records */ start = 0; nrecords = NRECORDS; if ( H5TBwrite_fields_name(fid,"table10","Pressure",start,nrecords, sizeof( float ),0,field_sizes_pre,pressure_in)<0) goto out; } start = 0; nrecords = NRECORDS; /* read an invalid field, should fail */ if ( H5TBread_fields_name(fid,"table10","DoesNotExist",start,nrecords, sizeof(float),0,field_sizes_pre,pressure_out) >=0) goto out; /* read the "Pressure" field */ if ( H5TBread_fields_name(fid,"table10","Pressure",start,nrecords, sizeof(float),0,field_sizes_pre,pressure_out)<0) goto out; /* Compare the extracted table with the initial values */ for ( i = 0; i < NRECORDS; i++ ) { if ( !H5_FLT_ABS_EQUAL(pressure_out[i], pressure_in[i]) ) { goto out; } } /*------------------------------------------------------------------------- * Write and read the "Latitude,Longitude" fields *------------------------------------------------------------------------- */ if (do_write) { /* Write the new longitude and latitude information to all the records */ start = 0; nrecords = NRECORDS_ADD; if ( H5TBwrite_fields_name(fid,"table10", "Latitude,Longitude", start, nrecords, sizeof( position_t ), field_offset_pos, field_sizes_pos, position_in ) < 0 ) goto out; }/*write*/ /* Read the "Latitude,Longitude" fields */ start = 0; nrecords = NRECORDS_ADD; if ( H5TBread_fields_name( fid, "table10", "Latitude,Longitude", start, nrecords, sizeof(position_t), field_offset_pos, field_sizes_pos, position_out ) < 0 ) goto out; /* Compare the extracted table with the initial values */ for( i = 0; i < NRECORDS_ADD; i++ ) { if ( position_out[i].lati != position_in[i].lati || position_out[i].longi != position_in[i].longi ) goto out; } /*------------------------------------------------------------------------- * Write and read the "Name,Pressure" fields *------------------------------------------------------------------------- */ if (do_write) { /* Write the new name and pressure information to all the records */ start = 0; nrecords = NRECORDS; if ( H5TBwrite_fields_name( fid, "table10", "Name,Pressure", start, nrecords, sizeof( namepressure_t ), field_offset_namepre, field_sizes_namepre, namepre_in ) < 0 ) goto out; }/*write*/ /* Read the "Name,Pressure" fields */ start = 0; nrecords = NRECORDS; if ( H5TBread_fields_name( fid, "table10", "Name,Pressure", start, nrecords, sizeof(namepressure_t), field_offset_namepre, field_sizes_namepre, namepre_out ) < 0 ) goto out; /* Compare the extracted table with the initial values */ for( i = 0; i < NRECORDS; i++ ) { if ( ( HDstrcmp( namepre_out[i].name, namepre_in[i].name ) != 0 ) || !H5_FLT_ABS_EQUAL(namepre_out[i].pressure,namepre_in[i].pressure) ) { goto out; } } /* reset buffer */ for( i = 0; i < NRECORDS; i++ ) { HDstrcpy( namepre_out[i].name, "\0" ); namepre_out[i].pressure = -1; } /*------------------------------------------------------------------------- * read only 3 records of the "Name,Pressure" fields, starting at record 2 *------------------------------------------------------------------------- */ start = 2; nrecords = 3; if ( H5TBread_fields_name( fid, "table10", "Name,Pressure", start, nrecords, sizeof(namepressure_t), field_offset_namepre, field_sizes_namepre, namepre_out ) < 0 ) goto out; /* Compare the extracted table with the initial values */ for( i = 0; i < 3; i++ ) { hsize_t iistart = start; if ( ( HDstrcmp( namepre_out[i].name, namepre_in[iistart+i].name ) != 0 ) || !H5_FLT_ABS_EQUAL(namepre_out[i].pressure, namepre_in[iistart+i].pressure) ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * * Functions tested: * * H5TBwrite_fields_index * *------------------------------------------------------------------------- */ if (do_write) { HL_TESTING2("writing fields by index"); /* make an empty table */ if (H5TBmake_table(TITLE,fid,"table11",FIELDS,RECORDS,type_size_mem, field_names,field_offset,field_type, chunk_size,fill,compress,NULL)<0) goto out; /* write the pressure field starting at record 2 */ nfields = 1; start = 2; nrecords = NRECORDS_ADD; if ( H5TBwrite_fields_index(fid, "table11", nfields, field_index_pre, start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in ) < 0 ) goto out; /* write the new longitude and latitude information starting at record 2 */ nfields = 2; start = 2; nrecords = NRECORDS_ADD; if ( H5TBwrite_fields_index(fid, "table11", nfields, field_index_pos, start, nrecords, sizeof( position_t ), field_offset_pos, field_sizes_pos, position_in ) < 0 ) goto out; /* read back the all table */ nfields = 5; start = 0; nrecords = NRECORDS; if ( H5TBread_fields_index(fid, "table11", nfields, field_index, start, nrecords, type_size_mem, field_offset, field_size, rbuf ) < 0 ) goto out; /* Compare the extracted table with the initial values */ for( i = 0; i < NRECORDS; i++ ) { if ( i >= 2 && i <= 4 ) { if ( rbuf[i].lati != position_in[i-NRECORDS_ADD+1].lati || rbuf[i].longi != position_in[i-NRECORDS_ADD+1].longi || !H5_FLT_ABS_EQUAL(rbuf[i].pressure,pressure_in[i-NRECORDS_ADD+1]) ) goto out; } } PASSED(); } /*------------------------------------------------------------------------- * * Functions tested: * * H5TBread_fields_index * *------------------------------------------------------------------------- */ HL_TESTING2("reading fields by index"); if (do_write) { /* make an empty table */ if (H5TBmake_table(TITLE,fid,"table12",FIELDS,RECORDS,type_size_mem, field_names,field_offset,field_type, chunk_size,fill,compress,NULL)<0) goto out; /*------------------------------------------------------------------------- * write and read the "Pressure" field *------------------------------------------------------------------------- */ /* write the pressure field to all the records */ nfields = 1; start = 0; nrecords = NRECORDS; if ( H5TBwrite_fields_index(fid, "table12", nfields, field_index_pre, start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in ) < 0 ) goto out; } /* read the "Pressure" field */ nfields = 1; start = 0; nrecords = NRECORDS; if ( H5TBread_fields_index(fid, "table12", nfields, field_index_pre, start, nrecords, sizeof(float), 0, field_sizes_pre, pressure_out ) < 0 ) goto out; /* compare the extracted table with the initial values */ for( i = 0; i < NRECORDS; i++ ) { if ( !H5_FLT_ABS_EQUAL(pressure_out[i], pressure_in[i]) ) { goto out; } } /*------------------------------------------------------------------------- * write and read the "Latitude,Longitude" fields *------------------------------------------------------------------------- */ if (do_write) { /* write the new longitude and latitude information to all the records */ nfields = 2; start = 0; nrecords = NRECORDS; if ( H5TBwrite_fields_index(fid, "table12", nfields, field_index_pos, start, nrecords, sizeof( position_t ), field_offset_pos, field_sizes_pos, position_in ) < 0 ) goto out; } /*write*/ /* read the "Latitude,Longitude" fields */ nfields = 2; start = 0; nrecords = NRECORDS_ADD; if ( H5TBread_fields_index(fid, "table12", nfields, field_index_pos, start, nrecords, sizeof(position_t), field_offset_pos, field_sizes_pos, position_out ) < 0 ) goto out; /* compare the extracted table with the initial values */ for( i = 0; i < NRECORDS_ADD; i++ ) { if ( position_out[i].lati != position_in[i].lati || position_out[i].longi != position_in[i].longi ) { goto out; } } /*------------------------------------------------------------------------- * write and read the "Name,Pressure" fields *------------------------------------------------------------------------- */ if (do_write) { /* write the new name and pressure information to all the records */ nfields = 2; start = 0; nrecords = NRECORDS; if ( H5TBwrite_fields_index(fid, "table12", nfields, field_index_namepre, start, nrecords, sizeof( namepressure_t ), field_offset_namepre, field_sizes_namepre, namepre_in ) < 0 ) goto out; } /* read the "Name,Pressure" fields */ nfields = 2; start = 0; nrecords = NRECORDS; if ( H5TBread_fields_index(fid, "table12", nfields, field_index_namepre, start, nrecords, sizeof(namepressure_t), field_offset_namepre, field_sizes_namepre, namepre_out ) < 0 ) goto out; /* compare the extracted table with the initial values */ for( i = 0; i < NRECORDS; i++ ) { if ( ( HDstrcmp( namepre_out[i].name, namepre_in[i].name ) != 0 ) || !H5_FLT_ABS_EQUAL(namepre_out[i].pressure,namepre_in[i].pressure) ) { goto out; } } /* reset buffer */ for( i = 0; i < NRECORDS; i++ ) { HDstrcpy( namepre_out[i].name, "\0" ); namepre_out[i].pressure = -1; } /*------------------------------------------------------------------------- * read only 3 records of the "Name,Pressure" fields, starting at record 2 *------------------------------------------------------------------------- */ /* write the new name and pressure information to all the records */ nfields = 2; start = 2; nrecords = 3; if ( H5TBread_fields_index(fid, "table12", nfields, field_index_namepre, start, nrecords, sizeof(namepressure_t), field_offset_namepre, field_sizes_namepre, namepre_out ) < 0 ) goto out; /* compare the extracted table with the initial values */ for( i = 0; i < 3; i++ ) { int iistart = (int) start; if ( ( HDstrcmp( namepre_out[i].name, wbuf[iistart+(int)i].name ) != 0 ) || !H5_FLT_ABS_EQUAL(namepre_out[i].pressure, wbuf[iistart+(int)i].pressure) ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * * Functions tested: * * H5TBinsert_field * *------------------------------------------------------------------------- */ if (do_write) { HL_TESTING2("inserting fields"); /* make a table */ if (H5TBmake_table(TITLE,fid,"table13",FIELDS,RECORDS,type_size_mem, field_names,field_offset,field_type, chunk_size,fill1,compress,wbuf)<0) goto out; /* insert the new field at the end of the field list */ position = NFIELDS; if ( H5TBinsert_field( fid, "table13", "New Field", field_type_new, position, fill1_new, buf_new ) < 0 ) goto out; /* read the table */ if ( H5TBread_table( fid, "table13", dst_size2, dst_offset2, dst_sizes2, rbuf2 ) < 0 ) goto out; /* compare the extracted table with the original array */ for( i = 0; i < NRECORDS; i++ ) { if ( ( HDstrcmp( rbuf2[i].name, wbuf[i].name ) != 0 ) || rbuf2[i].lati != wbuf[i].lati || rbuf2[i].longi != wbuf[i].longi || !H5_FLT_ABS_EQUAL(rbuf2[i].pressure,wbuf[i].pressure) || !H5_DBL_ABS_EQUAL(rbuf2[i].temperature,wbuf[i].temperature) || rbuf2[i].new_field != buf_new[i] ) { goto out; } } PASSED(); } /*------------------------------------------------------------------------- * * Functions tested: * * H5TBdelete_field * *------------------------------------------------------------------------- */ if (do_write) { HL_TESTING2("deleting fields"); /* make a table */ if (H5TBmake_table(TITLE,fid,"table14",FIELDS,RECORDS,type_size_mem, field_names,field_offset,field_type, chunk_size,fill,compress,wbuf)<0) goto out; /* delete the field */ if ( H5TBdelete_field(fid, "table14", "Pressure" ) < 0 ) goto out; /* read the table */ if ( H5TBread_table(fid, "table14", dst_size3, dst_offset3, dst_sizes3, rbuf3 ) < 0 ) goto out; /* compare the extracted table with the original array */ for( i = 0; i < NRECORDS; i++ ) { if ( ( HDstrcmp( rbuf3[i].name, wbuf[i].name ) != 0 ) || rbuf3[i].lati != wbuf[i].lati || rbuf3[i].longi != wbuf[i].longi || !H5_DBL_ABS_EQUAL(rbuf3[i].temperature,wbuf[i].temperature) ) { goto out; } } PASSED(); } /*------------------------------------------------------------------------- * * Functions tested: * * H5TBget_table_info * H5TBget_field_info * *------------------------------------------------------------------------- */ HL_TESTING2("getting table info"); /* get table info */ if ( H5TBget_table_info (fid, "table1", &rfields, &rrecords ) < 0 ) goto out; if ( NFIELDS != rfields || rrecords != NRECORDS ) { goto out; } PASSED(); /*------------------------------------------------------------------------- * * Functions tested: * * H5TBget_field_info * *------------------------------------------------------------------------- */ HL_TESTING2("getting field info"); /* alocate */ names_out = (char**) HDmalloc( sizeof(char*) * (size_t)NFIELDS ); for ( i = 0; i < NFIELDS; i++) { names_out[i] = (char*) HDmalloc( sizeof(char) * 255 ); } /* Get field info */ if ( H5TBget_field_info(fid, "table1", names_out, sizes_out, offset_out, &size_out ) < 0 ) goto out; for ( i = 0; i < NFIELDS; i++) { if ( (HDstrcmp( field_names[i], names_out[i] ) != 0)) { goto out; } } /* release */ for ( i = 0; i < NFIELDS; i++) { HDfree ( names_out[i] ); } HDfree ( names_out ); PASSED(); /*------------------------------------------------------------------------- * end *------------------------------------------------------------------------- */ return 0; out: H5_FAILED(); return -1; } /*------------------------------------------------------------------------- * the main program *------------------------------------------------------------------------- */ int main(void) { hid_t fid; /* identifier for the file */ unsigned flags=H5F_ACC_RDONLY; /*------------------------------------------------------------------------- * test1: create a file for the write/read test *------------------------------------------------------------------------- */ /* create a file using default properties */ fid=H5Fcreate("test_table.h5",H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT); HDputs("Testing table with file creation mode (read/write in native architecture):"); /* test, do write */ if (test_table(fid,1)<0) goto out; /* close */ H5Fclose(fid); /*------------------------------------------------------------------------- * test2: open a file written in test1 on a big-endian machine *------------------------------------------------------------------------- */ HDputs("Testing table with file open mode (read big-endian data):"); fid=h5file_open(TEST_FILE_BE,flags); /* test, do not write */ if (test_table(fid,0)<0) goto out; /* close */ H5Fclose(fid); /*------------------------------------------------------------------------- * test3: open a file written in test1 on a little-endian machine *------------------------------------------------------------------------- */ HDputs("Testing table with file open mode (read little-endian data):"); fid=h5file_open(TEST_FILE_LE,flags); /* test, do not write */ if (test_table(fid,0)<0) goto out; /* close */ H5Fclose(fid); /*------------------------------------------------------------------------- * test4: open a file written in test1 on the Cray T3 machine *------------------------------------------------------------------------- */ HDputs("Testing table with file open mode (read Cray data):"); fid=h5file_open(TEST_FILE_CRAY,flags); /* test, do not write */ if (test_table(fid,0)<0) goto out; /* close */ H5Fclose(fid); return 0; out: H5Fclose(fid); return 1; }