Skip to content

Commit

Permalink
libc: strchr tests: add test group for ASCII compare
Browse files Browse the repository at this point in the history
JIRA: CI-232
  • Loading branch information
maska989 committed Jun 5, 2023
1 parent 57b8927 commit 83665d6
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 64 deletions.
3 changes: 2 additions & 1 deletion libc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void runner(void)
RUN_TEST_GROUP(test_pthread_cond);
RUN_TEST_GROUP(strtod_family);
RUN_TEST_GROUP(stdlib_alloc);
RUN_TEST_GROUP(string_len);
RUN_TEST_GROUP(stdlib_env);
RUN_TEST_GROUP(ctype);
RUN_TEST_GROUP(stdio_scanf_d);
Expand All @@ -54,6 +53,8 @@ void runner(void)
RUN_TEST_GROUP(stdio_scanf_squareBrackets);
RUN_TEST_GROUP(stdio_scanf_rest);
RUN_TEST_GROUP(string_chr);
RUN_TEST_GROUP(string_len);
RUN_TEST_GROUP(string_spn);
}


Expand Down
251 changes: 188 additions & 63 deletions libc/string_lenchr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*
* POSIX.1-2017 standard library functions tests
* HEADER:
* - string.h
* - string.h
* TESTED:
* - strlen()
* - strnlen()
* - strchr()
* - strrchr()
* - memchr()
* - strlen()
* - strnlen()
* - strchr()
* - strrchr()
* - memchr()
*
* Copyright 2023 Phoenix Systems
* Author: Damian Modzelewski
Expand All @@ -21,18 +21,16 @@


#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <unity_fixture.h>

#define BUFF_SIZE 128

TEST_GROUP(string_len);
TEST_GROUP(string_chr);
TEST_GROUP(string_spn);


TEST_SETUP(string_len)
Expand All @@ -53,7 +51,7 @@ TEST(string_len, ascii)
doubleNul[] = "\0\0abc",
specials[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
whites[] = " \v\t\r\n";
char asciiSet[128] = { 0 };
char charSet[BUFF_SIZE] = { 0 };
int sz, i;

TEST_ASSERT_EQUAL_INT(0, strlen(""));
Expand Down Expand Up @@ -96,42 +94,33 @@ TEST(string_len, ascii)
TEST_ASSERT_EQUAL_INT(sz, strnlen(whites, sz + 1));

/* Checking ascii charset */
for (i = 1; i < sizeof(asciiSet); i++) {
asciiSet[i - 1] = i;
for (i = 1; i < sizeof(charSet); i++) {
charSet[i - 1] = i;
}

sz = sizeof(asciiSet) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen(asciiSet));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen(asciiSet, sz - 1));
TEST_ASSERT_EQUAL_INT(sz, strnlen(asciiSet, sz));
TEST_ASSERT_EQUAL_INT(sz, strnlen(asciiSet, sz + 1));
sz = sizeof(charSet) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen(charSet));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen(charSet, sz - 1));
TEST_ASSERT_EQUAL_INT(sz, strnlen(charSet, sz));
TEST_ASSERT_EQUAL_INT(sz, strnlen(charSet, sz + 1));
}


TEST(string_len, not_ascii)
{
const char notAsciiString[] = "♦♥♣♠◊⊗こんにちは❉❉⌨⌨⌨⌨⌨⌨⌨⌨❉❉";
unsigned char notAsciiSet[129];
unsigned char charSet[129] = { 0 };
int sz, i;

/* Checking ability to read the chars out of ascii charset */
sz = sizeof(notAsciiString) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen(notAsciiString));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen(notAsciiString, sz - 1));
TEST_ASSERT_EQUAL_INT(sz, strnlen(notAsciiString, sz));
TEST_ASSERT_EQUAL_INT(sz, strnlen(notAsciiString, sz + 1));

/* Checking out of ascii bytes */
/* Checking out of ASCII bytes */
for (i = 128; i <= 255; i++) {
notAsciiSet[i - 128] = i;
charSet[i - 128] = i;
}
notAsciiSet[128] = 0;

sz = sizeof(notAsciiSet) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen((const char *)notAsciiSet));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen((const char *)notAsciiSet, sz - 1));
TEST_ASSERT_EQUAL_INT(sz, strnlen((const char *)notAsciiSet, sz));
TEST_ASSERT_EQUAL_INT(sz, strnlen((const char *)notAsciiSet, sz + 1));
sz = sizeof(charSet) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen((const char *)charSet));
TEST_ASSERT_EQUAL_INT(sz - 1, strnlen((const char *)charSet, sz - 1));
TEST_ASSERT_EQUAL_INT(sz, strnlen((const char *)charSet, sz));
TEST_ASSERT_EQUAL_INT(sz, strnlen((const char *)charSet, sz + 1));
}


