diff --git a/_ci_tdd/Makefile b/_ci_tdd/Makefile index bc14eee..e7d9cae 100644 --- a/_ci_tdd/Makefile +++ b/_ci_tdd/Makefile @@ -6,7 +6,7 @@ # By: umeneses +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/05/10 10:09:44 by umeneses #+# #+# # -# Updated: 2024/06/04 16:33:23 by umeneses ### ########.fr # +# Updated: 2024/06/19 15:05:18 by umeneses ### ########.fr # # # # **************************************************************************** # @@ -75,7 +75,7 @@ COMP_EXE = $(CC) $(LDFLAGS) $(OBJ_FILES) $(LDLIBS) -o $(NAME) # **************************************************************************** # define arguments - 42 1 1024 + 42 1 10 21 11 7 4 2 endef define bonus @@ -96,13 +96,13 @@ $(BUILD_D)%.o: %.c @$(COMP_OBJS) @echo "Compiling: $(notdir $<)" -$(NAME): fclean push_swap $(OBJ_FILES) +$(NAME): clean push_swap $(OBJ_FILES) @$(COMP_EXE) @printf "$(PURPLE)" - @echo ">>> Running PUSH_SWAP with default arguments:" - @echo "$(call arguments)" - @printf "$(CYAN)" - $(PUSH_SWAP_D)./push_swap $(call arguments) +# @echo ">>> Running PUSH_SWAP with default arguments:" +# @echo "$(call arguments)" +# @printf "$(CYAN)" +# $(PUSH_SWAP_D)./push_swap $(call arguments) @printf "$(GREEN)" @echo "$(NAME) Ready!" @printf "$(RESET)" @@ -123,7 +123,6 @@ clean: @printf "$(PURPLE)" $(RM) $(OBJ_FILES) $(RM) $(BUILD_D) - $(MAKE) -C $(PUSH_SWAP_D) fclean @printf "$(RESET)" fclean: clean @@ -132,6 +131,7 @@ fclean: clean @printf "$(PURPLE)" $(RM) $(NAME) $(RM) $(NAME_BONUS) + $(MAKE) -C $(PUSH_SWAP_D) fclean @printf "$(RESET)" re: fclean all diff --git a/_ci_tdd/test_files/test_main_miunit.c b/_ci_tdd/test_files/test_main_miunit.c index a706458..365c99f 100644 --- a/_ci_tdd/test_files/test_main_miunit.c +++ b/_ci_tdd/test_files/test_main_miunit.c @@ -6,13 +6,15 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 19:06:12 by umeneses #+# #+# */ -/* Updated: 2024/06/10 09:55:28 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 15:18:24 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "minunit.h" #include "push_swap.h" #include "../../program_to_test/src/ft_argv_validation.c" +#include "../../program_to_test/src/ft_lst_map_all.c" +#include "../../program_to_test/src/ft_lst_map_indexers.c" #include "../../program_to_test/src/ft_lst_init.c" #include "../../program_to_test/src/ft_lst_goto.c" #include "../../program_to_test/src/ft_lst_addto.c" @@ -21,11 +23,14 @@ #include "../../program_to_test/src/ft_swap.c" #include "../../program_to_test/src/ft_push.c" #include "../../program_to_test/src/ft_rotate.c" -#include "../../program_to_test/src/ft_sort_until3.c" #include "../../program_to_test/src/ft_do_push_fts.c" #include "../../program_to_test/src/ft_do_rev_rotate_fts.c" #include "../../program_to_test/src/ft_do_rotate_fts.c" #include "../../program_to_test/src/ft_do_swap_fts.c" +#include "../../program_to_test/src/ft_do_sort.c" +#include "../../program_to_test/src/ft_sort_until3.c" +#include "../../program_to_test/src/ft_sort_until4.c" +#include "../../program_to_test/src/ft_sort_5_or_more.c" // #include "minunit_utils.c" // #include "minunit_utils.h" @@ -38,7 +43,102 @@ int ft_minunit_array_counter(char **array); void ft_array_clear(char **array, int arr_size); void ft_array_printer(char **array, int arr_size); -MU_TEST(test_1st_miastep_index_nbrs) + +MU_TEST(test_3rd_miastep_get_cost) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int actual_top_a; + int expected_bottom_a; + int actual_bottom_a; + int expected_cost; + int actual_cost; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 2 42 1 4 8 5 31 27"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[8]); + expected_bottom_a = ft_atoi(argv_simulation[2]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + expected_cost = 0; // stack_b->nbr[8] + actual_size = ft_lst_size(stack_a); + ft_lst_map_all_indexers(&stack_a); + ft_push_b_all_but_three(&stack_a, &stack_b); + ft_sort_three_nbrs(&stack_a); + ft_lst_get_cost(&stack_a, &stack_b); + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + ft_lst_printf_data_content("get_cost stack_a:", stack_a); + ft_lst_printf_data_content("get_cost stack_b:", stack_b); + actual_cost = stack_b->next->cost_b; + // ASSERT + mu_assert_int_eq(expected_cost, actual_cost); + mu_assert_int_eq(expected_size, actual_size); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_3rd_miastep_map_get_target_position) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int actual_top_a; + int expected_bottom_a; + int actual_bottom_a; + // int expected_target_pos; + // int actual_target_pos; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 2 42 1 4 8 5 31 27"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[8]); + expected_bottom_a = ft_atoi(argv_simulation[2]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + // expected_target_pos = 5; // stack_b->origin[5] + actual_size = ft_lst_size(stack_a); + ft_push_b_all_but_three(&stack_a, &stack_b); + ft_sort_three_nbrs(&stack_a); + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + + ft_lst_get_target_position(&stack_a, &stack_b); + // actual_target_pos = stack_b->target; + + // ASSERT + // mu_assert_int_eq(expected_target_pos, actual_target_pos); + mu_assert_int_eq(expected_size, actual_size); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_3rd_miastep_map_lowest_final_pos) { // ARRANGE t_stack *stack; @@ -50,8 +150,341 @@ MU_TEST(test_1st_miastep_index_nbrs) int actual_top_a; int expected_bottom_a; int actual_bottom_a; - int expected_highest_weight; - int actual_highest_weight; + int expected_lowest_final_pos_pos; + int expected_lowest_final_pos_nbr; + int actual_lowest_final_pos_pos; + int actual_lowest_final_pos_nbr; + + // ACT + stack = NULL; + userinput = "./push_swap 2 42 1 4 8 5 31 27"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[1]); + expected_bottom_a = ft_atoi(argv_simulation[8]); + + stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_actual_position(&stack); + ft_lst_map_final_pos(&stack, expected_size); + expected_lowest_final_pos_pos = 3; // argv[3] = 1; + expected_lowest_final_pos_nbr = ft_atoi(argv_simulation[expected_lowest_final_pos_pos]); + + actual_size = ft_lst_size(stack); + actual_top_a = ft_lst_goto_head(stack)->nbr; + actual_lowest_final_pos_pos = ft_lst_map_lowest_final_position(&stack); + actual_bottom_a = ft_lst_goto_end(stack)->nbr; + actual_lowest_final_pos_nbr = stack->next->next->nbr; + + + // ASSERT + mu_assert_int_eq(expected_lowest_final_pos_pos, actual_lowest_final_pos_pos); + mu_assert_int_eq(expected_lowest_final_pos_nbr, actual_lowest_final_pos_nbr); + mu_assert_int_eq(expected_size, actual_size); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + ft_lstclear_single_ptr(stack); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_sort_four_nbrs_v04) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int expected_bottom_a; + int actual_top_a; + int actual_bottom_a; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 7 10 5 12"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[3]); + expected_bottom_a = ft_atoi(argv_simulation[4]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + ft_lst_printf_data_content("stack_a before sort_four_nbrs:", stack_a); + ft_sort_four_nbrs(&stack_a, &stack_b); + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_size = ft_lst_size(stack_a); + ft_lst_printf_data_content("stack_a after sort_four_nbrs:", stack_a); + + // ASSERT + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_size, actual_size); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_sort_four_nbrs_v03) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int expected_bottom_a; + int actual_top_a; + int actual_bottom_a; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 7 5 12 10"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[2]); + expected_bottom_a = ft_atoi(argv_simulation[3]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + ft_sort_four_nbrs(&stack_a, &stack_b); + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_size = ft_lst_size(stack_a); + + // ASSERT + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_size, actual_size); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_sort_four_nbrs_v02) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int expected_bottom_a; + int actual_top_a; + int actual_bottom_a; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 5 12 10 7"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[1]); + expected_bottom_a = ft_atoi(argv_simulation[2]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + ft_sort_four_nbrs(&stack_a, &stack_b); + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_size = ft_lst_size(stack_a); + + // ASSERT + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_size, actual_size); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_sort_four_nbrs_v01) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int expected_bottom_a; + int actual_top_a; + int actual_bottom_a; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 12 5 7 10"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[2]); + expected_bottom_a = ft_atoi(argv_simulation[1]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_all_indexers(&stack_a); + ft_sort_four_nbrs(&stack_a, &stack_b); + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_size = ft_lst_size(stack_a); + + // ASSERT + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_size, actual_size); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_2nd_miastep_push_b_all_but3_sort_a) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size_before_push; + int actual_size_before_push; + int expected_top_a_before_push; + int expected_bottom_a_before_push; + int actual_top_a_before_push; + int actual_bottom_a_before_push; + + int expected_top_a_after_push; + int expected_top_b_after_push; + int expected_bottom_a_after_push; + int expected_bottom_b_after_push; + int actual_top_a_after_push; + int actual_top_b_after_push; + int actual_bottom_a_after_push; + int actual_bottom_b_after_push; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 2 42 11 4 8 3"; + argv_simulation = ft_split(userinput, ' '); + expected_size_before_push = ft_minunit_array_counter(argv_simulation); + expected_top_a_before_push = ft_atoi(argv_simulation[1]); + expected_bottom_a_before_push = ft_atoi(argv_simulation[6]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_actual_position(&stack_a); + ft_lst_map_final_pos(&stack_a, expected_size_before_push); + + actual_size_before_push = ft_lst_size(stack_a); + actual_top_a_before_push = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a_before_push = ft_lst_goto_end(stack_a)->nbr; + + ft_push_b_all_but_three(&stack_a, &stack_b); + ft_sort_three_nbrs(&stack_a); + + expected_top_a_after_push = ft_atoi(argv_simulation[6]); + expected_bottom_a_after_push = ft_atoi(argv_simulation[5]); + expected_bottom_b_after_push = ft_atoi(argv_simulation[1]); + expected_top_b_after_push = ft_atoi(argv_simulation[3]); + actual_top_a_after_push = ft_lst_goto_head(stack_a)->nbr; + actual_top_b_after_push = ft_lst_goto_head(stack_b)->nbr; + actual_bottom_a_after_push = ft_lst_goto_end(stack_a)->nbr; + actual_bottom_b_after_push = ft_lst_goto_end(stack_b)->nbr; + // ASSERT + mu_assert_int_eq(expected_top_a_after_push, actual_top_a_after_push); + mu_assert_int_eq(expected_top_b_after_push, actual_top_b_after_push); + mu_assert_int_eq(expected_bottom_b_after_push, actual_bottom_b_after_push); + mu_assert_int_eq(expected_bottom_a_after_push, actual_bottom_a_after_push); + mu_assert_int_eq(expected_size_before_push, actual_size_before_push); + mu_assert_int_eq(expected_top_a_before_push, actual_top_a_before_push); + mu_assert_int_eq(expected_bottom_a_before_push, actual_bottom_a_before_push); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size_before_push); +} + +MU_TEST(test_1st_miastep_push_b_all_but3) +{ + // ARRANGE + t_stack *stack_a; + t_stack *stack_b; + char **argv_simulation = NULL; + char *userinput; + int expected_size_before_push; + int actual_size_before_push; + int expected_top_a_before_push; + int expected_bottom_a_before_push; + int actual_top_a_before_push; + int actual_bottom_a_before_push; + + int expected_top_a_after_push; + int expected_top_b_after_push; + int expected_bottom_a_after_push; + int expected_bottom_b_after_push; + int actual_top_a_after_push; + int actual_top_b_after_push; + int actual_bottom_a_after_push; + int actual_bottom_b_after_push; + + // ACT + stack_a = NULL; + stack_b = NULL; + userinput = "./push_swap 2 42 11 4 8 3"; + argv_simulation = ft_split(userinput, ' '); + expected_size_before_push = ft_minunit_array_counter(argv_simulation); + expected_top_a_before_push = ft_atoi(argv_simulation[1]); + expected_bottom_a_before_push = ft_atoi(argv_simulation[6]); + + stack_a = ft_lts_buildstack_argv(&stack_a, argv_simulation); + ft_lst_map_actual_position(&stack_a); + ft_lst_map_final_pos(&stack_a, expected_size_before_push); + + actual_size_before_push = ft_lst_size(stack_a); + actual_top_a_before_push = ft_lst_goto_head(stack_a)->nbr; + actual_bottom_a_before_push = ft_lst_goto_end(stack_a)->nbr; + + ft_push_b_all_but_three(&stack_a, &stack_b); + + expected_top_a_after_push = ft_atoi(argv_simulation[4]); + expected_bottom_a_after_push = ft_atoi(argv_simulation[6]); + expected_bottom_b_after_push = ft_atoi(argv_simulation[1]); + expected_top_b_after_push = ft_atoi(argv_simulation[3]); + actual_top_a_after_push = ft_lst_goto_head(stack_a)->nbr; + actual_top_b_after_push = ft_lst_goto_head(stack_b)->nbr; + actual_bottom_a_after_push = ft_lst_goto_end(stack_a)->nbr; + actual_bottom_b_after_push = ft_lst_goto_end(stack_b)->nbr; + // ASSERT + mu_assert_int_eq(expected_top_a_after_push, actual_top_a_after_push); + mu_assert_int_eq(expected_top_b_after_push, actual_top_b_after_push); + mu_assert_int_eq(expected_bottom_b_after_push, actual_bottom_b_after_push); + mu_assert_int_eq(expected_bottom_a_after_push, actual_bottom_a_after_push); + mu_assert_int_eq(expected_size_before_push, actual_size_before_push); + mu_assert_int_eq(expected_top_a_before_push, actual_top_a_before_push); + mu_assert_int_eq(expected_bottom_a_before_push, actual_bottom_a_before_push); + ft_lstclear_single_ptr(stack_a); + ft_lstclear_single_ptr(stack_b); + ft_array_clear(argv_simulation, expected_size_before_push); +} + +MU_TEST(test_1st_miastep_final_pos_index) +{ + // ARRANGE + t_stack *stack; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int actual_top_a; + int expected_bottom_a; + int actual_bottom_a; + int expected_highest_final_pos; + int actual_highest_final_pos; // ACT stack = NULL; @@ -60,24 +493,70 @@ MU_TEST(test_1st_miastep_index_nbrs) expected_size = ft_minunit_array_counter(argv_simulation); expected_top_a = ft_atoi(argv_simulation[1]); expected_bottom_a = ft_atoi(argv_simulation[8]); - expected_highest_weight = expected_size; + expected_highest_final_pos = expected_size - 1; stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_actual_position(&stack); + ft_lst_map_final_pos(&stack, expected_size); actual_size = ft_lst_size(stack); - ft_weight_mesurement(&stack); actual_top_a = ft_lst_goto_head(stack)->nbr; - while (stack && stack->next != NULL) + + while (stack->origin <= 2) { - ft_printf("nbr = %d\n", stack->nbr); - ft_printf("weigth = %d\n", stack->weight); stack = stack->next; + if (stack->origin == 2) + actual_highest_final_pos = stack->final_pos; } - actual_highest_weight = stack->weight; actual_bottom_a = ft_lst_goto_end(stack)->nbr; // ASSERT - mu_assert_int_eq(expected_highest_weight, actual_highest_weight); + mu_assert_int_eq(expected_highest_final_pos, actual_highest_final_pos); + mu_assert_int_eq(expected_size, actual_size); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + ft_lstclear_single_ptr(stack); + ft_array_clear(argv_simulation, expected_size); +} + +MU_TEST(test_1st_miastep_pos_index) +{ + // ARRANGE + t_stack *stack; + char **argv_simulation = NULL; + char *userinput; + int expected_size; + int actual_size; + int expected_top_a; + int actual_top_a; + int expected_bottom_a; + int actual_bottom_a; + int expected_fith_position; + int actual_fith_position; + + // ACT + stack = NULL; + userinput = "./push_swap 2 42 0 4 8 3 31 27"; + argv_simulation = ft_split(userinput, ' '); + expected_size = ft_minunit_array_counter(argv_simulation); + expected_top_a = ft_atoi(argv_simulation[1]); + expected_bottom_a = ft_atoi(argv_simulation[8]); + expected_fith_position = ft_atoi(argv_simulation[5]); + + stack = ft_lts_buildstack_argv(&stack, argv_simulation); + ft_lst_map_actual_position(&stack); + + actual_size = ft_lst_size(stack); + actual_top_a = ft_lst_goto_head(stack)->nbr; + while (stack->origin <= 5) + { + stack = stack->next; + if (stack->origin == 5) + actual_fith_position = stack->nbr; + } + actual_bottom_a = ft_lst_goto_end(stack)->nbr; + // ASSERT + mu_assert_int_eq(expected_fith_position, actual_fith_position); mu_assert_int_eq(expected_size, actual_size); mu_assert_int_eq(expected_top_a, actual_top_a); mu_assert_int_eq(expected_bottom_a, actual_bottom_a); @@ -106,7 +585,8 @@ MU_TEST(test_sort_3_nbrs_already_sorted) expected_sorted_top = ft_atoi(argv_simulation[1]); expected_sorted_bottom = ft_atoi(argv_simulation[3]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -140,7 +620,8 @@ MU_TEST(test_sort_3_nbrs_highest_last) expected_sorted_top = ft_atoi(argv_simulation[2]); expected_sorted_bottom = ft_atoi(argv_simulation[3]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -174,7 +655,8 @@ MU_TEST(test_sort_3_nbrs_highest_first_v2) expected_sorted_top = ft_atoi(argv_simulation[3]); expected_sorted_bottom = ft_atoi(argv_simulation[1]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -208,7 +690,8 @@ MU_TEST(test_sort_3_nbrs_highest_first) expected_sorted_top = ft_atoi(argv_simulation[2]); expected_sorted_bottom = ft_atoi(argv_simulation[1]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -242,10 +725,12 @@ MU_TEST(test_sort_3_nbrs_highest_at_middle_v2) expected_sorted_top = ft_atoi(argv_simulation[1]); expected_sorted_bottom = ft_atoi(argv_simulation[2]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); - actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; + stack = ft_lst_goto_head(stack); + actual_sorted_top = (stack)->nbr; // ASSERT mu_assert_int_eq(expected_size, actual_size); @@ -276,7 +761,8 @@ MU_TEST(test_sort_3_nbrs_highest_at_middle) expected_sorted_top = ft_atoi(argv_simulation[3]); expected_sorted_bottom = ft_atoi(argv_simulation[2]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_3_nbrs(&stack); + ft_lst_map_all_indexers(&stack); + stack = ft_sort_three_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -310,7 +796,7 @@ MU_TEST(test_sort_2_nbrs_with_zero) expected_sorted_top = ft_atoi(argv_simulation[2]); expected_sorted_bottom = ft_atoi(argv_simulation[1]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_2_nbrs(&stack); + stack = ft_sort_two_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_top = ft_lst_goto_head(stack)->nbr; actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; @@ -344,7 +830,7 @@ MU_TEST(test_sort_2_nbrs_without_zero) expected_sorted_bottom = ft_atoi(argv_simulation[1]); expected_sorted_top = ft_atoi(argv_simulation[2]); stack = ft_lts_buildstack_argv(&stack, argv_simulation); - stack = ft_sort_2_nbrs(&stack); + stack = ft_sort_two_nbrs(&stack); actual_size = ft_lst_size(stack); actual_sorted_bottom = ft_lst_goto_end(stack)->nbr; actual_sorted_top = ft_lst_goto_head(stack)->nbr; @@ -357,6 +843,40 @@ MU_TEST(test_sort_2_nbrs_without_zero) ft_array_clear(argv_simulation, expected_size); } +MU_TEST(test_buildstack_non_nbr) +{ + // ARRANGE + char **argv_simulation = NULL; + char *userinput; + int index; + int expected_size; + int actual_size; + int expected_argv_validation; + int actual_argv_validation; + + // ACT + userinput = "./push_swap 1 3 5 2 4 7 a"; + argv_simulation = ft_split(userinput, ' '); + expected_size = 7; + expected_argv_validation = false; + actual_size = ft_minunit_array_counter(argv_simulation); + index = expected_size; + while (index >= 0) + { + if (!ft_isdigit(*argv_simulation[index])) + { + actual_argv_validation = false; + break ; + } + index--; + } + + // ASSERT + mu_assert_int_eq(expected_size, actual_size); + mu_assert_int_eq(expected_argv_validation, actual_argv_validation); + ft_array_clear(argv_simulation, expected_size); +} + MU_TEST(test_buildstack_argv) { // ARRANGE @@ -626,10 +1146,14 @@ MU_TEST(test_ft_push_b) int second_b = 888; int third_b = 777; int bottom_b = 666; - int actual_result_a; - int actual_result_b; - int expected_result_a; - int expected_result_b; + int actual_top_a; + int actual_top_b; + int expected_top_a; + int expected_top_b; + int actual_bottom_a; + int actual_bottom_b; + int expected_bottom_a; + int expected_bottom_b; int actual_size_a; int actual_size_b; int expected_size_a; @@ -648,19 +1172,25 @@ MU_TEST(test_ft_push_b) stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(second_b)); stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(third_b)); stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(bottom_b)); - expected_result_a = second_a; - expected_result_b = top_a; + expected_top_a = second_a; + expected_bottom_a = bottom_a; + expected_top_b = top_a; + expected_bottom_b = bottom_b; expected_size_a = ft_lst_size(stack_a) - 1; expected_size_b = ft_lst_size(stack_b) + 1; ft_push_b(&stack_a, &stack_b); - actual_result_a = ft_lst_goto_head(stack_a)->nbr; - actual_result_b = ft_lst_goto_head(stack_b)->nbr; + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_top_b = ft_lst_goto_head(stack_b)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_bottom_b = ft_lst_goto_end(stack_b)->nbr; actual_size_a = ft_lst_size(stack_a); actual_size_b = ft_lst_size(stack_b); // ASSERT - mu_assert_int_eq(expected_result_a, actual_result_a); - mu_assert_int_eq(expected_result_b, actual_result_b); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_top_b, actual_top_b); + mu_assert_int_eq(expected_bottom_b, actual_bottom_b); mu_assert_int_eq(expected_size_a, actual_size_a); mu_assert_int_eq(expected_size_b, actual_size_b); ft_lstclear_single_ptr(stack_a); @@ -678,10 +1208,14 @@ MU_TEST(test_ft_push_a) int second_b = 888; int third_b = 777; int bottom_b = 666; - int actual_result_a; - int actual_result_b; - int expected_result_a; - int expected_result_b; + int actual_top_a; + int actual_top_b; + int expected_top_a; + int expected_top_b; + int actual_bottom_a; + int actual_bottom_b; + int expected_bottom_a; + int expected_bottom_b; int actual_size_a; int actual_size_b; int expected_size_a; @@ -702,17 +1236,23 @@ MU_TEST(test_ft_push_a) stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(bottom_b)); expected_size_a = ft_lst_size(stack_a) + 1; expected_size_b = ft_lst_size(stack_b) - 1; - expected_result_a = top_b; - expected_result_b = second_b; + expected_top_a = top_b; + expected_bottom_a = bottom_a; + expected_top_b = second_b; + expected_bottom_b = bottom_b; ft_push_a(&stack_a, &stack_b); - actual_result_a = ft_lst_goto_head(stack_a)->nbr; - actual_result_b = ft_lst_goto_head(stack_b)->nbr; + actual_top_a = ft_lst_goto_head(stack_a)->nbr; + actual_top_b = ft_lst_goto_head(stack_b)->nbr; + actual_bottom_a = ft_lst_goto_end(stack_a)->nbr; + actual_bottom_b = ft_lst_goto_end(stack_b)->nbr; actual_size_a = ft_lst_size(stack_a); actual_size_b = ft_lst_size(stack_b); // ASSERT - mu_assert_int_eq(expected_result_a, actual_result_a); - mu_assert_int_eq(expected_result_b, actual_result_b); + mu_assert_int_eq(expected_top_a, actual_top_a); + mu_assert_int_eq(expected_bottom_a, actual_bottom_a); + mu_assert_int_eq(expected_top_b, actual_top_b); + mu_assert_int_eq(expected_bottom_b, actual_bottom_b); mu_assert_int_eq(expected_size_a, actual_size_a); mu_assert_int_eq(expected_size_b, actual_size_b); ft_lstclear_single_ptr(stack_a); @@ -725,11 +1265,13 @@ MU_TEST(test_ft_swap_ab) int top_a = 11; int second_a = 22; int third_a = 33; - int bottom_a = 44; - int top_b = 99; - int second_b = 88; - int third_b = 77; - int bottom_b = 66; + int fourth_a = 44; + int bottom_a = 55; + int top_b = 999; + int second_b = 888; + int third_b = 777; + int fourth_b = 666; + int bottom_b = 555; int actual_result_a; int actual_result_b; int expected_result_a; @@ -744,23 +1286,28 @@ MU_TEST(test_ft_swap_ab) // ACT stack_a = NULL; stack_b = NULL; - stack_a = ft_lst_init(top_a); + stack_a = ft_lst_addto_end(&stack_a, ft_lst_init(top_a)); stack_a = ft_lst_addto_end(&stack_a, ft_lst_init(second_a)); stack_a = ft_lst_addto_end(&stack_a, ft_lst_init(third_a)); + stack_a = ft_lst_addto_end(&stack_a, ft_lst_init(fourth_a)); stack_a = ft_lst_addto_end(&stack_a, ft_lst_init(bottom_a)); - stack_b = ft_lst_init(top_b); + stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(top_b)); stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(second_b)); stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(third_b)); + stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(fourth_b)); stack_b = ft_lst_addto_end(&stack_b, ft_lst_init(bottom_b)); expected_size_a = ft_lst_size(stack_a); expected_size_b = ft_lst_size(stack_b); expected_result_a = second_a; expected_result_b = second_b; - ft_swap_ab(&stack_a, &stack_b); + + ft_swap_ab(&stack_a, &stack_b); actual_result_a = ft_lst_goto_head(stack_a)->nbr; actual_result_b = ft_lst_goto_head(stack_b)->nbr; actual_size_a = ft_lst_size(stack_a); actual_size_b = ft_lst_size(stack_b); + ft_lst_printf_data_content("stack_a", stack_a); + ft_lst_printf_data_content("stack_b", stack_b); // ASSERT mu_assert_int_eq(expected_result_a, actual_result_a); @@ -777,6 +1324,7 @@ MU_TEST(test_ft_swap) int top = 11; int second = 22; int third = 33; + int fourth = 44; int bottom = 99; int expected_top; int expected_bottom; @@ -791,6 +1339,7 @@ MU_TEST(test_ft_swap) stack = ft_lst_init(top); stack = ft_lst_addto_end(&stack, ft_lst_init(second)); stack = ft_lst_addto_end(&stack, ft_lst_init(third)); + stack = ft_lst_addto_end(&stack, ft_lst_init(fourth)); stack = ft_lst_addto_end(&stack, ft_lst_init(bottom)); expected_top = second; expected_bottom = bottom; @@ -884,23 +1433,22 @@ MU_TEST(test_lst_delat_begin) MU_TEST(test_lst_gotoend_head_and_beforeend) { // ARRANGE - int top = 11; - int second = 22; - int third = 33; - int bottom = 44; - int expected_top; - int expected_bottom; - int expected_before_end; - int expected_ptr; - int actual_top; - int actual_bottom; - int actual_before_end; - int expected_size; - int actual_size; - int actual_top_again; - int actual_ptr; - t_stack *stack; - t_stack *head_tocheck; + int top = 11; + int second = 22; + int third = 33; + int bottom = 44; + int expected_top; + int expected_bottom; + int expected_before_end; + int actual_top; + int actual_bottom; + int actual_before_end; + int expected_size; + int actual_size; + int actual_top_again; + t_stack *actual_ptr; + t_stack *expected_ptr; + t_stack *stack; // ACT stack = NULL; @@ -912,8 +1460,8 @@ MU_TEST(test_lst_gotoend_head_and_beforeend) expected_before_end = third; expected_bottom = bottom; expected_size = 4; - expected_ptr = 1; - head_tocheck = stack; + expected_ptr = stack; + actual_bottom = ft_lst_goto_end(stack)->nbr; actual_top = ft_lst_goto_head(stack)->nbr; actual_size = ft_lst_size(stack); @@ -923,12 +1471,10 @@ MU_TEST(test_lst_gotoend_head_and_beforeend) stack = stack->next; } actual_top_again = ft_lst_goto_head(stack)->nbr; - actual_ptr = 0; - if (head_tocheck == stack) - actual_ptr = 1; + actual_ptr = ft_lst_goto_head(stack); // ASSERT - // mu_assert_int_eq(expected_ptr, actual_ptr); + mu_assert_mem_eq(&expected_ptr, &actual_ptr, sizeof(t_stack *)); mu_assert_int_eq(expected_top, actual_top_again); mu_assert_int_eq(expected_top, actual_top); mu_assert_int_eq(expected_before_end, actual_before_end); @@ -1019,24 +1565,6 @@ MU_TEST(test_lst_addto_begin) ft_lstclear_single_ptr(stack); } -// MU_TEST(test_lst_init_non_nbr) -// { -// // ARRANGE -// int char_test = 'a'; -// int expected_result; -// int actual_result; -// t_stack *stack; - -// // ACT -// stack = ft_lst_init(char_test); -// actual_result = stack->nbr; -// expected_result = '\0'; - -// // ASSERT -// mu_assert_int_eq(expected_result, actual_result); -// ft_lstclear_single_ptr(stack); -// } - MU_TEST(test_lstsize_int_expec_3) { // ARRANGE @@ -1087,7 +1615,6 @@ MU_TEST(test_lst_init_nbr) MU_TEST_SUITE(linked_list_tests) { MU_RUN_TEST(test_lst_init_nbr); - // MU_RUN_TEST(test_lst_init_non_nbr); MU_RUN_TEST(test_lstsize_int_expec_3); MU_RUN_TEST(test_lst_addto_begin); MU_RUN_TEST(test_lst_addto_end); @@ -1122,9 +1649,10 @@ MU_TEST_SUITE(argv_tests) MU_RUN_TEST(test_argv_is_duplicated_false); MU_RUN_TEST(test_argv_is_duplicated_true); MU_RUN_TEST(test_buildstack_argv); + MU_RUN_TEST(test_buildstack_non_nbr); } -MU_TEST_SUITE(sorting_tests) +MU_TEST_SUITE(sorting_2_or_3_nbrs_tests) { MU_RUN_TEST(test_sort_2_nbrs_without_zero); MU_RUN_TEST(test_sort_2_nbrs_with_zero); @@ -1136,9 +1664,27 @@ MU_TEST_SUITE(sorting_tests) MU_RUN_TEST(test_sort_3_nbrs_already_sorted); } -MU_TEST_SUITE(miacombeau_tests) +MU_TEST_SUITE(miacombeau_1st_and_2nd_steps_tests) +{ + MU_RUN_TEST(test_1st_miastep_pos_index); + MU_RUN_TEST(test_1st_miastep_final_pos_index); + MU_RUN_TEST(test_1st_miastep_push_b_all_but3); + MU_RUN_TEST(test_2nd_miastep_push_b_all_but3_sort_a); +} + +MU_TEST_SUITE(sorting_4_nbrs_tests) +{ + MU_RUN_TEST(test_sort_four_nbrs_v01); + MU_RUN_TEST(test_sort_four_nbrs_v02); + MU_RUN_TEST(test_sort_four_nbrs_v03); + MU_RUN_TEST(test_sort_four_nbrs_v04); +} + +MU_TEST_SUITE(miacombeau_3rd_step_tests) { - MU_RUN_TEST(test_1st_miastep_index_nbrs); + MU_RUN_TEST(test_3rd_miastep_map_lowest_final_pos); + MU_RUN_TEST(test_3rd_miastep_get_cost); + MU_RUN_TEST(test_3rd_miastep_map_get_target_position); } int main(void) @@ -1148,8 +1694,10 @@ int main(void) MU_RUN_SUITE(push_tests); MU_RUN_SUITE(rotate_tests); MU_RUN_SUITE(argv_tests); - MU_RUN_SUITE(sorting_tests); - MU_RUN_SUITE(miacombeau_tests); + MU_RUN_SUITE(sorting_2_or_3_nbrs_tests); + MU_RUN_SUITE(miacombeau_1st_and_2nd_steps_tests); + MU_RUN_SUITE(sorting_4_nbrs_tests); + MU_RUN_SUITE(miacombeau_3rd_step_tests); MU_REPORT(); return (0); } diff --git a/program_to_test/Makefile b/program_to_test/Makefile index 1dbb2bc..7d7deb3 100644 --- a/program_to_test/Makefile +++ b/program_to_test/Makefile @@ -6,7 +6,7 @@ # By: umeneses +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/05/09 17:59:28 by umeneses #+# #+# # -# Updated: 2024/06/06 18:49:18 by umeneses ### ########.fr # +# Updated: 2024/06/19 15:06:51 by umeneses ### ########.fr # # # # **************************************************************************** # @@ -56,7 +56,11 @@ SRC_FILES += ft_lst_clear.c SRC_FILES += ft_lst_deletes.c SRC_FILES += ft_lst_goto.c SRC_FILES += ft_lst_init.c +SRC_FILES += ft_lst_map_all.c +SRC_FILES += ft_lst_map_indexers.c SRC_FILES += ft_sort_until3.c +SRC_FILES += ft_sort_until4.c +SRC_FILES += ft_sort_5_or_more.c SRC_FILES += ft_swap.c SRC_FILES += ft_push.c SRC_FILES += ft_rotate.c @@ -74,7 +78,7 @@ NAME_BONUS = push_swap_bonus SRC_FILES_BONUS = -SRC_BONUS_ALL = $(addprefix $(SRC_SV_BONUS_D), $(SRC_FILES_BONUS)) +SRC_BONUS_ALL = $(addprefix $(SRC_BONUS_D), $(SRC_FILES_BONUS)) OBJS_FILES_BONUS = $(addprefix $(BUILD_D), $(SRC_BONUS_ALL:%.c=%.o)) OBJS_ALL_BONUS = $(OBJS_FILES_BONUS) diff --git a/program_to_test/headers/push_swap.h b/program_to_test/headers/push_swap.h index 327c7dc..2ba240b 100644 --- a/program_to_test/headers/push_swap.h +++ b/program_to_test/headers/push_swap.h @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 15:23:33 by umeneses #+# #+# */ -/* Updated: 2024/06/08 14:55:58 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 15:07:14 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,11 @@ typedef struct s_stack { int nbr; - int cost; - int pos; + int origin; + int final_pos; + int target; + int cost_a; + int cost_b; struct s_stack *prev; struct s_stack *next; } t_stack; @@ -36,7 +39,7 @@ t_stack *ft_lst_delat_begin(t_stack *stack); t_stack *ft_lst_delat_end(t_stack *stack); void ft_lstclear_double_ptr(t_stack **stack); void ft_lstclear_single_ptr(t_stack *stack); -void ft_lst_printf_int_content(char *msg, t_stack *stack); +void ft_lst_printf_data_content(char *msg, t_stack *stack); t_stack *ft_lts_buildstack_argv(t_stack **stack, char **argv); bool ft_is_sign(int c); bool ft_is_space(int c); @@ -59,16 +62,35 @@ void ft_reverse_rotate_ab(t_stack **stack_a, t_stack **stack_b); bool ft_is_sorted(t_stack *stack); /* sorting validation functions */ +int ft_lst_map_highest_pos(t_stack *list); void ft_do_sort(t_stack **stack_a, t_stack **stack_b); -t_stack *ft_sort_2_nbrs(t_stack **stack); -t_stack *ft_sort_3_nbrs(t_stack **stack); -/* sorting functions */ +t_stack *ft_sort_two_nbrs(t_stack **stack); +t_stack *ft_sort_three_nbrs(t_stack **stack); +/* sorting functions if list has until 3 numbers */ + +void ft_sort_four_nbrs(t_stack **stack_a, t_stack **stack_b); +void ft_sort_five_or_more_nbr(t_stack **stack_a, t_stack **stack_b); +void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b); +int ft_lst_get_target(t_stack **stack_a, int b_end_pos, + int target_end_pos, int target_pos); +void ft_lst_get_target_position(t_stack **stack_a, t_stack **stack_b); +void ft_lst_map_all_indexers(t_stack **stack); +void ft_lst_map_actual_position(t_stack **stack); +void ft_lst_map_final_pos(t_stack **stack, int stack_size); +int ft_lst_map_lowest_final_position(t_stack **stack); +void ft_lst_shift_stack(t_stack **stack_a); +void ft_lst_get_cost(t_stack **stack_a, t_stack **stack_b); +void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b); +void ft_do_move_after_cheapest(t_stack **stack_a, t_stack **stack_b, + int cost_a, int cost_b); +/* sorting functions if list has 4 or more numbers */ void ft_do_swap_a(t_stack **stack); void ft_do_swap_b(t_stack **stack); void ft_do_swap_ab(t_stack **stack_a, t_stack **stack_b); -void ft_do_push_a(t_stack *stack_a, t_stack *stack_b); -void ft_do_push_b(t_stack *stack_a, t_stack *stack_b); +void ft_do_push_a(t_stack **stack_a, t_stack **stack_b); +void ft_do_push_b(t_stack **stack_a, t_stack **stack_b); +void ft_push(t_stack **source, t_stack **destiny); void ft_do_rotate_a(t_stack **stack); void ft_do_rotate_b(t_stack **stack); void ft_do_rotate_ab(t_stack **stack_a, t_stack **stack_b); diff --git a/program_to_test/src/ft_do_push_fts.c b/program_to_test/src/ft_do_push_fts.c index 79a95a1..d59923e 100644 --- a/program_to_test/src/ft_do_push_fts.c +++ b/program_to_test/src/ft_do_push_fts.c @@ -6,20 +6,20 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:24:04 by umeneses #+# #+# */ -/* Updated: 2024/06/04 18:28:18 by umeneses ### ########.fr */ +/* Updated: 2024/06/10 14:57:19 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void ft_do_push_a(t_stack *stack_a, t_stack *stack_b) +void ft_do_push_a(t_stack **stack_a, t_stack **stack_b) { - ft_push_a(&stack_a, &stack_b); + ft_push_a(stack_a, stack_b); ft_putstr_fd("pa\n", 1); } -void ft_do_push_b(t_stack *stack_a, t_stack *stack_b) +void ft_do_push_b(t_stack **stack_a, t_stack **stack_b) { - ft_push_b(&stack_a, &stack_b); + ft_push_b(stack_a, stack_b); ft_putstr_fd("pb\n", 1); } diff --git a/program_to_test/src/ft_do_sort.c b/program_to_test/src/ft_do_sort.c index 2689e1b..546a387 100644 --- a/program_to_test/src/ft_do_sort.c +++ b/program_to_test/src/ft_do_sort.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/06 18:46:55 by umeneses #+# #+# */ -/* Updated: 2024/06/08 14:41:50 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 15:07:03 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,36 @@ void ft_do_sort(t_stack **stack_a, t_stack **stack_b) { - (void)stack_b; + int stack_size; + stack_size = ft_lst_size(*stack_a); if (!ft_is_sorted(*stack_a)) { - if (ft_lst_size(*stack_a) == 2) - ft_sort_2_nbrs(stack_a); - if (ft_lst_size(*stack_a) == 3) - ft_sort_3_nbrs(stack_a); + if (stack_size == 2) + ft_sort_two_nbrs(stack_a); + else if (stack_size == 3) + ft_sort_three_nbrs(stack_a); + else if (stack_size == 4) + ft_sort_four_nbrs(stack_a, stack_b); + else if (stack_size >= 5) + { + ft_sort_five_or_more_nbr(stack_a, stack_b); + } + // else + // { + // ft_sort_five_or_more_nbr(stack_a, stack_b); + // } } } + +void ft_do_move_after_cheapest(t_stack **stack_a, t_stack **stack_b, + int cost_a, int cost_b) +{ + if (cost_a < 0 && cost_b < 0) + ft_do_reverse_rotate_ab(stack_a, stack_b); + else if (cost_a > 0 && cost_b > 0) + ft_do_rotate_ab(stack_a, stack_b); + ft_do_rotate_a(stack_a); + ft_do_rotate_b(stack_b); + ft_do_push_a(stack_a, stack_b); +} diff --git a/program_to_test/src/ft_lst_addto.c b/program_to_test/src/ft_lst_addto.c index ef3a16d..b237dfe 100644 --- a/program_to_test/src/ft_lst_addto.c +++ b/program_to_test/src/ft_lst_addto.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/08 18:05:43 by umeneses ### ########.fr */ +/* Updated: 2024/06/11 18:37:43 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ t_stack *ft_lst_addto_end(t_stack **stack, t_stack *new) t_stack *ft_lst_addto_begin(t_stack **stack, t_stack *new) { - t_stack *head; + t_stack *head; if (!new) return (*stack); diff --git a/program_to_test/src/ft_lst_goto.c b/program_to_test/src/ft_lst_goto.c index a88e880..4d22959 100644 --- a/program_to_test/src/ft_lst_goto.c +++ b/program_to_test/src/ft_lst_goto.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/08 17:26:58 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 15:01:57 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,5 +36,7 @@ t_stack *ft_lst_goto_head(t_stack *list) return (NULL); while (list->prev) list = list->prev; + if (list->prev == NULL) + return (list); return (list); } diff --git a/program_to_test/src/ft_lst_init.c b/program_to_test/src/ft_lst_init.c index b595335..1484d75 100644 --- a/program_to_test/src/ft_lst_init.c +++ b/program_to_test/src/ft_lst_init.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/06/10 10:35:43 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 16:35:21 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ int ft_lst_size(t_stack *stack) int size; size = 0; + if (!stack) + return (size); while (stack) { size++; @@ -54,16 +56,19 @@ t_stack *ft_lts_buildstack_argv(t_stack **stack, char **argv) return (*stack); } -void ft_lst_printf_int_content(char *msg, t_stack *list) +void ft_lst_printf_data_content(char *msg, t_stack *list) { t_stack *temp; list = ft_lst_goto_head(list); temp = list; - ft_printf("%s %d\n", msg, temp->nbr); - while (temp->next != NULL) + while (temp) { + ft_printf("%s\tnbr[%d]\torigin[%d]\t", msg, temp->nbr, temp->origin); + ft_printf("final_pos[%d]\ttarget[%d]\t", temp->final_pos, temp->target); + ft_printf("cost_a[%d]\tcost_b[%d]\n", temp->cost_a, temp->cost_b); temp = temp->next; - ft_printf("%s %d\n", msg, temp->nbr); } + if (list == NULL) + ft_printf("%s\tEmpty list\n", msg); } diff --git a/program_to_test/src/ft_lst_map_all.c b/program_to_test/src/ft_lst_map_all.c new file mode 100644 index 0000000..6bdde69 --- /dev/null +++ b/program_to_test/src/ft_lst_map_all.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_map_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: umeneses +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/13 18:41:22 by umeneses #+# #+# */ +/* Updated: 2024/06/18 13:53:02 by umeneses ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_lst_map_all_indexers(t_stack **stack) +{ + int stack_size; + + stack_size = ft_lst_size(*stack); + ft_lst_map_actual_position(stack); + ft_lst_map_final_pos(stack, stack_size); +} diff --git a/program_to_test/src/ft_lst_map_indexers.c b/program_to_test/src/ft_lst_map_indexers.c new file mode 100644 index 0000000..4d19562 --- /dev/null +++ b/program_to_test/src/ft_lst_map_indexers.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_map_indexers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: umeneses +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/10 10:34:17 by umeneses #+# #+# */ +/* Updated: 2024/06/19 11:51:52 by umeneses ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_lst_map_actual_position(t_stack **stack) +{ + int actual_position; + + actual_position = 0; + while ((*stack)->next != NULL) + { + actual_position++; + (*stack)->origin = actual_position; + (*stack) = (*stack)->next; + if ((*stack)->next == NULL) + { + actual_position++; + (*stack)->origin = actual_position; + } + } + *stack = ft_lst_goto_head(*stack); +} + +void ft_lst_map_final_pos(t_stack **stack, int stack_size) +{ + int final_pos; + t_stack *target; + t_stack *highest; + + while (--stack_size > 0) + { + target = *stack; + final_pos = INT_MIN; + highest = NULL; + while (target) + { + if (target->nbr == INT_MIN && target->final_pos == 0) + target->final_pos = 1; + if (target->nbr > final_pos && target->final_pos == 0) + { + final_pos = target->nbr; + highest = target; + target = *stack; + } + else + target = target->next; + } + if (highest != NULL) + highest->final_pos = stack_size; + } +} + +int ft_lst_get_target(t_stack **stack_a, int b_end_pos, + int target_end_pos, int target_pos) +{ + t_stack *temp_a; + + temp_a = *stack_a; + while (temp_a) + { + if (temp_a->final_pos > b_end_pos && temp_a->final_pos < target_end_pos) + { + target_end_pos = temp_a->final_pos; + target_pos = temp_a->origin; + } + temp_a = temp_a->next; + } + if (target_end_pos != INT_MAX) + return (target_pos); + temp_a = ft_lst_goto_head(temp_a); + while (temp_a) + { + if (temp_a->final_pos < target_end_pos) + { + target_end_pos = temp_a->final_pos; + target_pos = temp_a->origin; + } + temp_a = temp_a->next; + } + return (target_pos); +} + +void ft_lst_get_target_position(t_stack **stack_a, t_stack **stack_b) +{ + int target_position; + t_stack *temp_b; + + temp_b = *stack_b; + ft_lst_map_actual_position(stack_a); + ft_lst_map_actual_position(stack_b); + target_position = 0; + while (temp_b) + { + target_position = ft_lst_get_target(stack_a, temp_b->final_pos, + INT_MAX, target_position); + temp_b->target = target_position; + temp_b = temp_b->next; + } +} diff --git a/program_to_test/src/ft_push.c b/program_to_test/src/ft_push.c index 88fd1a2..da65341 100644 --- a/program_to_test/src/ft_push.c +++ b/program_to_test/src/ft_push.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/22 11:52:34 by umeneses #+# #+# */ -/* Updated: 2024/06/05 18:52:29 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 13:52:30 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,22 +14,48 @@ void ft_push_a(t_stack **stack_a, t_stack **stack_b) { - int tobe_pushed; + t_stack *to_be_pushed; - if (*stack_a == NULL || *stack_b == NULL) + if (NULL == *stack_b) return ; - tobe_pushed = (*stack_b)->nbr; - *stack_a = ft_lst_addto_begin(stack_a, ft_lst_init(tobe_pushed)); - *stack_b = ft_lst_delat_begin(*stack_b); + to_be_pushed = *stack_b; + *stack_b = (*stack_b)->next; + if (*stack_b) + (*stack_b)->prev = NULL; + if (NULL == *stack_a) + { + *stack_a = to_be_pushed; + to_be_pushed->next = NULL; + } + else + { + to_be_pushed->next = *stack_a; + to_be_pushed->next->prev = to_be_pushed; + *stack_a = to_be_pushed; + } } void ft_push_b(t_stack **stack_a, t_stack **stack_b) { - int tobe_pushed; + t_stack *to_be_pushed; - if (*stack_a == NULL || *stack_b == NULL) + if (NULL == *stack_a) return ; - tobe_pushed = (*stack_a)->nbr; - *stack_b = ft_lst_addto_begin(stack_b, ft_lst_init(tobe_pushed)); - *stack_a = ft_lst_delat_begin(*stack_a); + to_be_pushed = (*stack_a); + (*stack_a) = (*stack_a)->next; + if ((*stack_a)) + (*stack_a)->prev = NULL; + if (NULL == (*stack_b)) + { + (*stack_b) = to_be_pushed; + (*stack_b)->next = NULL; + (*stack_b)->prev = NULL; + } + else + { + to_be_pushed->next = (*stack_b); + to_be_pushed->next->prev = to_be_pushed; + (*stack_b) = to_be_pushed; + } + (*stack_b) = ft_lst_goto_head(*stack_b); } diff --git a/program_to_test/src/ft_rotate.c b/program_to_test/src/ft_rotate.c index f7136b7..f2c0d08 100644 --- a/program_to_test/src/ft_rotate.c +++ b/program_to_test/src/ft_rotate.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/23 11:48:11 by umeneses #+# #+# */ -/* Updated: 2024/06/05 18:53:01 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 13:45:59 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,16 @@ */ void ft_rotate(t_stack **stack) { - t_stack *temp; t_stack *last_node; - if (*stack == NULL || (*stack)->next == NULL) + if (NULL == *stack || NULL== stack) return ; - temp = *stack; + last_node = ft_lst_goto_end(*stack); + last_node->next = *stack; *stack = (*stack)->next; (*stack)->prev = NULL; - last_node = ft_lst_goto_end(*stack); - temp->next = NULL; - last_node->next = temp; + last_node->next->prev = last_node; + last_node->next->next = NULL; } void ft_rotate_ab(t_stack **stack_a, t_stack **stack_b) @@ -41,13 +40,16 @@ void ft_rotate_ab(t_stack **stack_a, t_stack **stack_b) void ft_reverse_rotate(t_stack **stack) { - int temp; + t_stack *last_node; - if (*stack == NULL || (*stack)->next == NULL) + if (NULL == *stack || NULL == (*stack)->next || ft_lst_size(*stack) < 2) return ; - temp = ft_lst_goto_end(*stack)->nbr; - *stack = ft_lst_addto_begin(stack, ft_lst_init(temp)); - *stack = ft_lst_delat_end(*stack); + last_node = ft_lst_goto_end(*stack); + last_node->prev->next = NULL; + last_node->next = (*stack); + last_node->prev = NULL; + *stack = last_node; + (*stack)->next->prev = last_node; } void ft_reverse_rotate_ab(t_stack **stack_a, t_stack **stack_b) diff --git a/program_to_test/src/ft_sort_5_or_more.c b/program_to_test/src/ft_sort_5_or_more.c new file mode 100644 index 0000000..ca4ac92 --- /dev/null +++ b/program_to_test/src/ft_sort_5_or_more.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_5_or_more.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: umeneses +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ +/* Updated: 2024/06/19 15:06:57 by umeneses ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_sort_five_or_more_nbr(t_stack **stack_a, t_stack **stack_b) +{ + ft_push_b_all_but_three(stack_a, stack_b); + ft_sort_three_nbrs(stack_a); + while (*stack_b) + { + ft_lst_get_target_position(stack_a, stack_b); + ft_lst_get_cost(stack_a, stack_b); + ft_lst_do_cheapest_move(stack_a, stack_b); + } + if (!ft_is_sorted(*stack_a)) + ft_lst_shift_stack(stack_a); +} + +void ft_lst_get_cost(t_stack **stack_a, t_stack **stack_b) +{ + int size_a; + int size_b; + t_stack *temp_a; + t_stack *temp_b; + + temp_a = *stack_a; + temp_b = *stack_b; + size_a = ft_lst_size(temp_a); + size_b = ft_lst_size(temp_b); + while (temp_b) + { + temp_b->cost_b = temp_b->origin; + if (temp_b->origin > size_b / 2) + temp_b->cost_b = (size_b - temp_b->origin) * -1; + temp_b->cost_a = temp_b->target; + if (temp_b->target > size_a / 2) + temp_b->cost_a = (size_a - temp_b->target) * -1; + temp_b = temp_b->next; + } +} + +void ft_lst_do_cheapest_move(t_stack **stack_a, t_stack **stack_b) +{ + int cost_a; + int cost_b; + int cheapest; + t_stack *temp_b; + + temp_b = *stack_b; + cheapest = INT_MAX; + while (temp_b) + { + if ((temp_b->cost_a + temp_b->cost_b) < cheapest) + { + cheapest = temp_b->cost_b + temp_b->cost_a; + cost_a = temp_b->cost_a; + cost_b = temp_b->cost_b; + } + temp_b = temp_b->next; + } + ft_do_move_after_cheapest(stack_a, stack_b, cost_a, cost_b); +} diff --git a/program_to_test/src/ft_sort_until3.c b/program_to_test/src/ft_sort_until3.c index f002d2a..47fe819 100644 --- a/program_to_test/src/ft_sort_until3.c +++ b/program_to_test/src/ft_sort_until3.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/04 12:19:14 by umeneses #+# #+# */ -/* Updated: 2024/06/07 10:39:41 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 14:38:08 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,12 @@ bool ft_is_sorted(t_stack *list) { - t_stack *tmp; + t_stack *temp; while (list->next != NULL) { - tmp = list->next; - if (list->nbr > tmp->nbr) + temp = list->next; + if (list->nbr > temp->nbr) return (false); list = list->next; } @@ -28,35 +28,42 @@ bool ft_is_sorted(t_stack *list) return (true); } -t_stack *ft_sort_2_nbrs(t_stack **list) +t_stack *ft_sort_two_nbrs(t_stack **list) { if ((*list)->nbr > (*list)->next->nbr) ft_do_swap_a(list); return (*list); } -t_stack *ft_sort_3_nbrs(t_stack **list) +t_stack *ft_sort_three_nbrs(t_stack **list) { - while (!ft_is_sorted(*list)) + int highest_pos; + + *list = ft_lst_goto_head(*list); + if (ft_is_sorted(*list)) + return (*list); + highest_pos = ft_lst_map_highest_pos(*list); + if ((*list)->final_pos == highest_pos) + ft_do_rotate_a(list); + else if ((*list)->next->final_pos == highest_pos) + ft_do_reverse_rotate_a(list); + if ((*list)->final_pos > (*list)->next->final_pos) + ft_do_swap_a(list); + return (*list); +} + +int ft_lst_map_highest_pos(t_stack *list) +{ + int highest_pos; + t_stack *temp; + + temp = ft_lst_goto_head(list); + highest_pos = temp->final_pos; + while (temp) { - if (((*list)->nbr < (*list)->next->nbr) - && ((*list)->nbr < ft_lst_goto_end(*list)->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_reverse_rotate_a(list); - else if (((*list)->nbr < (*list)->next->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_reverse_rotate_a(list); - else if (((*list)->nbr > (*list)->next->nbr) - && ((*list)->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->next->nbr < ft_lst_goto_end(*list)->nbr)) - ft_do_rotate_a(list); - else if (((*list)->nbr > ft_lst_goto_end(*list)->nbr) - && ((*list)->nbr > (*list)->next->nbr) - && ((*list)->next->nbr > ft_lst_goto_end(*list)->nbr)) - ft_do_rotate_a(list); - else - ft_do_swap_a(list); + if (temp->final_pos > highest_pos) + highest_pos = temp->final_pos; + temp = temp->next; } - return (*list); + return (highest_pos); } diff --git a/program_to_test/src/ft_sort_until4.c b/program_to_test/src/ft_sort_until4.c new file mode 100644 index 0000000..0b29d19 --- /dev/null +++ b/program_to_test/src/ft_sort_until4.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sort_until5.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: umeneses +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/10 12:41:29 by umeneses #+# #+# */ +/* Updated: 2024/06/19 14:56:16 by umeneses ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_sort_four_nbrs(t_stack **stack_a, t_stack **stack_b) +{ + int highest_pos; + + ft_push_b_all_but_three(stack_a, stack_b); + ft_sort_three_nbrs(stack_a); + highest_pos = ft_lst_map_highest_pos(*stack_b); + if ((*stack_b)->final_pos == highest_pos) + { + ft_do_push_a(stack_a, stack_b); + ft_do_rotate_a(stack_a); + } + if (((*stack_a)->final_pos == 0) && ((*stack_a)->next->final_pos == 2) + && ((*stack_a)->next->next->final_pos == 3)) + { + ft_do_reverse_rotate_a(stack_a); + ft_do_swap_a(stack_a); + } + if (!ft_is_sorted(*stack_a)) + ft_lst_shift_stack(stack_a); +} + +void ft_push_b_all_but_three(t_stack **stack_a, t_stack **stack_b) +{ + int stack_size; + int pushed; + int index; + + stack_size = ft_lst_size(*stack_a); + pushed = 0; + index = 0; + while ((stack_size > 6) && (index < stack_size) + && (pushed < stack_size / 2)) + { + if ((*stack_a)->final_pos <= (stack_size / 2)) + { + ft_do_push_b(stack_a, stack_b); + pushed++; + } + else + ft_do_rotate_a(stack_a); + index++; + } + while ((stack_size - pushed) > 3) + { + ft_do_push_b(stack_a, stack_b); + pushed++; + } +} + +void ft_lst_shift_stack(t_stack **stack_a) +{ + int lowest_final_pos; + int stack_size; + + stack_size = ft_lst_size(*stack_a); + lowest_final_pos = ft_lst_map_lowest_final_position(stack_a); + if (lowest_final_pos < stack_size / 2) + { + while (lowest_final_pos < stack_size) + { + ft_do_reverse_rotate_a(stack_a); + lowest_final_pos++; + } + } + if (ft_lst_goto_end(*stack_a)->final_pos == 0) + ft_do_reverse_rotate_a(stack_a); + else + { + while (lowest_final_pos > 0) + { + ft_do_rotate_a(stack_a); + lowest_final_pos--; + } + } +} + +int ft_lst_map_lowest_final_position(t_stack **stack) +{ + int lowest_final_pos; + int lowest_position; + t_stack *temp; + + temp = *stack; + lowest_final_pos = INT_MAX; + ft_lst_map_actual_position(stack); + lowest_position = temp->origin; + while (temp) + { + if (temp->final_pos < lowest_final_pos) + { + lowest_final_pos = temp->final_pos; + lowest_position = temp->origin; + } + temp = temp->next; + } + return (lowest_position); +} diff --git a/program_to_test/src/ft_swap.c b/program_to_test/src/ft_swap.c index 6e8a439..e13bdef 100644 --- a/program_to_test/src/ft_swap.c +++ b/program_to_test/src/ft_swap.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/13 14:49:58 by umeneses #+# #+# */ -/* Updated: 2024/05/22 15:35:10 by umeneses ### ########.fr */ +/* Updated: 2024/06/19 14:37:01 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,23 @@ void ft_swap(t_stack **stack) { - int temp; + // int temp; - if (*stack == NULL || (*stack)->next == NULL) + // if (NULL == stack || NULL == (*stack)) + // return ; + // temp = (*stack)->nbr; + // (*stack)->nbr = (*stack)->next->nbr; + // (*stack)->next->nbr = temp; + + if (NULL == stack || NULL == (*stack)) return ; - temp = (*stack)->nbr; - (*stack)->nbr = (*stack)->next->nbr; - (*stack)->next->nbr = temp; + (*stack) = (*stack)->next; + (*stack)->prev->prev = (*stack); + (*stack)->prev->next = (*stack)->next; + if ((*stack)->next) + (*stack)->next->prev = (*stack)->prev; + (*stack)->next = (*stack)->prev; + (*stack)->prev = NULL; } void ft_swap_ab(t_stack **stack_a, t_stack **stack_b) diff --git a/program_to_test/src/push_main.c b/program_to_test/src/push_main.c index a9f16f5..c553b2a 100644 --- a/program_to_test/src/push_main.c +++ b/program_to_test/src/push_main.c @@ -6,7 +6,7 @@ /* By: umeneses +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 18:34:31 by umeneses #+# #+# */ -/* Updated: 2024/06/08 14:51:33 by umeneses ### ########.fr */ +/* Updated: 2024/06/18 18:16:09 by umeneses ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,15 @@ int main(int argc, char **argv) t_stack *stack_b; if (argc <= 2) - ft_error_msg("Not enough arguments. Please provide at leats 2 numbers"); + ft_error_msg("Not enough arguments. Please provide at least 2 numbers"); stack_a = NULL; stack_b = NULL; stack_a = ft_lts_buildstack_argv(&stack_a, argv); - ft_lst_printf_int_content("testing main content:", stack_a); - ft_printf("testing main size: %d\n", ft_lst_size(stack_a)); + ft_lst_map_all_indexers(&stack_a); + ft_lst_printf_data_content("original main stack_a:", stack_a); ft_do_sort(&stack_a, &stack_b); - ft_lst_printf_int_content("updated main content:", stack_a); - ft_printf("stack_a pos na main = %d\n", stack_a->pos); + ft_lst_printf_data_content("updated main stack_a:", stack_a); + ft_lst_printf_data_content("updated main stack_b:", stack_b); ft_lstclear_single_ptr(stack_a); ft_lstclear_single_ptr(stack_b); return (0);