ft_atoi()

This commit is contained in:
lalgarra 2025-10-07 18:50:55 +02:00
parent 3043d38bf0
commit fc301f82aa
3 changed files with 90 additions and 2 deletions

View file

@ -6,7 +6,7 @@
# By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ # # By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/10/03 18:03:12 by lalgarra #+# #+# # # Created: 2025/10/03 18:03:12 by lalgarra #+# #+# #
# Updated: 2025/10/07 18:08:12 by lalgarra ### ########.fr # # Updated: 2025/10/07 18:46:48 by lalgarra ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -22,7 +22,7 @@ TESTS := ft_isalpha_test ft_isdigit_test ft_isalnum_test ft_isascii_test \
ft_memset_test ft_bzero_test ft_memcpy_test ft_calloc_test \ ft_memset_test ft_bzero_test ft_memcpy_test ft_calloc_test \
ft_memmove_test ft_strlcpy_test ft_strlcat_test ft_strchr_test \ ft_memmove_test ft_strlcpy_test ft_strlcat_test ft_strchr_test \
ft_strrchr_test ft_strncmp_test ft_memcmp_test ft_strnstr_test \ ft_strrchr_test ft_strncmp_test ft_memcmp_test ft_strnstr_test \
ft_atoi_test
%_test : %_test.c %_test : %_test.c
$(CC) $(CFLAGS) $*_test.c -L$(LIBDIR) -lft -o $*_test $(CC) $(CFLAGS) $*_test.c -L$(LIBDIR) -lft -o $*_test

6
ft_atoi_expected.output Normal file
View file

@ -0,0 +1,6 @@
test 1: buff:[+35], result:[35]
test 2: buff:[+-50], result:[0]
test 3: buff:[ -3], result:[-3]
test 4: buff:[
+1367442], result:[1367442]
test 5: buff:[ -+5], result:[0]

82
ft_atoi_test.c Normal file
View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lalgarra <lalgarra@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/07 18:36:33 by lalgarra #+# #+# */
/* Updated: 2025/10/07 18:49:30 by lalgarra ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
#include <string.h>
static void test(char *prefix, char buff[])
{
int res;
int fd;
fd = STDOUT_FILENO;
ft_putstr_fd(prefix, fd);
ft_putstr_fd("buff:[", fd);
ft_putstr_fd(buff, fd);
res = ft_atoi(buff);
ft_putstr_fd("], result:[", fd);
ft_putnbr_fd(res, fd);
ft_putendl_fd("]", fd);
}
static void zero(char buff[])
{
ft_bzero(buff, 128);
}
static void do_test1(char buff[])
{
zero(buff);
ft_strlcpy(buff, "+350", 4);
test("test 1: ", buff);
}
static void do_test2(char buff[])
{
zero(buff);
ft_strlcpy(buff, "+-50", 5);
test("test 2: ", buff);
}
static void do_test3(char buff[])
{
zero(buff);
ft_strlcpy(buff, " -3", 5);
test("test 3: ", buff);
}
static void do_test4(char buff[])
{
zero(buff);
ft_strlcpy(buff, " \t\v\f\r\n +1367442", 20);
test("test 4: ", buff);
}
static void do_test5(char buff[])
{
zero(buff);
ft_strlcpy(buff, " -+50", 5);
test("test 5: ", buff);
}
int main(void)
{
char buff[128];
do_test1(buff);
do_test2(buff);
do_test3(buff);
do_test4(buff);
do_test5(buff);
return (0);
}