Skip to content

Commit

Permalink
Merge pull request #9 from biralavor/bug/fixing_3rd_step
Browse files Browse the repository at this point in the history
Bug/fixing 3rd step
  • Loading branch information
biralavor authored Jun 26, 2024
2 parents adabc36 + a29b495 commit a1be660
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 123 deletions.
4 changes: 2 additions & 2 deletions _ci_tdd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/05/10 10:09:44 by umeneses #+# #+# #
# Updated: 2024/06/19 17:28:23 by umeneses ### ########.fr #
# Updated: 2024/06/20 12:50:07 by umeneses ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -75,7 +75,7 @@ COMP_EXE = $(CC) $(LDFLAGS) $(OBJ_FILES) $(LDLIBS) -o $(NAME)
# **************************************************************************** #

define arguments
42 1 10 21
42 1 10 21 11 77 666
endef

define bonus
Expand Down
126 changes: 89 additions & 37 deletions _ci_tdd/test_files/test_main_miunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 19:06:12 by umeneses #+# #+# */
/* Updated: 2024/06/19 17:10:18 by umeneses ### ########.fr */
/* Updated: 2024/06/26 14:06:06 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,6 +28,7 @@
#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_do_move_with_cost.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"
Expand Down Expand Up @@ -66,19 +67,20 @@ MU_TEST(test_3rd_miastep_get_cost)
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_top_a = ft_atoi(argv_simulation[5]);
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]
expected_cost = 2; // stack_b[3]->nbr = 4
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;
actual_cost = stack_b->next->cost_b;
actual_bottom_a = ft_lst_goto_end(stack_a)->nbr;
actual_cost = stack_b->next->next->cost_b;

// ASSERT
mu_assert_int_eq(expected_cost, actual_cost);
mu_assert_int_eq(expected_size, actual_size);
Expand All @@ -98,38 +100,38 @@ MU_TEST(test_3rd_miastep_map_get_target_position)
char *userinput;
int expected_size;
int actual_size;
int expected_top_a;
int actual_top_a;
int expected_top_b;
int actual_top_b;
int expected_bottom_a;
int actual_bottom_a;
// int expected_target_pos;
// int actual_target_pos;
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_top_b = ft_atoi(argv_simulation[7]);
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]
expected_target_pos = 2; // should be stack_b->target
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;

actual_bottom_a = ft_lst_goto_end(stack_a)->nbr;
actual_top_b = ft_lst_goto_head(stack_b)->nbr;
actual_target_pos = stack_b->target;

// ASSERT
// mu_assert_int_eq(expected_target_pos, actual_target_pos);
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_top_b, actual_top_b);
mu_assert_int_eq(expected_bottom_a, actual_bottom_a);
ft_lstclear_single_ptr(stack_a);
ft_lstclear_single_ptr(stack_b);
Expand Down Expand Up @@ -164,15 +166,24 @@ MU_TEST(test_3rd_miastep_map_lowest_final_pos)
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]);
expected_lowest_final_pos_pos = 2; //should be stack->origin[2]
expected_lowest_final_pos_nbr = ft_atoi(argv_simulation[3]); // argv[3] = 1;

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;

actual_lowest_final_pos_nbr = 0;
while (stack)
{
if (stack->origin == 2)
{
actual_lowest_final_pos_pos = stack->origin;
actual_lowest_final_pos_nbr = stack->nbr;
break ;
}
stack = stack->next;
}

// ASSERT
mu_assert_int_eq(expected_lowest_final_pos_pos, actual_lowest_final_pos_pos);
Expand All @@ -184,7 +195,44 @@ MU_TEST(test_3rd_miastep_map_lowest_final_pos)
ft_array_clear(argv_simulation, expected_size);
}

MU_TEST(test_sort_four_nbrs_v05)
{
// 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 2 3 1 0";
argv_simulation = ft_split(userinput, ' ');
expected_size = ft_minunit_array_counter(argv_simulation);
expected_top_a = ft_atoi(argv_simulation[4]);
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_do_sort(&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_v04)
{
Expand All @@ -211,12 +259,10 @@ MU_TEST(test_sort_four_nbrs_v04)

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);
Expand Down Expand Up @@ -488,25 +534,27 @@ MU_TEST(test_1st_miastep_final_pos_index)

// ACT
stack = NULL;
userinput = "./push_swap 2 42 0 4 8 3 31 27";
userinput = "./push_swap 1 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_highest_final_pos = expected_size - 1;
expected_bottom_a = ft_atoi(argv_simulation[9]);
expected_highest_final_pos = expected_size;

stack = ft_lts_buildstack_argv(&stack, argv_simulation);
ft_lst_map_actual_position(&stack);
ft_lst_map_final_pos(&stack, expected_size);

ft_lst_map_final_pos(&stack, expected_size + 1);
actual_size = ft_lst_size(stack);
actual_top_a = ft_lst_goto_head(stack)->nbr;

while (stack->origin <= 2)
actual_highest_final_pos = 0;
while (stack)
{
stack = stack->next;
if (stack->origin == 2)
{
actual_highest_final_pos = stack->final_pos;
break ;
}
stack = stack->next;
}
actual_bottom_a = ft_lst_goto_end(stack)->nbr;

Expand Down Expand Up @@ -536,23 +584,26 @@ MU_TEST(test_1st_miastep_pos_index)

// ACT
stack = NULL;
userinput = "./push_swap 2 42 0 4 8 3 31 27";
userinput = "./push_swap 2 42 0 4 8 3 31 27 1 100 200 150 80";
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]);
expected_bottom_a = ft_atoi(argv_simulation[13]);
expected_fith_position = ft_atoi(argv_simulation[6]);

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)
actual_fith_position = 0;
while (stack)
{
stack = stack->next;
if (stack->origin == 5)
{
actual_fith_position = stack->nbr;
break ;
}
stack = stack->next;
}
actual_bottom_a = ft_lst_goto_end(stack)->nbr;
// ASSERT
Expand Down Expand Up @@ -1676,6 +1727,7 @@ MU_TEST_SUITE(sorting_4_nbrs_tests)
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_RUN_TEST(test_sort_four_nbrs_v05);
}

MU_TEST_SUITE(miacombeau_3rd_step_tests)
Expand Down
3 changes: 2 additions & 1 deletion program_to_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/05/09 17:59:28 by umeneses #+# #+# #
# Updated: 2024/06/19 15:06:51 by umeneses ### ########.fr #
# Updated: 2024/06/26 12:23:15 by umeneses ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -51,6 +51,7 @@ SRC_FILES += ft_do_push_fts.c
SRC_FILES += ft_do_rev_rotate_fts.c
SRC_FILES += ft_do_rotate_fts.c
SRC_FILES += ft_do_swap_fts.c
SRC_FILES += ft_do_move_with_cost.c
SRC_FILES += ft_lst_addto.c
SRC_FILES += ft_lst_clear.c
SRC_FILES += ft_lst_deletes.c
Expand Down
11 changes: 10 additions & 1 deletion program_to_test/headers/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 15:23:33 by umeneses #+# #+# */
/* Updated: 2024/06/19 15:07:14 by umeneses ### ########.fr */
/* Updated: 2024/06/26 12:21:53 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -80,11 +80,20 @@ 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);
int abs_nbr(int nbr);
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_rev_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b,
int *cost_a, int *cost_b);
void ft_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b,
int *cost_a, int *cost_b);
void ft_rotate_a_with_cost(t_stack **stack_a, int *cost_a);
void ft_rotate_b_with_cost(t_stack **stack_b, int *cost_b);
/* sorting functions take takes the cheapest cost and do the move */

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);
Expand Down
69 changes: 69 additions & 0 deletions program_to_test/src/ft_do_move_with_cost.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_do_move_with_cost.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: umeneses <umeneses@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 12:16:53 by umeneses #+# #+# */
/* Updated: 2024/06/26 12:20:49 by umeneses ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void ft_rev_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b,
int *cost_a, int *cost_b)
{
while (*cost_a < 0 && *cost_b < 0)
{
(*cost_a)++;
(*cost_b)++;
ft_do_reverse_rotate_ab(stack_a, stack_b);
}
}

void ft_rotate_ab_with_cost(t_stack **stack_a, t_stack **stack_b,
int *cost_a, int *cost_b)
{
while (*cost_a > 0 && *cost_b > 0)
{
(*cost_a)--;
(*cost_b)--;
ft_do_rotate_ab(stack_a, stack_b);
}
}

void ft_rotate_a_with_cost(t_stack **stack_a, int *cost_a)
{
while (*cost_a)
{
if (*cost_a > 0)
{
ft_do_rotate_a(stack_a);
(*cost_a)--;
}
else if (*cost_a < 0)
{
ft_do_reverse_rotate_a(stack_a);
(*cost_a)++;
}
}
}

void ft_rotate_b_with_cost(t_stack **stack_b, int *cost_b)
{
while (*cost_b)
{
if (*cost_b > 0)
{
ft_do_rotate_a(stack_b);
(*cost_b)--;
}
else if (*cost_b < 0)
{
ft_do_reverse_rotate_a(stack_b);
(*cost_b)++;
}
}
}
Loading

0 comments on commit a1be660

Please sign in to comment.