From a254a373db06dc09c54aa7730616cd3528b7951f Mon Sep 17 00:00:00 2001 From: mattjala <124107509+mattjala@users.noreply.github.com> Date: Wed, 10 Jan 2024 17:23:06 -0600 Subject: [PATCH] Test H5Ovisit on file, nested groups, and attributes (#58) --- vol_object_test.c | 255 +++++++++++++++++++++++++++++++++++++++++----- vol_object_test.h | 19 ++-- 2 files changed, 244 insertions(+), 30 deletions(-) diff --git a/vol_object_test.c b/vol_object_test.c index 855e5a7..4b78757 100644 --- a/vol_object_test.c +++ b/vol_object_test.c @@ -51,6 +51,8 @@ static herr_t object_copy_soft_link_expand_callback(hid_t group, const char *nam void *op_data); static herr_t object_visit_callback(hid_t o_id, const char *name, const H5O_info2_t *object_info, void *op_data); +static herr_t object_visit_simple_callback(hid_t o_id, const char *name, const H5O_info2_t *object_info, + void *op_data); static herr_t object_visit_dset_callback(hid_t o_id, const char *name, const H5O_info2_t *object_info, void *op_data); static herr_t object_visit_dtype_callback(hid_t o_id, const char *name, const H5O_info2_t *object_info, @@ -5057,15 +5059,23 @@ test_object_comments_invalid_params(void) static int test_object_visit(void) { - size_t i; - hid_t file_id = H5I_INVALID_HID; - hid_t container_group = H5I_INVALID_HID, group_id = H5I_INVALID_HID; - hid_t group_id2 = H5I_INVALID_HID; - hid_t gcpl_id = H5I_INVALID_HID; - hid_t type_id = H5I_INVALID_HID; - hid_t dset_id = H5I_INVALID_HID; - hid_t dset_dtype = H5I_INVALID_HID; - hid_t fspace_id = H5I_INVALID_HID; + size_t i; + hid_t file_id = H5I_INVALID_HID; + hid_t file_id2 = H5I_INVALID_HID; + hid_t container_group = H5I_INVALID_HID, group_id = H5I_INVALID_HID; + hid_t group_id2 = H5I_INVALID_HID; + hid_t gcpl_id = H5I_INVALID_HID; + hid_t type_id = H5I_INVALID_HID; + hid_t dset_id = H5I_INVALID_HID; + hid_t dset_dtype = H5I_INVALID_HID; + hid_t fspace_id = H5I_INVALID_HID; + hid_t attr_id = H5I_INVALID_HID; + hid_t group_id3 = H5I_INVALID_HID; + hid_t group_id4 = H5I_INVALID_HID; + hid_t group_id5 = H5I_INVALID_HID; + hssize_t num_elems = 0; + size_t elem_size = 0; + char visit_filename[VOL_TEST_FILENAME_MAX_LENGTH]; TESTING_MULTIPART("object visiting"); @@ -5088,6 +5098,15 @@ test_object_visit(void) goto error; } + HDsnprintf(visit_filename, VOL_TEST_FILENAME_MAX_LENGTH, "%s%s", test_path_prefix, + OBJECT_VISIT_TEST_FILE_NAME); + + if ((file_id2 = H5Fcreate(visit_filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) { + H5_FAILED(); + HDprintf(" couldn't open file '%s'\n", OBJECT_VISIT_TEST_FILE_NAME); + goto error; + } + if ((container_group = H5Gopen2(file_id, OBJECT_TEST_GROUP_NAME, H5P_DEFAULT)) < 0) { H5_FAILED(); HDprintf(" couldn't open container group '%s'\n", OBJECT_TEST_GROUP_NAME); @@ -5115,11 +5134,29 @@ test_object_visit(void) goto error; } - if ((fspace_id = generate_random_dataspace(OBJECT_VISIT_TEST_SPACE_RANK, NULL, NULL, FALSE)) < 0) - TEST_ERROR; + /* Make sure not to generate too much data for an attribute to hold */ + do { + if (fspace_id != H5I_INVALID_HID) + H5Sclose(fspace_id); - if ((dset_dtype = generate_random_datatype(H5T_NO_CLASS, FALSE)) < 0) - TEST_ERROR; + if (dset_dtype != H5I_INVALID_HID) + H5Tclose(dset_dtype); + + if ((fspace_id = generate_random_dataspace(OBJECT_VISIT_TEST_SPACE_RANK, NULL, NULL, FALSE)) < 0) { + TEST_ERROR; + } + + if ((dset_dtype = generate_random_datatype(H5T_NO_CLASS, FALSE)) < 0) { + TEST_ERROR; + } + + if ((num_elems = H5Sget_simple_extent_npoints(fspace_id)) < 0) + TEST_ERROR; + + if ((elem_size = H5Tget_size(dset_dtype)) == 0) + TEST_ERROR; + + } while ((num_elems * elem_size) > OBJECT_VISIT_TEST_TOTAL_DATA_SIZE_LIMIT); if ((type_id = generate_random_datatype(H5T_NO_CLASS, FALSE)) < 0) { H5_FAILED(); @@ -5127,6 +5164,14 @@ test_object_visit(void) goto error; } + if ((attr_id = H5Acreate2(group_id, OBJECT_VISIT_TEST_ATTR_NAME, dset_dtype, fspace_id, H5P_DEFAULT, + H5P_DEFAULT)) == H5I_INVALID_HID) { + H5_FAILED(); + HDprintf(" couldn't create attribute '%s' on group '%s'\n", OBJECT_VISIT_TEST_ATTR_NAME, + OBJECT_VISIT_TEST_SUBGROUP_NAME); + goto error; + } + if ((group_id2 = H5Gcreate2(group_id, OBJECT_VISIT_TEST_GROUP_NAME, H5P_DEFAULT, gcpl_id, H5P_DEFAULT)) < 0) { H5_FAILED(); @@ -5134,6 +5179,27 @@ test_object_visit(void) goto error; } + if ((group_id3 = H5Gcreate2(file_id2, OBJECT_VISIT_TEST_GROUP_NAME_PARENT, H5P_DEFAULT, gcpl_id, + H5P_DEFAULT)) < 0) { + H5_FAILED(); + HDprintf(" couldn't create group '%s'\n", OBJECT_VISIT_TEST_GROUP_NAME_PARENT); + goto error; + } + + if ((group_id4 = H5Gcreate2(group_id3, OBJECT_VISIT_TEST_GROUP_NAME_CHILD, H5P_DEFAULT, gcpl_id, + H5P_DEFAULT)) < 0) { + H5_FAILED(); + HDprintf(" couldn't create group '%s'\n", OBJECT_VISIT_TEST_GROUP_NAME_CHILD); + goto error; + } + + if ((group_id5 = H5Gcreate2(group_id4, OBJECT_VISIT_TEST_GROUP_NAME_GRANDCHILD, H5P_DEFAULT, gcpl_id, + H5P_DEFAULT)) < 0) { + H5_FAILED(); + HDprintf(" couldn't create group '%s'\n", OBJECT_VISIT_TEST_GROUP_NAME_GRANDCHILD); + goto error; + } + if ((dset_id = H5Dcreate2(group_id, OBJECT_VISIT_TEST_DSET_NAME, dset_dtype, fspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) { H5_FAILED(); @@ -5266,16 +5332,49 @@ test_object_visit(void) } PART_END(H5Ovisit_create_order_decreasing); + PART_BEGIN(H5Ovisit_group) + { + TESTING_2("H5Ovisit on a group"); + + i = 0; + + if (H5Ovisit3(group_id3, H5_INDEX_CRT_ORDER, H5_ITER_INC, object_visit_simple_callback, &i, + H5O_INFO_ALL) < 0) { + H5_FAILED(); + HDprintf(" H5Ovisit on a group failed!\n"); + PART_ERROR(H5Ovisit_group); + } + + if (i != OBJECT_VISIT_TEST_SUBGROUP_LAYERS) { + H5_FAILED(); + HDprintf(" some objects were not visited!\n"); + PART_ERROR(H5Ovisit_group); + } + + PASSED(); + } + PART_END(H5Ovisit_group); + PART_BEGIN(H5Ovisit_file) { TESTING_2("H5Ovisit on a file ID"); - /* - * XXX: - */ + i = 0; + + if (H5Ovisit3(file_id2, H5_INDEX_CRT_ORDER, H5_ITER_INC, object_visit_simple_callback, &i, + H5O_INFO_ALL) < 0) { + H5_FAILED(); + HDprintf(" H5Ovisit on a file ID failed!\n"); + PART_ERROR(H5Ovisit_file); + } - SKIPPED(); - PART_EMPTY(H5Ovisit_file); + if (i != OBJECT_VISIT_TEST_NUM_OBJS_VISITED) { + H5_FAILED(); + HDprintf(" some objects were not visited!\n"); + PART_ERROR(H5Ovisit_file); + } + + PASSED(); } PART_END(H5Ovisit_file); @@ -5309,6 +5408,30 @@ test_object_visit(void) } PART_END(H5Ovisit_dtype); + PART_BEGIN(H5Ovisit_attr) + { + TESTING_2("H5Ovisit on an attribute") + + i = 0; + + if (H5Ovisit3(attr_id, H5_INDEX_CRT_ORDER, H5_ITER_INC, object_visit_simple_callback, &i, + H5O_INFO_ALL) < 0) { + H5_FAILED(); + HDprintf(" H5Ovisit on an attribute failed!\n"); + PART_ERROR(H5Ovisit_attr); + } + + /* Should have same effect as calling H5Ovisit on group_id */ + if (i != OBJECT_VISIT_TEST_NUM_OBJS_VISITED) { + H5_FAILED(); + HDprintf(" some objects were not visited!\n"); + PART_ERROR(H5Ovisit_attr); + } + + PASSED(); + } + PART_END(H5Ovisit_attr); + PART_BEGIN(H5Ovisit_by_name_obj_name_increasing) { TESTING_2("H5Ovisit_by_name by object name in increasing order"); @@ -5489,12 +5612,22 @@ test_object_visit(void) { TESTING_2("H5Ovisit_by_name on a file ID"); - /* - * XXX: - */ + i = 0; + + if (H5Ovisit_by_name3(file_id2, "/", H5_INDEX_CRT_ORDER, H5_ITER_INC, + object_visit_simple_callback, &i, H5O_INFO_ALL, H5P_DEFAULT) < 0) { + H5_FAILED(); + HDprintf(" H5Ovisit on a file ID failed!\n"); + PART_ERROR(H5Ovisit_by_name_file); + } - SKIPPED(); - PART_EMPTY(H5Ovisit_by_name_file); + if (i != OBJECT_VISIT_TEST_NUM_OBJS_VISITED) { + H5_FAILED(); + HDprintf(" some objects were not visited!\n"); + PART_ERROR(H5Ovisit_by_name_file); + } + + PASSED(); } PART_END(H5Ovisit_by_name_file); @@ -5527,6 +5660,30 @@ test_object_visit(void) PASSED(); } PART_END(H5Ovisit_by_name_dtype); + + PART_BEGIN(H5Ovisit_by_name_attr) + { + TESTING_2("H5Ovisit_by_name on an attribute") + + i = 0; + + if (H5Ovisit_by_name(attr_id, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, object_visit_simple_callback, + &i, H5O_INFO_ALL, H5P_DEFAULT) < 0) { + H5_FAILED(); + HDprintf(" H5Ovisit_by_name on an attribute failed!\n"); + PART_ERROR(H5Ovisit_by_name_attr); + } + + /* Should have same effect as calling H5Ovisit on group_id */ + if (i != OBJECT_VISIT_TEST_NUM_OBJS_VISITED) { + H5_FAILED(); + HDprintf(" some objects were not visited!\n"); + PART_ERROR(H5Ovisit_by_name_attr); + } + + PASSED(); + } + PART_END(H5Ovisit_by_name_attr); } END_MULTIPART; @@ -5544,12 +5701,22 @@ test_object_visit(void) TEST_ERROR; if (H5Gclose(group_id2) < 0) TEST_ERROR; + if (H5Gclose(group_id3) < 0) + TEST_ERROR; + if (H5Gclose(group_id4) < 0) + TEST_ERROR; + if (H5Gclose(group_id5) < 0) + TEST_ERROR; + if (H5Aclose(attr_id) < 0) + TEST_ERROR; if (H5Gclose(group_id) < 0) TEST_ERROR; if (H5Gclose(container_group) < 0) TEST_ERROR; if (H5Fclose(file_id) < 0) TEST_ERROR; + if (H5Fclose(file_id2) < 0) + TEST_ERROR; PASSED(); @@ -5564,9 +5731,14 @@ test_object_visit(void) H5Dclose(dset_id); H5Pclose(gcpl_id); H5Gclose(group_id2); + H5Gclose(group_id3); + H5Gclose(group_id4); + H5Gclose(group_id5); + H5Aclose(attr_id); H5Gclose(group_id); H5Gclose(container_group); H5Fclose(file_id); + H5Fclose(file_id2); } H5E_END_TRY; @@ -7077,6 +7249,26 @@ object_visit_callback(hid_t o_id, const char *name, const H5O_info2_t *object_in return ret_val; } +static herr_t +object_visit_simple_callback(hid_t o_id, const char *name, const H5O_info2_t *object_info, void *op_data) +{ + size_t *i = (size_t *)op_data; + herr_t ret_val = 0; + + UNUSED(o_id); + UNUSED(object_info); + + if (name) + goto done; + + ret_val = -1; + +done: + (*i)++; + + return ret_val; +} + /* * H5Ovisit callback for visiting a singular dataset. */ @@ -7138,6 +7330,14 @@ object_visit_soft_link_callback(hid_t o_id, const char *name, const H5O_info2_t UNUSED(o_id); + if (!HDstrcmp(name, OBJECT_VISIT_TEST_GROUP_NAME_PARENT) || + !HDstrcmp(name, OBJECT_VISIT_TEST_GROUP_NAME_PARENT "/" OBJECT_VISIT_TEST_GROUP_NAME_CHILD) || + !HDstrcmp(name, OBJECT_VISIT_TEST_GROUP_NAME_PARENT "/" OBJECT_VISIT_TEST_GROUP_NAME_CHILD + "/" OBJECT_VISIT_TEST_GROUP_NAME_GRANDCHILD)) { + (*i)--; + goto done; + } + if (!HDstrncmp(name, ".", strlen(".") + 1) && (counter_val <= 5)) { if (H5O_TYPE_GROUP == object_info->type) goto done; @@ -7176,7 +7376,14 @@ object_visit_noop_callback(hid_t o_id, const char *name, const H5O_info2_t *obje static void cleanup_files(void) { - H5Fdelete(OBJECT_COPY_BETWEEN_FILES_TEST_FILE_NAME, H5P_DEFAULT); + char filename[VOL_TEST_FILENAME_MAX_LENGTH]; + + HDsnprintf(filename, VOL_TEST_FILENAME_MAX_LENGTH, "%s%s", test_path_prefix, + OBJECT_COPY_BETWEEN_FILES_TEST_FILE_NAME); + H5Fdelete(filename, H5P_DEFAULT); + + HDsnprintf(filename, VOL_TEST_FILENAME_MAX_LENGTH, "%s%s", test_path_prefix, OBJECT_VISIT_TEST_FILE_NAME); + H5Fdelete(filename, H5P_DEFAULT); } int diff --git a/vol_object_test.h b/vol_object_test.h index 8028671..ad6bb41 100644 --- a/vol_object_test.h +++ b/vol_object_test.h @@ -121,12 +121,19 @@ int vol_object_test(void); #define OBJECT_COPY_INVALID_PARAMS_TEST_GROUP_NAME "object_copy_invalid_params_group" #define OBJECT_COPY_INVALID_PARAMS_TEST_GROUP_NAME2 "object_copy_invalid_params_group_copy" -#define OBJECT_VISIT_TEST_NUM_OBJS_VISITED 4 -#define OBJECT_VISIT_TEST_SUBGROUP_NAME "object_visit_test" -#define OBJECT_VISIT_TEST_SPACE_RANK 2 -#define OBJECT_VISIT_TEST_GROUP_NAME "object_visit_test_group" -#define OBJECT_VISIT_TEST_DSET_NAME "object_visit_test_dset" -#define OBJECT_VISIT_TEST_TYPE_NAME "object_visit_test_type" +#define OBJECT_VISIT_TEST_NUM_OBJS_VISITED 4 +#define OBJECT_VISIT_TEST_SUBGROUP_NAME "object_visit_test" +#define OBJECT_VISIT_TEST_SPACE_RANK 2 +#define OBJECT_VISIT_TEST_GROUP_NAME "object_visit_test_group" +#define OBJECT_VISIT_TEST_DSET_NAME "object_visit_test_dset" +#define OBJECT_VISIT_TEST_TYPE_NAME "object_visit_test_type" +#define OBJECT_VISIT_TEST_ATTR_NAME "object_visit_test_attr" +#define OBJECT_VISIT_TEST_FILE_NAME "object_visit_test_file" +#define OBJECT_VISIT_TEST_SUBGROUP_LAYERS 3 +#define OBJECT_VISIT_TEST_GROUP_NAME_PARENT "object_visit_test_group_parent" +#define OBJECT_VISIT_TEST_GROUP_NAME_CHILD "object_visit_test_group_child" +#define OBJECT_VISIT_TEST_GROUP_NAME_GRANDCHILD "object_visit_test_group_grandchild" +#define OBJECT_VISIT_TEST_TOTAL_DATA_SIZE_LIMIT 32000 #define OBJECT_VISIT_SOFT_LINK_TEST_NUM_OBJS_VISITED 1 #define OBJECT_VISIT_SOFT_LINK_TEST_SUBGROUP_NAME "object_visit_soft_link"