diff options
author | Raymond Lu <songyulu@hdfgroup.org> | 2011-11-10 20:08:39 (GMT) |
---|---|---|
committer | Raymond Lu <songyulu@hdfgroup.org> | 2011-11-10 20:08:39 (GMT) |
commit | 6e0f6e4ebc5aefc9ccce51a1bf2de37d1ab98dc0 (patch) | |
tree | c90fef8de757da386b25914dac6a92cd33816590 | |
parent | 68da0f773642748d996b498e28b0d39dc3b08710 (diff) | |
download | hdf5-6e0f6e4ebc5aefc9ccce51a1bf2de37d1ab98dc0.zip hdf5-6e0f6e4ebc5aefc9ccce51a1bf2de37d1ab98dc0.tar.gz hdf5-6e0f6e4ebc5aefc9ccce51a1bf2de37d1ab98dc0.tar.bz2 |
[svn-r21736] Issue 7582 - The library allowed the conversion of strings between ASCII and UTF8. I added more test cases to the previous commit. Now it has conversion from UTF8 to ASCII, ASCII to UTF8, VL and fixed length, and H5Tconvert.
Tested on jam, koala, linew.
-rw-r--r-- | test/dtypes.c | 129 |
1 files changed, 107 insertions, 22 deletions
diff --git a/test/dtypes.c b/test/dtypes.c index 648650f..23a7bac 100644 --- a/test/dtypes.c +++ b/test/dtypes.c @@ -113,6 +113,8 @@ typedef enum dtype_t { /* Constant for testing conversion of UTF-8 characters */ #define UTF8_DATASET "utf8" #define UTF8_DATASET2 "2nd_utf8" +#define ASCII_DATASET "ascii" +#define ASCII_DATASET2 "2nd_ascii" /* Count opaque conversions */ static int num_opaque_conversions_g = 0; @@ -7026,23 +7028,40 @@ error: } /* end test_deprec() */ #endif /* H5_NO_DEPRECATED_SYMBOLS */ -int test_utf_conv() + +/*------------------------------------------------------------------------- + * Function: test_utf_ascii_conv + * + * Purpose: Make sure the library doesn't conversion strings between + * ASCII and UTF8. + * + * Return: Success: 0 + * Failure: number of errors + * + * Programmer: Raymond Lu + * 10 November 2011 + *------------------------------------------------------------------------- + */ +int test_utf_ascii_conv(void) { hid_t fid; hid_t did; hid_t utf8_vtid, ascii_vtid; hid_t utf8_tid, ascii_tid; hid_t sid; - const char *utf8 = "foo!"; - char *ascii = NULL; - char ascii2[4]; + const char *utf8_w = "foo!"; + char *ascii_r = NULL; + const char *ascii_w = "bar!"; + char *utf8_r = NULL; + + char ascii2[4], utf8_2[4]; herr_t status; TESTING("string conversion between ASCII and UTF"); - /*********************************** - * Test VL string conversion - ***********************************/ + /************************************************ + * Test VL string conversion from UTF8 to ASCII + ************************************************/ /* Create a variable-length string */ if((utf8_vtid = H5Tcopy(H5T_C_S1)) < 0) FAIL_STACK_ERROR @@ -7059,37 +7078,70 @@ int test_utf_conv() /* Set the character set for the string to ASCII (should already be so) */ if((status = H5Tset_cset(ascii_vtid, H5T_CSET_ASCII) < 0)) FAIL_STACK_ERROR + /* Test conversion in memory */ + H5E_BEGIN_TRY { + status = H5Tconvert(utf8_vtid, ascii_vtid, 1, (void *)utf8_w, NULL, H5P_DEFAULT); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + /* Create a file */ if((fid = H5Fcreate(FILENAME[10], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Create a scalar dataspace for the dataset */ if((sid = H5Screate(H5S_SCALAR)) < 0) FAIL_STACK_ERROR - /* Create a dataset */ + /* Create a dataset of UTF8 string type */ if((did = H5Dcreate2(fid, UTF8_DATASET, utf8_vtid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Write the UTF8 string, as UTF8 */ - if((status = H5Dwrite(did, utf8_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8)) < 0) FAIL_STACK_ERROR + if((status = H5Dwrite(did, utf8_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8_w)) < 0) FAIL_STACK_ERROR /* Read the UTF8 string, as ASCII, supposed to fail */ H5E_BEGIN_TRY { - status = H5Dread(did, ascii_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &ascii); + status = H5Dread(did, ascii_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &ascii_r); } H5E_END_TRY if(status >= 0) FAIL_STACK_ERROR + /* Close the dataset */ + if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR + + /************************************************ + * Test VL string conversion from ASCII to UTF8 + ************************************************/ + /* Test conversion in memory */ + H5E_BEGIN_TRY { + status = H5Tconvert(ascii_vtid, utf8_vtid, 1, (void *)ascii_w, NULL, H5P_DEFAULT); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + + /* Create a dataset of ASCII string type */ + if((did = H5Dcreate2(fid, ASCII_DATASET, ascii_vtid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + /* Write the ASCII string, as ASCII */ + if((status = H5Dwrite(did, ascii_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &ascii_w)) < 0) FAIL_STACK_ERROR + + /* Read the ASCII string, as UTF8, supposed to fail */ + H5E_BEGIN_TRY { + status = H5Dread(did, utf8_vtid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8_r); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + + /* Close the dataset */ + if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR + /* Close the UTF8 VL-string datatype */ if((status = H5Tclose(utf8_vtid)) < 0) FAIL_STACK_ERROR /* Close the ASCII VL-string datatype */ if((status = H5Tclose(ascii_vtid)) < 0) FAIL_STACK_ERROR - /* Close the dataset */ - if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR - - /*************************************** - * Test fixed-length string conversion - ***************************************/ + /********************************************************** + * Test fixed-length string conversion from UTF8 to ASCII + **********************************************************/ /* Create a fixed-length UTF8 string */ if((utf8_tid = H5Tcopy(H5T_C_S1)) < 0) FAIL_STACK_ERROR @@ -7106,28 +7158,61 @@ int test_utf_conv() /* Set the character set for the string to ASCII (should already be so) */ if((status = H5Tset_cset(ascii_tid, H5T_CSET_ASCII) < 0)) FAIL_STACK_ERROR + /* Test conversion in memory */ + H5E_BEGIN_TRY { + status = H5Tconvert(utf8_tid, ascii_tid, 1, utf8_2, NULL, H5P_DEFAULT); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + /* Create a dataset */ if((did = H5Dcreate2(fid, UTF8_DATASET2, utf8_tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Write the UTF8 string, as UTF8 */ - if((status = H5Dwrite(did, utf8_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8)) < 0) FAIL_STACK_ERROR + if((status = H5Dwrite(did, utf8_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8_w)) < 0) FAIL_STACK_ERROR /* Read the UTF8 string as ASCII, supposed to fail */ H5E_BEGIN_TRY { - status = H5Dread(did, ascii_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, ascii2); + status = H5Dread(did, ascii_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &ascii2); } H5E_END_TRY if(status >= 0) FAIL_STACK_ERROR + /* Close the dataset */ + if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR + + /********************************************************** + * Test fixed-length string conversion from ASCII to UTF8 + **********************************************************/ + /* Test conversion in memory */ + H5E_BEGIN_TRY { + status = H5Tconvert(ascii_tid, utf8_tid, 1, ascii2, NULL, H5P_DEFAULT); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + + /* Create a dataset */ + if((did = H5Dcreate2(fid, ASCII_DATASET2, ascii_tid, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR + + /* Write the ASCII string, as ASCII */ + if((status = H5Dwrite(did, ascii_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &ascii_w)) < 0) FAIL_STACK_ERROR + + /* Read the UTF8 string as ASCII, supposed to fail */ + H5E_BEGIN_TRY { + status = H5Dread(did, utf8_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &utf8_2); + } H5E_END_TRY + if(status >= 0) + FAIL_STACK_ERROR + + /* Close the dataset */ + if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR + /* Close the UTF8 string datatype */ if((status = H5Tclose(utf8_tid)) < 0) FAIL_STACK_ERROR /* Close the ASCII string datatype */ if((status = H5Tclose(ascii_tid)) < 0) FAIL_STACK_ERROR - /* Close the dataset */ - if((status = H5Dclose(did)) < 0) FAIL_STACK_ERROR - /* Close the dataspace */ if((status = H5Sclose(sid)) < 0) FAIL_STACK_ERROR @@ -7233,7 +7318,7 @@ main(void) nerrors += test_bitfield_funcs(); nerrors += test_opaque(); nerrors += test_set_order(); - nerrors += test_utf_conv(); + nerrors += test_utf_ascii_conv(); if(nerrors) { printf("***** %lu FAILURE%s! *****\n", |