removing the libft of rparodi

This commit is contained in:
Raphael 2024-11-08 19:37:30 +01:00
parent 0391323626
commit be6038dcc8
523 changed files with 724 additions and 3336 deletions

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_tests.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/30 13:39:29 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:23:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "tests/str__str_tests.h"
#include "tests/tests.h"
#include <stddef.h>
static const t_test *load_tests01(void)
{
static t_test tb[] = {{"putstr", test_putstr}, {"putendl", test_putendl},
{"putnbr", test_putnbr}, {"itoa", test_itoa}, {"utoa", test_utoa}, \
{"itoa_base", test_itoa_base}, {"atoi", test_atoi}, {"atoi_base", \
test_atoi_base}, {"tok", test_strtok}, {"split", test_split},
{"splits", tests_splits}, {"chr", test_strchr}, {"dup", test_strdup}, \
{"iteri", test_striteri}, {"join", test_strjoin}, {"lcat", test_strlcat}, \
{"lcpy", test_strlcpy}, {"len", test_strlen}, {"mapi", test_strmapi}, \
{"cmp", test_strcmp}, {"ncmp", test_strncmp}, {"ndup", test_strndup}, \
{"nstr", test_strnstr}, {"rchr", test_strrchr}, {"trim", test_strtrim}, \
{"substr", test_substr}, {"replace", test_str_replace}, \
{"replace_chr", test_str_replace_chr}, {"shift_args", test_shift_args}, \
{"gnl", test_gnl}, {"atof", test_atof}, {"isalpha", test_str_isalpha}, \
{"isbool", test_str_isbool}, {"alnum", test_str_isalnum}, \
{"isdigit", test_str_isdigit}, {"isdouble", test_str_isdouble}, \
{"ishex", test_str_ishex}, {"islong", test_str_islong}, \
{"isnum", test_str_isnum}, {"isoct", test_str_isoct}, \
{"isint", test_str_isint}, {"isfloat", test_str_isfloat}, \
{"isvalid", test_str_isvalid}, {"clen", test_strclen}, \
{"cnb", test_strcnb}, {"cspn", test_strcspn}, \
{"end_with", test_strend_with}, {"start_with", test_strstart_with}, \
{"spn", test_strspn}, {"append_c", test_strappend_c},
{NULL, NULL}};
return (tb);
}
int str_tests(void)
{
int collect;
const t_test *tests = load_tests01();
collect = 0;
run_test(tests, &collect);
return (collect);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_atof.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 16:08:52 by bgoulard #+# #+# */
/* Updated: 2024/05/31 16:00:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_atof(void)
{
double res;
size_t i;
const float marge = 0.00001;
const double t_cases[] = {
1.13, 3.14, 0.0, 0.1, 0.0001, -0.2, -0.3, 0.3, 1200, 0,
3.15, -3.15, 0.15
};
const char *t_str[] = {
"1.13", "3.14", "0.0", "0.1", "0.0001",
"-0.2", "-.3", ".3", "1200", "0", " +3.15",
" -3.15", " .15"
};
i = 0;
while (i < sizeof(t_cases) / sizeof(t_cases[0]))
{
res = ft_atof(t_str[i]);
if (res < t_cases[i] - marge || res > t_cases[i] + marge)
return (i + 1);
i++;
}
return (0);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/26 11:45:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_atoi(void)
{
size_t i;
const int t_cases[] = {
0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, -1, -9,
-10, -99, -100, -999, -1000, -9999, -10000
};
const char *t_str[] = {
"0", "1", "9", "10", "99", "100", "999", "1000",
"9999", "10000", "-1", "-9", "-10", "-99", "-100", "-999", "-1000",
"-9999", "-10000"
};
i = 0;
while (i < sizeof(t_cases) / sizeof(t_cases[0]))
{
if (ft_atoi(t_str[i]) != t_cases[i])
return (i + 1);
i++;
}
if (ft_atoi(" --99") != 99 || ft_atoi(" -0") != 0 || \
ft_atoi(" -++--1") != -1)
return (30);
if (ft_atoi("toto") != 0 || ft_atoi("-toto") != 0 || ft_atoi("toto-") \
!= 0 || ft_atoi("a") != 0 || ft_atoi("01234\t56789") != 1234)
return (31);
return (0);
}

View file

@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/26 16:42:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
static int test_base_resps(const char *base, const int *expected_results,
const char **inputs)
{
int i;
int res;
i = 0;
while (inputs[i])
{
res = ft_atoi_base(inputs[i], base);
if (res != expected_results[i])
return (i + 1);
i++;
}
return (0);
}
static int test_spaces(void)
{
if (ft_atoi_base(" 0", "0123456789") != 0)
return (1);
if (ft_atoi_base(" 1", "0123456789") != 1)
return (2);
if (ft_atoi_base(" 9", "0123456789") != 9)
return (3);
if (ft_atoi_base(" 10", "0123456789") != 10)
return (4);
if (ft_atoi_base(" 99", "0123456789") != 99)
return (5);
if (ft_atoi_base(" -99", "0123456789") != -99)
return (6);
if (ft_atoi_base(" -0", "0123456789") != 0)
return (7);
if (ft_atoi_base(" -1", "0123456789") != -1)
return (8);
if (ft_atoi_base(" -10", "0123456789") != -10)
return (9);
if (ft_atoi_base(" -99", "0123456789") != -99)
return (10);
return (0);
}
static int test_multi_sign(void)
{
if (ft_atoi_base(" --99", "0123456789") != 99)
return (1);
if (ft_atoi_base(" -0", "0123456789") != 0)
return (2);
if (ft_atoi_base(" -++--1", "0123456789") != -1)
return (3);
if (ft_atoi_base(" -+--10", "0123456789") != -10)
return (4);
if (ft_atoi_base(" -+--99", "0123456789") != -99)
return (5);
return (0);
}
int test_atoi_base(void)
{
const char *bases[] = {"0123456789", "01", "0123456789abcdef"};
const int expected_results[][11] = {
{0, 1, 9, 10, 99, 0, -1, -9, -10, -99},
{0, 1, 2, 3, 4, 0, -1, -2, -3, -4},
{0, 10, 15, 16, 153, 0, -10, -15, -16, -153}
};
const char *strs[][12] = {
{"0", "1", "9", "10", "99", "0", "-1", "-9", "-10", "-99", NULL},
{"0", "1", "10", "11", "100", "0", "-1", "-10", "-11", "-100", NULL},
{"0", "a", "f", "10", "99", "0", "-a", "-f", "-10", "-99", NULL}
};
if (test_base_resps(bases[0], expected_results[0], strs[0]) || \
test_base_resps(bases[1], expected_results[1], strs[1]) || \
test_base_resps(bases[2], expected_results[2], strs[2]))
return (1);
if (ft_atoi_base("23", "011") || ft_atoi_base("23", "0") || \
ft_atoi_base("23", "1") || ft_atoi_base("23", "0123456789-") || \
ft_atoi_base("23", "0123456789+"))
return (2);
if (test_spaces())
return (3);
if (test_multi_sign())
return (4);
return (0);
}

View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 14:26:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "tests/tests.h"
#include <fcntl.h>
static int write_lines(const char **lines, size_t n)
{
size_t i;
int fd;
fd = open(TESTS_FPREFIX "gnl.txt", O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
return (-1);
i = 0;
while (i < n)
{
write(fd, lines[i], ft_strlen(lines[i]));
i++;
}
close(fd);
return (0);
}
static const char **return_lines(size_t *n)
{
static const char *test_lines[] = {
"Hello World!\n",
"This is Zod!\n",
"Zod shall henceforth be the new earth emperor. Prepare yourself "
"earthilings to the terrible rule of zod the space warrior and self "
"proclaimed emperor of the gallaxy. this is a really long line btw... "
"i sure hope gnl will parse it............................\n",
"Goodbye World!\n",
};
*n = sizeof(test_lines) / sizeof(test_lines[0]);
return (test_lines);
}
int test_gnl(void)
{
int fd;
char *line;
size_t i;
size_t lines_count;
const char **test_lines = return_lines(&lines_count);
line = NULL;
if (write_lines(test_lines, lines_count) < 0)
return (1);
fd = open(TESTS_FPREFIX "gnl.txt", O_RDONLY);
i = 0;
while (i < lines_count)
{
line = get_next_line(fd);
if (ft_strcmp(line, test_lines[i++]) != 0)
return (i + 1);
free(line);
}
line = get_next_line(fd);
if (line)
return (2);
return (free(line), destroy_test_file(fd, TESTS_FPREFIX "gnl.txt"), 0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 14:26:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_string.h"
int test_itoa(void)
{
char *res;
size_t i;
const int t_cases[] = {0, 123, -456, 7890, -12345};
const char *expected_results[] = {"0", "123", "-456", "7890", "-12345"};
i = 0;
while (i < sizeof(t_cases) / sizeof(t_cases[0]))
{
res = ft_itoa(t_cases[i]);
if (ft_strcmp(res, expected_results[i++]) != 0)
return (i);
free(res);
}
return (0);
}

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_itoa_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:24:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_string.h"
#define MAGIC 42
int eval_base(int *t_cases, const char *bases, const char *expected_results[])
{
char *res;
size_t j;
j = 0;
while (t_cases[j] != MAGIC)
{
res = ft_itoa_base(t_cases[j], bases);
if (ft_strcmp(res, expected_results[j]) != 0)
return (free(res), j + 1);
free(res);
j++;
}
return (0);
}
int check_base_error(void)
{
if (ft_itoa_base(0, "0") || ft_itoa_base(0, "010") || \
ft_itoa_base(0, "0123456789+") || ft_itoa_base(0, "0123456789a-") \
|| ft_itoa_base(0, "0123456789a1") || ft_itoa_base(0, "0123456789a ") \
|| ft_itoa_base(0, "0123456789\t"))
return (15);
return (0);
}
int test_itoa_base(void)
{
size_t j;
const int t_cases[] = {0, 123, -456, 7890, -12345, MAGIC};
const char *bases[] = {"0123456789abcdef", "0123456789", "01",
"0123456789ABCDEF"};
const char *expected_results[sizeof(bases)
/ sizeof(bases[0])][sizeof(t_cases) / sizeof(t_cases[0])] = {{"0", "7b",
"-1c8", "1ed2", "-3039"}, {"0", "123", "-456", "7890", "-12345"}, {"0",
"1111011", "-111001000", "1111011010010", "-11000000111001"}, {"0",
"7B", "-1C8", "1ED2", "-3039"}};
j = 0;
while (j < sizeof(bases) / sizeof(bases[0]))
{
if (eval_base((int *)t_cases, bases[j], expected_results[j]) != 0)
return (j + 1);
j++;
}
if (check_base_error() != 0)
return (j + check_base_error());
return (0);
}

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_putendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:07:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "tests/tests.h"
int test_putendl(void)
{
const char *str;
int fd;
char buff[100];
int bread;
const char file_name[] = TESTS_FPREFIX "putendl.txt";
str = "Hello World!";
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
ft_putendl_fd(str, fd);
close(fd);
fd = open(file_name, O_RDONLY);
bread = read(fd, buff, 100);
if (bread < 0 || ft_strncmp(buff, str, ft_strlen(str)) != 0
|| buff[ft_strlen(str)] != '\n')
return (1);
destroy_test_file(fd, file_name);
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
ft_putendl_fd(NULL, fd);
close(fd);
fd = open(file_name, O_RDONLY);
bread = read(fd, buff, 100);
if (bread != 0)
return (2);
destroy_test_file(fd, file_name);
return (0);
}

View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/07/19 21:29:17 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "tests/tests.h"
#include <limits.h>
#include <unistd.h>
static int nb_to_file(const int *nbs, size_t size, const char *file_name)
{
int fd;
size_t i;
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd < 0)
return (-1);
i = 0;
while (i < size)
{
ft_putnbr_fd(nbs[i], fd);
ft_putstr_fd("\n", fd);
i++;
}
close(fd);
return (0);
}
static int file_cmp(const char *file_name, const char *expected)
{
int fd;
char buff[100];
fd = open(file_name, O_RDONLY);
ft_bzero(buff, sizeof(buff));
read(fd, buff, sizeof(buff));
if (ft_strncmp(buff, expected, sizeof(buff)))
return (1);
return (destroy_test_file(fd, file_name), 0);
}
static int test_positives(void)
{
const int t_cases[] = {0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, \
INT_MAX};
const char *expected = "0\n1\n9\n10\n99\n100\n999\n1000\n9999\n10000\n"
"2147483647\n";
const char *file_name = TESTS_FPREFIX "putnbr.txt";
nb_to_file(t_cases, sizeof(t_cases) / sizeof(t_cases[0]), file_name);
return (file_cmp(file_name, expected));
}
static int test_negatives(void)
{
const int nbs[] = {-1, -9, -10, -99, -100, -999, -1000, -9999, -10000, \
INT_MIN};
const char *exp = "-1\n-9\n-10\n-99\n-100\n-999\n-1000\n-9999\n-10000\n"
"-2147483648\n";
const char *file_name = TESTS_FPREFIX "putnbr.txt";
nb_to_file(nbs, sizeof(nbs) / sizeof(nbs[0]), file_name);
return (file_cmp(file_name, exp));
}
int test_putnbr(void)
{
if (test_positives() != 0)
return (1);
if (test_negatives() != 0)
return (2);
return (0);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:08:01 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "tests/tests.h"
int test_putstr(void)
{
const char file_name[] = TESTS_FPREFIX "putstr.txt";
const char *str = "Hello World!";
int fd;
char buff[100];
int bread;
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
ft_putstr_fd(str, fd);
close(fd);
fd = open(file_name, O_RDONLY);
bread = read(fd, buff, 100);
if (bread < 0 || ft_strncmp(buff, str, ft_strlen(str)) != 0)
return (1);
destroy_test_file(fd, file_name);
fd = open(file_name, O_RDWR | O_CREAT | O_TRUNC, 0666);
ft_putstr_fd(NULL, fd);
close(fd);
fd = open(file_name, O_RDONLY);
bread = read(fd, buff, 100);
if (bread != 0)
return (2);
destroy_test_file(fd, file_name);
return (0);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_shift_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/07/06 17:39:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_shift_args(void)
{
const char **tmp = NULL;
const char **test;
int fake_argc;
const char *ret;
const char *fake_args[] = (const char *[]) \
{"test", "1", "2", "3", "4", NULL};
test = fake_args;
ret = NULL;
fake_argc = 5;
ret = ft_shift_args(&test, &fake_argc);
if (fake_argc != 4 || ft_strcmp(test[0], "1") != 0 || \
ft_strcmp(test[1], "2") != 0 || ft_strcmp(test[2], "3") != 0 || \
ft_strcmp(test[3], "4") != 0 || ft_strcmp(ret, "test") != 0)
return (1);
(ft_shift_args(&test, &fake_argc), ft_shift_args(&test, &fake_argc), \
ft_shift_args(&test, &fake_argc), ft_shift_args(&test, &fake_argc));
ret = ft_shift_args(&test, &fake_argc);
if (fake_argc != 0 || ret)
return (2);
fake_argc = 4012;
if (ft_shift_args(&test, &fake_argc) || ft_shift_args(&tmp, &fake_argc))
return (3);
return (0);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:08:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_split(void)
{
const char *str = " Hello World! ";
char **res;
const char *str2 = "Hello World! ";
char **res2;
size_t i;
res = ft_split(str, ' ');
res2 = ft_split(str2, ' ');
if (ft_strcmp(res[0], "Hello") != 0 || ft_strcmp(res[1], "World!") != 0
|| res[2])
return (1);
if (ft_strcmp(res2[0], "Hello") != 0 || ft_strcmp(res2[1], "World!") != 0
|| res2[2])
return (2);
i = 0;
while (res[i])
free(res[i++]);
i = 0;
while (res2[i])
free(res2[i++]);
return (free(res), free(res2), 0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_splits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:22:48 by bgoulard #+# #+# */
/* Updated: 2024/05/31 14:16:23 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stddef.h>
int tests_splits(void)
{
const char *str;
char **res;
size_t i;
str = "path/to/file:another/path:yet/another/path";
res = ft_splits(str, " /:");
if (ft_strcmp(res[0], "path") != 0 || ft_strcmp(res[1], "to") != 0 || \
ft_strcmp(res[2], "file") != 0 || ft_strcmp(res[3], "another") != 0 || \
ft_strcmp(res[4], "path") != 0 || ft_strcmp(res[5], "yet") != 0 || \
ft_strcmp(res[6], "another") != 0 || ft_strcmp(res[7], "path") != 0 || \
res[8])
return (1);
i = 0;
while (res[i])
free(res[i++]);
free(res);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isalnum(void)
{
if (ft_str_isalnum("HelloWorld42") != 1)
return (1);
if (ft_str_isalnum("Hello World42") != 0)
return (2);
if (ft_str_isalnum("Hello21World!") != 0)
return (3);
if (ft_str_isalnum("Hello420BlazeIt.") != 0)
return (4);
if (ft_str_isalnum("HelloWorld696969\0") != 1)
return (5);
if (ft_str_isalnum("") != 0)
return (6);
if (ft_str_isalnum(NULL) != false)
return (7);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isalpha(void)
{
if (ft_str_isalpha("HelloWorld") != 1)
return (1);
if (ft_str_isalpha("Hello World") != 0)
return (2);
if (ft_str_isalpha("HelloWorld!") != 0)
return (3);
if (ft_str_isalpha("HelloWorld42") != 0)
return (4);
if (ft_str_isalpha("HelloWorld\0") != 1)
return (5);
if (ft_str_isalpha("") != 0)
return (6);
if (ft_str_isalpha(NULL) != false)
return (7);
return (0);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isbool.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:22:20 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:20 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isbool(void)
{
if (ft_str_isbool("1") != 1 || ft_str_isbool("true") != 1)
return (1);
if (ft_str_isbool("0") != 1 || ft_str_isbool("false") != 1)
return (2);
if (ft_str_isbool("true!") != 0)
return (3);
if (ft_str_isbool("falseeurt") != 0)
return (4);
if (ft_str_isbool("truetrue") != 0)
return (5);
if (ft_str_isbool("false42") != 0)
return (6);
if (ft_str_isbool("false\t") != 0)
return (7);
if (ft_str_isbool("") != 0)
return (8);
if (ft_str_isbool(NULL) != false)
return (9);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isdigit(void)
{
if (ft_str_isdigit("00000000000000000000012") != 1)
return (1);
if (ft_str_isdigit("1234567890q") != 0)
return (2);
if (ft_str_isdigit("8^)") != 0)
return (3);
if (ft_str_isdigit("42.5") != 0)
return (4);
if (ft_str_isdigit("+00") != 0)
return (5);
if (ft_str_isdigit("") != 0)
return (6);
if (ft_str_isdigit(NULL) != false)
return (7);
return (0);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isdouble.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:42 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isdouble(void)
{
if (ft_str_isdouble("23.3") != 1 || ft_str_isdouble("42") != 1 || \
ft_str_isdouble(".22") != 1 || ft_str_isdouble("0.0") != 1 || \
ft_str_isdouble("2.000000") != 1 || ft_str_isdouble("0.000000") != 1)
return (1);
if (ft_str_isdouble("34..4") != 0)
return (2);
if (ft_str_isdouble("0a.0") != 0 || ft_str_isdouble("0.0a") != 0)
return (3);
if (ft_str_isdouble("0.0.0") != 0)
return (4);
if (ft_str_isdouble("-") != 0 || ft_str_isdouble("+") != 0 || \
ft_str_isdouble("-.") != 0 || ft_str_isdouble("+.") != 0)
return (5);
if (ft_str_isdouble("94308243208523048750743") != 0 || \
ft_str_isdouble("") != 0 || ft_str_isdouble(".") != 0)
return (6);
if (ft_str_isdouble(NULL) != false)
return (7);
return (0);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:43:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isfloat(void)
{
if (ft_str_isfloat("23.3") != 1 || ft_str_isfloat("42") != 1 || \
ft_str_isfloat(".22") != 1 || ft_str_isfloat("0.0") != 1 || \
ft_str_isfloat("2.000000") != 1 || ft_str_isfloat("0.000000") != 1 || \
ft_str_isfloat("-2") != 1 || ft_str_isfloat("2") != 1 || \
ft_str_isfloat("2.") != 1 || ft_str_isfloat(".2") != 1)
return (1);
if (ft_str_isfloat("23.3.3") != 0 || \
ft_str_isfloat("-") != 0 || ft_str_isfloat("+") != 0 || \
ft_str_isfloat("") != 0)
return (2);
if (ft_str_isfloat(NULL) != false)
return (3);
return (0);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_ishex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:44:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_ishex(void)
{
if (ft_str_ishex("0123456789abcdefABCDEF") != 1)
return (1);
if (ft_str_ishex("0xdeadbeef") != 1 || ft_str_ishex("0xDEADBEEF") != 1)
return (2);
if (ft_str_ishex("0xdeadbeefg") != 0 || ft_str_ishex("0xDEADBEEFG") != 0)
return (3);
if (ft_str_ishex("0x") != 0 || ft_str_ishex("0X") != 0)
return (4);
if (ft_str_ishex("0x+32") != 0 || ft_str_ishex("0x-32") != 0)
return (5);
if (ft_str_ishex("0x0x") != 0 || ft_str_ishex("0X0X") != 0)
return (6);
if (ft_str_ishex("-0x0") != 0 || ft_str_ishex("+0X0") != 0)
return (7);
if (ft_str_ishex("0x0") != 1 || ft_str_ishex("0X0") != 1)
return (8);
if (ft_str_ishex("") != 0)
return (8);
if (ft_str_ishex(NULL) != false)
return (9);
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:11:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isint(void)
{
if (ft_str_isint("23") != 1 || ft_str_isint("42") != 1 || \
ft_str_isint("0") != 1 || ft_str_isint("-42") != 1 || \
ft_str_isint("2147483647") != 1 || ft_str_isint("-2147483648") != 1)
return (1);
if (ft_str_isint("23.3") != 0 || ft_str_isint("42.0") != 0 || \
ft_str_isint("0.0") != 0 || ft_str_isint("2.000000") != 0 || \
ft_str_isint("0.000000") != 0 || ft_str_isint("2147483648") != 0 || \
ft_str_isint("-2147483649") != 0 || ft_str_isint("2147483647.0") != 0 || \
ft_str_isint("-2147483648.0") != 0)
return (2);
if (ft_str_isint("") != 0 || ft_str_isint("999999999999") != 0 || \
ft_str_isint("-") != 0 || ft_str_isint("+") != 0)
return (3);
if (ft_str_isint(NULL) != false)
return (4);
return (0);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_islong.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:44:27 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_islong(void)
{
if (ft_str_islong("23") != 1 || ft_str_islong("42") != 1 || \
ft_str_islong("0") != 1 || ft_str_islong("2") != 1 || \
ft_str_islong("-34") != 1 || ft_str_islong("42") != 1 || \
ft_str_islong("9223372036854775807") != 1 || \
ft_str_islong("-9223372036854775808") != 1)
return (1);
if (ft_str_islong("9223372036854775808") != 0 || \
ft_str_islong("-9223372036854775809") != 0 || \
ft_str_islong("92233720368547758070") != 0 || \
ft_str_islong("-92233720368547758080") != 0 || \
ft_str_islong("9223372036854775807a") != 0 || \
ft_str_islong("-9223372036854775808a") != 0 || \
ft_str_islong("9223372036854775807 ") != 0 || \
ft_str_islong("-9223372036854775808 ") != 0 || \
ft_str_islong("-+0") != 0 || ft_str_islong("+-0") != 0 || \
ft_str_islong("0-") != 0 || ft_str_islong("0+") != 0)
return (2);
if (ft_str_islong("") != 0 || ft_str_islong("-") != 0)
return (3);
if (ft_str_islong(NULL) != false)
return (4);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:44:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isnum(void)
{
if (ft_str_isnum("-23") != 1 || ft_str_isnum("+42") != 1 || \
ft_str_isnum("0") != 1 || ft_str_isnum("2345865785645643532453") != 1 || \
ft_str_isnum("2147483647") != 1)
return (1);
if (ft_str_isnum("23.3") != 0 || ft_str_isnum("42.0") != 0 || \
ft_str_isnum("0.0") != 0 || ft_str_isnum("2.000000") != 0 || \
ft_str_isnum("0.000000") != 0 || ft_str_isnum("--2147483648") != 0 || \
ft_str_isnum("++2147483648") != 0)
return (2);
if (ft_str_isnum("") != 0 || ft_str_isnum("+") != 0 || \
ft_str_isnum("-") != 0)
return (3);
if (ft_str_isnum(NULL) != false)
return (4);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isoct.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:45:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_isoct(void)
{
if (ft_str_isoct("0o1234567") != 1 || ft_str_isoct("0O1234567") != 1 || \
ft_str_isoct("0o342") != 1 || ft_str_isoct("0o0") != 1 || \
ft_str_isoct("0o000000") != 1)
return (1);
if (ft_str_isoct("-0o1234568") != 0 || ft_str_isoct("0o1234568") != 0 || \
ft_str_isoct("0o9") != 0 || ft_str_isoct("12") != 0 || \
ft_str_isoct("-7") != 0 || ft_str_isoct("+67") != 0)
return (2);
if (ft_str_isoct(NULL) != false)
return (3);
return (0);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_isvalid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 08:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:45:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int string_validator(int c)
{
return (c == 'a' || c == 'b' || c == 'c');
}
int test_str_isvalid(void)
{
if (ft_str_isvalid("abc", string_validator) != 1 || ft_str_isvalid("abcabc",
string_validator) != 1 || ft_str_isvalid("aabbcc",
string_validator) != 1)
return (1);
if (ft_str_isvalid("abd", string_validator) != 0 || ft_str_isvalid("bap",
string_validator) != 0 || ft_str_isvalid("ck",
string_validator) != 0)
return (2);
if (ft_str_isvalid("", string_validator) != 0 || ft_str_isvalid(NULL,
string_validator) != false)
return (3);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:52:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_replace(void)
{
char *str;
char *res;
str = "Hello World!";
res = ft_str_replace(str, "World", "Zod");
if (ft_strcmp(res, "Hello Zod!") != 0)
return (1);
free(res);
res = ft_str_replace(str, "World", "");
if (ft_strcmp(res, "Hello !") != 0)
return (2);
free(res);
str = "Hello World!";
res = ft_str_replace(str, "toto", "tutu");
if (ft_strcmp(res, "Hello World!") != 0)
return (3);
free(res);
return (0);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_str_replace_chr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:13:01 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:10:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_str_replace_chr(void)
{
char *str;
char *res;
str = ft_strdup("Hello World!");
res = ft_str_replace_chr(str, 'o', 'a');
if (ft_strcmp(res, "Hella Warld!") != 0)
return (1);
free(res);
str = ft_strdup("Hello World!");
res = ft_str_replace_chr(str, 'o', '\0');
if (ft_strcmp(res, "Hell") != 0 || ft_strcmp(res + 5, " W") != 0
|| ft_strcmp(res + 8, "rld!") != 0)
return (4);
free(res);
return (0);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strappend_c.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:12:40 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:08:07 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
// calloc sets the \0 byte at the end of the string
// we test the function can append at empty string
int test_strappend_c(void)
{
char *str;
str = ft_strdup("Hello");
if (!str)
return (1);
if (ft_strappend_c(&str, ' ') == 0 || ft_strcmp(str, "Hello ") != 0)
return (2);
if (ft_strappend_c(&str, 'W') == 0 || ft_strcmp(str, "Hello W") != 0)
return (3);
if (ft_strappend_c(NULL, '\0'))
return (4);
free(str);
str = ft_calloc(1, 1);
if (!str || ft_strappend_c(&str, ' ') == 0 || ft_strcmp(str, " ") != 0)
return (5);
free(str);
str = NULL;
if (ft_strappend_c(&str, ' ') == 0 || ft_strcmp(str, " ") != 0)
return (6);
free(str);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:12:40 by bgoulard #+# #+# */
/* Updated: 2024/06/02 07:51:50 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strchr(void)
{
char *str;
char s;
char s2;
char *res;
str = "Hello World!";
s = 'o';
s2 = 'z';
res = ft_strchr(str, s);
if (res != str + 4)
return (1);
res = ft_strchr(str, s2);
if (res)
return (2);
res = ft_strchr(str, '\0');
if (res != str + ft_strlen(str))
return (3);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strclen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:47:16 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:05:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strclen(void)
{
if (ft_strclen("Hello World", 'o') != 4)
return (1);
if (ft_strclen("Hello World", 'z') != 11)
return (1);
if (ft_strclen("Hello World", 'H') != 0)
return (1);
if (ft_strclen("Hello World", 'd') != 10)
return (1);
if (ft_strclen("Hello World", ' ') != 5)
return (1);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:12:10 by bgoulard #+# #+# */
/* Updated: 2024/05/26 14:17:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strcmp(void)
{
const char *buffs[] = {
"none",
"some",
"nonethensome",
"nonethensome",
"!\0a",
"!\0b",
};
if (ft_strcmp(buffs[0], buffs[1]) >= 0)
return (1);
if (ft_strcmp(buffs[0], buffs[2]) >= 0)
return (2);
if (ft_strcmp(buffs[2], buffs[3]) != 0)
return (3);
if (ft_strcmp(buffs[4], buffs[5]) != 0)
return (4);
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strcnb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:47:58 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:06:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdio.h>
int test_strcnb(void)
{
if (ft_strcnb("Hello World", 'o') != 2)
return (1);
if (ft_strcnb("Hello World", 'z') != 0)
return (2);
if (ft_strcnb("Hello World", 'H') != 1)
return (3);
if (ft_strcnb("Hello World", 'd') != 1)
return (4);
if (ft_strcnb("Hello World", 'l') != 3)
return (5);
if (ft_strcnb(NULL, ' ') != 0)
return (6);
if (ft_strcnb("toto", '\0') != 1)
return (7);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strcspn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:48:32 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:03:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strcspn(void)
{
if (ft_strcspn("Hello World", "o") != 4)
return (1);
if (ft_strcspn("Hello World", "z") != 11)
return (1);
if (ft_strcspn("Hello World", "H") != 0)
return (1);
if (ft_strcspn("Hello World", "d") != 10)
return (1);
if (ft_strcspn("Hello World", " ") != 5)
return (1);
return (0);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:23:17 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:10:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strdup(void)
{
char *str;
char *res;
str = "Hello World!";
res = ft_strdup(str);
if (ft_strcmp(res, str) != 0)
return (1);
free(res);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strend_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:49:13 by bgoulard #+# #+# */
/* Updated: 2024/05/30 10:49:34 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strend_with(void)
{
if (ft_strend_with("Hello World", "World") != 1)
return (1);
if (ft_strend_with("Hello World", "World!") != 0)
return (1);
if (ft_strend_with("Hello World", "Hello") != 0)
return (1);
if (ft_strend_with("Hello World", "Hello World") != 1)
return (1);
if (ft_strend_with("Hello World", "Hello World!") != 0)
return (1);
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:39:56 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:11:48 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
static void local_iteri(unsigned int i, char *c)
{
*c = 'a';
(void)i;
}
// last 2 calls are to test NULL protection
int test_striteri(void)
{
char str[20];
ft_strlcpy(str, "Hello World!", 20);
ft_striteri(str, &local_iteri);
if (ft_strcmp(str, "aaaaaaaaaaaa") != 0)
return (1);
ft_striteri(NULL, &local_iteri);
ft_striteri(str, NULL);
return (0);
}

View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:11:19 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:10:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static int test_right_empty(void)
{
char *str;
char *str2;
char *res;
str = "Hello World!";
str2 = "";
res = ft_strjoin(str, str2);
if (ft_strcmp(res, "Hello World!") != 0 || !res || res == str
|| res == str2)
return (1);
free(res);
str2 = NULL;
res = ft_strjoin(str, str2);
if (ft_strcmp(res, "Hello World!") != 0 || !res || res == str)
return (2);
free(res);
return (0);
}
static int test_left_empty(void)
{
char *str;
char *str2;
char *res;
str = "";
str2 = "Hello World!";
res = ft_strjoin(str, str2);
if (ft_strcmp(res, "Hello World!") != 0 || !res || res == str
|| res == str2)
return (1);
free(res);
str = NULL;
res = ft_strjoin(str, str2);
if (ft_strcmp(res, "Hello World!") != 0 || !res || res == str2)
return (2);
free(res);
return (0);
}
int test_strjoin(void)
{
char *str;
char *str2;
char *res;
str = "Hello World!";
str2 = "Hello World!";
res = ft_strjoin(str, str2);
if (ft_strcmp(res, "Hello World!Hello World!") != 0)
return (1);
free(res);
if (test_right_empty() != 0)
return (20 + test_right_empty());
if (test_left_empty() != 0)
return (30 + test_left_empty());
return (0);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:11:01 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:24:26 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdio.h>
#include <stdlib.h>
int test_strlcat(void)
{
char *res;
size_t size;
int ret;
size = 15;
res = malloc(sizeof(char) * size);
ft_bzero(res, size);
ret = ft_strlcat(res, "Hello", size);
if (ft_strcmp(res, "Hello") != 0 || ret != 5)
return (1);
ret = ft_strlcat(res, " World!", size);
if (ft_strcmp(res, "Hello World!") != 0 || ret != 12)
return (2);
ret = ft_strlcat(res, "This is zod!", size);
if (ft_strcmp(res, "Hello World!Th") != 0 || ret != 24)
return (3);
ret = ft_strlcat(res, "This is zod!", 5);
if (ft_strcmp(res, "Hello World!Th") != 0 || ret != 5 + 12)
return (4);
if (ft_strlcat(NULL, "test", 0) != 0 || ft_strlcat(NULL, "test", 5) != 0)
return (5);
ret = ft_strlcat(res, NULL, size);
if (ft_strcmp(res, "Hello World!Th") != 0 || ret != 14)
return (6);
return (free(res), 0);
}

View file

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:10:11 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:10:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdio.h>
#include <stdlib.h>
static int base_cases(void)
{
const char *str = "Hello World!";
char *res;
size_t size;
int ret;
size = 15;
res = ft_calloc(sizeof(char), size);
ret = ft_strlcpy(res, str, size);
if (ft_strcmp(res, "Hello World!") != 0 || ret != 12)
return (1);
free(res);
res = malloc(sizeof(char) * size);
ret = ft_strlcpy(res, "This is zod!", size);
if (ft_strcmp(res, "This is zod!") != 0 || ret != 12)
return (2);
return (free(res), 0);
}
static int error_cases(void)
{
char *res;
size_t size;
int ret;
size = 15;
res = ft_calloc(sizeof(char), size);
ret = ft_strlcpy(res, "This is too large!", size);
if (ft_strncmp(res, "This is too large!", size - 1) != 0 || \
ret != (int)ft_strlen("This is too large!"))
return (3);
free(res);
res = ft_calloc(sizeof(char), 1);
ret = ft_strlcpy(res, "Hello World!", 0);
if (ft_strcmp(res, "") != 0 || ret != 12)
return (4);
ret = ft_strlcpy(res, NULL, 42);
if (ft_strcmp(res, "") != 0 || ret != 0)
return (5);
ret = ft_strlcpy(NULL, "Hello World!", 42);
if (ret != 42)
return (6);
return (free(res), 0);
}
int test_strlcpy(void)
{
int ret;
ret = base_cases();
if (ret != 0)
return (ret);
ret = error_cases();
if (ret != 0)
return (ret + 10);
return (0);
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 12:26:06 by bgoulard #+# #+# */
/* Updated: 2024/05/26 12:26:07 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strlen(void)
{
char *str;
char *str2;
char *str3;
str = "Hello World!";
str2 = "";
str3 = "Hello\0World!";
if (ft_strlen(str) != 12)
return (1);
if (ft_strlen(str2) != 0)
return (2);
if (ft_strlen(str3) != 5)
return (3);
return (0);
}

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:10:32 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:40:48 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
static char local_mapi(unsigned int i, char c)
{
(void)i;
if (c >= 'a' && c <= 'z')
return (c - 32);
if (c >= 'A' && c <= 'Z')
return (c + 32);
return (c);
}
int test_strmapi(void)
{
char *str;
char *res;
str = "Hello World!";
res = ft_strmapi(str, &local_mapi);
if (ft_strcmp(res, "hELLO wORLD!") != 0)
return (1);
free(res);
res = ft_strmapi(NULL, &local_mapi);
if (res)
return (2);
res = ft_strmapi(str, NULL);
if (res)
return (3);
return (0);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 12:23:03 by bgoulard #+# #+# */
/* Updated: 2024/05/26 12:23:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strncmp(void)
{
const char *str_buffs[] = {
"none",
"some",
"nonethensome",
"nonethensome",
"!\0a",
"!\0b",
NULL
};
if (ft_strncmp(str_buffs[0], str_buffs[1], 4) >= 0)
return (1);
if (ft_strncmp(str_buffs[0], str_buffs[2], 4) != 0)
return (2);
if (ft_strncmp(str_buffs[2], str_buffs[3], 4) != 0)
return (3);
if (ft_strncmp(str_buffs[4], str_buffs[5], 4) != 0)
return (4);
if (ft_strncmp(str_buffs[5], str_buffs[0], 0) != 0)
return (5);
return (0);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strndup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:23:38 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:13:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strndup(void)
{
char *str;
char *res;
str = "Hello World!";
res = ft_strndup(str, 5);
if (ft_strcmp(res, "Hello") != 0)
return (1);
free(res);
res = ft_strndup(str, 0);
if (ft_strcmp(res, "") != 0)
return (2);
free(res);
res = ft_strndup(str, 12);
if (ft_strcmp(res, "Hello World!") != 0)
return (3);
free(res);
res = ft_strndup(str, 15);
if (ft_strcmp(res, "Hello World!") != 0)
return (4);
free(res);
return (0);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:24:10 by bgoulard #+# #+# */
/* Updated: 2024/06/02 08:44:18 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stddef.h>
int test_strnstr(void)
{
char *str;
char *res;
str = "The cake is a lie !\0I'm hidden lol\n";
if (ft_strnstr(str, "The cake is a lie !", 100) != str)
return (1);
if (ft_strnstr(str, "The cake is a lie !", 20) != str)
return (2);
res = ft_strnstr(str, "The cake is a lie !", 19);
if (!res)
return (3);
if (ft_strnstr(str, "The cake is a lie !", 0))
return (4);
if (ft_strnstr(str, "hidden", 100))
return (5);
if (ft_strnstr(NULL, "hidden", 10) || \
ft_strnstr(str, NULL, 10))
return (6);
return (0);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:41:05 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:14:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stddef.h>
int test_strrchr(void)
{
char *str;
char s;
char s2;
char *res;
str = "Hello World!";
s = 'o';
s2 = 'z';
res = ft_strrchr(str, s);
if (res != str + 7)
return (1);
res = ft_strrchr(str, s2);
if (res)
return (2);
res = ft_strrchr(str, '\0');
if (res != str + ft_strlen(str))
return (3);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strspn.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:48:32 by bgoulard #+# #+# */
/* Updated: 2024/05/30 11:05:30 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strspn(void)
{
if (ft_strspn("Hello World", "o") != 0)
return (1);
if (ft_strspn("Hello World", "z") != 0)
return (1);
if (ft_strspn("Hello World", "H") != 1)
return (1);
if (ft_strspn("Hello World", "d") != 0)
return (1);
if (ft_strspn("Hello World", "Helo Wrd") != 11)
return (1);
return (0);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strstart_with.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:49:38 by bgoulard #+# #+# */
/* Updated: 2024/05/30 10:49:51 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strstart_with(void)
{
if (ft_strstart_with("Hello World", "Hello") != 1)
return (1);
if (ft_strstart_with("Hello World", "Hello!") != 0)
return (1);
if (ft_strstart_with("Hello World", "World") != 0)
return (1);
if (ft_strstart_with("Hello World", "Hello World") != 1)
return (1);
if (ft_strstart_with("Hello World", "Hello World!") != 0)
return (1);
return (0);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strtok.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:24:49 by bgoulard #+# #+# */
/* Updated: 2024/06/25 21:43:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strtok(void)
{
char test[50];
char lorem[30];
lorem[0] = '\0';
test[0] = '\0';
ft_strlcpy(lorem, "Lorem ipsum dolor ", 30);
ft_strlcpy(test, ":::path/to/file:::another/path::yet/:/another/path", 47);
if (ft_strcmp(ft_strtok(lorem, " "), "Lorem") != 0
|| ft_strcmp(ft_strtok(NULL, " "), "ipsum") != 0
|| ft_strcmp(ft_strtok(NULL, " "), "dolor") != 0)
return (1);
if (ft_strcmp(ft_strtok(NULL, " "), "") != 0 || ft_strtok(NULL,
" "))
return (2);
if (ft_strcmp(ft_strtok(test, ":"), "path/to/file") != 0
|| ft_strcmp(ft_strtok(NULL, ":"), "another/path") != 0)
return (3);
if (ft_strcmp(ft_strtok(NULL, ":/"), "yet") != 0)
return (4);
if (ft_strcmp(ft_strtok(NULL, ":/"), "another") != 0)
return (5);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:19:56 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:00:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_strtrim(void)
{
const char *str_ugly = " Hello World! ";
const char *str_ugly_front = " Hello World!";
char *res[7];
res[0] = ft_strtrim(ft_strchr(str_ugly, 'H'), " ");
res[1] = ft_strtrim(str_ugly, " ");
res[2] = ft_strtrim(str_ugly_front, " ");
res[3] = ft_strtrim(str_ugly, " !Wd");
res[4] = ft_strtrim(str_ugly, NULL);
res[5] = ft_strtrim(" ", " ");
res[6] = ft_strtrim(NULL, " ");
if (ft_strcmp(res[0], "Hello World!") != 0 || ft_strcmp(res[1], \
"Hello World!") != 0 || ft_strcmp(res[2], "Hello World!") != 0 || \
ft_strcmp(res[3], "Hello Worl") != 0 || ft_strcmp(res[4], str_ugly) \
!= 0 || ft_strcmp(res[5], "") != 0 || res[6])
return (1);
ft_apply_2d((void **)res, free);
return (0);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 12:25:34 by bgoulard #+# #+# */
/* Updated: 2024/06/02 09:02:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
int test_substr(void)
{
const char *str = "Hello World!";
char *res[7];
res[0] = ft_substr(str, 0, 5);
res[1] = ft_substr(str, 6, 6);
res[2] = ft_substr(str, 6, 100);
res[3] = ft_substr(str, 6, 0);
res[4] = ft_substr(str, 12, 0);
res[5] = ft_substr(str, 12, 100);
res[6] = ft_substr(NULL, 0, 5);
if (ft_strcmp(res[0], "Hello") != 0 || ft_strcmp(res[1], "World!") != 0
|| ft_strcmp(res[2], "World!") != 0)
return (1);
if (ft_strcmp(res[3], "") != 0 || ft_strcmp(res[4], "") != 0
|| ft_strcmp(res[5], "") != 0 || res[6])
return (2);
ft_apply_2d((void **)res, free);
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/26 11:17:28 by bgoulard #+# #+# */
/* Updated: 2024/05/31 15:39:44 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
int test_utoa(void)
{
char *res;
size_t i;
const unsigned int t_cases[] = {0, 123, 456, 7890, 12345};
const char *expected_results[] = \
{"0", "123", "456", "7890", "12345"};
i = 0;
while (i < sizeof(t_cases) / sizeof(t_cases[0]))
{
res = ft_utoa(t_cases[i]);
if (ft_strcmp(res, expected_results[i++]) != 0)
return (i);
free(res);
}
return (0);
}