Skip to content

Commit

Permalink
Add test for HDstrcasestr macro (#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendersonHDF committed Jul 27, 2023
1 parent 44ff239 commit e537b17
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/H5system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/H5win32defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 */
Expand Down
44 changes: 41 additions & 3 deletions test/th5_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -521,6 +558,7 @@ test_h5_system(void)
test_h5_dirname();
test_h5_basename();
test_h5_strcasestr();
test_HDstrcasestr();
test_h5_strndup();
}

Expand Down

0 comments on commit e537b17

Please sign in to comment.