Add tests for str_countchr, update documentation

This commit is contained in:
Robert Müller 2022-09-29 10:41:22 +02:00
parent b029452e99
commit 3383b7dc0f
2 changed files with 26 additions and 14 deletions

View file

@ -1601,21 +1601,20 @@ const char *str_find(const char *haystack, const char *needle);
*/
const char *str_rchr(const char *haystack, char needle);
/*
Function: str_countchr
Counts the number of occurrences of a character in a string.
/**
* Counts the number of occurrences of a character in a string.
*
* @ingroup Strings
*
* @param haystack String to count in
* @param needle Character to count
Parameters:
haystack - String to count in
needle - Character to count
Returns:
The number of characters in the haystack string matching
the needle character.
Remarks:
- The strings are treated as zero-terminated strings.
*/
* @return The number of characters in the haystack string matching
* the needle character.
*
* @remark The strings are treated as zero-terminated strings.
* @remark The number of zero-terminator characters cannot be counted.
*/
int str_countchr(const char *haystack, char needle);
/*

View file

@ -728,3 +728,16 @@ TEST(Str, RightChar)
EXPECT_EQ(str_rchr(pStr, '\0'), pStr + str_length(pStr));
EXPECT_EQ(str_rchr(pStr, 'y'), nullptr);
}
TEST(Str, CountChar)
{
const char *pStr = "a bb ccc dddd eeeee";
EXPECT_EQ(str_countchr(pStr, 'a'), 1);
EXPECT_EQ(str_countchr(pStr, 'b'), 2);
EXPECT_EQ(str_countchr(pStr, 'c'), 3);
EXPECT_EQ(str_countchr(pStr, 'd'), 4);
EXPECT_EQ(str_countchr(pStr, 'e'), 5);
EXPECT_EQ(str_countchr(pStr, ' '), 10);
EXPECT_EQ(str_countchr(pStr, '\0'), 0);
EXPECT_EQ(str_countchr(pStr, 'y'), 0);
}