Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
maska989 committed Jul 6, 2023
1 parent 4eb55b2 commit 060cdd7
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions libc/string/string_lenchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ TEST(string_len, ascii)
TEST_ASSERT_EQUAL_INT(sz, strnlen(whites, sz + 1));

/* Checking ascii charset */


sz = BUFF_SIZE;
TEST_ASSERT_EQUAL_INT(sz, strlen(asciiSet));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen(asciiSet, sz - 1));
Expand All @@ -116,7 +114,7 @@ TEST(string_len, not_ascii)
unsigned char charSet[BUFF_SIZE] = { 0 };
int sz, i;

/* Checking out of ASCII bytes */
/* Checking out of ASCII bytes */
for (i = sizeof(charSet); i < sizeof(charSet) * 2 - 1; i++) {
charSet[i - sizeof(charSet)] = i;
}
Expand Down Expand Up @@ -169,7 +167,7 @@ TEST(string_spn, basic)

memcpy(holder, pangram, sz + 1);

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

Expand Down Expand Up @@ -255,13 +253,15 @@ TEST(string_spn, big)
TEST_ASSERT_EQUAL_INT(sz, strspn(bigstr, bigstr));

memcpy(support, bigstr, sz);
support[sz - 1] = 'b';
support[sz - 3] = 'b';
TEST_ASSERT_EQUAL_INT(sz, strspn(bigstr, support));
support[sz - 3] = 'a';
TEST_ASSERT_EQUAL_INT(sz, strspn(bigstr, support));

memset(support, 'b', sz);
support[sz - 1] = 'a';
support[sz - 3] = 'a';
TEST_ASSERT_EQUAL_INT(0, strcspn(bigstr, support));
support[sz - 1] = 'b';
support[sz - 3] = 'b';
TEST_ASSERT_EQUAL_INT(sz, strcspn(bigstr, support));
}

Expand All @@ -278,24 +278,36 @@ TEST(string_spn, empty_output)

TEST(string_spn, mixed_order)
{
int i;
char *reversStr,
*testStr;

char strHolder[BUFF_SIZE] = { 0 },
supportSet[BUFF_SIZE] = { 0 };
testStr = testdata_createCharStr(BUFF_SIZE);
reversStr = malloc(BUFF_SIZE + 1);

for (i = 1; i < BUFF_SIZE; i++) {
reversStr[i - 1] = testStr[BUFF_SIZE - i];
}

/* Creating bounds for strings operations remembering we start counting from 0 */
testStr[BUFF_SIZE - 1] = 0;
reversStr[BUFF_SIZE - 1] = 0;

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

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(0, strcspn("abc", "bac"));
TEST_ASSERT_EQUAL_INT(3, strspn("bac", "abc"));

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

TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 3, strcspn(supportSet, strHolder));
TEST_ASSERT_EQUAL_INT(0, strspn(supportSet, strHolder));
/* Last element of set testStr is null terminator zero which means we need to start from one place further */
TEST_ASSERT_EQUAL_INT(0, strcspn(&reversStr[1], testStr));
TEST_ASSERT_EQUAL_INT(BUFF_SIZE - 2, strspn(&reversStr[1], testStr));

free(reversStr);
free(testStr);
}


Expand Down Expand Up @@ -391,12 +403,12 @@ TEST(string_chr, special)
TEST_ASSERT_EQUAL_PTR(specials, strrchr(specials, specials[0]));
TEST_ASSERT_EQUAL_PTR(specials, memchr(specials, specials[0], strlen(specials)));

/* Getting last element from string contains only special characters */
/* Getting the last element from string contains only special characters */
TEST_ASSERT_EQUAL_PTR(&specials[sz], strchr(specials, specials[sz]));
TEST_ASSERT_EQUAL_PTR(&specials[sz], strrchr(specials, specials[sz]));
TEST_ASSERT_EQUAL_PTR(&specials[sz], memchr(specials, specials[sz], sz + 1));

/* Getting middle element from string contains only special characters */
/* Getting the middle element from string contains only special characters */
TEST_ASSERT_EQUAL_PTR(&specials[sz / 2], strchr(specials, specials[sz / 2]));
TEST_ASSERT_EQUAL_PTR(&specials[sz / 2], strrchr(specials, specials[sz / 2]));
TEST_ASSERT_EQUAL_PTR(&specials[sz / 2], memchr(specials, specials[sz / 2], sz + 1));
Expand Down Expand Up @@ -439,7 +451,7 @@ TEST(string_chr, not_ascii)
char charSet[BUFF_SIZE] = { 0 };
int sz, i;

/* Checking out of ASCII bytes */
/* Checking out of ASCII bytes */
for (i = sizeof(charSet); i < sizeof(charSet) * 2 - 1; i++) {
charSet[i - sizeof(charSet)] = i;
}
Expand Down Expand Up @@ -535,7 +547,7 @@ TEST(string_chr, memchr_size)
TEST_ASSERT_EQUAL_PTR(&charSet[i], memchr(charSet, charSet[i], i + 1));
/*
* In this case, the size of the search is the same as the place
* where is search char placed and memchr never meets
* where is the search char placed and memchr never meets
* the criteria to find it
*/
TEST_ASSERT_EQUAL_PTR(NULL, memchr(charSet, charSet[i], i));
Expand Down

0 comments on commit 060cdd7

Please sign in to comment.