Expand All @@ -140,11 +129,10 @@ TEST(string_len, big)
char bigstr[PATH_MAX] = { 0 };
int sz, i;

/* The length of string is not clearly restricted, so we test one of bigger value, which may be used */
/* The length of the string is not restricted, so we test one of bigger value, which may be used */
for (i = 0; i < PATH_MAX - 1; i++) {
bigstr[i] = 'A';
}
bigstr[i] = '\0';

sz = sizeof(bigstr) - 1;
TEST_ASSERT_EQUAL_INT(sz, strlen(bigstr));
Expand All @@ -159,6 +147,140 @@ TEST(string_len, big)
*/


TEST_SETUP(string_spn)
{
}


TEST_TEAR_DOWN(string_spn)
{
}


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

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

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));
}
}


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

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

sz = sizeof(charSet) - 1;
TEST_ASSERT_EQUAL_INT(sz, strcspn((const char *)charSet, ""));
TEST_ASSERT_EQUAL_INT(sz, strspn((const char *)charSet, (const char *)charSet));
}


TEST(string_spn, big)
{
char bigstr[PATH_MAX] = { 0 };
int sz;

/*
* The length of the string is not restricted,
* so we test one of the bigger values, which may be used
* with remembering the last element must be a null term
* zero to collect the data as the string from
* declared space, that's why we memset one place less than its size
*/
memset(bigstr, 'a', PATH_MAX - 1);

sz = sizeof(bigstr) - 1;
TEST_ASSERT_EQUAL_INT(sz, strcspn(bigstr, ""));
TEST_ASSERT_EQUAL_INT(sz, strspn(bigstr, bigstr));
}


TEST(string_spn, empty_output)
{
char emptySet[BUFF_SIZE] = { 0 };

TEST_ASSERT_EQUAL_INT(0, strcspn(emptySet, emptySet));
TEST_ASSERT_EQUAL_INT(0, strspn(emptySet, emptySet));

TEST_ASSERT_EQUAL_INT(0, strcspn(emptySet, ""));
TEST_ASSERT_EQUAL_INT(0, strspn(emptySet, ""));

TEST_ASSERT_EQUAL_INT(0, strcspn(emptySet, "abc"));
TEST_ASSERT_EQUAL_INT(0, strspn(emptySet, "abc"));

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

TEST_ASSERT_EQUAL_INT(0, strcspn("", ""));
TEST_ASSERT_EQUAL_INT(0, strspn("", ""));

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

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

/*
* 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(0, strcspn("abc", "a"));
TEST_ASSERT_EQUAL_INT(0, strspn("abc", "b"));

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


TEST(string_spn, out_of_set)
{
char pangram[45] = " The quick brown fox jumps over the lazy dog",
holder[45] = { 0 };
int sz;

/* 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]));

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[sz / 2]));
TEST_ASSERT_EQUAL_INT(1, strspn(pangram, &holder[sz / 2]));

TEST_ASSERT_EQUAL_INT(sz, strcspn(pangram, &holder[sz]));
TEST_ASSERT_EQUAL_INT(0, strspn(pangram, &holder[sz]));
}


/*
////////////////////////////////////////////////////////////////////////////////////
*/


TEST_SETUP(string_chr)
{
}
Expand Down Expand Up @@ -231,7 +353,7 @@ TEST(string_chr, special)

/*
* Testing did strchr don't stop if found special signs
* Getting first element from string contains only special characters
* Getting the first element from the string contains only special characters
*/

sz = strlen(specials);
Expand All @@ -256,6 +378,7 @@ TEST(string_chr, special)
TEST_ASSERT_EQUAL_STRING(NULL, memchr(specials, 'I', sz));
}


TEST(string_chr, ascii)
{
int sz, i;
Expand Down Expand Up @@ -283,35 +406,27 @@ TEST(string_chr, ascii)

TEST(string_chr, not_ascii)
{
const char notAsciiString[] = "♥♣♠◊⊗こんにちは❉❉⌨⌨⌨⌨⌨⌨⌨⌨❉❉♦x";
char notAsciiSet[129];
char charSet[129] = { 0 };
int sz, i;

/* Checking ability to read the chars out of ascii charset */
sz = sizeof(notAsciiString) - 1;
TEST_ASSERT_EQUAL_STRING("x", strchr(notAsciiString, 'x'));
TEST_ASSERT_EQUAL_STRING("x", strrchr(notAsciiString, 'x'));
TEST_ASSERT_EQUAL_STRING("x", memchr(notAsciiString, 'x', sz));

/* Checking out of ascii bytes */
/* Checking out of ASCII bytes */
for (i = 128; i <= 255; i++) {
notAsciiSet[i - 128] = i;
charSet[i - 128] = i;
}
notAsciiSet[128] = 0;

/* Testing capability of functions to hold and read not ascii set */
sz = sizeof(notAsciiSet);
TEST_ASSERT_EQUAL_STRING(notAsciiSet, strchr(notAsciiSet, notAsciiSet[0]));
TEST_ASSERT_EQUAL_STRING(notAsciiSet, strrchr(notAsciiSet, notAsciiSet[0]));
TEST_ASSERT_EQUAL_STRING(notAsciiSet, memchr(notAsciiSet, notAsciiSet[0], sz));

TEST_ASSERT_EQUAL_STRING(&notAsciiSet[64], strchr(notAsciiSet, notAsciiSet[64]));
TEST_ASSERT_EQUAL_STRING(&notAsciiSet[64], strrchr(notAsciiSet, notAsciiSet[64]));
TEST_ASSERT_EQUAL_STRING(&notAsciiSet[64], memchr(notAsciiSet, notAsciiSet[64], sz));

TEST_ASSERT_EQUAL_STRING(&notAsciiSet[sz - 1], strchr(notAsciiSet, notAsciiSet[sz - 1]));
TEST_ASSERT_EQUAL_STRING(&notAsciiSet[sz - 1], strrchr(notAsciiSet, notAsciiSet[sz - 1]));
TEST_ASSERT_EQUAL_STRING(&notAsciiSet[sz - 1], memchr(notAsciiSet, notAsciiSet[sz - 1], sz));
sz = sizeof(charSet);
TEST_ASSERT_EQUAL_STRING(charSet, strchr(charSet, charSet[0]));
TEST_ASSERT_EQUAL_STRING(charSet, strrchr(charSet, charSet[0]));
TEST_ASSERT_EQUAL_STRING(charSet, memchr(charSet, charSet[0], sz));

TEST_ASSERT_EQUAL_STRING(&charSet[64], strchr(charSet, charSet[64]));
TEST_ASSERT_EQUAL_STRING(&charSet[64], strrchr(charSet, charSet[64]));
TEST_ASSERT_EQUAL_STRING(&charSet[64], memchr(charSet, charSet[64], sz));

TEST_ASSERT_EQUAL_STRING(&charSet[sz - 1], strchr(charSet, charSet[sz - 1]));
TEST_ASSERT_EQUAL_STRING(&charSet[sz - 1], strrchr(charSet, charSet[sz - 1]));
TEST_ASSERT_EQUAL_STRING(&charSet[sz - 1], memchr(charSet, charSet[sz - 1], sz));
}


Expand Down Expand Up @@ -343,7 +458,7 @@ TEST(string_chr, whitespaces)
whites[] = " \n\t\e\r\b\v\f\\";
int sz;

/* Checking if white signs doesn't make any interference on functions output */
/* Checking if white signs don't make any interference on functions output */
sz = sizeof(whites) - 1;
TEST_ASSERT_EQUAL_STRING(&whites[sz], strchr(whites, whites[sz]));
TEST_ASSERT_EQUAL_STRING(&whites[sz], strrchr(whites, whites[sz]));
Expand Down Expand Up @@ -385,7 +500,7 @@ TEST(string_chr, memchr_size)
{
char lorem[] = "Lorem";

/* Testing capability of setting size in memchr */
/* Testing capability of setting the size in memchr */
TEST_ASSERT_EQUAL_STRING("rem", memchr(lorem, 'r', 6));
TEST_ASSERT_EQUAL_STRING("rem", memchr(lorem, 'r', 5));
TEST_ASSERT_EQUAL_STRING("rem", memchr(lorem, 'r', 4));
Expand All @@ -409,6 +524,16 @@ TEST_GROUP_RUNNER(string_len)
}


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, out_of_set);
}


TEST_GROUP_RUNNER(string_chr)
{
RUN_TEST_CASE(string_chr, basic);
Expand Down

0 comments on commit 83665d6

Please sign in to comment.