Add test for HDstrcasestr macro (#2115)

This commit is contained in:
jhendersonHDF 2023-05-19 11:05:01 -05:00 committed by GitHub
parent 14edcfaffd
commit d7cb8aca5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 4 deletions

View File

@ -961,6 +961,28 @@ H5_strndup(const char *s, size_t n)
done:
FUNC_LEAVE_NOAPI(ret_value)
}
/*-------------------------------------------------------------------------
* Function: Wstrcasestr_wrap
*
* Purpose: Windows wrapper function for strcasestr to retain GNU
* behavior where searching for an empty substring returns the
* input string being searched. StrStrIA on Windows does not
* exhibit this same behavior.
*
* Return: Pointer to input string if 'needle' is the empty substring
* Otherwise, returns StrStrIA(haystack, needle)
*
*-------------------------------------------------------------------------
*/
char *
Wstrcasestr_wrap(const char *haystack, const char *needle)
{
if (needle && !*needle)
return haystack;
else
return StrStrIA(haystack, needle);
}
#endif /* H5_HAVE_WIN32_API */
/* dirname() and basename() are not easily ported to Windows and basename

View File

@ -81,7 +81,7 @@ struct timezone {
#define HDsleep(S) Sleep(S * 1000)
#define HDstat(S, B) _stati64(S, B)
#define HDstrcasecmp(A, B) _stricmp(A, B)
#define HDstrcasestr(A, B) StrStrIA(A, B)
#define HDstrcasestr(A, B) Wstrcasestr_wrap(A, B)
#define HDstrndup(S, N) H5_strndup(S, N)
#define HDstrtok_r(X, Y, Z) strtok_s(X, Y, Z)
#define HDtzset() _tzset()
@ -106,6 +106,7 @@ H5_DLL int Wopen_utf8(const char *path, int oflag, ...);
H5_DLL int Wremove_utf8(const char *path);
H5_DLL int H5_get_win32_times(H5_timevals_t *tvs);
H5_DLL char *H5_strndup(const char *s, size_t n);
H5_DLL char *Wstrcasestr_wrap(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -447,21 +447,58 @@ test_h5_strcasestr(void)
/* check that H5_strcasestr returns target in empty search */
str = H5_strcasestr(haystack, "");
CHECK_PTR_EQ(str, haystack, "H5_strcasestr search for empty");
CHECK_PTR(str, "H5_strcasestr for empty substring");
if (str)
VERIFY_STR(str, haystack, "comparing H5_strcasestr to original string for empty substring");
/* Check that H5_strcasestr find a string of same case */
str = H5_strcasestr(haystack, "string");
CHECK_PTR_EQ(str, &(haystack[8]), "H5_strcasestr search same case");
CHECK_PTR(str, "H5_strcasestr for substring of same case");
if (str)
VERIFY_STR(str, "string", "comparing H5_strcasestr for substring of same case");
/* Check that H5_strcasestr find a string of different case */
str = H5_strcasestr(haystack, "sTrInG");
CHECK_PTR_EQ(str, &(haystack[8]), "H5_strcasestr search different case");
CHECK_PTR(str, "H5_strcasestr for substring of different case");
if (str)
VERIFY_STR(str, "string", "comparing H5_strcasestr for substring of different case");
/* Check that H5_strcasestr returns NULL if no match is found */
str = H5_strcasestr(haystack, "nomatch");
CHECK_PTR_NULL(str, "H5_strcasestr search with no match");
}
static void
test_HDstrcasestr(void)
{
const char *const haystack = "My test string";
char *str = NULL;
MESSAGE(5, ("Testing HDstrcasestr\n"));
/* check that HDstrcasestr returns target in empty search */
str = HDstrcasestr(haystack, "");
CHECK_PTR(str, "HDstrcasestr for empty substring");
if (str)
VERIFY_STR(str, haystack, "comparing HDstrcasestr to original string for empty substring");
/* Check that HDstrcasestr find a string of same case */
str = HDstrcasestr(haystack, "string");
CHECK_PTR(str, "HDstrcasestr for substring of same case");
if (str)
VERIFY_STR(str, "string", "comparing HDstrcasestr for substring of same case");
/* Check that HDstrcasestr find a string of different case */
str = HDstrcasestr(haystack, "sTrInG");
CHECK_PTR(str, "HDstrcasestr for substring of different case");
if (str)
VERIFY_STR(str, "string", "comparing HDstrcasestr for substring of different case");
/* Check that HDstrcasestr returns NULL if no match is found */
str = HDstrcasestr(haystack, "nomatch");
CHECK_PTR_NULL(str, "HDstrcasestr search with no match");
}
static void
test_h5_strndup(void)
{
@ -521,6 +558,7 @@ test_h5_system(void)
test_h5_dirname();
test_h5_basename();
test_h5_strcasestr();
test_HDstrcasestr();
test_h5_strndup();
}