feat(utils): string utils + corresponding tests
This commit is contained in:
parent
453a8ab0da
commit
af1af49ae1
4 changed files with 140 additions and 22 deletions
16
src/utils/string_utils/string_utils.c
Normal file
16
src/utils/string_utils/string_utils.c
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include "utils/string_utils/string_utils.h"
|
||||||
|
|
||||||
|
ssize_t skip_blanks(char **str)
|
||||||
|
{
|
||||||
|
if (str == NULL || *str == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ssize_t skipped = 0;
|
||||||
|
while (str[skipped] != '\0' && !isblank(str[skipped]))
|
||||||
|
{
|
||||||
|
skipped++;
|
||||||
|
}
|
||||||
|
*str += skipped;
|
||||||
|
return skipped;
|
||||||
|
}
|
||||||
10
src/utils/string_utils/string_utils.h
Normal file
10
src/utils/string_utils/string_utils.h
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef STRING_UTILS_H
|
||||||
|
#define STRING_UTILS_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief: skips blank characters at the beginning of [str].
|
||||||
|
* @return: number of characters skipped.
|
||||||
|
*/
|
||||||
|
ssize_t skip_blanks(char **str);
|
||||||
|
|
||||||
|
#endif /* STRING_UTILS_H */
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#include <criterion/criterion.h>
|
#include <criterion/criterion.h>
|
||||||
#include <criterion/new/assert.h>
|
#include <criterion/new/assert.h>
|
||||||
#include <criterion/redirect.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "lexer/lexer.h"
|
#include "lexer/lexer.h"
|
||||||
|
|
||||||
|
|
@ -10,50 +11,44 @@ TestSuite(token_creation);
|
||||||
|
|
||||||
Test(token_creation, basic)
|
Test(token_creation, basic)
|
||||||
{
|
{
|
||||||
char *input = "Hello World";
|
char input[] = "Hello World";
|
||||||
|
|
||||||
char *actual = new_token(input, 5);
|
char actual[] = new_token(input, 5);
|
||||||
char *expected = "Hello";
|
char expected[] = "Hello";
|
||||||
cr_expect(eq(str, actual, expected));
|
cr_expect(eq(str, actual, expected));
|
||||||
free(actual);
|
free(actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(token_creation, nul)
|
Test(token_creation, nul)
|
||||||
{
|
{
|
||||||
char *input = NULL;
|
char input[] = NULL;
|
||||||
|
|
||||||
char *actual = new_token(input, 5);
|
char actual[] = new_token(input, 5);
|
||||||
char *expected = NULL;
|
cr_expect(actual == NULL);
|
||||||
cr_expect(eq(str, actual, expected));
|
|
||||||
free(actual);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(token_creation, too_large)
|
Test(token_creation, too_large)
|
||||||
{
|
{
|
||||||
char *input = "Hel";
|
char input[] = "Hel";
|
||||||
|
|
||||||
char *actual = new_token(input, 5);
|
char actual[] = new_token(input, 5);
|
||||||
char *expected = NULL;
|
cr_expect(actual == NULL);
|
||||||
cr_expect(eq(str, actual, expected));
|
|
||||||
free(actual);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(token_creation, empty)
|
Test(token_creation, empty)
|
||||||
{
|
{
|
||||||
char *input = "";
|
char input[] = "";
|
||||||
|
|
||||||
char *actual = new_token(input, 5);
|
char actual[] = new_token(input, 5);
|
||||||
char *expected = NULL;
|
cr_expect(actual == NULL);
|
||||||
cr_expect(eq(str, actual, expected));
|
|
||||||
free(actual);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(token_creation, basic_long)
|
Test(token_creation, basic_long)
|
||||||
{
|
{
|
||||||
char *input = "Hello World! This project is a mini shell, I love BIG G.";
|
char input[] = "Hello World! This project is a mini shell, I love BIG G.";
|
||||||
|
|
||||||
char *actual = new_token(input, 42);
|
char actual[] = new_token(input, 42);
|
||||||
char *expected = calloc(42 + 1, sizeof(char));
|
char expected[] = calloc(42 + 1, sizeof(char));
|
||||||
strncpy(input, expected, 42);
|
strncpy(input, expected, 42);
|
||||||
cr_expect(eq(str, actual, expected));
|
cr_expect(eq(str, actual, expected));
|
||||||
free(actual);
|
free(actual);
|
||||||
|
|
|
||||||
97
tests/unit/utils/utils_tests.c
Normal file
97
tests/unit/utils/utils_tests.c
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
#include <criterion/criterion.h>
|
||||||
|
#include <criterion/new/assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "utils/string_utils/string_utils.h"
|
||||||
|
|
||||||
|
TestSuite(string_utils);
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_basic)
|
||||||
|
{
|
||||||
|
char input[] = " Hello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 2;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_noblank)
|
||||||
|
{
|
||||||
|
char input[] = "Hello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 0;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_tab)
|
||||||
|
{
|
||||||
|
char input[] = "\tHello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 1;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_space_tab)
|
||||||
|
{
|
||||||
|
char input[] = " \tHello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 2;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_2tab_1space)
|
||||||
|
{
|
||||||
|
char input[] = "\t \tHello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 3;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_a_lot)
|
||||||
|
{
|
||||||
|
char input[] = "\t \t \tHello World";
|
||||||
|
char expected_str[] = "Hello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 8;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_newline)
|
||||||
|
{
|
||||||
|
char input[] = "\nHello World";
|
||||||
|
char expected_str[] = "\nHello World";
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 0;
|
||||||
|
cr_expect(eq(str, input, expected_str));
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(string_utils, skipblank_nul)
|
||||||
|
{
|
||||||
|
char *input = NULL;
|
||||||
|
|
||||||
|
ssize_t actual = skip_blanks(input);
|
||||||
|
ssize_t expected = 0;
|
||||||
|
cr_expect(input == NULL);
|
||||||
|
cr_expect(actual == expected);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue