feat(utils): string utils + corresponding tests

This commit is contained in:
Matteo Flebus 2026-01-08 17:03:52 +01:00
parent 453a8ab0da
commit af1af49ae1
4 changed files with 140 additions and 22 deletions

View file

@ -1,8 +1,9 @@
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <criterion/redirect.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "lexer/lexer.h"
@ -10,50 +11,44 @@ TestSuite(token_creation);
Test(token_creation, basic)
{
char *input = "Hello World";
char input[] = "Hello World";
char *actual = new_token(input, 5);
char *expected = "Hello";
char actual[] = new_token(input, 5);
char expected[] = "Hello";
cr_expect(eq(str, actual, expected));
free(actual);
}
Test(token_creation, nul)
{
char *input = NULL;
char input[] = NULL;
char *actual = new_token(input, 5);
char *expected = NULL;
cr_expect(eq(str, actual, expected));
free(actual);
char actual[] = new_token(input, 5);
cr_expect(actual == NULL);
}
Test(token_creation, too_large)
{
char *input = "Hel";
char input[] = "Hel";
char *actual = new_token(input, 5);
char *expected = NULL;
cr_expect(eq(str, actual, expected));
free(actual);
char actual[] = new_token(input, 5);
cr_expect(actual == NULL);
}
Test(token_creation, empty)
{
char *input = "";
char input[] = "";
char *actual = new_token(input, 5);
char *expected = NULL;
cr_expect(eq(str, actual, expected));
free(actual);
char actual[] = new_token(input, 5);
cr_expect(actual == NULL);
}
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 *expected = calloc(42 + 1, sizeof(char));
char actual[] = new_token(input, 42);
char expected[] = calloc(42 + 1, sizeof(char));
strncpy(input, expected, 42);
cr_expect(eq(str, actual, expected));
free(actual);