From d7cb8aca5b4d07f9d04cd5aba618d4ec4dda7ac2 Mon Sep 17 00:00:00 2001 From: jhendersonHDF Date: Fri, 19 May 2023 11:05:01 -0500 Subject: Add test for HDstrcasestr macro (#2115) --- src/H5system.c | 22 ++++++++++++++++++++++ src/H5win32defs.h | 3 ++- test/th5_system.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/H5system.c b/src/H5system.c index d6d734f..ecaa0f6 100644 --- a/src/H5system.c +++ b/src/H5system.c @@ -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 diff --git a/src/H5win32defs.h b/src/H5win32defs.h index a1299b1..1b65c79 100644 --- a/src/H5win32defs.h +++ b/src/H5win32defs.h @@ -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 */ diff --git a/test/th5_system.c b/test/th5_system.c index 784cb3f..4c652c1 100644 --- a/test/th5_system.c +++ b/test/th5_system.c @@ -447,15 +447,21 @@ 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"); @@ -463,6 +469,37 @@ test_h5_strcasestr(void) } 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) { #ifdef H5_HAVE_WIN32_API @@ -521,6 +558,7 @@ test_h5_system(void) test_h5_dirname(); test_h5_basename(); test_h5_strcasestr(); + test_HDstrcasestr(); test_h5_strndup(); } -- cgit v0.12