Skip to content

Commit

Permalink
Sync API tests with vol-tests (HDFGroup#3940)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjala authored and jhendersonHDF committed Dec 9, 2024
1 parent c42353a commit d7a5743
Show file tree
Hide file tree
Showing 6 changed files with 2,037 additions and 290 deletions.
270 changes: 270 additions & 0 deletions test/API/H5_api_attribute_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void test_attribute_iterate_datatype(void);
static void test_attribute_iterate_index_saving(void);
static void test_attribute_iterate_invalid_params(void);
static void test_attribute_iterate_0_attributes(void);
static void test_attribute_string_encodings(void);
static void test_delete_attribute(void);
static void test_delete_attribute_invalid_params(void);
static void test_attribute_exists(void);
Expand Down Expand Up @@ -8303,6 +8304,273 @@ test_attribute_iterate_0_attributes(void)
}


/*
* A test to check that attributes preserve data
* correctness for strings with ASCII or UTF-8 char sets
*/
static void
test_attribute_string_encodings(void)
{
hid_t file_id = H5I_INVALID_HID;
hid_t container_group = H5I_INVALID_HID;
hid_t dset_id1 = H5I_INVALID_HID;
hid_t dset_id2 = H5I_INVALID_HID;
hid_t type_id1 = H5I_INVALID_HID;
hid_t type_id2 = H5I_INVALID_HID;
hid_t space_id = H5I_INVALID_HID;
hid_t attr_id1 = H5I_INVALID_HID;
hid_t attr_id2 = H5I_INVALID_HID;
hsize_t dims[ATTRIBUTE_STRING_ENCODINGS_RANK] = {ATTRIBUTE_STRING_ENCODINGS_EXTENT};
size_t ascii_str_size = 0;
size_t utf8_str_size = 0;
char *write_buf = NULL;
char *read_buf = NULL;

TESTING_MULTIPART("string encoding read/write correctness on attributes");

/* Make sure the connector supports the API functions being tested */
if (!(vol_cap_flags_g & H5VL_CAP_FLAG_FILE_BASIC) || !(vol_cap_flags_g & H5VL_CAP_FLAG_GROUP_BASIC) ||
!(vol_cap_flags_g & H5VL_CAP_FLAG_DATASET_BASIC) || !(vol_cap_flags_g & H5VL_CAP_FLAG_ATTR_BASIC)) {
SKIPPED();
printf(" API functions for basic file, group, basic or more dataset aren't supported with this "
"connector\n");
return;
}

TESTING_2("test setup");

ascii_str_size = strlen(ATTRIBUTE_STRING_ENCODINGS_ASCII_STRING);
utf8_str_size = strlen(ATTRIBUTE_STRING_ENCODINGS_UTF8_STRING);

if ((file_id = H5Fopen(H5_API_TEST_FILENAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't open file '%s'\n", H5_API_TEST_FILENAME);
goto error;
}

if ((container_group = H5Gopen2(file_id, ATTRIBUTE_TEST_GROUP_NAME, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't open container group '%s'\n", ATTRIBUTE_TEST_GROUP_NAME);
goto error;
}

if ((space_id = H5Screate_simple(ATTRIBUTE_STRING_ENCODINGS_RANK, dims, NULL)) < 0) {
H5_FAILED();
printf(" couldn't create dataspace\n");
goto error;
}

if ((type_id1 = H5Tcopy(H5T_C_S1)) < 0) {
H5_FAILED();
printf(" couldn't copy builtin string datatype\n");
goto error;
}

if ((H5Tset_size(type_id1, ascii_str_size)) < 0) {
H5_FAILED();
printf(" couldn't set size of string datatype\n");
goto error;
}

if ((H5Tset_cset(type_id1, H5T_CSET_ASCII)) < 0) {
H5_FAILED();
printf(" couldn't set character set of string to ASCII\n");
goto error;
}

if ((dset_id1 = H5Dcreate(container_group, ATTRIBUTE_STRING_ENCODINGS_DSET_NAME1, type_id1, space_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't create dataset with ascii string\n");
goto error;
}

if ((attr_id1 = H5Acreate(dset_id1, ATTRIBUTE_STRING_ENCODINGS_ATTR_NAME1, type_id1, space_id,
H5P_DEFAULT, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't create attribute with ascii string\n");
goto error;
}

if ((type_id2 = H5Tcopy(H5T_C_S1)) < 0) {
H5_FAILED();
printf(" couldn't copy builtin string datatype\n");
goto error;
}

if ((H5Tset_size(type_id2, utf8_str_size)) < 0) {
H5_FAILED();
printf(" couldn't set size of string datatype\n");
goto error;
}

if ((H5Tset_cset(type_id2, H5T_CSET_UTF8)) < 0) {
H5_FAILED();
printf(" couldn't set character set of string to UTF-8\n");
goto error;
}

if ((dset_id2 = H5Dcreate(container_group, ATTRIBUTE_STRING_ENCODINGS_DSET_NAME2, type_id2, space_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't create dataset with UTF-8 string\n");
goto error;
}

if ((attr_id2 = H5Acreate(dset_id2, ATTRIBUTE_STRING_ENCODINGS_ATTR_NAME2, type_id2, space_id,
H5P_DEFAULT, H5P_DEFAULT)) < 0) {
H5_FAILED();
printf(" couldn't create attribute with ascii string\n");
goto error;
}

PASSED();

BEGIN_MULTIPART
{
PART_BEGIN(ASCII_cset)
{
TESTING_2("ASCII character set");
if ((write_buf = calloc(1, ascii_str_size + 1)) == NULL) {
H5_FAILED();
printf(" couldn't allocate memory for write buffer\n");
PART_ERROR(ASCII_cset);
}

memcpy(write_buf, ATTRIBUTE_STRING_ENCODINGS_ASCII_STRING, ascii_str_size);

if ((read_buf = calloc(1, ascii_str_size + 1)) == NULL) {
H5_FAILED();
printf(" couldn't allocate memory for read buffer\n");
PART_ERROR(ASCII_cset);
}

if (H5Awrite(attr_id1, type_id1, write_buf) < 0) {
H5_FAILED();
printf(" couldn't write to attribute with ASCII string\n");
PART_ERROR(ASCII_cset);
}

if (H5Aread(attr_id1, type_id1, read_buf) < 0) {
H5_FAILED();
printf(" couldn't read from attribute with ASCII string\n");
PART_ERROR(ASCII_cset);
}

if (strncmp(write_buf, read_buf, ascii_str_size)) {
H5_FAILED();
printf(" incorrect data read from attribute with ASCII string\n");
PART_ERROR(ASCII_cset);
}

free(write_buf);
write_buf = NULL;

free(read_buf);
read_buf = NULL;

PASSED();
}
PART_END(ASCII_cset);

PART_BEGIN(UTF8_cset)
{
TESTING_2("UTF-8 character set");

if ((write_buf = calloc(1, utf8_str_size + 1)) == NULL) {
H5_FAILED();
printf(" couldn't allocate memory for write buffer\n");
PART_ERROR(UTF8_cset);
}

memcpy(write_buf, ATTRIBUTE_STRING_ENCODINGS_UTF8_STRING, utf8_str_size);

if ((read_buf = calloc(1, utf8_str_size + 1)) == NULL) {
H5_FAILED();
printf(" couldn't allocate memory for read buffer\n");
PART_ERROR(UTF8_cset);
}

if (H5Awrite(attr_id2, type_id2, write_buf) < 0) {
H5_FAILED();
printf(" couldn't write to attribute with UTF-8 string\n");
PART_ERROR(UTF8_cset);
}

if (H5Aread(attr_id2, type_id2, read_buf) < 0) {
H5_FAILED();
printf(" couldn't read from attribute with UTF-8 string\n");
PART_ERROR(UTF8_cset);
}

if (strncmp(write_buf, read_buf, utf8_str_size)) {
H5_FAILED();
printf(" incorrect data read from attribute with UTF-8 string\n");
PART_ERROR(UTF8_cset);
}

free(write_buf);
write_buf = NULL;

free(read_buf);
read_buf = NULL;

PASSED();
}
PART_END(UTF8_cset);

PASSED();
}
END_MULTIPART;

TESTING_2("test cleanup");

if (H5Fclose(file_id) < 0)
TEST_ERROR;
if (H5Gclose(container_group) < 0)
TEST_ERROR;
if (H5Dclose(dset_id1) < 0)
TEST_ERROR;
if (H5Dclose(dset_id2) < 0)
TEST_ERROR;
if (H5Tclose(type_id1) < 0)
TEST_ERROR;
if (H5Tclose(type_id2) < 0)
TEST_ERROR;
if (H5Aclose(attr_id1) < 0)
TEST_ERROR;
if (H5Aclose(attr_id2) < 0)
TEST_ERROR;
if (write_buf)
free(write_buf);
if (read_buf)
free(read_buf);

PASSED();

return;

error:
H5E_BEGIN_TRY
{
H5Fclose(file_id);
H5Gclose(container_group);
H5Dclose(dset_id1);
H5Dclose(dset_id2);
H5Tclose(type_id1);
H5Tclose(type_id2);
H5Aclose(attr_id1);
H5Aclose(attr_id2);
if (write_buf)
free(write_buf);
if (read_buf)
free(read_buf);
}
H5E_END_TRY;

return;
}

/*
* A test to check that an attribute can be deleted
* using H5Adelete(_by_idx).
Expand Down Expand Up @@ -11089,6 +11357,8 @@ H5_api_attribute_test_add(void)
"attribute iteration with invalid parameters", NULL, testframe_flags);
AddTest("test_attribute_iterate_0_attributes", test_attribute_iterate_0_attributes, NULL,
"attribute iteration on object with 0 attributes", NULL, testframe_flags);
AddTest("test_attribute_string_encodings", test_attribute_string_encodings, NULL,
"attribute string encoding correctness", NULL, testframe_flags);
AddTest("test_delete_attribute", test_delete_attribute, NULL, "attribute deletion", NULL, testframe_flags);
AddTest("test_delete_attribute_invalid_params", test_delete_attribute_invalid_params, NULL,
"attribute deletion with invalid parameters", NULL, testframe_flags);
Expand Down
9 changes: 9 additions & 0 deletions test/API/H5_api_attribute_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ void H5_api_attribute_test_add(void);
#define ATTRIBUTE_ITERATE_TEST_0_ATTRIBUTES_SUBGROUP_NAME "attribute_iterate_test_0_attributes"
#define ATTRIBUTE_ITERATE_TEST_0_ATTRIBUTES_DSET_NAME "attribute_iterate_dset"

#define ATTRIBUTE_STRING_ENCODINGS_RANK 1
#define ATTRIBUTE_STRING_ENCODINGS_EXTENT 1
#define ATTRIBUTE_STRING_ENCODINGS_DSET_NAME1 "encoding_dset1"
#define ATTRIBUTE_STRING_ENCODINGS_DSET_NAME2 "encoding_dset2"
#define ATTRIBUTE_STRING_ENCODINGS_ASCII_STRING "asciistr"
#define ATTRIBUTE_STRING_ENCODINGS_UTF8_STRING "αaααaaaα"
#define ATTRIBUTE_STRING_ENCODINGS_ATTR_NAME1 "encoding_attr1"
#define ATTRIBUTE_STRING_ENCODINGS_ATTR_NAME2 "encoding_attr2"

#define ATTRIBUTE_ITERATE_INVALID_PARAMS_TEST_ATTR_SPACE_RANK 1
#define ATTRIBUTE_ITERATE_INVALID_PARAMS_TEST_SUBGROUP_NAME "attribute_iterate_invalid_params_test"
#define ATTRIBUTE_ITERATE_INVALID_PARAMS_TEST_ATTR_NAME "invalid_params_iter_attr1"
Expand Down
Loading

0 comments on commit d7a5743

Please sign in to comment.