diff options
author | Bill Wendling <wendling@ncsa.uiuc.edu> | 2000-10-09 20:41:03 (GMT) |
---|---|---|
committer | Bill Wendling <wendling@ncsa.uiuc.edu> | 2000-10-09 20:41:03 (GMT) |
commit | 452ee91027b4bf93d8afccaf948e685bb1341da8 (patch) | |
tree | 5b5bd992ee57b3738d9c56a1e8903afc78b07508 /tools | |
parent | 2c7f74e08812fdcb6d746257be478041a6414b32 (diff) | |
download | hdf5-452ee91027b4bf93d8afccaf948e685bb1341da8.zip hdf5-452ee91027b4bf93d8afccaf948e685bb1341da8.tar.gz hdf5-452ee91027b4bf93d8afccaf948e685bb1341da8.tar.bz2 |
[svn-r2643] Purpose:
Adding Testing
Description:
Alignment when putting elements in a compound datatype can be
off.
Solution:
This was a bug which I'd fixed. Here's a program to exercise the
bug.
Platforms tested:
Linux
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Makefile.in | 16 | ||||
-rw-r--r-- | tools/talign.c | 164 |
2 files changed, 173 insertions, 7 deletions
diff --git a/tools/Makefile.in b/tools/Makefile.in index 30f67b8..912b5ff 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -14,7 +14,7 @@ srcdir=@srcdir@ CPPFLAGS=-I. -I$(srcdir) -I../src -I$(top_srcdir)/src @CPPFLAGS@ ## Test programs and scripts. -TEST_PROGS= +TEST_PROGS=talign TEST_SCRIPTS=$(srcdir)/testh5dump.sh @TESTH5TOH4@ @TESTH4TOH5@ ## These are our main targets: library and tools. We link this library @@ -32,16 +32,15 @@ LIB_OBJ=$(LIB_SRC:.c=.lo) PUB_LIB= ## Source and object files for h4toh5 converter. -H4TOH5_SRC=h4toh5main.c h4toh5vgroup.c h4toh5vdata.c h4toh5sds.c h4toh5image.c h4toh5pal.c \ - h4toh5anno.c h4toh5util.c +H4TOH5_SRC=h4toh5main.c h4toh5vgroup.c h4toh5vdata.c h4toh5sds.c \ + h4toh5image.c h4toh5pal.c h4toh5anno.c h4toh5util.c H4TOH5_OBJ=$(H4TOH5_SRC:.c=.lo) H4TOH5_HDR=h4toh5main.h h4toh5util.h ## Source and object files for programs... -PROG_SRC=h5debug.c h5import.c h5ls.c h5repart.c h5dump.c \ - h5toh4.c h5dumptst.c pdb2hdf.c \ - $(H4TOH5_SRC) -PROG_OBJ=$(PROG_SRC:.c=.lo) $(H4TOH5_OBJ) +PROG_SRC=h5debug.c h5import.c h5ls.c h5repart.c h5dump.c h5toh4.c \ + h5dumptst.c pdb2hdf.c talign.c $(H4TOH5_SRC) +PROG_OBJ=$(PROG_SRC:.c=.lo) talign.lo $(H4TOH5_OBJ) PRIVATE_HDR=h5tools.h $(H4TOH5_HDR) ## Source and object files for the tests @@ -82,4 +81,7 @@ h4toh5: $(H4TOH5_OBJ) pdb2hdf: pdb2hdf.lo @$(LT_LINK_EXE) $(CFLAGS) -o $@ pdb2hdf.lo $(LIB) $(LIBHDF5) $(LDFLAGS) $(LIBS) +talign: talign.lo + @$(LT_LINK_EXE) $(CFLAGS) -o $@ talign.lo $(LIB) $(LIBHDF5) $(LDFLAGS) $(LIBS) + @CONCLUDE@ diff --git a/tools/talign.c b/tools/talign.c new file mode 100644 index 0000000..7c9b64b --- /dev/null +++ b/tools/talign.c @@ -0,0 +1,164 @@ +/* + * Small program to illustrate the "misalignment" of members within a compound + * datatype, in a datatype fixed by h5dump_fixtype(). + */ +#include <string.h> +#include <stdlib.h> +#include <unistd.h> /* Required for unlink() */ + +#include "hdf5.h" + +const char *fname = "talign.h5"; +const char *setname = "align"; + +/* + * This program assumes that there is no extra space between the members 'Ok' + * and 'Not Ok', (there shouldn't be because they are of the same atomic type + * H5T_NATIVE_FLOAT, and they are placed within the compound next to one + * another per construction) + */ + +extern hid_t h5dump_fixtype(hid_t); + +int main(void) +{ + hid_t fil,spc,set; + hid_t cs6, cmp, fix; + hid_t cmp1, cmp2, cmp3; + hid_t plist; + + hsize_t dim[2]; + size_t cdim[4]; + + char string5[5]; + float fok[2] = {1234., 2341.}; + float fnok[2] = {5678., 6785.}; + float *fptr; + + char *data; + char *mname; + + int result = 0; + + printf("%-70s", "Testing alignment in compound datatypes"); + + strcpy(string5, "Hi!"); + unlink(fname); + fil = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + if (fil < 0) { + puts("*FAILED*"); + return 1; + } + + H5E_BEGIN_TRY { + H5Gunlink(fil, setname); + } H5E_END_TRY; + + cs6 = H5Tcopy(H5T_C_S1); + H5Tset_size(cs6, sizeof(string5)); + H5Tset_strpad(cs6, H5T_STR_NULLPAD); + + cmp = H5Tcreate(H5T_COMPOUND, sizeof(fok) + sizeof(string5) + sizeof(fnok)); + H5Tinsert(cmp, "Awkward length", 0, cs6); + cdim[0] = sizeof(fok) / sizeof(float); + H5Tinsert_array(cmp, "Ok", sizeof(string5), 1, cdim, NULL, H5T_NATIVE_FLOAT); + cdim[0] = sizeof(fnok) / sizeof(float); + H5Tinsert_array(cmp, "Not Ok", sizeof(fok) + sizeof(string5), + 1, cdim, NULL, H5T_NATIVE_FLOAT); + + fix = h5dump_fixtype(cmp); + + cdim[0] = sizeof(fok) / sizeof(float); + cmp1 = H5Tcreate(H5T_COMPOUND, sizeof(fok)); + H5Tinsert_array(cmp1, "Ok", 0, 1, cdim, NULL, H5T_NATIVE_FLOAT); + + cmp2 = H5Tcreate(H5T_COMPOUND, sizeof(string5)); + H5Tinsert(cmp2, "Awkward length", 0, cs6); + + cdim[0] = sizeof(fnok) / sizeof(float); + cmp3 = H5Tcreate(H5T_COMPOUND, sizeof(fnok)); + H5Tinsert_array(cmp3, "Not Ok", 0, 1, cdim, NULL, + H5T_NATIVE_FLOAT); + + plist = H5Pcreate(H5P_DATASET_XFER); + H5Pset_preserve(plist, 1); + + /* + * Create a small dataset, and write data into it we write each field + * in turn so that we are avoid alignment issues at this point + */ + dim[0] = 1; + spc = H5Screate_simple(1, dim, NULL); + set = H5Dcreate(fil, setname, cmp, spc, H5P_DEFAULT); + + H5Dwrite(set, cmp1, spc, H5S_ALL, plist, fok); + H5Dwrite(set, cmp2, spc, H5S_ALL, plist, string5); + H5Dwrite(set, cmp3, spc, H5S_ALL, plist, fnok); + + H5Dclose(set); + + /* Now open the set, and read it back in */ + data = malloc(H5Tget_size(fix)); + + if (!data) { + perror("malloc() failed"); + abort(); + } + + set = H5Dopen(fil, setname); + + H5Dread(set, fix, spc, H5S_ALL, H5P_DEFAULT, data); + fptr = (float *)(data + H5Tget_member_offset(fix, 1)); + + if (fok[0] != fptr[0] || fok[1] != fptr[1] + || fnok[0] != fptr[2] || fnok[1] != fptr[3]) { + result = 1; + printf("%14s (%2d) %6s = %s\n", + mname = H5Tget_member_name(fix, 0), H5Tget_member_offset(fix,0), + string5, (char *)(data + H5Tget_member_offset(fix, 0))); + free(mname); + fptr = (float *)(data + H5Tget_member_offset(fix, 1)); + printf("Data comparison:\n" + "%14s (%2d) %6f = %f\n" + " %6f = %f\n", + mname = H5Tget_member_name(fix, 1), H5Tget_member_offset(fix,1), + fok[0], fptr[0], + fok[1], fptr[1]); + free(mname); + fptr = (float *)(data + H5Tget_member_offset(fix, 2)); + printf("%14s (%2d) %6f = %f\n" + " %6f = %6f\n", + mname = H5Tget_member_name(fix, 2), H5Tget_member_offset(fix,2), + fnok[0], fptr[0], + fnok[1], fptr[1]); + free(mname); + + fptr = (float *)(data + H5Tget_member_offset(fix, 1)); + printf("\n" + "Short circuit\n" + " %6f = %f\n" + " %6f = %f\n" + " %6f = %f\n" + " %6f = %f\n", + fok[0], fptr[0], + fok[1], fptr[1], + fnok[0], fptr[2], + fnok[1], fptr[3]); + puts("*FAILED*"); + } else { + puts(" PASSED"); + } + + free(data); + H5Sclose(spc); + H5Tclose(cmp); + H5Tclose(cmp1); + H5Tclose(cmp2); + H5Tclose(cmp3); + H5Pclose(plist); + H5Fclose(fil); + unlink(fname); + fflush(stdout); + return result; +} |