From 1c45b31cc674a38e32a87a168d50249a6875f821 Mon Sep 17 00:00:00 2001 From: Raymond Lu Date: Wed, 17 Oct 2012 13:38:38 -0500 Subject: [svn-r22914] I checked in the conversion function H5T__conv_enum_numeric (numeric type means integer or floating number) and the C test for it. This is the work for Chicago. Tested on jam, koala, and ostrich. --- src/H5T.c | 2 ++ src/H5Tconv.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/H5Tpkg.h | 4 +++ test/enum.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 182 insertions(+), 9 deletions(-) diff --git a/src/H5T.c b/src/H5T.c index fb9cf25..5801c3f 100644 --- a/src/H5T.c +++ b/src/H5T.c @@ -1060,6 +1060,8 @@ H5T_init_interface(void) status |= H5T_register(H5T_PERS_SOFT, "struct(no-opt)", compound, compound, H5T__conv_struct, H5AC_dxpl_id, FALSE); status |= H5T_register(H5T_PERS_SOFT, "struct(opt)", compound, compound, H5T__conv_struct_opt, H5AC_dxpl_id, FALSE); status |= H5T_register(H5T_PERS_SOFT, "enum", enum_type, enum_type, H5T__conv_enum, H5AC_dxpl_id, FALSE); + status |= H5T_register(H5T_PERS_SOFT, "enum_i", enum_type, fixedpt, H5T__conv_enum_numeric, H5AC_dxpl_id, FALSE); + status |= H5T_register(H5T_PERS_SOFT, "enum_f", enum_type, floatpt, H5T__conv_enum_numeric, H5AC_dxpl_id, FALSE); status |= H5T_register(H5T_PERS_SOFT, "vlen", vlen, vlen, H5T__conv_vlen, H5AC_dxpl_id, FALSE); status |= H5T_register(H5T_PERS_SOFT, "array", array, array, H5T__conv_array, H5AC_dxpl_id, FALSE); status |= H5T_register(H5T_PERS_SOFT, "objref", objref, objref, H5T__conv_order_opt, H5AC_dxpl_id, FALSE); diff --git a/src/H5Tconv.c b/src/H5Tconv.c index 983922f..b0827f8 100644 --- a/src/H5Tconv.c +++ b/src/H5Tconv.c @@ -2889,6 +2889,89 @@ done: /*------------------------------------------------------------------------- + * Function: H5T__conv_enum_numeric + * + * Purpose: Converts enumerated data to a numeric type (integer or + * floating-point number). This function is registered into + * the conversion table twice in H5T_init_interface in H5T.c. + * Once for enum-integer conversion. Once for enum-float conversion. + * + * Return: Success: Non-negative + * + * Failure: negative + * + * Programmer: Raymond Lu + * 12 October 2012 + *------------------------------------------------------------------------- + */ +herr_t +H5T__conv_enum_numeric(hid_t src_id, hid_t dst_id, H5T_cdata_t *cdata, size_t nelmts, + size_t buf_stride, size_t UNUSED bkg_stride, void *_buf, + void UNUSED *bkg, hid_t UNUSED dxpl_id) +{ + H5T_t *src = NULL, *dst = NULL; /*src and dst datatypes */ + H5T_t *src_parent = NULL; /*parent type for src */ + hid_t src_parent_id = -1; /*ID for parent of the source */ + H5T_path_t *tpath; /* Conversion information */ + herr_t ret_value = SUCCEED; /* Return value */ + + FUNC_ENTER_PACKAGE + + switch(cdata->command) { + case H5T_CONV_INIT: + /* + * Determine if this conversion function applies to the conversion + * path SRC_ID->DST_ID. If not, return failure. + */ + if(NULL == (src = (H5T_t *)H5I_object(src_id)) || NULL == (dst = (H5T_t *)H5I_object(dst_id))) + HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "not a datatype") + if(H5T_ENUM != src->shared->type) + HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "source type is not a H5T_ENUM datatype") + if(H5T_INTEGER != dst->shared->type && H5T_FLOAT != dst->shared->type) + HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "destination is not an integer type") + + cdata->need_bkg = H5T_BKG_NO; + break; + + case H5T_CONV_FREE: + break; + + case H5T_CONV_CONV: + if(NULL == (src = (H5T_t *)H5I_object(src_id)) || NULL == (dst = (H5T_t *)H5I_object(dst_id))) + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a datatype") + + src_parent = src->shared->parent; + if(H5T_INTEGER != src_parent->shared->type) + HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "the base type of the source enum is not an integer type") + + if(NULL == (tpath = H5T_path_find(src_parent, dst, NULL, NULL, dxpl_id, FALSE))) { + HGOTO_ERROR(H5E_DATASET, H5E_UNSUPPORTED, FAIL, "unable to convert between src and dest datatype") + } else if (!H5T_path_noop(tpath)) { + if((src_parent_id = H5I_register(H5I_DATATYPE, H5T_copy(src_parent, H5T_COPY_ALL), FALSE)) < 0) + HGOTO_ERROR(H5E_DATASET, H5E_CANTREGISTER, FAIL, "unable to register types for conversion") + } + + /* Convert the data */ + if(H5T_convert(tpath, src_parent_id, dst_id, nelmts, buf_stride, bkg_stride, _buf, bkg, dxpl_id) < 0) + HGOTO_ERROR(H5E_DATATYPE, H5E_CANTINIT, FAIL, "datatype conversion failed") + + /* Release the temporary datatype IDs used */ + if(src_parent_id >= 0) + H5I_dec_ref(src_parent_id); + + break; + + default: + /* Some other command we don't know about yet.*/ + HGOTO_ERROR(H5E_DATATYPE, H5E_UNSUPPORTED, FAIL, "unknown conversion command") + } /* end switch */ + +done: + FUNC_LEAVE_NOAPI(ret_value) +} /* end H5T__conv_enum_numeric() */ + + +/*------------------------------------------------------------------------- * Function: H5T__conv_vlen * * Purpose: Converts between VL datatypes in memory and on disk. diff --git a/src/H5Tpkg.h b/src/H5Tpkg.h index b9364d6..8323e15 100644 --- a/src/H5Tpkg.h +++ b/src/H5Tpkg.h @@ -544,6 +544,10 @@ H5_DLL herr_t H5T__conv_enum(hid_t src_id, hid_t dst_id, H5T_cdata_t *cdata, size_t nelmts, size_t buf_stride, size_t bkg_stride, void *buf, void *bkg, hid_t dset_xfer_plist); +H5_DLL herr_t H5T__conv_enum_numeric(hid_t src_id, hid_t dst_id, H5T_cdata_t *cdata, + size_t nelmts, size_t buf_stride, + size_t bkg_stride, void *buf, void *bkg, + hid_t dset_xfer_plist); H5_DLL herr_t H5T__conv_vlen(hid_t src_id, hid_t dst_id, H5T_cdata_t *cdata, size_t nelmts, size_t buf_stride, size_t bkg_stride, void *buf, void *bkg, diff --git a/test/enum.c b/test/enum.c index 3684102..c0526d8 100644 --- a/test/enum.c +++ b/test/enum.c @@ -109,9 +109,9 @@ test_named(hid_t file) /*------------------------------------------------------------------------- - * Function: test_noconv + * Function: test_conv * - * Purpose: Tests creation of datasets when no conversion is present. + * Purpose: Tests writing and read data * * Return: Success: 0 * @@ -119,24 +119,32 @@ test_named(hid_t file) * * Programmer: Robb Matzke * Monday, January 4, 1999 + * + * Raymond Lu + * 12 October 2012 + * I added tests for enum-integer and enum-float conversions *------------------------------------------------------------------------- */ static int -test_noconv(hid_t file) +test_conv(hid_t file) { hid_t cwg=-1, type=-1, space=-1, dset=-1; c_e1 val; + /* Some values are out of range for testing. The library should accept them */ static c_e1 data1[]={E1_RED, E1_GREEN, E1_BLUE, E1_GREEN, E1_WHITE, E1_WHITE, E1_BLACK, E1_GREEN, E1_BLUE, E1_RED, E1_RED, E1_BLUE, E1_GREEN, E1_BLACK, E1_WHITE, - E1_RED, E1_WHITE, E1_GREEN, E1_GREEN, E1_BLUE}; + E1_RED, E1_WHITE, 0, -1, -2}; c_e1 data2[NELMTS(data1)]; + short data_short[NELMTS(data1)]; + int data_int[NELMTS(data1)]; + double data_double[NELMTS(data1)]; hsize_t ds_size[1]={NELMTS(data1)}; size_t i; - TESTING("no-conversion datasets"); + TESTING("enumeration conversions"); - if((cwg = H5Gcreate2(file, "test_noconv", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + if((cwg = H5Gcreate2(file, "test_conv", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR if((type = H5Tcreate(H5T_ENUM, sizeof(c_e1))) < 0) FAIL_STACK_ERROR if(H5Tenum_insert(type, "RED", CPTR(val, E1_RED )) < 0) FAIL_STACK_ERROR @@ -146,20 +154,96 @@ test_noconv(hid_t file) if(H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK)) < 0) FAIL_STACK_ERROR if((space = H5Screate_simple(1, ds_size, NULL)) < 0) FAIL_STACK_ERROR - if((dset = H5Dcreate2(cwg, "color_table", type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + /*************************************** + * Dataset of enumeration type + ***************************************/ + /* Create a dataset of enum type and write enum data to it */ + if((dset = H5Dcreate2(cwg, "color_table1", type, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR if(H5Dwrite(dset, type, space, space, H5P_DEFAULT, data1) < 0) FAIL_STACK_ERROR + + /* Test reading back the data with no conversion */ if(H5Dread(dset, type, space, space, H5P_DEFAULT, data2) < 0) FAIL_STACK_ERROR for(i = 0; i < (size_t)ds_size[0]; i++) if(data1[i] != data2[i]) { H5_FAILED(); - printf(" data1[%lu]=%d, data2[%lu]=%d (should be same)\n", + printf(" 1. data1[%lu]=%d, data2[%lu]=%d (should be same)\n", (unsigned long)i, (int)(data1[i]), (unsigned long)i, (int)(data2[i])); goto error; } /* end if */ + /* Test converting the data to integer. Read enum data back as integer */ + if(H5Dread(dset, H5T_NATIVE_SHORT, space, space, H5P_DEFAULT, data_short) < 0) FAIL_STACK_ERROR + + for(i = 0; i < (size_t)ds_size[0]; i++) + if(data1[i] != data_short[i]) { + H5_FAILED(); + printf(" 2. data1[%lu]=%d, data_short[%lu]=%d (should be same)\n", + (unsigned long)i, (int)(data1[i]), + (unsigned long)i, (int)(data_short[i])); + goto error; + } /* end if */ + + /* Test converting the data to floating number. Read enum data back as floating number */ + if(H5Dread(dset, H5T_NATIVE_DOUBLE, space, space, H5P_DEFAULT, data_double) < 0) FAIL_STACK_ERROR + + for(i = 0; i < (size_t)ds_size[0]; i++) + if(data1[i] != (int)data_double[i]) { + H5_FAILED(); + printf(" 3. data1[%lu]=%d, data_double[%lu]=%d (should be same)\n", + (unsigned long)i, (int)(data1[i]), + (unsigned long)i, (int)(data_double[i])); + goto error; + } /* end if */ + + if(H5Dclose(dset) < 0) FAIL_STACK_ERROR + + /*************************************** + * Dataset of integer type + ***************************************/ + /* Create a dataset of native integer and write enum data to it */ + if((dset = H5Dcreate2(cwg, "color_table2", H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + if(H5Dwrite(dset, type, space, space, H5P_DEFAULT, data1) < 0) FAIL_STACK_ERROR + + /* Test reading back the data with no conversion */ + if(H5Dread(dset, H5T_NATIVE_INT, space, space, H5P_DEFAULT, data_int) < 0) FAIL_STACK_ERROR + + for(i = 0; i < (size_t)ds_size[0]; i++) + if(data1[i] != data_int[i]) { + H5_FAILED(); + printf(" 4. data1[%lu]=%d, data_int[%lu]=%d (should be same)\n", + (unsigned long)i, (int)(data1[i]), + (unsigned long)i, (int)(data_int[i])); + goto error; + } /* end if */ + if(H5Dclose(dset) < 0) FAIL_STACK_ERROR + + /*************************************** + * Dataset of double type + ***************************************/ + /* Create a dataset of native double and write enum data to it */ + if((dset = H5Dcreate2(cwg, "color_table3", H5T_NATIVE_DOUBLE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + if(H5Dwrite(dset, type, space, space, H5P_DEFAULT, data1) < 0) FAIL_STACK_ERROR + + /* Test reading back the data with no conversion */ + if(H5Dread(dset, H5T_NATIVE_DOUBLE, space, space, H5P_DEFAULT, data_double) < 0) FAIL_STACK_ERROR + + for(i = 0; i < (size_t)ds_size[0]; i++) + if(data1[i] != (int)data_double[i]) { + H5_FAILED(); + printf(" 5. data1[%lu]=%d, data_double[%lu]=%d (should be same)\n", + (unsigned long)i, (int)(data1[i]), + (unsigned long)i, (int)(data_double[i])); + goto error; + } /* end if */ + + if(H5Dclose(dset) < 0) FAIL_STACK_ERROR + if(H5Sclose(space) < 0) FAIL_STACK_ERROR if(H5Tclose(type) < 0) FAIL_STACK_ERROR if(H5Gclose(cwg) < 0) FAIL_STACK_ERROR @@ -572,7 +656,7 @@ main(void) /* Tests */ nerrors += test_named(file); - nerrors += test_noconv(file); + nerrors += test_conv(file); nerrors += test_tr1(file); nerrors += test_tr2(file); nerrors += test_value_dsnt_exist(); -- cgit v0.12