Skip to content

Commit

Permalink
libc: string chr tests improvements
Browse files Browse the repository at this point in the history
JIRA: CI-232
  • Loading branch information
maska989 committed Jun 15, 2023
1 parent 8ab5ca3 commit 82201d9
Showing 1 changed file with 92 additions and 18 deletions.
110 changes: 92 additions & 18 deletions libc/string_lenchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,36 @@ TEST_TEAR_DOWN(string_spn)

TEST(string_spn, ascii)
{
char charSet[BUFF_SIZE] = { 0 };
char supportCharSet[BUFF_SIZE] = { 0 };
char supportCharSet[BUFF_SIZE] = { 0 },
*asciiStr;
int i;

for (i = 1; i < BUFF_SIZE; i++) {
charSet[i - 1] = i;
}
asciiStr = testdata_createCharStr(BUFF_SIZE + 1);


for (i = 1; i < BUFF_SIZE; i++) {
supportCharSet[i - 1] = i;
TEST_ASSERT_EQUAL_INT(i, strcspn(charSet, &charSet[i]));
TEST_ASSERT_EQUAL_INT(i, strspn(charSet, supportCharSet));
/*
* In this case we need to use fully filled set for strcspn
* because it counts size based on elements that are not
* present in himself
*/
TEST_ASSERT_EQUAL_INT(i - 1, strcspn(&asciiStr[1], &asciiStr[i]));
TEST_ASSERT_EQUAL_INT(i, strspn(&asciiStr[1], supportCharSet));
}

free((void *)asciiStr);
}


TEST(string_spn, not_ascii)
{
unsigned char charSet[129] = { 0 };
unsigned char charSet[BUFF_SIZE] = { 0 };
int sz, i;

/* Checking out of ASCII bytes */
for (i = 128; i <= 255; i++) {
charSet[i - 128] = i;
for (i = sizeof(charSet); i < sizeof(charSet) * 2 - 1; i++) {
charSet[i - sizeof(charSet)] = i;
}

sz = sizeof(charSet) - 1;
Expand Down Expand Up @@ -238,19 +244,86 @@ TEST(string_spn, empty_output)

TEST_ASSERT_EQUAL_INT(0, strcspn("\0abc", ""));
TEST_ASSERT_EQUAL_INT(0, strspn("\0abc", ""));
}


TEST(string_spn, mixed_order)
{

char *asciiSet = testdata_createCharStr(BUFF_SIZE),
strHolder[BUFF_SIZE] = { 0 },
supportSet[BUFF_SIZE] = { 0 };
int i;

asciiSet[BUFF_SIZE - 1] = 0;

/*
* In those cases strcspn look for strings which not equal on the other hand strspn
* looking for equal strings
*/
TEST_ASSERT_EQUAL_INT(0, strcspn("a", "a"));
TEST_ASSERT_EQUAL_INT(0, strspn("a", "b"));
TEST_ASSERT_EQUAL_INT(1, strspn("a", "a"));

TEST_ASSERT_EQUAL_INT(0, strcspn("abc", "a"));
TEST_ASSERT_EQUAL_INT(0, strspn("abc", "b"));
TEST_ASSERT_EQUAL_INT(1, strspn("abc", "a"));

TEST_ASSERT_EQUAL_INT(0, strcspn("abc", "cba"));
TEST_ASSERT_EQUAL_INT(3, strspn("abc", "cba"));

TEST_ASSERT_EQUAL_INT(0, strcspn("abc", "abc1"));
TEST_ASSERT_EQUAL_INT(0, strspn("abc", "1"));
TEST_ASSERT_EQUAL_INT(3, strspn("abc", "abc1"));

memset(strHolder, 'a', BUFF_SIZE - 3);
strHolder[BUFF_SIZE - 2] = 'b';
memset(supportSet, 'b', BUFF_SIZE - 3);
supportSet[BUFF_SIZE - 2] = 'a';

TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 3, strcspn(strHolder, supportSet));
TEST_ASSERT_EQUAL_INT(0, strspn(strHolder, supportSet));

TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 3, strcspn(supportSet, strHolder));
TEST_ASSERT_EQUAL_INT(0, strspn(supportSet, strHolder));

memcpy(supportSet, asciiSet, BUFF_SIZE - 1);

for (i = 0; i < BUFF_SIZE - 1; i++) {

/*
* Checking if strcspn and strspn will search
* the whole string before returning value and
* he found an element of impact on the output
*/

/* Setting only the first element of set to values from asciiSet */
supportSet[0] = asciiSet[i];
TEST_ASSERT_EQUAL_INT(0, strcspn(asciiSet, supportSet));

/* After passing halfway through strspn will start to grow up again */
if (i < (BUFF_SIZE - 1) / 2) {
TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 1 - i, strspn(asciiSet, supportSet));
}
else {
TEST_ASSERT_EQUAL_INT(i + 1, strspn(asciiSet, supportSet));
}

/* Changing approach to setup elements, setting them up dynamically depending on the value from end to start */
supportSet[0] = asciiSet[0];
supportSet[BUFF_SIZE - 2 - i] = asciiSet[i];

printf("%c\n", asciiSet[i]);

TEST_ASSERT_EQUAL_INT(0, strcspn(asciiSet, supportSet));

/* Same case as before */
if (i < (BUFF_SIZE - 1) / 2) {
TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 2 - i, strspn(asciiSet, supportSet));
}
else {
TEST_ASSERT_EQUAL_INT(i + 1, strspn(asciiSet, supportSet));
}
}

free((void *)asciiSet);
}


Expand All @@ -262,14 +335,14 @@ TEST(string_spn, out_of_set)

/* Checking if both functions recognize the holder as a different set of elements */
sz = sizeof(pangram) - 1;
TEST_ASSERT_EQUAL_INT(sz, strcspn(pangram, &holder[0]));
TEST_ASSERT_EQUAL_INT(0, strspn(pangram, &holder[0]));
TEST_ASSERT_EQUAL_INT(sz, strcspn(pangram, holder));
TEST_ASSERT_EQUAL_INT(0, strspn(pangram, holder));

memcpy(holder, pangram, sz + 1);

/* Checking if both functions recognize holder as same set of elements */
TEST_ASSERT_EQUAL_INT(0, strcspn(pangram, &holder[0]));
TEST_ASSERT_EQUAL_INT(sz, strspn(pangram, &holder[0]));
TEST_ASSERT_EQUAL_INT(0, strcspn(pangram, holder));
TEST_ASSERT_EQUAL_INT(sz, strspn(pangram, holder));

TEST_ASSERT_EQUAL_INT(0, strcspn(pangram, &holder[sz / 2]));
TEST_ASSERT_EQUAL_INT(1, strspn(pangram, &holder[sz / 2]));
Expand Down Expand Up @@ -541,9 +614,10 @@ TEST_GROUP_RUNNER(string_spn)
{
RUN_TEST_CASE(string_spn, ascii);
RUN_TEST_CASE(string_spn, not_ascii);
RUN_TEST_CASE(string_spn, big);
RUN_TEST_CASE(string_spn, empty_output);
RUN_TEST_CASE(string_spn, mixed_order);
RUN_TEST_CASE(string_spn, out_of_set);
RUN_TEST_CASE(string_spn, big);
}


Expand Down

0 comments on commit 82201d9

Please sign in to comment.