diff --git a/Makefile b/Makefile index 69975a7..282d82c 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,14 @@ DEPS = $(_DEP:%.h=$(IDIR)/%.h) CFLAGS = -I$(IDIR) CFLAGS += -Wall -Wextra -Werror -g3 +# ft_putstr +# ft_atoi +# ft_putchar +# ft_putnbrendl +# ft_putnbrendl_fd +# ft_putchar_fd +# ft_putnbr_fd + SRCS = ft_memset.c \ ft_bzero.c \ ft_memcpy.c \ @@ -25,6 +33,7 @@ SRCS = ft_memset.c \ ft_isalpha.c \ ft_isdigit.c \ ft_isalnum.c \ + ft_isnumber.c \ ft_isascii.c \ ft_isprint.c \ ft_toupper.c \ @@ -37,6 +46,7 @@ SRCS = ft_memset.c \ ft_strlcat.c \ ft_strnstr.c \ ft_atoi.c \ + ft_atol.c \ ft_calloc.c \ ft_strdup.c \ \ @@ -107,7 +117,8 @@ SRCS = ft_memset.c \ ft_greater.c \ ft_smaller.c \ ft_sign.c \ - ft_sqrt.c + ft_sqrt.c \ + ft_free_tab.c ODIR = ./builds diff --git a/includes/libft.h b/includes/libft.h index 6cd98a7..058d3e3 100644 --- a/includes/libft.h +++ b/includes/libft.h @@ -27,6 +27,7 @@ int ft_memcmp(const void *s1, const void *s2, size_t n); size_t ft_strlen(const char *str); int ft_isalpha(int c); int ft_isdigit(int c); +int ft_isnumber(char *nb); int ft_isalnum(int c); int ft_isascii(int c); int ft_isprint(int c); @@ -40,6 +41,7 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size); size_t ft_strlcat(char *dst, const char *src, size_t size); char *ft_strnstr(const char *b, const char *l, size_t s); int ft_atoi(const char *str); +long ft_atol(const char *str); void *ft_calloc(size_t count, size_t size); char *ft_strdup(const char *s1); @@ -109,5 +111,6 @@ int ft_greater(int a, int b); int ft_smaller(int a, int b); int ft_sign(int i); int ft_sqrt(int i); +void ft_free_tab(char **tab); #endif diff --git a/srcs/ft_any.c b/srcs/ft_any.c index 349e3a9..9322945 100644 --- a/srcs/ft_any.c +++ b/srcs/ft_any.c @@ -10,6 +10,12 @@ /* */ /* ************************************************************************** */ +/* +** i don't understand the purpose of this function... +** it takes a 2D array, and for each array it checks +** if a function given in parameters is true or false +*/ + #include "libft.h" int ft_any(char **tab, int (*f)(char*)) diff --git a/srcs/ft_atoi.c b/srcs/ft_atoi.c index 01660a9..8d34949 100644 --- a/srcs/ft_atoi.c +++ b/srcs/ft_atoi.c @@ -14,26 +14,20 @@ int ft_atoi(const char *str) { - long long nbr; + long nbr; int i; - int n; + int negatif; i = 0; - n = 1; + negatif = 1; nbr = 0; while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14)) i++; if (str[i] == '-') - n = -1; + negatif = -1; if (str[i] == '+' || str[i] == '-') i++; while (str[i] >= '0' && str[i] <= '9') - { - if ((nbr >= 922337203685477580 - && ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0)))) - return ((n > 0) ? -1 : 0); - else - nbr = nbr * 10 + (str[i++] - '0'); - } - return (nbr * n); + nbr = nbr * 10 + (str[i++] - '0'); + return (nbr * negatif); } diff --git a/srcs/ft_atol.c b/srcs/ft_atol.c new file mode 100644 index 0000000..1bf87cf --- /dev/null +++ b/srcs/ft_atol.c @@ -0,0 +1,22 @@ + +#include "libft.h" + +long ft_atol(const char *str) +{ + long long nbr; + int i; + int negatif; + + i = 0; + negatif = 1; + nbr = 0; + while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14)) + i++; + if (str[i] == '-') + negatif = -1; + if (str[i] == '+' || str[i] == '-') + i++; + while (str[i] >= '0' && str[i] <= '9') + nbr = nbr * 10 + (str[i++] - '0'); + return (nbr * negatif); +} diff --git a/srcs/ft_free_tab.c b/srcs/ft_free_tab.c new file mode 100644 index 0000000..cc9c4bb --- /dev/null +++ b/srcs/ft_free_tab.c @@ -0,0 +1,15 @@ + +#include "libft.h" + +void ft_free_tab(char **tab) +{ + int i; + + i = 0; + while (tab[i] != NULL) + { + free(tab[i]); + i++; + } + free(tab); +} diff --git a/srcs/ft_isnumber.c b/srcs/ft_isnumber.c new file mode 100644 index 0000000..6ecc6c5 --- /dev/null +++ b/srcs/ft_isnumber.c @@ -0,0 +1,19 @@ + +#include "libft.h" + +int ft_isnumber(char *nb) +{ + int i; + + i = 0; + if (nb[i] == '+' || nb[i] == '-') + i++; + while (nb[i] != '\0') + { + if (ft_isdigit(nb[i]) == 0) + return (0); + i++; + } + return (1); +} + diff --git a/testing/includes/libftest.h b/testing/includes/libftest.h index 5d92e6b..d7d271c 100644 --- a/testing/includes/libftest.h +++ b/testing/includes/libftest.h @@ -1,7 +1,11 @@ #ifndef LIBFTEST_H # define LIBFTEST_H # include "libft.h" -# include +# include // for write +# include // for printf +# include // for malloc +# include // for strdup and strlen and strcmp +# include // for characters like isalpha or isdigit etc # define RED write(1, "\033[91m", 5); # define GREEN write(1, "\033[92m", 5); diff --git a/testing/libftest b/testing/libftest index 8085142..3e23413 100755 Binary files a/testing/libftest and b/testing/libftest differ diff --git a/testing/srcs/test_abs.c b/testing/srcs/test_abs.c index d3c42fd..f9c944e 100644 --- a/testing/srcs/test_abs.c +++ b/testing/srcs/test_abs.c @@ -1,6 +1,6 @@ #include "libftest.h" -void compare(int i, int ans) +void compare_abs(int i, int ans) { if (ft_abs(i) != ans) { @@ -15,10 +15,9 @@ void compare(int i, int ans) void test_abs(void) { - compare(-1, 1); - compare(0, 0); - compare(-0, 0); - compare(-1000, 1000); - compare(-2147483648, 2147483648); - compare(2147483647, 2147483647); + compare_abs(-1, 1); + compare_abs(0, 0); + compare_abs(-0, 0); + compare_abs(-1000, 1000); + compare_abs(-2147483647, 2147483647); } diff --git a/testing/srcs/test_any.c b/testing/srcs/test_any.c index 7bdbe9c..05b7692 100644 --- a/testing/srcs/test_any.c +++ b/testing/srcs/test_any.c @@ -1,6 +1,131 @@ #include "libftest.h" -//int test_any(char **tab, int (*f)(char*)) -void test_any(void) +char **create_tab(void) { + char **tab; + + tab = (char **)malloc(sizeof(char *) * (6 + 1)); + tab[0] = strdup("premiere ligne de test"); + tab[1] = strdup("deuxieme ligne de test"); + tab[2] = strdup("troisieme ligne pour le fun"); + tab[3] = strdup("quatrieme linge avec une erreur"); + tab[4] = strdup("cinquieme peche a la ligne"); + tab[5] = strdup("sixieme et derniere ligne"); + return (tab); } + +void free_tab(char **tab) +{ + free(tab[0]); + free(tab[1]); + free(tab[2]); + free(tab[3]); + free(tab[4]); + free(tab[5]); + free(tab); +} + +int contain_a(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == 'a') + return (1); + return (0); +} + +int contain_y(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == 'y') + return (1); + return (0); +} + +int contain_z(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == 'z') + return (1); + return (0); +} + +int contain_x(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == 'x') + return (1); + return (0); +} + +int contain_u(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == 'u') + return (1); + return (0); +} + +int contain_1(char *s) +{ + int i; + + i = -1; + while (s[++i]) + if (s[i] == '1') + return (1); + return (0); +} + +void compare_any(char **tab, int (*f)(char *), char *s, int solution) +{ + int i; + + i = ft_any(tab, f); + if (i != solution) + { + write(1, "\033[91m", 5); + printf("error: "); + write(1, "\033[0m", 4); + printf("ft_any(tab, %s) returned", s); + printf(" %i ", i); + printf("in tab :\n"); + printf(" %s:\n", tab[0]); + printf(" %s:\n", tab[1]); + printf(" %s:\n", tab[2]); + printf(" %s:\n", tab[3]); + printf(" %s:\n", tab[4]); + printf(" %s:\n", tab[5]); + } +} + +void test_any(void) +{ + char **tab; + + tab = create_tab(); + + compare_any(tab, contain_a, "contain_a", 1); + compare_any(tab, contain_y, "contain_y", 0); + compare_any(tab, contain_z, "contain_z", 0); + compare_any(tab, contain_x, "contain_x", 1); + compare_any(tab, contain_u, "contain_u", 1); + compare_any(tab, contain_1, "contain_1", 0); + + free_tab(tab); +} +