From 0a390934d660d0abe670e59d9e4a023db733ff8f Mon Sep 17 00:00:00 2001 From: "B.Goulard" <84p71573@gmail.com> Date: Fri, 1 Nov 2024 00:00:14 +0100 Subject: [PATCH] added bgoulard lib + made lib sources in their own directory in build/ for readability --- Makefile | 23 +- libft/.gitignore | 53 - libft/Makefile | 4 +- libft_personal/Makefile | 744 ++++++++++++++ libft_personal/include/ft_args.h | 54 + libft_personal/include/ft_args_types.h | 146 +++ libft_personal/include/ft_char.h | 101 ++ libft_personal/include/ft_colors.h | 42 + libft_personal/include/ft_defs.h | 43 + libft_personal/include/ft_errno.h | 415 ++++++++ libft_personal/include/ft_list.h | 450 +++++++++ libft_personal/include/ft_list_types.h | 46 + libft_personal/include/ft_map.h | 144 +++ libft_personal/include/ft_map_types.h | 86 ++ libft_personal/include/ft_math.h | 187 ++++ libft_personal/include/ft_math_types.h | 43 + libft_personal/include/ft_optional.h | 145 +++ libft_personal/include/ft_optional_types.h | 30 + libft_personal/include/ft_pair.h | 93 ++ libft_personal/include/ft_pair_types.h | 22 + libft_personal/include/ft_string.h | 925 ++++++++++++++++++ libft_personal/include/ft_string_types.h | 60 ++ libft_personal/include/ft_types.h | 43 + libft_personal/include/ft_vector.h | 218 +++++ libft_personal/include/ft_vector_types.h | 38 + libft_personal/include/internal/args_helper.h | 45 + libft_personal/include/tests/args_tests.h | 35 + libft_personal/include/tests/char_tests.h | 52 + libft_personal/include/tests/dl_tests.h | 60 ++ .../include/tests/lists_test_utils.h | 22 + libft_personal/include/tests/ll_tests.h | 57 ++ libft_personal/include/tests/map_tests.h | 48 + libft_personal/include/tests/math_tests.h | 59 ++ libft_personal/include/tests/optional_test.h | 44 + libft_personal/include/tests/pair_tests.h | 26 + libft_personal/include/tests/str__mem_tests.h | 64 ++ libft_personal/include/tests/str__str_tests.h | 169 ++++ .../include/tests/str__t_str_test.h | 60 ++ libft_personal/include/tests/tests.h | 47 + .../include/tests/tests_lambda_functions.h | 24 + libft_personal/include/tests/vector_tests.h | 42 + .../src/ft_args/ft_arg_custom_checker.c | 33 + libft_personal/src/ft_args/ft_parse_args.c | 68 ++ libft_personal/src/ft_args/ft_parse_err.c | 87 ++ libft_personal/src/ft_args/ft_parse_opt.c | 50 + libft_personal/src/ft_args/ft_progname.c | 36 + libft_personal/src/ft_args/ft_set_opt_args.c | 35 + libft_personal/src/ft_args/ft_setup_prog.c | 19 + libft_personal/src/ft_args/ft_version.c | 37 + libft_personal/src/ft_list/ft_dl/ft_dl_add.c | 37 + .../src/ft_list/ft_dl/ft_dl_apply.c | 52 + .../src/ft_list/ft_dl/ft_dl_clear.c | 40 + .../src/ft_list/ft_dl/ft_dl_create.c | 65 ++ .../src/ft_list/ft_dl/ft_dl_delete.c | 102 ++ libft_personal/src/ft_list/ft_dl/ft_dl_find.c | 28 + .../src/ft_list/ft_dl/ft_dl_getters.c | 58 ++ .../src/ft_list/ft_dl/ft_dl_iterator.c | 48 + libft_personal/src/ft_list/ft_dl/ft_dl_map.c | 32 + libft_personal/src/ft_list/ft_dl/ft_dl_new.c | 27 + .../src/ft_list/ft_dl/ft_dl_pushpop.c | 80 ++ libft_personal/src/ft_list/ft_dl/ft_dl_rev.c | 33 + libft_personal/src/ft_list/ft_dl/ft_dl_size.c | 44 + libft_personal/src/ft_list/ft_dl/ft_dl_sub.c | 42 + libft_personal/src/ft_list/ft_ll/ft_ll_add.c | 29 + .../src/ft_list/ft_ll/ft_ll_apply.c | 60 ++ .../src/ft_list/ft_ll/ft_ll_clear.c | 34 + .../src/ft_list/ft_ll/ft_ll_create.c | 61 ++ .../src/ft_list/ft_ll/ft_ll_delete.c | 42 + libft_personal/src/ft_list/ft_ll/ft_ll_find.c | 28 + .../src/ft_list/ft_ll/ft_ll_getters.c | 54 + .../src/ft_list/ft_ll/ft_ll_iterator.c | 37 + libft_personal/src/ft_list/ft_ll/ft_ll_map.c | 32 + libft_personal/src/ft_list/ft_ll/ft_ll_new.c | 22 + .../src/ft_list/ft_ll/ft_ll_pushpop.c | 76 ++ libft_personal/src/ft_list/ft_ll/ft_ll_rev.c | 35 + libft_personal/src/ft_list/ft_ll/ft_ll_size.c | 44 + libft_personal/src/ft_list/ft_ll/ft_ll_sub.c | 30 + libft_personal/src/ft_map/ft_map_clear.c | 34 + libft_personal/src/ft_map/ft_map_create.c | 38 + libft_personal/src/ft_map/ft_map_destroy.c | 72 ++ libft_personal/src/ft_map/ft_map_get.c | 53 + libft_personal/src/ft_map/ft_map_hash.c | 93 ++ libft_personal/src/ft_map/ft_map_remove.c | 46 + libft_personal/src/ft_map/ft_map_set.c | 67 ++ libft_personal/src/ft_math/ft_abs.c | 18 + libft_personal/src/ft_math/ft_align.c | 28 + libft_personal/src/ft_math/ft_clamp.c | 38 + libft_personal/src/ft_math/ft_complex.c | 35 + libft_personal/src/ft_math/ft_intrange.c | 49 + libft_personal/src/ft_math/ft_log.c | 44 + libft_personal/src/ft_math/ft_minmax.c | 25 + libft_personal/src/ft_math/ft_pow.c | 30 + libft_personal/src/ft_math/ft_round.c | 24 + libft_personal/src/ft_math/ft_sqrt.c | 29 + .../src/ft_optional/ft_optional_chain.c | 44 + .../src/ft_optional/ft_optional_copy.c | 19 + .../src/ft_optional/ft_optional_destroy.c | 24 + .../src/ft_optional/ft_optional_new.c | 53 + .../src/ft_optional/ft_optional_unwrap.c | 20 + libft_personal/src/ft_pair/ft_pair_cmp.c | 52 + libft_personal/src/ft_pair/ft_pair_destroy.c | 26 + libft_personal/src/ft_pair/ft_pair_get.c | 28 + libft_personal/src/ft_pair/ft_pair_new.c | 26 + libft_personal/src/ft_pair/ft_pair_set.c | 22 + .../src/ft_string/ft_chr/ft_isalnum.c | 18 + .../src/ft_string/ft_chr/ft_isalpha.c | 18 + .../src/ft_string/ft_chr/ft_isascii.c | 16 + .../src/ft_string/ft_chr/ft_isdigit.c | 16 + .../src/ft_string/ft_chr/ft_ishexdigit.c | 18 + .../src/ft_string/ft_chr/ft_islower.c | 16 + .../src/ft_string/ft_chr/ft_isoctdigit.c | 16 + .../src/ft_string/ft_chr/ft_isprint.c | 16 + .../src/ft_string/ft_chr/ft_isspace.c | 17 + .../src/ft_string/ft_chr/ft_isupper.c | 16 + .../src/ft_string/ft_chr/ft_putchar_fd.c | 19 + .../src/ft_string/ft_chr/ft_tolower.c | 20 + .../src/ft_string/ft_chr/ft_toupper.c | 20 + .../src/ft_string/ft_mem/ft_apply_2d.c | 23 + .../src/ft_string/ft_mem/ft_arena.c | 33 + .../src/ft_string/ft_mem/ft_arena_free.c | 0 .../src/ft_string/ft_mem/ft_bzero.c | 50 + .../src/ft_string/ft_mem/ft_calloc.c | 31 + .../src/ft_string/ft_mem/ft_fd_to_buff.c | 45 + libft_personal/src/ft_string/ft_mem/ft_free.c | 21 + .../src/ft_string/ft_mem/ft_free_2d.c | 20 + .../src/ft_string/ft_mem/ft_len_2d.c | 23 + .../src/ft_string/ft_mem/ft_malloc.c | 21 + .../src/ft_string/ft_mem/ft_memchr.c | 120 +++ .../src/ft_string/ft_mem/ft_memcmp.c | 28 + .../src/ft_string/ft_mem/ft_memcpy.c | 18 + .../src/ft_string/ft_mem/ft_memmap.c | 36 + .../src/ft_string/ft_mem/ft_memmove.c | 116 +++ .../src/ft_string/ft_mem/ft_memset.c | 28 + .../src/ft_string/ft_mem/ft_narena.c | 76 ++ .../src/ft_string/ft_mem/ft_qsort.c | 42 + .../src/ft_string/ft_mem/ft_realloc.c | 33 + libft_personal/src/ft_string/ft_mem/ft_swap.c | 20 + libft_personal/src/ft_string/ft_str/ft_atof.c | 57 ++ libft_personal/src/ft_string/ft_str/ft_atoi.c | 38 + .../src/ft_string/ft_str/ft_atoi_base.c | 57 ++ libft_personal/src/ft_string/ft_str/ft_itoa.c | 18 + .../src/ft_string/ft_str/ft_itoa_base.c | 81 ++ .../src/ft_string/ft_str/ft_perror.c | 24 + .../src/ft_string/ft_str/ft_putendl_fd.c | 28 + .../src/ft_string/ft_str/ft_putnbr_fd.c | 42 + .../src/ft_string/ft_str/ft_putstr_fd.c | 21 + .../src/ft_string/ft_str/ft_shift_args.c | 25 + .../src/ft_string/ft_str/ft_split.c | 88 ++ .../src/ft_string/ft_str/ft_splits.c | 65 ++ .../src/ft_string/ft_str/ft_str_isalnum.c | 21 + .../src/ft_string/ft_str/ft_str_isalpha.c | 31 + .../src/ft_string/ft_str/ft_str_isbool.c | 24 + .../src/ft_string/ft_str/ft_str_isdigit.c | 22 + .../src/ft_string/ft_str/ft_str_isdouble.c | 18 + .../src/ft_string/ft_str/ft_str_isfloat.c | 42 + .../src/ft_string/ft_str/ft_str_ishex.c | 23 + .../src/ft_string/ft_str/ft_str_isint.c | 39 + .../src/ft_string/ft_str/ft_str_islong.c | 39 + .../src/ft_string/ft_str/ft_str_isnum.c | 27 + .../src/ft_string/ft_str/ft_str_isoct.c | 23 + .../src/ft_string/ft_str/ft_str_isvalid.c | 32 + .../src/ft_string/ft_str/ft_str_replace.c | 74 ++ .../src/ft_string/ft_str/ft_strappend_c.c | 32 + .../src/ft_string/ft_str/ft_strchr.c | 32 + .../src/ft_string/ft_str/ft_strclen.c | 23 + .../src/ft_string/ft_str/ft_strcmp.c | 19 + .../src/ft_string/ft_str/ft_strcnb.c | 30 + .../src/ft_string/ft_str/ft_strcspn.c | 27 + .../src/ft_string/ft_str/ft_strdup.c | 18 + .../src/ft_string/ft_str/ft_strend_with.c | 32 + .../src/ft_string/ft_str/ft_strerror.c | 104 ++ .../src/ft_string/ft_str/ft_striteri.c | 25 + .../src/ft_string/ft_str/ft_strjoin.c | 40 + .../src/ft_string/ft_str/ft_strlcat.c | 37 + .../src/ft_string/ft_str/ft_strlcpy.c | 35 + .../src/ft_string/ft_str/ft_strlen.c | 23 + .../src/ft_string/ft_str/ft_strmapi.c | 30 + .../src/ft_string/ft_str/ft_strncmp.c | 28 + .../src/ft_string/ft_str/ft_strndup.c | 30 + .../src/ft_string/ft_str/ft_strnstr.c | 29 + .../src/ft_string/ft_str/ft_strrchr.c | 32 + .../src/ft_string/ft_str/ft_strspn.c | 27 + .../src/ft_string/ft_str/ft_strstart_with.c | 20 + .../src/ft_string/ft_str/ft_strtok.c | 35 + .../src/ft_string/ft_str/ft_strtrim.c | 71 ++ .../src/ft_string/ft_str/ft_substr.c | 38 + libft_personal/src/ft_string/ft_str/ft_utoa.c | 51 + .../src/ft_string/ft_str/get_next_line.c | 102 ++ .../ft_string/ft_string/ft_string_append.c | 80 ++ .../src/ft_string/ft_string/ft_string_chr.c | 65 ++ .../src/ft_string/ft_string/ft_string_clear.c | 19 + .../src/ft_string/ft_string/ft_string_cmp.c | 39 + .../ft_string/ft_string/ft_string_destroy.c | 22 + .../src/ft_string/ft_string/ft_string_from.c | 90 ++ .../src/ft_string/ft_string/ft_string_get.c | 28 + .../ft_string/ft_string/ft_string_insert.c | 133 +++ .../src/ft_string/ft_string/ft_string_new.c | 30 + .../src/ft_string/ft_string/ft_string_put.c | 24 + .../ft_string/ft_string/ft_string_replace.c | 51 + .../ft_string/ft_string/ft_string_reserve.c | 27 + .../ft_string/ft_string/ft_string_resize.c | 25 + .../src/ft_string/ft_string/ft_string_set.c | 51 + .../ft_string/ft_string/ft_string_shrink.c | 27 + .../ft_string/ft_string/ft_string_substr.c | 32 + .../ft_string/ft_string/ft_string_to_str.c | 25 + .../src/ft_string/ft_string/ft_string_trim.c | 47 + libft_personal/src/ft_vector/ft_vec_add.c | 27 + libft_personal/src/ft_vector/ft_vec_apply.c | 23 + libft_personal/src/ft_vector/ft_vec_at.c | 19 + libft_personal/src/ft_vector/ft_vec_cat.c | 29 + libft_personal/src/ft_vector/ft_vec_clear.c | 23 + libft_personal/src/ft_vector/ft_vec_destroy.c | 24 + libft_personal/src/ft_vector/ft_vec_filter.c | 37 + libft_personal/src/ft_vector/ft_vec_get.c | 28 + libft_personal/src/ft_vector/ft_vec_map.c | 31 + libft_personal/src/ft_vector/ft_vec_new.c | 67 ++ libft_personal/src/ft_vector/ft_vec_pop.c | 24 + libft_personal/src/ft_vector/ft_vec_remove.c | 38 + libft_personal/src/ft_vector/ft_vec_reserve.c | 38 + libft_personal/src/ft_vector/ft_vec_reverse.c | 31 + libft_personal/src/ft_vector/ft_vec_shift.c | 23 + libft_personal/src/ft_vector/ft_vec_shrink.c | 36 + libft_personal/src/ft_vector/ft_vec_sort.c | 44 + libft_personal/src/ft_vector/ft_vec_swap.c | 22 + .../src/ft_vector/ft_vec_to_array.c | 23 + libft_personal/tests/ft_args/args_tests.c | 33 + .../tests/ft_args/tests_custom_checker.c | 30 + libft_personal/tests/ft_args/tests_optlist.c | 37 + libft_personal/tests/ft_args/tests_progname.c | 30 + .../tests/ft_args/tests_setup_prog.c | 26 + libft_personal/tests/ft_args/tests_version.c | 31 + .../tests/ft_list/dl_tests/dl_list_tests.c | 52 + .../tests/ft_list/dl_tests/dl_tests_utils.c | 26 + .../tests/ft_list/dl_tests/tests_dlist_add.c | 73 ++ .../ft_list/dl_tests/tests_dlist_apply.c | 72 ++ .../ft_list/dl_tests/tests_dlist_clear.c | 68 ++ .../tests/ft_list/dl_tests/tests_dlist_copy.c | 65 ++ .../ft_list/dl_tests/tests_dlist_create.c | 34 + .../ft_list/dl_tests/tests_dlist_delete.c | 110 +++ .../tests/ft_list/dl_tests/tests_dlist_find.c | 45 + .../tests/ft_list/dl_tests/tests_dlist_get.c | 60 ++ .../ft_list/dl_tests/tests_dlist_iterators.c | 81 ++ .../tests/ft_list/dl_tests/tests_dlist_map.c | 46 + .../tests/ft_list/dl_tests/tests_dlist_new.c | 26 + .../tests/ft_list/dl_tests/tests_dlist_push.c | 115 +++ .../tests/ft_list/dl_tests/tests_dlist_rev.c | 41 + .../ft_list/dl_tests/tests_dlist_sizers.c | 59 ++ .../ft_list/dl_tests/tests_dlist_subrange.c | 44 + .../tests/ft_list/ll_tests/ll_list_tests.c | 43 + .../tests/ft_list/ll_tests/ll_tests_utils.c | 26 + .../tests/ft_list/ll_tests/tests_list_add.c | 65 ++ .../tests/ft_list/ll_tests/tests_list_apply.c | 92 ++ .../tests/ft_list/ll_tests/tests_list_clear.c | 30 + .../tests/ft_list/ll_tests/tests_list_copy.c | 62 ++ .../ft_list/ll_tests/tests_list_create.c | 34 + .../ft_list/ll_tests/tests_list_deletors.c | 81 ++ .../tests/ft_list/ll_tests/tests_list_find.c | 46 + .../tests/ft_list/ll_tests/tests_list_get.c | 54 + .../ft_list/ll_tests/tests_list_iterators.c | 59 ++ .../tests/ft_list/ll_tests/tests_list_map.c | 43 + .../tests/ft_list/ll_tests/tests_list_new.c | 26 + .../tests/ft_list/ll_tests/tests_list_push.c | 134 +++ .../tests/ft_list/ll_tests/tests_list_rev.c | 39 + .../ft_list/ll_tests/tests_list_sizers.c | 59 ++ .../ft_list/ll_tests/tests_list_subrange.c | 53 + libft_personal/tests/ft_map/map_tests.c | 39 + .../tests/ft_map/tests_map_cappacity.c | 27 + libft_personal/tests/ft_map/tests_map_clear.c | 32 + .../tests/ft_map/tests_map_create.c | 25 + .../tests/ft_map/tests_map_destroy.c | 48 + libft_personal/tests/ft_map/tests_map_get.c | 84 ++ libft_personal/tests/ft_map/tests_map_hash.c | 42 + .../tests/ft_map/tests_map_remove.c | 80 ++ libft_personal/tests/ft_map/tests_map_set.c | 34 + .../tests/ft_map/tests_map_set_cmphash.c | 38 + libft_personal/tests/ft_map/tests_map_size.c | 33 + libft_personal/tests/ft_math/math_tests.c | 37 + libft_personal/tests/ft_math/tests_abs.c | 29 + libft_personal/tests/ft_math/tests_align.c | 57 ++ libft_personal/tests/ft_math/tests_clamp.c | 85 ++ libft_personal/tests/ft_math/tests_complex.c | 78 ++ libft_personal/tests/ft_math/tests_intrange.c | 134 +++ libft_personal/tests/ft_math/tests_log.c | 74 ++ libft_personal/tests/ft_math/tests_minmax.c | 39 + libft_personal/tests/ft_math/tests_pow.c | 33 + libft_personal/tests/ft_math/tests_round.c | 32 + libft_personal/tests/ft_math/tests_sqrt.c | 33 + .../tests/ft_optional/optional_tests.c | 43 + .../tests/ft_optional/tests_optional_chain.c | 41 + .../tests/ft_optional/tests_optional_copy.c | 34 + .../ft_optional/tests_optional_destroy.c | 35 + .../tests/ft_optional/tests_optional_dup.c | 33 + .../ft_optional/tests_optional_from_val.c | 35 + .../tests/ft_optional/tests_optional_map.c | 34 + .../tests/ft_optional/tests_optional_new.c | 29 + .../tests/ft_optional/tests_optional_unwrap.c | 37 + libft_personal/tests/ft_pair/pair_tests.c | 34 + libft_personal/tests/ft_pair/tests_pair_cmp.c | 74 ++ .../tests/ft_pair/tests_pair_cmp_first.c | 74 ++ .../tests/ft_pair/tests_pair_cmp_second.c | 73 ++ .../tests/ft_pair/tests_pair_destroy.c | 34 + .../tests/ft_pair/tests_pair_get_first.c | 31 + .../tests/ft_pair/tests_pair_get_second.c | 31 + libft_personal/tests/ft_pair/tests_pair_new.c | 35 + libft_personal/tests/ft_pair/tests_pair_set.c | 35 + .../tests/ft_string/ft_char/ft_char_tests.c | 31 + .../tests/ft_string/ft_char/tests_isalnum.c | 28 + .../tests/ft_string/ft_char/tests_isalpha.c | 27 + .../tests/ft_string/ft_char/tests_isascii.c | 27 + .../tests/ft_string/ft_char/tests_isdigit.c | 27 + .../ft_string/ft_char/tests_ishexdigit.c | 35 + .../ft_string/ft_char/tests_isoctdigit.c | 34 + .../tests/ft_string/ft_char/tests_isprint.c | 27 + .../tests/ft_string/ft_char/tests_isspace.c | 35 + .../tests/ft_string/ft_char/tests_puchar.c | 46 + .../tests/ft_string/ft_char/tests_tolower.c | 34 + .../tests/ft_string/ft_char/tests_toupper.c | 34 + .../tests/ft_string/ft_mem/mem_tests.c | 32 + .../tests/ft_string/ft_mem/tests_apply_2d.c | 41 + .../tests/ft_string/ft_mem/tests_bzero.c | 25 + .../tests/ft_string/ft_mem/tests_calloc.c | 33 + .../tests/ft_string/ft_mem/tests_fd_to_buff.c | 71 ++ .../tests/ft_string/ft_mem/tests_free.c | 26 + .../tests/ft_string/ft_mem/tests_free_2d.c | 24 + .../tests/ft_string/ft_mem/tests_len_2d.c | 26 + .../tests/ft_string/ft_mem/tests_memchr.c | 32 + .../tests/ft_string/ft_mem/tests_memcmp.c | 35 + .../tests/ft_string/ft_mem/tests_memcpy.c | 31 + .../tests/ft_string/ft_mem/tests_memmap.c | 40 + .../tests/ft_string/ft_mem/tests_memmove.c | 37 + .../tests/ft_string/ft_mem/tests_memset.c | 37 + .../tests/ft_string/ft_mem/tests_qsort.c | 31 + .../tests/ft_string/ft_mem/tests_realloc.c | 31 + .../tests/ft_string/ft_mem/tests_swap.c | 32 + .../tests/ft_string/ft_str/str_tests.c | 53 + .../tests/ft_string/ft_str/test_atof.c | 39 + .../tests/ft_string/ft_str/test_atoi.c | 42 + .../tests/ft_string/ft_str/test_atoi_base.c | 99 ++ .../tests/ft_string/ft_str/test_gnl.c | 75 ++ .../tests/ft_string/ft_str/test_itoa.c | 32 + .../tests/ft_string/ft_str/test_itoa_base.c | 67 ++ .../tests/ft_string/ft_str/test_putendl.c | 43 + .../tests/ft_string/ft_str/test_putnbr.c | 81 ++ .../tests/ft_string/ft_str/test_putstr.c | 41 + .../tests/ft_string/ft_str/test_shift_args.c | 41 + .../tests/ft_string/ft_str/test_split.c | 38 + .../tests/ft_string/ft_str/test_splits.c | 35 + .../tests/ft_string/ft_str/test_str_isalnum.c | 32 + .../tests/ft_string/ft_str/test_str_isalpha.c | 32 + .../tests/ft_string/ft_str/test_str_isbool.c | 36 + .../tests/ft_string/ft_str/test_str_isdigit.c | 32 + .../ft_string/ft_str/test_str_isdouble.c | 36 + .../tests/ft_string/ft_str/test_str_isfloat.c | 30 + .../tests/ft_string/ft_str/test_str_ishex.c | 38 + .../tests/ft_string/ft_str/test_str_isint.c | 33 + .../tests/ft_string/ft_str/test_str_islong.c | 39 + .../tests/ft_string/ft_str/test_str_isnum.c | 32 + .../tests/ft_string/ft_str/test_str_isoct.c | 28 + .../tests/ft_string/ft_str/test_str_isvalid.c | 34 + .../tests/ft_string/ft_str/test_str_replace.c | 35 + .../ft_string/ft_str/test_str_replace_chr.c | 32 + .../tests/ft_string/ft_str/test_strappend_c.c | 40 + .../tests/ft_string/ft_str/test_strchr.c | 35 + .../tests/ft_string/ft_str/test_strclen.c | 28 + .../tests/ft_string/ft_str/test_strcmp.c | 35 + .../tests/ft_string/ft_str/test_strcnb.c | 33 + .../tests/ft_string/ft_str/test_strcspn.c | 28 + .../tests/ft_string/ft_str/test_strdup.c | 26 + .../tests/ft_string/ft_str/test_strend_with.c | 28 + .../tests/ft_string/ft_str/test_striteri.c | 33 + .../tests/ft_string/ft_str/test_strjoin.c | 75 ++ .../tests/ft_string/ft_str/test_strlcat.c | 44 + .../tests/ft_string/ft_str/test_strlcpy.c | 74 ++ .../tests/ft_string/ft_str/test_strlen.c | 31 + .../tests/ft_string/ft_str/test_strmapi.c | 43 + .../tests/ft_string/ft_str/test_strncmp.c | 38 + .../tests/ft_string/ft_str/test_strndup.c | 38 + .../tests/ft_string/ft_str/test_strnstr.c | 37 + .../tests/ft_string/ft_str/test_strrchr.c | 36 + .../tests/ft_string/ft_str/test_strspn.c | 28 + .../ft_string/ft_str/test_strstart_with.c | 28 + .../tests/ft_string/ft_str/test_strtok.c | 39 + .../tests/ft_string/ft_str/test_strtrim.c | 35 + .../tests/ft_string/ft_str/test_substr.c | 35 + .../tests/ft_string/ft_str/test_utoa.c | 33 + .../ft_string/ft_string/t_string_tests.c | 60 ++ .../tests/ft_string/ft_string/test_append.c | 35 + .../tests/ft_string/ft_string/test_append_c.c | 37 + .../tests/ft_string/ft_string/test_append_n.c | 37 + .../tests/ft_string/ft_string/test_append_s.c | 40 + .../ft_string/ft_string/test_append_sn.c | 38 + .../tests/ft_string/ft_string/test_cap.c | 25 + .../tests/ft_string/ft_string/test_chr.c | 34 + .../tests/ft_string/ft_string/test_clear.c | 32 + .../tests/ft_string/ft_string/test_cmp.c | 33 + .../tests/ft_string/ft_string/test_cmp_str.c | 36 + .../tests/ft_string/ft_string/test_destroy.c | 31 + .../tests/ft_string/ft_string/test_from.c | 31 + .../tests/ft_string/ft_string/test_from_c.c | 29 + .../tests/ft_string/ft_string/test_from_n.c | 34 + .../tests/ft_string/ft_string/test_from_s.c | 32 + .../tests/ft_string/ft_string/test_from_sn.c | 38 + .../tests/ft_string/ft_string/test_get.c | 27 + .../tests/ft_string/ft_string/test_insert.c | 33 + .../tests/ft_string/ft_string/test_insert_c.c | 33 + .../tests/ft_string/ft_string/test_insert_n.c | 33 + .../tests/ft_string/ft_string/test_insert_s.c | 36 + .../ft_string/ft_string/test_insert_sn.c | 36 + .../tests/ft_string/ft_string/test_len.c | 25 + .../tests/ft_string/ft_string/test_ncmp.c | 29 + .../tests/ft_string/ft_string/test_ncmp_str.c | 39 + .../tests/ft_string/ft_string/test_new.c | 29 + .../tests/ft_string/ft_string/test_offset.c | 35 + .../tests/ft_string/ft_string/test_put.c | 72 ++ .../tests/ft_string/ft_string/test_rchr.c | 34 + .../tests/ft_string/ft_string/test_replace.c | 30 + .../ft_string/ft_string/test_replace_chr.c | 26 + .../tests/ft_string/ft_string/test_reserve.c | 33 + .../tests/ft_string/ft_string/test_resize.c | 30 + .../tests/ft_string/ft_string/test_roffset.c | 35 + .../tests/ft_string/ft_string/test_set.c | 35 + .../ft_string/ft_string/test_set_inplace.c | 35 + .../tests/ft_string/ft_string/test_set_n.c | 29 + .../tests/ft_string/ft_string/test_shrink.c | 41 + .../tests/ft_string/ft_string/test_substr.c | 35 + .../tests/ft_string/ft_string/test_to_str.c | 28 + .../tests/ft_string/ft_string/test_trim.c | 30 + .../tests/ft_string/ft_string/test_trim_chr.c | 30 + .../tests/ft_string/ft_string/test_trimstr.c | 30 + libft_personal/tests/ft_string/string_tests.c | 57 ++ .../tests/ft_vector/tests_vec_add.c | 39 + .../tests/ft_vector/tests_vec_apply.c | 30 + libft_personal/tests/ft_vector/tests_vec_at.c | 40 + .../tests/ft_vector/tests_vec_cat.c | 70 ++ .../tests/ft_vector/tests_vec_clear.c | 39 + .../ft_vector/tests_vec_convert_alloc_array.c | 43 + .../tests/ft_vector/tests_vec_destroy.c | 34 + .../tests/ft_vector/tests_vec_filter.c | 45 + .../tests/ft_vector/tests_vec_from_array.c | 61 ++ .../tests/ft_vector/tests_vec_from_size.c | 29 + .../tests/ft_vector/tests_vec_get.c | 43 + .../tests/ft_vector/tests_vec_map.c | 36 + .../tests/ft_vector/tests_vec_new.c | 29 + .../tests/ft_vector/tests_vec_pop.c | 39 + .../tests/ft_vector/tests_vec_remove.c | 71 ++ .../tests/ft_vector/tests_vec_remove_if.c | 40 + .../tests/ft_vector/tests_vec_reserve.c | 35 + .../tests/ft_vector/tests_vec_reverse.c | 42 + .../tests/ft_vector/tests_vec_shift.c | 42 + .../tests/ft_vector/tests_vec_shrink.c | 46 + .../tests/ft_vector/tests_vec_sort.c | 40 + .../tests/ft_vector/tests_vec_swap.c | 40 + .../tests/ft_vector/tests_vec_to_array.c | 40 + libft_personal/tests/ft_vector/vector_tests.c | 37 + libft_personal/tests/lambdas_for_tests.c | 60 ++ libft_personal/tests/main_tests.c | 67 ++ libft_personal/tests/tests_utils.c | 111 +++ 457 files changed, 21807 insertions(+), 61 deletions(-) delete mode 100644 libft/.gitignore create mode 100644 libft_personal/Makefile create mode 100644 libft_personal/include/ft_args.h create mode 100644 libft_personal/include/ft_args_types.h create mode 100644 libft_personal/include/ft_char.h create mode 100644 libft_personal/include/ft_colors.h create mode 100644 libft_personal/include/ft_defs.h create mode 100644 libft_personal/include/ft_errno.h create mode 100644 libft_personal/include/ft_list.h create mode 100644 libft_personal/include/ft_list_types.h create mode 100644 libft_personal/include/ft_map.h create mode 100644 libft_personal/include/ft_map_types.h create mode 100644 libft_personal/include/ft_math.h create mode 100644 libft_personal/include/ft_math_types.h create mode 100644 libft_personal/include/ft_optional.h create mode 100644 libft_personal/include/ft_optional_types.h create mode 100644 libft_personal/include/ft_pair.h create mode 100644 libft_personal/include/ft_pair_types.h create mode 100644 libft_personal/include/ft_string.h create mode 100644 libft_personal/include/ft_string_types.h create mode 100644 libft_personal/include/ft_types.h create mode 100644 libft_personal/include/ft_vector.h create mode 100644 libft_personal/include/ft_vector_types.h create mode 100644 libft_personal/include/internal/args_helper.h create mode 100644 libft_personal/include/tests/args_tests.h create mode 100644 libft_personal/include/tests/char_tests.h create mode 100644 libft_personal/include/tests/dl_tests.h create mode 100644 libft_personal/include/tests/lists_test_utils.h create mode 100644 libft_personal/include/tests/ll_tests.h create mode 100644 libft_personal/include/tests/map_tests.h create mode 100644 libft_personal/include/tests/math_tests.h create mode 100644 libft_personal/include/tests/optional_test.h create mode 100644 libft_personal/include/tests/pair_tests.h create mode 100644 libft_personal/include/tests/str__mem_tests.h create mode 100644 libft_personal/include/tests/str__str_tests.h create mode 100644 libft_personal/include/tests/str__t_str_test.h create mode 100644 libft_personal/include/tests/tests.h create mode 100644 libft_personal/include/tests/tests_lambda_functions.h create mode 100644 libft_personal/include/tests/vector_tests.h create mode 100644 libft_personal/src/ft_args/ft_arg_custom_checker.c create mode 100644 libft_personal/src/ft_args/ft_parse_args.c create mode 100644 libft_personal/src/ft_args/ft_parse_err.c create mode 100644 libft_personal/src/ft_args/ft_parse_opt.c create mode 100644 libft_personal/src/ft_args/ft_progname.c create mode 100644 libft_personal/src/ft_args/ft_set_opt_args.c create mode 100644 libft_personal/src/ft_args/ft_setup_prog.c create mode 100644 libft_personal/src/ft_args/ft_version.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_add.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_apply.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_clear.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_create.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_delete.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_find.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_getters.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_iterator.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_map.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_new.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_pushpop.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_rev.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_size.c create mode 100644 libft_personal/src/ft_list/ft_dl/ft_dl_sub.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_add.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_apply.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_clear.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_create.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_delete.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_find.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_getters.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_iterator.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_map.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_new.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_pushpop.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_rev.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_size.c create mode 100644 libft_personal/src/ft_list/ft_ll/ft_ll_sub.c create mode 100644 libft_personal/src/ft_map/ft_map_clear.c create mode 100644 libft_personal/src/ft_map/ft_map_create.c create mode 100644 libft_personal/src/ft_map/ft_map_destroy.c create mode 100644 libft_personal/src/ft_map/ft_map_get.c create mode 100644 libft_personal/src/ft_map/ft_map_hash.c create mode 100644 libft_personal/src/ft_map/ft_map_remove.c create mode 100644 libft_personal/src/ft_map/ft_map_set.c create mode 100644 libft_personal/src/ft_math/ft_abs.c create mode 100644 libft_personal/src/ft_math/ft_align.c create mode 100644 libft_personal/src/ft_math/ft_clamp.c create mode 100644 libft_personal/src/ft_math/ft_complex.c create mode 100644 libft_personal/src/ft_math/ft_intrange.c create mode 100644 libft_personal/src/ft_math/ft_log.c create mode 100644 libft_personal/src/ft_math/ft_minmax.c create mode 100644 libft_personal/src/ft_math/ft_pow.c create mode 100644 libft_personal/src/ft_math/ft_round.c create mode 100644 libft_personal/src/ft_math/ft_sqrt.c create mode 100644 libft_personal/src/ft_optional/ft_optional_chain.c create mode 100644 libft_personal/src/ft_optional/ft_optional_copy.c create mode 100644 libft_personal/src/ft_optional/ft_optional_destroy.c create mode 100644 libft_personal/src/ft_optional/ft_optional_new.c create mode 100644 libft_personal/src/ft_optional/ft_optional_unwrap.c create mode 100644 libft_personal/src/ft_pair/ft_pair_cmp.c create mode 100644 libft_personal/src/ft_pair/ft_pair_destroy.c create mode 100644 libft_personal/src/ft_pair/ft_pair_get.c create mode 100644 libft_personal/src/ft_pair/ft_pair_new.c create mode 100644 libft_personal/src/ft_pair/ft_pair_set.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isalnum.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isalpha.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isascii.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isdigit.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_ishexdigit.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_islower.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isoctdigit.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isprint.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isspace.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_isupper.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_putchar_fd.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_tolower.c create mode 100644 libft_personal/src/ft_string/ft_chr/ft_toupper.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_apply_2d.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_arena.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_arena_free.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_bzero.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_calloc.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_fd_to_buff.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_free.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_free_2d.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_len_2d.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_malloc.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memchr.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memcmp.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memcpy.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memmap.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memmove.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_memset.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_narena.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_qsort.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_realloc.c create mode 100644 libft_personal/src/ft_string/ft_mem/ft_swap.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_atof.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_atoi.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_atoi_base.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_itoa.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_itoa_base.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_perror.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_putendl_fd.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_putnbr_fd.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_putstr_fd.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_shift_args.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_split.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_splits.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isalnum.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isalpha.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isbool.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isdigit.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isdouble.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isfloat.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_ishex.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isint.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_islong.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isnum.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isoct.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_isvalid.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_str_replace.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strappend_c.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strchr.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strclen.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strcmp.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strcnb.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strcspn.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strdup.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strend_with.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strerror.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_striteri.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strjoin.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strlcat.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strlcpy.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strlen.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strmapi.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strncmp.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strndup.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strnstr.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strrchr.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strspn.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strstart_with.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strtok.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_strtrim.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_substr.c create mode 100644 libft_personal/src/ft_string/ft_str/ft_utoa.c create mode 100644 libft_personal/src/ft_string/ft_str/get_next_line.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_append.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_chr.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_clear.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_cmp.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_destroy.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_from.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_get.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_insert.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_new.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_put.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_replace.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_reserve.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_resize.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_set.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_shrink.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_substr.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_to_str.c create mode 100644 libft_personal/src/ft_string/ft_string/ft_string_trim.c create mode 100644 libft_personal/src/ft_vector/ft_vec_add.c create mode 100644 libft_personal/src/ft_vector/ft_vec_apply.c create mode 100644 libft_personal/src/ft_vector/ft_vec_at.c create mode 100644 libft_personal/src/ft_vector/ft_vec_cat.c create mode 100644 libft_personal/src/ft_vector/ft_vec_clear.c create mode 100644 libft_personal/src/ft_vector/ft_vec_destroy.c create mode 100644 libft_personal/src/ft_vector/ft_vec_filter.c create mode 100644 libft_personal/src/ft_vector/ft_vec_get.c create mode 100644 libft_personal/src/ft_vector/ft_vec_map.c create mode 100644 libft_personal/src/ft_vector/ft_vec_new.c create mode 100644 libft_personal/src/ft_vector/ft_vec_pop.c create mode 100644 libft_personal/src/ft_vector/ft_vec_remove.c create mode 100644 libft_personal/src/ft_vector/ft_vec_reserve.c create mode 100644 libft_personal/src/ft_vector/ft_vec_reverse.c create mode 100644 libft_personal/src/ft_vector/ft_vec_shift.c create mode 100644 libft_personal/src/ft_vector/ft_vec_shrink.c create mode 100644 libft_personal/src/ft_vector/ft_vec_sort.c create mode 100644 libft_personal/src/ft_vector/ft_vec_swap.c create mode 100644 libft_personal/src/ft_vector/ft_vec_to_array.c create mode 100644 libft_personal/tests/ft_args/args_tests.c create mode 100644 libft_personal/tests/ft_args/tests_custom_checker.c create mode 100644 libft_personal/tests/ft_args/tests_optlist.c create mode 100644 libft_personal/tests/ft_args/tests_progname.c create mode 100644 libft_personal/tests/ft_args/tests_setup_prog.c create mode 100644 libft_personal/tests/ft_args/tests_version.c create mode 100644 libft_personal/tests/ft_list/dl_tests/dl_list_tests.c create mode 100644 libft_personal/tests/ft_list/dl_tests/dl_tests_utils.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_add.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_apply.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_clear.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_copy.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_create.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_delete.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_find.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_get.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_iterators.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_map.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_new.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_push.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_rev.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_sizers.c create mode 100644 libft_personal/tests/ft_list/dl_tests/tests_dlist_subrange.c create mode 100644 libft_personal/tests/ft_list/ll_tests/ll_list_tests.c create mode 100644 libft_personal/tests/ft_list/ll_tests/ll_tests_utils.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_add.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_apply.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_clear.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_copy.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_create.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_deletors.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_find.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_get.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_iterators.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_map.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_new.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_push.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_rev.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_sizers.c create mode 100644 libft_personal/tests/ft_list/ll_tests/tests_list_subrange.c create mode 100644 libft_personal/tests/ft_map/map_tests.c create mode 100644 libft_personal/tests/ft_map/tests_map_cappacity.c create mode 100644 libft_personal/tests/ft_map/tests_map_clear.c create mode 100644 libft_personal/tests/ft_map/tests_map_create.c create mode 100644 libft_personal/tests/ft_map/tests_map_destroy.c create mode 100644 libft_personal/tests/ft_map/tests_map_get.c create mode 100644 libft_personal/tests/ft_map/tests_map_hash.c create mode 100644 libft_personal/tests/ft_map/tests_map_remove.c create mode 100644 libft_personal/tests/ft_map/tests_map_set.c create mode 100644 libft_personal/tests/ft_map/tests_map_set_cmphash.c create mode 100644 libft_personal/tests/ft_map/tests_map_size.c create mode 100644 libft_personal/tests/ft_math/math_tests.c create mode 100644 libft_personal/tests/ft_math/tests_abs.c create mode 100644 libft_personal/tests/ft_math/tests_align.c create mode 100644 libft_personal/tests/ft_math/tests_clamp.c create mode 100644 libft_personal/tests/ft_math/tests_complex.c create mode 100644 libft_personal/tests/ft_math/tests_intrange.c create mode 100644 libft_personal/tests/ft_math/tests_log.c create mode 100644 libft_personal/tests/ft_math/tests_minmax.c create mode 100644 libft_personal/tests/ft_math/tests_pow.c create mode 100644 libft_personal/tests/ft_math/tests_round.c create mode 100644 libft_personal/tests/ft_math/tests_sqrt.c create mode 100644 libft_personal/tests/ft_optional/optional_tests.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_chain.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_copy.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_destroy.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_dup.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_from_val.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_map.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_new.c create mode 100644 libft_personal/tests/ft_optional/tests_optional_unwrap.c create mode 100644 libft_personal/tests/ft_pair/pair_tests.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_cmp.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_cmp_first.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_cmp_second.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_destroy.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_get_first.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_get_second.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_new.c create mode 100644 libft_personal/tests/ft_pair/tests_pair_set.c create mode 100644 libft_personal/tests/ft_string/ft_char/ft_char_tests.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isalnum.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isalpha.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isascii.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isdigit.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_ishexdigit.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isoctdigit.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isprint.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_isspace.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_puchar.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_tolower.c create mode 100644 libft_personal/tests/ft_string/ft_char/tests_toupper.c create mode 100644 libft_personal/tests/ft_string/ft_mem/mem_tests.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_apply_2d.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_bzero.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_calloc.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_fd_to_buff.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_free.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_free_2d.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_len_2d.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memchr.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memcmp.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memcpy.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memmap.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memmove.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_memset.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_qsort.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_realloc.c create mode 100644 libft_personal/tests/ft_string/ft_mem/tests_swap.c create mode 100644 libft_personal/tests/ft_string/ft_str/str_tests.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_atof.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_atoi.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_atoi_base.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_gnl.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_itoa.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_itoa_base.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_putendl.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_putnbr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_putstr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_shift_args.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_split.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_splits.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isalnum.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isalpha.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isbool.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isdigit.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isdouble.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isfloat.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_ishex.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isint.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_islong.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isnum.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isoct.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_isvalid.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_replace.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_str_replace_chr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strappend_c.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strchr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strclen.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strcmp.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strcnb.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strcspn.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strdup.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strend_with.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_striteri.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strjoin.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strlcat.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strlcpy.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strlen.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strmapi.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strncmp.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strndup.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strnstr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strrchr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strspn.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strstart_with.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strtok.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_strtrim.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_substr.c create mode 100644 libft_personal/tests/ft_string/ft_str/test_utoa.c create mode 100644 libft_personal/tests/ft_string/ft_string/t_string_tests.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_append.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_append_c.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_append_n.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_append_s.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_append_sn.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_cap.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_chr.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_clear.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_cmp.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_cmp_str.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_destroy.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_from.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_from_c.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_from_n.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_from_s.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_from_sn.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_get.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_insert.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_insert_c.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_insert_n.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_insert_s.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_insert_sn.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_len.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_ncmp.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_ncmp_str.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_new.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_offset.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_put.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_rchr.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_replace.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_replace_chr.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_reserve.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_resize.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_roffset.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_set.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_set_inplace.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_set_n.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_shrink.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_substr.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_to_str.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_trim.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_trim_chr.c create mode 100644 libft_personal/tests/ft_string/ft_string/test_trimstr.c create mode 100644 libft_personal/tests/ft_string/string_tests.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_add.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_apply.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_at.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_cat.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_clear.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_convert_alloc_array.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_destroy.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_filter.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_from_array.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_from_size.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_get.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_map.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_new.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_pop.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_remove.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_remove_if.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_reserve.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_reverse.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_shift.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_shrink.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_sort.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_swap.c create mode 100644 libft_personal/tests/ft_vector/tests_vec_to_array.c create mode 100644 libft_personal/tests/ft_vector/vector_tests.c create mode 100644 libft_personal/tests/lambdas_for_tests.c create mode 100644 libft_personal/tests/main_tests.c create mode 100644 libft_personal/tests/tests_utils.c diff --git a/Makefile b/Makefile index 83a8af3..2ccd3f2 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/31 17:19:01 by rparodi ### ########.fr # +# Updated: 2024/10/31 23:54:04 by bgoulard ### ########.fr # # # # **************************************************************************** # @@ -24,13 +24,19 @@ RM = rm -rf # Flags CFLAGS = -Werror -Wextra -Wall CFLAGS += -g3 -MMD -#-lm # CFLAGS += -fsanitize=address # CFLAGS += -fsanitize=thread INCLUDES = -I ./includes/ -I ./includes/libft/ +# Library flags + +LDFLAGS =\ + -L./build -lft -lft_personal + +# -lm \ + SRC = sources/main.c \ sources/error.c \ parsing/arguments.c @@ -52,10 +58,10 @@ END = \033[0m all: header $(NAME) footer # Bonus (make bonus) -bonus: header $(OBJ) $(LIB_OBJ) footer +bonus: header $(OBJ) footer @mkdir -p $(OBJDIRNAME) @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' - @cc $(CFLAGS) -D BONUS=1 -o $(NAME_BONUS) $(OBJ) $(LIB_OBJ) + @cc $(CFLAGS) -D BONUS=1 -o $(NAME_BONUS) $(OBJ) $(LDFLAGS) # Clean (make clean) clean: @@ -73,12 +79,17 @@ fclean: clean # Restart (make re) re: header fclean all +build/libft.a: + @make --no-print-directory -C ./libft +build/libft_personal.a: + @make --no-print-directory -C ./libft_personal + # Dependences for all -$(NAME): $(OBJ) +$(NAME): $(OBJ) build/libft.a build/libft_personal.a @make --no-print-directory -C ./libft lib @mkdir -p $(OBJDIRNAME) @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' - @cc $(CFLAGS) ./build/libft.a -o $(NAME) $(OBJ) + @cc $(CFLAGS) ./build/libft.a -o $(NAME) $(OBJ) $(LDFLAGS) # Creating the objects $(OBJDIRNAME)/%.o: %.c diff --git a/libft/.gitignore b/libft/.gitignore deleted file mode 100644 index d564185..0000000 --- a/libft/.gitignore +++ /dev/null @@ -1,53 +0,0 @@ -to_do* -# Prerequisites -*.d - -# Object files -*.o -*.ko -*.obj -*.elf - -# Linker output -*.ilk -*.map -*.exp - -# Precompiled Headers -*.gch -*.pch - -# Libraries -*.lib -*.a -*.la -*.lo - -# Shared objects (inc. Windows DLLs) -*.dll -*.so -*.so.* -*.dylib - -# Executables -*.exe -*.out -*.app -*.i*86 -*.x86_64 -*.hex - -# Debug files -*.dSYM/ -*.su -*.idb -*.pdb - -# Kernel Module Compile Results -*.mod* -*.cmd -.tmp_versions/ -modules.order -Module.symvers -Mkfile.old -dkms.conf diff --git a/libft/Makefile b/libft/Makefile index 9aa0a86..edcd242 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/31 18:17:13 by rparodi ### ########.fr # +# Updated: 2024/10/31 23:46:44 by bgoulard ### ########.fr # # # # **************************************************************************** # @@ -33,7 +33,7 @@ LDLIBS = -lft INCLUDES = ./includes/libft/ # Objects -OBJDIRNAME = ../build +OBJDIRNAME = ../build/rparodi OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o)) SRC = char/ft_isdigit.c \ diff --git a/libft_personal/Makefile b/libft_personal/Makefile new file mode 100644 index 0000000..5432fca --- /dev/null +++ b/libft_personal/Makefile @@ -0,0 +1,744 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: bgoulard +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/05 09:04:05 by bgoulard #+# #+# # +# Updated: 2024/10/31 23:53:29 by bgoulard ### ########.fr # +# # +# **************************************************************************** # + +# Colors +GRAY = "\\e[90m" +GREEN = "\\e[42m" +RED = "\\e[41m" +RESET = "\\e[0m" +BOLD = "\\e[1m" + +# Commands +CC = clang +NAME = ft_personal +OUTDIR = ../build + +TEST_NAME = tests_run +AR = ar +COV = llvm-cov +PRD = llvm-profdata +ECHO = $(shell which echo) -e +PRINTF = $(shell which printf) + +# Directories + +SRC_DIR = src +BUILD_DIR = ../build/bgoulard +TESTS_DIR = tests +INC_DIR = include +COVERAGE_DIR = coverage + +FT_MAP_DIR = ft_map +FT_LIST_DIR = ft_list +FT_STRING_DIR = ft_string +FT_VEC_DIR = ft_vector +FT_OPTIONAL_DIR = ft_optional +FT_ARGS_DIR = ft_args +FT_MATH_DIR = ft_math +FT_PAIR_DIR = ft_pair + +# Counpound directories + +FT_LIST_LL_DIR = $(FT_LIST_DIR)/ft_ll +FT_LIST_DL_DIR = $(FT_LIST_DIR)/ft_dl +FT_CHR_DIR = $(FT_STRING_DIR)/ft_chr +FT_MEM_DIR = $(FT_STRING_DIR)/ft_mem +FT_STR_DIR = $(FT_STRING_DIR)/ft_str +FT_T_STRING_DIR = $(FT_STRING_DIR)/ft_string + +# Compilation flags +## +## To change debug level run make DEBUG_LEVEL=xxx +## + +LDFLAGS = +CPPFLAGS = -I$(INC_DIR) -MMD -MP +FFLAGS =\ + -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined \ + -fsanitize=leak -fsanitize=pointer-compare \ + -fsanitize=pointer-subtract \ + -fsanitize-address-use-after-scope -fsanitize=pointer-overflow +CFLAGS =\ + -Wall -Wextra $(CPPFLAGS) -Werror -fPIC -fdiagnostics-color +TEST_FLAGS =\ + -g2 -DTEST \ + -fprofile-instr-generate -ftest-coverage -fcoverage-mapping \ + +DEBUG_LEVEL =\ + 0 +DEBUG_FLAGS =\ + -g2 -DDEBUG $(FFLAGS) -DDEBUG_LEVEL=$(DEBUG_LEVEL) + +# Inner variables + +MAX_FILE_LEN = 0 +TARGET ?= "ALL" +CLOG_FILE = ./compilation.log + +# Check for llvm-cov and llvm-profdata +# If not found, use the version 12 if available +# If not found, use the version 14 (latest version) +ifeq (, $(shell which $(COV) 2> /dev/null)) + COV = llvm-cov-12 + ifeq (, $(shell which $(COV) 2> /dev/null)) + COV = llvm-cov-14 + endif +endif + +ifeq (, $(shell which $(PRD) 2> /dev/null)) + PRD = llvm-profdata-12 + ifeq (, $(shell which $(PRD) 2> /dev/null)) + PRD = llvm-profdata-14 + endif +endif + +# Sources + +FT_PAIR_SRC = \ + $(FT_PAIR_DIR)/ft_pair_cmp.c \ + $(FT_PAIR_DIR)/ft_pair_destroy.c \ + $(FT_PAIR_DIR)/ft_pair_get.c \ + $(FT_PAIR_DIR)/ft_pair_new.c \ + $(FT_PAIR_DIR)/ft_pair_set.c \ + +FT_MATH_SRC = \ + $(FT_MATH_DIR)/ft_clamp.c \ + $(FT_MATH_DIR)/ft_complex.c \ + $(FT_MATH_DIR)/ft_intrange.c \ + $(FT_MATH_DIR)/ft_log.c \ + $(FT_MATH_DIR)/ft_minmax.c \ + $(FT_MATH_DIR)/ft_sqrt.c \ + $(FT_MATH_DIR)/ft_pow.c \ + $(FT_MATH_DIR)/ft_abs.c \ + $(FT_MATH_DIR)/ft_align.c \ + $(FT_MATH_DIR)/ft_round.c + +FT_MAP_SRC = \ + $(FT_MAP_DIR)/ft_map_clear.c \ + $(FT_MAP_DIR)/ft_map_create.c \ + $(FT_MAP_DIR)/ft_map_destroy.c \ + $(FT_MAP_DIR)/ft_map_get.c \ + $(FT_MAP_DIR)/ft_map_hash.c \ + $(FT_MAP_DIR)/ft_map_remove.c \ + $(FT_MAP_DIR)/ft_map_set.c + +FT_LIST_LL_SRC = \ + $(FT_LIST_LL_DIR)/ft_ll_find.c \ + $(FT_LIST_LL_DIR)/ft_ll_add.c \ + $(FT_LIST_LL_DIR)/ft_ll_clear.c \ + $(FT_LIST_LL_DIR)/ft_ll_delete.c \ + $(FT_LIST_LL_DIR)/ft_ll_apply.c \ + $(FT_LIST_LL_DIR)/ft_ll_iterator.c \ + $(FT_LIST_LL_DIR)/ft_ll_map.c \ + $(FT_LIST_LL_DIR)/ft_ll_new.c \ + $(FT_LIST_LL_DIR)/ft_ll_rev.c \ + $(FT_LIST_LL_DIR)/ft_ll_size.c \ + $(FT_LIST_LL_DIR)/ft_ll_create.c \ + $(FT_LIST_LL_DIR)/ft_ll_getters.c \ + $(FT_LIST_LL_DIR)/ft_ll_pushpop.c \ + $(FT_LIST_LL_DIR)/ft_ll_sub.c \ + +FT_LIST_DL_SRC = \ + $(FT_LIST_DL_DIR)/ft_dl_apply.c \ + $(FT_LIST_DL_DIR)/ft_dl_clear.c \ + $(FT_LIST_DL_DIR)/ft_dl_create.c \ + $(FT_LIST_DL_DIR)/ft_dl_delete.c \ + $(FT_LIST_DL_DIR)/ft_dl_getters.c \ + $(FT_LIST_DL_DIR)/ft_dl_iterator.c \ + $(FT_LIST_DL_DIR)/ft_dl_pushpop.c \ + $(FT_LIST_DL_DIR)/ft_dl_size.c \ + $(FT_LIST_DL_DIR)/ft_dl_sub.c \ + $(FT_LIST_DL_DIR)/ft_dl_add.c \ + $(FT_LIST_DL_DIR)/ft_dl_rev.c \ + $(FT_LIST_DL_DIR)/ft_dl_map.c \ + $(FT_LIST_DL_DIR)/ft_dl_new.c \ + $(FT_LIST_DL_DIR)/ft_dl_find.c \ + +FT_STR_SRC = \ + $(FT_STR_DIR)/ft_atof.c \ + $(FT_STR_DIR)/ft_atoi.c \ + $(FT_STR_DIR)/ft_atoi_base.c \ + $(FT_STR_DIR)/ft_itoa.c \ + $(FT_STR_DIR)/ft_itoa_base.c \ + $(FT_STR_DIR)/ft_perror.c \ + $(FT_STR_DIR)/ft_putendl_fd.c \ + $(FT_STR_DIR)/ft_putnbr_fd.c \ + $(FT_STR_DIR)/ft_putstr_fd.c \ + $(FT_STR_DIR)/ft_shift_args.c \ + $(FT_STR_DIR)/ft_split.c \ + $(FT_STR_DIR)/ft_splits.c \ + $(FT_STR_DIR)/ft_str_isalpha.c \ + $(FT_STR_DIR)/ft_str_isalnum.c \ + $(FT_STR_DIR)/ft_str_isbool.c \ + $(FT_STR_DIR)/ft_str_isdigit.c \ + $(FT_STR_DIR)/ft_str_isfloat.c \ + $(FT_STR_DIR)/ft_str_ishex.c \ + $(FT_STR_DIR)/ft_str_isdouble.c \ + $(FT_STR_DIR)/ft_str_isint.c \ + $(FT_STR_DIR)/ft_str_islong.c \ + $(FT_STR_DIR)/ft_str_isnum.c \ + $(FT_STR_DIR)/ft_str_isoct.c \ + $(FT_STR_DIR)/ft_str_isvalid.c \ + $(FT_STR_DIR)/ft_str_replace.c \ + $(FT_STR_DIR)/ft_strappend_c.c \ + $(FT_STR_DIR)/ft_strchr.c \ + $(FT_STR_DIR)/ft_strclen.c \ + $(FT_STR_DIR)/ft_strcmp.c \ + $(FT_STR_DIR)/ft_strcnb.c \ + $(FT_STR_DIR)/ft_strcspn.c \ + $(FT_STR_DIR)/ft_strdup.c \ + $(FT_STR_DIR)/ft_strend_with.c \ + $(FT_STR_DIR)/ft_strerror.c \ + $(FT_STR_DIR)/ft_striteri.c \ + $(FT_STR_DIR)/ft_strjoin.c \ + $(FT_STR_DIR)/ft_strlcat.c \ + $(FT_STR_DIR)/ft_strlcpy.c \ + $(FT_STR_DIR)/ft_strlen.c \ + $(FT_STR_DIR)/ft_strmapi.c \ + $(FT_STR_DIR)/ft_strncmp.c \ + $(FT_STR_DIR)/ft_strndup.c \ + $(FT_STR_DIR)/ft_strnstr.c \ + $(FT_STR_DIR)/ft_strrchr.c \ + $(FT_STR_DIR)/ft_strspn.c \ + $(FT_STR_DIR)/ft_strstart_with.c \ + $(FT_STR_DIR)/ft_strtok.c \ + $(FT_STR_DIR)/ft_strtrim.c \ + $(FT_STR_DIR)/ft_substr.c \ + $(FT_STR_DIR)/ft_utoa.c \ + $(FT_STR_DIR)/get_next_line.c \ + +FT_T_STRING_SRC = \ + $(FT_T_STRING_DIR)/ft_string_append.c \ + $(FT_T_STRING_DIR)/ft_string_new.c \ + $(FT_T_STRING_DIR)/ft_string_put.c \ + $(FT_T_STRING_DIR)/ft_string_from.c \ + $(FT_T_STRING_DIR)/ft_string_clear.c \ + $(FT_T_STRING_DIR)/ft_string_destroy.c \ + $(FT_T_STRING_DIR)/ft_string_insert.c \ + $(FT_T_STRING_DIR)/ft_string_reserve.c \ + $(FT_T_STRING_DIR)/ft_string_resize.c \ + $(FT_T_STRING_DIR)/ft_string_shrink.c \ + $(FT_T_STRING_DIR)/ft_string_substr.c \ + $(FT_T_STRING_DIR)/ft_string_to_str.c \ + $(FT_T_STRING_DIR)/ft_string_trim.c \ + $(FT_T_STRING_DIR)/ft_string_cmp.c \ + $(FT_T_STRING_DIR)/ft_string_get.c \ + $(FT_T_STRING_DIR)/ft_string_chr.c \ + $(FT_T_STRING_DIR)/ft_string_replace.c \ + $(FT_T_STRING_DIR)/ft_string_set.c + +FT_MEM_SRC = \ + $(FT_MEM_DIR)/ft_apply_2d.c \ + $(FT_MEM_DIR)/ft_bzero.c \ + $(FT_MEM_DIR)/ft_calloc.c \ + $(FT_MEM_DIR)/ft_fd_to_buff.c \ + $(FT_MEM_DIR)/ft_free.c \ + $(FT_MEM_DIR)/ft_free_2d.c \ + $(FT_MEM_DIR)/ft_len_2d.c \ + $(FT_MEM_DIR)/ft_malloc.c \ + $(FT_MEM_DIR)/ft_memchr.c \ + $(FT_MEM_DIR)/ft_memcmp.c \ + $(FT_MEM_DIR)/ft_memcpy.c \ + $(FT_MEM_DIR)/ft_memmap.c \ + $(FT_MEM_DIR)/ft_memmove.c \ + $(FT_MEM_DIR)/ft_memset.c \ + $(FT_MEM_DIR)/ft_qsort.c \ + $(FT_MEM_DIR)/ft_realloc.c \ + $(FT_MEM_DIR)/ft_swap.c + +FT_CHR_SRC = \ + $(FT_CHR_DIR)/ft_isalnum.c \ + $(FT_CHR_DIR)/ft_isalpha.c \ + $(FT_CHR_DIR)/ft_isascii.c \ + $(FT_CHR_DIR)/ft_isdigit.c \ + $(FT_CHR_DIR)/ft_ishexdigit.c \ + $(FT_CHR_DIR)/ft_islower.c \ + $(FT_CHR_DIR)/ft_isoctdigit.c \ + $(FT_CHR_DIR)/ft_isprint.c \ + $(FT_CHR_DIR)/ft_isspace.c \ + $(FT_CHR_DIR)/ft_isupper.c \ + $(FT_CHR_DIR)/ft_putchar_fd.c \ + $(FT_CHR_DIR)/ft_tolower.c \ + $(FT_CHR_DIR)/ft_toupper.c + +FT_VEC_SRC = \ + $(FT_VEC_DIR)/ft_vec_add.c \ + $(FT_VEC_DIR)/ft_vec_apply.c \ + $(FT_VEC_DIR)/ft_vec_at.c \ + $(FT_VEC_DIR)/ft_vec_cat.c \ + $(FT_VEC_DIR)/ft_vec_clear.c \ + $(FT_VEC_DIR)/ft_vec_destroy.c \ + $(FT_VEC_DIR)/ft_vec_filter.c \ + $(FT_VEC_DIR)/ft_vec_get.c \ + $(FT_VEC_DIR)/ft_vec_map.c \ + $(FT_VEC_DIR)/ft_vec_new.c \ + $(FT_VEC_DIR)/ft_vec_pop.c \ + $(FT_VEC_DIR)/ft_vec_remove.c \ + $(FT_VEC_DIR)/ft_vec_reserve.c \ + $(FT_VEC_DIR)/ft_vec_reverse.c \ + $(FT_VEC_DIR)/ft_vec_shift.c \ + $(FT_VEC_DIR)/ft_vec_shrink.c \ + $(FT_VEC_DIR)/ft_vec_sort.c \ + $(FT_VEC_DIR)/ft_vec_swap.c \ + $(FT_VEC_DIR)/ft_vec_to_array.c + +FT_OPTIONAL_SRC = \ + $(FT_OPTIONAL_DIR)/ft_optional_chain.c \ + $(FT_OPTIONAL_DIR)/ft_optional_copy.c \ + $(FT_OPTIONAL_DIR)/ft_optional_destroy.c \ + $(FT_OPTIONAL_DIR)/ft_optional_new.c \ + $(FT_OPTIONAL_DIR)/ft_optional_unwrap.c + +FT_ARGS_SRC = \ + $(FT_ARGS_DIR)/ft_arg_custom_checker.c \ + $(FT_ARGS_DIR)/ft_parse_args.c \ + $(FT_ARGS_DIR)/ft_parse_err.c \ + $(FT_ARGS_DIR)/ft_parse_opt.c \ + $(FT_ARGS_DIR)/ft_progname.c \ + $(FT_ARGS_DIR)/ft_set_opt_args.c \ + $(FT_ARGS_DIR)/ft_setup_prog.c \ + $(FT_ARGS_DIR)/ft_version.c + +# Counpound sources + +FT_LIST_SRC = \ + $(FT_LIST_LL_SRC) \ + $(FT_LIST_DL_SRC) \ + +FT_STRING_SRC = \ + $(FT_CHR_SRC) \ + $(FT_MEM_SRC) \ + $(FT_STR_SRC) \ + $(FT_T_STRING_SRC) + +# Tests sources + +TESTS_SRC =\ + $(TESTS_DIR)/ft_args/tests_custom_checker.c \ + $(TESTS_DIR)/ft_args/tests_optlist.c \ + $(TESTS_DIR)/ft_args/tests_progname.c \ + $(TESTS_DIR)/ft_args/tests_version.c \ + $(TESTS_DIR)/ft_args/tests_setup_prog.c \ + $(TESTS_DIR)/ft_args/args_tests.c \ + \ + $(TESTS_DIR)/ft_list/ll_tests/ll_tests_utils.c \ + $(TESTS_DIR)/ft_list/ll_tests/ll_list_tests.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_push.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_new.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_map.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_rev.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_sizers.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_apply.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_iterators.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_clear.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_copy.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_create.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_deletors.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_find.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_get.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_subrange.c \ + $(TESTS_DIR)/ft_list/ll_tests/tests_list_add.c \ + $(TESTS_DIR)/ft_list/dl_tests/dl_tests_utils.c \ + $(TESTS_DIR)/ft_list/dl_tests/dl_list_tests.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_add.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_clear.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_copy.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_create.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_delete.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_iterators.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_get.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_subrange.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_map.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_new.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_push.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_rev.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_sizers.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_apply.c \ + $(TESTS_DIR)/ft_list/dl_tests/tests_dlist_find.c \ + \ + $(TESTS_DIR)/ft_map/map_tests.c \ + $(TESTS_DIR)/ft_map/tests_map_remove.c \ + $(TESTS_DIR)/ft_map/tests_map_hash.c \ + $(TESTS_DIR)/ft_map/tests_map_create.c \ + $(TESTS_DIR)/ft_map/tests_map_destroy.c \ + $(TESTS_DIR)/ft_map/tests_map_set_cmphash.c \ + $(TESTS_DIR)/ft_map/tests_map_get.c \ + $(TESTS_DIR)/ft_map/tests_map_size.c \ + $(TESTS_DIR)/ft_map/tests_map_cappacity.c \ + $(TESTS_DIR)/ft_map/tests_map_clear.c \ + $(TESTS_DIR)/ft_map/tests_map_set.c \ + \ + $(TESTS_DIR)/ft_math/tests_abs.c \ + $(TESTS_DIR)/ft_math/tests_align.c \ + $(TESTS_DIR)/ft_math/tests_clamp.c \ + $(TESTS_DIR)/ft_math/tests_complex.c \ + $(TESTS_DIR)/ft_math/tests_intrange.c \ + $(TESTS_DIR)/ft_math/tests_log.c \ + $(TESTS_DIR)/ft_math/tests_minmax.c \ + $(TESTS_DIR)/ft_math/tests_pow.c \ + $(TESTS_DIR)/ft_math/tests_sqrt.c \ + $(TESTS_DIR)/ft_math/tests_round.c \ + $(TESTS_DIR)/ft_math/math_tests.c \ + \ + $(TESTS_DIR)/ft_optional/tests_optional_chain.c \ + $(TESTS_DIR)/ft_optional/tests_optional_copy.c \ + $(TESTS_DIR)/ft_optional/tests_optional_destroy.c \ + $(TESTS_DIR)/ft_optional/tests_optional_dup.c \ + $(TESTS_DIR)/ft_optional/tests_optional_from_val.c \ + $(TESTS_DIR)/ft_optional/tests_optional_map.c \ + $(TESTS_DIR)/ft_optional/tests_optional_new.c \ + $(TESTS_DIR)/ft_optional/tests_optional_unwrap.c \ + $(TESTS_DIR)/ft_optional/optional_tests.c \ + \ + $(TESTS_DIR)/ft_pair/tests_pair_cmp.c \ + $(TESTS_DIR)/ft_pair/tests_pair_cmp_first.c \ + $(TESTS_DIR)/ft_pair/tests_pair_cmp_second.c \ + $(TESTS_DIR)/ft_pair/tests_pair_destroy.c \ + $(TESTS_DIR)/ft_pair/tests_pair_get_first.c \ + $(TESTS_DIR)/ft_pair/tests_pair_get_second.c \ + $(TESTS_DIR)/ft_pair/tests_pair_new.c \ + $(TESTS_DIR)/ft_pair/tests_pair_set.c \ + $(TESTS_DIR)/ft_pair/pair_tests.c \ + \ + $(TESTS_DIR)/ft_string/ft_char/tests_isalnum.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isalpha.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isascii.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isdigit.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_ishexdigit.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isoctdigit.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isspace.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_isprint.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_puchar.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_tolower.c \ + $(TESTS_DIR)/ft_string/ft_char/tests_toupper.c \ + $(TESTS_DIR)/ft_string/ft_char/ft_char_tests.c \ + \ + $(TESTS_DIR)/ft_string/ft_mem/tests_apply_2d.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_bzero.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_calloc.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_fd_to_buff.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_free.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_free_2d.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_len_2d.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memchr.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memcmp.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memcpy.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memmap.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memmove.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_memset.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_qsort.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_realloc.c \ + $(TESTS_DIR)/ft_string/ft_mem/tests_swap.c \ + $(TESTS_DIR)/ft_string/ft_mem/mem_tests.c \ + \ + $(TESTS_DIR)/ft_string/ft_str/test_atoi_base.c \ + $(TESTS_DIR)/ft_string/ft_str/test_atoi.c \ + $(TESTS_DIR)/ft_string/ft_str/test_atof.c \ + $(TESTS_DIR)/ft_string/ft_str/test_gnl.c \ + $(TESTS_DIR)/ft_string/ft_str/test_itoa_base.c \ + $(TESTS_DIR)/ft_string/ft_str/test_itoa.c \ + $(TESTS_DIR)/ft_string/ft_str/test_putendl.c \ + $(TESTS_DIR)/ft_string/ft_str/test_putnbr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_putstr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_shift_args.c \ + $(TESTS_DIR)/ft_string/ft_str/test_split.c \ + $(TESTS_DIR)/ft_string/ft_str/test_splits.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isalpha.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isalnum.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isdouble.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isdigit.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isbool.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isfloat.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_ishex.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isint.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_islong.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isnum.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isoct.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_isvalid.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strchr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strclen.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strcmp.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strcnb.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strcspn.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strdup.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strend_with.c \ + $(TESTS_DIR)/ft_string/ft_str/test_striteri.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strjoin.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strlcat.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strlcpy.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strlen.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strmapi.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strncmp.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strndup.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strnstr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strrchr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strspn.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strstart_with.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_replace.c \ + $(TESTS_DIR)/ft_string/ft_str/test_str_replace_chr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strappend_c.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strtok.c \ + $(TESTS_DIR)/ft_string/ft_str/test_strtrim.c \ + $(TESTS_DIR)/ft_string/ft_str/test_substr.c \ + $(TESTS_DIR)/ft_string/ft_str/test_utoa.c \ + $(TESTS_DIR)/ft_string/ft_str/str_tests.c \ + $(TESTS_DIR)/ft_string/string_tests.c \ + \ + $(TESTS_DIR)/ft_string/ft_string/test_append.c \ + $(TESTS_DIR)/ft_string/ft_string/test_append_c.c \ + $(TESTS_DIR)/ft_string/ft_string/test_append_n.c \ + $(TESTS_DIR)/ft_string/ft_string/test_append_s.c \ + $(TESTS_DIR)/ft_string/ft_string/test_append_sn.c \ + $(TESTS_DIR)/ft_string/ft_string/test_cap.c \ + $(TESTS_DIR)/ft_string/ft_string/test_chr.c \ + $(TESTS_DIR)/ft_string/ft_string/test_clear.c \ + $(TESTS_DIR)/ft_string/ft_string/test_cmp.c \ + $(TESTS_DIR)/ft_string/ft_string/test_cmp_str.c \ + $(TESTS_DIR)/ft_string/ft_string/test_destroy.c \ + $(TESTS_DIR)/ft_string/ft_string/test_from.c \ + $(TESTS_DIR)/ft_string/ft_string/test_from_c.c \ + $(TESTS_DIR)/ft_string/ft_string/test_from_n.c \ + $(TESTS_DIR)/ft_string/ft_string/test_from_s.c \ + $(TESTS_DIR)/ft_string/ft_string/test_from_sn.c \ + $(TESTS_DIR)/ft_string/ft_string/test_get.c \ + $(TESTS_DIR)/ft_string/ft_string/test_insert.c \ + $(TESTS_DIR)/ft_string/ft_string/test_insert_c.c \ + $(TESTS_DIR)/ft_string/ft_string/test_insert_n.c \ + $(TESTS_DIR)/ft_string/ft_string/test_insert_s.c \ + $(TESTS_DIR)/ft_string/ft_string/test_insert_sn.c \ + $(TESTS_DIR)/ft_string/ft_string/test_len.c \ + $(TESTS_DIR)/ft_string/ft_string/test_ncmp.c \ + $(TESTS_DIR)/ft_string/ft_string/test_ncmp_str.c \ + $(TESTS_DIR)/ft_string/ft_string/test_new.c \ + $(TESTS_DIR)/ft_string/ft_string/test_offset.c \ + $(TESTS_DIR)/ft_string/ft_string/test_put.c \ + $(TESTS_DIR)/ft_string/ft_string/test_rchr.c \ + $(TESTS_DIR)/ft_string/ft_string/test_replace.c \ + $(TESTS_DIR)/ft_string/ft_string/test_replace_chr.c \ + $(TESTS_DIR)/ft_string/ft_string/test_reserve.c \ + $(TESTS_DIR)/ft_string/ft_string/test_resize.c \ + $(TESTS_DIR)/ft_string/ft_string/test_roffset.c \ + $(TESTS_DIR)/ft_string/ft_string/test_set.c \ + $(TESTS_DIR)/ft_string/ft_string/test_set_inplace.c \ + $(TESTS_DIR)/ft_string/ft_string/test_set_n.c \ + $(TESTS_DIR)/ft_string/ft_string/test_shrink.c \ + $(TESTS_DIR)/ft_string/ft_string/test_substr.c \ + $(TESTS_DIR)/ft_string/ft_string/test_to_str.c \ + $(TESTS_DIR)/ft_string/ft_string/test_trim.c \ + $(TESTS_DIR)/ft_string/ft_string/test_trim_chr.c \ + $(TESTS_DIR)/ft_string/ft_string/test_trimstr.c \ + $(TESTS_DIR)/ft_string/ft_string/t_string_tests.c \ + \ + $(TESTS_DIR)/ft_vector/tests_vec_add.c \ + $(TESTS_DIR)/ft_vector/tests_vec_apply.c \ + $(TESTS_DIR)/ft_vector/tests_vec_at.c \ + $(TESTS_DIR)/ft_vector/tests_vec_cat.c \ + $(TESTS_DIR)/ft_vector/tests_vec_clear.c \ + $(TESTS_DIR)/ft_vector/tests_vec_convert_alloc_array.c \ + $(TESTS_DIR)/ft_vector/tests_vec_destroy.c \ + $(TESTS_DIR)/ft_vector/tests_vec_filter.c \ + $(TESTS_DIR)/ft_vector/tests_vec_from_array.c \ + $(TESTS_DIR)/ft_vector/tests_vec_from_size.c \ + $(TESTS_DIR)/ft_vector/tests_vec_get.c \ + $(TESTS_DIR)/ft_vector/tests_vec_map.c \ + $(TESTS_DIR)/ft_vector/tests_vec_new.c \ + $(TESTS_DIR)/ft_vector/tests_vec_pop.c \ + $(TESTS_DIR)/ft_vector/tests_vec_remove.c \ + $(TESTS_DIR)/ft_vector/tests_vec_remove_if.c \ + $(TESTS_DIR)/ft_vector/tests_vec_reserve.c \ + $(TESTS_DIR)/ft_vector/tests_vec_reverse.c \ + $(TESTS_DIR)/ft_vector/tests_vec_shift.c \ + $(TESTS_DIR)/ft_vector/tests_vec_shrink.c \ + $(TESTS_DIR)/ft_vector/tests_vec_sort.c \ + $(TESTS_DIR)/ft_vector/tests_vec_swap.c \ + $(TESTS_DIR)/ft_vector/tests_vec_to_array.c \ + $(TESTS_DIR)/ft_vector/vector_tests.c \ + \ + $(TESTS_DIR)/main_tests.c \ + $(TESTS_DIR)/lambdas_for_tests.c \ + $(TESTS_DIR)/tests_utils.c + +# Inner variables for targets + +STABLE = \ + $(FT_MATH_SRC) \ + $(FT_LIST_SRC) \ + $(FT_VEC_SRC) \ + $(FT_STRING_SRC) \ + $(FT_MAP_SRC) \ + $(FT_OPTIONAL_SRC) \ + $(FT_ARGS_SRC) \ + $(FT_PAIR_SRC) \ + +UNSTABLE = \ + +INNER_SRC = \ + $(STABLE) + +# Check if user wants to compile unstable sources +# to compile unstable sources run make with TARGET=UNSTABLE +ifeq (UNSTABLE, $(findstring UNSTABLE, $(TARGET))) +INNER_SRC += \ + $(UNSTABLE) +endif +# Check if user wants to compile all sources +# to compile all sources run make with TARGET=ALL +ifeq (ALL, $(findstring ALL, $(TARGET))) +INNER_SRC = \ + $(STABLE) \ + $(UNSTABLE) +endif + +# Objects creation + +# add prefix to sources to specify the directory src/ +SRCS = $(addprefix $(SRC_DIR)/, $(INNER_SRC)) + +# add prefix to sources to specify the directory build/ for objects +OBJ = $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/,$(INNER_SRC))) + +# add prefix to sources to specify the directory build/tests/ for test objects +TOBJ = $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/$(TESTS_DIR)/,$(INNER_SRC))) +TOBJ += $(patsubst %.c, %.o, $(addprefix $(BUILD_DIR)/,$(TESTS_SRC))) + +# Inner variables for rules + +# Get the max length of the sources names to align the output +MAX_FILE_LEN = $(shell $(PRINTF) "%s\n" $(SRCS) | \ + awk '{print length}' | sort -n | tail -1) + +# Rules + +# Default rule for objects imported from src/ +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + @$(ECHO) -n $(GRAY) "building from " + @$(PRINTF) "%*s ... "$(RESET) $(MAX_FILE_LEN) $< + @mkdir -p $(dir $@) + @( $(CC) $(CFLAGS) -c $< -o $@ 2>> $(CLOG_FILE) && \ + $(ECHO) $(GREEN) "Success" $(RESET) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) \ + $(BOLD) "see:" $(CLOG_FILE) $(RESET) + +# Rule for tests objects imported from src/ +$(BUILD_DIR)/$(TESTS_DIR)/%.o: $(SRC_DIR)/%.c + @$(ECHO) -n $(GRAY) "building from " $< "..." $(RESET) + @mkdir -p $(dir $@) + @( $(CC) $(CFLAGS) $(TEST_FLAGS) -c $< -o $@ \ + 2>> $(CLOG_FILE) && \ + $(ECHO) $(GREEN) "Success" $(RESET) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) \ + $(BOLD) "see:" $(CLOG_FILE) $(RESET) + +# Rule for tests objects imported from tests/ +$(BUILD_DIR)/$(TESTS_DIR)/%.o: $(TESTS_DIR)/%.c + @$(ECHO) -n $(GRAY) "building from " $< "..." $(RESET) + @mkdir -p $(dir $@) + @( $(CC) $(CFLAGS) $(TEST_FLAGS) -c $< -o $@ \ + 2>> $(CLOG_FILE) && \ + $(ECHO) $(GREEN) "Success" $(RESET) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) \ + $(BOLD) "see:" $(CLOG_FILE) $(RESET) + +# Default rule +all: $(OUTDIR)/lib$(NAME).a + +tmp: + @echo $(SRCS) + +so: $(OUTDIR)/lib$(NAME).so + +# Rule for shared library +$(OUTDIR)/lib$(NAME).so: $(OBJ) + @$(ECHO) -n $(GRAY) "Making ... " $(RESET) $(BOLD) \ + "$(OUTDIR)/lib$(NAME).so" $(RESET) $(GRAY) " ... " $(RESET) + @( $(CC) -shared -o $(OUTDIR)/lib$(NAME).so $(OBJ) 2> /dev/null && \ + $(ECHO) $(GREEN) "Success" $(RESET) && $(RM) $(CLOG_FILE) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) "see:" $(CLOG_FILE) + +# Rule for static library +$(OUTDIR)/lib$(NAME).a: $(OBJ) + @$(ECHO) -n $(GRAY) "Making ... " $(RESET) $(BOLD) \ + "$(OUTDIR)/lib$(NAME).a" $(RESET) $(GRAY) " ... " $(RESET) + @( $(AR) -rcs $(OUTDIR)/lib$(NAME).a $(OBJ) 2> /dev/null && \ + $(ECHO) $(GREEN) "Success" $(RESET) && $(RM) $(CLOG_FILE) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) "see:" $(CLOG_FILE) + +# Rule to compile and run tests +$(TEST_NAME): $(TOBJ) + @$(ECHO) -n $(GRAY) "Compiling tests ... " $(RESET) + @$(CC) $(CFLAGS) $(TOBJ) -o $(TEST_NAME) $(TEST_FLAGS) \ + $(LDFLAGS) -lgcov \ + 2>> $(CLOG_FILE) && \ + $(ECHO) $(GREEN) "Success" $(RESET) || \ + $(ECHO) $(RED) "Failed" $(RESET) + @$(ECHO) -n $(GRAY) "Running tests ... " $(RESET) && \ + ./$(TEST_NAME) && \ + $(ECHO) $(GREEN) "Success" $(RESET) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +# Rule to generate coverage using llvm +$(COVERAGE_DIR): $(TEST_NAME) + @$(ECHO) -n $(GRAY) "Generating profraw ... " $(RESET) && \ + ./$(TEST_NAME) && \ + $(ECHO) -n $(GRAY) " profdata ... " $(RESET) && \ + $(PRD) merge -sparse default.profraw -o \ + $(TEST_NAME).profdata && \ + $(ECHO) -n $(GRAY) "coverage in html ... " \ + $(RESET) && \ + $(COV) show -format=html \ + -instr-profile=$(TEST_NAME).profdata \ + -ignore-filename-regex=$(TESTS_DIR)/* \ + --show-branches=count \ + ./$(TEST_NAME) -output-dir=$(COVERAGE_DIR) > /dev/null && \ + $(RM) *.profraw *.profdata && \ + $(ECHO) $(GREEN) "Success" $(RESET) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +# Rule to compile using debug flags +debug: + @$(ECHO) $(GRAY) "Compiling debug, flags are" $(RESET) \ + "$(CFLAGS) $(DEBUG_FLAGS)" $(RESET) && \ + make --no-print-directory -C . re \ + CFLAGS="$(CFLAGS) $(DEBUG_FLAGS)" && \ + make --no-print-directory -C . \ + so CFLAGS="$(CFLAGS) $(DEBUG_FLAGS)" && \ + $(ECHO) $(GREEN) "Success" $(RESET) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +# Rule to clean objects +clean: + @$(ECHO) -n $(GRAY) "Clean ... " $(RESET) && \ + ( $(RM) -rf $(BUILD_DIR) $(CLOG_FILE) $(TEST_NAME) *.gcov \ + *.gcno *.gcda 2> /dev/null && \ + $(ECHO) $(GREEN) "Success" $(RESET) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +# Rule to clean objects and libraries/compiled files +fclean: clean + @$(ECHO) -n $(GRAY) "FClean ... " $(RESET) && \ + ( $(RM) -rf $(OUTDIR)/lib$(NAME).a l$(OUTDIR)/ib$(NAME).so \ + $(COVERAGE_DIR) $(TEST_NAME) \ + *.profraw *.profdata Doxygen 2> /dev/null && \ + $(ECHO) $(GREEN) "Success" $(RESET) ) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +# Rule to recompile +re: fclean + @$(ECHO) -ne $(GRAY) "Recompiling ..." $(RESET) "\n" && \ + make --no-print-directory all && \ + $(ECHO) $(GREEN) "Success" $(RESET) || \ + $(ECHO) $(RED) "Failed" $(RESET) + +-include $(OBJ:.o=.d) + +# rule to force rules to be executed even if files exist +.PHONY: re fclean clean diff --git a/libft_personal/include/ft_args.h b/libft_personal/include/ft_args.h new file mode 100644 index 0000000..209d6cc --- /dev/null +++ b/libft_personal/include/ft_args.h @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_args.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/13 23:42:28 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:01:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_ARGS_H +# define FT_ARGS_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_ARGS */ +/* Prefix: ft_arg */ +/* */ +/* The Module FT_ARGS provides an easy way to handle task related to the cli */ +/* arguments both for manipulation and parsing. */ +/* */ +/* ************************************************************************** */ + +// Change version with -DVERSION="x.y.z-W" via Makefile +# ifndef VERSION +# define VERSION "1.0.0" +# endif + +# include "ft_defs.h" +# include +# include "ft_args_types.h" + +/* @file: src/ft_args/ft_arg_custom_checker.c */ +void ft_arg_set_custom_checker(t_data_is custom_checker); +t_data_is ft_arg_get_custom_checker(void); + +/* @file: src/ft_args/ft_setup_prog.c */ +void ft_setup_prog(const char *const *av); + +/// @file: src/ft_args/ft_parse_args +int ft_parse_args(const char **argv, void *usr_control_struct); + +void ft_set_opt_list(const t_opt *opt_list); +const t_opt *ft_get_opt_list(void); + +void ft_set_progname(const char *program_name); +const char *ft_progname(void); + +void ft_set_version(const char *version); +const char *ft_progversion(void); + +#endif diff --git a/libft_personal/include/ft_args_types.h b/libft_personal/include/ft_args_types.h new file mode 100644 index 0000000..9c19468 --- /dev/null +++ b/libft_personal/include/ft_args_types.h @@ -0,0 +1,146 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_args_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/13 23:42:58 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:25:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_ARGS_TYPES_H +# define FT_ARGS_TYPES_H + +# define ARG_MASK_ATYPE ~3 +# define ARG_MASK_ANY_ARG 0x1 +/* + OPT_ARG = 1,// technically the inverse of OPT_NOARG so mask it + OPT_EQSIGN = 2,// if it has an arg and not eqsign, + it will be the next arg aka space separated + OPT_OTHER = 4,// custom type, see set_custom_checker +* +*/ +/// @brief Enum to define the type of the option +/// @details The type of the option is defined by the flags that are set in the +/// t_opt structure. The flags are defined in the enum e_opt_type. +/// The flags are defined as follows: +/// - OPT_ARG: The option has an argument +/// - OPT_EQSIGN: The option has an argument and the argument is separated by an +/// equal sign '=' (e.g. --option=arg). If the flag is not set, the argument is +/// considered to be in the next argument (e.g. --option arg). +/// - OPT_OTHER: The argument of the option is of another custom type (e.g. a +/// structure or an array or file with a custom extension etc.) +/// - OPT_INT: The argument of the option is an integer +/// - OPT_STRING: The argument of the option is a string +/// - OPT_BOOL: The argument of the option is a boolean (0 | 1 | true | false) +/// - OPT_FLOAT: The argument of the option is a float +/// - OPT_LONG: The argument of the option is a long +/// - OPT_DOUBLE: The argument of the option is a double +/// - OPT_ALPHANUM: The argument of the option is an alphanumeric string +/// - OPT_HEX: The argument of the option is a hexadecimal number +/// - OPT_OCT: The argument of the option is an octal number +/// @details The flags can be combined with the bitwise OR operator '|'. +/// @details The flags OPT_INT, OPT_STRING, OPT_BOOL, OPT_FLOAT, OPT_LONG, +/// OPT_DOUBLE, OPT_ALPHANUM, OPT_HEX, OPT_OCT are the standard types that can +/// be used to define the type of the argument of the option. The flag OPT_OTHER +/// is a custom type that can be used to define the type of the argument of the +/// option. The custom type can be defined by setting a custom checker with the +/// function set_custom_checker. The custom checker will be called to check the +/// argument of the option. The custom checker should return 0 if the argument +/// is valid and -1 if the argument is invalid. The custom checker should be +/// defined as follows: +/// @code +/// int custom_checker(char *arg) +/// { +/// // check the argument +/// return (0 or -1); +/// } +/// @endcode +/// @see: set_custom_checker to set a custom checker for the argument of type +/// OPT_OTHER. +/// @warning: System limitations: Currently you cannot set an option which uses +/// more than one argument. You can set an option which uses a custom type +/// (OPT_OTHER) but the custom type should be defined as a single argument. +/// @see: t_opt +/// @see: set_custom_checker +/// +typedef enum e_opt_type +{ + OPT_ARG = 1, + OPT_EQSIGN = 2, + OPT_OTHER = 4, + OPT_INT = 8, + OPT_STRING = 12, + OPT_BOOL = 16, + OPT_FLOAT = 20, + OPT_LONG = 24, + OPT_DOUBLE = 28, + OPT_ALPHANUM = 32, + OPT_HEX = 36, + OPT_OCT = 40, +} t_opt_type; + +/// @brief Structure to define the options +/// @param opt_long_name: The long name of the option "name" (without the "--") +/// @param opt_short_name: The short name of the option 'n' (without the "-") +/// @param opt_type: The type of the option +/// @param opt_func: The function to call when the option is found casted as +/// void * and later casted depending on the flags +/// @details An example of how to use the structure: +/// @code +/// #include "ft_args.h" +/// #include "ft_args_types.h" +/// #include "ft_string.h" +/// #include +/// +/// typedef struct s_control { +/// int n; +/// char *name; +/// float f; +/// } t_control; +/// +/// void set_n(void *control_struct, char *arg) { +/// printf("set_n\n"); +/// ((t_control *)control_struct)->n = ft_atoi(arg); +/// } +/// void set_name(void *control_struct, char *arg) { +/// printf("set_name\n"); +/// ((t_control *)control_struct)->name = arg; +/// } +/// void set_f(void *control_struct, char *arg) { +/// printf("set_f\n"); +/// ((t_control *)control_struct)->f = ft_atof(arg); +/// } +/// int main(int argc, char **argv) { +/// t_control control; +/// t_opt opt_list[] = { +/// {"nbr", 'n', set_n, OPT_INT | OPT_EQSIGN}, +/// {"name", 'a', set_name, OPT_STRING | OPT_EQSIGN}, +/// {"float", 'f', set_f, OPT_FLOAT | OPT_EQSIGN}, +/// {NULL, 0, NULL, 0} +/// }; +/// +/// ft_bzero(&control, sizeof(t_control)); +/// ft_set_opt_list(opt_list); +/// ft_parse_args(argv, &control); +/// printf("n = %d\nname = %s\nf = %f\n", control.n, control.name, +/// control.f); +/// return (0); +/// } +/// @endcode +/// @details The previous code will parse the arguments and set the values of +/// the control structure according to the options found in the arguments. +/// @details the function set_n will be called when the option --nbr or -n is +/// found in the arguments, and will be casted as a void (*) (void *, char *). +/// +typedef struct s_opt +{ + char *long_name; + char short_name; + void *func; + t_opt_type type; +} t_opt; + +#endif diff --git a/libft_personal/include/ft_char.h b/libft_personal/include/ft_char.h new file mode 100644 index 0000000..28c72f2 --- /dev/null +++ b/libft_personal/include/ft_char.h @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_char.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 16:59:44 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:49:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_CHAR_H +# define FT_CHAR_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_CHAR */ +/* Prefix: ft_* */ +/* */ +/* The module ft_char - (a sub module of the module string) provides an easy */ +/* way to handle task or queries related purely to chars. It was */ +/* separated from module ft_string to lighten and clarify it. */ +/* */ +/* ************************************************************************** */ + +# include + +/// @file: src/ft_string/ft_char/ft_isoctdigit.c +int ft_isoctdigit(int c); + +/// @file: src/ft_string/ft_char/ft_ishexdigit.c +int ft_ishexdigit(int c); + +/// @brief check if the char is a space character +/// @param c char to check +/// @return 1 if the char is a space character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isspace.c +int ft_isspace(int c); + +/// @brief check if the char is a lower case character +/// @param c char to check +/// @return 1 if the char is a lower case character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_islower.c +int ft_islower(int c); + +/// @brief check if the char is an ascii character +/// @param c char to check +/// @return 1 if the char is an ascii character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isascii.c +int ft_isascii(int c); + +/// @brief check if the char is an upper case character +/// @param c char to check +/// @return 1 if the char is an upper case character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isupper.c +int ft_isupper(int c); + +/// @brief check if the char is an alphanumeric character +/// @param c char to check +/// @return 1 if the char is an alphanumeric character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isalnum.c +int ft_isalnum(int c); + +/// @brief check if the char is a digit +/// @param c char to check +/// @return 1 if the char is a digit, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isdigit.c +int ft_isdigit(int c); + +/// @brief check if the char is a letter +/// @param c char to check +/// @return 1 if the char is a letter, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isalpha.c +int ft_isalpha(int c); + +/// @brief pass a char to lower case +/// @param c char to pass to lower case +/// @return the char in lower case +/// @file: src/ft_string/ft_char/ft_tolower.c +int ft_tolower(int c); + +/// @brief check if the char is printable +/// @param c char to check +/// @return 1 if the char is printable, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isprint.c +int ft_isprint(int c); + +/// @brief print the char on the specified file descriptor +/// @param c char to print +/// @param fd file descriptor to print on +/// @return 1 if the char was printed, -1 otherwise +int ft_putchar_fd(char c, int fd); + +/// @brief pass a char to lower case +/// @param c char to pass to lower case +/// @return the char in lower case +/// @file: src/ft_string/ft_char/ft_toupper.c +int ft_toupper(int c); + +#endif /* FT_CHAR_H */ diff --git a/libft_personal/include/ft_colors.h b/libft_personal/include/ft_colors.h new file mode 100644 index 0000000..9dc7c19 --- /dev/null +++ b/libft_personal/include/ft_colors.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_colors.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/20 15:01:55 by bgoulard #+# #+# */ +/* Updated: 2024/08/20 15:04:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_COLORS_H +# define FT_COLORS_H + +# define FT_C_BOLD "\033[1m" +# define FT_C_DIM "\033[2m" +# define FT_C_ITALIC "\033[3m" +# define FT_C_UNDERLINED "\033[4m" +# define FT_C_BLINK "\033[5m" + +# define FT_C_BLACK "\033[30m" +# define FT_C_RED "\033[31m" +# define FT_C_GREEN "\033[32m" +# define FT_C_YELLOW "\033[33m" +# define FT_C_BLUE "\033[34m" +# define FT_C_MAGENTA "\033[35m" +# define FT_C_CYAN "\033[36m" +# define FT_C_WHITE "\033[37m" + +# define FT_C_BG_BLACK "\033[40m" +# define FT_C_BG_RED "\033[41m" +# define FT_C_BG_GREEN "\033[42m" +# define FT_C_BG_YELLOW "\033[43m" +# define FT_C_BG_BLUE "\033[44m" +# define FT_C_BG_MAGENTA "\033[45m" +# define FT_C_BG_CYAN "\033[46m" +# define FT_C_BG_WHITE "\033[47m" + +# define FT_C_RESET "\033[0m" + +#endif diff --git a/libft_personal/include/ft_defs.h b/libft_personal/include/ft_defs.h new file mode 100644 index 0000000..4c843cd --- /dev/null +++ b/libft_personal/include/ft_defs.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_defs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/21 14:55:34 by bgoulard #+# #+# */ +/* Updated: 2024/05/27 09:00:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_DEFS_H +# define FT_DEFS_H + +# include +# include + +/// @brief Type of function to compare two elements +/// @param A The first element to compare +/// @param B The second element to compare +/// @return Standard cmp c function ( < 0, 0, > 0 for a < b, a == b, a > b) +typedef int (*t_data_cmp)(const void *, const void *); + +/// @brief Type of function to apply on a node data +/// @param Data The data to apply the function on +/// @return Void +typedef void (*t_data_apply)(void *); + +/// @brief Type of function to see if a node data is something +/// @return True if the data is what we are looking for, false otherwise +typedef bool (*t_data_is)(const void *); + +/// @brief Type of function to transform a data into something else +/// @return The new data +typedef void *(*t_data_tr)(const void *); + +/// @brief Type of function to transform inplace a data into some other data +/// @return pointer to the data (think of strcat or memcpy, returns a pointer +/// to dst) +typedef void *(*t_data_tr_i)(void *); + +#endif /* FT_DEFS_H */ diff --git a/libft_personal/include/ft_errno.h b/libft_personal/include/ft_errno.h new file mode 100644 index 0000000..4bac457 --- /dev/null +++ b/libft_personal/include/ft_errno.h @@ -0,0 +1,415 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_errno.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 14:10:11 by bgoulard #+# #+# */ +/* Updated: 2024/07/08 14:24:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_ERRNO_H +# define FT_ERRNO_H + +/* ************************************************************************** */ +/* */ +/* Module: ft_string */ +/* */ +/* Prefix: */ +/* */ +/* Description: */ +/* - This file is part of the ft_string module. Was separated for */ +/* clarity. */ +/* */ +/* ************************************************************************** */ + +# define EPERM 1 +// EPERM 'Operation not permitted' + +# define ENOENT 2 +// ENOENT 'No such file or directory' + +# define ESRCH 3 +// ESRCH 'No such process' + +# define EINTR 4 +// EINTR 'Interrupted system call' + +# define EIO 5 +// EIO 'I/O error' + +# define ENXIO 6 +// ENXIO 'No such device or address' + +# define E2BIG 7 +// E2BIG 'Argument list too long' + +# define ENOEXEC 8 +// ENOEXEC 'Exec format error' + +# define EBADF 9 +// EBADF 'Bad file number' + +# define ECHILD 10 +// ECHILD 'No child processes' + +# define EAGAIN 11 +// EAGAIN 'Try again' + +# define ENOMEM 12 +// ENOMEM 'Out of memory' + +# define EACCES 13 +// EACCES 'Permission denied' + +# define EFAULT 14 +// EFAULT 'Bad address' + +# define ENOTBLK 15 +// ENOTBLK 'Block device required' + +# define EBUSY 16 +// EBUSY 'Device or resource busy' + +# define EEXIST 17 +// EEXIST 'File exists' + +# define EXDEV 18 +// EXDEV 'Cross-device link' + +# define ENODEV 19 +// ENODEV 'No such device' + +# define ENOTDIR 20 +// ENOTDIR 'Not a directory' + +# define EISDIR 21 +// EISDIR 'Is a directory' + +# define EINVAL 22 +// EINVAL 'Invalid argument' + +# define ENFILE 23 +// ENFILE 'File table overflow' + +# define EMFILE 24 +// EMFILE 'Too many open files' + +# define ENOTTY 25 +// ENOTTY 'Not a typewriter' + +# define ETXTBSY 26 +// ETXTBSY 'Text file busy' + +# define EFBIG 27 +// EFBIG 'File too large' + +# define ENOSPC 28 +// ENOSPC 'No space left on device' + +# define ESPIPE 29 +// ESPIPE 'Illegal seek' + +# define EROFS 30 +// EROFS 'Read-only file system' + +# define EMLINK 31 +// EMLINK 'Too many links' + +# define EPIPE 32 +// EPIPE 'Broken pipe' + +# define EDOM 33 +// EDOM 'Math argument out of domain of func' + +# define ERANGE 34 +// ERANGE 'Math result not representable' + +# define EDEADLK 35 +// EDEADLK 'Resource deadlock would occur' + +# define ENAMETOOLONG 36 +// ENAMETOOLONG 'File name too long' + +# define ENOLCK 37 +// ENOLCK 'No record locks available' + +# define ENOSYS 38 +// ENOSYS 'Function not implemented' + +# define ENOTEMPTY 39 +// ENOTEMPTY 'Directory not empty' + +# define ELOOP 40 +// ELOOP 'Too many symbolic links encountered' + +# define ENOMSG 42 +// ENOMSG 'No message of desired type' + +# define EIDRM 43 +// EIDRM 'Identifier removed' + +# define ECHRNG 44 +// ECHRNG 'Channel number out of range' + +# define EL2NSYNC 45 +// EL2NSYNC 'Level 2 not synchronized' + +# define EL3HLT 46 +// EL3HLT 'Level 3 halted' + +# define EL3RST 47 +// EL3RST 'Level 3 reset' + +# define ELNRNG 48 +// ELNRNG 'Link number out of range' + +# define EUNATCH 49 +// EUNATCH 'Protocol driver not attached' + +# define ENOCSI 50 +// ENOCSI 'No CSI structure available' + +# define EL2HLT 51 +// EL2HLT 'Level 2 halted' + +# define EBADE 52 +// EBADE 'Invalid exchange' + +# define EBADR 53 +// EBADR 'Invalid request descriptor' + +# define EXFULL 54 +// EXFULL 'Exchange full' + +# define ENOANO 55 +// ENOANO 'No anode' + +# define EBADRQC 56 +// EBADRQC 'Invalid request code' + +# define EBADSLT 57 +// EBADSLT 'Invalid slot' + +# define EBFONT 59 +// EBFONT 'Bad font file format' + +# define ENOSTR 60 +// ENOSTR 'Device not a stream' + +# define ENODATA 61 +// ENODATA 'No data available' + +# define ETIME 62 +// ETIME 'Timer expired' + +# define ENOSR 63 +// ENOSR 'Out of streams resources' + +# define ENONET 64 +// ENONET 'Machine is not on the network' + +# define ENOPKG 65 +// ENOPKG 'Package not installed' + +# define EREMOTE 66 +// EREMOTE 'Object is remote' + +# define ENOLINK 67 +// ENOLINK 'Link has been severed' + +# define EADV 68 +// EADV 'Advertise error' + +# define ESRMNT 69 +// ESRMNT 'Srmount error' + +# define ECOMM 70 +// ECOMM 'Communication error on send' + +# define EPROTO 71 +// EPROTO 'Protocol error' + +# define EMULTIHOP 72 +// EMULTIHOP 'Multihop attempted' + +# define EDOTDOT 73 +// EDOTDOT 'RFS specific error' + +# define EBADMSG 74 +// EBADMSG 'Not a data message' + +# define EOVERFLOW 75 +// EOVERFLOW 'Value too large for defined data type' + +# define ENOTUNIQ 76 +// ENOTUNIQ 'Name not unique on network' + +# define EBADFD 77 +// EBADFD 'File descriptor in bad state' + +# define EREMCHG 78 +// EREMCHG 'Remote address changed' + +# define ELIBACC 79 +// ELIBACC 'Can not access a needed shared library' + +# define ELIBBAD 80 +// ELIBBAD 'Accessing a corrupted shared library' + +# define ELIBSCN 81 +// ELIBSCN '.lib section in a.out corrupted' + +# define ELIBMAX 82 +// ELIBMAX 'Attempting to link in too many shared libraries' + +# define ELIBEXEC 83 +// ELIBEXEC 'Cannot exec a shared library directly' + +# define EILSEQ 84 +// EILSEQ 'Illegal byte sequence' + +# define ERESTART 85 +// ERESTART 'Interrupted system call should be restarted' + +# define ESTRPIPE 86 +// ESTRPIPE 'Streams pipe error' + +# define EUSERS 87 +// EUSERS 'Too many users' + +# define ENOTSOCK 88 +// ENOTSOCK 'Socket operation on non-socket' + +# define EDESTADDRREQ 89 +// EDESTADDRREQ 'Destination address required' + +# define EMSGSIZE 90 +// EMSGSIZE 'Message too long' + +# define EPROTOTYPE 91 +// EPROTOTYPE 'Protocol wrong type for socket' + +# define ENOPROTOOPT 92 +// ENOPROTOOPT 'Protocol not available' + +# define EPROTONOSUPPORT 93 +// EPROTONOSUPPORT 'Protocol not supported' + +# define ESOCKTNOSUPPORT 94 +// ESOCKTNOSUPPORT 'Socket type not supported' + +# define EOPNOTSUPP 95 +// EOPNOTSUPP 'Operation not supported on transport endpoint' + +# define EPFNOSUPPORT 96 +// EPFNOSUPPORT 'Protocol family not supported' + +# define EAFNOSUPPORT 97 +// EAFNOSUPPORT 'Address family not supported by protocol' + +# define EADDRINUSE 98 +// EADDRINUSE 'Address already in use' + +# define EADDRNOTAVAIL 99 +// EADDRNOTAVAIL 'Cannot assign requested address' + +# define ENETDOWN 100 +// ENETDOWN 'Network is down' + +# define ENETUNREACH 101 +// ENETUNREACH 'Network is unreachable' + +# define ENETRESET 102 +// ENETRESET 'Network dropped connection because of reset' + +# define ECONNABORTED 103 +// ECONNABORTED 'Software caused connection abort' + +# define ECONNRESET 104 +// ECONNRESET 'Connection reset by peer' + +# define ENOBUFS 105 +// ENOBUFS 'No buffer space available' + +# define EISCONN 106 +// EISCONN 'Transport endpoint is already connected' + +# define ENOTCONN 107 +// ENOTCONN 'Transport endpoint is not connected' + +# define ESHUTDOWN 108 +// ESHUTDOWN 'Cannot send after transport endpoint shutdown' + +# define ETOOMANYREFS 109 +// ETOOMANYREFS 'Too many references: cannot splice' + +# define ETIMEDOUT 110 +// ETIMEDOUT 'Connection timed out' + +# define ECONNREFUSED 111 +// ECONNREFUSED 'Connection refused' + +# define EHOSTDOWN 112 +// EHOSTDOWN 'Host is down' + +# define EHOSTUNREACH 113 +// EHOSTUNREACH 'No route to host' + +# define EALREADY 114 +// EALREADY 'Operation already in progress' + +# define EINPROGRESS 115 +// EINPROGRESS 'Operation now in progress' + +# define ESTALE 116 +// ESTALE 'Stale NFS file handle' + +# define EUCLEAN 117 +// EUCLEAN 'Structure needs cleaning' + +# define ENOTNAM 118 +// ENOTNAM 'Not a XENIX named type file' + +# define ENAVAIL 119 +// ENAVAIL 'No XENIX semaphores available' + +# define EISNAM 120 +// EISNAM 'Is a named type file' + +# define EREMOTEIO 121 +// EREMOTEIO 'Remote I/O error' + +# define EDQUOT 122 +// EDQUOT 'Quota exceeded' + +# define ENOMEDIUM 123 +// ENOMEDIUM 'No medium found' + +# define EMEDIUMTYPE 124 +// EMEDIUMTYPE 'Wrong medium type' + +# define ECANCELED 125 +// ECANCELED 'Operation Canceled' + +# define ENOKEY 126 +// ENOKEY 'Required key not available' + +# define EKEYEXPIRED 127 +// EKEYEXPIRED 'Key has expired' + +# define EKEYREVOKED 128 +// EKEYREVOKED 'Key has been revoked' + +# define EKEYREJECTED 129 +// EKEYREJECTED 'Key was rejected by service' + +# define EOWNERDEAD 130 +// EOWNERDEAD 'Owner died' + +# define ENOTRECOVERABLE 131 +// ENOTRECOVERABLE 'State not recoverable' + +#endif /* FT_ERRNO_H */ diff --git a/libft_personal/include/ft_list.h b/libft_personal/include/ft_list.h new file mode 100644 index 0000000..74b0143 --- /dev/null +++ b/libft_personal/include/ft_list.h @@ -0,0 +1,450 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 11:40:02 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:04:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_H +# define FT_LIST_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_LIST */ +/* Prefix: ft_dl, ft_ll */ +/* */ +/* The module FT_LIST provides a way to handle task related to linked */ +/* lists whether the list happens to be simply linked, doubly linked. */ +/* Due to the nature of the module (handling different types of the same */ +/* variety of struct the prototypes are arranged using functionality */ +/* instead of type as would be common. */ +/* Currently supported types: */ +/* -simply_linked, */ +/* -doubly_linked. */ +/* */ +/* ************************************************************************** */ + +# include "ft_list_types.h" +# include "ft_defs.h" + +/* ************************************************************************** */ +/* ADD */ +/* ************************************************************************** */ + +/// @brief Add a node at the end of the list +/// @param head The head of the list +/// @param added The node to add +void ft_dl_add_back(t_dlist **head, t_dlist *const added); + +/// @brief Add a node at the beginning of the list +/// @param head The head of the list +/// @param added The node to add +void ft_dl_add_front(t_dlist **head, t_dlist *const added); + +/// @brief Add a node at the begining of the list +/// @param head The head of the list +/// @param added The node to add +/// @return void +void ft_ll_add_front(t_list **lst, t_list *const new_node); + +/// @brief Add a node at the end of the list +/// @param head The head of the list +/// @param added The node to add +/// @return void +void ft_ll_add_back(t_list **lst, t_list *const new_node); + +/* ************************************************************************** */ +/* APPLY */ +/* ************************************************************************** */ + +/// @brief Apply a function on every node of the list +/// @param start The start of the list +/// @param applied The function to apply +/// @return The number of nodes applied +size_t ft_dl_apply(const t_dlist *start, t_data_apply applied); + +/// @brief Apply a function on every node of the list until the end +/// @param start The start of the list +/// @param end The end of the list +/// @param applied The function to apply +/// @return The number of nodes applied +size_t ft_dl_apply_range(const t_dlist *start, const t_dlist *end, + t_data_apply applied); + +/// @brief Apply a function on every node of the list until the end +/// @param start The start of the list +/// @param end The end of the list +/// @param applied The function to apply +/// @return The number of nodes applied +size_t ft_dl_apply_range_node(const t_dlist *start, const t_dlist *end, + t_dnode_apply applied); + +/// @brief Apply a function on every node of the list +/// @param lst The list +/// @param f The function to apply +/// @return void +void ft_ll_apply(const t_list *lst, t_data_apply f); + +/// @brief Apply a function on every node of the list until the element end +/// @param lst The list +/// @param end The end of the list +/// @param f The function to apply +/// @return void +void ft_ll_apply_range(const t_list *lst, const t_list *end, + t_data_apply f); + +/// @brief Apply a function on every node of the list until the element end +/// @param lst The list +/// @param end The end of the list +/// @param f The function to apply +/// @return void +void ft_ll_apply_range_node(const t_list *lst, const t_list *end, + t_lnode_apply f); + +/* ************************************************************************** */ +/* CLEAR */ +/* ************************************************************************** */ + +/// @brief Clear a list +/// @param head The adress of head of the list to clear +/// @param del The function to delete the data +/// @return The number of nodes deleted +/// @note The head is set to NULL +size_t ft_dl_clear(t_dlist **head, t_data_apply del); + +/// @brief Clear a list until the end +/// @param start The start of the list +/// @param end The end of the list +/// @param del The function to delete the data +/// @return The number of nodes deleted +size_t ft_dl_clear_range(t_dlist *start, t_dlist *end, t_data_apply del); + +/// @brief Clear a list +/// @param lst The list +/// @param del The function to delete the data +/// @return void +void ft_ll_clear(t_list **lst, t_data_apply del); + +// TODO: implement clear range for ll + +/* ************************************************************************** */ +/* CREATE & COPY */ +/* ************************************************************************** */ + +/// @brief Create a new node +/// @param data The data of the node +/// @return The new node +t_dlist *ft_dl_create(const void *data); + +/// @brief Copy a node +/// @param other The node to copy +/// @return The new node +/// @note The node is a copy of the original node but does not copy the data +/// in a new ptr +t_dlist *ft_dl_copy_node(const t_dlist *const other); + +/// @brief Copy a node +/// @param other The node to copy +/// @return The new node +/// @note The node is a copy of the original node but does not copy the data +/// in a new ptr. This is wgy it doesn't need a delete function. +t_dlist *ft_dl_copy_list(const t_dlist *const other); + +/// @brief Create a new node +/// @param data The data of the node +/// @return The new node +t_list *ft_ll_create(const void *const data); + +/// @brief Copy a node +/// @param other The node to copy +/// @return The new node +t_list *ft_ll_copy_node(const t_list *const other); + +/// @brief Copy a list +/// @param other The list to copy +/// @return The new list +t_list *ft_ll_copy_list(const t_list *const other); + +/* ************************************************************************** */ +/* DELETE */ +/* ************************************************************************** */ + +/// @brief Delete a node +/// @param node The node to delete +/// @param del The function to delete the data +/// @return The number of nodes deleted +int ft_dl_delete_self(t_dlist *node, t_data_apply del); + +/// @brief Delete a node +/// @param start The node from which to delete +/// @param end The node until which to delete +/// @return The number of nodes deleted +size_t ft_dl_delete_range(t_dlist *start, const t_dlist *target, + t_data_apply del); + +/// @brief Delete a doubly linked list entirely +/// @param head The head of the list +/// @return The number of nodes deleted +size_t ft_dl_delete(t_dlist **head, t_data_apply del); + +// TODO: implement delete dup for dl +// not currently implemented - idea of the function: +// /// @brief Delete duplicates node +// /// @param head The head of the list +// /// @param cmp The compare function +// /// @param del The function to delete the data +// /// @return The number of nodes deleted +// size_t ft_dl_delete_dup(t_dlist **src, t_data_cmp cmp, +// t_data_apply del); + +/// @brief Delete a node +/// @param node The node to delete +/// @param del The function to delete the data +/// @return void +/// @note this is a useles function, it is here for compatibility with the +/// libft project. +void ft_ll_delone(t_list *lst, t_data_apply del); + +/// @brief Delete a node +/// @param lst The node from which to delete +/// @param target The node until which to delete +/// @param del The function to delete the data +/// @return void +size_t ft_ll_delete_range(t_list *lst, const t_list *end, t_data_apply del); + +// TODO: implement delete for ll +// delete should delete the whole list + +// TODO: implement delete dup for ll +// not currently implemented - idea of the function: +// /// @brief Delete duplicates node +// /// @param head The head of the list +// /// @param cmp The compare function +// /// @param del The function to delete the data +// /// @return The number of nodes deleted +// size_t ft_listdelete_dup(t_list **src, t_data_cmp cmp, +// t_data_apply del); + +/* ************************************************************************** */ +/* FIND */ +/* ************************************************************************** */ + +/// @brief Find a node in a list +/// @param head The head of the list +/// @param data The data to find +/// @param cmp The compare function +/// @return The node found or NULL +t_dlist *ft_dl_find(const t_dlist *head, const void *data, t_data_cmp cmp); + +/// @brief Find a node in a list +/// @param list The list +/// @param data The data to find +/// @param cmp The compare function +/// @return The node found or NULL +void *ft_ll_find(const t_list *list, const void *data, t_data_cmp cmp); + +/* ************************************************************************** */ +/* GETTERS */ +/* ************************************************************************** */ + +/// @brief Get the datas of a list +/// @param src The list +/// @return The datas of the list +/// @note The datas are in the same order as the nodes and the pointers to the +/// datas need to be freed +void **ft_dl_get_datas(const t_dlist *src); + +/// @brief Get the nodes of a list +/// @param src The list +/// @return The nodes of the list +/// @note The nodes are in the same order as the datas and the pointers to the +/// nodes need to be freed +t_dlist **ft_dl_get_nodes(const t_dlist *src); + +/// @brief Get the datas of a list +/// @param src The list +/// @return The datas of the list +void **ft_ll_get_datas(const t_list *src); + +/// @brief Get the nodes of a list +/// @param src The list +/// @return The nodes of the list +t_list **ft_ll_get_nodes(const t_list *src); + +/* ************************************************************************** */ +/* ITERATORS */ +/* ************************************************************************** */ + +/// @brief Get the last node of a list +/// @param head The head of the list +/// @param index The index of the node +/// @return The node at index or NULL +t_dlist *ft_dl_at(const t_dlist *head, size_t index); + +/// @brief Get the last node of a list +/// @param head The head of the list +/// @return The last node of the list +t_dlist *ft_dl_end(const t_dlist *head); + +/// @brief Get the first node of a list +/// @param head The head of the list +/// @return The first node of the list +t_dlist *ft_dl_begin(const t_dlist *head); + +/// @brief Get the last node of a list +/// @param lst The list +/// @return The last node of the list +t_list *ft_ll_end(const t_list *lst); + +/// @brief Get the node of a list at position index +/// @param lst The list +/// @param index The index of the node +/// @return The node at index or NULL +t_list *ft_ll_at(const t_list *lst, size_t index); + +/* ************************************************************************** */ +/* MAP */ +/* ************************************************************************** */ + +/// @brief Apply a function on every node of the list +/// @param lst The list +/// @param f The function to apply +/// @param del The function to delete the data if allocation fails +/// @return The new list +t_dlist *ft_dl_map(const t_dlist *lst, t_data_tr f, t_data_apply del); + +/// @brief Apply a function on every node of the list +/// @param lst The list +/// @param f The function to apply +/// @param del The function to delete the data if allocation fails +/// @return The new list +t_list *ft_ll_map(const t_list *lst, t_data_tr f, t_data_apply del); + +/* ************************************************************************** */ +/* NEW */ +/* ************************************************************************** */ + +/// @brief Create a new node +/// @return The new node +t_dlist *ft_dl_new(void); + +/// @brief Create a new node +/// @return The new node +t_list *ft_ll_new(void); + +/* ************************************************************************** */ +/* PUSH & POP */ +/* ************************************************************************** */ + +/// @brief Push a node at the beginning of the list +/// @param node The head of the list +/// @param data The data of the node +/// @return The new head of the list +t_dlist *ft_dl_push(t_dlist **node, const void *data); + +/// @brief Push a node at the end of the list +/// @param node The head of the list +/// @param data The data of the node +/// @return The new head of the list +t_dlist *ft_dl_push_back(t_dlist **node, const void *data); + +/// @brief Pop a node at the beginning of the list +/// @param node The head of the list +/// @return The data of the node +void *ft_dl_pop(t_dlist **node); + +/// @brief Pop a node at the end of the list +/// @param node The head of the list +/// @return The data of the node +void *ft_dl_pop_back(t_dlist **node); + +/// @brief Push a node at the beginning of the list +/// @param lst The head of the list +/// @param data The data of the node +/// @return The new head of the list +t_list *ft_ll_push(t_list **lst, const void *data); + +/// @brief Push a node at the end of the list +/// @param lst The head of the list +/// @param data The data of the node +/// @return The new head of the list +t_list *ft_ll_push_back(t_list **lst, const void *data); + +/// @brief Pop a node at the beginning of the list +/// @param lst The head of the list +/// @return The data of the node +void *ft_ll_pop(t_list **lst); + +/// @brief Pop a node at the end of the list +/// @param lst The head of the list +/// @return The data of the node +void *ft_ll_pop_back(t_list **lst); + +/* ************************************************************************** */ +/* REVERSE */ +/* ************************************************************************** */ + +/// @brief Reverse a list +/// @param head The head of the list +/// @return the new head of the list +t_dlist *ft_dl_rev(t_dlist **head); + +/// @brief Reverse a list +/// @param head The head of the list +/// @return the new head of the list +t_list *ft_ll_rev(t_list **head); + +/* ************************************************************************** */ +/* SIZE */ +/* ************************************************************************** */ + +/// @brief Get the size of a list +/// @param head The head of the list +/// @return The size of the list +size_t ft_dl_size(const t_dlist *head); + +/// @brief Get the size of a list +/// @param head The head of the list +/// @param function The function to check if the data is something +/// @return The size of the list +size_t ft_dl_size_of_data(const t_dlist *head, t_data_is function); + +/// @brief Get the size of a list +/// @param lst The list +/// @return The size of the list +size_t ft_ll_size(const t_list *lst); + +/// @brief Get the size of a list +/// @param lst The list +/// @param function The function to check if the data is something +/// @return The size of the list +size_t ft_ll_size_match(const t_list *lst, t_data_is function); + +// todo: rename either ll_size_of_data or dl_size_match + +/* ************************************************************************** */ +/* SUB */ +/* ************************************************************************** */ + +/// @brief Get a sublist of a list +/// @param src The list +/// @param to The node until which to get the sublist +/// @return The sublist +/// @note The sublist is a copy of the original list but does not copy +/// the data in a new ptr. This is why it doesn't need a delete function. +t_dlist *ft_dl_subrange(const t_dlist *src, const t_dlist *to); + +/// @brief Get a sublist of a list +/// @param src The list +/// @param to The node until which to get the sublist +/// @note The sublist is a copy of the original list but does not copy +/// the data in a new ptr. This is why it doesn't need a delete function. +/// @return The sublist +t_list *ft_ll_subrange(const t_list *lst, const t_list *end); + +#endif /* FT_LIST_H */ diff --git a/libft_personal/include/ft_list_types.h b/libft_personal/include/ft_list_types.h new file mode 100644 index 0000000..88d4476 --- /dev/null +++ b/libft_personal/include/ft_list_types.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_list_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/07 10:37:44 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:48:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_LIST_TYPES_H +# define FT_LIST_TYPES_H + +# include +# define FTLIST_SUCCESS 0 +# define FTLIST_FAILURE 1 + +/// @brief Structure representing a node in a list +/// @param data The data of the node +/// @param next The next node +typedef struct s_list +{ + void *data; + struct s_list *next; +} t_list; + +/// @brief Structure representing a node in a doubly linked list +/// @param data The data of the node +/// @param next The next node +/// @param prev The previous node +typedef struct s_dl_list +{ + struct s_dl_list *next; + struct s_dl_list *prev; + void *data; +} t_dlist; + +/// @brief Type of function to apply on a doubly linked list node +typedef void (*t_dnode_apply)(t_dlist *); + +/// @brief Type of function to apply on a simply linked list node +typedef void (*t_lnode_apply)(t_list *); + +#endif /* FT_LIST_TYPES_H */ diff --git a/libft_personal/include/ft_map.h b/libft_personal/include/ft_map.h new file mode 100644 index 0000000..f7cff97 --- /dev/null +++ b/libft_personal/include/ft_map.h @@ -0,0 +1,144 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 16:08:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:05:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MAP_H +# define FT_MAP_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_MAP */ +/* Prefix: ft_map */ +/* */ +/* The module FT_MAP provides a way to handle hash maps and function */ +/* related to hashmaps. */ +/* */ +/* ************************************************************************** */ + +# include "ft_defs.h" +# include "ft_map_types.h" + +/// @brief Create a new map +/// @param capacity possible number of elements in the map +/// @return a pointer to the new map +/// @file ft_map/ft_map_create.c +t_map *ft_map_create(size_t capacity); + +/// @brief Destroy a map and free the nodes +/// @param map map to destroy +/// @file ft_map/ft_map_destroy.c +/// @return void +void ft_map_destroy(t_map *map); + +/// @brief Destroy a map and free the nodes and the user data in them using a +/// function passed as an argument +/// @param map map to destroy +/// @param free_data function to free the data in the map +/// @file ft_map/ft_map_destroy.c +/// @return void +void ft_map_destroy_free(t_map *map, t_data_apply free_data); + +/// @brief Clears a map +/// @param map map to clear +/// @file ft_map/ft_map_clear.c +/// @return void +void ft_map_clear(t_map *map); + +/// @brief Set a value in a map +/// @param map map to set the value in +/// @param key key of the value +/// @param value value to set +/// @param size size of the key +/// @file ft_map/ft_map_set.c +/// @return true if the value was set, otherwise if key creation failed false +bool ft_map_set(t_map *map, const void *key, const void *value, + size_t size); + +/// @brief Set the compare function of a map +/// @param map map to set the compare function +/// @param cmp compare function newly set +/// @file ft_map/ft_map_set.c +/// @return void +void ft_map_set_cmp(t_map *map, t_data_cmp cmp); + +/// @brief Set the hash function of a map +/// @param map map to set the hash function +/// @param hash hash function newly set +/// @file ft_map/ft_map_set.c +/// @return void +void ft_map_set_hash(t_map *map, t_memhash hash); + +/// @brief Get a node from a map +/// @param map map to get the node from +/// @param key key of the node +/// @param size size of the key +/// @file ft_map/ft_map_get.c +t_map_node *ft_map_get_node(t_map *map, const void *key, size_t size); + +/// @brief Get a value from a map +/// @param map map to get the value from +/// @param key key of the value +/// @param key_size size of the key +/// @file ft_map/ft_map_get.c +/// @return a pointer to the user data or NULL if the key is not found +void *ft_map_get(t_map *map, const void *key, size_t key_size); + +/// @brief Get the number of elements in a map +/// @param map map to get the size from +/// @file ft_map/ft_map_size.c +/// @return the number of elements in the map +size_t ft_map_size(const t_map *map); + +/// @brief Get the capacity of a map +/// @param map map to get the capacity from +/// @file ft_map/ft_map_get.c +/// @return the capacity of the map +size_t ft_map_capacity(const t_map *map); + +/// @brief Remove a value from a map +/// @param map map to remove the value from +/// @param key key of the value +/// @param size size of the key +/// @file ft_map/ft_map_remove.c +/// @return the value removed or NULL if the key is not found +void *ft_map_remove(t_map *map, const void *key, size_t size); + +/// @brief Hash a key +/// @param key key to hash +/// @param size size of the key +/// @file ft_map/ft_map_hash.c +/// @return the hash of the key +size_t ft_hash_djb2(const void *key, size_t size); + +/// @brief Hash a key +/// @param key key to hash +/// @param size size of the key +/// @file ft_map/ft_map_hash.c +/// @return the hash of the key +size_t ft_hash_sdbm(const void *key, size_t size); + +/// @brief Hash a key +/// @param key key to hash +/// @param size size of the key +/// @return the hash of the key +size_t ft_hash_fnv1a(const void *key, size_t size); + +/// @brief Hash a key +/// @param key key to hash +/// @param size size of the key +/// @file ft_map/ft_map_hash.c +/// @return the hash of the key +/// @note this hash function is not very good, full of collisions +/// but it's very fast, easy to understand and never overflows. +/// use it ONLY for TESTing purposes +size_t ft_hash_dummy(const void *key, size_t size); + +#endif /* FT_MAP_H */ diff --git a/libft_personal/include/ft_map_types.h b/libft_personal/include/ft_map_types.h new file mode 100644 index 0000000..3088058 --- /dev/null +++ b/libft_personal/include/ft_map_types.h @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 13:02:13 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:14:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MAP_TYPES_H +# define FT_MAP_TYPES_H + +# include "ft_list_types.h" +# include "ft_defs.h" +# include "ft_vector_types.h" +# include +# include + +/// @brief Structure representing a node in a map +/// @param data The data of the node +/// @param key The key of the node +/// @param used Whether the node is used or not +typedef struct s_map_node +{ + void *data; + const void *key; + size_t hash; +} t_map_node; + +typedef size_t (*t_memhash)(const void *data, size_t data_len); + +/// @brief Structure representing a map +/// @param capacity The capacity of the map +/// @param size The size of the map +/// @param nodes The nodes of the map (array of lists distributed by hash) +/// @param cmp The compare function of the map +/// @param hash The hash function of the map +/// @code +/// #include "ft_map.h" +/// #include "ft_string.h" +/// #include +/// +/// static void goodbye(void *data) +/// { +/// printf("Goodbye %s\n", (char *)data); +/// free(data); +/// } +/// +/// int main() +/// { +/// t_map *map; +/// char *key = "key"; +/// char *value = ft_strdup("value"); +/// char *ptr; +/// +/// map = ft_map_create(5); +/// +/// ft_map_set(map, key, value, ft_strlen(key)); +/// ft_map_set(map, "key2", ft_strdup("val32"), ft_strlen("key2")); +/// +/// ptr = ft_map_remove(map, "key2", ft_strlen("key2")); +/// goodbye(ptr); +/// +/// ft_map_set(map, "key3", ft_strdup("val3"), ft_strlen("key3")); +/// +/// printf("---\n"); +/// ft_map_destroy_free(map, goodbye); +/// return (0); +/// } +/// @endcode +/// @see ft_map_create +typedef struct s_map +{ + size_t capacity; + size_t *weights; + size_t w_total; + t_list **nodes; + t_data_cmp cmp; + t_memhash hash; + t_vector *reserved_nodes; +} t_map; + +#endif /* FT_MAP_TYPES_H */ diff --git a/libft_personal/include/ft_math.h b/libft_personal/include/ft_math.h new file mode 100644 index 0000000..6f14661 --- /dev/null +++ b/libft_personal/include/ft_math.h @@ -0,0 +1,187 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_math.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 18:59:37 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:38:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MATH_H +# define FT_MATH_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_MATH */ +/* Prefix: ft_* */ +/* */ +/* The FT_MATH module provides a way to handle task or queries related */ +/* to arithmetic operations. */ +/* */ +/* ************************************************************************** */ + +# include "ft_math_types.h" +# include + +/// @brief Return the nearest aligned value of size on the alignment +/// @param size The size to align +/// @param alignment The alignment to use +/// @return The aligned value of size on the alignment +/// @note Optimized for power of 2 alignment +size_t ft_align_2(size_t size, size_t alignment); + +/// @brief Return the nearest aligned value of size on the alignment +/// @param size The size to align +/// @param alignment The alignment to use +/// @return The aligned value of size on the alignment +/// @note See: ft_align_2 for an optimized version for power of 2 +size_t ft_align(size_t size, size_t alignment); + +/// @brief return the logaritm of the number in the specified base +/// @param nbr number to get the logaritm +/// @param base base of the logaritm +/// @return the logaritm of the number in the specified base. in case of error +/// return -1 +int ft_llogof(long long nbr, int base); + +/// @brief return the logaritm of the number in the specified base +/// @param nbr number to get the logaritm +/// @param base base of the logaritm +/// @return the logaritm of the number in the specified base. in case of error +/// return -1 +int ft_ullogof(unsigned long long nbr, int base); + +/// @brief return the logaritm of the number in the specified base +/// @param nbr number to get the logaritm +/// @param base base of the logaritm +/// @return the logaritm of the number in the specified base. in case of error +/// return -1 +int ft_logof(int nbr, int base); + +/// @brief return the logaritm of the number in the specified base +/// @param nbr number to get the logaritm +/// @param base base of the logaritm +/// @return the logaritm of the number in the specified base. in case of error +/// return -1 +int ft_log(int nbr); + +/// @brief returns the minimum of a and b +/// @param a first number +/// @param b second number +/// @return the smallest between a and b +int ft_min(int a, int b); + +/// @brief returns the maximum of a and b +/// @param a first number +/// @param b second number +/// @return the biggest between a and b +int ft_max(int a, int b); + +/// @brief Clamp a value between a minimum and a maximum +/// @param value The value to clamp +/// @param min The minimum value +/// @param max The maximum value +/// @return The clamped value between or at the minimum or maximum +/// @note This is a clamp function aka inferior and superior to min and max +/// are set to min and max +/// @file ft_clamp.c +int ft_clamp(int value, int min, int max); + +/// @brief Clamp a value between a minimum and a maximum +/// @param value The value to clamp +/// @param min The minimum value +/// @param max The maximum value +/// @return The clamped value between or at the minimum or maximum +/// @note See ft_clamp. +/// @file ft_clamp.c +float ft_clamp_f(float value, float min, float max); + +/// @brief Clamp a value between a minimum and a maximum +/// @param value The value to clamp +/// @param min The minimum value +/// @param max The maximum value +/// @return The clamped value between or at the minimum or maximum +/// @note See ft_clamp. +/// @file ft_clamp.c +double ft_clamp_d(double value, double min, double max); + +/// @brief Take a value in a range and puts it in another range of 1 to new_max +/// @param value The value to range +/// @param min The minimum value of the range +/// @param max The maximum value of the range +/// @param new_max The maximum value of the new range +/// @return The value ranged between 1 and new_max +int ft_range(int value, int min, int max, int new_max); + +/// @brief Take a value in a range and puts it in another range of 1 to new_max +/// @param value The value to range +/// @param min The minimum value of the range +/// @param max The maximum value of the range +/// @param new_max The maximum value of the new range +/// @return The value ranged between 1 and new_max +/// @note See ft_range. +float ft_range_f(float value, float min, float max, float new_max); + +/// @brief Take a value in a range and puts it in another range of 1 to new_max +/// @param value The value to range +/// @param min The minimum value of the range +/// @param max The maximum value of the range +/// @param new_max The maximum value of the new range +/// @return The value ranged between 1 and new_max +/// @note See ft_range. +double ft_range_d(double value, double min, double max, double new_max); + +/// @brief Return the absolute value of a +/// @param a The value to get the absolute value +/// @return The absolute value of a +int ft_abs(int a); + +/// @brief Return the rounded value of x +/// @param x The value to round +/// @return The rounded value of x +/// @note This function round the value to the nearest integer +double ft_round(double x); + +/// @brief Return the power of a number x to the power of y +/// @param x The number to power +/// @param y The power +/// @return The result of x to the power of y +size_t ft_pow(size_t x, size_t y); + +/// @brief Return the root square of a number +/// @param nb The number to get the root square +/// @return The root square of nb +/// @note This function use the newton's method to get the root square +/// @note If nb is negative, return -1 +/// @file ft_sqrt.c +double ft_sqrt(double nb); + +/// @brief Return the absolute value of a +/// @param a The value to get the absolute value +/// @return The absolute value of a +/// @note See ft_abs. +double ft_complex_abs(t_complex nb); + +/// @brief Add a long to a complex number +/// @param nb The complex number to add the factor +/// @param factor The factor to add to the complex number +/// @return The complex number with the factor added +/// @note See ft_complex_addl. +t_complex ft_complex_addl(t_complex nb, long factor); + +/// @brief Multiply a complex number by a long +/// @param nb The complex number to multiply +/// @param factor The factor to multiply the complex number +/// @return The complex number multiplied by the factor +t_complex ft_complex_mull(t_complex nb, long factor); + +/// @brief Multiply a complex number by a double +/// @param nb The complex number to multiply +/// @param factor The factor to multiply the complex number +/// @return The complex number multiplied by the factor +t_complex ft_complex_muld(t_complex nb, double factor); + +#endif diff --git a/libft_personal/include/ft_math_types.h b/libft_personal/include/ft_math_types.h new file mode 100644 index 0000000..b001639 --- /dev/null +++ b/libft_personal/include/ft_math_types.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_math_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 19:08:43 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 19:09:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_MATH_TYPES_H +# define FT_MATH_TYPES_H + +typedef struct s_complex +{ + double real; + double imaginary; +} t_complex; + +typedef struct s_vec2 +{ + double x; + double y; +} t_vec2; + +typedef struct s_vec3 +{ + double x; + double y; + double z; +} t_vec3; + +typedef struct s_vec4 +{ + double x; + double y; + double z; + double w; +} t_vec4; + +#endif diff --git a/libft_personal/include/ft_optional.h b/libft_personal/include/ft_optional.h new file mode 100644 index 0000000..8148d67 --- /dev/null +++ b/libft_personal/include/ft_optional.h @@ -0,0 +1,145 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:07:13 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:08:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_OPTIONAL_H +# define FT_OPTIONAL_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_OPTIONAL */ +/* Prefix: ft_optional */ +/* */ +/* The FT_OPTIONAL module provides a way to add the optional type to the */ +/* project, this type is used to handle the case where a function can */ +/* return a value or nothing, or to chain functions on a pointer */ +/* (creating a 'pipeline' for the data to follow). */ +/* */ +/* ************************************************************************** */ + +# include "ft_defs.h" +# include "ft_optional_types.h" +# include + +/// @brief Create a new empty optional +/// @return The new optional +/// @note prefer creating from stack +/// @note WARNING: you must free the optional after use +t_optional *ft_optional_new(void); + +/// @brief Create a new optional with a value +/// @param ptr The value to store in the optional +/// @return The new optional +/// @note prefer creating from stack +/// @note WARNING: you must free the optional after use +t_optional *ft_optional_from_val(void *ptr); + +/// @brief Copies the value of an optional into another +/// @param dest The destination optional +/// @param src The source optional +void ft_optional_copy(t_optional *dest, t_optional *src); + +/// @brief Chain functions calls on an optional until either there are no more +/// functions to call or one of the functions returns NULL +/// @param opt The optional to chain +/// @param f The functions to call +/// @return true if all functions were called, false otherwise +/// @note This function was made to emulate object programming pipelines +/// @note WARNING: The function will return false if the optional is empty +/// or if the function list is NULL +/// @code +/// #include "ft_optional.h" +/// #include "ft_optional_types.h" +/// #include "ft_string.h" +/// #include "ft_defs.h" +/// #include +/// #include +/// +/// char *ft_fd_to_buff(int fd); +/// +/// void *check_file_ext(void *filename) { +/// if (!filename) +/// return (NULL); +/// if (ft_strend_with(filename, ".txt")) +/// return (filename); +/// return (printf("Error: file must be a .txt\n"), free(filename), NULL); +/// } +/// +/// void *load_file_to_buff(void *filename) { +/// int fd; +/// char *buff; +/// +/// fd = open(filename, O_RDONLY); +/// if (fd < 0) +/// { +/// printf("Error: could not open file\n"); +/// return (free(filename), NULL); +/// } +/// buff = ft_fd_to_buff(fd); +/// return (close(fd), free(filename), buff); +/// } +/// +/// int main(int ac, char **av) { +/// t_optional opt = {OPT_NONE, NULL}; +/// const t_data_tr_i function_list[] = { +/// check_file_ext, +/// load_file_to_buff, +/// NULL}; +/// +/// if (ac != 2) +/// return (printf("Usage: %s \n", av[0]), 1); +/// opt.pres = OPT_SOME; +/// opt.val = ft_strdup(av[1]); +/// if (!ft_optional_chain(&opt, function_list)) +/// return (printf("Error: could not load file\n"), 1); +/// // Do something with the file +/// printf("File content: %s\n", (char *)opt.val); +/// return (free(opt.val), ft_optional_destroy(&opt), 0); +/// } +/// +/// @endcode +/// @note Demonstrated above, we crate a list of operations to perform on a file +/// name, we then chain the operations on the optional until either: +/// 1) an operation returns NULL (we return false) +/// -or- +/// 2) there are no more operations to perform (we return true) +bool ft_optional_chain(t_optional *opt, const t_data_tr_i *f); + +/// @brief Map a result of a function on an optional +/// @param opt The optional with the value to map +/// @param f The function to call +/// @return The new optional with the result of the functions. +/// @note WARNING: The function will return an empty optional if the result +/// of the function is NULL. +t_optional ft_optional_map(t_optional *opt, void *(**f)(void *)); + +/// @brief Destroy an optional +/// @param opt The optional to destroy +/// @return true if the optional was destroyed, false otherwise +/// @note WARNING: The optional must be empty before destroying it +/// otherwise the function will return false +bool ft_optional_destroy(t_optional *opt); + +/// @brief Duplicate an optional +/// @param org The optional to duplicate +/// @return The new optional +/// @note prefer creating from stack +/// @note WARNING: you must free the optional after use +t_optional *ft_optional_dup(t_optional *org); + +/// @brief Return the value contained in an optional +/// @param opt The optional to unwrap +/// @return The value contained in the optional +/// @note WARNING: The optional must not be empty otherwise the function will +/// crash +void *ft_optional_unwrap(t_optional opt); + +#endif /* FT_OPTIONAL_H */ diff --git a/libft_personal/include/ft_optional_types.h b/libft_personal/include/ft_optional_types.h new file mode 100644 index 0000000..c8ff865 --- /dev/null +++ b/libft_personal/include/ft_optional_types.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:07:23 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:47:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_OPTIONAL_TYPES_H +# define FT_OPTIONAL_TYPES_H + +# include + +typedef enum e_optional_type +{ + OPT_NONE, + OPT_SOME, +} t_optional_type; + +typedef struct s_optional +{ + t_optional_type pres; + void *val; +} t_optional; + +#endif /* FT_OPTIONAL_TYPES_H */ diff --git a/libft_personal/include/ft_pair.h b/libft_personal/include/ft_pair.h new file mode 100644 index 0000000..7d69150 --- /dev/null +++ b/libft_personal/include/ft_pair.h @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:14:08 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:12:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PAIR_H +# define FT_PAIR_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_PAIR */ +/* Prefix: ft_pair */ +/* */ +/* This module provide a pair structure and a simple way to interact with it. */ +/* */ +/* ************************************************************************** */ + +# include "ft_defs.h" +# include "ft_pair_types.h" + +/// @brief Create a new pair +/// @param first The first element of the pair +/// @param second The second element of the pair +/// @return The new pair allocated +/// @note avoid this function if you can, use ft_pair_set instead with a +/// stack allocated pair. +/// @note Free with basic free call. +t_pair *ft_pair_new(void *first, void *second); + +/// @brief Set the first and second element of the pair +/// @param pair The pointer to the pair to set +/// @param first The first element of the pair +/// @param second The second element of the pair +/// @return void +void ft_pair_set(t_pair *pair, void *first, void *second); + +/// @brief Get the first element of the pair +/// @param pair The pointer to the pair +/// @return The first element of the pair +/// @note Null safe. +void *ft_pair_first(t_pair *pair); + +/// @brief Get the second element of the pair +/// @param pair The pointer to the pair +/// @return The second element of the pair +/// @note Null safe. +void *ft_pair_second(t_pair *pair); + +/// @brief Frees the pair +/// @param pair The pointer to the pair to free. +/// @param del_f The function to apply to the first elements of the pair before +/// freeing the pair. +/// @param del_s The function to apply to the second elements of the pair before +/// freeing the pair. +/// @note The pair pointer is set to NULL. +void ft_pair_destroy(t_pair **pair, t_data_apply del_f, t_data_apply del_s); + +/// @brief Compare two pairs +/// @param pair1 The first pair to compare +/// @param pair2 The second pair to compare +/// @param cmp The comparison function to use +/// @return The result of the comparison +/// @note The comparison function should conform to the c standard, +/// aka < 0, 0 or > 0 for pair1 < pair2, pair1 == pair2 or pair1 > pair2 +/// respectively. +/// @note If no comparison function is provided, the comparison will be done +/// on the pointer value. +int ft_pair_cmp(t_pair *pair1, t_pair *pair2, t_data_cmp cmp); + +/// @brief Compare two pairs +/// @param pair1 The first pair to compare +/// @param pair2 The second pair to compare +/// @param cmp The comparison function to use +/// @return The result of the comparison +/// @note Same as ft_pair_cmp but compare the first element of the pair +int ft_pair_cmp_first(t_pair *pair1, t_pair *pair2, t_data_cmp cmp); + +/// @brief Compare two pairs +/// @param pair1 The first pair to compare +/// @param pair2 The second pair to compare +/// @param cmp The comparison function to use +/// @return The result of the comparison +/// @note Same as ft_pair_cmp but compare the second element of the pair +int ft_pair_cmp_second(t_pair *pair1, t_pair *pair2, t_data_cmp cmp); + +#endif diff --git a/libft_personal/include/ft_pair_types.h b/libft_personal/include/ft_pair_types.h new file mode 100644 index 0000000..8da8d62 --- /dev/null +++ b/libft_personal/include/ft_pair_types.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:14:56 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 22:15:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PAIR_TYPES_H +# define FT_PAIR_TYPES_H + +typedef struct s_pair +{ + void *first; + void *second; +} t_pair; + +#endif diff --git a/libft_personal/include/ft_string.h b/libft_personal/include/ft_string.h new file mode 100644 index 0000000..f0daf7a --- /dev/null +++ b/libft_personal/include/ft_string.h @@ -0,0 +1,925 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:25:27 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:42:42 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STRING_H +# define FT_STRING_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_STRING */ +/* Prefix: ft_, ft_string (t_string) */ +/* */ +/* This module provides a usefull c function for manipulationg string and */ +/* manipulating memory. It also includes the type t_string a string that */ +/* caches it's malloc and allocates by chunks instead of 'JIT' in the */ +/* code. */ +/* */ +/* ************************************************************************** */ + +// sys types +# include +# include +// self types +# include "ft_defs.h" +# include "ft_string_types.h" + +// malloc + free + +# include +# include + +// errno + +# include + +/* ************************************************************************** */ +/* ** FT_MEM SUB MODULE ** */ +/* ************************************************************************** */ + +/// @brief apply the function f on each byte of the memory +/// @param s start of the memory +/// @param n size of the memory +/// @param f function to apply +/// @return void +/// @note the memory is modified in place +void ft_apply_2d(void **array, t_data_apply f); + +/// @brief fill the memory with 0 +/// @param s start of the memory +/// @param n size of the memory +/// @return void +void ft_bzero(void *s, size_t n); + +/// @brief allocate memory +/// @param size size of the memory to allocate +/// @return pointer to the allocated memory otherwise NULL +void *ft_malloc(size_t size); + +/// @brief allocate memory and fill it with 0 +/// @param nmemb number of elements +/// @param weight size of each element +/// @return pointer to the allocated memory +void *ft_calloc(size_t nmemb, size_t weight); + +/// @brief load the content of the file descriptor into a string +/// @param fd file descriptor to read from +/// @return pointer to the string otherwise NULL +/// @note You must free the returned string +char *ft_fd_to_buff(int fd); + +/// @brief allocate memory and copy the content of the source memory +/// @param ptr pointer to the source memory. +/// @param sizeNew size of the destination memory +/// @param sizeOld size of the source memory +/// @return pointer to a larger chunk of allocated memory otherwise NULL +/// @note WARNING: the pointer is FREE'ed after the copy in case of success +void *ft_realloc(void *ptr, size_t sizeNew, size_t sizeOld); + +/// @brief free the memory +/// @param ptr pointer to the memory to free (set to NULL after) +/// @return void +void ft_free(void **ptr); + +/// @brief free the memory +/// @param arr pointer to the 2d array to free. +/// @return void +/// @note the array is not set to NULL after as memory is freed and no longer +/// valid to write to. +void ft_free_2d(void **arr); + +/// @brief return the length of the 2d array +/// @param array pointer to the 2d array +/// @return the length of the 2d array +/// @note the array must be NULL terminated +size_t ft_len_2d(const void *const *array); + +/// @brief search for the first occurence of c in the memory +/// @param s start of the memory +/// @param c char to search +/// @param n size of the memory +/// @return pointer to the first occurence of c in the memory otherwise NULL +void *ft_memchr(const void *s, int c, size_t n); + +/// @brief compare the memory +/// @param s1 start of the first memory +/// @param s2 start of the second memory +/// @param n size of the memory to compare +/// @return 0 if the memory are identical, otherwise the difference between the +/// first different char +int ft_memcmp(const void *s1, const void *s2, size_t n); + +/// @brief copy the memory +/// @param dest start of the destination memory +/// @param src start of the source memory +/// @param n size of the memory to copy +/// @return pointer to the destination memory +void *ft_memcpy(void *dest, const void *src, size_t n); + +/// @brief map memory region src to new region using f +/// @param src start of the source memory +/// @param nb_e number of elements +/// @param f function to apply +/// @return pointer to the new memory or NULL +void **ft_memmap(void **src, size_t nb_e, t_data_tr f); + +/// @brief copy the memory +/// @param dest start of the destination memory +/// @param src start of the source memory +/// @param n size of the memory to copy +/// @return pointer to the destination memory +void *ft_memmove(void *dest, const void *src, size_t n); + +/// @brief set the memory with c +/// @param s start of the memory +/// @param c char to set +/// @param n size of the memory +/// @return pointer to the memory +void *ft_memset(void *s, int c, size_t n); + +/// @brief swap the memory +/// @param a first memory +/// @param b second memory +/// @return void +void ft_swap(void **a, void **b); + +/// @brief sort the memory with the cmp function by chunks of size +/// @param array pointer to the memory +/// @param nmb number of chunks +/// @param size size of each chunk +/// @param cmp comparison function +/// @return void +/// @WARNING Do not use. Not implemented fully. +void ft_qsort(void *array, size_t nmb, size_t size, t_data_cmp cmp); + +/* ************************************************************************** */ +/* ** FT_PUT SUB MODULE ** */ +/* ************************************************************************** */ + +/// @brief print the string on the specified file descriptor followed by a new +/// line +/// @param s string to print +/// @param fd file descriptor to print on +/// @return void +int ft_putendl_fd(const char *s, int fd); + +/// @brief print the string on the specified file descriptor +/// @param nbr string to print +/// @param fd file descriptor to print on +/// @return void +int ft_putnbr_fd(int nbr, int fd); + +/// @brief print the string on the specified file descriptor +/// @param s string to print +/// @param fd file descriptor to print on +/// @return void +int ft_putstr_fd(const char *s, int fd); + +/* ************************************************************************** */ +/* ** FT_STR MAIN MODULE ** */ +/* ************************************************************************** */ + +/// @brief convert the string to a float +/// @param str string to convert +/// @return the float converted from the string +double ft_atof(const char *str); + +/// @brief convert the string to an int +/// @param str string to convert +/// @return the int converted from the string +int ft_atoi(const char *str); + +/// @brief convert the string to an int in the specified base +/// @param str string to convert +/// @param base base of the string +/// @return the int converted from the string +int ft_atoi_base(const char *str, const char *base); + +/// @brief convert a string with the int converted in ascii chars +/// @param nbr number to convert +/// @return pointer to the string. +/// @note You must free the returned string +char *ft_itoa(int nbr); + +/// @brief convert the int to a string in the specified base +/// @param nbr number to convert +/// @param base base of the string to return. +/// @return pointer to the string. +/// @note You must free the returned string +char *ft_itoa_base(int nbr, const char *base); + +/// @brief convert the unsigned int to a string +/// @param nbr number to convert +/// @return pointer to the string. +/// @note You must free the returned string +char *ft_utoa(unsigned int nbr); + +/// @brief Return a pointer to the first occurrence of any character contained +/// in the string delims in the string str +/// @param str_init string to search into WARNING: the string is modified by +/// this function. DO NOT PASS A CONST STRING. +/// @param delims string of delimiters +/// @return a pointer to the first occurrence of any character contained in the +/// string delims in the string str otherwise NULL +char *ft_strtok(char *str_init, const char *delims); + +/// @brief Split the string str with the specified delimiter +/// @param str String to split +/// @param delim Char to split the string +/// @return A pointer to the array of string +/// @note You must free the returned array of string and its content +char **ft_split(const char *str, char delim); + +/// @brief Same as ft_split but with multiple delimiters +/// @param str string to split +/// @param delims delimiters to split the string +/// @return a pointer to the array of string +/// @note You must free the returned array of string and its content +char **ft_splits(const char *str, const char *delims); + +/// @brief Search for the first occurence of the char c in the string str +/// @param str string to search into +/// @param c char to search +/// @return a pointer to the first occurence of c in the string str otherwise +/// NULL +/// @note The returned pointer is from str and has the same constness as str +/// it was left as non-const to align with glibc's strchr +char *ft_strchr(const char *str, int c); + +/// @brief duplicate the string src into a new allocated string +/// @param strsrc string to copy from +/// @return The copy of string src +/// @note You must free the returned string +char *ft_strdup(const char *strsrc); + +/// @brief Iterate over the string s and execute the function f on each char +/// @param s String to iterate over +/// @param f Function to execute on each char +/// @return void +/// @note The first parameter of the function f is the index of the char in the +/// string s. +void ft_striteri(char *s, void (*f)(unsigned int, char *)); + +/// @brief Add up two strings s1 and s2 and return the result +/// @param s1 String to add +/// @param s2 String to add +/// @return The result of the addition of the two strings +/// @note You must free the returned string +char *ft_strjoin(char const *s1, char const *s2); + +/// @brief Add up a char c at the end of a pre-existing and allocated string +/// @param str String to append to +/// @param c Char to append +/// @return The new memory segment pointed by *str or null if re-allocation +/// failed +/// @note You must free the returned string +/// @note Do no use this method to add two string, please use strjoin instead +char *ft_strappend_c(char **str, char c); + +/// @brief copy up to size - 1 characters from the NULL-terminated string src +/// to dst, NULL-terminating the result. +/// Puts the result after the characters already in dst. +/// @param dst string to copy to +/// @param src string to copy from +/// @param size maximum number of characters in destination string, including +/// the terminating null character. +/// @return returns the total length of the string they tried to create. +size_t ft_strlcat(char *dst, const char *src, size_t size); + +/// @brief Copies up to size - 1 characters from the NULL-terminated string src +/// to dst, NULL-terminating the result. +/// Puts the result directly at dst. +/// @param dst string to copy to +/// @param src string to copy from +/// @param size maximum number of characters in destination string, including +/// the terminating null character. +/// @return returns the total length of the string they tried to create. +/// @note If the length of src is greater than or equal to size, the string +/// will be truncated. +size_t ft_strlcpy(char *dst, const char *src, size_t size); + +/// @brief Get the length of the string str +/// @param str String to get the length of +/// @return the length of the string str +size_t ft_strlen(const char *str); + +/// @brief Get the length of the string str up to the first occurence of c +/// @param str String to get the length of +/// @param c Char to search +/// @return the length of the string str up to the first c, if c is not found +/// the length of the string str +size_t ft_strclen(char *str, char c); + +/// @brief Get the number of occurance of char c in string str +/// @param str String to search from +/// @param c Char to search +/// @return the number of occurance of char c in string str +size_t ft_strcnb(char *str, char c); + +/// @brief Calculate the length of the starting segment of str that contain +/// char from the accept string +/// @param str String to search from +/// @param accept String of the valid char +/// @return The calculated length. +size_t ft_strspn(const char *str, const char *accept); + +/// @brief Calculate the length of the starting segment of str that don't +/// contain chars from the exclude string +/// @param str String to search from +/// @param exclude String of char to exclude +/// @return The calculated length. +size_t ft_strcspn(const char *str, const char *exclude); + +/// @brief Execute the function f on each char of the string s +/// @param s String to iterate over +/// @param f function to execute on each char +/// @return an allocated string with the result of the function f on each char +/// of the string s otherwise NULL +/// @note You must free the returned string +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); + +/// @brief Compare the two strings s1 and s2 +/// @param s1 String to compare s1 +/// @param s2 String to compare s2 +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_strcmp(const char *s1, const char *s2); + +/// @brief Compare the two strings s1 and s2 up to n characters or until a `\0' +/// @param s1 String to compare s1 +/// @param s2 String to compare s2 +/// @param n number of chars to compare maximum +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_strncmp(const char *s1, const char *s2, size_t n); + +/// @brief duplicate no more than n character of the string src into a +// new allocated string +/// @param str string to copy from +/// @param n number of chars to copy +/// @return A copy of the string src +/// @note You must free the returned string +char *ft_strndup(const char *str, size_t n); + +/// @brief duplicate the string src into a new allocated string +/// @param src string to copy from +/// @return A copy of the string src +/// @note You must free the returned string +char *ft_strdup(const char *src); + +/// @brief search for the first occurence of the string small in the string big +/// @param big string to search into +/// @param small string to search +/// @param n minimum number of characters to search +/// @return returns a pointer to the first occurrence of the string small in the +/// string big, where not more than n characters are searched. Characters that +/// appear after a `\0' character are not searched. +/// @note The returned pointer is from str and has the same constness as str +/// it was left as non-const to align with glibc's strnstr +char *ft_strnstr(const char *big, const char *small, size_t n); + +/// @brief search for the last occurence of c in the string +/// @param str string to search from +/// @param c char to search +/// @return pointer to the last occurence of c in the string otherwise NULL +/// @note The returned pointer is from str and has the same constness as str +/// it was left as non-const to align with glibc's strrchr +char *ft_strrchr(const char *str, int c); + +/// @brief remove the specified chars from the string s1 +/// @param s1 string to trim +/// @param set characters to remove from s1 +/// @return allocated string without the specified chars otherwise NULL +/// @note You must free the returned string +char *ft_strtrim(char const *s1, char const *set); + +/// @brief return a substring of the string s from the specified position +/// @param s string to get the substring +/// @param start position of the substring +/// @param len length of the substring +/// @return allocated string with the substring otherwise NULL +/// @note You must free the returned string +char *ft_substr(char const *s, unsigned int start, size_t len); + +/// @brief search and replace the string to_replace in the string str +/// by the string replace_by +/// @param str String to in which the string will be searched and replaced +/// @param to_replace string to search and replace +/// @param replace_by string to replace with +/// @return the string with the modified chars otherwise NULL +/// @note to_replace and replace_by must not be NULL +/// @note You must free the returned string ! +char *ft_str_replace(const char *str, const char *to_replace, + const char *replace_by); + +/// @brief search if the string str ends with the string end +/// @param str string to search from +/// @param end string to search +/// @return 1 if the string str ends with the string end, 0 otherwise +int ft_strend_with(const char *str, const char *end); + +/// @brief search if the string str starts with the string start +/// @param str string to search from +/// @param start string to search +/// @return 1 if the string str starts with the string start, 0 otherwise +int ft_strstart_with(const char *str, const char *start); + +/// @brief replace the chars to_replace in the string by the char replace_by +/// @param str string to in which the char will be replaced +/// @param to_replace char to replace +/// @param replace_by char to replace by +/// @return A pointer to the string str +/// @note The string is modified in place +char *ft_str_replace_chr(char *str, char to_replace, char replace_by); + +/// @brief Returns a pointer to the first string pointed to by args +/// @param args Pointer to a list of const char pointer +/// @param index Number of elements in Args +/// @return if index is negative or the first string of args +/// is null fails and return null otherwise return the first +/// const char pointed to by args +const char *ft_shift_args(const char **args[], int *index); + +/// @brief Checks if the string str is composed only of alphabetical characters +/// @param str string to check +/// @return true if the string is composed only of alphabetical characters, +/// false otherwise +bool ft_str_isalpha(const char *str); + +/// @brief Checks if the string str is composed only of alphabetical and +/// numerical characters +/// @param str string to check +/// @return true if the string is composed only of alphabetical and numerical +/// characters, false otherwise +bool ft_str_isalnum(const char *str); + +/// @brief Checks if the string str is comprised of only numbers. +/// @param str string to check +/// @return true if the string is composed only of numerical characters, false +/// otherwise +/// @note This function is not the same as ft_str_isdigit, as it checks for +/// and accepts negative symbols. +bool ft_str_isnum(const char *str); + +/// @brief Checks if the string str is a valid boolean value ("false" || +/// "true" || "0" || "1") +/// @param str string to check +/// @return true if it ;atches with any of the following: "false" "0" "true" "1" +/// false otherwise +bool ft_str_isbool(const char *str); + +/// @brief Checks if the string str is composed only of numerical characters +/// @param str string to check +/// @return true if the string is composed only of numerical characters, false +/// otherwise +/// @note This function is not the same as ft_str_isnum, as it does not accept +/// ANYTHING other than numerical characters. +bool ft_str_isdigit(const char *str); + +/// @brief Check if the string is a float +/// @param str string to check +/// @return 1 if the string is a float, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isfloat.c +bool ft_str_isfloat(const char *str); + +/// @brief Check if the string is a float +/// @param str string to check +/// @return 1 if the string is a float, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isdouble.c +bool ft_str_isdouble(const char *str); + +/// @brief Check if the string is an int valid value +/// @param str string to check +/// @return 1 if the string is an int, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isint.c +bool ft_str_isint(const char *str); + +/// @brief Check if the string is a long +/// @param str string to check +/// @return 1 if the string is a number, 0 otherwise +/// @file: src/ft_string/ft_char/ft_islong.c +bool ft_str_islong(const char *str); + +/// @brief check if the string is a hex character +/// @param c char to check +/// @return 1 if the char is a hex character, 0 otherwise +/// @file: src/ft_string/ft_char/ft_ishex.c +bool ft_str_ishex(const char *str); + +/// @brief check if the string is an octal number +/// @param str string to check +/// @return 1 if the string is an octal number, 0 otherwise +bool ft_str_isoct(const char *str); + +/// @brief Check if the string is valid using a function pointer +/// @param str string to check +/// @param f function pointer to check the string (takes a char c as int +/// and returns 0 if the char is invalid) +/// @return 1 if the string is valid, 0 otherwise +/// @file: src/ft_string/ft_char/ft_isvalid.c +bool ft_str_isvalid(const char *str, int (*f)(int)); + +/// @brief Return a pointer to a constant string describing the error code +/// @param errnum Error code +/// @return A pointer to a constant string describing the error code +/// @note The returned pointer can be null if errnum is out of range (0 - 133) +const char *ft_strerror(int errnum); + +/// @brief Print the string s if present to STDERR followed by the error code +/// as a string +/// @param s String to print before the error code +void ft_perror(const char *s); + +/* ************************************************************************** */ +/* FT_GNL SUB MODULE */ +/* ************************************************************************** */ + +/// @brief Read the next line from the file descriptor +/// @param fd file descriptor to read from +/// @return the next line from the file descriptor otherwise NULL +/// @note You must free the returned string +/// @note You can see the number of supported file descriptor in the macro +/// MAX_FD +char *get_next_line(int fd); + +// printf +// missing ... todo + +/* ************************************************************************** */ +/* FT_STRING SUB MODULE */ +/* ************************************************************************** */ + +/* ************************************************************************** */ +/* ** ft_string_new ** */ +/* ************************************************************************** */ + +/// @brief create a new t_string +/// @param capacity initial capacity of the string +/// @return a pointer to the new t_string +/// @note You must free the returned string with ft_string_destroy +t_string *ft_string_new(size_t capacity); + +/* ************************************************************************** */ +/* ** ft_string_from ** */ +/* ************************************************************************** */ + +/// @brief create a new t_string from the string +/// @param str string to copy from +/// @return a pointer to the new t_string +/// @note You must free the returned string with ft_string_destroy +/// @note This function does NOT take ownership of the passed string. +t_string *ft_string_from(const char *str); + +/// @brief create a new t_string from the string with at most n chars +/// @param str string to copy from +/// @param n number of chars to copy (including the '\0') "1234" with n = 3 +/// -> "123" +/// @return a pointer to the new t_string +/// @note You must free the returned string with ft_string_destroy +t_string *ft_string_from_n(const char *str, size_t n); + +/// @brief create a new t_string from the char c +/// @param c char to copy from +/// @return a pointer to the new t_string +/// @note You must free the returned string with ft_string_destroy +t_string *ft_string_from_c(char c); + +/// @brief create a new t_string from the t_string str +/// @param str t_string to copy from +/// @return a pointer to the new t_string +/// @note You must free the returned string with ft_string_destroy +t_string *ft_string_from_s(const t_string *str); + +/// @brief create a new t_string from the t_string str using at most n chars +/// @param str t_string to copy from +/// @param n number of chars to copy +/// @return a pointer to the new t_string created +/// @note You must free the returned string with ft_string_destroy +t_string *ft_string_from_s_n(const t_string *str, size_t n); + +/* ************************************************************************** */ +/* ** ft_string_put ** */ +/* ************************************************************************** */ + +/// @brief write the string on the specified file descriptor +/// @param str t_string to write +/// @param fd file descriptor to write on +/// @return the return of write if the fd and str are valid otherwise -1 +int ft_string_put(t_string *str, int fd); + +/* ************************************************************************** */ +/* ** ft_string_append ** */ +/* ************************************************************************** */ + +/// @brief append the string src to the string str +/// @param str t_string to modify +/// @param src string to append +/// @return 1 if the string has been appended otherwise 0 +int ft_string_append(t_string *str, const char *src); + +/// @brief append at most n chars of the string src to the string str +/// @param str t_string to modify +/// @param src string to append +/// @param n number of chars to append +/// @return 1 if the string has been appended otherwise 0 +int ft_string_append_n(t_string *str, const char *src, size_t n); + +/// @brief append the char c to the string str +/// @param str t_string to modify +/// @param c char to append +/// @return 1 if the string has been appended otherwise 0 +int ft_string_append_c(t_string *str, char c); + +/// @brief append the string src to the string str +/// @param str t_string to modify +/// @param src string to append +/// @return 1 if the string has been appended otherwise 0 +int ft_string_append_s(t_string *str, t_string *src); + +/// @brief append at most n chars of the string src to the string str +/// @param str t_string to modify +/// @param src string to append +/// @param n number of chars to append +/// @return 1 if the string has been appended otherwise 0 +int ft_string_append_s_n(t_string *str, t_string *src, size_t n); + +/* ************************************************************************** */ +/* ** ft_string_clear ** */ +/* ************************************************************************** */ + +/// @brief clear the string +/// @param str t_string to clear +/// @return void +/// @note the string is not freed but the content is cleared +void ft_string_clear(t_string *str); + +/* ************************************************************************** */ +/* ** ft_string_destroy ** */ +/* ************************************************************************** */ + +/// @brief free the string +/// @param str pointer to the t_string to free +/// @return void +void ft_string_destroy(t_string **str); + +/* ************************************************************************** */ +/* ** ft_string_insert ** */ +/* ************************************************************************** */ + +/// @brief insert the string src in the string str at the specified position +/// @param str t_string to modify +/// @param src string to insert +/// @param pos position to insert the string +/// @return 1 if the string has been inserted otherwise 0 +int ft_string_insert(t_string *str, const char *src, size_t pos); + +/// @brief insert at most n chars of the string src in the string str at the +/// specified position +/// @param str t_string to modify +/// @param src string to insert +/// @param pos position to insert the string +/// @param n number of chars to insert +/// @return 1 if the string has been inserted otherwise 0 +int ft_string_insert_n(t_string *str, const char *src, size_t pos, \ + size_t n); + +/// @brief insert the char c in the string str at the specified position +/// @param str t_string to modify +/// @param c char to insert +/// @param pos position to insert the char +/// @return 1 if the string has been inserted otherwise 0 +int ft_string_insert_c(t_string *str, char c, size_t pos); + +/// @brief insert the string src in the string str at the specified position +/// @param str t_string to modify +/// @param src string to insert +/// @param pos position to insert the string +/// @return 1 if the string has been inserted otherwise 0 +int ft_string_insert_s(t_string *str, t_string *src, size_t pos); + +/// @brief insert at most n chars of the string src in the string str at the +/// specified position +/// @param str t_string to modify +/// @param src string to insert +/// @param pos position to insert the string +/// @param n number of chars to insert +/// @return 1 if the string has been inserted otherwise 0 +int ft_string_insert_s_n(t_string *str, t_string *src, size_t pos, + size_t n); + +/* ************************************************************************** */ +/* ** ft_string_reserve ** */ +/* ************************************************************************** */ + +/// @brief reserve the specified capacity for the string +/// @param str t_string to modify +/// @param capacity capacity to reserve +/// @return 1 if the string has been reserved otherwise 0 +int ft_string_reserve(t_string *str, size_t capacity); + +/* ************************************************************************** */ +/* ** ft_string_resize ** */ +/* ************************************************************************** */ + +/// @brief resize the string to the specified size +/// @param str t_string to modify +/// @param size size to resize +/// @return 1 if the string has been resized otherwise -1 +/// @note if the size is smaller than the current size, the string is truncated +int ft_string_resize(t_string *str, size_t size); + +/* ************************************************************************** */ +/* ** ft_string_shrink ** */ +/* ************************************************************************** */ + +/// @brief shrink the string to the minimum size +/// @param str t_string to modify +/// @return 1 if the string has been shrinked otherwise 0 +int ft_string_shrink(t_string *str); + +/* ************************************************************************** */ +/* ** ft_string_substr ** */ +/* ************************************************************************** */ + +/// @brief return a substring of the string str from the specified position +/// @param str t_string to get the substring +/// @param start position of the substring +/// @param len length of the substring +/// @return allocated string with the substring otherwise NULL +/// @note You must free the returned string. use ft_string_destroy. +t_string *ft_string_substr(t_string *str, size_t start, size_t len); + +/* ************************************************************************** */ +/* ** ft_string_to_str ** */ +/* ************************************************************************** */ + +/// @brief convert the t_string to a string +/// @param str t_string to convert +/// @return allocated string with the content of the t_string otherwise NULL +/// @note You must free the returned string. +/// @note The t_string is not freed or modified. +char *ft_string_to_str(t_string *str); + +/* ************************************************************************** */ +/* ** ft_string_trim ** */ +/* ************************************************************************** */ + +/// @brief trim the characters ' ' from the string +/// @param str t_string to trim +/// @return void +/// @note the inner string is not freed but the content modified. +void ft_string_trim(t_string *str); + +/// @brief trim the specified char from the string +/// @param str t_string to trim +/// @param c char to trim +/// @return void +/// @note the inner string is not freed but the content modified. +void ft_string_trim_chr(t_string *str, char c); + +/// @brief trim the specified chars from the string +/// @param str t_string to trim +/// @param to_trim chars to trim +/// @return void +/// @note the inner string is not freed but the content modified. +void ft_string_trimstr(t_string *str, const char *to_trim); + +/* ************************************************************************** */ +/* ** ft_string_cmp ** */ +/* ************************************************************************** */ + +/// @brief compare the string with the string cmp +/// @param str t_string to compare +/// @param cmp string to compare +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_string_cmp(const t_string *str, const char *cmp); + +/// @brief compare the string with the string cmp up to n chars +/// @param str t_string to compare +/// @param cmp string to compare +/// @param n number of chars to compare +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_string_ncmp(const t_string *str, const char *cmp, size_t n); + +/// @brief compare the string with the string cmp +/// @param str t_string to compare +/// @param cmp string to compare +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_string_cmpstr(const t_string *str, const t_string *cmp); + +/// @brief compare the string with the string cmp up to n chars +/// @param str t_string to compare +/// @param cmp string to compare +/// @param n number of chars to compare +/// @return 0 if the strings are identical, otherwise the difference between the +/// first different char (s1 - s2) +int ft_string_ncmpstr(const t_string *str, const t_string *cmp, size_t \ + n); + +/* ************************************************************************** */ +/* ** ft_string_get ** */ +/* ************************************************************************** */ + +/// @brief get the length of the string +/// @param str t_string to get the length +/// @return the length of the string +size_t ft_string_len(const t_string *str); + +/// @brief get the capacity of the string +/// @param str t_string to get the capacity +/// @return the capacity of the string +size_t ft_string_cap(const t_string *str); + +/// @brief get the content of the string +/// @param str t_string to get the content +/// @return the content of the string +const char *ft_string_get(const t_string *str); + +/* ************************************************************************** */ +/* ** ft_string_set ** */ +/* ************************************************************************** */ + +/// @brief replace the content of the string with the new string src +/// @param str t_string to modify +/// @param src string to copy from +/// @return 1 if the string has been set otherwise 0 +int ft_string_set(t_string *str, const char *src); + +/// @brief replace the content of the string with at most n chars of the new +/// string src +/// @param str t_string to modify +/// @param src string to copy from +/// @param n number of chars to set (including the '\0') "1234" with n = 3 +/// -> "123" +/// @return 1 if the string has been set otherwise 0 +int ft_string_set_n(t_string *str, const char *src, size_t n); + +/// @brief replace the content of the string with the new string src +/// @param str t_string to modify +/// @param src string to set +/// @return 1 if the string has been set otherwise 0 +/// @note This function takes ownership of the string src and does no alloc. +int ft_string_set_inplace(t_string *str, char *src); + +/* ************************************************************************** */ +/* ** ft_string_chr ** */ +/* ************************************************************************** */ + +/// @brief search for the first occurence of c in the string +/// @param str t_string to search from +/// @param c char to search +/// @return an offset to the first occurence of c in the string otherwise -1 +ssize_t ft_string_offset(const t_string *str, char c); + +/// @brief search for the last occurence of c in the string +/// @param str t_string to search from +/// @param c char to search +/// @return an offset to the last occurence of c in the string otherwise -1 +ssize_t ft_string_roffset(const t_string *str, char c); + +/// @brief search for the first occurence of c in the string +/// @param str t_string to search from +/// @param c char to search +/// @return a pointer to the first occurence of c in the string otherwise NULL +char *ft_string_chr(const t_string *str, char c); + +/// @brief search for the last occurence of c in the string +/// @param str t_string to search from +/// @param c char to search +/// @return a pointer to the last occurence of c in the string otherwise NULL +char *ft_string_rchr(const t_string *str, char c); + +/* ************************************************************************** */ +/* ** ft_string_replace ** */ +/* ************************************************************************** */ + +/// @brief search and replace the string to_replace in the string str +/// by the string replace_by +/// @param str t_string to in which the string will be searched and replaced +/// @param to_replace string to search and replace +/// @param replace_by string to replace with +/// @return 1 if the string has been replaced otherwise 0 +int ft_string_replace(t_string *str, const char *to_replace, + const char *replace_by); + +/// @brief search and replace the string to_replace in the string str +/// by the string replace_by +/// @param str t_string to in which the string will be searched and replaced +/// @param to_replace string to search and replace +/// @param replace_by string to replace with +/// @return 1 if the string has been replaced otherwise 0 +int ft_string_replace_chr(t_string *str, char to_replace, + char replace_by); + +#endif // FT_STRING_H diff --git a/libft_personal/include/ft_string_types.h b/libft_personal/include/ft_string_types.h new file mode 100644 index 0000000..d42eebf --- /dev/null +++ b/libft_personal/include/ft_string_types.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:54:48 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:19:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_STRING_TYPES_H +# define FT_STRING_TYPES_H + +# include + +// maximum number of file descriptors +// - get_next_line +# ifndef MAX_FD +# ifdef TEST +# define MAX_FD 5 +# else +# define MAX_FD 1024 +# endif +# endif + +// default allocation size for t_strings +// - ft_string_new +# ifndef T_STRING_BUFF +# ifdef TEST +# define T_STRING_BUFF 5 +# else +# define T_STRING_BUFF 4096 +# endif +# endif + +// buffer size for temporary read buffers +// - get_next_line +// - ft_fd_to_buff +# ifndef BUFFER_SIZE +# ifdef TEST +# define BUFFER_SIZE 5 +# else +# define BUFFER_SIZE 4096 +# endif +# endif + +/// @brief Structure representing a string +/// @param str The string +/// @param length The length of the string +/// @param capacity The capacity of the string +typedef struct s_string +{ + char *str; + size_t length; + size_t capacity; +} t_string; + +#endif /* FT_STRING_TYPES_H */ diff --git a/libft_personal/include/ft_types.h b/libft_personal/include/ft_types.h new file mode 100644 index 0000000..2b58732 --- /dev/null +++ b/libft_personal/include/ft_types.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 23:11:19 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:12:08 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_TYPES_H +# define FT_TYPES_H + +/* ************************************************************************** */ +/* */ +/* Useful in early development of projects where you might no know what you */ +/* want from the ft_personal lib - otherwise if you already know what */ +/* you use / are at the end of a project remove this to lighten the .h */ +/* step of compilation */ +/* */ +/* ************************************************************************** */ + +// true - false +# include + +// size_t - ssize_t - ptrdiff_t ... NULL +# include + +// int8_t - int16_t - uint32_t ... INT_MAX - INT_MIN +# include + +# include "ft_args_types.h" +# include "ft_list_types.h" +# include "ft_map_types.h" +# include "ft_math_types.h" +# include "ft_optional_types.h" +# include "ft_pair_types.h" +# include "ft_string_types.h" +# include "ft_vector_types.h" + +#endif diff --git a/libft_personal/include/ft_vector.h b/libft_personal/include/ft_vector.h new file mode 100644 index 0000000..bac87c1 --- /dev/null +++ b/libft_personal/include/ft_vector.h @@ -0,0 +1,218 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vector.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 13:37:46 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:09:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_VECTOR_H +# define FT_VECTOR_H + +/* ************************************************************************** */ +/* */ +/* Module: FT_VECTOR */ +/* Prefix: ft_vec_ */ +/* */ +/* This module provides the vector type, a way to create array that are chunk */ +/* allocated and allows for easy manipulations. */ +/* */ +/* ************************************************************************** */ + +# include "ft_defs.h" +# include "ft_vector_types.h" + +// ft_vector/ft_vec_add.c + +/// @brief Add an element to the end of the vector +/// @param vec pointer to the vector +/// @param data data to add +/// @return true if the element was added, false otherwise +bool ft_vec_add(t_vector **vec, void *data); + +// ft_vector/ft_vec_apply.c + +/// @brief Apply a function to all the elements of the vector +/// @param vec pointer to the vector +/// @param func function to apply +/// @return void +void ft_vec_apply(t_vector *vec, t_data_apply func); + +// ft_vector/ft_vec_at.c + +/// @brief Get an element from the vector +/// @param vec pointer to the vector +/// @param n index of the element +/// @return pointer to the element +void *ft_vec_at(t_vector *vec, size_t n); + +// ft_vector/ft_vec_cat.c + +/// @brief Concatenate two vectors +/// @param vec_a pointer to the first vector +/// @param vec_b pointer to the second vector +/// @return true if the vectors were concatenated, false otherwise +/// @note the second vector is not modified therefore it is not cleared. +/// Be careful with double free. +bool ft_vec_cat(t_vector **vec_a, const t_vector *vec_b); + +// ft_vector/ft_vec_clear.c + +/// @brief Clear a vector by setting its size to 0 and all elements to NULL +/// @param vec pointer to the vector cleared +/// @return void +/// @note the data is not freed, cappacity is not changed. if you want to free +// the data / reduce the cappacity use ft_vec_clear, then ft_vec_shrink. +void ft_vec_clear(t_vector *vec); + +// ft_vector/ft_vec_destroy.c + +/// @brief Destroy a vector +/// @param vec pointer to the vector +/// @return true if the vector was destroyed, false otherwise +bool ft_vec_destroy(t_vector **vec); + +// ft_vector/ft_vec_filter.c + +/// @brief Filter a vector +/// @param vec pointer to the vector +/// @param func function to filter the vector +/// @param del function to delete the elements filtered +/// @return void +void ft_vec_filter(t_vector *vec, t_data_is func, t_data_apply del); + +// ft_vector/ft_vec_get.c + +/// @brief Get an element from the vector +/// @param vector vector to get the element from +/// @param key key to search for using the cmp function +/// @param cmp function to compare the elements where the first argument is the +/// key and the second is the element of the vector +/// @return pointer to the element or NULL if not found +/// @note If you want to get an element by index, use ft_vec_at +void *ft_vec_get(t_vector *vector, const void *key, t_data_cmp cmp); + +// ft_vector/ft_vec_map.c + +/// @brief Map a vector +/// @param vec pointer to the vector +/// @param func function to map the vector +/// @return pointer to the new vector +/// @note the new vector is allocated and must be freed +/// @note as vec is an array under the hood, +// the new vector will be a collection of adress. +/// therefore, if your function changes the value of the data, +// the new vector will be a collection of pointers to the +// data of the old vector. If you want to do that and change the +// value of the data, use ft_vec_apply instead. +t_vector *ft_vec_map(t_vector *vec, t_data_tr func); + +// ft_vector/ft_vec_new.c + +/// @brief Create a new vector +/// @return pointer to the new vector +t_vector *ft_vec_new(void); + +// ft_vector/ft_vec_pop.c + +/// @brief Pop an element from the vector's end +/// @param vec pointer to the vector +/// @return pointer to the element +void *ft_vec_pop(t_vector *vec); + +/// @brief Create a new vector with a given capacity +/// @param n capacity of the new vector +/// @return pointer to the new vector +t_vector *ft_vec_from_size(size_t n); + +/// @brief Create a new vector from an array +/// @param data data to add to the vector +/// @param count count of the data +/// @return pointer to the new vector +t_vector *ft_vec_from_array(void **data, size_t count); + +/// @brief Create a new vector from an array and steals ownership of the array +/// @param data data to create the vector from +/// @param count count of the data array +/// @return pointer to the new vector +t_vector *ft_vec_convert_alloccarray(void **data, size_t count); + +// ft_vector/ft_vec_remove.c + +/// @brief Remove an element from the vector +/// @param vector vector to remove the element from +/// @param n index of the element to remove +/// @param del function to delete the elements data +void ft_vec_remove(t_vector *vector, size_t n, t_data_apply del); + +/// @brief Remove an element from the vector +/// @param vector vector to remove the element from +/// @param func function to remove the element +/// @param del function to delete the elements data +/// @return void +/// @note Similar to ft_vec_filter +void ft_vec_remove_if(t_vector *vector, t_data_is func, + t_data_apply del); + +// ft_vector/ft_vec_reserve.c + +/// @brief Reserve a given size for the vector +/// @param vec pointer to the vector +/// @param size size to reserve +/// @return true if the vector was reserved or size is smaller +/// than the current cappacity, false otherwise +bool ft_vec_reserve(t_vector **vec, size_t size); + +// ft_vector/ft_vec_reverse.c + +/// @brief Reverse a vector +/// @param vector vector to reverse +/// @return void +void ft_vec_reverse(t_vector *vector); + +// ft_vector/ft_vec_shift.c + +/// @brief Shift a vector +/// @param vec pointer to the vector +/// @param start index to start the shift +/// @param shift number of elements to shift +/// @return void +void ft_vec_shift(t_vector *vec, size_t start, size_t shift); + +// ft_vector/ft_vec_sort.c + +/// @brief Sort a vector +/// @param vec pointer to the vector +/// @param cmp_f function to compare the elements +/// @return void +void ft_vec_sort(t_vector *vec, t_data_cmp cmp_f); + +// ft_vector/ft_vec_shrink.c + +/// @brief Shrink a vector +/// @param vec pointer to the vector +/// @return true if the vector was shrunk, false otherwise +bool ft_vec_shrink(t_vector *vec); + +// ft_vector/ft_vec_swap.c + +/// @brief Swap two elements of a vector +/// @param vec pointer to the vector +/// @param a index of the first element +/// @param b index of the second element +/// @return void +void ft_vec_swap(t_vector *vec, size_t a, size_t b); + +// ft_vector/ft_vec_to_array.c + +/// @brief Convert a vector to an array +/// @param vec pointer to the vector +/// @return pointer to the array +/// @note the array is allocated and must be freed but the vector is freed +void **ft_vec_to_array(t_vector **vec); + +#endif /* FT_VECTOR_H */ diff --git a/libft_personal/include/ft_vector_types.h b/libft_personal/include/ft_vector_types.h new file mode 100644 index 0000000..f3e9bc6 --- /dev/null +++ b/libft_personal/include/ft_vector_types.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vector_types.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 15:13:10 by bgoulard #+# #+# */ +/* Updated: 2023/12/30 13:20:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_VECTOR_TYPES_H +# define FT_VECTOR_TYPES_H + +# ifndef FT_VECTOR_BASE_LEN +# ifdef TEST +# define FT_VECTOR_BASE_LEN 5 +# else +# define FT_VECTOR_BASE_LEN 4096 +# endif +# endif + +# include +# include + +/// @brief vector structure +/// @param datas array of pointers to the elements +/// @param count number of elements in the vector +/// @param cappacity number of elements that can be stored in the vector +typedef struct s_vector +{ + void **datas; + size_t count; + size_t cappacity; +} t_vector; + +#endif /* FT_VECTOR_TYPES_H */ \ No newline at end of file diff --git a/libft_personal/include/internal/args_helper.h b/libft_personal/include/internal/args_helper.h new file mode 100644 index 0000000..003c593 --- /dev/null +++ b/libft_personal/include/internal/args_helper.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args_helper.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 17:47:15 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:48:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARGS_HELPER_H +# define ARGS_HELPER_H + +# include "ft_args_types.h" +# include +# include + +// Parser opt +// +// Parse long option +ssize_t parse_long_opt(const char *str_op, const t_opt *opt_list); +// Parse short option +ssize_t parse_short_opt(const char *str_op, const t_opt *opt_list); + +// Checker +// +// Check if the argument is valid +int checker_arg(t_opt_type type, const char *arg); + +// Run +// +// Run the function associated with the option +int run_opt_func(const t_opt opt, void *usr_control_struct, \ + const char **arg, int *i); + +// Error +// +// Print error message for option +int arg_opt_err(const char *opt); +// Print error message for argument type +int arg_type_err(const t_opt opt, const char *arg); + +#endif diff --git a/libft_personal/include/tests/args_tests.h b/libft_personal/include/tests/args_tests.h new file mode 100644 index 0000000..c4a981a --- /dev/null +++ b/libft_personal/include/tests/args_tests.h @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:39:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 18:33:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ARGS_TESTS_H +# define ARGS_TESTS_H + +/* @file: tests/ft_args/args_tests.c */ +int parse_args_test(void); +int tests_args(void); + +/* @file: tests/ft_args/tests_custom_checker.c */ +int getset_custom_checker_test(void); + +/* @file: tests/ft_args/tests_optlist.c */ +int getset_opt_list_test(void); + +/* @file: tests/ft_args/tests_setup_prog.c */ +int tests_setup_prog(void); + +/* @file: tests/ft_args/tests_version.c */ +int getset_version_test(void); + +/* @file: tests/ft_args/tests_progname.c */ +int getset_program_name_test(void); + +#endif /* ARGS_TESTS_H */ diff --git a/libft_personal/include/tests/char_tests.h b/libft_personal/include/tests/char_tests.h new file mode 100644 index 0000000..5abd06e --- /dev/null +++ b/libft_personal/include/tests/char_tests.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* char_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:39:35 by bgoulard #+# #+# */ +/* Updated: 2024/05/26 12:28:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CHAR_TESTS_H +# define CHAR_TESTS_H + +/* @file: tests/ft_string/ft_char/test_tolower.c */ +int test_ft_tolower(void); + +/* @file: tests/ft_string/ft_char/test_isalpha.c */ +int test_ft_isalpha(void); + +/* @file: tests/ft_string/ft_char/test_isalnum.c */ +int test_ft_isalnum(void); + +/* @file: tests/ft_string/ft_char/test_puchar.c */ +int test_ft_putchar(void); + +/* @file: tests/ft_string/ft_char/test_ishexdigit.c */ +int test_ft_ishexdigit(void); + +/* @file: tests/ft_string/ft_char/test_isspace.c */ +int test_ft_isspace(void); + +/* @file: tests/ft_string/ft_char/test_isoctdigit.c */ +int test_ft_isoctdigit(void); + +/* @file: tests/ft_string/ft_char/test_isprint.c */ +int test_ft_isprint(void); + +/* @file: tests/ft_string/ft_char/test_toupper.c */ +int test_ft_toupper(void); + +/* @file: tests/ft_string/ft_char/test_isascii.c */ +int test_ft_isascii(void); + +/* @file: tests/ft_string/ft_char/ft_char_tests.c */ +int char_tests(void); + +/* @file: tests/ft_string/ft_char/test_isdigit.c */ +int test_ft_isdigit(void); + +#endif /* CHAR_TESTS_H */ diff --git a/libft_personal/include/tests/dl_tests.h b/libft_personal/include/tests/dl_tests.h new file mode 100644 index 0000000..520cdec --- /dev/null +++ b/libft_personal/include/tests/dl_tests.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dl_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:51:11 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:52:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DL_TESTS_H +# define DL_TESTS_H + +int t_dl_add_front(void); +int t_dl_add_back(void); + +int t_dl_apply(void); +int t_dl_apply_range(void); +int t_dl_apply_range_node(void); + +int t_dl_clear(void); +int t_dl_clear_range(void); + +int t_dl_create(void); + +int t_dl_copy_node(void); +int t_dl_copy_list(void); + +int t_dl_delete_self(void); +int t_dl_delete_range(void); +int t_dl_delete(void); + +int t_dl_find(void); + +int t_dl_get_datas(void); +int t_dl_get_nodes(void); + +int t_dl_at(void); +int t_dl_begin(void); +int t_dl_end(void); + +int t_dl_map(void); + +int t_dl_new(void); + +int t_dl_pop(void); +int t_dl_pop_back(void); +int t_dl_push(void); +int t_dl_push_back(void); + +int t_dl_rev(void); + +int t_dl_size(void); +int t_dl_size_of_data(void); + +int t_dl_subrange(void); + +#endif /* DL_TESTS_H */ diff --git a/libft_personal/include/tests/lists_test_utils.h b/libft_personal/include/tests/lists_test_utils.h new file mode 100644 index 0000000..70a5e9e --- /dev/null +++ b/libft_personal/include/tests/lists_test_utils.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lists_test_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 13:30:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:42:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LISTS_TEST_UTILS_H +# define LISTS_TEST_UTILS_H + +# include "ft_list_types.h" +# include + +void create_2elem_dlist(t_dlist **list, void **data1, void **data2); +void create_2elem_list(t_list **list, void **data1, void **data2); + +#endif diff --git a/libft_personal/include/tests/ll_tests.h b/libft_personal/include/tests/ll_tests.h new file mode 100644 index 0000000..1b35186 --- /dev/null +++ b/libft_personal/include/tests/ll_tests.h @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ll_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:10:07 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:11:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LL_TESTS_H +# define LL_TESTS_H + +int t_ll_add_front(void); +int t_ll_add_back(void); + +int t_ll_apply(void); +int t_ll_apply_range(void); +int t_ll_apply_range_node(void); + +int t_ll_clear(void); + +int t_ll_create(void); + +int t_ll_copy_node(void); +int t_ll_copy_list(void); + +int t_ll_delone(void); +int t_ll_delete_range(void); + +int t_ll_find(void); + +int t_ll_get_datas(void); +int t_ll_get_nodes(void); + +int t_ll_end(void); +int t_ll_at(void); + +int t_ll_map(void); + +int t_ll_new(void); + +int t_ll_push(void); +int t_ll_push_back(void); +int t_ll_pop(void); +int t_ll_pop_back(void); + +int t_ll_rev(void); + +int t_ll_size(void); +int t_ll_size_match(void); + +int t_ll_subrange(void); + +#endif /* LL_TESTS_H */ diff --git a/libft_personal/include/tests/map_tests.h b/libft_personal/include/tests/map_tests.h new file mode 100644 index 0000000..81b757d --- /dev/null +++ b/libft_personal/include/tests/map_tests.h @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:23:29 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 18:24:01 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MAP_TESTS_H +# define MAP_TESTS_H + +// File: tests/ft_map/tests_map_cappacity.c +int test_map_capacity(void); + +// File: tests/ft_map/tests_map_clear.c +int test_map_clear(void); + +// File: tests/ft_map/tests_map_create.c +int test_map_create(void); + +// File: tests/ft_map/tests_map_destroy.c +int test_map_destroy(void); +int test_map_destroy_free(void); + +// File: tests/ft_map/tests_map_get.c +int test_map_get(void); + +// File: tests/ft_map/tests_map_hash.c +int test_map_hash(void); + +// File: tests/ft_map/tests_map_remove.c +int test_map_remove(void); + +// File: tests/ft_map/tests_map_set.c +int test_map_set(void); + +// File: tests/ft_map/tests_map_set_cmphash.c +int test_map_set_cmp(void); +int test_map_set_hash(void); + +// File: tests/ft_map/tests_map_size.c +int test_map_size(void); + +#endif /* MAP_TESTS_H */ diff --git a/libft_personal/include/tests/math_tests.h b/libft_personal/include/tests/math_tests.h new file mode 100644 index 0000000..ac88321 --- /dev/null +++ b/libft_personal/include/tests/math_tests.h @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* math_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:17:37 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 20:51:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MATH_TESTS_H +# define MATH_TESTS_H + +/* @file: tests/ft_math/tests_complex.c */ +int test_ft_complex_abs(void); +int test_ft_complex_add(void); +int test_ft_complex_addl(void); +int test_ft_complex_mull(void); +int test_ft_complex_muld(void); + +/* @file: tests/ft_math/tests_log.c */ +int test_ft_log(void); +int test_ft_llogof(void); +int test_ft_ullogof(void); +int test_ft_logof(void); + +/* @file: tests/ft_math/tests_intrange.c */ +int test_ft_range(void); +int test_ft_range_f(void); +int test_ft_range_d(void); + +/* @file: tests/ft_math/tests_sqrt.c */ +int test_ft_sqrt(void); + +/* @file: tests/ft_math/tests_minmax.c */ +int test_ft_min(void); +int test_ft_max(void); + +/* @file: tests/ft_math/tests_pow.c */ +int test_ft_pow(void); + +/* @file: tests/ft_math/tests_round.c */ +int test_ft_round(void); + +/* @file: tests/ft_math/tests_clamp.c */ +int test_ft_clamp(void); +int test_ft_clamp_f(void); +int test_ft_clamp_d(void); + +/* @file: tests/ft_math/tests_abs.c */ +int test_ft_abs(void); + +/* @file: tests/ft_math/tests_align.c */ +int test_ft_align_2(void); +int test_ft_align(void); + +#endif diff --git a/libft_personal/include/tests/optional_test.h b/libft_personal/include/tests/optional_test.h new file mode 100644 index 0000000..4d07ecf --- /dev/null +++ b/libft_personal/include/tests/optional_test.h @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* optional_test.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:39:46 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 12:08:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef OPTIONAL_TEST_H +# define OPTIONAL_TEST_H + +/* @file: tests/ft_optional/test_optional_new.c */ +int test_optional_new(void); + +/* @file: tests/ft_optional/test_optional_dup.c */ +int test_optional_dup(void); + +/* @file: tests/ft_optional/test_optional_copy.c */ +int test_optional_copy(void); + +/* @file: tests/ft_optional/test_optional_unwrap.c */ +int test_optional_unwrap(void); + +/* @file: tests/ft_optional/test_optional_from_val.c */ +int test_optional_from_val(void); + +/* @file: tests/ft_optional/test_optional_chain.c */ +int test_optional_chain(void); + +/* @file: tests/ft_optional/optional_tests.c */ +void *add_4(void *val); +int tests_optional(void); + +/* @file: tests/ft_optional/test_optional_map.c */ +int test_optional_map(void); + +/* @file: tests/ft_optional/test_optional_destroy.c */ +int test_optional_destroy(void); + +#endif /* INCLUDE/TESTS/OPTIONAL_TEST_H */ diff --git a/libft_personal/include/tests/pair_tests.h b/libft_personal/include/tests/pair_tests.h new file mode 100644 index 0000000..af2d5b4 --- /dev/null +++ b/libft_personal/include/tests/pair_tests.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pair_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/24 00:30:00 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:11:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PAIR_TESTS_H +# define PAIR_TESTS_H + +int tests_pair_destroy(void); +int test_pair_set(void); +int test_pair_new(void); +int test_pair_second(void); +int test_pair_first(void); +int test_pair_destroy(void); +int test_pair_cmp(void); +int test_pair_cmp_first(void); +int test_pair_cmp_second(void); + +#endif /* PAIR_TESTS_H */ diff --git a/libft_personal/include/tests/str__mem_tests.h b/libft_personal/include/tests/str__mem_tests.h new file mode 100644 index 0000000..f5b6181 --- /dev/null +++ b/libft_personal/include/tests/str__mem_tests.h @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str__mem_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:26:09 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 12:40:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR__MEM_TESTS_H +# define STR__MEM_TESTS_H + +/// @file: tests/ft_string/ft_mem/tests_apply_2d +int test_apply_2d(void); + +/// @file: tests/ft_string/ft_mem/tests_bzero.c +int test_bzero(void); + +/// @file: tests/ft_string/ft_mem/tests_calloc.c +int test_calloc(void); + +/// @file: tests/ft_string/ft_mem/tests_fd_to_buff.c +int test_fd_to_buff(void); + +/// @file: tests/ft_string/ft_mem/tests_free.c +int test_free(void); + +/// @file: tests/ft_string/ft_mem/tests_free_2d.c +int test_free_2d(void); + +/// @file: tests/ft_string/ft_mem/tests_len_2d.c +int test_len_2d(void); + +/// @file: tests/ft_string/ft_mem/tests_memchr.c +int test_memchr(void); + +/// @file: tests/ft_string/ft_mem/tests_memcmp.c +int test_memcmp(void); + +/// @file: tests/ft_string/ft_mem/tests_memcpy.c +int test_memcpy(void); + +/// @file: tests/ft_string/ft_mem/tests_memmap.c +int test_memmap(void); + +/// @file: tests/ft_string/ft_mem/tests_memmove.c +int test_memmove(void); + +/// @file: tests/ft_string/ft_mem/tests_memset.c +int test_memset(void); + +/// @file: tests/ft_string/ft_mem/tests_qsort.c +int test_qsort(void); + +/// @file: tests/ft_string/ft_mem/tests_realloc.c +int test_realloc(void); + +/// @file: tests/ft_string/ft_mem/tests_swap.c +int test_swap(void); + +#endif /* STR__MEM_TESTS_H */ diff --git a/libft_personal/include/tests/str__str_tests.h b/libft_personal/include/tests/str__str_tests.h new file mode 100644 index 0000000..6549608 --- /dev/null +++ b/libft_personal/include/tests/str__str_tests.h @@ -0,0 +1,169 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str__str_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:28:40 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 11:42:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR__STR_TESTS_H +# define STR__STR_TESTS_H + +/// @brief test_striteri +/// @file: tests/ft_string/ft_str/test_striteri.c +int test_striteri(void); + +/// @brief test_utoa +/// @file: tests/ft_string/ft_str/test_utoa.c +int test_utoa(void); + +/// @brief test_strdup +/// @file: tests/ft_string/ft_str/test_strdup.c +int test_strdup(void); + +/// @brief test_strnstr +/// @file: tests/ft_string/ft_str/test_strnstr.c +int test_strnstr(void); + +/// @brief test_strrchr +/// @file: tests/ft_string/ft_str/test_strrchr.c +int test_strrchr(void); + +/// @brief test_strtrim +/// @file: tests/ft_string/ft_str/test_strtrim.c +int test_strtrim(void); + +/// @brief test_atoi +/// @file: tests/ft_string/ft_str/test_atoi.c +int test_atoi(void); + +/// @brief test_strlcpy +/// @file: tests/ft_string/ft_str/test_strlcpy.c +int test_strlcpy(void); + +/// @brief test_shift_args +/// @file: tests/ft_string/ft_str/test_shift_args.c +int test_shift_args(void); + +/// @brief str_tests +/// @file: tests/ft_string/ft_str/str_tests.c +int str_tests(void); + +/// @brief test_itoa_base +/// @file: tests/ft_string/ft_str/test_itoa_base.c +int test_itoa_base(void); + +/// @brief test_putendl +/// @file: tests/ft_string/ft_str/test_putendl.c +int test_putendl(void); + +/// @brief test_strtok +/// @file: tests/ft_string/ft_str/test_strtok.c +int test_strtok(void); + +/// @brief test_strmapi +/// @file: tests/ft_string/ft_str/test_strmapi.c +int test_strmapi(void); + +/// @brief test_strndup +/// @file: tests/ft_string/ft_str/test_strndup.c +int test_strndup(void); + +/// @brief test_putstr +/// @file: tests/ft_string/ft_str/test_putstr.c +int test_putstr(void); + +/// @brief test_strncmp +/// @file: tests/ft_string/ft_str/test_strncmp.c +int test_strncmp(void); + +/// @brief test_strlcat +/// @file: tests/ft_string/ft_str/test_strlcat.c +int test_strlcat(void); + +/// @brief test_atof +/// @file: tests/ft_string/ft_str/test_atof.c +int test_atof(void); + +/// @brief test_itoa +/// @file: tests/ft_string/ft_str/test_itoa.c +int test_itoa(void); + +/// @brief tests_splits +/// @file: tests/ft_string/ft_str/test_splits.c +int tests_splits(void); + +/// @brief test_str_replace +/// @file: tests/ft_string/ft_str/test_str_replace.c +int test_str_replace(void); + +/// @brief test_atoi_base +/// @file: tests/ft_string/ft_str/test_atoi_base.c +int test_atoi_base(void); + +/// @brief test_str_replace_chr +/// @file: tests/ft_string/ft_str/test_str_replace_chr.c +int test_str_replace_chr(void); + +/// @brief test_putnbr +/// @file: tests/ft_string/ft_str/test_putnbr.c +int test_putnbr(void); + +/// @brief test_strjoin +/// @file: tests/ft_string/ft_str/test_strjoin.c +int test_strjoin(void); + +/// @brief test_strcmp +/// @file: tests/ft_string/ft_str/test_strcmp.c +int test_strcmp(void); + +/// @brief test_substr +/// @file: tests/ft_string/ft_str/test_substr.c +int test_substr(void); + +/// @brief test_gnl +/// @file: tests/ft_string/ft_str/test_gnl.c +int test_gnl(void); + +/// @brief test_strlen +/// @file: tests/ft_string/ft_str/test_strlen.c +int test_strlen(void); + +/// @brief test_split +/// @file: tests/ft_string/ft_str/test_split.c +int test_split(void); + +/// @brief test_strchr +/// @file: tests/ft_string/ft_str/test_strchr.c +int test_strchr(void); + +/// @brief test_str_isbool +/// @file: tests/ft_string/ft_str/test_str_isbool.c +int test_str_isbool(void); + +/// @brief test_str_isalpha +/// @file: tests/ft_string/ft_str/test_str_isalpha.c +int test_str_isalpha(void); +int test_str_isdouble(void); +int test_str_isalnum(void); +int test_str_isdigit(void); +int test_str_ishex(void); +int test_str_isfloat(void); +int test_str_isint(void); +int test_str_islong(void); +int test_str_isnum(void); +int test_str_isoct(void); +int test_str_isvalid(void); +int test_strclen(void); +int test_strcnb(void); +int test_strcspn(void); +int test_strspn(void); +int test_strend_with(void); +int test_strstart_with(void); +int test_strappend_c(void); + +#endif /* STR__STR_TESTS_H */ diff --git a/libft_personal/include/tests/str__t_str_test.h b/libft_personal/include/tests/str__t_str_test.h new file mode 100644 index 0000000..2d310f1 --- /dev/null +++ b/libft_personal/include/tests/str__t_str_test.h @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str__t_str_test.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:42:08 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 13:33:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR__T_STR_TEST_H +# define STR__T_STR_TEST_H + +int test_string_append_c(void); +int test_string_append_n(void); +int test_string_append_sn(void); +int test_string_append_s(void); +int test_string_append(void); +int test_string_cap(void); +int test_string_chr(void); +int test_string_clear(void); +int test_string_cmp_str(void); +int test_string_cmp(void); +int test_string_destroy(void); +int test_string_from_c(void); +int test_string_from_n(void); +int test_string_from_sn(void); +int test_string_from_s(void); +int test_string_from(void); +int test_string_get(void); +int test_string_insert_c(void); +int test_string_insert_n(void); +int test_string_insert_sn(void); +int test_string_insert_s(void); +int test_string_insert(void); +int test_string_len(void); +int test_string_ncmp_str(void); +int test_string_ncmp(void); +int test_string_new(void); +int test_string_offset(void); +int test_string_put(void); +int test_string_rchr(void); +int test_string_replace_chr(void); +int test_string_replace(void); +int test_string_reserve(void); +int test_string_resize(void); +int test_string_roffset(void); +int test_string_set_inplace(void); +int test_string_set_n(void); +int test_string_set(void); +int test_string_shrink(void); +int test_string_substr(void); +int test_string_to_str(void); +int test_string_trim_chr(void); +int test_string_trimstr(void); +int test_string_trim(void); + +#endif /* STR__T_STR_TEST_H */ diff --git a/libft_personal/include/tests/tests.h b/libft_personal/include/tests/tests.h new file mode 100644 index 0000000..93738ee --- /dev/null +++ b/libft_personal/include/tests/tests.h @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/29 11:15:14 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 16:23:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TESTS_H +# define TESTS_H + +# include +# include +# include +# include +# include +# define TESTS_FPREFIX "build/test_" + +# ifndef FORK_TESTS +# define FORK_TESTS 1 +# endif + +typedef struct s_test +{ + char *name; + int (*test)(void); +} t_test; + +int run_test(const t_test *test, int *collect); +int open_test_file(char **func_to_test); +void destroy_test_file(int fd, const char *file); + +int tests_args(void); +int tests_doubly_linked_list_all(void); +int tests_linked_list_all(void); +int tests_map(void); +int tests_math(void); +int tests_optional(void); +int tests_pair(void); +int tests_string(void); +int tests_vector(void); + +#endif /* TESTS_H */ diff --git a/libft_personal/include/tests/tests_lambda_functions.h b/libft_personal/include/tests/tests_lambda_functions.h new file mode 100644 index 0000000..d7bb2d5 --- /dev/null +++ b/libft_personal/include/tests/tests_lambda_functions.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_lambda_functions.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:41:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 14:05:47 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef TESTS_LAMBDA_FUNCTIONS_H +# define TESTS_LAMBDA_FUNCTIONS_H +# include + +// File: tests/lambda_functions.h +bool is42(const void *data); +void add42(void *data); +void *add42_ret(const void *data); +int cmp_int(const void *a, const void *b); +void **creat_tb(void); + +#endif diff --git a/libft_personal/include/tests/vector_tests.h b/libft_personal/include/tests/vector_tests.h new file mode 100644 index 0000000..0547517 --- /dev/null +++ b/libft_personal/include/tests/vector_tests.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector_tests.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:10:06 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 11:33:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VECTOR_TESTS_H +# define VECTOR_TESTS_H + +// File: tests/ft_vector/vector_tests.c +int test_vec_add(void); +int test_vec_apply(void); +int test_vec_at(void); +int test_vec_cat(void); +int test_vec_clear(void); +int test_vec_convert_alloc_array(void); +int test_vec_destroy(void); +int test_vec_filter(void); +int test_vec_from_array(void); +int test_vec_from_size(void); +int test_vec_get(void); +int test_vec_map(void); +int test_vec_new(void); +int test_vec_pop(void); +int test_vec_remove(void); +int test_vec_remove_if(void); +int test_vec_reserve(void); +int test_vec_reverse(void); +int test_vec_shift(void); +int test_vec_shrink(void); +int test_vec_sort(void); +int test_vec_swap(void); +int test_vec_to_array(void); +int tests_vector(void); + +#endif /* VECTOR_TESTS_H */ diff --git a/libft_personal/src/ft_args/ft_arg_custom_checker.c b/libft_personal/src/ft_args/ft_arg_custom_checker.c new file mode 100644 index 0000000..9242bee --- /dev/null +++ b/libft_personal/src/ft_args/ft_arg_custom_checker.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arg_custom_checker.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 14:13:26 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 14:10:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include + +static t_data_is singleton_custom_checker(t_data_is custom_checker) +{ + static t_data_is custom_checker_ptr = NULL; + + if (custom_checker) + custom_checker_ptr = custom_checker; + return (custom_checker_ptr); +} + +void ft_arg_set_custom_checker(t_data_is custom_checker) +{ + singleton_custom_checker(custom_checker); +} + +t_data_is ft_arg_get_custom_checker(void) +{ + return (singleton_custom_checker(NULL)); +} diff --git a/libft_personal/src/ft_args/ft_parse_args.c b/libft_personal/src/ft_args/ft_parse_args.c new file mode 100644 index 0000000..0eed9db --- /dev/null +++ b/libft_personal/src/ft_args/ft_parse_args.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/14 01:10:29 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 10:28:11 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_args_types.h" +#include "ft_string.h" +#include "internal/args_helper.h" +#include +#include +#include + +int run_opt_func(const t_opt opt, void *usr_control_struct, const char **arg, \ + int *i) +{ + const char *arg_ptr; + void (*func_arg)(void *, const char *); + void (*func_no_arg)(void *); + + func_arg = opt.func; + func_no_arg = opt.func; + if (opt.type & OPT_ARG && opt.type & OPT_EQSIGN) + arg_ptr = ft_strchr(arg[*i], '=') + 1; + else if (opt.type & OPT_ARG) + arg_ptr = arg[*i + 1]; + else + arg_ptr = NULL; + if (checker_arg(opt.type, arg_ptr) != 0) + return (arg_type_err(opt, arg_ptr)); + if (opt.type & OPT_ARG) + func_arg(usr_control_struct, arg_ptr); + else + func_no_arg(usr_control_struct); + *i += 1 + ((opt.type != 0) && (opt.type & OPT_EQSIGN) == 0); + return (EXIT_SUCCESS); +} + +int ft_parse_args(const char **argv, void *usr_control_struct) +{ + const t_opt *opt; + ssize_t opt_index; + int i; + + opt = ft_get_opt_list(); + i = 1; + while (argv[i] && argv[i][0] == '-') + { + if (argv[i][1] == '-' && argv[i][2] == '\0') + return (i + 1); + else if (argv[i][1] == '-') + opt_index = parse_long_opt(argv[i] + 2, opt); + else + opt_index = parse_short_opt(argv[i] + 1, opt); + if (opt_index == -1) + return (arg_opt_err(argv[i])); + if (run_opt_func(opt[opt_index], usr_control_struct, argv, &i) != 0) + return (-1); + } + return (i); +} diff --git a/libft_personal/src/ft_args/ft_parse_err.c b/libft_personal/src/ft_args/ft_parse_err.c new file mode 100644 index 0000000..b90962f --- /dev/null +++ b/libft_personal/src/ft_args/ft_parse_err.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_err.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:23:49 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 18:22:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args_types.h" +#include "ft_args.h" +#include "ft_char.h" +#include "ft_string.h" +#include +#include + +// 0 success, !0 failure +int checker_arg(t_opt_type type, const char *arg) +{ + if (!arg || arg == (char *)0x1) + return (type); + if ((type & ARG_MASK_ATYPE) == OPT_INT) + return (!ft_str_isint(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_STRING) + return (EXIT_SUCCESS); + else if ((type & ARG_MASK_ATYPE) == OPT_BOOL) + return (!ft_str_isbool(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_FLOAT) + return (!ft_str_isfloat(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_LONG) + return (!ft_str_islong(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_DOUBLE) + return (!ft_str_isdouble(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_ALPHANUM) + return (!ft_str_isvalid(arg, ft_isalnum)); + else if ((type & ARG_MASK_ATYPE) == OPT_HEX) + return (!ft_str_ishex(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_OCT) + return (!ft_str_isoct(arg)); + else if ((type & ARG_MASK_ATYPE) == OPT_OTHER + && ft_arg_get_custom_checker()) + return (!ft_arg_get_custom_checker()(arg)); + return (EXIT_SUCCESS); +} + +int arg_type_err(t_opt opt, const char *arg) +{ + const char *progname; + + progname = ft_progname(); + if (progname) + { + ft_putstr_fd(progname, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + } + ft_putstr_fd("Error: invalid argument for option: ", STDERR_FILENO); + if (opt.long_name) + ft_putstr_fd(opt.long_name, STDERR_FILENO); + else + ft_putchar_fd(opt.short_name, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + if (!arg || arg == (char *)1) + ft_putstr_fd("NULL", STDERR_FILENO); + else + ft_putstr_fd(arg, STDERR_FILENO); + ft_putstr_fd("\n", STDERR_FILENO); + return (EXIT_FAILURE); +} + +int arg_opt_err(const char *arg) +{ + const char *progname; + + progname = ft_progname(); + if (progname) + { + ft_putstr_fd(progname, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + } + ft_putstr_fd("Error: invalid option: ", STDERR_FILENO); + ft_putstr_fd(arg, STDERR_FILENO); + ft_putstr_fd("\n", STDERR_FILENO); + return (EXIT_FAILURE); +} diff --git a/libft_personal/src/ft_args/ft_parse_opt.c b/libft_personal/src/ft_args/ft_parse_opt.c new file mode 100644 index 0000000..477e75d --- /dev/null +++ b/libft_personal/src/ft_args/ft_parse_opt.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_opt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 14:06:00 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 17:57:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args_types.h" +#include "ft_math.h" +#include "ft_string.h" +#include + +ssize_t parse_short_opt(char *str_op, const t_opt *opt_list) +{ + ssize_t i; + + i = 0; + while (opt_list[i].func) + { + if (opt_list[i].short_name && \ + opt_list[i].short_name == str_op[0]) + return (i); + i++; + } + return (-1); +} + +ssize_t parse_long_opt(char *str_op, const t_opt *opt_list) +{ + ssize_t i; + ssize_t offset; + + i = 0; + offset = 0; + if (ft_strchr(str_op, '=')) + offset = ft_strchr(str_op, '=') - str_op; + while (opt_list[i].func) + { + if (opt_list[i].long_name && ft_strncmp(opt_list[i].long_name, str_op, \ + ft_max(offset, ft_strlen(opt_list[i].long_name))) == 0) + return (i); + i++; + } + return (-1); +} diff --git a/libft_personal/src/ft_args/ft_progname.c b/libft_personal/src/ft_args/ft_progname.c new file mode 100644 index 0000000..f22781b --- /dev/null +++ b/libft_personal/src/ft_args/ft_progname.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_progname.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/14 00:00:49 by bgoulard #+# #+# */ +/* Updated: 2024/04/21 14:44:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include + +static const char *singleton_progname(const char *progname) +{ + static const char *singleton_progname = NULL; + + if (progname) + singleton_progname = progname; + return (singleton_progname); +} + +void ft_set_progname(const char *progname) +{ + if (progname) + singleton_progname(progname); + else + singleton_progname("a.out"); +} + +const char *ft_progname(void) +{ + return (singleton_progname(NULL)); +} diff --git a/libft_personal/src/ft_args/ft_set_opt_args.c b/libft_personal/src/ft_args/ft_set_opt_args.c new file mode 100644 index 0000000..ef8a8b4 --- /dev/null +++ b/libft_personal/src/ft_args/ft_set_opt_args.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_set_opt_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/14 00:01:33 by bgoulard #+# #+# */ +/* Updated: 2024/04/21 14:44:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_args_types.h" +#include + +static const t_opt *singleton_opt_list(const t_opt *opt_list) +{ + static const t_opt *singleton_opt_list = NULL; + + if (opt_list) + singleton_opt_list = opt_list; + return (singleton_opt_list); +} + +void ft_set_opt_list(const t_opt *opt_list) +{ + if (opt_list) + singleton_opt_list(opt_list); +} + +const t_opt *ft_get_opt_list(void) +{ + return (singleton_opt_list(NULL)); +} diff --git a/libft_personal/src/ft_args/ft_setup_prog.c b/libft_personal/src/ft_args/ft_setup_prog.c new file mode 100644 index 0000000..1bd506e --- /dev/null +++ b/libft_personal/src/ft_args/ft_setup_prog.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_setup_prog.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 11:31:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 00:47:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" + +void ft_setup_prog(const char *const *av) +{ + ft_set_progname(av[0]); + ft_set_version(VERSION); +} diff --git a/libft_personal/src/ft_args/ft_version.c b/libft_personal/src/ft_args/ft_version.c new file mode 100644 index 0000000..fdb6489 --- /dev/null +++ b/libft_personal/src/ft_args/ft_version.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_version.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/13 23:57:19 by bgoulard #+# #+# */ +/* Updated: 2024/04/21 14:45:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include + +static const char *singleton_version(const char *version) +{ + static const char *singleton_version = NULL; + + if (version) + singleton_version = version; + return (singleton_version); +} + +void ft_set_version(const char *version) +{ + if (version) + singleton_version(version); + else + singleton_version(VERSION); + return ; +} + +const char *ft_progversion(void) +{ + return (singleton_version(NULL)); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_add.c b/libft_personal/src/ft_list/ft_dl/ft_dl_add.c new file mode 100644 index 0000000..4b5d3f5 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_add.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:17:48 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void ft_dl_add_back(t_dlist **head, t_dlist *const added) +{ + t_dlist *last; + + if (!head || !added) + return ; + if (!*head) + return (ft_dl_add_front(head, added)); + last = ft_dl_end(*head); + last->next = added; + added->prev = last; +} + +void ft_dl_add_front(t_dlist **head, t_dlist *const added) +{ + if (!head || !added) + return ; + if (!*head) + return ((void)(*head = added)); + added->next = *head; + (*head)->prev = added; + *head = added; +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_apply.c b/libft_personal/src/ft_list/ft_dl/ft_dl_apply.c new file mode 100644 index 0000000..7a6b448 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_apply.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 18:59:42 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:59:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +size_t ft_dl_apply(const t_dlist *start, t_data_apply applied) +{ + return (ft_dl_apply_range(start, NULL, applied)); +} + +size_t ft_dl_apply_range(const t_dlist *start, const t_dlist *end, + t_data_apply applied) +{ + size_t i; + t_dlist *it; + + it = (t_dlist *)start; + i = 0; + while (it != end) + { + applied(it->data); + it = it->next; + i++; + } + return (i); +} + +size_t ft_dl_apply_range_node(const t_dlist *start, const t_dlist *end, + t_dnode_apply applied) +{ + size_t i; + t_dlist *it; + + it = (t_dlist *)start; + i = 0; + while (it != end) + { + applied(it); + it = it->next; + i++; + } + return (i); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_clear.c b/libft_personal/src/ft_list/ft_dl/ft_dl_clear.c new file mode 100644 index 0000000..fc78182 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_clear.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:33:59 by iron #+# #+# */ +/* Updated: 2024/06/23 18:59:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +size_t ft_dl_clear(t_dlist **head, t_data_apply del) +{ + size_t ret; + + ret = ft_dl_delete_range(*head, NULL, del); + *head = NULL; + return (ret); +} + +size_t ft_dl_clear_range(t_dlist *start, t_dlist *end, t_data_apply del) +{ + size_t i; + + i = 0; + if (!start) + return (0); + while (start != end) + { + if (del) + del(start->data); + start->data = NULL; + start = start->next; + i++; + } + return (i); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_create.c b/libft_personal/src/ft_list/ft_dl/ft_dl_create.c new file mode 100644 index 0000000..173bcd8 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_create.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:05 by iron #+# #+# */ +/* Updated: 2024/06/23 18:59:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_create(const void *data) +{ + t_dlist *ret; + + ret = ft_dl_new(); + if (!ret) + return (ret); + ret->data = (void *)data; + return (ret); +} + +t_dlist *ft_dl_copy_node(const t_dlist *const other) +{ + t_dlist *ret; + + ret = ft_dl_new(); + if (!ret) + return (ret); + ret->data = (void *)other->data; + ret->next = (t_dlist *)other->next; + ret->prev = (t_dlist *)other->prev; + return (ret); +} + +t_dlist *ft_dl_copy_list(const t_dlist *const other) +{ + t_dlist *node; + t_dlist *head; + t_dlist *prev; + t_dlist *it; + + it = (t_dlist *)other; + node = NULL; + head = NULL; + prev = NULL; + while (it) + { + node = ft_dl_copy_node(it); + if (!node) + return (ft_dl_delete(&head, NULL), head); + node->prev = prev; + if (prev) + prev->next = node; + else + head = node; + if (it->next) + prev = node; + it = it->next; + } + return (head); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_delete.c b/libft_personal/src/ft_list/ft_dl/ft_dl_delete.c new file mode 100644 index 0000000..0da320e --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_delete.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_delete.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:10 by iron #+# #+# */ +/* Updated: 2024/06/23 19:00:01 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include + +int ft_dl_delete_self(t_dlist *node, t_data_apply del) +{ + if (!node) + return (FTLIST_FAILURE); + if (del) + del(node->data); + if (node->prev) + node->prev->next = node->next; + if (node->next) + node->next->prev = node->prev; + free(node); + return (FTLIST_SUCCESS); +} + +size_t ft_dl_delete_range(t_dlist *start, const t_dlist *target, + t_data_apply del) +{ + t_dlist *next; + t_dlist *prev; + size_t i; + + i = 0; + next = NULL; + prev = NULL; + if (start && start->prev) + prev = start->prev; + while (start != target) + { + next = start->next; + if (del) + del(start->data); + free(start); + start = next; + i++; + } + if (prev) + prev->next = next; + if (next) + next->prev = prev; + return (i); +} + +size_t ft_dl_delete(t_dlist **head, t_data_apply del) +{ + size_t i; + t_dlist *firs_elem; + + if (!head) + return (0); + firs_elem = ft_dl_begin(*head); + i = ft_dl_delete_range(firs_elem, NULL, del); + *head = NULL; + return (i); +} + +/* +size_t ft_dl_delete_dup(t_dlist **src) +{ + t_dlist **node_ptrs; + size_t nb_del; + size_t length; + + node_ptrs = NULL; + nb_del = 0; + length = 0; + if (!*src) + return (0); + node_ptrs = ft_dl_get_nodes(*src); + if (!node_ptrs) + return (0); + length = ft_dl_count(*src); + for (size_t j = 0; j + 1 != length; j++) { + for (size_t k = j + 1; k != length; k++) { + if (!node_ptrs[j] || !node_ptrs[k]) + continue ; + if (node_ptrs[j]->data == node_ptrs[k]->data) { + ft_dl_delete_self(node_ptrs[k]); + node_ptrs[k] = NULL; + nb_del++; + j++; + } + } + } + free(node_ptrs); + return (nb_del); +} +*/ diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_find.c b/libft_personal/src/ft_list/ft_dl/ft_dl_find.c new file mode 100644 index 0000000..68d5f84 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_find.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 13:05:32 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_find(const t_dlist *head, const void *data, + int (*cmp)(const void *, const void *)) +{ + t_dlist *it; + + it = (t_dlist *)head; + while (it) + { + if (data == it->data || (cmp && !cmp(it->data, data))) + return (it); + it = it->next; + } + return (NULL); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_getters.c b/libft_personal/src/ft_list/ft_dl/ft_dl_getters.c new file mode 100644 index 0000000..b5d3d6a --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_getters.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_getters.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:21 by iron #+# #+# */ +/* Updated: 2024/07/19 17:36:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_string.h" +#include + +void **ft_dl_get_datas(const t_dlist *src) +{ + size_t size; + void **ret; + + if (!src) + return (NULL); + size = ft_dl_size(src); + ret = ft_calloc(sizeof(void *), (size + 1)); + if (!ret) + return (NULL); + ret[size] = NULL; + size = 0; + while (src) + { + ret[size++] = (void *)src->data; + src = src->next; + } + return (ret); +} + +t_dlist **ft_dl_get_nodes(const t_dlist *src) +{ + size_t size; + t_dlist **ret; + + size = ft_dl_size(src); + ret = NULL; + if (!src) + return (NULL); + ret = ft_calloc(sizeof(t_dlist *), (size + 1)); + if (!ret) + return (ret); + ret[size] = NULL; + size = 0; + while (src) + { + ret[size++] = (t_dlist *)src; + src = src->next; + } + return (ret); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_iterator.c b/libft_personal/src/ft_list/ft_dl/ft_dl_iterator.c new file mode 100644 index 0000000..81c31f3 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_iterator.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_iterator.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:27 by iron #+# #+# */ +/* Updated: 2024/06/23 19:00:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_at(const t_dlist *head, size_t index) +{ + size_t i; + t_dlist *it; + + it = (t_dlist *)head; + i = 0; + while (i != index) + { + i++; + it = it->next; + } + return (it); +} + +t_dlist *ft_dl_end(const t_dlist *head) +{ + t_dlist *it; + + it = (t_dlist *)head; + while (it->next) + it = it->next; + return (it); +} + +t_dlist *ft_dl_begin(const t_dlist *head) +{ + t_dlist *it; + + it = (t_dlist *)head; + while (it->prev) + it = it->prev; + return (it); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_map.c b/libft_personal/src/ft_list/ft_dl/ft_dl_map.c new file mode 100644 index 0000000..70c0f35 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_map.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:21:43 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_map(const t_dlist *lst, t_data_tr f, t_data_apply del) +{ + t_dlist *ret; + t_dlist *tmp; + + if (!lst || !f) + return (NULL); + ret = NULL; + while (lst) + { + tmp = ft_dl_create(f(lst->data)); + if (!tmp) + return (ft_dl_clear(&ret, del), NULL); + ft_dl_add_back(&ret, tmp); + lst = lst->next; + } + return (ret); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_new.c b/libft_personal/src/ft_list/ft_dl/ft_dl_new.c new file mode 100644 index 0000000..5302706 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_new.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:42:00 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_string.h" + +t_dlist *ft_dl_new(void) +{ + t_dlist *ret; + + ret = ft_calloc(1, sizeof(t_dlist)); + if (!ret) + return (ret); + ret->data = NULL; + ret->prev = NULL; + ret->next = NULL; + return (ret); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_pushpop.c b/libft_personal/src/ft_list/ft_dl/ft_dl_pushpop.c new file mode 100644 index 0000000..f95d07c --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_pushpop.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_pushpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:38 by iron #+# #+# */ +/* Updated: 2024/06/23 19:00:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include + +t_dlist *ft_dl_push(t_dlist **node, const void *data) +{ + t_dlist *added; + + added = ft_dl_create(data); + added->next = *node; + if (*node) + (*node)->prev = added; + *node = added; + return (added); +} + +t_dlist *ft_dl_push_back(t_dlist **node, const void *data) +{ + t_dlist *added; + t_dlist *it; + + it = *node; + if (!*node) + return (ft_dl_push(node, data)); + added = ft_dl_create(data); + while (it->next) + it = it->next; + it->next = added; + added->prev = it; + return (*node); +} + +void *ft_dl_pop(t_dlist **node) +{ + void *data; + t_dlist *it; + + data = NULL; + if (!node || !*node) + return (NULL); + it = *node; + *node = (*node)->next; + data = it->data; + free(it); + if (*node) + (*node)->prev = NULL; + return (data); +} + +void *ft_dl_pop_back(t_dlist **node) +{ + void *data; + t_dlist *it; + t_dlist *prev; + + data = NULL; + prev = NULL; + if (!node || !*node) + return (NULL); + it = ft_dl_end(*node); + data = it->data; + prev = it->prev; + if (prev) + prev->next = NULL; + else + *node = NULL; + free(it); + return (data); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_rev.c b/libft_personal/src/ft_list/ft_dl/ft_dl_rev.c new file mode 100644 index 0000000..99c0869 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_rev.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_rev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:20:27 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_rev(t_dlist **head) +{ + t_dlist *it; + t_dlist *tmp; + + if (!head || !*head) + return (NULL); + it = *head; + while (it) + { + tmp = it->next; + it->next = it->prev; + it->prev = tmp; + if (!tmp) + *head = it; + it = tmp; + } + return (*head); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_size.c b/libft_personal/src/ft_list/ft_dl/ft_dl_size.c new file mode 100644 index 0000000..7b20179 --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_size.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:50 by iron #+# #+# */ +/* Updated: 2024/06/23 19:00:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +size_t ft_dl_size(const t_dlist *head) +{ + t_dlist *it; + size_t i; + + i = 0; + it = (t_dlist *)head; + while (it) + { + it = it->next; + i++; + } + return (i); +} + +size_t ft_dl_size_of_data(const t_dlist *head, t_data_is function) +{ + t_dlist *it; + size_t i; + + it = (t_dlist *)head; + i = 0; + while (it) + { + if (function(it->data)) + i++; + it = it->next; + } + return (i); +} diff --git a/libft_personal/src/ft_list/ft_dl/ft_dl_sub.c b/libft_personal/src/ft_list/ft_dl/ft_dl_sub.c new file mode 100644 index 0000000..ef1038e --- /dev/null +++ b/libft_personal/src/ft_list/ft_dl/ft_dl_sub.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dl_sub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 21:34:45 by iron #+# #+# */ +/* Updated: 2024/06/23 19:00:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_dlist *ft_dl_subrange(const t_dlist *src, const t_dlist *to) +{ + t_dlist *ret; + t_dlist *prev; + t_dlist *new_node; + t_dlist *it; + + ret = NULL; + if (!src) + return (ret); + ret = ft_dl_create(src->data); + if (src == to) + return (ret); + prev = ret; + it = src->next; + while (it != to) + { + new_node = ft_dl_create(it->data); + if (!new_node) + return (ft_dl_delete(&prev, NULL), NULL); + new_node->prev = prev; + new_node->next = NULL; + prev->next = new_node; + prev = new_node; + it = it->next; + } + return (ret); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_add.c b/libft_personal/src/ft_list/ft_ll/ft_ll_add.c new file mode 100644 index 0000000..5885700 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_add.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:48:35 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void ft_ll_add_front(t_list **lst, t_list *new) +{ + if (!lst) + return ; + new->next = *lst; + *lst = new; +} + +void ft_ll_add_back(t_list **lst, t_list *new) +{ + if (!*lst) + *lst = new; + else + ft_ll_end(*lst)->next = new; +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_apply.c b/libft_personal/src/ft_list/ft_ll/ft_ll_apply.c new file mode 100644 index 0000000..7a05206 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_apply.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:01:53 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:54 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void ft_ll_apply(const t_list *lst, t_data_apply f) +{ + t_list *it; + + if (!f) + return ; + it = (t_list *)lst; + while (it) + { + f(it->data); + it = it->next; + } +} + +void ft_ll_apply_range(const t_list *lst, const t_list *end, t_data_apply f) +{ + t_list *it; + + if (!f) + return ; + it = (t_list *)lst; + while (it && it != end) + { + f(it->data); + it = it->next; + } +} + +// nxt = lst->next; +// f may modify lst->next, we dont know +void ft_ll_apply_range_node(const t_list *lst, const t_list *end, + t_lnode_apply f) +{ + t_list *nxt; + t_list *it; + + if (!f) + return ; + it = (t_list *)lst; + while (it && it != end) + { + nxt = it->next; + f(it); + it = nxt; + } +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_clear.c b/libft_personal/src/ft_list/ft_ll/ft_ll_clear.c new file mode 100644 index 0000000..4757e2e --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_clear.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 03:12:14 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:00:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include + +void ft_ll_clear(t_list **lst, t_data_apply del) +{ + t_list *runner; + t_list *next; + + if (!lst) + return ; + runner = *lst; + while (runner) + { + next = runner->next; + if (del) + del(runner->data); + runner->data = NULL; + free(runner); + runner = next; + } + *lst = NULL; +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_create.c b/libft_personal/src/ft_list/ft_ll/ft_ll_create.c new file mode 100644 index 0000000..dd26578 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_create.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 11:40:49 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_ll_create(const void *const data) +{ + t_list *ret; + + ret = ft_ll_new(); + if (!ret) + return (ret); + ret->data = (void *)data; + return (ret); +} + +t_list *ft_ll_copy_node(const t_list *const other) +{ + t_list *ret; + + ret = ft_ll_new(); + if (!ret) + return (ret); + ret->data = (void *)other->data; + ret->next = (t_list *)other->next; + return (ret); +} + +t_list *ft_ll_copy_list(const t_list *const other) +{ + t_list *node; + t_list *head; + t_list *prev; + t_list *it; + + it = (t_list *)other; + head = ft_ll_copy_node(it); + if (!head) + return (head); + prev = head; + it = (t_list *)it->next; + while (it) + { + node = ft_ll_copy_node(it); + if (!node) + return (ft_ll_clear(&head, NULL), NULL); + prev->next = node; + prev = node; + it = (t_list *)it->next; + } + return (head); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_delete.c b/libft_personal/src/ft_list/ft_ll/ft_ll_delete.c new file mode 100644 index 0000000..66ba506 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_delete.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_delete.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 03:09:13 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include + +void ft_ll_delone(t_list *lst, t_data_apply del) +{ + if (!lst) + return ; + if (del) + del(lst->data); + free(lst); +} + +size_t ft_ll_delete_range(t_list *lst, const t_list *end, t_data_apply del) +{ + t_list *tmp; + size_t i; + + i = 0; + if (!lst || !del) + return (i); + while (lst && lst != end) + { + i++; + tmp = lst->next; + del(lst->data); + free(lst); + lst = tmp; + } + return (i); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_find.c b/libft_personal/src/ft_list/ft_ll/ft_ll_find.c new file mode 100644 index 0000000..043e329 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_find.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/08 11:12:59 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +void *ft_ll_find(const t_list *list, const void *data, + int (*cmp)(const void *, const void *)) +{ + t_list *it; + + it = (t_list *)list; + while (it) + { + if (data == it->data || (cmp && cmp(it->data, data) == 0)) + return (it->data); + it = it->next; + } + return (NULL); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_getters.c b/libft_personal/src/ft_list/ft_ll/ft_ll_getters.c new file mode 100644 index 0000000..69a5773 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_getters.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_getters.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 11:54:40 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_string.h" + +void **ft_ll_get_datas(const t_list *src) +{ + void **datas; + size_t i; + + if (!src) + return (NULL); + datas = ft_calloc(sizeof(void *), ft_ll_size(src) + 1); + if (!datas) + return (NULL); + i = 0; + while (src) + { + datas[i] = src->data; + src = src->next; + i++; + } + return (datas); +} + +t_list **ft_ll_get_nodes(const t_list *src) +{ + t_list **nodes; + size_t i; + + if (!src) + return (NULL); + nodes = ft_calloc(sizeof(t_list *), ft_ll_size(src) + 1); + if (!nodes) + return (NULL); + i = 0; + while (src) + { + nodes[i] = (t_list *)src; + src = src->next; + i++; + } + return (nodes); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_iterator.c b/libft_personal/src/ft_list/ft_ll/ft_ll_iterator.c new file mode 100644 index 0000000..320216c --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_iterator.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_iterator.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:43:27 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_ll_end(const t_list *lst) +{ + if (!lst) + return (NULL); + while (lst->next) + lst = lst->next; + return ((t_list *)lst); +} + +t_list *ft_ll_at(const t_list *lst, size_t index) +{ + size_t i; + + i = 0; + while (lst && i < index) + { + lst = lst->next; + i++; + } + if (i != index) + return (NULL); + return ((t_list *)lst); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_map.c b/libft_personal/src/ft_list/ft_ll/ft_ll_map.c new file mode 100644 index 0000000..88e41a2 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_map.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:04:38 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_ll_map(const t_list *lst, t_data_tr f, t_data_apply del) +{ + t_list *ret; + t_list *node; + + ret = NULL; + if (!f || !del || !lst) + return (NULL); + while (lst) + { + node = ft_ll_create(f(lst->data)); + if (!node) + return (ft_ll_clear(&ret, del), NULL); + ft_ll_add_back(&ret, node); + lst = lst->next; + } + return (ret); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_new.c b/libft_personal/src/ft_list/ft_ll/ft_ll_new.c new file mode 100644 index 0000000..96304e4 --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_new.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:30:32 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_string.h" + +t_list *ft_ll_new(void) +{ + t_list *elem; + + elem = ft_calloc(sizeof(t_list), 1); + return (elem); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_pushpop.c b/libft_personal/src/ft_list/ft_ll/ft_ll_pushpop.c new file mode 100644 index 0000000..a2704fa --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_pushpop.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_pushpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:02:47 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_string.h" + +t_list *ft_ll_push(t_list **lst, const void *data) +{ + t_list *new; + + if (!lst) + return (NULL); + new = ft_ll_create(data); + if (!new) + return (NULL); + new->next = *lst; + *lst = new; + return (*lst); +} + +t_list *ft_ll_push_back(t_list **lst, const void *data) +{ + t_list *added; + + if (!lst) + return (NULL); + added = ft_ll_create(data); + if (!added) + return (NULL); + if (!*lst) + *lst = added; + else + ft_ll_end(*lst)->next = added; + return (*lst); +} + +void *ft_ll_pop(t_list **lst) +{ + t_list *tmp; + void *data; + + if (!lst || !*lst) + return (NULL); + tmp = *lst; + data = (*lst)->data; + *lst = (*lst)->next; + ft_free((void **)&tmp); + return (data); +} + +void *ft_ll_pop_back(t_list **lst) +{ + t_list *pre_last; + void *data; + + if (!lst || !*lst) + return (NULL); + if (!(*lst)->next) + return (ft_ll_pop(lst)); + pre_last = *lst; + while (pre_last->next->next) + pre_last = pre_last->next; + data = pre_last->next->data; + free(pre_last->next); + pre_last->next = NULL; + return (data); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_rev.c b/libft_personal/src/ft_list/ft_ll/ft_ll_rev.c new file mode 100644 index 0000000..76f316f --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_rev.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_rev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: iron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/21 15:22:54 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_ll_rev(t_list **head) +{ + t_list *next; + t_list *it; + t_list *prev; + + if (!head || !*head) + return (NULL); + it = *head; + prev = NULL; + next = it->next; + while (next) + { + next = it->next; + it->next = prev; + prev = it; + it = next; + } + *head = prev; + return (*head); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_size.c b/libft_personal/src/ft_list/ft_ll/ft_ll_size.c new file mode 100644 index 0000000..15c481f --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_size.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:38:32 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +size_t ft_ll_size(const t_list *lst) +{ + t_list *it; + size_t i; + + i = 0; + it = (t_list *)lst; + while (it) + { + it = it->next; + i++; + } + return (i); +} + +size_t ft_ll_size_match(const t_list *lst, t_data_is function) +{ + size_t i; + t_list *it; + + i = 0; + it = (t_list *)lst; + while (it) + { + if (function(it->data)) + i++; + it = it->next; + } + return (i); +} diff --git a/libft_personal/src/ft_list/ft_ll/ft_ll_sub.c b/libft_personal/src/ft_list/ft_ll/ft_ll_sub.c new file mode 100644 index 0000000..52708cb --- /dev/null +++ b/libft_personal/src/ft_list/ft_ll/ft_ll_sub.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ll_sub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 12:10:57 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 19:01:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" + +t_list *ft_ll_subrange(const t_list *lst, const t_list *end) +{ + t_list *sub; + + sub = NULL; + if (!lst) + return (sub); + if (lst == end) + return (ft_ll_create(lst->data)); + while (lst && lst != end) + { + ft_ll_push_back(&sub, lst->data); + lst = lst->next; + } + return (sub); +} diff --git a/libft_personal/src/ft_map/ft_map_clear.c b/libft_personal/src/ft_map/ft_map_clear.c new file mode 100644 index 0000000..9fa10d7 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_clear.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 18:29:23 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 23:30:14 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_vector.h" + +void ft_map_clear(t_map *map) +{ + size_t i; + t_list *cur; + + i = 0; + while (i < map->capacity) + { + while (map->nodes[i]) + { + cur = map->nodes[i]; + map->nodes[i] = map->nodes[i]->next; + ft_vec_add(&map->reserved_nodes, cur); + } + map->weights[i] = 0; + i++; + } + map->w_total = 0; +} diff --git a/libft_personal/src/ft_map/ft_map_create.c b/libft_personal/src/ft_map/ft_map_create.c new file mode 100644 index 0000000..d0079f2 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_create.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 16:11:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 23:12:34 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_string.h" +#include "ft_vector.h" + +t_map *ft_map_create(size_t size) +{ + t_map *map; + + map = ft_calloc(1, sizeof(t_map)); + if (!map) + return (0); + map->capacity = size; + map->w_total = 0; + map->weights = ft_calloc(size, sizeof(size_t)); + if (!map->weights) + return (free(map), NULL); + map->nodes = ft_calloc(size, sizeof(t_list *)); + if (!map->nodes) + return (free(map->weights), free(map), NULL); + map->hash = &ft_hash_djb2; + map->cmp = (int (*)(const void *, const void *)) & ft_strcmp; + map->reserved_nodes = ft_vec_new(); + if (!map->reserved_nodes) + return (free(map->weights), free(map->nodes), free(map), NULL); + return (map); +} diff --git a/libft_personal/src/ft_map/ft_map_destroy.c b/libft_personal/src/ft_map/ft_map_destroy.c new file mode 100644 index 0000000..5981cdb --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_destroy.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:32:38 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:26:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_list.h" +#include "ft_map.h" +#include "ft_vector.h" +#include + +static t_data_apply singleton_custom_destroy(const t_data_apply custom_destroy, + bool set) +{ + static t_data_apply f_ptr = NULL; + + if (set == true) + f_ptr = custom_destroy; + return (f_ptr); +} + +static void wrapper_destroy(void *restrict data) +{ + t_data_apply destroy; + t_map_node *map_node; + + destroy = singleton_custom_destroy(NULL, false); + map_node = (t_map_node *)data; + if (destroy) + (*destroy)(map_node->data); + free(map_node); +} + +void ft_map_destroy(t_map *map) +{ + ft_map_destroy_free(map, NULL); +} + +static void rsv_del(void *data) +{ + t_list *node; + + node = (t_list *)data; + if (node && node->data) + wrapper_destroy(node->data); + free(node); +} + +void ft_map_destroy_free(t_map *map, t_data_apply free_fun) +{ + size_t i; + + singleton_custom_destroy(free_fun, true); + i = 0; + while (i < map->capacity) + { + ft_ll_clear(&map->nodes[i++], wrapper_destroy); + } + free(map->nodes); + free(map->weights); + ft_vec_apply(map->reserved_nodes, rsv_del); + ft_vec_destroy(&map->reserved_nodes); + free(map); + singleton_custom_destroy(NULL, true); +} diff --git a/libft_personal/src/ft_map/ft_map_get.c b/libft_personal/src/ft_map/ft_map_get.c new file mode 100644 index 0000000..02a2cf7 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_get.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 18:05:52 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 11:54:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include + +t_map_node *ft_map_get_node(t_map *map, const void *key, size_t size) +{ + t_list *bucket; + t_map_node *map_node; + + bucket = map->nodes[map->hash(key, size) % map->capacity]; + map_node = NULL; + while (bucket) + { + map_node = bucket->data; + if (map->cmp(map_node->key, key) == 0) + break ; + bucket = bucket->next; + } + if (map_node && map->cmp(map_node->key, key) == 0) + return (map_node); + return (NULL); +} + +void *ft_map_get(t_map *map, const void *key, size_t size) +{ + t_map_node *map_node; + + map_node = ft_map_get_node(map, key, size); + if (!map_node) + return (NULL); + return (map_node->data); +} + +size_t ft_map_size(const t_map *map) +{ + return (map->w_total); +} + +size_t ft_map_capacity(const t_map *map) +{ + return (map->capacity); +} diff --git a/libft_personal/src/ft_map/ft_map_hash.c b/libft_personal/src/ft_map/ft_map_hash.c new file mode 100644 index 0000000..40c0a64 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_hash.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_hash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 18:07:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 22:18:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" + +// Default hash function provided by map +// Overflows are possible + +// Default hash function for ft_map +// Very fast, but not very good +// see http://www.cse.yorku.ca/~oz/hash.html +// See ft_hash_sdbm or ft_hash_fnv1a for less collisions prone hash functions +size_t ft_hash_djb2(const void *key, size_t size) +{ + unsigned char *str; + size_t hash; + + hash = 5381; + str = (unsigned char *)key; + while (size--) + hash = (hash << 5) + hash + str[size]; + return (hash); +} + +// Better hash function +// Slower, but better +// see http://www.cse.yorku.ca/~oz/hash.html +// overal better than djb2 but default hash function stays djb2 because it's +// faster and easier to understand, plus it's not that bad... right ? +size_t ft_hash_sdbm(const void *key, size_t size) +{ + unsigned char *str; + size_t hash; + + hash = 0; + str = (unsigned char *)key; + while (size--) + hash = *str++ + (hash << 6) + (hash << 16) - hash; + return (hash); +} + +// Another hash function +// Slower, but better +// see +// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function +// +// 64 bits version check is just that the size of size_t impact the starting +// hash value to lower the number of collisions otherwise we use the 32 bits +// version. +// lets hope that 128 bits computers will not be a thing... +// +// note: djb2 and sdbm are 32 bits hash functions and are not impacted by +// the size of size_t +size_t ft_hash_fnv1a(const void *key, size_t size) +{ + unsigned char *str; + size_t hash; + + if (sizeof(size_t) == 8) + hash = 0xcbf29ce484222325; + else + hash = 0x811c9dc5; + str = (unsigned char *)key; + while (size--) + hash = (*str++ ^ hash) * 0x01000193; + return (hash); +} + +// dumbest hash function ever +// just for testing purposes when you don't care about collisions +// or when you want to test your map with a hash function that doesn't +// overflow at all ever +// +size_t ft_hash_dummy(const void *key, size_t size) +{ + unsigned char *str; + size_t hash; + + str = (unsigned char *)key; + hash = 0; + while (size--) + hash += *str++; + return (hash); +} diff --git a/libft_personal/src/ft_map/ft_map_remove.c b/libft_personal/src/ft_map/ft_map_remove.c new file mode 100644 index 0000000..458ddd1 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_remove.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 18:08:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 11:56:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_vector.h" +#include "ft_list_types.h" +#include + +void *ft_map_remove(t_map *map, const void *key, size_t size) +{ + size_t hash; + t_list *prev; + t_list *cur; + + hash = map->hash(key, size) % map->capacity; + prev = NULL; + cur = map->nodes[hash]; + while (cur) + { + if (map->cmp(((t_map_node *)cur->data)->key, key) == 0) + break ; + prev = cur; + cur = cur->next; + } + if (!cur) + return (NULL); + if (!prev) + map->nodes[hash] = cur->next; + else + prev->next = cur->next; + cur->next = NULL; + ft_vec_add(&map->reserved_nodes, cur); + map->weights[hash]--; + map->w_total--; + return (((t_map_node *)cur->data)->data); +} diff --git a/libft_personal/src/ft_map/ft_map_set.c b/libft_personal/src/ft_map/ft_map_set.c new file mode 100644 index 0000000..8bd1475 --- /dev/null +++ b/libft_personal/src/ft_map/ft_map_set.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_map_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 17:36:14 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:16:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "ft_list.h" +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_string.h" +#include "ft_vector.h" + +static void setup_map_node(t_map_node *map_node, const void *key, + const void *value) +{ + map_node->data = (void *)value; + map_node->key = key; + map_node->hash = 0; +} + +// works but dog shit doesnt check all alloc faillure cases +// +bool ft_map_set(t_map *map, const void *key, const void *value, size_t size) +{ + t_map_node *map_node; + t_list *reuse_node; + + map_node = ft_map_get_node(map, key, size); + reuse_node = NULL; + if (map_node) + return (map_node->data = (void *)value, true); + if (map->reserved_nodes->count) + reuse_node = ft_vec_pop(map->reserved_nodes); + else + map_node = ft_malloc(sizeof(t_map_node)); + if (!map_node && !reuse_node) + return (false); + if (reuse_node) + map_node = reuse_node->data; + else + reuse_node = ft_ll_create(map_node); + setup_map_node(map_node, key, value); + map_node->hash = map->hash(key, size); + ft_ll_add_front(&map->nodes[map_node->hash % map->capacity], reuse_node); + map->weights[map_node->hash % map->capacity]++; + map->w_total++; + return (true); +} + +void ft_map_set_cmp(t_map *map, t_data_cmp cmp) +{ + map->cmp = cmp; +} + +void ft_map_set_hash(t_map *map, t_memhash hash) +{ + map->hash = hash; +} diff --git a/libft_personal/src/ft_math/ft_abs.c b/libft_personal/src/ft_math/ft_abs.c new file mode 100644 index 0000000..6f6a14d --- /dev/null +++ b/libft_personal/src/ft_math/ft_abs.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:10:52 by bgoulard #+# #+# */ +/* Updated: 2024/05/26 10:21:20 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_abs(int x) +{ + if (x < 0) + return (-x); + return (x); +} diff --git a/libft_personal/src/ft_math/ft_align.c b/libft_personal/src/ft_math/ft_align.c new file mode 100644 index 0000000..a62445d --- /dev/null +++ b/libft_personal/src/ft_math/ft_align.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_align.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 19:51:21 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 20:46:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_align_2(size_t size, size_t alignment) +{ + return ((size + alignment - 1) & ~(alignment - 1)); +} + +size_t ft_align(size_t size, size_t alignment) +{ + if (alignment < 2) + return (size); + if (size % alignment == 0) + return (size); + size += alignment - size % alignment; + return (size); +} diff --git a/libft_personal/src/ft_math/ft_clamp.c b/libft_personal/src/ft_math/ft_clamp.c new file mode 100644 index 0000000..2af3e97 --- /dev/null +++ b/libft_personal/src/ft_math/ft_clamp.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_torange.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/16 14:51:08 by bgoulard #+# #+# */ +/* Updated: 2024/05/16 15:04:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_clamp(int value, int min, int max) +{ + if (value < min) + return (min); + else if (value > max) + return (max); + return (value); +} + +float ft_clamp_f(float value, float min, float max) +{ + if (value < min) + return (min); + else if (value > max) + return (max); + return (value); +} + +double ft_clamp_d(double value, double min, double max) +{ + if (value < min) + return (min); + else if (value > max) + return (max); + return (value); +} diff --git a/libft_personal/src/ft_math/ft_complex.c b/libft_personal/src/ft_math/ft_complex.c new file mode 100644 index 0000000..9641050 --- /dev/null +++ b/libft_personal/src/ft_math/ft_complex.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_complex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/01 08:11:12 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 16:39:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +double ft_complex_abs(t_complex nb) +{ + return (ft_sqrt(nb.real * nb.real + nb.imaginary * nb.imaginary)); +} + +t_complex ft_complex_addl(t_complex nb, long factor) +{ + return ((t_complex){.real = nb.real + (double)factor, + .imaginary = nb.imaginary + (double)factor}); +} + +t_complex ft_complex_mull(t_complex nb, long factor) +{ + return (ft_complex_muld(nb, (double)factor)); +} + +t_complex ft_complex_muld(t_complex nb, double factor) +{ + return ((t_complex){.real = nb.real * factor, + .imaginary = nb.imaginary * factor}); +} diff --git a/libft_personal/src/ft_math/ft_intrange.c b/libft_personal/src/ft_math/ft_intrange.c new file mode 100644 index 0000000..8dfa764 --- /dev/null +++ b/libft_personal/src/ft_math/ft_intrange.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_intrange.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/29 16:22:01 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 16:41:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int ft_range(int x, int min, int max, int new_max) +{ + int res; + + if (max == min || new_max == 0 || min >= max) + return (0); + if (x <= min) + return (0); + if (x >= max) + return (new_max); + res = x - min; + max -= min; + return ((int)(((double)res / max) * new_max)); +} + +float ft_range_f(float x, float min, float max, float new_max) +{ + return ((float)ft_range_d((double)x, (double)min, (double)max, \ + (double)new_max)); +} + +double ft_range_d(double x, double min, double max, double new_max) +{ + double res; + + if (max == min || new_max == 0 || min >= max) + return (0); + if (x <= min) + return (0); + if (x >= max) + return (new_max); + res = x - min; + max -= min; + return ((res / max) * new_max); +} diff --git a/libft_personal/src/ft_math/ft_log.c b/libft_personal/src/ft_math/ft_log.c new file mode 100644 index 0000000..bfbd133 --- /dev/null +++ b/libft_personal/src/ft_math/ft_log.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_log.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 10:09:57 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:27:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_ullogof(unsigned long long nbr, int base) +{ + const unsigned long long bul = (unsigned long long)base; + int pow; + + pow = -1; + while (nbr) + { + nbr /= bul; + pow++; + } + return (pow); +} + +int ft_llogof(long long nbr, int base) +{ + if (nbr <= 0) + return (-1); + return (ft_ullogof((unsigned long long)nbr, base)); +} + +int ft_logof(int nbr, int base) +{ + if (nbr <= 0) + return (-1); + return (ft_llogof((long long)nbr, base)); +} + +int ft_log(int nbr) +{ + return (ft_logof(nbr, 10)); +} diff --git a/libft_personal/src/ft_math/ft_minmax.c b/libft_personal/src/ft_math/ft_minmax.c new file mode 100644 index 0000000..58fae0b --- /dev/null +++ b/libft_personal/src/ft_math/ft_minmax.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_minmax.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/23 10:23:07 by bgoulard #+# #+# */ +/* Updated: 2023/11/23 10:35:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_min(int a, int b) +{ + if (a < b) + return (a); + return (b); +} + +int ft_max(int a, int b) +{ + if (a > b) + return (a); + return (b); +} diff --git a/libft_personal/src/ft_math/ft_pow.c b/libft_personal/src/ft_math/ft_pow.c new file mode 100644 index 0000000..361f987 --- /dev/null +++ b/libft_personal/src/ft_math/ft_pow.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:04:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 01:01:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_pow(size_t x, size_t y) +{ + size_t res; + + res = 1; + if (y == 0) + return (1); + if (x == 0) + return (0); + while (y > 0) + { + res *= x; + y--; + } + return (res); +} diff --git a/libft_personal/src/ft_math/ft_round.c b/libft_personal/src/ft_math/ft_round.c new file mode 100644 index 0000000..08b3e43 --- /dev/null +++ b/libft_personal/src/ft_math/ft_round.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_round.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:10:18 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 23:10:36 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +double ft_round(double x) +{ + double dec; + double round; + + dec = x - (int)x; + if (dec >= 0.5) + round = (int)x + 1; + else + round = (int)x; + return (round); +} diff --git a/libft_personal/src/ft_math/ft_sqrt.c b/libft_personal/src/ft_math/ft_sqrt.c new file mode 100644 index 0000000..e6ffd0a --- /dev/null +++ b/libft_personal/src/ft_math/ft_sqrt.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:00:06 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 09:33:30 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// using newton's method +double ft_sqrt(double nb) +{ + double x; + double y; + + x = nb; + if (nb <= 0) + return (-1); + y = 1; + while (x - y > 0.0000001) + { + x = (x + y) / 2; + y = nb / x; + } + return (x); +} diff --git a/libft_personal/src/ft_optional/ft_optional_chain.c b/libft_personal/src/ft_optional/ft_optional_chain.c new file mode 100644 index 0000000..1e023e7 --- /dev/null +++ b/libft_personal/src/ft_optional/ft_optional_chain.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_chain.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:06:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/27 09:04:29 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include + +bool ft_optional_chain(t_optional *opt, const t_data_tr_i *f) +{ + size_t i; + + if (!f) + return (false); + i = 0; + while (f[i] && opt->pres != OPT_NONE) + { + opt->val = f[i](opt->val); + if (!opt->val) + opt->pres = OPT_NONE; + i++; + } + if (opt->pres == OPT_NONE) + return (false); + return (true); +} + +t_optional ft_optional_map(t_optional *opt, void *(**f)(void *)) +{ + t_optional ret; + + ft_optional_copy(&ret, opt); + if (ret.pres == OPT_NONE || !f) + return (ret); + ft_optional_chain(&ret, f); + return (ret); +} diff --git a/libft_personal/src/ft_optional/ft_optional_copy.c b/libft_personal/src/ft_optional/ft_optional_copy.c new file mode 100644 index 0000000..3b8f012 --- /dev/null +++ b/libft_personal/src/ft_optional/ft_optional_copy.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:06:40 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 15:39:27 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" + +void ft_optional_copy(t_optional *dest, t_optional *src) +{ + dest->pres = src->pres; + dest->val = src->val; +} diff --git a/libft_personal/src/ft_optional/ft_optional_destroy.c b/libft_personal/src/ft_optional/ft_optional_destroy.c new file mode 100644 index 0000000..226068f --- /dev/null +++ b/libft_personal/src/ft_optional/ft_optional_destroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:06:49 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 15:39:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include +#include + +bool ft_optional_destroy(t_optional *opt) +{ + if (!opt) + return (false); + if (opt->pres == OPT_NONE) + return (free(opt), true); + return (false); +} diff --git a/libft_personal/src/ft_optional/ft_optional_new.c b/libft_personal/src/ft_optional/ft_optional_new.c new file mode 100644 index 0000000..b39d0f9 --- /dev/null +++ b/libft_personal/src/ft_optional/ft_optional_new.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:06:55 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:15:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_string.h" +#include + +t_optional *ft_optional_new(void) +{ + t_optional *elem; + + elem = ft_malloc(sizeof(*elem)); + if (!elem) + return (NULL); + elem->pres = OPT_NONE; + elem->val = NULL; + return (elem); +} + +t_optional *ft_optional_from_val(void *ptr) +{ + t_optional *elem; + + elem = ft_optional_new(); + if (!elem) + return (elem); + elem->pres = OPT_SOME; + elem->val = ptr; + return (elem); +} + +// As this function can fail it should never be called, +// the type t_optional is supposed to be ultra reliable. +// Carefull use is preconized. +t_optional *ft_optional_dup(t_optional *org) +{ + t_optional *ret; + + ret = ft_optional_new(); + if (!ret) + return (NULL); + ft_optional_copy(ret, org); + return (ret); +} diff --git a/libft_personal/src/ft_optional/ft_optional_unwrap.c b/libft_personal/src/ft_optional/ft_optional_unwrap.c new file mode 100644 index 0000000..abdbc0c --- /dev/null +++ b/libft_personal/src/ft_optional/ft_optional_unwrap.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_optional_unwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 18:07:00 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 22:28:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" + +void *ft_optional_unwrap(t_optional opt) +{ + if (opt.pres != OPT_NONE) + return (opt.val); + return (NULL); +} diff --git a/libft_personal/src/ft_pair/ft_pair_cmp.c b/libft_personal/src/ft_pair/ft_pair_cmp.c new file mode 100644 index 0000000..aeea987 --- /dev/null +++ b/libft_personal/src/ft_pair/ft_pair_cmp.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_cmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:58:24 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 16:52:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" + +int ft_pair_cmp(t_pair *pair1, t_pair *pair2, t_data_cmp cmp) +{ + if (!pair1 && !pair2) + return (0); + if (!pair1) + return (-(pair2 != NULL)); + if (!pair2) + return (pair1 != NULL); + if (cmp) + return (cmp(pair1, pair2)); + return (pair1 - pair2); +} + +int ft_pair_cmp_first(t_pair *pair1, t_pair *pair2, t_data_cmp cmp) +{ + if (!pair1 && !pair2) + return (0); + if (!pair1) + return (-(pair2->first != NULL)); + if (!pair2) + return (pair1->first != NULL); + if (cmp) + return (cmp(pair1->first, pair2->first)); + return (pair1->first - pair2->first); +} + +int ft_pair_cmp_second(t_pair *pair1, t_pair *pair2, t_data_cmp cmp) +{ + if (!pair1 && !pair2) + return (0); + if (!pair1) + return (-(pair2->second != NULL)); + if (!pair2) + return (pair1->second != NULL); + if (cmp) + return (cmp(pair1->second, pair2->second)); + return (pair1->second - pair2->second); +} diff --git a/libft_personal/src/ft_pair/ft_pair_destroy.c b/libft_personal/src/ft_pair/ft_pair_destroy.c new file mode 100644 index 0000000..2b32c85 --- /dev/null +++ b/libft_personal/src/ft_pair/ft_pair_destroy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 23:04:56 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:13:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_pair_types.h" +#include "ft_string.h" + +void ft_pair_destroy(t_pair **pair, t_data_apply del_f, t_data_apply del_s) +{ + if (!pair) + return ; + if (del_f) + del_f((*pair)->first); + if (del_s) + del_s((*pair)->second); + ft_free((void **)pair); +} diff --git a/libft_personal/src/ft_pair/ft_pair_get.c b/libft_personal/src/ft_pair/ft_pair_get.c new file mode 100644 index 0000000..75dc91b --- /dev/null +++ b/libft_personal/src/ft_pair/ft_pair_get.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:57:05 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 16:30:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair_types.h" +#include + +void *ft_pair_first(t_pair *pair) +{ + if (!pair) + return (NULL); + return (pair->first); +} + +void *ft_pair_second(t_pair *pair) +{ + if (!pair) + return (NULL); + return (pair->second); +} diff --git a/libft_personal/src/ft_pair/ft_pair_new.c b/libft_personal/src/ft_pair/ft_pair_new.c new file mode 100644 index 0000000..0ca5f5e --- /dev/null +++ b/libft_personal/src/ft_pair/ft_pair_new.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:28:49 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:15:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "ft_string.h" +#include + +t_pair *ft_pair_new(void *a, void *b) +{ + t_pair *ret; + + ret = ft_malloc(sizeof(*ret)); + if (!ret) + return (NULL); + return (ft_pair_set(ret, a, b), ret); +} diff --git a/libft_personal/src/ft_pair/ft_pair_set.c b/libft_personal/src/ft_pair/ft_pair_set.c new file mode 100644 index 0000000..5e5b47f --- /dev/null +++ b/libft_personal/src/ft_pair/ft_pair_set.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_pair_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/23 22:55:07 by bgoulard #+# #+# */ +/* Updated: 2024/06/24 00:10:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair_types.h" +#include "ft_pair.h" + +void ft_pair_set(t_pair *pair, void *first, void *second) +{ + if (!pair) + return ; + pair->first = first; + pair->second = second; +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isalnum.c b/libft_personal/src/ft_string/ft_chr/ft_isalnum.c new file mode 100644 index 0000000..7823b71 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:19:44 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:57:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isalpha.c b/libft_personal/src/ft_string/ft_chr/ft_isalpha.c new file mode 100644 index 0000000..20d79ad --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:13:03 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:57:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int ft_isalpha(int c) +{ + return (ft_islower(c) || ft_isupper(c)); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isascii.c b/libft_personal/src/ft_string/ft_chr/ft_isascii.c new file mode 100644 index 0000000..271e350 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isascii.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:23:34 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 14:57:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isdigit.c b/libft_personal/src/ft_string/ft_chr/ft_isdigit.c new file mode 100644 index 0000000..43f58ac --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isdigit.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:27:37 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 14:56:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_ishexdigit.c b/libft_personal/src/ft_string/ft_chr/ft_ishexdigit.c new file mode 100644 index 0000000..a414055 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_ishexdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ishexdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:48:46 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 18:56:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int ft_ishexdigit(int c) +{ + return (ft_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_islower.c b/libft_personal/src/ft_string/ft_chr/ft_islower.c new file mode 100644 index 0000000..cae6ffd --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_islower.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:14:15 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 15:56:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_islower(int c) +{ + return (c >= 'a' && c <= 'z'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isoctdigit.c b/libft_personal/src/ft_string/ft_chr/ft_isoctdigit.c new file mode 100644 index 0000000..9e53fac --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isoctdigit.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isoctdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:52:06 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 18:56:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isoctdigit(int c) +{ + return (c >= '0' && c <= '7'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isprint.c b/libft_personal/src/ft_string/ft_chr/ft_isprint.c new file mode 100644 index 0000000..3e16879 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isprint.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:31:52 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 14:58:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + return (c >= ' ' && c <= '~'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isspace.c b/libft_personal/src/ft_string/ft_chr/ft_isspace.c new file mode 100644 index 0000000..f72895c --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isspace.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 11:53:46 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 14:58:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isspace(int c) +{ + return (c == ' ' || c == '\f' || c == '\n' \ + || c == '\r' || c == '\t' || c == '\v'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_isupper.c b/libft_personal/src/ft_string/ft_chr/ft_isupper.c new file mode 100644 index 0000000..d0e6ad3 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_isupper.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:42:20 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 15:34:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isupper(int c) +{ + return (c >= 'A' && c <= 'Z'); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_putchar_fd.c b/libft_personal/src/ft_string/ft_chr/ft_putchar_fd.c new file mode 100644 index 0000000..94afd76 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_putchar_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */ +/* Updated: 2023/11/09 10:03:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); + return ; +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_tolower.c b/libft_personal/src/ft_string/ft_chr/ft_tolower.c new file mode 100644 index 0000000..007914d --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:42:46 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 18:17:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int ft_tolower(int c) +{ + if (ft_isupper(c)) + c += 32; + return (c); +} diff --git a/libft_personal/src/ft_string/ft_chr/ft_toupper.c b/libft_personal/src/ft_string/ft_chr/ft_toupper.c new file mode 100644 index 0000000..82060b0 --- /dev/null +++ b/libft_personal/src/ft_string/ft_chr/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:43:00 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 18:18:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int ft_toupper(int c) +{ + if (ft_islower(c)) + c -= 32; + return (c); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_apply_2d.c b/libft_personal/src/ft_string/ft_mem/ft_apply_2d.c new file mode 100644 index 0000000..c37a17e --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_apply_2d.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_apply_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/11 09:47:17 by bgoulard #+# #+# */ +/* Updated: 2024/05/27 09:08:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include + +void ft_apply_2d(void **array, t_data_apply f) +{ + size_t i; + + i = 0; + while (array[i]) + f(array[i++]); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_arena.c b/libft_personal/src/ft_string/ft_mem/ft_arena.c new file mode 100644 index 0000000..5a3c9e2 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_arena.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arena.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/25 17:10:05 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 17:40:54 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void *ft_arena_alloc(size_t size) +{ + return (ft_narena_alloc(size, 0)); +} + +void *ft_arena_realloc(void *ptr, size_t size) +{ + return (ft_narena_realloc(ptr, size, 0)); +} + +void *ft_arena_calloc(size_t count, size_t size) +{ + return (ft_narena_calloc(count, size, 0)); +} + +void ft_arena_free(void *ptr) +{ + ft_narena_free(ptr, 0); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_arena_free.c b/libft_personal/src/ft_string/ft_mem/ft_arena_free.c new file mode 100644 index 0000000..e69de29 diff --git a/libft_personal/src/ft_string/ft_mem/ft_bzero.c b/libft_personal/src/ft_string/ft_mem/ft_bzero.c new file mode 100644 index 0000000..3283ea0 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_bzero.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:05:47 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 19:48:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static void byte_bzero(char *s, size_t n) +{ + while (n--) + { + *s = 0; + s++; + } +} + +static void word_bzero(size_t *s, size_t *n) +{ + size_t word_count; + size_t i; + + word_count = *n / sizeof(size_t); + i = 0; + while (i < word_count) + { + s[i] = 0; + i++; + } + *n %= sizeof(size_t); +} + +void ft_bzero(void *s, size_t n) +{ + if (!n || !s) + return ; + if (n < sizeof(size_t)) + return (byte_bzero(s, n)); + byte_bzero(s, (size_t)s % sizeof(size_t)); + n -= (size_t)s % sizeof(size_t); + s += (size_t)s % sizeof(size_t); + word_bzero(s, &n); + byte_bzero(s, n); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_calloc.c b/libft_personal/src/ft_string/ft_mem/ft_calloc.c new file mode 100644 index 0000000..2eb7352 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_calloc.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:28:30 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:20:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_string.h" + +void *ft_calloc(size_t nmemb, size_t weight) +{ + void *ret; + size_t tot; + + if (nmemb == 0 || weight == 0) + return (ft_malloc(0)); + if (nmemb > SIZE_MAX / weight) + return (NULL); + tot = nmemb * weight; + ret = ft_malloc(tot); + if (!ret) + return (NULL); + return (ft_memset(ret, 0, tot)); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_fd_to_buff.c b/libft_personal/src/ft_string/ft_mem/ft_fd_to_buff.c new file mode 100644 index 0000000..90fc327 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_fd_to_buff.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fd_to_buff.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 19:52:02 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 16:46:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "ft_string.h" +#include "ft_string_types.h" + +char *ft_fd_to_buff(int fd) +{ + char buff[BUFFER_SIZE + 1]; + char *file; + char *prev; + ssize_t ret; + + if (fd == -1) + return (NULL); + file = NULL; + ret = read(fd, buff, BUFFER_SIZE); + if (ret == -1) + return (NULL); + buff[ret] = '\0'; + file = ft_strdup(buff); + while (ret == BUFFER_SIZE) + { + ret = read(fd, buff, BUFFER_SIZE); + if (ret == -1) + return (ft_free((void **)&file), NULL); + buff[ret] = '\0'; + prev = file; + file = ft_strjoin(file, buff); + ft_free((void **)&prev); + } + return (file); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_free.c b/libft_personal/src/ft_string/ft_mem/ft_free.c new file mode 100644 index 0000000..18159bc --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_free.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 10:18:13 by bgoulard #+# #+# */ +/* Updated: 2024/05/27 09:26:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_free(void **ptr) +{ + if (!ptr || !*ptr) + return ; + free(*ptr); + *ptr = NULL; +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_free_2d.c b/libft_personal/src/ft_string/ft_mem/ft_free_2d.c new file mode 100644 index 0000000..fb4a7b5 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_free_2d.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 09:32:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 23:47:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +void ft_free_2d(void **arr) +{ + ft_apply_2d(arr, free); + free(arr); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_len_2d.c b/libft_personal/src/ft_string/ft_mem/ft_len_2d.c new file mode 100644 index 0000000..88bcb79 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_len_2d.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_len_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 09:41:42 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:22:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_len_2d(const void *const *array) +{ + size_t len; + + len = 0; + while (array[len]) + len++; + return (len); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_malloc.c b/libft_personal/src/ft_string/ft_mem/ft_malloc.c new file mode 100644 index 0000000..5ea3b5c --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_malloc.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_malloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/19 18:20:32 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:20:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#include "ft_string.h" + +void *ft_malloc(size_t size) +{ + return (malloc(size)); +} + diff --git a/libft_personal/src/ft_string/ft_mem/ft_memchr.c b/libft_personal/src/ft_string/ft_mem/ft_memchr.c new file mode 100644 index 0000000..82bdd96 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memchr.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:15:12 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:19:54 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static void *sword_memchr(const void *s, int c, size_t n) +{ + char *casted; + size_t i; + const char target = (char)c; + + casted = (char *)s; + i = 0; + while (n != i) + { + if (casted[i++] == target) + return (casted + i - 1); + } + return (NULL); +} + +/* +** we check if the size of size_t is 8 bytes by checkning the platform +** we are on +** if yes we use the 1st function else we are on the 32 bit platform +** so use the second version +*/ + +#ifdef __x86_64__ + +static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight, + char c) +{ + *target = (size_t)c; + *target |= *target << 8; + *target |= *target << 16; + *target |= *target << 32; + *rp_one = 0x0101010101010101; + *rp_eight = 0x8080808080808080; +} + +#else + +static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight, + char c) +{ + *target = (size_t)c; + *target |= *target << 8; + *target |= *target << 16; + *rp_one = 0x0101010101010101; + *rp_eight = 0x8080808080808080; +} + +#endif + +/* +** *casted ^ target : XOR between the target and the casted value to leave a +** segment of bits where if the target is present, the bits will be 0. +** ((*casted ^ target) - 0x0101010101010101) : XOR result - 0x0101010101010101 +** remove 1 to the said bits. if target is there the result will be FF. +** ~((casted ^ target) : XOR result inverted aka 0x00 -> 0xFF and 0xFF -> 0x00 +** Remember if target is present on xor segment the res is 0 so ~ will be 0xFF +** we effectively have a mask where the target is present. +** ((*casted ^ target) - repeated_1) & ~((*casted ^ target) : +** XOR result - repeated_1 & ~XOR result +** xor result gets us to 0 where the target is +** -repeated_1 will get us to FF where the target is +** ~XOR result will get us to FF where the target is +** & between the two will get us to FF where the target is +** to filter the match we only keep the 8th bit of the result +** if the 8th bit is different from 0 we have a match in the section. +** when match send to sword_memchr to find the exact position of the target. +** +** For more infos check ft_strlen.c it is the same principle but simpler +** as we are looking directly for a 0x00. +*/ + +static void *bword_memchr(const void *s, int c, size_t n) +{ + size_t *casted; + size_t target; + size_t rp_one; + size_t rp_eight; + + casted = (size_t *)s; + constr_bword(&target, &rp_one, &rp_eight, c); + while (n > sizeof(size_t)) + { + if ((((*casted ^ target) - rp_one) & ~((*casted ^ target) & rp_eight))) + return (sword_memchr(casted, c, sizeof(size_t))); + casted++; + n -= sizeof(size_t); + } + if (n) + return (sword_memchr(casted, c, n)); + return (NULL); +} + +void *ft_memchr(const void *s, int c, size_t n) +{ + char *res; + + if (n < sizeof(n)) + return (sword_memchr(s, c, n)); + res = sword_memchr(s, c, (size_t)s % sizeof(size_t)); + if (res) + return (res); + res = (char *)s + ((size_t)s % sizeof(size_t)); + n -= (size_t)s % sizeof(size_t); + return (bword_memchr(res, c, n)); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_memcmp.c b/libft_personal/src/ft_string/ft_mem/ft_memcmp.c new file mode 100644 index 0000000..e9c06a2 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:11:32 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 15:59:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t index; + + index = 0; + if (s1 == s2 || n == 0) + return (0); + while (index != n && ((const unsigned char *)s1)[index] == \ + ((const unsigned char *)s2)[index]) + index++; + if (index == n) + index--; + return (((unsigned char *)s1)[index] - ((unsigned char *)s2)[index]); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_memcpy.c b/libft_personal/src/ft_string/ft_mem/ft_memcpy.c new file mode 100644 index 0000000..ba451d9 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memcpy.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 13:48:06 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 22:57:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + return (ft_memmove(dest, src, n)); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_memmap.c b/libft_personal/src/ft_string/ft_mem/ft_memmap.c new file mode 100644 index 0000000..7bd61c0 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memmap.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/27 18:14:50 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:15:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_string.h" +#include +#include + +void **ft_memmap(void **src, size_t nb_e, t_data_tr f) +{ + void **dst; + size_t i; + + if (!src || !f || !nb_e) + return (NULL); + dst = ft_malloc((nb_e + 1) * sizeof(void *)); + if (!dst) + return (NULL); + dst[nb_e] = NULL; + i = 0; + while (i < nb_e) + { + dst[i] = f(src[i]); + i++; + } + return (dst); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_memmove.c b/libft_personal/src/ft_string/ft_mem/ft_memmove.c new file mode 100644 index 0000000..f9c66ca --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memmove.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:14:49 by bgoulard #+# #+# */ +/* Updated: 2024/07/11 11:59:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static void *byte_memcpy(void *dest, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (dest > src) + while (n--) + ((unsigned char *)dest)[n] = ((unsigned char *)src)[n]; + else + { + while (i < n) + { + ((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; + i++; + } + } + return (dest); +} + +static void word_memcpy(void *dest, const void *src, size_t *n) +{ + size_t *casted_dest; + size_t *casted_src; + size_t word_count; + size_t i; + + if (*n < sizeof(size_t)) + return ; + casted_dest = (size_t *)dest; + casted_src = (size_t *)src; + word_count = *n / sizeof(size_t); + i = 0; + if (dest < src) + { + while (i++ < word_count) + casted_dest[i - 1] = casted_src[i - 1]; + } + else + { + i = word_count; + while (i--) + casted_dest[i] = casted_src[i]; + } + *n %= sizeof(size_t); +} + +/* + * If src is not aligned then we copy byte by byte otherwise + * the compiler will need to align each time we pull from src +*/ + +static void *ft_memmove_forward(void *dest, const void *src, size_t n) +{ + const void *ret = dest; + size_t align_offset; + size_t prev_n; + + if ((size_t)src % sizeof(size_t)) + return (byte_memcpy(dest, src, n)); + align_offset = (size_t)dest % sizeof(size_t); + byte_memcpy(dest, src, align_offset); + n -= align_offset; + src += align_offset; + dest += align_offset; + prev_n = n; + word_memcpy(dest, src, &n); + dest += prev_n - n; + src += prev_n - n; + if (n) + byte_memcpy(dest, src, n); + return ((void *)ret); +} + +static void *ft_memmove_backward(void *dest, const void *src, size_t n) +{ + const void *ret = dest; + size_t align_offset; + + if ((size_t)src % sizeof(size_t)) + return (byte_memcpy(dest, src, n)); + align_offset = (size_t)(dest + n) % sizeof(size_t); + n -= align_offset; + byte_memcpy(dest + n, src + n, align_offset); + word_memcpy(dest, src, &n); + if (n) + byte_memcpy(dest, src, n); + return ((void *)ret); +} + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + const void *ret = dest; + + if (dest == src || !n || !dest || !src) + return ((void *)ret); + if (n < sizeof(size_t)) + return (byte_memcpy(dest, src, n)); + if (dest < src) + return (ft_memmove_forward(dest, src, n)); + return (ft_memmove_backward(dest, src, n)); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_memset.c b/libft_personal/src/ft_string/ft_mem/ft_memset.c new file mode 100644 index 0000000..6ddf8a6 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:07:20 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 16:48:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memset(void *s, int c, size_t n) +{ + char *casted; + + casted = (char *)s; + if (!n) + return (s); + while (n--) + { + *casted = (char)c; + casted++; + } + return (s); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_narena.c b/libft_personal/src/ft_string/ft_mem/ft_narena.c new file mode 100644 index 0000000..f9119e7 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_narena.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_narena.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/25 17:41:16 by bgoulard #+# #+# */ +/* Updated: 2024/06/30 18:06:29 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "ft_math.h" +#include +#include + +#define FT_NARENAS_MAX 16 +#define FT_AREA_BLOCK_SIZE 4096 + +typedef struct s_arena +{ + void *block_origin; + void *block_current; + void *block_end; +} t_arena; + +t_list *ft_arena_get(int n) +{ + static t_list arena_array[FT_NARENAS_MAX] = {0}; + + if (n < 0 || n >= FT_NARENAS_MAX) + return (NULL); + return (&arena_array[n]); +} + +bool ft_arena_create_handler(t_list *ar_ptr, size_t size) +{ + t_arena *arena; + + size = ft_align_2(size + sizeof(t_arena), FT_AREA_BLOCK_SIZE); + arena = malloc(size); + if (!arena) + return (false); + arena->block_origin = arena + 1; + arena->block_current = arena->block_origin; + arena->block_end = (void *)arena + size; + ar_ptr->data = arena; + return (true); +} + +void *ft_arena_alloc(size_t size, int ar_nb) +{ + t_list *ar_list; + t_arena *arena; + void *ret; + + size = ft_align_2(size, FT_AREA_BLOCK_SIZE); + ar_list = ft_arena_get(ar_nb); + if (!ar_list) + return (NULL); + arena = ft_ll_end(ar_list)->data; + if (!arena && ft_arena_create_handler(ar_list, size) == false) + return (NULL); + // create new block handler - if fail ret null + if (arena->block_origin == NULL || \ + arena->block_current + size > arena->block_end) + { + // allocate a new block + ; + } + ret = arena->block_current; + arena->block_current += size; + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_qsort.c b/libft_personal/src/ft_string/ft_mem/ft_qsort.c new file mode 100644 index 0000000..f9e7d00 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_qsort.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_qsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 11:31:15 by bgoulard #+# #+# */ +/* Updated: 2023/12/30 12:37:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void ft_qsort(void *array, size_t nmb, size_t size, \ + int (*cmp)(const void *, const void *)) +{ + size_t i; + size_t j; + size_t pivot; + + if (nmb < 2) + return ; + i = 0; + j = nmb - 1; + pivot = nmb / 2; + while (i < j) + { + while (cmp(array + (i * size), array + (pivot * size)) < 0) + i++; + while (cmp(array + (j * size), array + (pivot * size)) > 0) + j--; + if (i < j) + { + ft_swap(array + (i * size), array + (j * size)); + if (i == pivot || j == pivot) + pivot = (i == pivot) * i + (j == pivot) * j; + } + } + ft_qsort(array, pivot, size, cmp); + ft_qsort(array + ((pivot + 1) * size), nmb - pivot - 1, size, cmp); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_realloc.c b/libft_personal/src/ft_string/ft_mem/ft_realloc.c new file mode 100644 index 0000000..fd71dca --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_realloc.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:27:54 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:16:11 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +// direct call to ft_malloc as ft_bzero is not needed +// >> we are copying the content of ptr to new +void *ft_realloc(void *ptr, size_t sizeNew, size_t sizeOld) +{ + void *new; + + if (sizeNew == sizeOld) + return (ptr); + new = ft_malloc(sizeNew); + if (!new) + return (NULL); + if (sizeNew < sizeOld) + ft_memcpy(new, ptr, sizeNew); + else + ft_memcpy(new, ptr, sizeOld); + free(ptr); + return (new); +} diff --git a/libft_personal/src/ft_string/ft_mem/ft_swap.c b/libft_personal/src/ft_string/ft_mem/ft_swap.c new file mode 100644 index 0000000..f138a09 --- /dev/null +++ b/libft_personal/src/ft_string/ft_mem/ft_swap.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 11:34:29 by bgoulard #+# #+# */ +/* Updated: 2023/12/12 11:39:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_swap(void **a, void **b) +{ + void *tmp; + + tmp = *a; + *a = *b; + *b = tmp; +} diff --git a/libft_personal/src/ft_string/ft_str/ft_atof.c b/libft_personal/src/ft_string/ft_str/ft_atof.c new file mode 100644 index 0000000..b54d9e2 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_atof.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/04 11:09:02 by bgoulard #+# #+# */ +/* Updated: 2024/05/26 16:37:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_char.h" + +static int loc_forward(const char *str, int *i) +{ + bool neg; + + neg = false; + *i = 0; + while (ft_isspace(str[*i]) || str[*i] == '-' || str[*i] == '+' || \ + ft_isdigit(str[*i])) + { + if (str[*i] == '-') + neg = true; + (*i)++; + } + return (neg); +} + +double ft_atof(const char *str) +{ + int whole; + bool neg; + double decimal; + int i; + int j; + + j = 1; + decimal = 0; + whole = ft_atoi(str); + neg = loc_forward(str, &i); + if (str[i] == '.') + { + i++; + while (ft_isdigit(str[i])) + { + decimal = decimal * 10 + str[i] - '0'; + i++; + j *= 10; + } + } + if (neg == true) + decimal *= -1; + return ((double)whole + (double)decimal / j); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_atoi.c b/libft_personal/src/ft_string/ft_str/ft_atoi.c new file mode 100644 index 0000000..00b20f2 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_atoi.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:05:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:36:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_char.h" + +int ft_atoi(const char *str) +{ + int ret; + int sign; + size_t offset; + + ret = 0; + offset = 0; + sign = 0; + while (ft_isspace(str[offset])) + offset++; + while (str[offset] == '+' || str[offset] == '-') + { + if (str[offset] == '-') + sign++; + offset++; + } + while (ft_isdigit(str[offset])) + ret = ret * 10 + str[offset++] - '0'; + if (sign % 2 == 1) + return (-ret); + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_atoi_base.c b/libft_personal/src/ft_string/ft_str/ft_atoi_base.c new file mode 100644 index 0000000..1c17082 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_atoi_base.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:52:58 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 16:54:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_char.h" + +static int ft_is_valid(const char *base) +{ + int i; + int j; + + i = 0; + if (ft_strlen(base) < 2) + return (0); + while (base[i]) + { + if (base[i] == '+' || base[i] == '-' || ft_isspace(base[i])) + return (0); + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + i++; + } + return (1); +} + +int ft_atoi_base(const char *str, const char *base) +{ + const size_t base_len = ft_strlen(base); + int i; + int nb; + int sign; + + i = 0; + nb = 0; + sign = 1; + if (!ft_is_valid(base)) + return (0); + while (ft_isspace(str[i])) + i++; + while (str[i] == '+' || str[i] == '-') + if (str[i++] == '-') + sign *= -1; + while (str[i] && ft_strchr(base, str[i])) + nb = nb * base_len + ft_strchr(base, str[i++]) - base; + return (nb * sign); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_itoa.c b/libft_personal/src/ft_string/ft_str/ft_itoa.c new file mode 100644 index 0000000..220682a --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_itoa.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:20:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:37:11 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_itoa(int nbr) +{ + return (ft_itoa_base(nbr, "0123456789")); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_itoa_base.c b/libft_personal/src/ft_string/ft_str/ft_itoa_base.c new file mode 100644 index 0000000..6442495 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_itoa_base.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 15:03:11 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 17:53:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_math.h" +#include +#include "ft_char.h" + +static int ft_is_valid(const char *base) +{ + int i; + int j; + + i = 0; + if (ft_strlen(base) < 2) + return (0); + while (base[i]) + { + if (base[i] == '+' || base[i] == '-' || ft_isspace(base[i])) + return (0); + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + i++; + } + return (1); +} + +static int loc_getlen(long long nbr, size_t blen) +{ + size_t nbr_len; + + nbr_len = 0; + if (nbr == 0) + return (1); + if (nbr < 0) + { + nbr *= -1; + nbr_len++; + } + nbr_len += ft_llogof(nbr, blen) + 1; + return (nbr_len); +} + +char *ft_itoa_base(int nbr, const char *base) +{ + char *ret; + size_t off; + long long srcnbr; + const size_t blen = ft_strlen(base); + const int nbrlen = loc_getlen((long long)nbr, blen); + + srcnbr = (long long)nbr; + if (!ft_is_valid(base)) + return (NULL); + ret = ft_calloc(sizeof(char), (nbrlen + 1)); + if (!ret) + return (NULL); + if (srcnbr <= 0) + { + ret[0] = (srcnbr == 0) * '0' + (srcnbr < 0) * '-'; + srcnbr *= -1; + } + off = 0; + while (srcnbr) + { + ret[nbrlen - ++off] = base[srcnbr % blen]; + srcnbr /= blen; + } + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_perror.c b/libft_personal/src/ft_string/ft_str/ft_perror.c new file mode 100644 index 0000000..42aa9b6 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_perror.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_perror.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 14:09:15 by bgoulard #+# #+# */ +/* Updated: 2024/07/08 14:36:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void ft_perror(const char *s) +{ + if (s) + { + ft_putstr_fd(s, STDERR_FILENO); + ft_putstr_fd(": ", STDERR_FILENO); + } + ft_putstr_fd(ft_strerror(errno), STDERR_FILENO); + ft_putstr_fd("\n", STDERR_FILENO); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_putendl_fd.c b/libft_personal/src/ft_string/ft_str/ft_putendl_fd.c new file mode 100644 index 0000000..7ba02e1 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_putendl_fd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:48:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +int ft_putendl_fd(const char *s, int fd) +{ + int ret; + + if (!s) + return (-1); + ret = ft_putstr_fd(s, fd); + if (ret < 0) + return (ret); + if (write(fd, "\n", 1) < 0) + return (-1); + return (ret + 1); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_putnbr_fd.c b/libft_personal/src/ft_string/ft_str/ft_putnbr_fd.c new file mode 100644 index 0000000..06b8218 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_putnbr_fd.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:08:01 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 21:28:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "ft_string.h" + +int ft_putnbr_fd(int nb, int fd) +{ + char result[13] = {0}; + int it; + int neg; + + neg = 0; + it = 12; + if (nb == INT_MIN) + return (ft_putstr_fd("-2147483648", fd)); + if (nb < 0) + { + neg = 1; + nb = -nb; + } + while (nb >= 10) + { + result[--it] = nb % 10 + '0'; + nb /= 10; + } + result[--it] = nb + '0'; + if (neg) + result[--it] = '-'; + return (ft_putstr_fd(result + it, fd)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_putstr_fd.c b/libft_personal/src/ft_string/ft_str/ft_putstr_fd.c new file mode 100644 index 0000000..31c76b1 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_putstr_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:57:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +int ft_putstr_fd(const char *s, int fd) +{ + if (!s) + return (-1); + return (write(fd, s, ft_strlen(s))); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_shift_args.c b/libft_personal/src/ft_string/ft_str/ft_shift_args.c new file mode 100644 index 0000000..71aa544 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_shift_args.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_shift_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/14 17:10:50 by bgoulard #+# #+# */ +/* Updated: 2024/01/01 17:14:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +const char *ft_shift_args(const char **args[], int *index) +{ + const char *arg; + + if (*index <= 0 || !*args || !**args) + return (NULL); + (*index)--; + arg = **args; + (*args)++; + return (arg); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_split.c b/libft_personal/src/ft_string/ft_str/ft_split.c new file mode 100644 index 0000000..7d76d0c --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_split.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 21:48:50 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 15:48:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +static size_t loc_get_nbwords(const char *str, char delim) +{ + size_t ret; + size_t i; + size_t inword; + + ret = 0; + i = 0; + inword = 0; + if (!str) + return (0); + while (str[i]) + { + if (str[i++] == delim) + { + if (inword == 1) + ret++; + inword = 0; + } + else + inword = 1; + } + if (inword == 1) + ret++; + return (ret); +} + +static char *loc_cpy_til(const char *str, char delim, size_t *offset_str) +{ + size_t len; + char *ret; + + len = 0; + while (str[*offset_str + len] != delim && str[*offset_str + len]) + len++; + if (len == 0) + return (NULL); + ret = ft_calloc(sizeof(char), (len + 1)); + if (!ret) + return (NULL); + len = 0; + while (str[*offset_str + len] != delim && str[*offset_str + len]) + { + ret[len] = str[*offset_str + len]; + len++; + } + *offset_str += len; + return (ret); +} + +char **ft_split(const char *str, char delim) +{ + size_t offset_str; + size_t offset_words; + char **words; + + words = ft_calloc(sizeof(char *), (loc_get_nbwords(str, delim) + 1)); + if (!words) + return (words); + offset_words = 0; + offset_str = 0; + while (str && str[offset_str] == delim && delim != 0) + offset_str++; + while (str && str[offset_str]) + { + words[offset_words++] = loc_cpy_til(str, delim, &offset_str); + if (!words[offset_words - 1]) + return (ft_free_2d((void **)words), NULL); + while (str[offset_str] == delim && str[offset_str]) + offset_str++; + } + return (words); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_splits.c b/libft_personal/src/ft_string/ft_str/ft_splits.c new file mode 100644 index 0000000..edc19fb --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_splits.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_splits.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 21:48:50 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 15:51:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +static size_t loc_get_nbwords(const char *str, const char *delim) +{ + size_t ret; + size_t i; + size_t inword; + + ret = 0; + i = 0; + inword = 0; + if (!str) + return (0); + while (str[i]) + { + if (ft_strchr(delim, str[i++])) + { + if (inword == 1) + ret++; + inword = 0; + } + else + inword = 1; + } + if (inword == 1) + ret++; + return (ret); +} + +char **ft_splits(const char *str, const char *delim) +{ + char *str_cpy; + char *s; + size_t offset_words; + char **words; + + words = ft_calloc(sizeof(char *), (loc_get_nbwords(str, delim) + 1)); + if (!words) + return (NULL); + offset_words = 0; + str_cpy = ft_strdup(str); + s = ft_strtok(str_cpy, delim); + while (s) + { + words[offset_words++] = ft_strdup(s); + if (!words[offset_words - 1]) + return (ft_free_2d((void **)words), NULL); + s = ft_strtok(NULL, delim); + } + free(str_cpy); + return (words); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isalnum.c b/libft_personal/src/ft_string/ft_str/ft_str_isalnum.c new file mode 100644 index 0000000..6e02aff --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isalnum.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:52:54 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:42:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" + +bool ft_str_isalnum(const char *str) +{ + if (!str) + return (false); + return (ft_str_isvalid(str, ft_isalnum)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isalpha.c b/libft_personal/src/ft_string/ft_str/ft_str_isalpha.c new file mode 100644 index 0000000..4550c4a --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isalpha.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 15:55:40 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:42:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +bool ft_str_isalpha(const char *str) +{ + int i; + + if (!str) + return (false); + i = 0; + while (str[i]) + { + if (!ft_isalpha(str[i])) + return (false); + i++; + } + if (i == 0) + return (false); + return (true); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isbool.c b/libft_personal/src/ft_string/ft_str/ft_str_isbool.c new file mode 100644 index 0000000..b00df4c --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isbool.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isbool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:37:46 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +bool ft_str_isbool(const char *str) +{ + if (!str) + return (false); + if (ft_strcmp(str, "true") == 0 || ft_strcmp(str, "false") == 0 || \ + ft_strcmp(str, "0") == 0 || ft_strcmp(str, "1") == 0) + return (true); + return (false); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isdigit.c b/libft_personal/src/ft_string/ft_str/ft_str_isdigit.c new file mode 100644 index 0000000..6e720ba --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isdigit.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 13:39:43 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:40:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" +#include + +bool ft_str_isdigit(const char *str) +{ + if (!str) + return (false); + return (ft_str_isvalid(str, ft_isdigit)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isdouble.c b/libft_personal/src/ft_string/ft_str/ft_str_isdouble.c new file mode 100644 index 0000000..cccab8f --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isdouble.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isdouble.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:42:00 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 18:29:01 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +bool ft_str_isdouble(const char *str) +{ + return (ft_str_isfloat(str)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isfloat.c b/libft_personal/src/ft_string/ft_str/ft_str_isfloat.c new file mode 100644 index 0000000..6fabee7 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isfloat.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isfloat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:45:19 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:40:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_char.h" + +// no value check like isint due to the fact that it's a floating point +bool ft_str_isfloat(const char *str) +{ + size_t i; + bool dot; + + if (!str) + return (false); + i = 0; + dot = false; + if (str[i] == '-') + i++; + while (str[i]) + { + if (str[i] == '.' && !dot) + dot = true; + else if (!ft_isdigit(str[i])) + return (false); + i++; + } + if (i == 0 || (i == 1 && (str[0] == '.' || str[0] == '-')) || \ + (i == 2 && str[0] == '-' && str[1] == '.') || \ + i > 20) + return (false); + return (true); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_ishex.c b/libft_personal/src/ft_string/ft_str/ft_str_ishex.c new file mode 100644 index 0000000..a4d4d9e --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_ishex.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_ishex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:46:22 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:40:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" + +bool ft_str_ishex(const char *str) +{ + if (!str) + return (false); + if (ft_strstart_with(str, "0X") || ft_strstart_with(str, "0x")) + str += 2; + return (ft_str_isvalid(str, ft_ishexdigit)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isint.c b/libft_personal/src/ft_string/ft_str/ft_str_isint.c new file mode 100644 index 0000000..d2a9148 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isint.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:47:57 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" +#include + +// 0 success, !0 failure +bool ft_str_isint(const char *str) +{ + size_t i; + + if (!str) + return (false); + i = 0; + if (str[i] == '-') + i++; + while (str[i]) + { + if (!ft_isdigit(str[i])) + return (false); + i++; + } + if ((i == 10 && ft_strcmp(str, "2147483647") > 0) || i > 11 || \ + (i == 11 && ft_strcmp(str, "-2147483648") > 0)) + return (false); + if (i == 0 || (i == 1 && (str[0] == '-'))) + return (false); + return (true); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_islong.c b/libft_personal/src/ft_string/ft_str/ft_str_islong.c new file mode 100644 index 0000000..7d956f5 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_islong.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_islong.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:50:51 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_string.h" +#include "ft_char.h" + +bool ft_str_islong(const char *str) +{ + size_t i; + + i = 0; + if (!str) + return (false); + if (str[i] == '-') + i++; + while (str[i]) + { + if (!ft_isdigit(str[i])) + return (false); + i++; + } + if ((i == 19 && ft_strcmp(str, "9223372036854775807") > 0) || (i == 20 + && ft_strcmp(str, "-9223372036854775808") > 0) || i > 20) + return (false); + if (i == 0 || (i == 1 && (str[0] == '-'))) + return (false); + return (true); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isnum.c b/libft_personal/src/ft_string/ft_str/ft_str_isnum.c new file mode 100644 index 0000000..78e35a9 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isnum.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 15:56:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" +#include + +bool ft_str_isnum(const char *str) +{ + int i; + + if (!str) + return (false); + i = 0; + if (str[i] == '-' || str[i] == '+') + i++; + return (ft_str_isvalid(str + i, ft_isdigit)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isoct.c b/libft_personal/src/ft_string/ft_str/ft_str_isoct.c new file mode 100644 index 0000000..6aca72c --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isoct.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isoct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:49:53 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:40 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "ft_string.h" + +bool ft_str_isoct(const char *str) +{ + if (!str) + return (false); + if (!ft_strstart_with(str, "0o") && !ft_strstart_with(str, "0O")) + return (false); + return (ft_str_isvalid(str + 2, ft_isoctdigit)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_isvalid.c b/libft_personal/src/ft_string/ft_str/ft_str_isvalid.c new file mode 100644 index 0000000..bbee6a0 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_isvalid.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_isvalid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/21 15:53:23 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:41:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +bool ft_str_isvalid(const char *str, int (*is_type)(int)) +{ + size_t i; + + if (!str) + return (false); + i = 0; + while (str[i]) + { + if (!is_type(str[i])) + return (false); + i++; + } + if (i == 0) + return (false); + return (true); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_str_replace.c b/libft_personal/src/ft_string/ft_str/ft_str_replace.c new file mode 100644 index 0000000..1b9246b --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_str_replace.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_str_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 18:35:51 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:16:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +static bool setup(char **res, const char *targ, size_t targ_len, size_t sub_len) +{ + char *occurence; + size_t nb_occurence; + + occurence = ft_strnstr(*res, targ, ft_strlen(*res)); + nb_occurence = 0; + while (occurence) + { + occurence = ft_strnstr(occurence + 1, targ, ft_strlen(occurence + 1)); + nb_occurence++; + } + *res = ft_malloc(ft_strlen(*res) + (sub_len - targ_len) * nb_occurence + 1); + if (!*res) + return (false); + return (true); +} + +char *ft_str_replace(const char *str, const char *target, \ + const char *substitute) +{ + char *res; + char *ret; + char *occurence; + const size_t target_len = ft_strlen(target); + const size_t substitute_len = ft_strlen(substitute); + + occurence = ft_strnstr(str, target, ft_strlen(str)); + if (!occurence) + return (ft_strdup(str)); + res = (char *)str; + if (!setup(&res, target, target_len, substitute_len)) + return (NULL); + ret = res; + while (occurence) + { + ft_memcpy(res, str, occurence - str); + res += occurence - str; + ft_memcpy(res, substitute, substitute_len); + res += substitute_len; + str = occurence + target_len; + occurence = ft_strnstr(str, target, ft_strlen(str)); + } + return (ft_strlcpy(res, str, ft_strlen(str) + 1), ret); +} + +char *ft_str_replace_chr(char *str, char to_replace, char replace_by) +{ + char *ptr; + + ptr = str; + str = ft_strchr(str, to_replace); + while (str && *str) + { + *str = replace_by; + str++; + str = ft_strchr(str, to_replace); + } + return (ptr); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strappend_c.c b/libft_personal/src/ft_string/ft_str/ft_strappend_c.c new file mode 100644 index 0000000..1b4c86b --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strappend_c.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strappend_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/10 12:02:19 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 07:59:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_strappend_c(char **str, char c) +{ + size_t len; + char *prev; + + if (!str) + return (NULL); + len = 0; + if (*str) + len = ft_strlen(*str); + prev = *str; + *str = ft_realloc(*str, len + 2, len); + if (!*str) + return (*str = prev, NULL); + (*str)[len] = c; + (*str)[len + 1] = '\0'; + return (*str); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strchr.c b/libft_personal/src/ft_string/ft_str/ft_strchr.c new file mode 100644 index 0000000..3ac7c5c --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:46:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 17:54:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strchr(const char *str, int c) +{ + char *ret; + int i; + const char target = (char)c; + + ret = NULL; + i = 0; + while (str[i] && !ret) + { + if (str[i] == target) + ret = (char *)str + i; + i++; + } + if (!str[i] && target == 0) + return ((char *)str + i); + return ((char *)ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strclen.c b/libft_personal/src/ft_string/ft_str/ft_strclen.c new file mode 100644 index 0000000..9ace267 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strclen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/28 11:00:55 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 20:03:48 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strclen(char *str, char c) +{ + size_t i; + + i = 0; + while (str[i] && str[i] != c) + i++; + return (i); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strcmp.c b/libft_personal/src/ft_string/ft_str/ft_strcmp.c new file mode 100644 index 0000000..ae0ecdd --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strcmp.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/08 11:05:16 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 19:45:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_math.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + return (ft_strncmp(s1, s2, ft_max(ft_strlen(s1), ft_strlen(s2)))); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strcnb.c b/libft_personal/src/ft_string/ft_str/ft_strcnb.c new file mode 100644 index 0000000..19f5960 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strcnb.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcnb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/28 10:58:46 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 20:04:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strcnb(char *str, char c) +{ + size_t i; + size_t nb; + + i = 0; + nb = 0; + if (!str) + return (0); + while (str[i]) + if (str[i++] == c) + nb++; + if (c == '\0') + nb++; + return (nb); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strcspn.c b/libft_personal/src/ft_string/ft_str/ft_strcspn.c new file mode 100644 index 0000000..d2b2717 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strcspn.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 23:02:45 by bgoulard #+# #+# */ +/* Updated: 2024/05/10 09:11:18 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +size_t ft_strcspn(const char *str, const char *charset) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (ft_strchr(charset, str[i])) + return (i); + i++; + } + return (i); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strdup.c b/libft_personal/src/ft_string/ft_str/ft_strdup.c new file mode 100644 index 0000000..3dfb3d4 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strdup.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:33:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:52:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_strdup(const char *strsrc) +{ + return (ft_strndup(strsrc, ft_strlen(strsrc))); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strend_with.c b/libft_personal/src/ft_string/ft_str/ft_strend_with.c new file mode 100644 index 0000000..819a8e9 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strend_with.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strend_with.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/08 14:05:34 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 20:05:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_strend_with(const char *str, const char *end) +{ + int i; + int j; + + i = ft_strlen(str) - 1; + j = ft_strlen(end) - 1; + if (j > i) + return (0); + while (j >= 0) + { + if (str[i] != end[j]) + return (0); + i--; + j--; + } + return (1); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strerror.c b/libft_personal/src/ft_string/ft_str/ft_strerror.c new file mode 100644 index 0000000..0ee9821 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strerror.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strerror.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 11:43:23 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:19:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static const char *block_1(int errnum) +{ + static const char *str[] = {\ + "Success", "Operation not permitted", \ + "No such file or directory", "No such process", "Interrupted system call", \ + "Input/output error", "No such device or address", \ + "Argument list too long", "Exec format error", "Bad file descriptor", \ + "No child processes", "Resource temporarily unavailable", \ + "Cannot allocate memory", "Permission denied", "Bad address", \ + "Block device required", "Device or resource busy", "File exists", \ + "Invalid cross-device link", "No such device", "Not a directory", \ + "Is a directory", "Invalid argument", "Too many open files in system", \ + "Too many open files", "Inappropriate ioctl for device", "Text file busy", \ + "File too large", "No space left on device", "Illegal seek", \ + "Read-only file system", "Too many links", "Broken pipe", \ + "Numerical argument out of domain", "Numerical result out of range", \ + "Resource deadlock avoided", "File name too long", "No locks available", \ + "Function not implemented", "Directory not empty", \ + "Too many levels of symbolic links", "Unknown error 41", \ + "No message of desired type", "Identifier removed", \ + "Channel number out of range", "Level 2 not synchronized", \ + "Level 3 halted", "Level 3 reset", "Link number out of range", \ + "Protocol driver not attached", "No CSI structure available"}; + + return (str[errnum]); +} + +static const char *block_2(int errnum) +{ + static const char *str[] = {\ + "Level 2 halted", "Invalid exchange", "Invalid request descriptor", \ + "Exchange full", "No anode", "Invalid request code", \ + "Invalid slot", "Unknown error 58", \ + "Bad font file format", "Device not a stream", "No data available", \ + "Timer expired", "Out of streams resources", \ + "Machine is not on the network", "Package not installed", \ + "Object is remote", "Link has been severed", "Advertise error", \ + "Srmount error", "Communication error on send", "Protocol error", \ + "Multihop attempted", "RFS specific error", "Bad message", \ + "Value too large for defined data type", "Name not unique on network", \ + "File descriptor in bad state", "Remote address changed", \ + "Can not access a needed shared library", \ + "Accessing a corrupted shared library", ".lib section in a.out corrupted", \ + "Attempting to link in too many shared libraries", \ + "Cannot exec a shared library directly", \ + "Invalid or incomplete multibyte or wide character", \ + "Interrupted system call should be restarted", "Streams pipe error", \ + "Too many users", "Socket operation on non-socket", \ + "Destination address required", "Message too long", \ + "Protocol wrong type for socket", "Protocol not available"}; + + return (str[errnum - 51]); +} + +static const char *block_3(int errnum) +{ + static const char *str[] = {\ + "Protocol not supported", "Socket type not supported", \ + "Operation not supported", "Protocol family not supported", \ + "Address family not supported by protocol", "Address already in use", \ + "Cannot assign requested address", "Network is down", \ + "Network is unreachable", "Network dropped connection on reset", \ + "Software caused connection abort", "Connection reset by peer", \ + "No buffer space available", "Transport endpoint is already connected", \ + "Transport endpoint is not connected", \ + "Cannot send after transport endpoint shutdown", \ + "Too many references: cannot splice", "Connection timed out", \ + "Connection refused", "Host is down", "No route to host", \ + "Operation already in progress", "Operation now in progress", \ + "Stale file handle", "Structure needs cleaning", \ + "Not a XENIX named type file", "No XENIX semaphores available", \ + "Is a named type file", "Remote I/O error", "Disk quota exceeded", \ + "No medium found", "Wrong medium type", "Operation canceled", \ + "Required key not available", "Key has expired", "Key has been revoked", \ + "Key was rejected by service", "Owner died", "State not recoverable", \ + "Operation not possible due to RF-kill", "Memory page has hardware error"}; + + return (str[errnum - 93]); +} + +const char *ft_strerror(int errnum) +{ + if (errnum > 133) + return (NULL); + if (errnum < 51) + return (block_1(errnum)); + if (errnum < 93) + return (block_2(errnum)); + return (block_3(errnum)); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_striteri.c b/libft_personal/src/ft_string/ft_str/ft_striteri.c new file mode 100644 index 0000000..ecbae9a --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:36:23 by bgoulard #+# #+# */ +/* Updated: 2023/11/12 09:59:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int index; + + if (!s || !f) + return ; + index = 0; + while (s[index]) + { + f(index, (char *)s + index); + index++; + } +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strjoin.c b/libft_personal/src/ft_string/ft_str/ft_strjoin.c new file mode 100644 index 0000000..5597e91 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strjoin.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:55:53 by bgoulard #+# #+# */ +/* Updated: 2023/12/05 10:27:52 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t tot; + size_t off; + char *ret; + + tot = 0; + if (s1) + tot += ft_strlen(s1); + if (s2) + tot += ft_strlen(s2); + ret = ft_calloc(sizeof(char), (tot + 1)); + if (!ret) + return (NULL); + off = 0; + tot = 0; + while (s1 && s1[off]) + { + ret[off] = s1[off]; + off++; + } + while (s2 && s2[tot]) + ret[off++] = s2[tot++]; + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strlcat.c b/libft_personal/src/ft_string/ft_str/ft_strlcat.c new file mode 100644 index 0000000..2bed530 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strlcat.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:11:59 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:15:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t dst_len; + size_t src_len; + size_t i; + + if (!size || !dst) + return (0); + if (!src) + return (ft_strlen(dst)); + dst_len = ft_strlen(dst); + src_len = ft_strlen(src); + if (dst_len >= size) + return (size + src_len); + i = 0; + while (src[i] && dst_len + i < size - 1) + { + dst[dst_len + i] = src[i]; + i++; + } + dst[dst_len + i] = 0; + return (dst_len + src_len); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strlcpy.c b/libft_personal/src/ft_string/ft_str/ft_strlcpy.c new file mode 100644 index 0000000..b02004b --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strlcpy.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:21:14 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:39:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t src_len; + size_t dst_len; + + if (!dst) + return (size); + if (!src) + return (ft_strlen(dst)); + if (!size) + return (ft_strlen(src)); + src_len = ft_strlen(src); + dst_len = 0; + while (dst_len < size - 1 && src[dst_len]) + { + dst[dst_len] = src[dst_len]; + dst_len++; + } + dst[dst_len] = 0; + return (src_len); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strlen.c b/libft_personal/src/ft_string/ft_str/ft_strlen.c new file mode 100644 index 0000000..5912549 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:46:04 by bgoulard #+# #+# */ +/* Updated: 2023/11/10 12:51:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +size_t ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strmapi.c b/libft_personal/src/ft_string/ft_str/ft_strmapi.c new file mode 100644 index 0000000..84ea52c --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strmapi.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 09:39:40 by bgoulard #+# #+# */ +/* Updated: 2023/12/04 15:33:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_string.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *ret; + unsigned int index; + + index = 0; + if (!f || !s) + return (NULL); + ret = ft_calloc(sizeof(char), (ft_strlen(s) + 1)); + if (!ret) + return (ret); + while (s[index++]) + ret[index - 1] = f(index - 1, s[index - 1]); + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strncmp.c b/libft_personal/src/ft_string/ft_str/ft_strncmp.c new file mode 100644 index 0000000..df0670d --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strncmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:56:17 by bgoulard #+# #+# */ +/* Updated: 2023/12/13 08:25:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + const unsigned char *str1 = (const unsigned char *)s1; + const unsigned char *str2 = (const unsigned char *)s2; + size_t index; + + index = 0; + if (n == 0) + return (0); + while (str1[index] == str2[index] && str1[index] && index < n - 1) + index++; + return (str1[index] - str2[index]); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strndup.c b/libft_personal/src/ft_string/ft_str/ft_strndup.c new file mode 100644 index 0000000..33242e5 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strndup.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 18:16:47 by bgoulard #+# #+# */ +/* Updated: 2024/05/21 17:53:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_strndup(const char *str, size_t n) +{ + char *ret; + size_t index; + + ret = ft_calloc(sizeof(char), (n + 1)); + if (!ret) + return (ret); + index = 0; + while (index < n && str[index]) + { + ret[index] = str[index]; + index++; + } + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strnstr.c b/libft_personal/src/ft_string/ft_str/ft_strnstr.c new file mode 100644 index 0000000..63af76e --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strnstr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:55:09 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 09:09:52 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_strnstr(const char *big, const char *small, size_t n) +{ + size_t len_small; + const size_t len_big = n; + + if (!big || !small || n == 0) + return (NULL); + len_small = ft_strlen(small); + while (n-- > len_small && *big) + if (ft_strncmp(big++, small, len_small) == 0) + return ((char *)big - 1); + if (len_big >= len_small && ft_strncmp(big, small, len_small) == 0) + return ((char *)big); + return (NULL); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strrchr.c b/libft_personal/src/ft_string/ft_str/ft_strrchr.c new file mode 100644 index 0000000..4ce8653 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strrchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:46:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/13 17:54:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strrchr(const char *str, int c) +{ + char *ret; + int i; + const char target = (char)c; + + ret = NULL; + i = 0; + while (str[i]) + { + if (str[i] == target) + ret = (char *)str + i; + i++; + } + if (str[i] == target) + return ((char *)str + i); + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strspn.c b/libft_personal/src/ft_string/ft_str/ft_strspn.c new file mode 100644 index 0000000..b47a12e --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strspn.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 23:02:45 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 11:02:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +size_t ft_strspn(const char *str, const char *charset) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (!ft_strchr(charset, str[i])) + return (i); + i++; + } + return (i); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strstart_with.c b/libft_personal/src/ft_string/ft_str/ft_strstart_with.c new file mode 100644 index 0000000..9ef71a9 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strstart_with.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstart_with.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/08 14:07:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/18 20:05:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_strstart_with(const char *str, const char *start) +{ + if (ft_strlen(start) > ft_strlen(str)) + return (0); + return (ft_strncmp(str, start, ft_strlen(start)) == 0); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strtok.c b/libft_personal/src/ft_string/ft_str/ft_strtok.c new file mode 100644 index 0000000..13364b5 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strtok.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtok.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/08 13:19:26 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:52:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_strtok(char *str, const char *token) +{ + static char *ptr; + char *ret; + + if (str) + ptr = str; + while (ptr && ft_strchr(token, *ptr) && *ptr) + ptr++; + ret = ptr; + while (ptr && !ft_strchr(token, *ptr)) + ptr++; + if (ptr && *ptr) + { + *ptr = 0; + ptr++; + } + else + ptr = 0; + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_strtrim.c b/libft_personal/src/ft_string/ft_str/ft_strtrim.c new file mode 100644 index 0000000..0d08ca7 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_strtrim.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 21:03:17 by bgoulard #+# #+# */ +/* Updated: 2023/12/05 10:28:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +static int loc_strchr(const char *str, int c) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] == c) + return (i); + i++; + } + return (-1); +} + +static size_t loc_strtrim_size(size_t *offset, char const *s1, + char const *set) +{ + size_t ret_size; + + ret_size = ft_strlen(s1); + *offset = 0; + while (ret_size > 0 && loc_strchr(set, s1[ret_size - 1]) != -1) + ret_size--; + while (ret_size && s1[*offset] && loc_strchr(set, s1[*offset]) != -1) + { + (*offset)++; + ret_size--; + } + return (ret_size); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + char *ret; + size_t ret_size; + size_t offset; + size_t offset_ret; + + if (!s1) + return (NULL); + if (!set) + return (ft_strdup(s1)); + offset_ret = 0; + ret_size = loc_strtrim_size(&offset, s1, set); + if (ret_size == 0) + return (ft_strdup("")); + ret = ft_calloc(sizeof(char), (ret_size + 1)); + if (!ret) + return (ret); + while (s1[offset] && ret_size--) + ret[offset_ret++] = s1[offset++]; + offset--; + while (loc_strchr(set, s1[offset--]) != -1) + ret[offset_ret--] = 0; + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_substr.c b/libft_personal/src/ft_string/ft_str/ft_substr.c new file mode 100644 index 0000000..6c9cf34 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_substr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 20:46:31 by bgoulard #+# #+# */ +/* Updated: 2023/12/05 10:28:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *res; + size_t i; + + if (!s) + return (NULL); + if (start >= ft_strlen(s)) + return (ft_strdup("")); + i = ft_strlen(s + start); + if (len < i) + i = len; + res = ft_calloc(sizeof(char), (i + 1)); + if (!res) + return (res); + i = 0; + while (s[start + i] && i < len) + { + res[i] = s[start + i]; + i++; + } + return (res); +} diff --git a/libft_personal/src/ft_string/ft_str/ft_utoa.c b/libft_personal/src/ft_string/ft_str/ft_utoa.c new file mode 100644 index 0000000..605180f --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/ft_utoa.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_utoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 14:54:49 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 09:04:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +static int loc_getlen(unsigned long long nbr) +{ + size_t nbr_len; + + nbr_len = 0; + if (nbr == 0) + return (1); + while (nbr) + { + nbr_len++; + nbr /= 10; + } + return (nbr_len); +} + +char *ft_utoa(unsigned int nbr) +{ + char *ret; + size_t off; + unsigned long long srcnbr; + const int nbrlen = loc_getlen((unsigned long long)nbr); + + srcnbr = (unsigned long long)nbr; + ret = ft_calloc(sizeof(char), (nbrlen + 1)); + if (!ret) + return (NULL); + if (srcnbr == 0) + ret[0] = '0'; + off = 0; + while (srcnbr) + { + ret[nbrlen - ++off] = "0123456789"[srcnbr % 10]; + srcnbr /= 10; + } + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_str/get_next_line.c b/libft_personal/src/ft_string/ft_str/get_next_line.c new file mode 100644 index 0000000..7feafc8 --- /dev/null +++ b/libft_personal/src/ft_string/ft_str/get_next_line.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/13 11:02:12 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:16:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string_types.h" +#include "ft_string.h" + +static int ft_strchr_index(const char *from, char c) +{ + int i; + + i = 0; + if (!from) + return (-1); + while (from[i] && from[i] != c) + i++; + if (from[i] == c) + return (i); + return (-1); +} + +static int mvstr(char *str, int nb, char delim) +{ + size_t off; + + if (ft_strchr_index(str, delim) != -1) + nb++; + off = 0; + while (off != BUFFER_SIZE) + { + if (off + nb < BUFFER_SIZE && str[off + nb]) + str[off] = str[off + nb]; + else + str[off] = 0; + off++; + } + return (0); +} + +static int split_from(char **dst, char *from, int pos, char delim) +{ + char *ret; + size_t or_len; + size_t len; + size_t i; + + or_len = 0; + if (*dst) + or_len += ft_strlen(*dst); + len = or_len + pos + 1; + ret = ft_malloc(sizeof(char) * (len + 1)); + if (!ret) + return (-1); + ret[len] = 0; + i = 0; + while (i != len) + { + if (i < or_len) + ret[i] = (*dst)[i]; + else + ret[i] = from[i - or_len]; + i++; + } + if (*dst) + free(*dst); + return (*dst = ret, (mvstr(from, pos, delim) || 0)); +} + +char *get_next_line(int fd) +{ + static char loc_buff[1024][BUFFER_SIZE + 1] = {0}; + char *ret; + int rep; + + ret = NULL; + while (ft_strchr_index(loc_buff[fd], '\n') == -1) + { + if (loc_buff[fd][0] && \ + split_from(&ret, loc_buff[fd], ft_strlen(loc_buff[fd]), '\n')) + return (NULL); + rep = read(fd, loc_buff[fd], BUFFER_SIZE); + if (rep <= 0) + return (ret); + loc_buff[fd][rep] = 0; + if (rep != BUFFER_SIZE) + break ; + } + rep = ft_strchr_index(loc_buff[fd], '\n'); + if (rep == -1) + rep = ft_strlen(loc_buff[fd]); + if (split_from(&ret, loc_buff[fd], rep, '\n')) + return (NULL); + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_append.c b/libft_personal/src/ft_string/ft_string/ft_string_append.c new file mode 100644 index 0000000..761547e --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_append.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_append.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/02 22:24:43 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:45:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_append(t_string *str, const char *append) +{ + size_t len; + + len = ft_strlen(append) + 1; + if (str->length + len >= str->capacity) + { + if (ft_string_resize(str, str->capacity + len) == -1) + return (-1); + } + ft_memcpy(str->str + str->length, append, len); + str->length += len - 1; + return (0); +} + +int ft_string_append_n(t_string *str, const char *append, size_t n) +{ + if (str->length + n + 1 >= str->capacity) + { + if (ft_string_resize(str, str->capacity + n + 1) == -1) + return (-1); + } + ft_memcpy(str->str + str->length, append, n); + str->length += n; + str->str[str->length] = '\0'; + return (0); +} + +int ft_string_append_c(t_string *str, char c) +{ + if (str->length + 2 >= str->capacity) + { + if (ft_string_resize(str, str->capacity + 2) == -1) + return (-1); + } + str->str[str->length] = c; + str->str[str->length + 1] = '\0'; + str->length += 1; + return (0); +} + +int ft_string_append_s(t_string *str, t_string *append) +{ + if (str->length + append->length + 1 >= str->capacity) + { + if (ft_string_resize(str, str->capacity + append->length + 1) == -1) + return (-1); + } + ft_memcpy(str->str + str->length, append->str, append->length + 1); + str->length += append->length; + str->str[str->length] = '\0'; + return (0); +} + +int ft_string_append_s_n(t_string *str, t_string *append, size_t n) +{ + if (str->length + n + 1 >= str->capacity) + { + if (ft_string_resize(str, str->capacity + n + 1) == -1) + return (-1); + } + ft_memcpy(str->str + str->length, append->str, n); + str->length += n; + str->str[str->length] = '\0'; + return (0); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_chr.c b/libft_personal/src/ft_string/ft_string/ft_string_chr.c new file mode 100644 index 0000000..95a1012 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_chr.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:51:16 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:38:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_string_chr(const t_string *str, char c) +{ + size_t i; + + i = 0; + while (i < str->length) + { + if (str->str[i] == c) + return (str->str + i); + i++; + } + if (c == '\0') + return (str->str + i); + return (NULL); +} + +char *ft_string_rchr(const t_string *str, char c) +{ + size_t i; + + i = str->length; + while (i > 0) + { + if (str->str[i] == c) + return (str->str + i); + i--; + } + if (str->str[i] == c) + return (str->str + i); + return (NULL); +} + +ssize_t ft_string_offset(const t_string *str, char c) +{ + char *ptr; + + ptr = ft_string_chr(str, c); + if (ptr) + return (ptr - str->str); + return (-1); +} + +ssize_t ft_string_roffset(const t_string *str, char c) +{ + char *ptr; + + ptr = ft_string_rchr(str, c); + if (ptr) + return (ptr - str->str); + return (-1); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_clear.c b/libft_personal/src/ft_string/ft_string/ft_string_clear.c new file mode 100644 index 0000000..e18f451 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_clear.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:20:00 by bgoulard #+# #+# */ +/* Updated: 2023/12/09 17:20:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void ft_string_clear(t_string *str) +{ + ft_bzero(str->str, str->length); + str->length = 0; +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_cmp.c b/libft_personal/src/ft_string/ft_string/ft_string_cmp.c new file mode 100644 index 0000000..4e720cb --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_cmp.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_cmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:50:20 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:27:34 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_cmp(const t_string *str, const char *cmp) +{ + return (ft_strcmp(str->str, cmp)); +} + +int ft_string_ncmp(const t_string *str, const char *cmp, size_t n) +{ + return (ft_strncmp(str->str, cmp, n)); +} + +int ft_string_cmpstr(const t_string *str, const t_string *cmp) +{ + if (str->length != cmp->length) + return (str->length - cmp->length); + return (ft_strcmp(str->str, cmp->str)); +} + +int ft_string_ncmpstr(const t_string *str, const t_string *cmp, size_t n) +{ + if (n < str->length && n < cmp->length) + return (ft_strncmp(str->str, cmp->str, n)); + if (str->length != cmp->length) + return (str->length - cmp->length); + return (ft_strcmp(str->str, cmp->str)); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_destroy.c b/libft_personal/src/ft_string/ft_string/ft_string_destroy.c new file mode 100644 index 0000000..376b3c3 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:20:41 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 17:45:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void ft_string_destroy(t_string **str) +{ + if (!str || !*str) + return ; + free((*str)->str); + free(*str); + *str = NULL; +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_from.c b/libft_personal/src/ft_string/ft_string/ft_string_from.c new file mode 100644 index 0000000..d1c86f6 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_from.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_from.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:14:14 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:40:36 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_math.h" + +// +1 for the null terminator +t_string *ft_string_from(const char *str) +{ + t_string *new; + size_t len; + + if (!str) + return (ft_string_new(0)); + len = ft_strlen(str); + new = ft_string_new(len + 1); + if (!new) + return (NULL); + ft_memcpy(new->str, str, len + 1); + new->length = len; + return (new); +} + +t_string *ft_string_from_n(const char *str, size_t n) +{ + t_string *new; + + new = ft_string_new(n + 1); + if (!new) + return (NULL); + if (n == 0 || !str) + return (new); + ft_memcpy(new->str, str, n); + new->str[n] = '\0'; + new->length = n; + return (new); +} + +t_string *ft_string_from_c(char c) +{ + t_string *new; + + new = ft_string_new(2); + if (!new) + return (NULL); + new->str[0] = c; + new->str[1] = '\0'; + new->length = 1; + return (new); +} + +t_string *ft_string_from_s(const t_string *str) +{ + t_string *new; + + if (!str) + return (ft_string_new(0)); + new = ft_string_new(str->length + 1); + if (!new) + return (NULL); + ft_memcpy(new->str, str->str, str->length + 1); + new->length = str->length; + return (new); +} + +t_string *ft_string_from_s_n(const t_string *str, size_t n) +{ + t_string *new; + size_t len; + + if (!str || n == 0) + return (ft_string_new(0)); + len = ft_min(n, str->length); + new = ft_string_new(len + 1); + if (!new) + return (NULL); + ft_memcpy(new->str, str->str, len); + new->str[len] = '\0'; + new->length = len; + return (new); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_get.c b/libft_personal/src/ft_string/ft_string/ft_string_get.c new file mode 100644 index 0000000..1d3246c --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_get.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:50:23 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 10:33:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +const char *ft_string_get(const t_string *str) +{ + return (str->str); +} + +size_t ft_string_len(const t_string *str) +{ + return (str->length); +} + +size_t ft_string_cap(const t_string *str) +{ + return (str->capacity); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_insert.c b/libft_personal/src/ft_string/ft_string/ft_string_insert.c new file mode 100644 index 0000000..396976c --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_insert.c @@ -0,0 +1,133 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:22:33 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:46:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_insert(t_string *str, const char *insert, size_t index) +{ + size_t insert_len; + size_t new_len; + char *new_str; + + insert_len = ft_strlen(insert); + if (index > str->length) + index = str->length; + new_len = str->length + insert_len; + new_str = ft_calloc(new_len + 1, sizeof(char)); + if (!new_str) + return (0); + ft_memcpy(new_str, str->str, index); + ft_memcpy(new_str + index, insert, insert_len); + ft_memcpy(new_str + index + insert_len, str->str + index, str->length + - index); + ft_free((void **)&str->str); + str->str = new_str; + if (index == str->length) + str->str[new_len] = '\0'; + str->length = new_len; + str->capacity += insert_len + 1; + return (1); +} + +int ft_string_insert_n(t_string *str, const char *insert, size_t index, size_t n) +{ + size_t new_len; + char *new_str; + + if (index > str->length) + index = str->length; + new_len = str->length + n; + new_str = ft_calloc(new_len + 1, sizeof(char)); + if (!new_str) + return (0); + ft_memcpy(new_str, str->str, index); + ft_memcpy(new_str + index, insert, n); + ft_memcpy(new_str + index + n, str->str + index, str->length - index); + ft_free((void **)&str->str); + str->str = new_str; + if (index == str->length) + str->str[new_len] = '\0'; + str->length = new_len; + str->capacity = new_len + 1; + return (1); +} + +int ft_string_insert_c(t_string *str, char insert, size_t index) +{ + size_t new_len; + char *new_str; + + if (index > str->length) + index = str->length; + new_len = str->length + 1; + new_str = ft_calloc(new_len + 1, sizeof(char)); + if (!new_str) + return (0); + ft_memcpy(new_str, str->str, index); + new_str[index] = insert; + ft_memcpy(new_str + index + 1, str->str + index, str->length - index); + ft_free((void **)&str->str); + str->str = new_str; + if (index == str->length) + str->str[new_len] = '\0'; + str->length = new_len; + str->capacity = new_len + 1; + return (1); +} + +int ft_string_insert_s(t_string *str, t_string *insert, size_t index) +{ + size_t new_len; + char *new_str; + + if (index > str->length) + index = str->length; + new_len = str->length + insert->length; + new_str = ft_calloc(new_len + 1, sizeof(char)); + if (!new_str) + return (0); + ft_memcpy(new_str, str->str, index); + ft_memcpy(new_str + index, insert->str, insert->length); + ft_memcpy(new_str + index + insert->length, str->str + index, str->length + - index); + ft_free((void **)&str->str); + str->str = new_str; + if (index == str->length) + str->str[new_len] = '\0'; + str->length = new_len; + str->capacity = new_len + 1; + return (1); +} + +int ft_string_insert_s_n(t_string *str, t_string *insert, size_t index, + size_t n) +{ + size_t new_len; + char *new_str; + + if (index > str->length) + index = str->length; + new_len = str->length + n; + new_str = ft_calloc(new_len + 1, sizeof(char)); + if (!new_str) + return (0); + ft_memcpy(new_str, str->str, index); + ft_memcpy(new_str + index, insert->str, n); + ft_memcpy(new_str + index + n, str->str + index, str->length - index); + ft_free((void **)&str->str); + str->str = new_str; + if (index == str->length) + str->str[new_len] = '\0'; + str->length = new_len; + str->capacity = new_len + 1; + return (1); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_new.c b/libft_personal/src/ft_string/ft_string/ft_string_new.c new file mode 100644 index 0000000..47b70ee --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_new.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:00:19 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:22:47 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +t_string *ft_string_new(size_t capacity) +{ + t_string *new; + + new = ft_calloc(1, sizeof(t_string)); + if (!new) + return (NULL); + if (capacity < T_STRING_BUFF) + capacity = T_STRING_BUFF; + new->str = ft_calloc(capacity, sizeof(char)); + if (!new->str) + return (ft_free((void **)&new), NULL); + new->capacity = capacity; + new->length = 0; + return (new); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_put.c b/libft_personal/src/ft_string/ft_string/ft_string_put.c new file mode 100644 index 0000000..89524f6 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_put.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/07 03:49:59 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:46:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string_types.h" +#include + +int ft_string_put(const t_string *str, int fd) +{ + int ret; + + if (fd < 0 || !str || (!str->str && str->length != 0)) + return (-1); + ret = write(fd, str->str, str->length); + return (ret); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_replace.c b/libft_personal/src/ft_string/ft_string/ft_string_replace.c new file mode 100644 index 0000000..1b78ee3 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_replace.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:53:23 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:58:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_replace(t_string *str, const char *to_replace, const char *replace_by) +{ + char *tmp; + char *tmp2; + + tmp = (char *)ft_string_get(str); + if (!tmp) + return (0); + tmp2 = ft_str_replace(tmp, to_replace, replace_by); + if (!tmp2) + return (0); + ft_string_set_inplace(str, tmp2); + return (1); +} + +int ft_string_replace_chr(t_string *str, char to_replace, char replace_by) +{ + size_t i; + size_t nb_rep; + + if (!str->str) + return (0); + i = 0; + nb_rep = 0; + while (i < str->length) + { + if (str->str[i] == to_replace) + { + str->str[i] = replace_by; + nb_rep++; + } + i++; + } + if (nb_rep == 0) + return (0); + return (1); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_reserve.c b/libft_personal/src/ft_string/ft_string/ft_string_reserve.c new file mode 100644 index 0000000..e17d41f --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_reserve.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:26:02 by bgoulard #+# #+# */ +/* Updated: 2023/12/30 12:27:08 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_reserve(t_string *str, size_t size) +{ + char *new; + + if (size <= str->capacity) + return (0); + new = ft_realloc(str->str, str->capacity, size); + if (!new) + return (-1); + str->str = new; + str->capacity = size; + return (0); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_resize.c b/libft_personal/src/ft_string/ft_string/ft_string_resize.c new file mode 100644 index 0000000..19c9040 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_resize.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_resize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:26:28 by bgoulard #+# #+# */ +/* Updated: 2024/01/05 02:46:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_resize(t_string *str, size_t size) +{ + char *new; + + new = ft_realloc(str->str, size, str->capacity); + if (!new) + return (-1); + str->str = new; + str->capacity = size; + return (0); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_set.c b/libft_personal/src/ft_string/ft_string/ft_string_set.c new file mode 100644 index 0000000..dba8fb6 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_set.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 18:17:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:35:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_set(t_string *str, const char *new_str) +{ + return (ft_string_set_n(str, new_str, ft_strlen(new_str) + 1)); +} + +int ft_string_set_n(t_string *str, const char *new_str, size_t n) +{ + size_t new_len; + + new_len = ft_strlen(new_str) + 1; + if (new_len > n) + new_len = n; + if (new_len > str->capacity) + { + str->str = ft_realloc(str->str, new_len, str->capacity); + if (!str->str) + return (0); + str->capacity = new_len; + } + ft_memcpy(str->str, new_str, new_len - 1); + str->str[new_len - 1] = '\0'; + str->length = new_len - 1; + return (1); +} + +int ft_string_set_inplace(t_string *str, char *new_str) +{ + size_t new_len; + + new_len = ft_strlen(new_str); + str->length = new_len; + if (str->str) + free(str->str); + str->str = new_str; + str->capacity = new_len + 1; + return (1); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_shrink.c b/libft_personal/src/ft_string/ft_string/ft_string_shrink.c new file mode 100644 index 0000000..8c46e2e --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_shrink.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_shrink.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:31:09 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:56:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int ft_string_shrink(t_string *str) +{ + char *new; + + if (str->capacity <= str->length + 1) + return (0); + new = ft_realloc(str->str, str->length + 1, str->capacity); + if (!new) + return (-1); + str->str = new; + str->capacity = str->length + 1; + return (0); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_substr.c b/libft_personal/src/ft_string/ft_string/ft_string_substr.c new file mode 100644 index 0000000..18fdd62 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_substr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:42:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:58:29 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +t_string *ft_string_substr(t_string *str, size_t start, size_t len) +{ + t_string *new; + + if (start > str->length) + return (NULL); + if (len > str->length - start) + len = str->length - start; + new = ft_string_new(len); + if (!new) + return (NULL); + if (ft_string_cap(new) != len) + return (ft_string_destroy(&new), NULL); + ft_memcpy(new->str, str->str + start, len); + new->length = len - 1; + new->str[new->length] = '\0'; + return (new); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_to_str.c b/libft_personal/src/ft_string/ft_string/ft_string_to_str.c new file mode 100644 index 0000000..c989245 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_to_str.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_to_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:47:29 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 11:06:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +char *ft_string_to_str(t_string *str) +{ + char *new; + + new = ft_calloc(str->length + 1, sizeof(char)); + if (!new) + return (NULL); + ft_memcpy(new, str->str, str->length); + new[str->length] = '\0'; + return (new); +} diff --git a/libft_personal/src/ft_string/ft_string/ft_string_trim.c b/libft_personal/src/ft_string/ft_string/ft_string_trim.c new file mode 100644 index 0000000..3db8865 --- /dev/null +++ b/libft_personal/src/ft_string/ft_string/ft_string_trim.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_string_trim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 17:50:18 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:57:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +void ft_string_trim(t_string *str) +{ + return (ft_string_trimstr(str, " \t\n")); +} + +void ft_string_trim_chr(t_string *str, char c) +{ + const char to_trim[2] = {c, '\0'}; + + return (ft_string_trimstr(str, (char *)to_trim)); +} + +// placing the null terminator at the end of the string +// because str->str is not reset after. +void ft_string_trimstr(t_string *str, const char *to_trim) +{ + size_t rpad; + size_t lpad; + + lpad = 0; + if (!to_trim || !str || !str->str || str->length == 0) + return ; + while (ft_strchr(to_trim, str->str[lpad]) && lpad < str->length) + lpad++; + rpad = str->length - 1; + while (ft_strchr(to_trim, str->str[rpad]) && rpad > 0) + rpad--; + if (rpad < lpad) + return ((void)ft_string_set(str, "")); + ft_memmove(str->str, str->str + lpad, rpad - lpad + 1); + str->length = rpad - lpad + 1; + str->str[str->length] = '\0'; +} diff --git a/libft_personal/src/ft_vector/ft_vec_add.c b/libft_personal/src/ft_vector/ft_vec_add.c new file mode 100644 index 0000000..6ac77ea --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_add.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 15:59:23 by bgoulard #+# #+# */ +/* Updated: 2023/12/13 09:49:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +bool ft_vec_add(t_vector **vec, void *data) +{ + size_t nw_size; + + if ((*vec)->count == (*vec)->cappacity) + { + nw_size = (*vec)->cappacity + FT_VECTOR_BASE_LEN; + if (ft_vec_reserve(vec, nw_size) == false) + return (false); + } + (*vec)->datas[(*vec)->count++] = data; + return (true); +} diff --git a/libft_personal/src/ft_vector/ft_vec_apply.c b/libft_personal/src/ft_vector/ft_vec_apply.c new file mode 100644 index 0000000..fd6c96a --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_apply.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:09:28 by bgoulard #+# #+# */ +/* Updated: 2023/12/04 10:38:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +void ft_vec_apply(t_vector *vec, void (*func)(void *)) +{ + size_t i; + + i = 0; + while (i < vec->count) + func(vec->datas[i++]); + return ; +} diff --git a/libft_personal/src/ft_vector/ft_vec_at.c b/libft_personal/src/ft_vector/ft_vec_at.c new file mode 100644 index 0000000..e078789 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_at.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:03:56 by bgoulard #+# #+# */ +/* Updated: 2023/12/04 10:38:27 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +// return elem n +void *ft_vec_at(t_vector *vec, size_t n) +{ + return (vec->datas[n]); +} diff --git a/libft_personal/src/ft_vector/ft_vec_cat.c b/libft_personal/src/ft_vector/ft_vec_cat.c new file mode 100644 index 0000000..5b82083 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_cat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_cat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 15:40:24 by bgoulard #+# #+# */ +/* Updated: 2023/12/04 10:41:54 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +bool ft_vec_cat(t_vector **vec_a, const t_vector *vec_b) +{ + size_t i; + + i = 0; + if ((*vec_a)->cappacity < ((*vec_a)->count + vec_b->count)) + return (false); + while (i < vec_b->count) + { + (*vec_a)->datas[(*vec_a)->count + i] = vec_b->datas[i]; + i++; + } + (*vec_a)->count += i; + return (true); +} diff --git a/libft_personal/src/ft_vector/ft_vec_clear.c b/libft_personal/src/ft_vector/ft_vec_clear.c new file mode 100644 index 0000000..af569f3 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_clear.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 14:35:29 by bgoulard #+# #+# */ +/* Updated: 2023/12/13 09:34:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +void ft_vec_clear(t_vector *vec) +{ + size_t size; + + size = 0; + while (size < vec->count) + vec->datas[size++] = NULL; + vec->count = 0; +} diff --git a/libft_personal/src/ft_vector/ft_vec_destroy.c b/libft_personal/src/ft_vector/ft_vec_destroy.c new file mode 100644 index 0000000..dd50413 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_destroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:03:20 by bgoulard #+# #+# */ +/* Updated: 2023/12/13 10:07:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include + +bool ft_vec_destroy(t_vector **vec) +{ + (*vec)->count = 0; + (*vec)->cappacity = 0; + free((*vec)->datas); + free(*vec); + *vec = NULL; + return (true); +} diff --git a/libft_personal/src/ft_vector/ft_vec_filter.c b/libft_personal/src/ft_vector/ft_vec_filter.c new file mode 100644 index 0000000..3932ad6 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_filter.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_filter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:10:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 12:39:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_vector.h" + +void ft_vec_filter(t_vector *vec, t_data_is func, t_data_apply del) +{ + size_t i; + size_t shift_count; + + i = 0; + while (vec->count > i) + { + shift_count = 0; + while (vec->count > i && func(vec->datas[i]) == false) + { + if (del) + del(vec->datas[i]); + shift_count++; + vec->datas[i++] = NULL; + } + ft_vec_shift(vec, i - shift_count, shift_count); + while (vec->count > i && func(vec->datas[i]) == true) + i++; + } + return ; +} diff --git a/libft_personal/src/ft_vector/ft_vec_get.c b/libft_personal/src/ft_vector/ft_vec_get.c new file mode 100644 index 0000000..db07d00 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_get.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 14:24:49 by bgoulard #+# #+# */ +/* Updated: 2024/05/10 09:33:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector_types.h" + +void *ft_vec_get(t_vector *vector, const void *key, \ + int (*cmp)(const void *v_data, const void *key)) +{ + size_t i; + + i = 0; + while (i < vector->count) + { + if (cmp(vector->datas[i], key) == 0) + return (vector->datas[i]); + i++; + } + return (NULL); +} diff --git a/libft_personal/src/ft_vector/ft_vec_map.c b/libft_personal/src/ft_vector/ft_vec_map.c new file mode 100644 index 0000000..a4af096 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_map.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:08:55 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 12:41:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +t_vector *ft_vec_map(t_vector *vec, t_data_tr func) +{ + t_vector *ret; + size_t i; + + i = 0; + ret = ft_vec_from_size(vec->count); + if (!ret) + return (NULL); + while (i < vec->count) + { + ret->datas[i] = func(vec->datas[i]); + i++; + } + ret->count = i; + return (ret); +} diff --git a/libft_personal/src/ft_vector/ft_vec_new.c b/libft_personal/src/ft_vector/ft_vec_new.c new file mode 100644 index 0000000..580cbaf --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_new.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 14:13:51 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:28:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_string.h" +#include + +t_vector *ft_vec_new(void) +{ + return (ft_vec_from_size(FT_VECTOR_BASE_LEN)); +} + +t_vector *ft_vec_from_size(size_t n) +{ + t_vector *ret; + + ret = ft_calloc(sizeof(t_vector), 1); + if (!ret) + return (ret); + ret->datas = ft_calloc(sizeof(void *), n); + if (!ret->datas) + return (free(ret), NULL); + ret->cappacity = n; + ret->count = 0; + return (ret); +} + +t_vector *ft_vec_from_array(void **data, size_t count) +{ + t_vector *ret; + size_t i; + + if (count < FT_VECTOR_BASE_LEN) + ret = ft_vec_from_size(FT_VECTOR_BASE_LEN); + else + ret = ft_vec_from_size(count); + i = 0; + while (i < count) + { + ret->datas[i] = data[i]; + i++; + } + ret->count = i; + return (ret); +} + +t_vector *ft_vec_convert_alloccarray(void **data, size_t count) +{ + t_vector *ret; + + ret = ft_calloc(sizeof(t_vector), 1); + if (!ret) + return (ret); + ret->datas = data; + ret->count = count; + ret->cappacity = count; + return (ret); +} diff --git a/libft_personal/src/ft_vector/ft_vec_pop.c b/libft_personal/src/ft_vector/ft_vec_pop.c new file mode 100644 index 0000000..4e0c0e1 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_pop.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_pop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 23:00:25 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 13:56:08 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector_types.h" + +void *ft_vec_pop(t_vector *vec) +{ + void *data; + + if (vec->count == 0) + return (NULL); + data = vec->datas[vec->count - 1]; + vec->datas[--vec->count] = NULL; + return (data); +} diff --git a/libft_personal/src/ft_vector/ft_vec_remove.c b/libft_personal/src/ft_vector/ft_vec_remove.c new file mode 100644 index 0000000..2d46ab8 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_remove.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 19:03:06 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 12:41:42 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_vector.h" + +void ft_vec_remove(t_vector *vector, size_t n, void (*del)(void *)) +{ + if (n >= vector->count) + return ; + if (del) + del(vector->datas[n]); + vector->datas[n] = NULL; + ft_vec_shift(vector, n, 1); +} + +void ft_vec_remove_if(t_vector *vector, t_data_is func, t_data_apply del) +{ + size_t i; + + i = 0; + while (i < vector->count) + { + if (func(vector->datas[i])) + ft_vec_remove(vector, i, del); + else + i++; + } +} diff --git a/libft_personal/src/ft_vector/ft_vec_reserve.c b/libft_personal/src/ft_vector/ft_vec_reserve.c new file mode 100644 index 0000000..7a891dc --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_reserve.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:01:18 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:18:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_vector.h" +#include + +bool ft_vec_reserve(t_vector **vec, size_t size) +{ + t_vector ret; + size_t i; + + if ((*vec)->cappacity > size) + return (true); + ret.datas = ft_malloc(sizeof(void *) * size); + if (!ret.datas) + return (false); + i = 0; + while (i < (*vec)->count) + { + ret.datas[i] = (*vec)->datas[i]; + i++; + } + ret.count = (*vec)->count; + ret.cappacity = size; + free((*vec)->datas); + **vec = ret; + return (true); +} diff --git a/libft_personal/src/ft_vector/ft_vec_reverse.c b/libft_personal/src/ft_vector/ft_vec_reverse.c new file mode 100644 index 0000000..5358c20 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_reverse.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 19:29:24 by bgoulard #+# #+# */ +/* Updated: 2023/12/09 19:29:40 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +void ft_vec_reverse(t_vector *vector) +{ + size_t i; + size_t j; + void *tmp; + + i = 0; + j = vector->count - 1; + while (i < j) + { + tmp = vector->datas[i]; + vector->datas[i] = vector->datas[j]; + vector->datas[j] = tmp; + i++; + j--; + } +} diff --git a/libft_personal/src/ft_vector/ft_vec_shift.c b/libft_personal/src/ft_vector/ft_vec_shift.c new file mode 100644 index 0000000..8253b47 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_shift.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_shift.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 16:11:28 by bgoulard #+# #+# */ +/* Updated: 2023/12/13 09:24:48 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +void ft_vec_shift(t_vector *vec, size_t start, size_t shift) +{ + while (start + shift < vec->count) + { + vec->datas[start] = vec->datas[start + shift]; + start++; + } + vec->count -= shift; +} diff --git a/libft_personal/src/ft_vector/ft_vec_shrink.c b/libft_personal/src/ft_vector/ft_vec_shrink.c new file mode 100644 index 0000000..bc4d712 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_shrink.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_shrink.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/13 09:23:12 by bgoulard #+# #+# */ +/* Updated: 2023/12/30 12:58:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_vector.h" + +bool ft_vec_shrink(t_vector *vec) +{ + void **new_datas; + size_t i; + + if (vec->count == vec->cappacity) + return (true); + new_datas = ft_calloc(sizeof(void *), vec->count); + if (!new_datas) + return (false); + i = 0; + while (i < vec->count) + { + new_datas[i] = vec->datas[i]; + i++; + } + free(vec->datas); + vec->datas = new_datas; + vec->cappacity = vec->count; + return (true); +} diff --git a/libft_personal/src/ft_vector/ft_vec_sort.c b/libft_personal/src/ft_vector/ft_vec_sort.c new file mode 100644 index 0000000..314d7a6 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_sort.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/27 15:02:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:03:44 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_vector.h" + +static void ft_loc_swap(void **a, void **b) +{ + void *tmp; + + tmp = *a; + *a = *b; + *b = tmp; + return ; +} + +void ft_vec_sort(t_vector *vec, t_data_cmp cmp_f) +{ + size_t i; + size_t j; + + i = 0; + while (i < vec->count) + { + j = i + 1; + while (j < vec->count) + { + if (cmp_f(vec->datas[i], vec->datas[j]) > 0) + ft_loc_swap(&vec->datas[i], &vec->datas[j]); + j++; + } + i++; + } + return ; +} diff --git a/libft_personal/src/ft_vector/ft_vec_swap.c b/libft_personal/src/ft_vector/ft_vec_swap.c new file mode 100644 index 0000000..26d1f67 --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_swap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/09 16:53:11 by bgoulard #+# #+# */ +/* Updated: 2023/12/09 16:53:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" + +void ft_vec_swap(t_vector *vec, size_t a, size_t b) +{ + void *tmp; + + tmp = vec->datas[a]; + vec->datas[a] = vec->datas[b]; + vec->datas[b] = tmp; +} diff --git a/libft_personal/src/ft_vector/ft_vec_to_array.c b/libft_personal/src/ft_vector/ft_vec_to_array.c new file mode 100644 index 0000000..d39f9da --- /dev/null +++ b/libft_personal/src/ft_vector/ft_vec_to_array.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_to_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/09 23:39:27 by bgoulard #+# #+# */ +/* Updated: 2024/05/10 08:03:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector_types.h" +#include + +void **ft_vec_to_array(t_vector **vec) +{ + void **array; + + array = (*vec)->datas; + free(*vec); + return (array); +} diff --git a/libft_personal/tests/ft_args/args_tests.c b/libft_personal/tests/ft_args/args_tests.c new file mode 100644 index 0000000..f2db3f0 --- /dev/null +++ b/libft_personal/tests/ft_args/args_tests.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* args_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/18 17:02:31 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 18:33:18 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/args_tests.h" + +int parse_args_test(void) +{ + return (0); +} + +int tests_args(void) +{ + int collect; + const t_test test[] = { + {"getset_version", getset_version_test}, {"getset_progname", \ + getset_program_name_test}, {"getset_opt_list", getset_opt_list_test}, + {"parse_args", parse_args_test}, {"setup_prog", tests_setup_prog}, + {"getset_custom_parser", getset_custom_checker_test}, {NULL, NULL}}; + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_args/tests_custom_checker.c b/libft_personal/tests/ft_args/tests_custom_checker.c new file mode 100644 index 0000000..9a29e01 --- /dev/null +++ b/libft_personal/tests/ft_args/tests_custom_checker.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_custom_checker.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 18:15:10 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 14:28:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" + +static bool loc_checker(const void *arg) +{ + const char *str = (const char *)arg; + + if (str[0] == 'a') + return (true); + return (false); +} + +int getset_custom_checker_test(void) +{ + ft_arg_set_custom_checker(loc_checker); + if (ft_arg_get_custom_checker() != loc_checker) + return (-1); + return (0); +} diff --git a/libft_personal/tests/ft_args/tests_optlist.c b/libft_personal/tests/ft_args/tests_optlist.c new file mode 100644 index 0000000..02e3b1c --- /dev/null +++ b/libft_personal/tests/ft_args/tests_optlist.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optlist.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 15:52:31 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 15:52:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_args_types.h" +#include + +static void empty(void *ar, char *arg) +{ + (void)ar; + (void)arg; +} + +int getset_opt_list_test(void) +{ + const t_opt *op2 = NULL; + const t_opt op1[] = { + {"--test", 't', &empty, 0}, + }; + + ft_set_opt_list(op1); + if (ft_get_opt_list() != op1) + return (1); + ft_set_opt_list(op2); + if (ft_get_opt_list() != op1) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_args/tests_progname.c b/libft_personal/tests/ft_args/tests_progname.c new file mode 100644 index 0000000..25b1052 --- /dev/null +++ b/libft_personal/tests/ft_args/tests_progname.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_progname.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:37:47 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 22:38:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_string.h" + +int getset_program_name_test(void) +{ + char *s1; + char *s2; + + s1 = "toto"; + s2 = 0; + ft_set_progname(s1); + if (ft_progname() != s1) + return (ft_putstr_fd(ft_progname(), 2), 1); + ft_set_progname(s2); + if (ft_progname() == s2) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_args/tests_setup_prog.c b/libft_personal/tests/ft_args/tests_setup_prog.c new file mode 100644 index 0000000..1a18709 --- /dev/null +++ b/libft_personal/tests/ft_args/tests_setup_prog.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_setup_prog.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 15:55:09 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 16:04:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_string.h" + +int tests_setup_prog(void) +{ + const char *argv[] = {"dummy_prog", "--test", NULL}; + + ft_setup_prog(argv); + if (ft_progname() != argv[0]) + return (1); + if (ft_strcmp(ft_progversion(), VERSION) != 0) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_args/tests_version.c b/libft_personal/tests/ft_args/tests_version.c new file mode 100644 index 0000000..e0d3675 --- /dev/null +++ b/libft_personal/tests/ft_args/tests_version.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_version.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:37:13 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 22:38:27 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_args.h" +#include "ft_string.h" +#include + +int getset_version_test(void) +{ + char *s1; + char *s2; + + s1 = "toto"; + s2 = 0; + ft_set_version(s1); + if (ft_progversion() != s1) + return (ft_putstr_fd(ft_progversion(), STDERR_FILENO), 1); + ft_set_version(s2); + if (ft_progversion() == s2) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/dl_list_tests.c b/libft_personal/tests/ft_list/dl_tests/dl_list_tests.c new file mode 100644 index 0000000..2233d7c --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/dl_list_tests.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dl_list_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 20:37:03 by iron #+# #+# */ +/* Updated: 2024/05/30 12:10:48 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/dl_tests.h" + +static const t_test *init_tests(void) +{ + static const t_test var[] = { + {"add_front", t_dl_add_front}, {"add_back", \ + t_dl_add_back}, {"apply", t_dl_apply}, {\ + "apply_range", t_dl_apply_range}, {\ + "apply_range_node", t_dl_apply_range_node}, {\ + "clear", t_dl_clear}, {"clear_range", \ + t_dl_clear_range}, {"create", t_dl_create}, {\ + "copy_node", t_dl_copy_node}, {"copy_list", \ + t_dl_copy_list}, {"delete_self", \ + t_dl_delete_self}, {"delete_range", \ + t_dl_delete_range}, {"delete", t_dl_delete}, \ + {"find", t_dl_find}, {"get_datas", \ + t_dl_get_datas}, {"get_nodes", t_dl_get_nodes}, \ + {"at", t_dl_at}, {"begin", \ + t_dl_begin}, {"end", t_dl_end}, {\ + "map", t_dl_map}, {"new", t_dl_new}, \ + {"pop", t_dl_pop}, {"pop_back", \ + t_dl_pop_back}, {"push", t_dl_push}, \ + {"push_back", t_dl_push_back}, {"rev", \ + t_dl_rev}, {"size", t_dl_size}, {\ + "size_of_data", t_dl_size_of_data}, {\ + "subrange", t_dl_subrange}, {NULL, NULL}}; + + return (var); +} + +int tests_doubly_linked_list_all(void) +{ + int collect; + const t_test *test = init_tests(); + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_list/dl_tests/dl_tests_utils.c b/libft_personal/tests/ft_list/dl_tests/dl_tests_utils.c new file mode 100644 index 0000000..303d040 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/dl_tests_utils.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dl_test_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 11:08:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 14:27:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +void create_2elem_dlist(t_dlist **list, void **data1, void **data2) +{ + *data1 = malloc(sizeof(int)); + *data2 = malloc(sizeof(int)); + *(int *)*data1 = 42; + *(int *)*data2 = 21; + *list = NULL; + ft_dl_push(list, *data1); + ft_dl_push_back(list, *data2); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_add.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_add.c new file mode 100644 index 0000000..d630f0f --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_add.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:48:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:54:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +int t_dl_add_front(void) +{ + t_dlist *list; + int *data; + int *data2; + + data = malloc(sizeof(int)); + data2 = malloc(sizeof(int)); + *data = 42; + *data2 = 21; + list = ft_dl_create(data2); + ft_dl_add_front(&list, NULL); + if (!list || list->next) + return (1); + else + { + ft_dl_add_front(&list, ft_dl_create(data)); + if (!list || list->data != data || !list->next) + return (1); + else if (list->next->data != data2 || list->next->next) + return (1); + else if (list->next->prev != list) + return (1); + } + ft_dl_clear(&list, free); + ft_dl_add_front(NULL, NULL); + return (0); +} + +int t_dl_add_back(void) +{ + t_dlist *list; + int *data; + int *data2; + + list = NULL; + data = malloc(sizeof(int)); + data2 = malloc(sizeof(int)); + *data = 42; + *data2 = 21; + ft_dl_add_back(&list, ft_dl_create(data)); + ft_dl_add_back(&list, NULL); + if (!list || list->next) + return (1); + else + { + ft_dl_add_back(&list, ft_dl_create(data2)); + if (!list || list->data != data || !list->next || \ + list->next->data != data2) + return (1); + else if (list->next->next || list->next->prev != list) + return (1); + } + ft_dl_clear(&list, free); + ft_dl_add_front(NULL, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_apply.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_apply.c new file mode 100644 index 0000000..8776929 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_apply.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:55:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:48:52 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +static void dnode_add42(t_dlist *node) +{ + *(int *)node->data += 42; +} + +int t_dl_apply(void) +{ + t_dlist *list; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + ft_dl_apply(list, add42); + if (ft_dl_size(list) != 2) + return (1); + else if (*(int *)list->data != 84) + return (2); + else if (*(int *)list->next->data != 63) + return (3); + ft_dl_clear(&list, free); + return (0); +} + +int t_dl_apply_range(void) +{ + t_dlist *list; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + ft_dl_apply_range(list, list->next, add42); + if (ft_dl_size(list) != 2) + return (1); + else if (*(int *)list->data != 84 || \ + *(int *)list->next->data != 21) + return (1); + return (ft_dl_clear(&list, free), 0); +} + +int t_dl_apply_range_node(void) +{ + t_dlist *list; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + ft_dl_apply_range_node(list, list->next, dnode_add42); + if (ft_dl_size(list) != 2) + return (1); + else if (*(int *)list->data != 84 || \ + *(int *)list->next->data != 21) + return (1); + return (ft_dl_clear(&list, free), 0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_clear.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_clear.c new file mode 100644 index 0000000..fe755e7 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_clear.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:56:57 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:57:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_clear(void) +{ + t_dlist *list; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_dl_create(data); + ft_dl_clear(&list, NULL); + list = ft_dl_create(data); + ft_dl_clear(&list, free); + return (0); +} + +// +//ft_dl_add_back(&list, ft_dl_create(data3)) +// data -> data2 -> data3 +//ft_dl_clear_range(list->next, list->next->next, NULL) +// data -> NULL -> data3 +//ft_dl_add_back(&list, ft_dl_create(data2)) +// data -> NULL -> data3 -> data2 +//ft_dl_clear_range(list->next->next, NULL, free) +// data -> NULL -> NULL -> NULL +//ft_dl_delete_range(list, NULL, NULL) +// delete nodes +// + +int t_dl_clear_range(void) +{ + t_dlist *list; + int *data; + int *data2; + int *data3; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + data3 = malloc(sizeof(int)); + *data3 = 63; + ft_dl_add_back(&list, ft_dl_create(data3)); + ft_dl_clear_range(list->next, list->next->next, NULL); + if ((ft_dl_size(list) != 3) || (list->data != data || \ + list->next->data || list->next->next->data != data3)) + return (1); + ft_dl_add_back(&list, ft_dl_create(data2)); + ft_dl_clear_range(list->next->next, NULL, free); + if (ft_dl_size(list) != 4 || list->data != data \ + || list->next->data || list->next->next->data) + return (2); + ft_dl_clear_range(list, NULL, free); + ft_dl_clear_range(NULL, NULL, free); + ft_dl_delete_range(list, NULL, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_copy.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_copy.c new file mode 100644 index 0000000..b055bd0 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_copy.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:58:55 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:59:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_copy_node(void) +{ + int *data; + t_dlist *list; + t_dlist *copy; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_dl_create(data); + copy = ft_dl_copy_node(list); + if (!copy) + return (1); + else if (copy->data != list->data) + return (1); + else if (copy->next != list->next) + return (1); + else if (copy->prev != list->prev) + return (1); + ft_dl_clear(&list, free); + ft_dl_clear(©, NULL); + return (0); +} + +int t_dl_copy_list(void) +{ + t_dlist *list; + t_dlist *copy; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + copy = ft_dl_copy_list(list); + if (!copy) + return (1); + else if (copy->data != list->data) + return (1); + else if (!copy->next) + return (1); + else if (copy->next->data != list->next->data) + return (1); + else if (copy->next->next) + return (1); + else if (copy->next->prev != copy) + return (1); + ft_dl_clear(&list, free); + ft_dl_clear(©, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_create.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_create.c new file mode 100644 index 0000000..e7cfe18 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_create.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:58:15 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:00:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_list.h" +#include "ft_list_types.h" +#include + +int t_dl_create(void) +{ + t_dlist *list; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_dl_create(data); + if (!list) + return (1); + else if (list->data != data) + return (1); + else if (list->next) + return (1); + else if (list->prev) + return (1); + ft_dl_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_delete.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_delete.c new file mode 100644 index 0000000..8e8d872 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_delete.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_delete.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:01:32 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:20:30 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_delete_self(void) +{ + int *data; + int *data2; + int *data3; + t_dlist *list; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + data3 = malloc(sizeof(int)); + *data3 = 63; + ft_dl_add_back(&list, ft_dl_create(data3)); + ft_dl_delete_self(list->next, NULL); + if (!list || list->data != data || !list->next || \ + list->next->data != data3 || list->next->next) + return (1); + ft_dl_clear(&list, free); + data3 = malloc(sizeof(int)); + *data3 = 63; + *data2 = 21; + list = ft_dl_create(data3); + ft_dl_add_back(&list, ft_dl_create(data2)); + ft_dl_delete_self(list->next, free); + if (list->next || list->data != data3) + return (1); + return (ft_dl_clear(&list, free), \ + ft_dl_delete_self(NULL, NULL), 0); +} +/* + ft_dl_add_back(&list, ft_dl_create(data3)); + // NULL <-(42)<=>(21)<=>(63)-> NULL + list2 = list->next; + // 42 <-(21)<=>(63)-> NULL + nb_deleted = ft_dl_delete_range(list, list->next, NULL); + // NULL <-(21)<=>(63)-> NULL + ft_dl_add_front(&list2, ft_dl_create(data)); + // NULL <-(42)<=>(21)<=>(63)-> NULL + list = list2; + nb_deleted = ft_dl_delete_range(list->next, list->next->next, free); + // NULL <-(42)<=>(63)-> NULL +*/ + +int t_dl_delete_range(void) +{ + t_dlist *list; + t_dlist *list2; + int *data_array[3]; + int nb_deleted; + + create_2elem_dlist(&list, (void **)&data_array[0], (void **)&data_array[1]); + data_array[2] = malloc(sizeof(int)); + *data_array[2] = 63; + ft_dl_add_back(&list, ft_dl_create(data_array[2])); + list2 = list->next; + nb_deleted = ft_dl_delete_range(list, list->next, NULL); + if (nb_deleted != 1 || ft_dl_size(list2) != 2 || *(int *)list2->data \ + != *data_array[1] || *(int *)list2->next->data != *data_array[2]) + return (1); + ft_dl_add_front(&list2, ft_dl_create(data_array[0])); + list = list2; + nb_deleted = ft_dl_delete_range(list->next, list->next->next, free); + if (nb_deleted != 1 || ft_dl_size(list) != 2 || *(int *)list2->data \ + != *data_array[0] || *(int *)list2->next->data != *data_array[2]) + return (1); + return (ft_dl_clear(&list2, free), 0); +} + +int t_dl_delete(void) +{ + t_dlist *list; + int *data; + int *data2; + int *data3; + int nb_deleted; + + data3 = malloc(sizeof(int)); + *data3 = 63; + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + ft_dl_add_back(&list, ft_dl_create(data3)); + nb_deleted = ft_dl_delete(&list, NULL); + if (nb_deleted != 3) + return (1); + else if (list) + return (1); + ft_dl_add_front(&list, ft_dl_create(data)); + ft_dl_add_back(&list, ft_dl_create(data2)); + ft_dl_add_back(&list, ft_dl_create(data3)); + nb_deleted = ft_dl_delete(&list, free); + if (nb_deleted != 3) + return (1); + else if (list) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_find.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_find.c new file mode 100644 index 0000000..2210e46 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_find.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:01:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:49:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +/* +ft_dl_add_back(&list, ft_dl_create(data2)); +// data-> data2 +*/ + +int t_dl_find(void) +{ + t_dlist *list; + t_dlist *found_ret[3]; + int *data; + int *data2; + int *data3; + + data3 = malloc(sizeof(int)); + *data3 = 21; + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + found_ret[0] = ft_dl_find(list, data2, NULL); + found_ret[1] = ft_dl_find(list, data3, NULL); + found_ret[2] = ft_dl_find(list, data3, cmp_int); + if (ft_dl_size(found_ret[0]) != 1 || found_ret[0]->data != data2) + return (1); + else if (found_ret[1]) + return (1); + else if (ft_dl_size(found_ret[2]) != 1 || found_ret[2]->data != data2) + return (1); + return (ft_dl_clear(&list, free), free(data3), 0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_get.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_get.c new file mode 100644 index 0000000..eed83af --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_get.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:04:21 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:04:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_get_datas(void) +{ + t_dlist *list; + int *data; + int *data2; + void **datas; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + datas = ft_dl_get_datas(list); + if (!datas) + return (1); + else if (datas[0] != data) + return (1); + else if (datas[1] != data2) + return (1); + if (ft_dl_get_datas(NULL)) + return (1); + ft_dl_clear(&list, free); + free(datas); + return (0); +} + +int t_dl_get_nodes(void) +{ + t_dlist *list; + int *data; + int *data2; + t_dlist **nodes; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + nodes = ft_dl_get_nodes(list); + if (!nodes) + return (1); + else if (nodes[0] != list) + return (1); + else if (nodes[1] != list->next) + return (1); + if (ft_dl_get_nodes(NULL)) + return (1); + ft_dl_clear(&list, free); + free(nodes); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_iterators.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_iterators.c new file mode 100644 index 0000000..5f99449 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_iterators.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_iterators.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:04:13 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:04:17 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_at(void) +{ + t_dlist *list; + t_dlist *at; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + at = ft_dl_at(list, 1); + if (!at) + return (1); + else if (at->data != data2) + return (1); + else if (at->next) + return (1); + else if (at->prev != list) + return (1); + ft_dl_clear(&list, free); + return (0); +} + +int t_dl_begin(void) +{ + t_dlist *list; + t_dlist *begin; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + begin = ft_dl_begin(list->next); + if (!begin) + return (1); + else if (begin != list) + return (1); + else if (begin->data != data) + return (1); + else if (begin->next != list->next) + return (1); + else if (begin->prev) + return (1); + ft_dl_clear(&list, free); + return (0); +} + +int t_dl_end(void) +{ + t_dlist *list; + t_dlist *end; + int *data; + int *data2; + + create_2elem_dlist(&list, (void **)&data, (void **)&data2); + end = ft_dl_end(list); + if (!end) + return (1); + else if (end->data != data2) + return (1); + else if (end->next) + return (1); + else if (end->prev != list) + return (1); + ft_dl_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_map.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_map.c new file mode 100644 index 0000000..e5c91c0 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_map.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:06:32 by bgoulard #+# #+# */ +/* Updated: 2024/07/19 18:24:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include +#include + +int t_dl_map(void) +{ + t_dlist *list; + t_dlist *map; + t_dlist *map2; + int *data1; + int *data2; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + map2 = ft_dl_map(list, NULL, free); + map = ft_dl_map(list, add42_ret, free); + if (!map) + return (1); + else if (*(int *)map->data != 84) + return (1); + else if (!map->next) + return (1); + else if (*(int *)map->next->data != 63) + return (1); + else if (map->next->next) + return (1); + else if (map2) + return (1); + ft_dl_clear(&list, free); + ft_dl_clear(&map, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_new.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_new.c new file mode 100644 index 0000000..5f46385 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_new.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:05:52 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:06:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +int t_dl_new(void) +{ + t_dlist *list; + + list = ft_dl_new(); + if (!list) + return (1); + ft_dl_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_push.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_push.c new file mode 100644 index 0000000..2d7063f --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_push.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:07:52 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:07:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_pop(void) +{ + t_dlist *list; + int *data1; + int *data2; + int *ret; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + ret = ft_dl_pop(&list); + if (!list) + return (1); + else if (ret != data1) + return (2); + ret = ft_dl_pop(&list); + if (ret != data2) + return (3); + if (list) + return (4); + ret = ft_dl_pop(&list); + if (ret) + return (5); + ret = ft_dl_pop(NULL); + if (ret) + return (6); + return (free(data1), free(data2), 0); +} + +int t_dl_pop_back(void) +{ + t_dlist *list; + int *data1; + int *data2; + int *data3; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + data3 = ft_dl_pop_back(&list); + if (!list) + return (1); + else if (data3 != data2) + return (1); + data3 = ft_dl_pop_back(&list); + if (list) + return (1); + else if (data3 != data1) + return (1); + data3 = ft_dl_pop_back(NULL); + if (data3) + return (1); + data3 = ft_dl_pop_back(&list); + if (data3) + return (1); + free(data1); + free(data2); + return (0); +} + +int t_dl_push(void) +{ + t_dlist *list; + int *data1; + + data1 = malloc(sizeof(int)); + *data1 = 42; + list = NULL; + ft_dl_push(&list, data1); + if (!list) + return (1); + else if (list->data != data1) + return (1); + ft_dl_clear(&list, free); + return (0); +} + +int t_dl_push_back(void) +{ + t_dlist *list; + int *data1; + int *data2; + int *data3; + + data1 = malloc(sizeof(int)); + *data1 = 42; + list = NULL; + data2 = malloc(sizeof(int)); + *data2 = 21; + data3 = malloc(sizeof(int)); + *data3 = 63; + ft_dl_push_back(&list, data1); + ft_dl_push_back(&list, data2); + ft_dl_push_back(&list, data3); + if (ft_dl_size(list) != 3) + return (1); + else if (list->data != data1 || list->next->data != data2 || \ + list->next->next->data != data3) + return (1); + ft_dl_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_rev.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_rev.c new file mode 100644 index 0000000..654a8ad --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_rev.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_rev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:08:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:08:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_rev(void) +{ + t_dlist *list; + int *data1; + int *data2; + int *original_data1; + int *original_data2; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + original_data1 = data1; + original_data2 = data2; + ft_dl_rev(&list); + if (ft_dl_size(list) != 2) + return (1); + else if (list->data != original_data2 || list->next->data != original_data1) + return (1); + ft_dl_clear(&list, free); + if (ft_dl_rev(NULL)) + return (1); + list = NULL; + if (ft_dl_rev(&list)) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_sizers.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_sizers.c new file mode 100644 index 0000000..dcfe396 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_sizers.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_sizers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:09:18 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:48:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +int t_dl_size(void) +{ + t_dlist *list; + int *data1; + int *data2; + size_t size_ret[3]; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + size_ret[0] = ft_dl_size(NULL); + size_ret[1] = ft_dl_size(list->next); + size_ret[2] = ft_dl_size(list); + if (size_ret[0] != 0) + return (1); + else if (size_ret[1] != 1) + return (1); + else if (size_ret[2] != 2) + return (1); + ft_dl_clear(&list, free); + return (0); +} + +int t_dl_size_of_data(void) +{ + t_dlist *list; + int *data1; + int *data2; + size_t size_ret[3]; + + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + size_ret[0] = ft_dl_size_of_data(NULL, is42); + size_ret[1] = ft_dl_size_of_data(list->next, is42); + size_ret[2] = ft_dl_size_of_data(list, is42); + if (size_ret[0] != 0) + return (1); + else if (size_ret[1] != 0) + return (2); + else if (size_ret[2] != 1) + return (3); + ft_dl_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/dl_tests/tests_dlist_subrange.c b/libft_personal/tests/ft_list/dl_tests/tests_dlist_subrange.c new file mode 100644 index 0000000..b60ffe0 --- /dev/null +++ b/libft_personal/tests/ft_list/dl_tests/tests_dlist_subrange.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_dlist_subrange.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 17:10:08 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 17:10:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_dl_subrange(void) +{ + t_dlist *list; + t_dlist *sub; + int *data1; + int *data2; + int *data3; + + data3 = malloc(sizeof(int)); + *data3 = 63; + create_2elem_dlist(&list, (void **)&data1, (void **)&data2); + ft_dl_push_back(&list, data3); + sub = ft_dl_subrange(list, list->next); + if (ft_dl_size(sub) != 1 || sub->data != data1) + return (1); + ft_dl_clear(&sub, NULL); + sub = ft_dl_subrange(list, list->next->next); + if (ft_dl_size(sub) != 2 || sub->data != data1 || \ + sub->next->data != data2 || ft_dl_clear(&sub, NULL), 0) + return (2); + if (ft_dl_subrange(NULL, NULL)) + return (3); + sub = ft_dl_subrange(list, list); + if (ft_dl_size(sub) != 1 || sub->data != data1) + return (4); + return (ft_dl_clear(&sub, NULL), ft_dl_clear(&list, free), 0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/ll_list_tests.c b/libft_personal/tests/ft_list/ll_tests/ll_list_tests.c new file mode 100644 index 0000000..6a61d96 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/ll_list_tests.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ll_list_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 13:17:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 12:11:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/ll_tests.h" + +int tests_linked_list_all(void) +{ + int collect; + const t_test test[] = { + {"add_front", t_ll_add_front}, {"add_back", + t_ll_add_back}, {"apply", t_ll_apply}, { + "apply_range", t_ll_apply_range}, { + "apply_range_node", t_ll_apply_range_node}, { + "clear", t_ll_clear}, {"create", + t_ll_create}, {"copy_node", t_ll_copy_node}, + {"copy_list", t_ll_copy_list}, {"delone", + t_ll_delone}, {"delete_range", t_ll_delete_range}, + {"find", t_ll_find}, {"get_datas", + t_ll_get_datas}, {"get_nodes", t_ll_get_nodes}, { + "end", t_ll_end}, {"at", t_ll_at}, { + "map", t_ll_map}, {"new", t_ll_new}, { + "push", t_ll_push}, {"push_back", + t_ll_push_back}, {"pop", t_ll_pop}, { + "pop_back", t_ll_pop_back}, {"rev", t_ll_rev}, + {"size", t_ll_size}, {"size_match", + t_ll_size_match}, {"subrange", t_ll_subrange}, + {NULL, NULL} + }; + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_list/ll_tests/ll_tests_utils.c b/libft_personal/tests/ft_list/ll_tests/ll_tests_utils.c new file mode 100644 index 0000000..1c16a95 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/ll_tests_utils.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ll_test_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 11:08:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 14:27:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +void create_2elem_list(t_list **list, void **data1, void **data2) +{ + *data1 = malloc(sizeof(int)); + *data2 = malloc(sizeof(int)); + *(int *)*data1 = 42; + *(int *)*data2 = 21; + *list = NULL; + ft_ll_push(list, *data1); + ft_ll_push_back(list, *data2); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_add.c b/libft_personal/tests/ft_list/ll_tests/tests_list_add.c new file mode 100644 index 0000000..a138041 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_add.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:15:52 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:46:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +/* + list = NULL; + ft_ll_add_front(&list, ft_ll_create(data2)); // (21)-> NULL + ft_ll_add_front(&list, ft_ll_create(data)); // (42)-> (21)-> NULL + --- + ft_ll_add_front(NULL, list); // null resiliency + ft_ll_clear(&list, free); +*/ + +int t_ll_add_front(void) +{ + t_list *list; + int *data; + int *data2; + + data = malloc(sizeof(int)); + *data = 42; + data2 = malloc(sizeof(int)); + *data2 = 21; + list = NULL; + ft_ll_add_front(&list, ft_ll_create(data2)); + ft_ll_add_front(&list, ft_ll_create(data)); + if (!list || list->data != data || !list->next + || list->next->data != data2 || list->next->next) + return (1); + ft_ll_add_front(NULL, list); + ft_ll_clear(&list, free); + return (0); +} + +int t_ll_add_back(void) +{ + t_list *list; + int *data; + int *data2; + + data = malloc(sizeof(int)); + *data = 42; + data2 = malloc(sizeof(int)); + *data2 = 21; + list = ft_ll_create(data); + ft_ll_add_back(&list, ft_ll_create(data2)); + if (!list || list->data != data || !list->next) + return (1); + else if (list->next->data != data2 || list->next->next) + return (1); + ft_ll_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_apply.c b/libft_personal/tests/ft_list/ll_tests/tests_list_apply.c new file mode 100644 index 0000000..523d295 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_apply.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:18:39 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:47:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/ll_tests.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +static void lnode_add42(t_list *node) +{ + *(int *)node->data += 42; +} + +int t_ll_apply(void) +{ + const int data3 = 63; + const int data4 = 84; + t_list *list; + int *data; + int *data2; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + ft_ll_apply(list, add42); + if (!list || !list->next || list->next->next + || *(int *)list->data != data4 || *(int *)list->next->data != data3) + return (1); + ft_ll_apply(list, NULL); + if (!list || !list->next || list->next->next + || *(int *)list->data != data4 || *(int *)list->next->data != data3) + return (1); + ft_ll_clear(&list, free); + return (0); +} + +int t_ll_apply_range(void) +{ + t_list *list; + int *data; + int *data2; + const int data3 = 84; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + ft_ll_apply_range(list, list->next, add42); + if (*(int *)list->data != data3) + return (1); + else if (ft_ll_size(list) != 2) + return (1); + else if (*(int *)list->next->data != 21) + return (1); + ft_ll_apply_range(list, list->next, NULL); + if (ft_ll_size(list) != 2) + return (1); + else if (*(int *)list->data != data3) + return (1); + else if (*(int *)list->next->data != 21) + return (1); + ft_ll_apply_range(NULL, NULL, add42); + ft_ll_clear(&list, free); + return (0); +} + +int t_ll_apply_range_node(void) +{ + const int data3 = 84; + t_list *list; + int *data; + int *data2; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + ft_ll_apply_range_node(list, list->next, lnode_add42); + if (*(int *)list->data != data3 || !list->next + || *(int *)list->next->data != 21 || list->next->next) + return (1); + ft_ll_apply_range_node(list, list->next, NULL); + if (*(int *)list->data != data3 || !list->next + || *(int *)list->next->data != 21 || list->next->next) + return (1); + ft_ll_apply_range_node(NULL, NULL, lnode_add42); + ft_ll_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_clear.c b/libft_personal/tests/ft_list/ll_tests/tests_list_clear.c new file mode 100644 index 0000000..53613e0 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_clear.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:21:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:22:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "tests/ll_tests.h" +#include + +int t_ll_clear(void) +{ + t_list *list; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_ll_create(data); + ft_ll_clear(&list, NULL); + list = ft_ll_create(data); + ft_ll_clear(&list, free); + ft_ll_clear(NULL, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_copy.c b/libft_personal/tests/ft_list/ll_tests/tests_list_copy.c new file mode 100644 index 0000000..c9a1396 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_copy.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listcopy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:24:02 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:25:18 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "tests/lists_test_utils.h" +#include "tests/ll_tests.h" +#include + +int t_ll_copy_node(void) +{ + t_list *list; + t_list *copy; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_ll_create(data); + copy = ft_ll_copy_node(list); + if (!copy) + return (1); + else if (copy->data != list->data) + return (1); + else if (copy->next != list->next) + return (1); + free(data); + free(list); + free(copy); + return (0); +} + +int t_ll_copy_list(void) +{ + t_list *list; + t_list *copy; + int *data; + int *data2; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + copy = ft_ll_copy_list(list); + if (!copy) + return (1); + else if (copy->data != list->data) + return (1); + else if (!copy->next) + return (1); + else if (copy->next->data != list->next->data) + return (1); + else if (copy->next->next) + return (1); + ft_ll_clear(&list, free); + ft_ll_clear(©, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_create.c b/libft_personal/tests/ft_list/ll_tests/tests_list_create.c new file mode 100644 index 0000000..97c1fb7 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_create.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listcreate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:23:08 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:23:34 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "tests/ll_tests.h" +#include + +int t_ll_create(void) +{ + t_list *list; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_ll_create(data); + if (!list) + return (1); + else if (list->data != data) + return (1); + else if (list->next) + return (1); + free(data); + free(list); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_deletors.c b/libft_personal/tests/ft_list/ll_tests/tests_list_deletors.c new file mode 100644 index 0000000..9f34ee2 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_deletors.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listdeletors.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:25:58 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:26:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// ft_ll_delone(NULL, NULL); // null resiliency + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_ll_delone(void) +{ + t_list *list; + int *data; + + data = malloc(sizeof(int)); + *data = 42; + list = ft_ll_create(data); + ft_ll_delone(list, NULL); + list = ft_ll_create(data); + ft_ll_delone(list, free); + ft_ll_delone(NULL, NULL); + return (0); +} + +/* + ft_ll_add_back(&list, ft_ll_create(data2)); // (42)-> (21)-> NULL + list2 = list->next; + nb_deleted = ft_ll_delete_range(list, list->next, NULL); + // (42)-> (21)-> NULL + nb_deleted2 = ft_ll_delete_range(list, list->next, free); + // (21)-> NULL + // we use list2 to check if the list is still valid + // list was destroyed by ft_ll_delete_range + --- + ft_ll_clear(&list2, free); // NULL + --- + nb_deleted = ft_ll_delete_range(NULL, NULL, free); // null resiliency + --- + list = ft_ll_create(data); // (42)-> NULL + nb_deleted = ft_ll_delete_range(list, NULL, free); // NULL + --- + +*/ + +int t_ll_delete_range(void) +{ + t_list *list; + t_list *list2; + int *data; + int *data2; + int nb_del[2]; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + list2 = list->next; + nb_del[0] = ft_ll_delete_range(list, list->next, NULL); + nb_del[1] = ft_ll_delete_range(list, list->next, free); + if ((nb_del[0] != 0 || nb_del[1] != 1) || \ + (list2->data != data2 || list2->next)) + return (__LINE__); + ft_ll_clear(&list2, free); + nb_del[0] = ft_ll_delete_range(NULL, NULL, free); + if (nb_del[0] != 0) + return (__LINE__); + data = malloc(sizeof(int)); + *data = 42; + list = ft_ll_create(data); + nb_del[0] = ft_ll_delete_range(list, NULL, free); + if (nb_del[0] != 1) + return (__LINE__); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_find.c b/libft_personal/tests/ft_list/ll_tests/tests_list_find.c new file mode 100644 index 0000000..2c66690 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_find.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:27:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:47:52 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +int t_ll_find(void) +{ + t_list *list; + int *data; + int *data2; + int *data3; + void *found; + + data3 = malloc(sizeof(int)); + create_2elem_list(&list, (void **)&data, (void **)&data2); + found = ft_ll_find(list, data2, NULL); + if (!found || *(int *)found != *data2) + return (1); + *data3 = 63; + found = ft_ll_find(list, data3, cmp_int); + if (found) + return (2); + found = ft_ll_find(NULL, data2, NULL); + if (found) + return (3); + *data3 = 42; + found = ft_ll_find(list, data3, cmp_int); + if (!found || *(int *)found != *data) + return (4); + ft_ll_clear(&list, free); + free(data3); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_get.c b/libft_personal/tests/ft_list/ll_tests/tests_list_get.c new file mode 100644 index 0000000..b62a2f5 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_get.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:32:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:33:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_ll_get_datas(void) +{ + t_list *list; + int *data; + int *data2; + void **datas; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + datas = ft_ll_get_datas(list); + if (!datas || datas[2] || datas[0] != data || \ + datas[1] != data2) + return (1); + if (ft_ll_get_datas(NULL)) + return (1); + ft_ll_clear(&list, free); + free(datas); + return (0); +} + +int t_ll_get_nodes(void) +{ + t_list *list; + int *data; + int *data2; + t_list **nodes; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + nodes = ft_ll_get_nodes(list); + if (!nodes || nodes[2] || nodes[0] != list || \ + nodes[1] != list->next) + return (1); + if (ft_ll_get_nodes(NULL)) + return (1); + ft_ll_clear(&list, free); + free(nodes); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_iterators.c b/libft_personal/tests/ft_list/ll_tests/tests_list_iterators.c new file mode 100644 index 0000000..a8c228e --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_iterators.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_listend.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:33:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:34:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_ll_end(void) +{ + t_list *list; + int *data; + int *data2; + t_list *last; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + last = ft_ll_end(list); + if (!last) + return (1); + else if (last->data != data2) + return (1); + else if (last->next) + return (1); + last = ft_ll_end(NULL); + if (last) + return (1); + ft_ll_clear(&list, free); + return (0); +} + +int t_ll_at(void) +{ + t_list *list; + int *data; + int *data2; + t_list *at; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + at = ft_ll_at(list, 1); + if (!at || at->data != data2 || at->next) + return (1); + at = ft_ll_at(list, 456); + if (at) + return (1); + at = ft_ll_at(NULL, 0); + if (at) + return (1); + ft_ll_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_map.c b/libft_personal/tests/ft_list/ll_tests/tests_list_map.c new file mode 100644 index 0000000..14a1c6c --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_map.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:36:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:44:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +int t_ll_map(void) +{ + t_list *list; + int *data; + int *data2; + t_list *map; + t_list *map_err[3]; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + map = ft_ll_map(list, add42_ret, free); + map_err[0] = ft_ll_map(list, NULL, free); + map_err[1] = ft_ll_map(NULL, add42_ret, free); + map_err[2] = ft_ll_map(list, add42_ret, NULL); + if (!map || *(int *)map->data != 84 || !map->next) + return (1); + else if (*(int *)map->next->data != 63) + return (1); + else if (map->next->next) + return (1); + else if (map_err[0] || map_err[1] || map_err[2]) + return (1); + ft_ll_clear(&list, free); + ft_ll_clear(&map, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_new.c b/libft_personal/tests/ft_list/ll_tests/tests_list_new.c new file mode 100644 index 0000000..10c0a2d --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_new.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:37:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:39:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include + +int t_ll_new(void) +{ + t_list *list; + + list = ft_ll_new(); + if (!list) + return (1); + free(list); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_push.c b/libft_personal/tests/ft_list/ll_tests/tests_list_push.c new file mode 100644 index 0000000..4565c7f --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_push.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_push.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:37:47 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:39:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_ll_push(void) +{ + t_list *list; + t_list *list_rep1; + t_list *list_rep2; + const int data[][1] = { + {42}, + {21} + }; + + list = NULL; + list_rep1 = ft_ll_push(&list, &data[0]); + list_rep2 = ft_ll_push(&list, &data[1]); + if (!list || !list_rep1 || !list_rep2) + return (1); + else if (list != list_rep2) + return (1); + else if (list->next != list_rep1) + return (1); + else if (ft_ll_size(list) != 2) + return (1); + else if (list->data != &data[1] || list->next->data != &data[0]) + return (1); + ft_ll_clear(&list, NULL); + ft_ll_push(NULL, NULL); + return (0); +} + +int t_ll_push_back(void) +{ + t_list *list; + int *data; + int *data2; + + data = malloc(sizeof(int)); + *data = 42; + data2 = malloc(sizeof(int)); + *data2 = 21; + list = NULL; + ft_ll_push_back(&list, data); + ft_ll_push_back(&list, data2); + if (ft_ll_size(list) != 2) + return (1); + else if (list->data != data) + return (1); + else if (list->next->data != data2) + return (1); + ft_ll_clear(&list, free); + ft_ll_push_back(NULL, NULL); + return (0); +} + +int t_ll_pop(void) +{ + t_list *list; + int *data; + int *data2; + void *pop; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + pop = ft_ll_pop(&list); + if (!list || ft_ll_size(list) != 1) + return (1); + else if (list->data != data2) + return (1); + else if (pop != data) + return (1); + ft_ll_clear(&list, free); + free(pop); + if (ft_ll_pop(&list) || ft_ll_pop(NULL)) + return (1); + return (0); +} + +/* + create_2elem_list(&list, (void **)&data, (void **)&data2); // (42)-> (21)-> NULL + data3 = malloc(sizeof(int)); + *data3 = 63; + ft_ll_push(&list, data3); // (63)-> (42)-> (21)-> NULL + pop = ft_ll_pop_back(&list); // 21 : (63)-> (42)-> NULL + if (ft_ll_size(list) != 2) + return (1); + else if (list->data != data3) + return (1); + else if (list->next->data != data) + return (1); + else if (pop != data2) + return (1); + pop = ft_ll_pop_back(&list); // 42 : (63)-> NULL + +*/ +int t_ll_pop_back(void) +{ + t_list *list; + int *data; + int *data2; + int *data3; + void *pop; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + data3 = malloc(sizeof(int)); + *data3 = 63; + ft_ll_push(&list, data3); + pop = ft_ll_pop_back(&list); + if (ft_ll_size(list) != 2) + return (1); + else if (list->data != data3 || list->next->data != data || pop != data2) + return (1); + pop = ft_ll_pop_back(&list); + if (ft_ll_size(list) != 1 || list->data != data3 || pop != data) + return (1); + pop = ft_ll_pop_back(&list); + if (list || pop != data3 || ft_ll_size(list) != 0 || \ + ft_ll_pop_back(&list)) + return (1); + return (free(data), free(data2), free(data3), 0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_rev.c b/libft_personal/tests/ft_list/ll_tests/tests_list_rev.c new file mode 100644 index 0000000..7f30c5f --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_rev.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_rev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:40:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:41:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +int t_ll_rev(void) +{ + t_list *list; + t_list *rev; + int *data; + int *data2; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + rev = ft_ll_rev(&list); + if (ft_ll_size(list) != 2) + return (1); + else if (rev->data != data2) + return (1); + else if (rev->next->data != data) + return (1); + ft_ll_clear(&rev, free); + rev = NULL; + rev = ft_ll_rev(NULL); + if (rev || ft_ll_rev(&rev)) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_sizers.c b/libft_personal/tests/ft_list/ll_tests/tests_list_sizers.c new file mode 100644 index 0000000..2df3fdf --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_sizers.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_sizers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:43:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:46:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include "tests/tests_lambda_functions.h" +#include + +int t_ll_size(void) +{ + t_list *list; + int *data; + int *data2; + size_t size_ret[3]; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + size_ret[0] = ft_ll_size(NULL); + size_ret[1] = ft_ll_size(list->next); + size_ret[2] = ft_ll_size(list); + if (size_ret[0] != 0) + return (1); + if (size_ret[1] != 1) + return (1); + if (size_ret[2] != 2) + return (1); + ft_ll_clear(&list, free); + return (0); +} + +int t_ll_size_match(void) +{ + t_list *list; + int *data; + int *data2; + size_t size_ret[3]; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + size_ret[0] = ft_ll_size_match(NULL, is42); + size_ret[1] = ft_ll_size_match(list, is42); + size_ret[2] = ft_ll_size_match(list->next, is42); + if (size_ret[0] != 0) + return (1); + if (size_ret[1] != 1) + return (1); + if (size_ret[2] != 0) + return (1); + ft_ll_clear(&list, free); + return (0); +} diff --git a/libft_personal/tests/ft_list/ll_tests/tests_list_subrange.c b/libft_personal/tests/ft_list/ll_tests/tests_list_subrange.c new file mode 100644 index 0000000..25e5e46 --- /dev/null +++ b/libft_personal/tests/ft_list/ll_tests/tests_list_subrange.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_list_subrange.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 16:44:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 16:46:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_list.h" +#include "ft_list_types.h" +#include "tests/lists_test_utils.h" +#include + +/* + ft_ll_push(&list, data); // (42)-> NULL + ft_ll_push(&list, data2); // (21)-> (42)-> NULL + sub = ft_ll_subrange(list, list->next); // (21)-> NULL + sub = ft_ll_subrange(list, NULL); // (21)-> (42)-> NULL + sub = ft_ll_subrange(list, (const t_list *)data2); // (21)-> (42)-> NULL +*/ + +int t_ll_subrange(void) +{ + t_list *list; + int *data; + int *data2; + t_list *sub; + + create_2elem_list(&list, (void **)&data, (void **)&data2); + ft_ll_rev(&list); + sub = ft_ll_subrange(list, list->next); + if (ft_ll_size(sub) != 1 || sub->data != data2) + return (1); + ft_ll_clear(&sub, NULL); + sub = ft_ll_subrange(list, NULL); + if (ft_ll_size(sub) != 2 || sub->data != data2 || sub->next->data != data) + return (2); + ft_ll_clear(&sub, NULL); + sub = ft_ll_subrange(list, (const t_list *)data2); + if (ft_ll_size(sub) != 2 || sub->data != data2 || sub->next->data != data) + return (3); + ft_ll_clear(&sub, NULL); + if (ft_ll_subrange(NULL, NULL)) + return (4); + sub = ft_ll_subrange(list, list); + if (ft_ll_size(sub) != 1 || sub->data != data2) + return (5); + return (ft_ll_clear(&sub, NULL), ft_ll_clear(&list, free), 0); +} diff --git a/libft_personal/tests/ft_map/map_tests.c b/libft_personal/tests/ft_map/map_tests.c new file mode 100644 index 0000000..28bd79a --- /dev/null +++ b/libft_personal/tests/ft_map/map_tests.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 18:27:46 by bgoulard #+# #+# */ +/* Updated: 2024/06/07 14:59:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/map_tests.h" + +// see with --leak-check=full +int tests_map(void) +{ + int collect; + const t_test test[] = { + {"create", test_map_create}, + {"destroy", test_map_destroy}, // here + {"destroy_free", test_map_destroy_free}, + {"clear", test_map_clear}, // here + {"set", test_map_set}, + {"set_cmp", test_map_set_cmp}, + {"set_hash", test_map_set_hash}, + {"get", test_map_get}, + {"size", test_map_size}, + {"capacity", test_map_capacity}, + {"remove", test_map_remove}, // here + {"hash", test_map_hash}, + {NULL, NULL} + }; + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_map/tests_map_cappacity.c b/libft_personal/tests/ft_map/tests_map_cappacity.c new file mode 100644 index 0000000..6bd46dd --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_cappacity.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_cappacity.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:16:49 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:03:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" + +int test_map_capacity(void) +{ + t_map *map; + size_t ret; + + map = ft_map_create(10); + ret = ft_map_capacity(map); + if (ret != 10) + return (1); + ft_map_destroy(map); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_clear.c b/libft_personal/tests/ft_map/tests_map_clear.c new file mode 100644 index 0000000..525b47e --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_clear.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:11:13 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:41:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_string.h" + +// clear the map and check if the value is still present +// try to double clear the map + +int test_map_clear(void) +{ + t_map *map; + + map = ft_map_create(10); + ft_map_set(map, "key", "value", ft_strlen("key")); + ft_map_clear(map); + if (ft_map_get(map, "key", ft_strlen("key"))) + return (1); + ft_map_clear(map); + ft_map_destroy(map); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_create.c b/libft_personal/tests/ft_map/tests_map_create.c new file mode 100644 index 0000000..51bcc73 --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_create.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_create.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:07:38 by bgoulard #+# #+# */ +/* Updated: 2024/05/19 18:07:42 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" + +int test_map_create(void) +{ + t_map *map; + + map = ft_map_create(10); + if (!map) + return (1); + ft_map_destroy(map); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_destroy.c b/libft_personal/tests/ft_map/tests_map_destroy.c new file mode 100644 index 0000000..4449d4b --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_destroy.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:08:04 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:07:40 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_string.h" +#include "ft_map_types.h" +#include + +// check valgrind for no memory leak +int test_map_destroy(void) +{ + t_map *map; + + map = ft_map_create(10); + ft_map_destroy(map); + return (0); +} + +// accessing to key[0] and value[0] after the call to check +// that no free occured on the data at first call +int test_map_destroy_free(void) +{ + t_map *map; + char *key; + char *value; + + key = ft_strdup("keys"); + value = ft_strdup("values"); + map = ft_map_create(10); + ft_map_set(map, key, value, ft_strlen(key)); + ft_map_destroy_free(map, NULL); + key[0] = 'k'; + value[0] = 'v'; + map = ft_map_create(15); + ft_map_set(map, key, value, ft_strlen(key)); + ft_map_destroy_free(map, free); + free(key); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_get.c b/libft_personal/tests/ft_map/tests_map_get.c new file mode 100644 index 0000000..383c9de --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_get.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:14:47 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:28:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* + // search for a non existing key + ret = ft_map_get(map, "key2", strlen("key2") + 1); + --- + // search for a non existing key in a full map + map = ft_map_create(1); +*/ + +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_string.h" +#include + +int test_map_get_ultra_small(void) +{ + char *str[2]; + t_map *map; + void *ret; + + str[0] = ft_strdup("value"); + str[1] = ft_strdup("value2"); + map = ft_map_create(1); + ft_map_set(map, "key", str[0], ft_strlen("key")); + ft_map_set(map, "key2", str[1], ft_strlen("key")); + ret = ft_map_get(map, "key", ft_strlen("key")); + if (!ret) + return (1); + else if (ft_strcmp((char *)ret, "value")) + return (2); + ret = ft_map_get(map, "key2", ft_strlen("key2")); + if (!ret) + return (3); + else if (ft_strcmp((char *)ret, "value2")) + return (4); + ft_map_destroy_free(map, free); + return (0); +} + +int test_map_normal(void) +{ + char *str; + t_map *map; + void *ret; + + str = ft_strdup("value"); + map = ft_map_create(10); + ft_map_set(map, "key", str, ft_strlen("key")); + ret = ft_map_get(map, "key", ft_strlen("key")); + if (!ret) + return (1); + else if (ft_strcmp((char *)ret, "value")) + return (2); + ret = ft_map_get(map, "key2", ft_strlen("key2")); + if (ret) + return (3); + ft_map_destroy(map); + free(str); + return (0); +} + +int test_map_get(void) +{ + int ret; + + ret = test_map_get_ultra_small(); + if (ret != 0) + return (ret + 10 * 0); + ret = test_map_normal(); + if (ret != 0) + return (ret + 10 * 1); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_hash.c b/libft_personal/tests/ft_map/tests_map_hash.c new file mode 100644 index 0000000..5f881d3 --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_hash.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_hash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:21:33 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:24:06 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_map.h" +#include "ft_string.h" + +// we dont really test the hash function, we just test that +// it doesn't overflow our map + +int test_map_hash(void) +{ + size_t ret; + size_t ret2; + + ret = ft_hash_djb2("key", ft_strlen("key")); + ret2 = ft_hash_djb2("key2", ft_strlen("key2")); + if (ret == ret2) + return (1); + ret = ft_hash_sdbm("key", ft_strlen("key")); + ret2 = ft_hash_sdbm("key2", ft_strlen("key2")); + if (ret == ret2) + return (1); + ret = ft_hash_fnv1a("key", ft_strlen("key")); + ret2 = ft_hash_fnv1a("key2", ft_strlen("key2")); + if (ret == ret2) + return (1); + ret = ft_hash_dummy("key", ft_strlen("key")); + ret2 = ft_hash_dummy("key2", ft_strlen("key2")); + if (ret == ret2) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_remove.c b/libft_personal/tests/ft_map/tests_map_remove.c new file mode 100644 index 0000000..93f682b --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_remove.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:18:53 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:31:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_string.h" +#include "ft_map_types.h" +#include "ft_string.h" +#include + +static int test_map_remove_normal(void) +{ + char *str; + void *ret; + t_map *map; + + str = ft_strdup("value"); + map = ft_map_create(10); + ft_map_set(map, "key", str, ft_strlen("key")); + ret = ft_map_remove(map, "key", ft_strlen("key")); + if (!ret) + return (1); + else if (ret != str) + return (2); + ret = ft_map_remove(map, "key2", ft_strlen("key2")); + if (ret) + return (3); + ft_map_destroy(map); + free(str); + return (0); +} + +static int test_map_remove_colision(void) +{ + const char *keys[] = {"key", "key2", "key3", "key4", NULL}; + const char *str[] = { + ft_strdup("value"), ft_strdup("value2"), ft_strdup("value3"), + ft_strdup("value4"), NULL}; + t_map *map; + int ret; + + map = ft_map_create(1); + ret = 0; + while (str[ret]) + { + ft_map_set(map, keys[ret], str[ret], ft_strlen(keys[ret])); + ret++; + } + ret = 0; + if (ft_map_remove(map, keys[2], ft_strlen(keys[2])) != str[2]) + return (1); + if (ft_map_remove(map, keys[1], ft_strlen(keys[1])) != str[1]) + return (2); + if (ft_map_remove(map, keys[0], ft_strlen(keys[0])) != str[0]) + return (3); + if (ft_map_remove(map, keys[3], ft_strlen(keys[3])) != str[3]) + return (4); + return (ft_map_destroy(map), ft_apply_2d((void **)str, free), 0); +} + +int test_map_remove(void) +{ + int ret; + + ret = test_map_remove_normal(); + if (ret != 0) + return (ret); + ret = test_map_remove_colision(); + if (ret != 0) + return (ret + 10); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_set.c b/libft_personal/tests/ft_map/tests_map_set.c new file mode 100644 index 0000000..344cb13 --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_set.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:12:34 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:39:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_string.h" + +int test_map_set(void) +{ + char *str; + char *str2; + t_map *map; + + str = ft_strdup("value"); + str2 = ft_strdup("value2"); + map = ft_map_create(1); + if (ft_map_set(map, "key", str, ft_strlen("key")) != true) + return (1); + if (ft_map_set(map, "key", str2, ft_strlen("key")) != true) + return (1); + ft_map_destroy(map); + free(str); + free(str2); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_set_cmphash.c b/libft_personal/tests/ft_map/tests_map_set_cmphash.c new file mode 100644 index 0000000..5e4ed38 --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_set_cmphash.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_set_cmphash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:13:15 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:14:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" + +int test_map_set_cmp(void) +{ + t_map *map; + + map = ft_map_create(10); + ft_map_set_cmp(map, NULL); + if (map->cmp) + return (1); + ft_map_destroy(map); + return (0); +} + +int test_map_set_hash(void) +{ + t_map *map; + + map = ft_map_create(10); + ft_map_set_hash(map, NULL); + if (map->hash) + return (1); + ft_map_destroy(map); + return (0); +} diff --git a/libft_personal/tests/ft_map/tests_map_size.c b/libft_personal/tests/ft_map/tests_map_size.c new file mode 100644 index 0000000..cd3a65a --- /dev/null +++ b/libft_personal/tests/ft_map/tests_map_size.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_map_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 18:15:34 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 22:17:17 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_map.h" +#include "ft_map_types.h" +#include "ft_string.h" +#include + +int test_map_size(void) +{ + char *str; + t_map *map; + size_t ret; + + str = ft_strdup("value"); + map = ft_map_create(10); + ft_map_set(map, "key", str, ft_strlen("key")); + ret = ft_map_size(map); + if (ret != 1) + return (1); + ft_map_destroy(map); + free(str); + return (0); +} diff --git a/libft_personal/tests/ft_math/math_tests.c b/libft_personal/tests/ft_math/math_tests.c new file mode 100644 index 0000000..0d7e5e1 --- /dev/null +++ b/libft_personal/tests/ft_math/math_tests.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* math_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:16:35 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 20:50:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/math_tests.h" + +int tests_math(void) +{ + int i; + const t_test tests[] = { + {"abs", test_ft_abs}, {"clamp", test_ft_clamp}, + {"complex_abs", test_ft_complex_abs}, + {"complex_addl", test_ft_complex_addl}, + {"complex_mull", test_ft_complex_mull}, + {"complex_muld", test_ft_complex_muld}, + {"range", test_ft_range}, {"range_f", test_ft_range_f}, + {"range_d", test_ft_range_d}, {"log", test_ft_log}, + {"llogof", test_ft_llogof}, {"ullogof", test_ft_ullogof}, + {"logof", test_ft_logof}, {"min", test_ft_min}, + {"max", test_ft_max}, {"pow", test_ft_pow}, {"round", test_ft_round}, + {"sqrt", test_ft_sqrt}, {"clamp_f", test_ft_clamp_f}, + {"clamp_d", test_ft_clamp_d}, {"align", test_ft_align}, + {"align_2", test_ft_align_2}, {NULL, NULL}}; + + i = 0; + run_test(tests, &i); + return (i); +} diff --git a/libft_personal/tests/ft_math/tests_abs.c b/libft_personal/tests/ft_math/tests_abs.c new file mode 100644 index 0000000..04277e7 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_abs.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:20:34 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 23:21:18 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_abs(void) +{ + int x; + + x = -42; + if (ft_abs(x) != 42) + return (1); + x = 42; + if (ft_abs(x) != 42) + return (1); + x = 0; + if (ft_abs(x) != 0) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_align.c b/libft_personal/tests/ft_math/tests_align.c new file mode 100644 index 0000000..88182f4 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_align.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_align.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/26 19:53:26 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:05:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_align_2(void) +{ + size_t size; + size_t result; + + size = 42; + result = ft_align_2(size, 4); + if (result != 44) + return (1); + result = ft_align_2(size, 8); + if (result != 48) + return (2); + result = ft_align_2(size, 16); + if (result != 48) + return (3); + size = 275; + result = ft_align_2(size, 4); + if (result != 276) + return (4); + return (0); +} + +int test_ft_align(void) +{ + size_t size; + size_t result; + + size = 42; + result = ft_align(size, 4); + if (result != 44) + return (1); + result = ft_align(size, 8); + if (result != 48) + return (2); + result = ft_align(size, 16); + if (result != 48) + return (3); + size = 275; + result = ft_align(size, 6); + if (result != 276) + return (4); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_clamp.c b/libft_personal/tests/ft_math/tests_clamp.c new file mode 100644 index 0000000..08861b7 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_clamp.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_clamp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:46:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 09:12:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_clamp(void) +{ + const int max = 42; + const int min = 21; + int x; + + x = 0; + if (ft_clamp(x, min, max) != min) + return (1); + x = 21; + if (ft_clamp(x, min, max) != min) + return (1); + x = 42; + if (ft_clamp(x, min, max) != max) + return (1); + x = 84; + if (ft_clamp(x, min, max) != max) + return (1); + x = 23; + if (ft_clamp(x, min, max) != x) + return (1); + return (0); +} + +int test_ft_clamp_f(void) +{ + const float max = 42.0; + const float min = 21.0; + float x; + + x = 0; + if (ft_clamp_f(x, min, max) != min) + return (1); + x = 21; + if (ft_clamp_f(x, min, max) != min) + return (1); + x = 42; + if (ft_clamp_f(x, min, max) != max) + return (1); + x = 84; + if (ft_clamp_f(x, min, max) != max) + return (1); + x = 23; + if (ft_clamp_f(x, min, max) != x) + return (1); + return (0); +} + +int test_ft_clamp_d(void) +{ + const double max = 42.0; + const double min = 21.0; + double x; + + x = 0; + if (ft_clamp_d(x, min, max) != min) + return (1); + x = 21; + if (ft_clamp_d(x, min, max) != min) + return (1); + x = 42; + if (ft_clamp_d(x, min, max) != max) + return (1); + x = 84; + if (ft_clamp_d(x, min, max) != max) + return (1); + x = 23; + if (ft_clamp_d(x, min, max) != x) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_complex.c b/libft_personal/tests/ft_math/tests_complex.c new file mode 100644 index 0000000..dd3cf28 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_complex.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_complex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:46:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 09:08:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" +#include "ft_math_types.h" + +int test_ft_complex_abs(void) +{ + const t_complex x = {42, 0}; + const t_complex y = {0, 42}; + const t_complex z = {42, 42}; + double res; + + res = ft_complex_abs(x); + if (res < 41.99 || res > 42.01) + return (1); + res = ft_complex_abs(y); + if (res < 41.99 || res > 42.01) + return (2); + res = ft_complex_abs(z); + if (res < 59.39 || res > 59.41) + return (3); + return (0); +} + +int test_ft_complex_addl(void) +{ + const t_complex x = {42, 0}; + const t_complex y = {0, 42}; + t_complex res; + + res = ft_complex_addl(x, 42); + if (res.real != 84 || res.imaginary != 42) + return (1); + res = ft_complex_addl(y, 42); + if (res.real != 42 || res.imaginary != 84) + return (1); + return (0); +} + +int test_ft_complex_mull(void) +{ + const t_complex x = {42, 0}; + const t_complex y = {0, 42}; + t_complex res; + + res = ft_complex_mull(x, 2); + if (res.real != 84 || res.imaginary != 0) + return (1); + res = ft_complex_mull(y, 2); + if (res.real != 0 || res.imaginary != 84) + return (1); + return (0); +} + +int test_ft_complex_muld(void) +{ + const t_complex x = {42, 0}; + const t_complex y = {0, 42}; + t_complex res; + + res = ft_complex_muld(x, 2.5); + if (res.real != 105 || res.imaginary != 0) + return (1); + res = ft_complex_muld(y, 2.5); + if (res.real != 0 || res.imaginary != 105) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_intrange.c b/libft_personal/tests/ft_math/tests_intrange.c new file mode 100644 index 0000000..febd3e7 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_intrange.c @@ -0,0 +1,134 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_intrange.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:41:40 by bgoulard #+# #+# */ +/* Updated: 2024/05/26 17:09:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_range_errors(void) +{ + const int min = 9; + const int max = 42; + const int new_max = 21; + int x; + + x = -1; + if (ft_range(x, min, max, new_max) != 0) + return (1); + x = min; + if (ft_range(x, min + max, max, new_max) != 0) + return (2); + x = max + 1; + if (ft_range(x, min, max, 0) != 0) + return (3); + return (0); +} + +int test_ft_range(void) +{ + const int min = 9; + const int max = 42; + const int new_max = 21; + int x; + + x = -1; + if (ft_range(x, min, max, new_max) != 0) + return (1); + x = min; + if (ft_range(x, min, max, new_max) != 0) + return (2); + x = max + 1; + if (ft_range(x, max, max, new_max) != 0) + return (3); + if (ft_range(x, min, max, new_max) != new_max) + return (4); + x = 42; + if (ft_range(x, min, max, new_max) != new_max) + return (5); + x = 21; + if (ft_range(x, min, max, new_max) != (int)(((double)(x - min) / \ + (max - min)) * new_max)) + return (6); + return (test_ft_range_errors()); +} + +int test_ft_range_f(void) +{ + const float min = 9.0f; + const float max = 42.0f; + const float new_max = 21.0f; + float x; + + x = -1; + if (ft_range_f(x, min, max, new_max) != 0) + return (1); + x = min; + if (ft_range_f(x, min, max, new_max) != 0) + return (2); + x = max + 1; + if (ft_range_f(x, max, max, new_max) != 0) + return (3); + if (ft_range_f(x, min, max, new_max) != new_max) + return (4); + x = 42; + if (ft_range_f(x, min, max, new_max) != new_max) + return (5); + x = 21; + if (ft_range_f(x, min, max, new_max) != \ + (float)(((double)(x - min) / (max - min)) * new_max)) + return (6); + return (0); +} + +int test_ft_range_d_errors(void) +{ + const double min = 9.0; + const double max = 42.0; + const double new_max = 21.0; + double x; + + x = -1; + if (ft_range_d(x, min, max, new_max) != 0) + return (1); + x = min; + if (ft_range_d(x, min + max, max, new_max) != 0) + return (2); + x = max + 1; + if (ft_range_d(x, min, max, 0) != 0) + return (3); + return (0); +} + +int test_ft_range_d(void) +{ + const double min = 9.0; + const double max = 42.0; + const double new_max = 21.0; + double x; + + x = -1; + if (ft_range_d(x, min, max, new_max) != 0) + return (1); + x = min; + if (ft_range_d(x, min, max, new_max) != 0) + return (2); + x = max + 1; + if (ft_range_d(x, max, max, new_max) != 0) + return (3); + if (ft_range_d(x, min, max, new_max) != new_max) + return (4); + x = 42; + if (ft_range_d(x, min, max, new_max) != new_max) + return (5); + x = 21; + if (ft_range_d(x, min, max, new_max) != ((x - min) / (max - min)) * new_max) + return (6); + return (test_ft_range_d_errors()); +} diff --git a/libft_personal/tests/ft_math/tests_log.c b/libft_personal/tests/ft_math/tests_log.c new file mode 100644 index 0000000..0b344b7 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_log.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_log.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:42:03 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 09:27:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_log(void) +{ + int x; + + x = 23; + if (ft_log(x) != 1) + return (1); + x = 0; + if (ft_log(x) != -1) + return (2); + x = 1; + if (ft_log(x) != 0) + return (3); + x = 420; + if (ft_log(x) != 2) + return (4); + x = -12; + if (ft_log(x) != -1) + return (5); + return (0); +} + +int test_ft_llogof(void) +{ + if (ft_llogof(0, 2) != -1) + return (1); + if (ft_llogof(1, 2) != 0) + return (2); + if (ft_llogof(2, 2) != 1) + return (3); + if (ft_llogof(8, 2) != 3) + return (4); + if (ft_llogof(23, 3) != 2) + return (5); + return (0); +} + +int test_ft_ullogof(void) +{ + if (ft_ullogof(0, 2) != -1) + return (1); + if (ft_ullogof(1, 2) != 0) + return (2); + if (ft_ullogof(2, 2) != 1) + return (3); + return (0); +} + +int test_ft_logof(void) +{ + if (ft_logof(0, 2) != -1) + return (1); + if (ft_logof(1, 2) != 0) + return (2); + if (ft_logof(2, 2) != 1) + return (3); + if (ft_logof(8, 2) != 3) + return (4); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_minmax.c b/libft_personal/tests/ft_math/tests_minmax.c new file mode 100644 index 0000000..6a24213 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_minmax.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_minmax.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:49:30 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 00:54:36 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_min(void) +{ + const int a = 1; + const int b = 2; + const int c = 3; + + if (ft_min(a, b) != a) + return (1); + if (ft_min(b, c) != b) + return (1); + return (0); +} + +int test_ft_max(void) +{ + const int a = 1; + const int b = 2; + const int c = 3; + + if (ft_max(a, b) != b) + return (1); + if (ft_max(c, b) != c) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_pow.c b/libft_personal/tests/ft_math/tests_pow.c new file mode 100644 index 0000000..6d90959 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_pow.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:21:25 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 01:01:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_pow(void) +{ + size_t x; + size_t y; + + x = 2; + y = 3; + if (ft_pow(x, y) != 8) + return (1); + y = 0; + if (ft_pow(x, y) != 1) + return (1); + y = 1; + if (ft_pow(x, y) != 2) + return (1); + if (ft_pow(0, y) != 0) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_round.c b/libft_personal/tests/ft_math/tests_round.c new file mode 100644 index 0000000..60a5e8f --- /dev/null +++ b/libft_personal/tests/ft_math/tests_round.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ft_round.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:24:56 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 23:25:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_round(void) +{ + double x; + + x = 42.42; + if (ft_round(x) != 42) + return (1); + x = 42.52; + if (ft_round(x) != 43) + return (1); + x = 42.499; + if (ft_round(x) != 42) + return (1); + x = 42.5; + if (ft_round(x) != 43) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_math/tests_sqrt.c b/libft_personal/tests/ft_math/tests_sqrt.c new file mode 100644 index 0000000..2f99a35 --- /dev/null +++ b/libft_personal/tests/ft_math/tests_sqrt.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_sqrt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 23:49:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 09:34:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_math.h" + +int test_ft_sqrt(void) +{ + double x; + double result; + + x = 4; + result = ft_sqrt(x); + if (result > 2.01 || result < 1.99) + return (1); + x = 2; + result = ft_sqrt(x); + if (result > 1.42 || result < 1.41) + return (2); + x = -1; + result = ft_sqrt(x); + if (result >= -0.99 || result <= -1.01) + return (3); + return (0); +} diff --git a/libft_personal/tests/ft_optional/optional_tests.c b/libft_personal/tests/ft_optional/optional_tests.c new file mode 100644 index 0000000..4f31dbf --- /dev/null +++ b/libft_personal/tests/ft_optional/optional_tests.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* optional_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/06 00:57:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 12:08:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/optional_test.h" +#include "tests/tests.h" + +void *add_4(void *val) +{ + long long ptr; + + ptr = (long long)val; + ptr += 4; + return ((void *)ptr); +} + +int tests_optional(void) +{ + int collect; + const t_test test[] = { + {"copy", test_optional_copy}, + {"new", test_optional_new}, + {"from_val", test_optional_from_val}, + {"dup", test_optional_dup}, + {"chain", test_optional_chain}, + {"map", test_optional_map}, + {"unwrap", test_optional_unwrap}, + {"destroy", test_optional_destroy}, + {NULL, NULL} + }; + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_chain.c b/libft_personal/tests/ft_optional/tests_optional_chain.c new file mode 100644 index 0000000..aa3b539 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_chain.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optional_chain.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:28:51 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:58:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" +#include "tests/optional_test.h" +#include "ft_defs.h" + +int test_optional_chain(void) +{ + t_optional opt; + bool ret; + const t_data_tr_i function_list[3] = { + add_4, + add_4, + NULL + }; + + opt.pres = OPT_SOME; + opt.val = (void *)42; + ret = ft_optional_chain(&opt, function_list); + if (ret != true || opt.pres != OPT_SOME || opt.val != (void *)42 + 8) + return (1); + opt.pres = OPT_NONE; + ret = ft_optional_chain(&opt, function_list); + if (ret != false || opt.pres != OPT_NONE) + return (1); + ret = ft_optional_chain(&opt, NULL); + if (ret != false || opt.pres != OPT_NONE) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_copy.c b/libft_personal/tests/ft_optional/tests_optional_copy.c new file mode 100644 index 0000000..14cc5ec --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_copy.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optional_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:29:49 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 12:08:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" +#include + +int test_optional_copy(void) +{ + int *ptr; + t_optional opt_a; + t_optional opt_b; + + ptr = malloc(sizeof(int)); + *ptr = 42; + opt_a.pres = OPT_SOME; + opt_a.val = ptr; + ft_optional_copy(&opt_b, &opt_a); + if (opt_b.pres != OPT_SOME) + return (1); + if (*(int *)opt_b.val != 42) + return (2); + free(ptr); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_destroy.c b/libft_personal/tests/ft_optional/tests_optional_destroy.c new file mode 100644 index 0000000..3ddd972 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_destroy.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_optional_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:30:05 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 22:32:48 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" + +int test_optional_destroy(void) +{ + t_optional *opt_a; + + opt_a = ft_optional_new(); + opt_a->val = (void *)42; + opt_a->pres = OPT_SOME; + if (ft_optional_destroy(opt_a) != false) + return (1); + if (opt_a->pres != OPT_SOME) + return (2); + if (opt_a->val != (void *)42) + return (3); + opt_a->pres = OPT_NONE; + if (ft_optional_destroy(opt_a) != true) + return (4); + if (ft_optional_destroy(NULL) != false) + return (5); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_dup.c b/libft_personal/tests/ft_optional/tests_optional_dup.c new file mode 100644 index 0000000..8722b80 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_dup.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_optional_dup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:30:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 22:32:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" + +int test_optional_dup(void) +{ + t_optional opt_a; + t_optional *opt_b; + + opt_a.pres = OPT_SOME; + opt_a.val = (void *)42; + opt_b = ft_optional_dup(&opt_a); + if (!opt_b) + return (1); + if (opt_b->pres != OPT_SOME) + return (2); + if (opt_b->val != (void *)42) + return (3); + opt_b->pres = OPT_NONE; + ft_optional_destroy(opt_b); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_from_val.c b/libft_personal/tests/ft_optional/tests_optional_from_val.c new file mode 100644 index 0000000..6257618 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_from_val.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_optional_from_val.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:30:45 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 22:32:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" +#include + +int test_optional_from_val(void) +{ + int *ptr; + t_optional *opt; + + ptr = malloc(sizeof(int)); + *ptr = 42; + opt = ft_optional_from_val(ptr); + if (!opt) + return (1); + if (opt->pres != OPT_SOME) + return (2); + if (opt->val != ptr) + return (3); + opt->pres = OPT_NONE; + ft_optional_destroy(opt); + free(ptr); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_map.c b/libft_personal/tests/ft_optional/tests_optional_map.c new file mode 100644 index 0000000..4bb8590 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_map.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optional_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:31:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:59:14 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" +#include "tests/optional_test.h" + +int test_optional_map(void) +{ + t_optional opt; + t_optional ret; + void *(*function_list[3])(void *); + + function_list[0] = &add_4; + function_list[1] = &add_4; + function_list[2] = NULL; + opt.pres = OPT_SOME; + opt.val = (void *)42; + ret = ft_optional_map(&opt, function_list); + if (ret.pres != OPT_SOME) + return (1); + if (ret.val != (void *)42 + 8) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_new.c b/libft_personal/tests/ft_optional/tests_optional_new.c new file mode 100644 index 0000000..36a7a26 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_new.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optional_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:31:28 by bgoulard #+# #+# */ +/* Updated: 2024/07/17 22:39:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" + +int test_optional_new(void) +{ + t_optional *opt; + + opt = ft_optional_new(); + if (!opt) + return (1); + if (opt->pres != OPT_NONE) + return (2); + if (opt->val) + return (3); + ft_optional_destroy(opt); + return (0); +} diff --git a/libft_personal/tests/ft_optional/tests_optional_unwrap.c b/libft_personal/tests/ft_optional/tests_optional_unwrap.c new file mode 100644 index 0000000..9d8e768 --- /dev/null +++ b/libft_personal/tests/ft_optional/tests_optional_unwrap.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_optional_unwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 22:32:05 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 22:30:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_optional.h" +#include "ft_optional_types.h" +#include +#include +#include +#include + +int test_optional_unwrap(void) +{ + t_optional opt; + int *ptr; + + opt.pres = OPT_SOME; + ptr = malloc(sizeof(int)); + *ptr = 42; + opt.val = ptr; + if (ft_optional_unwrap(opt) != ptr) + return (1); + free(ptr); + opt.pres = OPT_NONE; + opt.val = (void *)0xDEADBEEF; + if (ft_optional_unwrap(opt) != NULL) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_pair/pair_tests.c b/libft_personal/tests/ft_pair/pair_tests.c new file mode 100644 index 0000000..c895311 --- /dev/null +++ b/libft_personal/tests/ft_pair/pair_tests.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pair_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 15:53:09 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:15:20 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/pair_tests.h" +#include "tests/tests.h" + +int tests_pair(void) +{ + int collect; + const t_test test[] = { + {"test_pair_set", test_pair_set}, + {"test_pair_new", test_pair_new}, + {"test_pair_second", test_pair_second}, + {"test_pair_first", test_pair_first}, + {"test_pair_cmp", test_pair_cmp}, + {"test_pair_cmp_first", test_pair_cmp_first}, + {"test_pair_cmp_second", test_pair_cmp_second}, + {"tests_pair_destroy", tests_pair_destroy}, + {NULL, NULL} + }; + + collect = 0; + run_test(test, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_cmp.c b/libft_personal/tests/ft_pair/tests_pair_cmp.c new file mode 100644 index 0000000..25b1b0d --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_cmp.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_cmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:20:58 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:43:29 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "tests/pair_tests.h" +#include "tests/tests_lambda_functions.h" + +static int test_ptr(void) +{ + t_pair pair_a; + t_pair pair_b; + void *a; + void *b; + void *c; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADDEAD; + c = (void *)0xDEADCAFE; + ft_pair_set(&pair_a, a, b); + ft_pair_set(&pair_b, c, a); + if (ft_pair_cmp(&pair_a, &pair_b, NULL) == 0) + return (1); + if (ft_pair_cmp(&pair_a, &pair_a, NULL) != 0) + return (2); + if (ft_pair_cmp(&pair_b, &pair_a, NULL) == 0) + return (3); + if (ft_pair_cmp(NULL, &pair_a, NULL) == 0 || ft_pair_cmp(&pair_a, NULL, + NULL) == 0) + return (4); + if (ft_pair_cmp(NULL, NULL, NULL) != 0) + return (5); + return (0); +} + +static int test_cmp(void) +{ + t_pair pair_a; + t_pair pair_b; + long a; + long b; + long c; + + a = 21; + b = 42; + c = 420; + ft_pair_set(&pair_a, (void *)a, (void *)b); + ft_pair_set(&pair_b, (void *)c, (void *)a); + if (ft_pair_cmp(&pair_a, &pair_b, &cmp_int) == 0) + return (1); + return (0); +} + +int test_pair_cmp(void) +{ + int ret; + + ret = test_ptr(); + if (ret != 0) + return (ret); + ret = test_cmp(); + if (ret != 0) + return (ret + 10); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_cmp_first.c b/libft_personal/tests/ft_pair/tests_pair_cmp_first.c new file mode 100644 index 0000000..8f42714 --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_cmp_first.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_cmp_first.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:20:45 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:48:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "ft_string.h" +#include "tests/pair_tests.h" + +static int test_ptr(void) +{ + t_pair pair; + t_pair pair_b; + void *a; + void *b; + void *c; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADCAFE; + c = (void *)0xDEADDEAD; + ft_pair_set(&pair, b, b); + pair_b.first = c; + if (ft_pair_cmp_first(&pair, &pair_b, NULL) >= 0) + return (1); + pair_b.first = b; + if (ft_pair_cmp_first(&pair, &pair_b, NULL) != 0) + return (2); + pair_b.first = a; + if (ft_pair_cmp_first(&pair, &pair_b, NULL) <= 0) + return (3); + if (ft_pair_cmp_first(&pair, NULL, NULL) == 0 || ft_pair_cmp_first(NULL, + &pair, NULL) == 0) + return (4); + if (ft_pair_cmp_first(NULL, NULL, NULL) != 0) + return (5); + return (0); +} + +static int test_cmp(void) +{ + t_pair pair_a; + t_pair pair_b; + const char *a = "a"; + const char *b = "b"; + const char *c = "c"; + + ft_pair_set(&pair_a, (void *)a, (void *)b); + ft_pair_set(&pair_b, (void *)c, (void *)a); + if (ft_pair_cmp_first(&pair_a, &pair_b, (t_data_cmp) & ft_strcmp) == 0) + return (1); + return (0); +} + +int test_pair_cmp_first(void) +{ + int ret; + + ret = test_ptr(); + if (ret != 0) + return (ret); + ret = test_cmp(); + if (ret != 0) + return (ret + 10); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_cmp_second.c b/libft_personal/tests/ft_pair/tests_pair_cmp_second.c new file mode 100644 index 0000000..81d70ed --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_cmp_second.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_cmp_second.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:19:11 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:48:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "ft_string.h" +#include "tests/pair_tests.h" + +static int test_ptr(void) +{ + t_pair pair; + t_pair pair_b; + void *a; + void *b; + void *c; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADCAFE; + c = (void *)0xDEADDEAD; + pair_b.second = c; + ft_pair_set(&pair, b, b); + if (ft_pair_cmp_second(&pair, &pair_b, NULL) >= 0) + return (1); + pair_b.second = b; + if (ft_pair_cmp_second(&pair, &pair_b, NULL) != 0) + return (2); + pair_b.second = a; + if (ft_pair_cmp_second(&pair, &pair_b, NULL) <= 0) + return (3); + if (ft_pair_cmp_second(&pair, NULL, NULL) == 0 || ft_pair_cmp_second(NULL, + &pair, NULL) == 0) + return (4); + if (ft_pair_cmp_second(NULL, NULL, NULL) != 0) + return (5); + return (0); +} + +static int test_cmp(void) +{ + t_pair pair_a; + t_pair pair_b; + const char *a = "a"; + const char *b = "b"; + const char *c = "c"; + + ft_pair_set(&pair_a, (void *)a, (void *)b); + ft_pair_set(&pair_b, (void *)c, (void *)a); + if (ft_pair_cmp_second(&pair_a, &pair_b, (t_data_cmp) & ft_strcmp) == 0) + return (1); + return (0); +} + +int test_pair_cmp_second(void) +{ + int ret; + + ret = test_ptr(); + if (ret != 0) + return (ret); + ret = test_cmp(); + if (ret != 0) + return (ret); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_destroy.c b/libft_personal/tests/ft_pair/tests_pair_destroy.c new file mode 100644 index 0000000..4536f24 --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_destroy.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 17:09:25 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:16:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/pair_tests.h" +#include "ft_pair_types.h" +#include "ft_pair.h" + +int tests_pair_destroy(void) +{ + t_pair *pair; + + pair = ft_pair_new("key", "value"); + ft_pair_destroy(&pair, NULL, NULL); + if (pair != NULL) + return (1); + pair = ft_pair_new(ft_strdup("key"), ft_strdup("value")); + ft_pair_destroy(&pair, &free, &free); + if (pair != NULL) + return (2); + ft_pair_destroy(NULL, NULL, NULL); + pair = NULL; + ft_pair_destroy(&pair, NULL, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_get_first.c b/libft_personal/tests/ft_pair/tests_pair_get_first.c new file mode 100644 index 0000000..4a73c2a --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_get_first.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_get_first.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:17:14 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:04:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "tests/pair_tests.h" + +int test_pair_first(void) +{ + t_pair pair; + void *a; + void *b; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADDEAD; + ft_pair_set(&pair, a, b); + if (ft_pair_first(&pair) != a) + return (1); + if (ft_pair_first(NULL) != NULL) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_get_second.c b/libft_personal/tests/ft_pair/tests_pair_get_second.c new file mode 100644 index 0000000..46d2bec --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_get_second.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_get_second.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:15:50 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:04:40 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "tests/pair_tests.h" + +int test_pair_second(void) +{ + t_pair pair; + void *a; + void *b; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADDEAD; + ft_pair_set(&pair, a, b); + if (ft_pair_second(&pair) != b) + return (1); + if (ft_pair_second(NULL) != NULL) + return (2); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_new.c b/libft_personal/tests/ft_pair/tests_pair_new.c new file mode 100644 index 0000000..0b78dd2 --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_new.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:14:03 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 17:25:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "tests/pair_tests.h" + +int test_pair_new(void) +{ + t_pair *pair; + void *a; + void *b; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADDEAD; + pair = ft_pair_new(NULL, NULL); + if (!pair) + return (1); + if (pair->first || pair->second) + return (2); + ft_pair_set(pair, a, b); + if (pair->first != a || pair->second != b) + return (3); + ft_pair_destroy(&pair, NULL, NULL); + return (0); +} diff --git a/libft_personal/tests/ft_pair/tests_pair_set.c b/libft_personal/tests/ft_pair/tests_pair_set.c new file mode 100644 index 0000000..2d8bcc8 --- /dev/null +++ b/libft_personal/tests/ft_pair/tests_pair_set.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_pair_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/06 16:09:32 by bgoulard #+# #+# */ +/* Updated: 2024/07/06 16:46:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_pair.h" +#include "ft_pair_types.h" +#include "tests/pair_tests.h" + +int test_pair_set(void) +{ + t_pair pair; + void *a; + void *b; + void *c; + + a = (void *)0xDEADBEEF; + b = (void *)0xDEADDEAD; + c = (void *)0xDEADCAFE; + ft_pair_set(&pair, a, b); + if (pair.first != a || pair.second != b) + return (1); + ft_pair_set(&pair, c, a); + if (pair.first != c || pair.second != a) + return (1); + ft_pair_set(NULL, a, b); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/ft_char_tests.c b/libft_personal/tests/ft_string/ft_char/ft_char_tests.c new file mode 100644 index 0000000..d2e66b9 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/ft_char_tests.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_char_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 14:31:03 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:37:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/char_tests.h" +#include "tests/tests.h" + +int char_tests(void) +{ + int i; + const t_test tests[] = { + {"isalnum", test_ft_isalnum}, {"isalpha", test_ft_isalpha}, + {"isascii", test_ft_isascii}, {"isdigit", test_ft_isdigit}, + {"isprint", test_ft_isprint}, {"tolower", test_ft_tolower}, + {"toupper", test_ft_toupper}, {"isalnum", test_ft_isalnum}, + {"putchar", test_ft_putchar}, {"ishexdigit", test_ft_ishexdigit}, + {"isoctdigit", test_ft_isoctdigit}, {"isspace", test_ft_isspace}, + {NULL, NULL}}; + + i = 0; + run_test(tests, &i); + return (i); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isalnum.c b/libft_personal/tests/ft_string/ft_char/tests_isalnum.c new file mode 100644 index 0000000..c7e28a2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isalnum.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:55:54 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:11:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int test_ft_isalnum(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isalnum(i) != ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') + || (i >= 'A' && i <= 'Z'))) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isalpha.c b/libft_personal/tests/ft_string/ft_char/tests_isalpha.c new file mode 100644 index 0000000..6b98ade --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isalpha.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:56:07 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 15:56:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int test_ft_isalpha(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isalpha(i) != ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isascii.c b/libft_personal/tests/ft_string/ft_char/tests_isascii.c new file mode 100644 index 0000000..77aa91a --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isascii.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:56:46 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:30:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int test_ft_isascii(void) +{ + int i; + + i = -1; + while (i < 256) + { + if (ft_isascii(i) != (i >= 0 && i <= 127)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isdigit.c b/libft_personal/tests/ft_string/ft_char/tests_isdigit.c new file mode 100644 index 0000000..e42bdd3 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isdigit.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:57:05 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 15:57:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int test_ft_isdigit(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isdigit(i) != (i >= '0' && i <= '9')) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_ishexdigit.c b/libft_personal/tests/ft_string/ft_char/tests_ishexdigit.c new file mode 100644 index 0000000..7c7d309 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_ishexdigit.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ishexdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 16:25:47 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:26:01 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +static int local_ishexdigit(int c) +{ + if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') + || (c >= 'A' && c <= 'F')) + return (1); + return (0); +} + +int test_ft_ishexdigit(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_ishexdigit(i) != local_ishexdigit(i)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isoctdigit.c b/libft_personal/tests/ft_string/ft_char/tests_isoctdigit.c new file mode 100644 index 0000000..011466b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isoctdigit.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isoctdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 16:26:13 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:26:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +static int local_isoctdigit(int c) +{ + if (c >= '0' && c <= '7') + return (1); + return (0); +} + +int test_ft_isoctdigit(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isoctdigit(i) != local_isoctdigit(i)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isprint.c b/libft_personal/tests/ft_string/ft_char/tests_isprint.c new file mode 100644 index 0000000..417a336 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isprint.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:57:59 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 15:58:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +int test_ft_isprint(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isprint(i) != (i >= 32 && i <= 126)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_isspace.c b/libft_personal/tests/ft_string/ft_char/tests_isspace.c new file mode 100644 index 0000000..6edce03 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_isspace.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_char_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 16:32:37 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 21:25:51 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +static int local_isspace(int c) +{ + if (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' + || c == '\v') + return (1); + return (0); +} + +int test_ft_isspace(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_isspace(i) != local_isspace(i)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_puchar.c b/libft_personal/tests/ft_string/ft_char/tests_puchar.c new file mode 100644 index 0000000..d666b26 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_puchar.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_puchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 16:01:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:18:25 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" +#include "tests/tests.h" +#include +#include +#include + +int test_ft_putchar(void) +{ + const char *file; + char buf[256]; + char c; + int fd; + int ret; + + file = "ft_putchar"; + c = 0; + fd = open_test_file((char **)&file); + if (fd < 0) + return (1); + while ((unsigned char)c < 255) + ft_putchar_fd(c++, fd); + close(fd); + fd = open(file, O_RDONLY); + if (fd < 0) + return (2); + ret = read(fd, buf, 256); + if (ret != 255) + return (3); + buf[255] = 0; + while (ret--) + if (buf[ret] != --c) + return (4); + return (destroy_test_file(fd, (char *)file), free((char *)file), 0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_tolower.c b/libft_personal/tests/ft_string/ft_char/tests_tolower.c new file mode 100644 index 0000000..6387c64 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_tolower.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 15:59:59 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:00:44 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +static int local_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} + +int test_ft_tolower(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_tolower(i) != local_tolower(i)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_char/tests_toupper.c b/libft_personal/tests/ft_string/ft_char/tests_toupper.c new file mode 100644 index 0000000..0cae61c --- /dev/null +++ b/libft_personal/tests/ft_string/ft_char/tests_toupper.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/23 16:01:14 by bgoulard #+# #+# */ +/* Updated: 2024/05/23 16:18:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_char.h" + +static int local_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} + +int test_ft_toupper(void) +{ + int i; + + i = 0; + while (i < 256) + { + if (ft_toupper(i) != local_toupper(i)) + return (1); + i++; + } + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/mem_tests.c b/libft_personal/tests/ft_string/ft_mem/mem_tests.c new file mode 100644 index 0000000..48cf83b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/mem_tests.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mem_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:39:27 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 20:54:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/str__mem_tests.h" + +int mem_tests(void) +{ + int collect; + const t_test tests[] = { + {"apply_2d", test_apply_2d}, {"free_2d", test_free_2d}, + {"len_2d", test_len_2d}, {"fd_to_buff", test_fd_to_buff}, + {"bzero", test_bzero}, {"calloc", test_calloc}, {"realloc", test_realloc}, + {"free", test_free}, {"memchr", test_memchr}, {"memcmp", test_memcmp}, + {"memcpy", test_memcpy}, {"memmap", test_memmap}, {"memmove", test_memmove}, + {"memset", test_memset}, {"swap", test_swap}, {"qsort", test_qsort}, + {NULL, NULL} + }; + + collect = 0; + run_test(tests, &collect); + return (collect); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_apply_2d.c b/libft_personal/tests/ft_string/ft_mem/tests_apply_2d.c new file mode 100644 index 0000000..fe96bf3 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_apply_2d.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_apply_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 16:05:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:59:40 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +static void to_a(void *ptr) +{ + char *sptr; + + sptr = (char *)ptr; + while (*sptr) + { + *sptr = 'a'; + sptr++; + } +} + +int test_apply_2d(void) +{ + char *arr2d[4]; + size_t i; + + i = 0; + while (i < 3) + arr2d[i++] = ft_strdup("Hello World"); + arr2d[i] = NULL; + ft_apply_2d((void **)arr2d, to_a); + if (ft_strcmp(arr2d[0], "aaaaaaaaaaa") != 0 || ft_strcmp(arr2d[1], + "aaaaaaaaaaa") != 0 || ft_strcmp(arr2d[2], "aaaaaaaaaaa") != 0) + return (1); + return (ft_apply_2d((void **)arr2d, free), 0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_bzero.c b/libft_personal/tests/ft_string/ft_mem/tests_bzero.c new file mode 100644 index 0000000..071a752 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_bzero.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:24:17 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 14:37:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_bzero(void) +{ + char str[10]; + const char str2[10] = {0}; + + ft_strlcpy(str, "123456789", 10); + ft_bzero(str, 10); + if (ft_memcmp(str, str2, 10) != 0) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_calloc.c b/libft_personal/tests/ft_string/ft_mem/tests_calloc.c new file mode 100644 index 0000000..fcc10e9 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_calloc.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:23:35 by bgoulard #+# #+# */ +/* Updated: 2024/06/25 21:40:19 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +// str[x] = 'a' is to test that we can write in the memory we asked for +int test_calloc(void) +{ + char *str; + + str = ft_calloc(10, sizeof(char)); + str[9] = 'a'; + free(str); + str = ft_calloc(10, sizeof(char *)); + str[9 * sizeof(char *)] = 'a'; + free(str); + str = ft_calloc(0, sizeof(char)); + free(str); + str = ft_calloc(SIZE_MAX, SIZE_MAX); + if (str) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_fd_to_buff.c b/libft_personal/tests/ft_string/ft_mem/tests_fd_to_buff.c new file mode 100644 index 0000000..0001b0b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_fd_to_buff.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_fd_to_buff.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 16:15:20 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 19:34:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/tests.h" + +static int base_case(void) +{ + char buff[100]; + char *ret; + int fd; + + ft_bzero(buff, 100); + ft_strlcat(buff, "Hello World\n\n\n\n\tv\vjkldqskjfsldfjsmldfkj\n", 100); + fd = open(TESTS_FPREFIX "test_fd_to_buff.txt", O_RDWR | O_CREAT, 0644); + if (fd < 0) + return (1); + write(fd, buff, ft_strlen(buff)); + close(fd); + fd = open(TESTS_FPREFIX "test_fd_to_buff.txt", O_RDONLY | O_CREAT); + if (fd < 0) + return (2); + ret = ft_fd_to_buff(fd); + if (ft_strcmp(ret, buff) != 0) + return (3); + destroy_test_file(fd, TESTS_FPREFIX "test_fd_to_buff.txt"); + return (free(ret), 0); +} + +static int error_case(void) +{ + char *ret; + int bad_fd; + + bad_fd = -1; + ret = ft_fd_to_buff(bad_fd); + if (ret) + return (1); + bad_fd = open(TESTS_FPREFIX "test_fd_to_buff.txt", O_CREAT, 0004); + if (bad_fd < 0) + return (2); + close(bad_fd); + bad_fd = open(TESTS_FPREFIX "test_fd_to_buff.txt", O_RDONLY); + ret = ft_fd_to_buff(bad_fd); + if (ret) + return (3); + destroy_test_file(bad_fd, TESTS_FPREFIX "test_fd_to_buff.txt"); + return (0); +} + +int test_fd_to_buff(void) +{ + int ret; + + ret = base_case(); + if (ret != 0) + return (ret); + ret = error_case(); + if (ret != 0) + return (ret + 10); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_free.c b/libft_personal/tests/ft_string/ft_mem/tests_free.c new file mode 100644 index 0000000..3d4d167 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_free.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:22:59 by bgoulard #+# #+# */ +/* Updated: 2024/08/21 21:49:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_free(void) +{ + char *str; + + str = ft_calloc(10, sizeof(char)); + ft_free((void **)&str); + if (str) + return (1); + ft_free((void **)&str); + ft_free((void **) NULL); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_free_2d.c b/libft_personal/tests/ft_string/ft_mem/tests_free_2d.c new file mode 100644 index 0000000..1dd62ed --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_free_2d.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_free_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/28 23:42:53 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 23:55:41 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +// we assume that free_2d is working, +// checking if memory is freed properly is not possible in this context +int test_free_2d(void) +{ + char **tab; + + tab = ft_split("Hello World", ' '); + ft_free_2d((void **)tab); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_len_2d.c b/libft_personal/tests/ft_string/ft_mem/tests_len_2d.c new file mode 100644 index 0000000..0ca24e2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_len_2d.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_len_2d.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/29 00:11:07 by bgoulard #+# #+# */ +/* Updated: 2024/05/29 00:15:46 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_len_2d(void) +{ + char *arr2d[4]; + + arr2d[0] = "Hello"; + arr2d[1] = "World"; + arr2d[2] = "!"; + arr2d[3] = NULL; + if (ft_len_2d((const void *const *)arr2d) != 3) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memchr.c b/libft_personal/tests/ft_string/ft_mem/tests_memchr.c new file mode 100644 index 0000000..9472931 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:22:42 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:18:21 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_memchr(void) +{ + char str[10]; + + ft_strlcpy(str, "1234567", 10); + ft_strlcat(str, "890", 10); + if (ft_memchr(str, '8', 10) != &str[7]) + return (1); + if (ft_memchr(str, '9', 10) != &str[8]) + return (2); + if (ft_memchr(str, 'z', 10)) + return (3); + if (ft_memchr(str, '7', 10) != &str[6]) + return (4); + if (ft_memchr(str, '7', 0)) + return (5); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memcmp.c b/libft_personal/tests/ft_string/ft_mem/tests_memcmp.c new file mode 100644 index 0000000..58c646a --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memcmp.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:22:12 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 00:46:20 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_memcmp(void) +{ + const char str[] = "1234567\0009"; + const char str2[] = "1234567\0009"; + const char str3[] = "1234567\000a"; + const char str4[] = "1234567\0009"; + + if (ft_memcmp(str, str2, 10) != 0) + return (1); + if (ft_memcmp(str, str3, 10) >= 0) + return (2); + if (ft_memcmp(str3, str, 10) <= 0) + return (3); + if (ft_memcmp(str, str2, 0) != 0) + return (4); + if (ft_memcmp(str, str4, 10) != 0) + return (5); + if (ft_memcmp(str, str, 999) != 0) + return (6); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memcpy.c b/libft_personal/tests/ft_string/ft_mem/tests_memcpy.c new file mode 100644 index 0000000..4234723 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:22:05 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 00:49:34 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +// last ft_memcpy is a test to check that we do nothing on +// dst if src and dst are the same otherwise it will segfault +int test_memcpy(void) +{ + char str[10]; + const char str3[] = "1234567\0009"; + + ft_strlcpy(str, "123456789", 10); + ft_memcpy(str, "abc", 3); + if (ft_memcmp(str, "abc456789", 10) != 0) + return (1); + ft_memcpy(str, str3, 10); + if (ft_memcmp(str, str3, 10) != 0) + return (2); + ft_memcpy(str, str, 999); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memmap.c b/libft_personal/tests/ft_string/ft_mem/tests_memmap.c new file mode 100644 index 0000000..9ca90a8 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memmap.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:21:39 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 00:53:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_defs.h" +#include "ft_string.h" + +int test_memmap(void) +{ + const char *tb[3] = {"Hello", "World", "!"}; + char **str2; + + str2 = (char **) ft_memmap((void *)tb, sizeof(tb) / sizeof(tb[0]), + (t_data_tr)ft_strdup); + if (ft_strcmp(str2[0], "Hello") != 0) + return (1); + if (ft_strcmp(str2[1], "World") != 0) + return (1); + if (ft_strcmp(str2[2], "!") != 0) + return (1); + ft_free_2d((void **)str2); + str2 = (char **) ft_memmap(NULL, 0, (t_data_tr)ft_strdup); + if (str2) + return (2); + str2 = (char **) ft_memmap((void *)tb, 0, (t_data_tr)ft_strdup); + if (str2) + return (3); + str2 = (char **) ft_memmap((void *)tb, sizeof(tb) / sizeof(tb[0]), NULL); + if (str2) + return (4); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memmove.c b/libft_personal/tests/ft_string/ft_mem/tests_memmove.c new file mode 100644 index 0000000..0628cc3 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memmove.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:21:39 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 19:27:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +// last ft_memmove is a test to check that we do nothing on +// dst if src and dst are the same otherwise it will segfault +int test_memmove(void) +{ + char str[10]; + const char str3[] = "1234567a9"; + + ft_strlcpy(str, "123456789", 10); + ft_memmove(str, "abc", 3); + if (ft_memcmp(str, "abc456789", 10) != 0) + return (1); + ft_memmove(str, str3, 10); + if (ft_memcmp(str, str3, 10) != 0) + return (2); + ft_memmove(str, str + 2, 5); + if (ft_memcmp(str, "3456767a9", 10) != 0) + return (3); + ft_memmove(str + 2, str, 5); + if (ft_memcmp(str, "3434567a9", 10) != 0) + return (4); + ft_memmove(str, str, 999); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_memset.c b/libft_personal/tests/ft_string/ft_mem/tests_memset.c new file mode 100644 index 0000000..ccd3956 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_memset.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:21:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:01:56 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +/* + // ignore warning for memset args + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wmemset-transposed-args" +*/ +int test_memset(void) +{ + char str[10]; + char str2[10]; + + ft_strlcpy(str, "123456789", 10); + ft_strlcpy(str2, "123456789", 10); + ft_memset(str, 'a', 10); + if (ft_memcmp(str, "aaaaaaaaaa", 10) != 0) + return (1); + ft_memset(str, 'b', 0); + if (ft_memcmp(str, "aaaaaaaaaa", 10) != 0) + return (2); + ft_memset(str, 'c', 5); + if (ft_memcmp(str, "cccccaaaaa", 10) != 0) + return (3); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_qsort.c b/libft_personal/tests/ft_string/ft_mem/tests_qsort.c new file mode 100644 index 0000000..4a855f0 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_qsort.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_qsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:20:05 by bgoulard #+# #+# */ +/* Updated: 2024/05/25 12:20:11 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + + // non stable +// int tab[10] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9}; +// int tab2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9}; +// int tab3[10] = {1, 256, 3, 4, -6, 7, 81, 10, 9}; +// int tab4[10] = {1, 256, 3, 4, -6, 7, 81, 10, 9}; + +// ft_qsort(tab, 10, sizeof(int), cmp_int); +// qsort(tab2, 10, sizeof(int), cmp_int); +// if (memcmp(tab, tab2, 10) != 0) +// return (1); + +// ft_qsort(tab3, 10, sizeof(int), cmp_int); +// qsort(tab4, 10, sizeof(int), cmp_int); +// if (memcmp(tab3, tab4, 10) != 0) +// return (2); +int test_qsort(void) +{ + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_realloc.c b/libft_personal/tests/ft_string/ft_mem/tests_realloc.c new file mode 100644 index 0000000..31945c2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_realloc.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:21:02 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 19:27:29 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_realloc(void) +{ + char *str; + char *str_ret; + + str = ft_calloc(15, sizeof(char)); + ft_strlcpy(str, "Hello world", 15); + str = ft_realloc(str, 25, 15); + ft_strlcat(str, " this is zod!", 25); + if (ft_strcmp(str, "Hello world this is zod!") != 0) + return (1); + str_ret = ft_realloc(str, 10, 10); + if (str != str_ret) + return (2); + free(str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_mem/tests_swap.c b/libft_personal/tests/ft_string/ft_mem/tests_swap.c new file mode 100644 index 0000000..407739d --- /dev/null +++ b/libft_personal/tests/ft_string/ft_mem/tests_swap.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 12:20:29 by bgoulard #+# #+# */ +/* Updated: 2024/05/25 15:13:47 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +/* + // ignore warning for memset args + #pragma clang diagnostic pop +*/ + +int test_swap(void) +{ + const int pair_og[2] = {1, 2}; + const int *pair[2]; + const int *pair2[2] = {&pair_og[1], &pair_og[0]}; + + pair[0] = &pair_og[0]; + pair[1] = &pair_og[1]; + ft_swap((void **)&pair[0], (void **)&pair[1]); + if (pair[0] != pair2[0] || pair[1] != pair2[1]) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_str/str_tests.c b/libft_personal/tests/ft_string/ft_str/str_tests.c new file mode 100644 index 0000000..2847cb8 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/str_tests.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_atof.c b/libft_personal/tests/ft_string/ft_str/test_atof.c new file mode 100644 index 0000000..3c4a362 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_atof.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_atoi.c b/libft_personal/tests/ft_string/ft_str/test_atoi.c new file mode 100644 index 0000000..bb3f0be --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_atoi.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_atoi_base.c b/libft_personal/tests/ft_string/ft_str/test_atoi_base.c new file mode 100644 index 0000000..8f9b9da --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_atoi_base.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_atoi_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_gnl.c b/libft_personal/tests/ft_string/ft_str/test_gnl.c new file mode 100644 index 0000000..1f730a9 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_gnl.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_gnl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_itoa.c b/libft_personal/tests/ft_string/ft_str/test_itoa.c new file mode 100644 index 0000000..27fcbd5 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_itoa.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_itoa_base.c b/libft_personal/tests/ft_string/ft_str/test_itoa_base.c new file mode 100644 index 0000000..9552d8b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_itoa_base.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_putendl.c b/libft_personal/tests/ft_string/ft_str/test_putendl.c new file mode 100644 index 0000000..3b2fc13 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_putendl.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_putnbr.c b/libft_personal/tests/ft_string/ft_str/test_putnbr.c new file mode 100644 index 0000000..0005790 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_putnbr.c @@ -0,0 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_putstr.c b/libft_personal/tests/ft_string/ft_str/test_putstr.c new file mode 100644 index 0000000..fd1b2d7 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_putstr.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_shift_args.c b/libft_personal/tests/ft_string/ft_str/test_shift_args.c new file mode 100644 index 0000000..3967b67 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_shift_args.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_shift_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_split.c b/libft_personal/tests/ft_string/ft_str/test_split.c new file mode 100644 index 0000000..0289220 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_split.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_splits.c b/libft_personal/tests/ft_string/ft_str/test_splits.c new file mode 100644 index 0000000..52c0d8a --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_splits.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_splits.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:22:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 14:16:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isalnum.c b/libft_personal/tests/ft_string/ft_str/test_str_isalnum.c new file mode 100644 index 0000000..b046103 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isalnum.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isalpha.c b/libft_personal/tests/ft_string/ft_str/test_str_isalpha.c new file mode 100644 index 0000000..489931e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isalpha.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isbool.c b/libft_personal/tests/ft_string/ft_str/test_str_isbool.c new file mode 100644 index 0000000..229d231 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isbool.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isbool.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isdigit.c b/libft_personal/tests/ft_string/ft_str/test_str_isdigit.c new file mode 100644 index 0000000..0b1be9b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isdigit.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isdouble.c b/libft_personal/tests/ft_string/ft_str/test_str_isdouble.c new file mode 100644 index 0000000..27521b4 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isdouble.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isdouble.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isfloat.c b/libft_personal/tests/ft_string/ft_str/test_str_isfloat.c new file mode 100644 index 0000000..d183a92 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isfloat.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isfloat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_ishex.c b/libft_personal/tests/ft_string/ft_str/test_str_ishex.c new file mode 100644 index 0000000..c88598b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_ishex.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_ishex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isint.c b/libft_personal/tests/ft_string/ft_str/test_str_isint.c new file mode 100644 index 0000000..58bd990 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isint.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_islong.c b/libft_personal/tests/ft_string/ft_str/test_str_islong.c new file mode 100644 index 0000000..f784acd --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_islong.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_islong.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isnum.c b/libft_personal/tests/ft_string/ft_str/test_str_isnum.c new file mode 100644 index 0000000..1bd26e9 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isnum.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isoct.c b/libft_personal/tests/ft_string/ft_str/test_str_isoct.c new file mode 100644 index 0000000..788ebb6 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isoct.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isoct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_isvalid.c b/libft_personal/tests/ft_string/ft_str/test_str_isvalid.c new file mode 100644 index 0000000..aaadd32 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_isvalid.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_isvalid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_replace.c b/libft_personal/tests/ft_string/ft_str/test_str_replace.c new file mode 100644 index 0000000..63706a4 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_replace.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_str_replace_chr.c b/libft_personal/tests/ft_string/ft_str/test_str_replace_chr.c new file mode 100644 index 0000000..a0a26da --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_str_replace_chr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_str_replace_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strappend_c.c b/libft_personal/tests/ft_string/ft_str/test_strappend_c.c new file mode 100644 index 0000000..0235755 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strappend_c.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strappend_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strchr.c b/libft_personal/tests/ft_string/ft_str/test_strchr.c new file mode 100644 index 0000000..cb74fdc --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strchr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strclen.c b/libft_personal/tests/ft_string/ft_str/test_strclen.c new file mode 100644 index 0000000..12c95b0 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strclen.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strclen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strcmp.c b/libft_personal/tests/ft_string/ft_str/test_strcmp.c new file mode 100644 index 0000000..50551af --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strcmp.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strcnb.c b/libft_personal/tests/ft_string/ft_str/test_strcnb.c new file mode 100644 index 0000000..a2a7b69 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strcnb.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strcnb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 10:47:58 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:06:11 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strcspn.c b/libft_personal/tests/ft_string/ft_str/test_strcspn.c new file mode 100644 index 0000000..0d3d359 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strcspn.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strcspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strdup.c b/libft_personal/tests/ft_string/ft_str/test_strdup.c new file mode 100644 index 0000000..ced528f --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strend_with.c b/libft_personal/tests/ft_string/ft_str/test_strend_with.c new file mode 100644 index 0000000..5494cfa --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strend_with.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strend_with.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_striteri.c b/libft_personal/tests/ft_string/ft_str/test_striteri.c new file mode 100644 index 0000000..927c843 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_striteri.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strjoin.c b/libft_personal/tests/ft_string/ft_str/test_strjoin.c new file mode 100644 index 0000000..ddd9372 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strjoin.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:11:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:10:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strlcat.c b/libft_personal/tests/ft_string/ft_str/test_strlcat.c new file mode 100644 index 0000000..90d9454 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strlcat.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:11:01 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:24:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strlcpy.c b/libft_personal/tests/ft_string/ft_str/test_strlcpy.c new file mode 100644 index 0000000..a8d70ed --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strlcpy.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:10:11 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 09:10:36 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strlen.c b/libft_personal/tests/ft_string/ft_str/test_strlen.c new file mode 100644 index 0000000..d3373e8 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strlen.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strmapi.c b/libft_personal/tests/ft_string/ft_str/test_strmapi.c new file mode 100644 index 0000000..07642ed --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strmapi.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:10:32 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:40:48 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strncmp.c b/libft_personal/tests/ft_string/ft_str/test_strncmp.c new file mode 100644 index 0000000..f7d036b --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strncmp.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strndup.c b/libft_personal/tests/ft_string/ft_str/test_strndup.c new file mode 100644 index 0000000..65fbaf2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strndup.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strnstr.c b/libft_personal/tests/ft_string/ft_str/test_strnstr.c new file mode 100644 index 0000000..b2bdb6f --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:24:10 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 08:44:18 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strrchr.c b/libft_personal/tests/ft_string/ft_str/test_strrchr.c new file mode 100644 index 0000000..149647e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strrchr.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:41:05 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:14:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strspn.c b/libft_personal/tests/ft_string/ft_str/test_strspn.c new file mode 100644 index 0000000..fa673bc --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strspn.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strspn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strstart_with.c b/libft_personal/tests/ft_string/ft_str/test_strstart_with.c new file mode 100644 index 0000000..fef0f33 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strstart_with.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strstart_with.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strtok.c b/libft_personal/tests/ft_string/ft_str/test_strtok.c new file mode 100644 index 0000000..a7709ed --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strtok.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strtok.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_strtrim.c b/libft_personal/tests/ft_string/ft_str/test_strtrim.c new file mode 100644 index 0000000..33334c1 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_strtrim.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_substr.c b/libft_personal/tests/ft_string/ft_str/test_substr.c new file mode 100644 index 0000000..8468b92 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_substr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/libft_personal/tests/ft_string/ft_str/test_utoa.c b/libft_personal/tests/ft_string/ft_str/test_utoa.c new file mode 100644 index 0000000..3e86956 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_str/test_utoa.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_utoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/26 11:17:28 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:39:44 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include + +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); +} diff --git a/libft_personal/tests/ft_string/ft_string/t_string_tests.c b/libft_personal/tests/ft_string/ft_string/t_string_tests.c new file mode 100644 index 0000000..cf9dd6e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/t_string_tests.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* t_string_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:39:25 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:36:14 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/str__t_str_test.h" +#include "tests/tests.h" + +static const t_test *loadtests01(void) +{ + static const t_test tests[] = { + {"append_c", test_string_append_c}, {"append_n", test_string_append_n}, + {"append_sn", test_string_append_sn}, {"append_s", test_string_append_s}, + {"append", test_string_append}, {"cap", test_string_cap}, + {"chr", test_string_chr}, {"clear", test_string_clear}, + {"cmp_str", test_string_cmp_str}, {"cmp", test_string_cmp}, + {"destroy", test_string_destroy}, {"from_c", test_string_from_c}, + {"from_n", test_string_from_n}, {"from_sn", test_string_from_sn}, + {"from_s", test_string_from_s}, {"from", test_string_from}, + {"get", test_string_get}, {"insert_c", test_string_insert_c}, + {"insert_n", test_string_insert_n}, {"insert_sn", test_string_insert_sn}, + {"insert_s", test_string_insert_s}, {"insert", test_string_insert}, + {"length", test_string_len}, {"ncmp_str", test_string_ncmp_str}, + {"ncmp", test_string_ncmp}, {"new", test_string_new}, + {"offset", test_string_offset}, {"put", test_string_put}, + {"rchr", test_string_rchr}, {"replace_c", test_string_replace_chr}, + {"replace", test_string_replace}, {"reserve", test_string_reserve}, + {"resize", test_string_resize}, {"roffset", test_string_roffset}, + {"set", test_string_set}, {"set_n", test_string_set_n}, + {"set_inplace", test_string_set_inplace}, {"shrink", test_string_shrink}, + {"substr", test_string_substr}, {"to_str", test_string_to_str}, + {"trim_chr", test_string_trim_chr}, {"trimstr", test_string_trimstr}, + {"trim", test_string_trim}, {NULL, NULL}}; + + return (tests); +} + +static const t_test *loadtests02(void) +{ + return (NULL); +} + +int t_string_tests(void) +{ + int sum; + const t_test *tests = loadtests01(); + const t_test *tests02 = loadtests02(); + + sum = 0; + (void)tests02; + run_test(tests, &sum); + return (sum); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_append.c b/libft_personal/tests/ft_string/ft_string/test_append.c new file mode 100644 index 0000000..e019d17 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_append.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_append.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:24:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_append(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_append(str, " World"); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_destroy(&str); + str = ft_string_from("hi"); + ft_string_append(str, "i"); + if (ft_string_cmp(str, "hii") != 0) + return (3); + if (str->length != 3 || str->capacity < 3) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_append_c.c b/libft_personal/tests/ft_string/ft_string/test_append_c.c new file mode 100644 index 0000000..183f1c4 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_append_c.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_append_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:32 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:26:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +// second test for ft_string_append_c is to check in case buffer is greater +// than current lenght that char will be indeed appended +int test_string_append_c(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_append_c(str, ' '); + if (ft_string_cmp(str, "Hello ") != 0) + return (1); + if (str->length != 6 || str->capacity < 6) + return (2); + ft_string_destroy(&str); + str = ft_string_from("hi"); + ft_string_append_c(str, 'i'); + if (ft_string_cmp(str, "hii") != 0) + return (3); + if (str->length != 3 || str->capacity < 3) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_append_n.c b/libft_personal/tests/ft_string/ft_string/test_append_n.c new file mode 100644 index 0000000..b26d595 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_append_n.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_append_n.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:05 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:27:16 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_append_n(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_append_n(str, " World", 5); + if (ft_string_cmp(str, "Hello Worl") != 0) + return (1); + if (str->length != 10) + return (2); + if (str->capacity < 10) + return (3); + ft_string_destroy(&str); + str = ft_string_from("hi"); + ft_string_append_n(str, "i", 1); + if (ft_string_cmp(str, "hii") != 0) + return (4); + if (str->length != 3 || str->capacity < 3) + return (5); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_append_s.c b/libft_personal/tests/ft_string/ft_string/test_append_s.c new file mode 100644 index 0000000..071c7c2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_append_s.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_append_s.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:56:56 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:27:43 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_append_s(void) +{ + t_string *str; + t_string *str2; + + str = ft_string_from("Hello"); + str2 = ft_string_from(" World"); + ft_string_append_s(str, str2); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11) + return (2); + if (str->capacity < 11) + return (3); + ft_string_destroy(&str); + ft_string_destroy(&str2); + str = ft_string_from("hi"); + str2 = ft_string_from("i"); + ft_string_append_s(str, str2); + if (ft_string_cmp(str, "hii") != 0) + return (4); + if (str->length != 3 || str->capacity < 3) + return (5); + return (ft_string_destroy(&str), ft_string_destroy(&str2), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_append_sn.c b/libft_personal/tests/ft_string/ft_string/test_append_sn.c new file mode 100644 index 0000000..19c3b95 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_append_sn.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_append_sn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:55:52 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:30:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_append_sn(void) +{ + t_string *str; + t_string *str2; + + str = ft_string_from("Hello"); + str2 = ft_string_from(" World!!!!"); + ft_string_append_s_n(str, str2, 6); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_destroy(&str); + ft_string_destroy(&str2); + str = ft_string_from("hi"); + str2 = ft_string_from("ii"); + ft_string_append_s_n(str, str2, 1); + if (ft_string_cmp(str, "hii") != 0) + return (3); + if (str->length != 3 || str->capacity < 3) + return (4); + return (ft_string_destroy(&str), ft_string_destroy(&str2), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_cap.c b/libft_personal/tests/ft_string/ft_string/test_cap.c new file mode 100644 index 0000000..50eed55 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_cap.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_cap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_cap(void) +{ + t_string *str; + + str = ft_string_from("Hello world this is zod!"); + if (str->capacity != ft_string_cap(str)) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_chr.c b/libft_personal/tests/ft_string/ft_string/test_chr.c new file mode 100644 index 0000000..1a31d92 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_chr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:35:34 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_chr(void) +{ + t_string *str; + const char c[] = "z!H@\0"; + char *ptr_exp[sizeof(c) / sizeof(c[0])]; + size_t i; + + str = ft_string_from("Hello world this is zod!"); + i = 0; + while (i < sizeof(c) / sizeof(c[0])) + { + ptr_exp[i] = ft_strchr(str->str, c[i]); + if (ft_string_chr(str, c[i]) != ptr_exp[i]) + return (i + 1); + i++; + } + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_clear.c b/libft_personal/tests/ft_string/ft_string/test_clear.c new file mode 100644 index 0000000..aa34cff --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_clear.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_clear(void) +{ + t_string *str; + size_t capacity; + + str = ft_string_from("Hello"); + capacity = str->capacity; + ft_string_clear(str); + if (ft_string_cmp(str, "") != 0) + return (1); + if (str->length != 0) + return (2); + if (str->capacity != capacity) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_cmp.c b/libft_personal/tests/ft_string/ft_string/test_cmp.c new file mode 100644 index 0000000..56ce46e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_cmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_cmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:20:23 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_cmp(void) +{ + t_string *str; + + str = ft_string_from("Hello World"); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (ft_string_cmp(str, "Hello Warld!") != 'o' - 'a') + return (2); + if (ft_string_cmp(str, "Hello Wprld!") != 'o' - 'p') + return (3); + if (ft_string_cmp(str, "Hello World!") != '\0' - '!') + return (4); + if (ft_string_cmp(str, "Hell") != 'o' - '\0') + return (5); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_cmp_str.c b/libft_personal/tests/ft_string/ft_string/test_cmp_str.c new file mode 100644 index 0000000..c7c1d4d --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_cmp_str.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_cmp_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 11:01:15 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_cmp_str(void) +{ + t_string *str; + t_string *str2; + + str = ft_string_from("Hello"); + str2 = ft_string_from("Hello"); + if (ft_string_cmpstr(str, str2) != 0) + return (1); + ft_string_destroy(&str2); + str2 = ft_string_from("Hello World"); + if (ft_string_cmpstr(str, str2) >= 0) + return (2); + ft_string_destroy(&str2); + str2 = ft_string_from("Hell"); + if (ft_string_cmpstr(str, str2) <= 0) + return (3); + ft_string_destroy(&str); + ft_string_destroy(&str2); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_destroy.c b/libft_personal/tests/ft_string/ft_string/test_destroy.c new file mode 100644 index 0000000..6240a5a --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_destroy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/25 17:37:36 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +/* + ft_string_destroy(&str); // null resiliancy + ft_string_destroy(NULL); // null resiliancy +*/ +int test_string_destroy(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_destroy(&str); + if (str) + return (1); + ft_string_destroy(&str); + ft_string_destroy(NULL); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_from.c b/libft_personal/tests/ft_string/ft_string/test_from.c new file mode 100644 index 0000000..81e968d --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_from.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_from.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:35:08 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_from(void) +{ + t_string *str; + + str = ft_string_from("Hello World"); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_destroy(&str); + str = ft_string_from(NULL); + if (str->length != 0 || str->capacity < 1) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_from_c.c b/libft_personal/tests/ft_string/ft_string/test_from_c.c new file mode 100644 index 0000000..02494d3 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_from_c.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_from_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_from_c(void) +{ + t_string *str; + + str = ft_string_from_c('c'); + if (ft_string_cmp(str, "c") != 0) + return (1); + if (str->length != 1) + return (2); + if (str->capacity < 1) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_from_n.c b/libft_personal/tests/ft_string/ft_string/test_from_n.c new file mode 100644 index 0000000..4fbb1bd --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_from_n.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_from_n.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:36:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_from_n(void) +{ + t_string *str; + + str = ft_string_from_n("Hello World", 5); + if (ft_string_cmp(str, "Hello") != 0) + return (1); + if (str->length != 5 || str->capacity < 5) + return (2); + ft_string_destroy(&str); + str = ft_string_from_n(NULL, 5); + if (str->length != 0 || str->capacity < 1) + return (3); + ft_string_destroy(&str); + str = ft_string_from_n("Hello World", 0); + if (str->length != 0 || str->capacity < 1) + return (4); + return (ft_string_destroy(&str), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_from_s.c b/libft_personal/tests/ft_string/ft_string/test_from_s.c new file mode 100644 index 0000000..e64a447 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_from_s.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_from_s.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:38:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_from_s(void) +{ + t_string *str; + t_string *str_src; + + str_src = ft_string_from("Hello World"); + str = ft_string_from_s(str_src); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_destroy(&str); + str = ft_string_from_s(NULL); + if (str->length != 0 || str->capacity < 1) + return (3); + return (ft_string_destroy(&str), ft_string_destroy(&str_src), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_from_sn.c b/libft_personal/tests/ft_string/ft_string/test_from_sn.c new file mode 100644 index 0000000..d957359 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_from_sn.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_from_sn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:41:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_from_sn(void) +{ + t_string *str; + t_string *str_src; + + str_src = ft_string_from("Hello World"); + str = ft_string_from_s_n(str_src, 5); + if (ft_string_cmp(str, "Hello") != 0) + return (1); + if (str->length != 5) + return (2); + if (str->capacity < 5) + return (3); + ft_string_destroy(&str); + str = ft_string_from_s_n(NULL, 5); + if (str->length != 0 || str->capacity < 1) + return (4); + ft_string_destroy(&str); + str = ft_string_from_s_n(str_src, 0); + if (str->length != 0 || str->capacity < 1) + return (5); + return (ft_string_destroy(&str), ft_string_destroy(&str_src), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_get.c b/libft_personal/tests/ft_string/ft_string/test_get.c new file mode 100644 index 0000000..a24f85e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_get.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 06:21:54 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_get(void) +{ + t_string *str; + const char *c_str; + + str = ft_string_from("Hello worlds!"); + c_str = ft_string_get(str); + if (ft_strncmp(str->str, c_str, str->length) != 0) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_insert.c b/libft_personal/tests/ft_string/ft_string/test_insert.c new file mode 100644 index 0000000..9cdecc2 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_insert.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:14:37 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_insert(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_insert(str, " World", 0); + if (ft_string_cmp(str, " WorldHello") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_insert(str, "!", 99); + if (ft_string_cmp(str, " WorldHello!") != 0) + return (3); + if (str->length != 12 || str->capacity < 12) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_insert_c.c b/libft_personal/tests/ft_string/ft_string/test_insert_c.c new file mode 100644 index 0000000..9e956eb --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_insert_c.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_insert_c.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:15:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_insert_c(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_insert_c(str, ' ', 0); + if (ft_string_cmp(str, " Hello") != 0) + return (1); + if (str->length != 6 || str->capacity < 6) + return (2); + ft_string_insert_c(str, '!', 99); + if (ft_string_cmp(str, " Hello!") != 0) + return (3); + if (str->length != 7 || str->capacity < 7) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_insert_n.c b/libft_personal/tests/ft_string/ft_string/test_insert_n.c new file mode 100644 index 0000000..2844b1a --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_insert_n.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_insert_n.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:16:00 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_insert_n(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_insert_n(str, " World", 0, 5); + if (ft_string_cmp(str, " WorlHello") != 0) + return (1); + if (str->length != 10 || str->capacity < 10) + return (2); + ft_string_insert_n(str, "!", 99, 1); + if (ft_string_cmp(str, " WorlHello!") != 0) + return (3); + if (str->length != 11 || str->capacity < 11) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_insert_s.c b/libft_personal/tests/ft_string/ft_string/test_insert_s.c new file mode 100644 index 0000000..772b62f --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_insert_s.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_insert_s.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:18:17 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_insert_s(void) +{ + t_string *str; + t_string *str2; + + str = ft_string_from("Hello"); + str2 = ft_string_from(" World"); + ft_string_insert_s(str, str2, 0); + if (ft_string_cmp(str, " WorldHello") != 0) + return (1); + if (str->length != 11 || str->capacity < 11) + return (2); + ft_string_insert_s(str, str2, 99); + if (ft_string_cmp(str, " WorldHello World") != 0) + return (3); + if (str->length != 17 || str->capacity < 17) + return (4); + ft_string_destroy(&str); + ft_string_destroy(&str2); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_insert_sn.c b/libft_personal/tests/ft_string/ft_string/test_insert_sn.c new file mode 100644 index 0000000..4c203af --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_insert_sn.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_insert_sn.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:21:14 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_insert_sn(void) +{ + t_string *str; + t_string *str2; + + str = ft_string_from("Hello"); + str2 = ft_string_from(" World!!!!"); + ft_string_insert_s_n(str, str2, 0, 5); + if (ft_string_cmp(str, " WorlHello") != 0) + return (1); + if (str->length != 10 || str->capacity < 10) + return (2); + ft_string_insert_s_n(str, str2, 99, 1); + if (ft_string_cmp(str, " WorlHello ") != 0) + return (3); + if (str->length != 11 || str->capacity < 11) + return (4); + ft_string_destroy(&str); + ft_string_destroy(&str2); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_len.c b/libft_personal/tests/ft_string/ft_string/test_len.c new file mode 100644 index 0000000..26a0a95 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_len.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 06:21:27 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_len(void) +{ + t_string *str; + + str = ft_string_from("Hello worlds!"); + if (ft_string_len(str) != 13) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_ncmp.c b/libft_personal/tests/ft_string/ft_string/test_ncmp.c new file mode 100644 index 0000000..fd4528e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_ncmp.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:23:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_ncmp(void) +{ + t_string *str; + + str = ft_string_from("Hello World"); + if (ft_string_ncmp(str, "Hello World", 11) != 0) + return (1); + if (ft_string_ncmp(str, "Hello_foobar", 5) != 0) + return (2); + if (ft_string_ncmp(str, "Hello World!", 12) == 0) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_ncmp_str.c b/libft_personal/tests/ft_string/ft_string/test_ncmp_str.c new file mode 100644 index 0000000..839ea7e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_ncmp_str.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_ncmp_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/23 18:29:22 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_ncmp_str(void) +{ + t_string *str; + t_string *str2; + t_string *str3; + t_string *str4; + + str = ft_string_from("Hello World"); + str2 = ft_string_from("Hello Worle"); + str3 = ft_string_from("Hello Worla"); + str4 = ft_string_from("Hello"); + if (ft_string_ncmpstr(str, str2, 10) != 0 || \ + ft_string_ncmpstr(str, str3, 10) != 0) + return (1); + if (ft_string_ncmpstr(str, str2, 11) == 0 || \ + ft_string_ncmpstr(str, str3, 11) == 0 || \ + ft_string_ncmpstr(str3, str, 11) == 0) + return (2); + if (ft_string_ncmpstr(str2, str, 99) == 0 || \ + ft_string_ncmpstr(str3, str4, 9) == 0) + return (6); + return (ft_string_destroy(&str), ft_string_destroy(&str2), + ft_string_destroy(&str3), ft_string_destroy(&str4), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_new.c b/libft_personal/tests/ft_string/ft_string/test_new.c new file mode 100644 index 0000000..9580e2d --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_new.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 19:34:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_new(void) +{ + t_string *str; + + str = ft_string_new(42); + if (!str->str || str->length != 0 || str->capacity < 42) + return (1); + ft_string_destroy(&str); + str = ft_string_new(0); + if (!str->str || str->length != 0 || str->capacity < 1) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_offset.c b/libft_personal/tests/ft_string/ft_string/test_offset.c new file mode 100644 index 0000000..fb4a119 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_offset.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_offset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 13:31:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_offset(void) +{ + t_string *str; + const char *src; + char c; + + src = "Hello world this is zod!"; + str = ft_string_from(src); + c = 'z'; + if (ft_string_offset(str, c) != 20) + return (1); + c = '!'; + if (ft_string_offset(str, c) != 23) + return (2); + c = '@'; + if (ft_string_offset(str, c) != -1) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_put.c b/libft_personal/tests/ft_string/ft_string/test_put.c new file mode 100644 index 0000000..653d874 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_put.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 12:45:09 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:27:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/tests.h" + +static int normal_cases(void) +{ + t_string *str; + const char *src; + char *res; + int fd; + const char *file = TESTS_FPREFIX "test_string_put.txt"; + + src = "Hello world this is zod!"; + str = ft_string_from(src); + fd = open(file, O_CREAT | O_RDWR | O_TRUNC, 0644); + if (fd == -1) + return (1); + ft_string_put(str, fd); + close(fd); + fd = open(file, O_RDONLY); + if (fd == -1) + return (2); + res = ft_fd_to_buff(fd); + destroy_test_file(fd, file); + if (ft_strcmp(src, res) != 0) + return (3); + return (free(res), ft_string_destroy(&str), 0); +} + +static int error_case(void) +{ + t_string *str; + const int fd = -1; + + str = ft_string_from("Hello"); + if (ft_string_put(str, fd) != -1) + return (1); + if (ft_string_put(NULL, STDOUT_FILENO) != -1) + return (2); + ft_free((void **)&str->str); + if (ft_string_put(str, STDOUT_FILENO) != -1) + return (3); + str->str = NULL; + str->length = 0; + if (ft_string_put(str, STDOUT_FILENO) != 0) + return (4); + return (ft_string_destroy(&str), 0); +} + +int test_string_put(void) +{ + int ret; + + ret = normal_cases(); + if (ret) + return (ret); + ret = error_case(); + if (ret) + return (ret + 10); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_rchr.c b/libft_personal/tests/ft_string/ft_string/test_rchr.c new file mode 100644 index 0000000..6c15803 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_rchr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_rchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 10:37:31 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_rchr(void) +{ + t_string *str; + const char c[] = "z!Hol@\0"; + char *ptr_exp[sizeof(c) / sizeof(c[0])]; + size_t i; + + str = ft_string_from("Hello world this is zod!"); + i = 0; + while (i < sizeof(c) / sizeof(c[0])) + { + ptr_exp[i] = ft_strrchr(str->str, c[i]); + if (ft_string_rchr(str, c[i]) != ptr_exp[i]) + return (i + 1); + i++; + } + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_replace.c b/libft_personal/tests/ft_string/ft_string/test_replace.c new file mode 100644 index 0000000..c11a9ba --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_replace.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 06:21:43 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_replace(void) +{ + t_string *str; + + str = ft_string_from("Hello worlds!"); + ft_string_replace(str, "worlds", "Worlds"); + if (ft_string_cmp(str, "Hello Worlds!")) + return (1); + ft_string_append(str, " Worlds!"); + ft_string_replace(str, "Worlds", "earth"); + if (ft_string_cmp(str, "Hello earth! earth!")) + return (2); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_replace_chr.c b/libft_personal/tests/ft_string/ft_string/test_replace_chr.c new file mode 100644 index 0000000..ead5dd8 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_replace_chr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_replace_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 06:21:48 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_replace_chr(void) +{ + t_string *str; + + str = ft_string_from("Hello worlds!"); + ft_string_replace_chr(str, 'o', 'O'); + if (ft_string_cmp(str, "HellO wOrlds!")) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_reserve.c b/libft_personal/tests/ft_string/ft_string/test_reserve.c new file mode 100644 index 0000000..43de776 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_reserve.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:36:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_reserve(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_reserve(str, 42); + if (ft_string_cmp(str, "Hello") != 0) + return (1); + if (str->length != 5 || str->capacity < 42) + return (2); + ft_string_reserve(str, 10); + if (ft_string_cmp(str, "Hello") != 0) + return (3); + if (str->length != 5 || str->capacity < 42) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_resize.c b/libft_personal/tests/ft_string/ft_string/test_resize.c new file mode 100644 index 0000000..7472d47 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_resize.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_resize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_resize(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_resize(str, 42); + if (ft_string_cmp(str, "Hello") != 0) + return (1); + if (str->length != 5) + return (2); + if (str->capacity < 42) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_roffset.c b/libft_personal/tests/ft_string/ft_string/test_roffset.c new file mode 100644 index 0000000..fb6f672 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_roffset.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_roffset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:57:50 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 13:28:27 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_roffset(void) +{ + t_string *str; + const char *src; + char c; + + src = "Hello world this is zod!"; + str = ft_string_from(src); + c = 'z'; + if (ft_string_roffset(str, c) != 20) + return (1); + c = '!'; + if (ft_string_roffset(str, c) != 23) + return (2); + c = '@'; + if (ft_string_roffset(str, c) != -1) + return (4); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_set.c b/libft_personal/tests/ft_string/ft_string/test_set.c new file mode 100644 index 0000000..0685507 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_set.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 12:50:00 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:39:55 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_string_set(void) +{ + t_string *str; + const char *src; + const char *res; + + src = "Hello world this is zod!"; + str = ft_string_new(0); + ft_string_set(str, src); + res = ft_string_get(str); + if (ft_strcmp(src, res) != 0) + return (1); + ft_string_destroy(&str); + str = ft_string_new(99); + ft_string_set(str, src); + res = ft_string_get(str); + if (ft_strcmp(src, res) != 0) + return (2); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_set_inplace.c b/libft_personal/tests/ft_string/ft_string/test_set_inplace.c new file mode 100644 index 0000000..abfa6c8 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_set_inplace.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_set_inplace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 12:56:27 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:43:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_string_set_inplace(void) +{ + t_string *str; + char *src; + const char *res; + + src = ft_strdup("Hello world this is zod!"); + str = ft_string_new(0); + ft_string_set_inplace(str, src); + res = ft_string_get(str); + if (ft_strcmp(src, res) != 0) + return (1); + src = ft_strdup("Hello world this is zod!"); + free(str->str); + str->str = NULL; + ft_string_set_inplace(str, src); + res = ft_string_get(str); + if (ft_strcmp(src, res) != 0) + return (2); + return (ft_string_destroy(&str), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_set_n.c b/libft_personal/tests/ft_string/ft_string/test_set_n.c new file mode 100644 index 0000000..339b8c3 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_set_n.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_set_n.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/31 12:57:59 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 13:12:45 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" + +int test_string_set_n(void) +{ + t_string *str; + const char *src; + const char *res; + + src = "Hello world this is zod!"; + str = ft_string_new(0); + ft_string_set_n(str, src, 6); + res = ft_string_get(str); + if (ft_strcmp("Hello", res) != 0) + return (1); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_shrink.c b/libft_personal/tests/ft_string/ft_string/test_shrink.c new file mode 100644 index 0000000..a81884e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_shrink.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_shrink.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/06/03 10:56:52 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_shrink(void) +{ + t_string *str; + + str = ft_string_from("Hello"); + ft_string_shrink(str); + if (ft_string_cmp(str, "Hello") != 0) + return (1); + if (str->length != 5 || str->capacity < 5) + return (2); + ft_string_destroy(&str); + str = ft_string_from("^_^"); + if (str->length != 3 || str->capacity != T_STRING_BUFF) + return (3); + ft_string_shrink(str); + if (ft_string_cmp(str, "^_^") != 0) + return (4); + if (str->length != 3 || str->capacity < 3) + return (5); + ft_string_shrink(str); + if (ft_string_cmp(str, "^_^") != 0) + return (6); + if (str->length != 3 || str->capacity < 3) + return (7); + return (ft_string_destroy(&str), 0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_substr.c b/libft_personal/tests/ft_string/ft_string/test_substr.c new file mode 100644 index 0000000..a34ea77 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_substr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:16:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_substr(void) +{ + t_string *str; + t_string *sub; + + str = ft_string_from("Hello World"); + sub = ft_string_substr(str, 0, 6); + if (!sub) + return (1); + if (ft_strcmp(sub->str, "Hello") != 0) + return (2); + if (sub->length != 5) + return (3); + if (sub->capacity < 5) + return (4); + ft_strlen(sub->str); + ft_string_destroy(&str); + ft_string_destroy(&sub); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_to_str.c b/libft_personal/tests/ft_string/ft_string/test_to_str.c new file mode 100644 index 0000000..7964ca0 --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_to_str.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test_to_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:16:39 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_to_str(void) +{ + t_string *str; + char *cstr; + + str = ft_string_from("Hello World"); + cstr = ft_string_to_str(str); + if (ft_strcmp(cstr, "Hello World") != 0) + return (1); + ft_string_destroy(&str); + free(cstr); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_trim.c b/libft_personal/tests/ft_string/ft_string/test_trim.c new file mode 100644 index 0000000..d099a9e --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_trim.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_trim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_trim(void) +{ + t_string *str; + + str = ft_string_from(" Hello World "); + ft_string_trim(str); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11) + return (2); + if (str->capacity < 11) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_trim_chr.c b/libft_personal/tests/ft_string/ft_string/test_trim_chr.c new file mode 100644 index 0000000..1f12dad --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_trim_chr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_trim_chr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_trim_chr(void) +{ + t_string *str; + + str = ft_string_from(" Hello World "); + ft_string_trim_chr(str, ' '); + if (ft_string_cmp(str, "Hello World") != 0) + return (1); + if (str->length != 11) + return (2); + if (str->capacity < 11) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/ft_string/test_trimstr.c b/libft_personal/tests/ft_string/ft_string/test_trimstr.c new file mode 100644 index 0000000..fa7b2fe --- /dev/null +++ b/libft_personal/tests/ft_string/ft_string/test_trimstr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_t_str_trimstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 17:34:36 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 12:21:38 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_string_types.h" + +int test_string_trimstr(void) +{ + t_string *str; + + str = ft_string_from(" Hello World "); + ft_string_trimstr(str, " d"); + if (ft_string_cmp(str, "Hello Worl") != 0) + return (1); + if (str->length != 10) + return (2); + if (str->capacity < 10) + return (3); + ft_string_destroy(&str); + return (0); +} diff --git a/libft_personal/tests/ft_string/string_tests.c b/libft_personal/tests/ft_string/string_tests.c new file mode 100644 index 0000000..a2d05b0 --- /dev/null +++ b/libft_personal/tests/ft_string/string_tests.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 13:36:16 by bgoulard #+# #+# */ +/* Updated: 2024/06/26 19:26:02 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/tests.h" + +int t_string_tests(void); +int mem_tests(void); +int str_tests(void); +int char_tests(void); + +static const t_test *load_submodules_tests(void) +{ + static const t_test tests[] = { + {"mem", mem_tests}, + {"str", str_tests}, + {"char", char_tests}, + {"t_string", t_string_tests}, + {NULL, NULL} + }; + + return (tests); +} + +int tests_string(void) +{ + size_t i; + int collect; + int prev; + const t_test *tests = load_submodules_tests(); + + i = 0; + collect = 0; + while (tests[i].name) + { + prev = collect; + ft_putendl_fd("\nTesting::", STDOUT_FILENO); + ft_putendl_fd(tests[i].name, STDOUT_FILENO); + collect += tests[i++].test(); + ft_putstr_fd("\nTesting sub-module ", STDOUT_FILENO); + ft_putstr_fd(tests[i - 1].name, STDOUT_FILENO); + if (prev != collect) + ft_putendl_fd(" \033[31mKO\033[0m", STDOUT_FILENO); + else + ft_putendl_fd(" \033[32mOK\033[0m", STDOUT_FILENO); + } + return (collect); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_add.c b/libft_personal/tests/ft_vector/tests_vec_add.c new file mode 100644 index 0000000..4b499ed --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_add.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_add.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:13:02 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:13:07 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_add(void) +{ + t_vector *vec; + + vec = ft_vec_new(); + ft_vec_add(&vec, (void *)42); + if (vec->count != 1) + return (1); + if (vec->datas[0] != (void *)42) + return (1); + ft_vec_add(&vec, (void *)43); + ft_vec_add(&vec, (void *)44); + ft_vec_add(&vec, (void *)45); + ft_vec_add(&vec, (void *)46); + ft_vec_add(&vec, (void *)47); + if (vec->count != 6) + return (1); + if (vec->datas[0] != (void *)42 || vec->datas[1] != (void *)43 || \ + vec->datas[2] != (void *)44 || vec->datas[3] != (void *)45 || \ + vec->datas[4] != (void *)46 || vec->datas[5] != (void *)47) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_apply.c b/libft_personal/tests/ft_vector/tests_vec_apply.c new file mode 100644 index 0000000..36adc99 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_apply.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_apply.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:13:11 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 17:13:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "tests/tests_lambda_functions.h" + +int test_vec_apply(void) +{ + t_vector *vec; + int i; + + i = 0; + vec = ft_vec_new(); + ft_vec_add(&vec, &i); + ft_vec_apply(vec, add42); + if (*(int *)vec->datas[0] != 42) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_at.c b/libft_personal/tests/ft_vector/tests_vec_at.c new file mode 100644 index 0000000..2200f4d --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_at.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:22:41 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 20:44:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_at(void) +{ + t_vector *vec; + int a; + int b; + int c; + + a = 0; + b = 1; + c = 2; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + if (*(int *)ft_vec_at(vec, 0) != 0) + return (1); + else if (*(int *)ft_vec_at(vec, 1) != 1) + return (1); + else if (*(int *)ft_vec_at(vec, 2) != 2) + return (1); + else if (ft_vec_at(vec, 3)) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_cat.c b/libft_personal/tests/ft_vector/tests_vec_cat.c new file mode 100644 index 0000000..48cfa63 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_cat.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_cat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:15:21 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:11:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include + +/* + ft_vec_add(&vec_a, &a); // 42 + ft_vec_add(&vec_a, &b); // 42 43 + ft_vec_add(&vec_a, &c); // 42 43 44 + ft_vec_add(&vec_b, &d); // 45 + ft_vec_add(&vec_b, &e); // 45 46 + ft_vec_add(&vec_b, &f); // 45 46 47 + ret = ft_vec_cat(&vec_a, vec_b); // 42 43 44 + 45 46 47 + ft_vec_add(&vec_a, &a); // 42 + ft_vec_add(&vec_a, &b); // 42 43 + ft_vec_add(&vec_a, &c); // 42 43 44 + ret = ft_vec_cat(&vec_a, vec_b); // 42 43 44 + 45 46 47 +*/ + +void init_vec_cat(t_vector **vec_a, t_vector **vec_b) +{ + *vec_a = ft_vec_from_size(6); + ft_vec_add(vec_a, (void *)42); + ft_vec_add(vec_a, (void *)43); + ft_vec_add(vec_a, (void *)44); + *vec_b = ft_vec_new(); + ft_vec_add(vec_b, (void *)45); + ft_vec_add(vec_b, (void *)46); + ft_vec_add(vec_b, (void *)47); +} + +int test_vec_cat(void) +{ + bool ret; + t_vector *vec_a; + t_vector *vec_b; + + init_vec_cat(&vec_a, &vec_b); + ret = ft_vec_cat(&vec_a, vec_b); + if (ret != true || vec_a->count != 6) + return (1); + else if (ft_vec_at(vec_a, 0) != (void *)42 || ft_vec_at(vec_a, 1) != \ + (void *)43 || ft_vec_at(vec_a, 2) != (void *)44 || ft_vec_at(vec_a, 3) != \ + (void *)45 || ft_vec_at(vec_a, 4) != (void *)46 || ft_vec_at(vec_a, 5) != \ + (void *)47) + return (2); + ft_vec_destroy(&vec_a); + vec_a = ft_vec_new(); + ft_vec_add(&vec_a, (void *)42); + ft_vec_add(&vec_a, (void *)43); + ft_vec_add(&vec_a, (void *)44); + ret = ft_vec_cat(&vec_a, vec_b); + if (ret != false || vec_a->count != 3) + return (3); + else if (ft_vec_at(vec_a, 0) != (void *)42 || ft_vec_at(vec_a, 1) != \ + (void *)43 || ft_vec_at(vec_a, 2) != (void *)44) + return (4); + return (ft_vec_destroy(&vec_a), ft_vec_destroy(&vec_b), 0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_clear.c b/libft_personal/tests/ft_vector/tests_vec_clear.c new file mode 100644 index 0000000..7f9592f --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_clear.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_clear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:21:55 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:05:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_clear(void) +{ + t_vector *vec; + int a; + int b; + int c; + + a = 0; + b = 1; + c = 2; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + ft_vec_clear(vec); + if (vec->count != 0) + return (1); + else if (vec->cappacity == 0) + return (1); + else if (vec->datas[0]) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_convert_alloc_array.c b/libft_personal/tests/ft_vector/tests_vec_convert_alloc_array.c new file mode 100644 index 0000000..ad7c6be --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_convert_alloc_array.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_convert_alloc_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:32:32 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include + +// ft_vec_destroy(&vec); + // free(data); -> segfault : double free or corruption. + // ft_vec_convert_alloccarray takes ownership of the data. refert to the + // doc. + +int test_vec_convert_alloc_array(void) +{ + void **data; + t_vector *vec; + int arr[3]; + + arr[0] = 42; + arr[1] = 43; + arr[2] = 44; + data = malloc(sizeof(void *) * 3); + data[0] = (void *)&arr[0]; + data[1] = (void *)&arr[1]; + data[2] = (void *)&arr[2]; + vec = ft_vec_convert_alloccarray(data, 3); + if (vec->count != 3 || vec->cappacity != 3 || vec->datas != data) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 42 || *(int *)ft_vec_at(vec, 1) != \ + 43 || *(int *)ft_vec_at(vec, 2) != 44) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_destroy.c b/libft_personal/tests/ft_vector/tests_vec_destroy.c new file mode 100644 index 0000000..3533f1f --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_destroy.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:20:58 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:20:59 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_destroy(void) +{ + t_vector *vec; + int a; + int b; + int c; + + a = 0; + b = 1; + c = 2; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + ft_vec_destroy(&vec); + if (vec) + return (1); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_filter.c b/libft_personal/tests/ft_vector/tests_vec_filter.c new file mode 100644 index 0000000..2809f41 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_filter.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_filter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:20:40 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:29:04 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "tests/tests_lambda_functions.h" +#include + +int test_vec_filter(void) +{ + const int arr[3] = {21, 42, 63}; + t_vector *vec; + int *pp[3]; + + vec = ft_vec_new(); + ft_vec_add(&vec, (void *)&arr[0]); + ft_vec_add(&vec, (void *)&arr[1]); + ft_vec_add(&vec, (void *)&arr[2]); + ft_vec_filter(vec, is42, NULL); + if (vec->count != 1 || *(int *)ft_vec_at(vec, 0) != 42) + return (1); + ft_vec_clear(vec); + pp[0] = malloc(sizeof(int)); + *pp[0] = 21; + pp[1] = malloc(sizeof(int)); + *pp[1] = 42; + pp[2] = malloc(sizeof(int)); + *pp[2] = 63; + ft_vec_add(&vec, pp[0]); + ft_vec_add(&vec, pp[1]); + ft_vec_add(&vec, pp[2]); + ft_vec_filter(vec, is42, free); + if (vec->count != 1 || ft_vec_at(vec, 0) != pp[1]) + return (1); + return (ft_vec_apply(vec, free), ft_vec_destroy(&vec), 0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_from_array.c b/libft_personal/tests/ft_vector/tests_vec_from_array.c new file mode 100644 index 0000000..b7af6da --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_from_array.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_from_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:19:37 by bgoulard #+# #+# */ +/* Updated: 2024/05/28 08:04:26 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int checks_01(t_vector *vec, void **data) +{ + if (vec->count != 3 || vec->cappacity != FT_VECTOR_BASE_LEN + || !vec->datas) + return (1); + else if (ft_vec_at(vec, 0) != data[0] || ft_vec_at(vec, 1) != data[1] + || ft_vec_at(vec, 2) != data[2]) + return (2); + return (0); +} + +int checks_02(t_vector *vec, void **data) +{ + if (vec->count != 6 || vec->cappacity != 6 || !vec->datas) + return (3); + else if (ft_vec_at(vec, 0) != data[0] || ft_vec_at(vec, 1) != data[1] + || ft_vec_at(vec, 2) != data[2] || ft_vec_at(vec, 3) != data[3] + || ft_vec_at(vec, 4) != data[4] || ft_vec_at(vec, 5) != data[5]) + return (4); + return (0); +} + +int test_vec_from_array(void) +{ + void *data[3]; + void *data2[6]; + t_vector *vec; + + data[0] = (void *)12; + data[1] = (void *)13; + data[2] = (void *)14; + data2[0] = (void *)20; + data2[1] = (void *)21; + data2[2] = (void *)22; + data2[3] = (void *)23; + data2[4] = (void *)24; + data2[5] = (void *)25; + vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0])); + if (checks_01(vec, data)) + return (checks_01(vec, data)); + ft_vec_destroy(&vec); + vec = ft_vec_from_array(data2, sizeof(data2) / sizeof(*data2)); + if (checks_02(vec, data2)) + return (checks_02(vec, data2)); + return (ft_vec_destroy(&vec), 0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_from_size.c b/libft_personal/tests/ft_vector/tests_vec_from_size.c new file mode 100644 index 0000000..7c9420a --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_from_size.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_from_size.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:20:00 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:20:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_from_size(void) +{ + t_vector *vec; + + vec = ft_vec_from_size(42); + if (vec->count != 0) + return (1); + else if (vec->cappacity != 42) + return (1); + else if (!vec->datas) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_get.c b/libft_personal/tests/ft_vector/tests_vec_get.c new file mode 100644 index 0000000..e1eb031 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_get.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_get.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 11:25:01 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 11:41:24 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_vector.h" + +static int cmp_fun(const void *v_data, const void *key) +{ + return (ft_strcmp(v_data, key)); +} + +int test_vec_get(void) +{ + t_vector *vector; + void *data_ret; + + vector = ft_vec_new(); + ft_vec_add(&vector, "Hello"); + ft_vec_add(&vector, "world"); + ft_vec_add(&vector, "this"); + ft_vec_add(&vector, "is"); + ft_vec_add(&vector, "Zod"); + data_ret = ft_vec_get(vector, "world", cmp_fun); + if (ft_strcmp(data_ret, "world") != 0) + return (1); + data_ret = ft_vec_get(vector, "Zod", cmp_fun); + if (ft_strcmp(data_ret, "Zod") != 0) + return (1); + data_ret = ft_vec_get(vector, "not here", cmp_fun); + if (data_ret) + return (1); + ft_vec_destroy(&vector); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_map.c b/libft_personal/tests/ft_vector/tests_vec_map.c new file mode 100644 index 0000000..0aa7ccc --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_map.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:21:45 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:12:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "tests/tests_lambda_functions.h" +#include + +int test_vec_map(void) +{ + t_vector *vec; + t_vector *ret; + int **arr; + + arr = (int **)creat_tb(); + vec = ft_vec_from_array((void **)arr, 3); + ret = ft_vec_map(vec, add42_ret); + if (ret->count != 3) + return (1); + else if (*(int *)ft_vec_at(ret, 0) != 84 || *(int *)ft_vec_at(ret, 1) != 85 + || *(int *)ft_vec_at(ret, 2) != 86) + return (2); + ft_vec_apply(ret, free); + ft_vec_destroy(&vec); + ft_vec_destroy(&ret); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_new.c b/libft_personal/tests/ft_vector/tests_vec_new.c new file mode 100644 index 0000000..a023a34 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_new.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:21:20 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 20:44:14 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_new(void) +{ + t_vector *vec; + + vec = ft_vec_new(); + if (vec->count != 0) + return (1); + else if (vec->cappacity != FT_VECTOR_BASE_LEN) + return (1); + else if (!vec->datas) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_pop.c b/libft_personal/tests/ft_vector/tests_vec_pop.c new file mode 100644 index 0000000..303bf2a --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_pop.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_pop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/01 13:47:14 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 13:56:27 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_vector.h" +#include + +int test_vec_pop(void) +{ + t_vector *vec; + const char *str; + + vec = ft_vec_new(); + ft_vec_add(&vec, "value1"); + ft_vec_add(&vec, "value2"); + ft_vec_add(&vec, "value3"); + str = ft_vec_pop(vec); + if (!str || ft_strcmp(str, "value3") != 0 || vec->count != 2) + return (1); + str = ft_vec_pop(vec); + if (!str || ft_strcmp(str, "value2") != 0 || vec->count != 1) + return (2); + str = ft_vec_pop(vec); + if (!str || ft_strcmp(str, "value1") != 0 || vec->count != 0) + return (3); + str = ft_vec_pop(vec); + if (str) + return (4); + return (ft_vec_destroy(&vec), 0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_remove.c b/libft_personal/tests/ft_vector/tests_vec_remove.c new file mode 100644 index 0000000..fef15b8 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_remove.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 10:13:12 by bgoulard #+# #+# */ +/* Updated: 2024/05/31 15:55:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include + +// tests vector has removed the arr[2]orrect element +// and +// tests vector_remove is bound checked with 42's call +static int checks_01(t_vector *vec) +{ + if (vec->count != 2) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 42 || \ + *(int *)ft_vec_at(vec, 1) != 44) + return (2); + ft_vec_remove(vec, 42, NULL); + if (vec->count != 2) + return (3); + return (0); +} + +// tests vector_remove calls free on the correct element +static int checks_02(t_vector *vec, const int *arr) +{ + if (vec->count != 2) + return (1); + if (*(int *)ft_vec_at(vec, 0) != arr[0] || *(int *)ft_vec_at(vec, 1) != \ + arr[2]) + return (1); + return (0); +} + +int test_vec_remove(void) +{ + t_vector *vec; + const int arr[3] = {42, 43, 44}; + int *ptr; + size_t i; + + i = 0; + vec = ft_vec_new(); + ft_vec_add(&vec, (void *)&arr[0]); + ft_vec_add(&vec, (void *)&arr[1]); + ft_vec_add(&vec, (void *)&arr[2]); + ft_vec_remove(vec, 1, NULL); + if (checks_01(vec)) + return (checks_01(vec)); + ft_vec_destroy(&vec); + vec = ft_vec_new(); + while (i < 3) + { + ptr = (int *)malloc(sizeof(int)); + *ptr = arr[i++]; + ft_vec_add(&vec, (void *)ptr); + } + ft_vec_remove(vec, 1, free); + if (checks_02(vec, arr)) + return (checks_02(vec, arr)); + return (ft_vec_apply(vec, free), ft_vec_destroy(&vec), 0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_remove_if.c b/libft_personal/tests/ft_vector/tests_vec_remove_if.c new file mode 100644 index 0000000..61aeec4 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_remove_if.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_remove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:51:57 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "tests/tests_lambda_functions.h" + +int test_vec_remove_if(void) +{ + t_vector *vec; + int a; + int b; + int c; + + a = 42; + b = 43; + c = 44; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + ft_vec_remove_if(vec, is42, NULL); + if (vec->count != 2) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 43) + return (1); + else if (*(int *)ft_vec_at(vec, 1) != 44) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_reserve.c b/libft_personal/tests/ft_vector/tests_vec_reserve.c new file mode 100644 index 0000000..fff26f0 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_reserve.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_reserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:28:57 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:30:10 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_reserve(void) +{ + t_vector *vec; + bool ret[3]; + + vec = ft_vec_from_size(FT_VECTOR_BASE_LEN - 1); + ret[0] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN - 2); + ret[1] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN); + ret[2] = ft_vec_reserve(&vec, FT_VECTOR_BASE_LEN + 2); + if (ret[0] != true) + return (1); + else if (ret[1] != true) + return (1); + else if (ret[2] == false) + return (1); + else if (vec->cappacity != FT_VECTOR_BASE_LEN + 2) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_reverse.c b/libft_personal/tests/ft_vector/tests_vec_reverse.c new file mode 100644 index 0000000..39d1e48 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_reverse.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:30:26 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:30:33 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_reverse(void) +{ + t_vector *vec; + int a; + int b; + int c; + + a = 42; + b = 43; + c = 44; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + ft_vec_shrink(vec); + ft_vec_reverse(vec); + if (vec->count != 3) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 44) + return (1); + else if (*(int *)ft_vec_at(vec, 1) != 43) + return (1); + else if (*(int *)ft_vec_at(vec, 2) != 42) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_shift.c b/libft_personal/tests/ft_vector/tests_vec_shift.c new file mode 100644 index 0000000..92e459c --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_shift.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_shift.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:28:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 11:28:35 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_shift(void) +{ + t_vector *vec; + int a; + int b; + int c; + int d; + + a = 42; + b = 43; + c = 44; + d = 45; + vec = ft_vec_new(); + ft_vec_add(&vec, &a); + ft_vec_add(&vec, &b); + ft_vec_add(&vec, &c); + ft_vec_add(&vec, &d); + ft_vec_shift(vec, 1, 2); + if (vec->count != 2) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 42) + return (1); + else if (*(int *)ft_vec_at(vec, 1) != 45) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_shrink.c b/libft_personal/tests/ft_vector/tests_vec_shrink.c new file mode 100644 index 0000000..3dcee44 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_shrink.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_shrink.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:28:19 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 20:43:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +/* +// try to shrink an already shrunk vector + ft_vec_shrink(vec); +*/ + +int test_vec_shrink(void) +{ + t_vector *vec; + void *data[3]; + int numbers[3]; + + numbers[0] = 42; + numbers[1] = 43; + numbers[2] = 44; + data[0] = (void *)&numbers[0]; + data[1] = (void *)&numbers[1]; + data[2] = (void *)&numbers[2]; + vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0])); + ft_vec_shrink(vec); + if (vec->count != 3 || vec->cappacity != 3) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 42 || *(int *)ft_vec_at(vec, 1) != \ + 43 || *(int *)ft_vec_at(vec, 2) != 44) + return (1); + ft_vec_shrink(vec); + if (!vec || !vec->datas || !vec->cappacity || !vec->count || \ + vec->count != vec->cappacity || vec->count != 3) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_sort.c b/libft_personal/tests/ft_vector/tests_vec_sort.c new file mode 100644 index 0000000..a7fc0c0 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_sort.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:27:57 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:04:49 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "tests/tests_lambda_functions.h" + +int test_vec_sort(void) +{ + t_vector *vec; + int nbrs[3]; + + nbrs[0] = 44; + vec = ft_vec_new(); + ft_vec_add(&vec, (void *)&nbrs[0]); + nbrs[1] = 43; + ft_vec_add(&vec, (void *)&nbrs[1]); + nbrs[2] = 42; + ft_vec_add(&vec, (void *)&nbrs[2]); + ft_vec_sort(vec, cmp_int); + if (vec->count != 3) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 42) + return (2); + else if (*(int *)ft_vec_at(vec, 1) != 43) + return (3); + else if (*(int *)ft_vec_at(vec, 2) != 44) + return (4); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_swap.c b/libft_personal/tests/ft_vector/tests_vec_swap.c new file mode 100644 index 0000000..3a629bc --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_swap.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:27:24 by bgoulard #+# #+# */ +/* Updated: 2024/05/24 21:18:13 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_vector.h" +#include "ft_vector_types.h" + +int test_vec_swap(void) +{ + t_vector *vec; + void *data[3]; + int arr[3]; + + arr[0] = 42; + arr[1] = 43; + arr[2] = 44; + data[0] = (void *)&arr[0]; + data[1] = (void *)&arr[1]; + data[2] = (void *)&arr[2]; + vec = ft_vec_from_array(data, sizeof(data) / sizeof(data[0])); + ft_vec_swap(vec, 0, 2); + if (vec->count != 3) + return (1); + else if (*(int *)ft_vec_at(vec, 0) != 44) + return (1); + else if (*(int *)ft_vec_at(vec, 1) != 43) + return (1); + else if (*(int *)ft_vec_at(vec, 2) != 42) + return (1); + ft_vec_destroy(&vec); + return (0); +} diff --git a/libft_personal/tests/ft_vector/tests_vec_to_array.c b/libft_personal/tests/ft_vector/tests_vec_to_array.c new file mode 100644 index 0000000..a616572 --- /dev/null +++ b/libft_personal/tests/ft_vector/tests_vec_to_array.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_vec_to_array.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/30 11:35:25 by bgoulard #+# #+# */ +/* Updated: 2024/05/30 11:41:05 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "ft_vector.h" + +int test_vec_to_array(void) +{ + t_vector *vector; + const char **array; + + vector = ft_vec_new(); + ft_vec_add(&vector, "Hello"); + ft_vec_add(&vector, "world"); + ft_vec_add(&vector, "this"); + ft_vec_add(&vector, "is"); + ft_vec_add(&vector, "Zod"); + array = (const char **)ft_vec_to_array(&vector); + if (ft_strcmp(array[0], "Hello") != 0) + return (1); + if (ft_strcmp(array[1], "world") != 0) + return (1); + if (ft_strcmp(array[2], "this") != 0) + return (1); + if (ft_strcmp(array[3], "is") != 0) + return (1); + if (ft_strcmp(array[4], "Zod") != 0) + return (1); + free(array); + return (0); +} diff --git a/libft_personal/tests/ft_vector/vector_tests.c b/libft_personal/tests/ft_vector/vector_tests.c new file mode 100644 index 0000000..999f9fc --- /dev/null +++ b/libft_personal/tests/ft_vector/vector_tests.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vector_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/13 08:30:25 by bgoulard #+# #+# */ +/* Updated: 2024/06/02 11:33:20 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tests/tests.h" +#include "tests/vector_tests.h" + +int tests_vector(void) +{ + int collect; + const t_test tests[] = { + {"add", test_vec_add}, {"apply", test_vec_apply}, {"at", test_vec_at}, \ + {"cat", test_vec_cat}, {"clear", test_vec_clear}, \ + {"destroy", test_vec_destroy}, {"filter", test_vec_filter}, \ + {"map", test_vec_map}, {"new", test_vec_new}, {"pop", test_vec_pop}, \ + {"from_size", test_vec_from_size}, {"from_array", test_vec_from_array}, \ + {"convert_alloc_array", test_vec_convert_alloc_array}, \ + {"remove", test_vec_remove}, {"remove_if", test_vec_remove_if}, \ + {"reserve", test_vec_reserve}, {"reverse", test_vec_reverse}, \ + {"shift", test_vec_shift}, {"sort", test_vec_sort}, \ + {"shrink", test_vec_shrink}, {"swap", test_vec_swap}, \ + {"get", test_vec_get}, {"to_array", test_vec_to_array}, \ + {NULL, NULL} + }; + + collect = 0; + run_test(tests, &collect); + return (collect); +} diff --git a/libft_personal/tests/lambdas_for_tests.c b/libft_personal/tests/lambdas_for_tests.c new file mode 100644 index 0000000..6742799 --- /dev/null +++ b/libft_personal/tests/lambdas_for_tests.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lambdas_for_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/24 11:08:37 by bgoulard #+# #+# */ +/* Updated: 2024/05/29 00:03:50 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void **creat_tb(void) +{ + static int a; + static int b; + static int c; + static int *tab[3] = {0}; + + a = 42; + b = 43; + c = 44; + tab[0] = &a; + tab[1] = &b; + tab[2] = &c; + return ((void **)tab); +} + +void add42(void *data) +{ + int *ptr; + + ptr = (int *)data; + *ptr += 42; +} + +bool is42(const void *data) +{ + if (!data) + return (false); + return (*(int *)data == 42); +} + +void *add42_ret(const void *data) +{ + int *ret; + + ret = malloc(sizeof(int)); + *ret = *(int *)data; + *ret += 42; + return ((void *)ret); +} + +int cmp_int(const void *a, const void *b) +{ + return (*(int *)a - *(int *)b); +} diff --git a/libft_personal/tests/main_tests.c b/libft_personal/tests/main_tests.c new file mode 100644 index 0000000..f92bdae --- /dev/null +++ b/libft_personal/tests/main_tests.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_tests.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/12 20:33:46 by iron #+# #+# */ +/* Updated: 2024/07/06 16:23:30 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/tests.h" + +const t_test *get_tests(void) +{ + static const t_test tests[] = { + {"string", tests_string}, + {"vec", tests_vector}, + {"map", tests_map}, + {"simply linked lists", tests_linked_list_all}, + {"doubly linked lists", tests_doubly_linked_list_all}, + {"optional", tests_optional}, + {"args", tests_args}, + {"math", tests_math}, + {"pair", tests_pair}, + {NULL, NULL} + }; + + return (tests); +} + +static int exit_msg(int collect) +{ + if (collect == 0) + ft_putendl_fd("\033[32mAll tests passed\033[0m", STDOUT_FILENO); + else + ft_putendl_fd("\033[31mSome tests failed\033[0m", STDOUT_FILENO); + return (collect != 0); +} + +int main(void) +{ + int collect; + int prev; + size_t i; + const t_test *tests = get_tests(); + + i = 0; + collect = 0; + while (tests[i].name) + { + prev = collect; + ft_putstr_fd("\n\nTesting ", STDOUT_FILENO); + ft_putendl_fd(tests[i].name, STDOUT_FILENO); + collect += tests[i].test(); + ft_putstr_fd("\nModule:: ", STDOUT_FILENO); + ft_putstr_fd(tests[i].name, STDOUT_FILENO); + if (collect == prev) + ft_putstr_fd(" \033[32mOK\033[0m\n", STDOUT_FILENO); + else + ft_putstr_fd(" \033[31mKO\033[0m\n", STDOUT_FILENO); + i++; + } + return (exit_msg(collect)); +} diff --git a/libft_personal/tests/tests_utils.c b/libft_personal/tests/tests_utils.c new file mode 100644 index 0000000..b24a9df --- /dev/null +++ b/libft_personal/tests/tests_utils.c @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tests_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/05 04:18:57 by bgoulard #+# #+# */ +/* Updated: 2024/06/01 12:12:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "tests/tests.h" +#include +#include +#include + +#ifdef FORK_TESTS + +static int run_t_test(t_test test, int *collect) +{ + int ret; + int pid; + + pid = fork(); + if (pid == 0) + exit(test.test()); + else + { + waitpid(pid, &ret, 0); + ft_putstr_fd(test.name, STDOUT_FILENO); + if (WIFEXITED(ret) && ret == 0) + ft_putstr_fd(" \033[32mOK \033[0m", STDOUT_FILENO); + else + { + ft_putstr_fd(" \033[31mKO\033[0m ret::", STDOUT_FILENO); + if (WIFEXITED(ret)) + ft_putnbr_fd(WEXITSTATUS(ret), STDOUT_FILENO); + else + ft_putstr_fd("crash", STDOUT_FILENO); + } + ft_putstr_fd("\n", STDOUT_FILENO); + *collect += ret; + } + return (ret); +} + +#else + +static int run_t_test(t_test test, int *collect) +{ + int ret; + + ret = test.test(); + *collect += ret; + if (ret != 0) + { + ft_putstr_fd(test.name, STDOUT_FILENO); + ft_putstr_fd(" \033[31mKO\033[0m ret::", STDOUT_FILENO); + ft_putnbr_fd(ret, STDOUT_FILENO); + ft_putstr_fd("\n", STDOUT_FILENO); + } + else + ft_putstr_fd(" \033[32mOK\033[0m", STDOUT_FILENO); + return (ret); +} + +#endif + +int open_test_file(char **func_to_test) +{ + char *file; + int fd; + + file = ft_strjoin(TESTS_FPREFIX, *func_to_test); + fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + { + ft_putstr_fd("Error: on oppening ", STDERR_FILENO); + ft_putstr_fd(file, STDERR_FILENO); + return (free(file), -1); + } + *func_to_test = file; + return (fd); +} + +void destroy_test_file(int fd, const char *file) +{ + close(fd); + unlink(file); +} + +int run_test(const t_test *test, int *collect) +{ + size_t i; + int ret; + int sum; + + ret = 0; + sum = 0; + i = 0; + while (test[i].name) + { + ret = run_t_test(test[i], &sum); + *collect += ret; + sum += ret; + i++; + } + return (sum); +}