diff --git a/Makefile b/Makefile index c6495a7..514a2ac 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,8 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/sort/large_sort/large_sort.c \ $(SRC_DIR)/sort/large_sort/push_b_segmented.c \ $(SRC_DIR)/sort/large_sort/set_cost.c \ - $(SRC_DIR)/sort/large_sort/set_opt.c \ + $(SRC_DIR)/sort/large_sort/set_min_cost_opt_method.c \ + $(SRC_DIR)/sort/large_sort/set_is_target.c \ $(SRC_DIR)/sort/large_sort/greedy_operation.c \ $(SRC_DIR)/stack_operations/push.c \ $(SRC_DIR)/stack_operations/swap.c \ @@ -65,7 +66,8 @@ TEST_SRC = $(TEST_DIR)/test_check_args.cpp \ $(TEST_DIR)/test_swap_stack.cpp \ $(TEST_DIR)/test_push_b_segmented.cpp \ $(TEST_DIR)/test_set_cost.cpp \ - $(TEST_DIR)/test_set_opt_method.cpp \ + $(TEST_DIR)/test_set_min_cost_opt_method.cpp \ + $(TEST_DIR)/test_set_is_target.cpp \ $(TEST_DIR)/test_greedy_operation.cpp \ $(TEST_DIR)/test_is_sorted_stack.cpp \ $(TEST_DIR)/test_sort.cpp diff --git a/include/large_sort.h b/include/large_sort.h index 163fea3..7f637a7 100644 --- a/include/large_sort.h +++ b/include/large_sort.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:36:38 by reasuke #+# #+# */ -/* Updated: 2024/02/12 18:32:55 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 16:41:29 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,8 @@ void large_sort(t_stack **p_a, t_stack **p_b); void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int num_seg); void set_cost(t_stack **p_a, t_stack **p_b); -void set_opt(t_stack **p_b); +void set_min_cost_opt_method(t_stack **p_b); +void set_is_target(t_stack **p_b); void greedy_operation(t_stack **p_a, t_stack **p_b); #endif diff --git a/include/types.h b/include/types.h index 2788114..ba6481c 100644 --- a/include/types.h +++ b/include/types.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:57:41 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:58:04 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 16:40:21 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,21 +18,22 @@ typedef t_list t_stack; typedef enum e_method { INIT, - FF, - FR, - RF, - RR, + RA_RB, + RRA_RB, + RA_RRB, + RRA_RRB, } t_method; typedef struct s_content { int index; - int sf_cost; - int sr_cost; - int if_cost; - int ir_cost; + int segment; + int rb_cost; + int rrb_cost; + int ra_cost; + int rra_cost; int min_cost; t_method opt_method; - bool is_opt; + bool is_target; } t_content; #endif diff --git a/src/initialization/generate_stack.c b/src/initialization/generate_stack.c index 3e4ae48..37da8cf 100644 --- a/src/initialization/generate_stack.c +++ b/src/initialization/generate_stack.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */ -/* Updated: 2024/02/13 14:01:23 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 14:28:39 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,6 +103,7 @@ t_stack *generate_stack(int argc, char **argv) if (!ptr) exit_with_error(); ptr->index = comp[i]; + ptr->segment = -1; ft_lstadd_back(&st, ft_lstnew(ptr)); i++; } diff --git a/src/sort/large_sort/greedy_operation.c b/src/sort/large_sort/greedy_operation.c index becf669..b7914d9 100644 --- a/src/sort/large_sort/greedy_operation.c +++ b/src/sort/large_sort/greedy_operation.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:53:46 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 16:40:21 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ static t_stack *_find_opt_st_b(t_stack **p_b) t_stack *st_b; st_b = *p_b; - while (!get_content(st_b)->is_opt) + while (!get_content(st_b)->is_target) st_b = st_b->next; return (st_b); } @@ -32,11 +32,11 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b, int if_abs; int ir_abs; - sf_abs = ft_abs(get_content(opt_st_b)->sf_cost); - sr_abs = ft_abs(get_content(opt_st_b)->sr_cost); - if_abs = ft_abs(get_content(opt_st_b)->if_cost); - ir_abs = ft_abs(get_content(opt_st_b)->ir_cost); - if (get_content(opt_st_b)->opt_method == FF) + sf_abs = ft_abs(get_content(opt_st_b)->rb_cost); + sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost); + if_abs = ft_abs(get_content(opt_st_b)->ra_cost); + ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); + if (get_content(opt_st_b)->opt_method == RA_RB) { do_double_n_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr); if (sf_abs > if_abs) @@ -44,7 +44,7 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b, else if (sf_abs < if_abs) do_single_n_operations(p_a, if_abs - sf_abs, operate_ra); } - if (get_content(opt_st_b)->opt_method == RR) + if (get_content(opt_st_b)->opt_method == RRA_RRB) { do_double_n_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr); if (sr_abs > ir_abs) @@ -61,16 +61,16 @@ static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b) int if_abs; int ir_abs; - sf_abs = ft_abs(get_content(opt_st_b)->sf_cost); - sr_abs = ft_abs(get_content(opt_st_b)->sr_cost); - if_abs = ft_abs(get_content(opt_st_b)->if_cost); - ir_abs = ft_abs(get_content(opt_st_b)->ir_cost); - if (get_content(opt_st_b)->opt_method == FR) + sf_abs = ft_abs(get_content(opt_st_b)->rb_cost); + sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost); + if_abs = ft_abs(get_content(opt_st_b)->ra_cost); + ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); + if (get_content(opt_st_b)->opt_method == RRA_RB) { do_single_n_operations(p_b, sf_abs, operate_rb); do_single_n_operations(p_a, ir_abs, operate_rra); } - if (get_content(opt_st_b)->opt_method == RF) + if (get_content(opt_st_b)->opt_method == RA_RRB) { do_single_n_operations(p_b, sr_abs, operate_rrb); do_single_n_operations(p_a, if_abs, operate_ra); @@ -82,9 +82,9 @@ static void _do_opt_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b) t_method opt_method; opt_method = get_content(opt_st_b)->opt_method; - if (opt_method == FF || opt_method == RR) + if (opt_method == RA_RB || opt_method == RRA_RRB) _do_alined_operation(p_a, p_b, opt_st_b); - else if (opt_method == FR || opt_method == RF) + else if (opt_method == RRA_RB || opt_method == RA_RRB) _do_mixed_operation(p_a, p_b, opt_st_b); operate_pa(p_b, p_a); } diff --git a/src/sort/large_sort/large_sort.c b/src/sort/large_sort/large_sort.c index 7683f8d..a080e83 100644 --- a/src/sort/large_sort/large_sort.c +++ b/src/sort/large_sort/large_sort.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ -/* Updated: 2024/02/13 13:28:59 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 16:41:29 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,8 @@ void large_sort(t_stack **p_a, t_stack **p_b) while (size_b--) { set_cost(p_a, p_b); - set_opt(p_b); + set_min_cost_opt_method(p_b); + set_is_target(p_b); greedy_operation(p_a, p_b); } _sort_stack_a(p_a, size_a); diff --git a/src/sort/large_sort/push_b_segmented.c b/src/sort/large_sort/push_b_segmented.c index c383255..ab153ed 100644 --- a/src/sort/large_sort/push_b_segmented.c +++ b/src/sort/large_sort/push_b_segmented.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 22:57:28 by reasuke #+# #+# */ -/* Updated: 2024/02/13 15:01:23 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 14:31:29 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) { int pushed; int index; + int segment; pushed = 0; while (pushed < n) @@ -69,8 +70,10 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) index = get_content(*p_a)->index; if (_should_push_b(index, pushed, n, segs)) { + segment = _calc_segment_id(index, n, segs); + get_content(*p_a)->segment = segment; operate_pb(p_a, p_b); - if (_calc_segment_id(index, n, segs) % 2 == 1) + if (segment % 2 == 1) operate_rb(p_b); pushed++; } diff --git a/src/sort/large_sort/set_cost.c b/src/sort/large_sort/set_cost.c index 7c97854..3050838 100644 --- a/src/sort/large_sort/set_cost.c +++ b/src/sort/large_sort/set_cost.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/08 12:17:33 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:53:50 by reasuke ### ########.fr */ +/* Updated: 2024/02/14 14:15:57 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,14 +71,14 @@ static void _set_insertion_cost(t_stack **p_a, t_stack *st_b) max_index = _calc_max_index(p_a); if (threshold == INT_MAX) threshold = (max_index + 1) % size_a; - get_content(st_b)->if_cost = threshold % size_a; - get_content(st_b)->ir_cost = -((size_a - threshold) % size_a); + get_content(st_b)->ra_cost = threshold % size_a; + get_content(st_b)->rra_cost = -((size_a - threshold) % size_a); } static void _set_selection_cost(t_stack *st_b, int i, int size_b) { - get_content(st_b)->sf_cost = i; - get_content(st_b)->sr_cost = -((size_b - i) % size_b); + get_content(st_b)->rb_cost = i; + get_content(st_b)->rrb_cost = -((size_b - i) % size_b); } void set_cost(t_stack **p_a, t_stack **p_b) diff --git a/src/sort/large_sort/set_is_target.c b/src/sort/large_sort/set_is_target.c new file mode 100644 index 0000000..d26ebd7 --- /dev/null +++ b/src/sort/large_sort/set_is_target.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_is_target.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/14 14:44:38 by reasuke #+# #+# */ +/* Updated: 2024/02/14 16:42:11 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "large_sort.h" +#include "push_swap.h" + +static int _calc_max_segment(t_stack *st_b) +{ + int max_seg; + + max_seg = INT_MIN; + while (st_b) + { + ft_chmax(&max_seg, get_content(st_b)->segment); + st_b = st_b->next; + } + return (max_seg); +} + +static int _calc_target_index(t_stack *st_b, int max_seg) +{ + int target_index; + int cost; + int i; + + target_index = -1; + cost = INT_MAX; + i = 0; + while (st_b) + { + if (get_content(st_b)->segment == max_seg && ft_chmin(&cost, + get_content(st_b)->min_cost)) + target_index = i; + i++; + st_b = st_b->next; + } + return (target_index); +} + +void set_is_target(t_stack **p_b) +{ + int i; + t_stack *st_b; + int max_seg; + int target_index; + + st_b = *p_b; + max_seg = _calc_max_segment(st_b); + target_index = _calc_target_index(st_b, max_seg); + i = 0; + while (i++ < target_index) + st_b = st_b->next; + get_content(st_b)->is_target = true; +} diff --git a/src/sort/large_sort/set_min_cost_opt_method.c b/src/sort/large_sort/set_min_cost_opt_method.c new file mode 100644 index 0000000..4b88be0 --- /dev/null +++ b/src/sort/large_sort/set_min_cost_opt_method.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_min_cost_opt_method.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 10:23:28 by reasuke #+# #+# */ +/* Updated: 2024/02/14 15:44:15 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "large_sort.h" +#include "push_swap.h" + +static int _calc_cost(t_stack *st_b, t_method method) +{ + int rb_cost; + int rrb_cost; + int ra_cost; + int rra_cost; + + rb_cost = get_content(st_b)->rb_cost; + rrb_cost = get_content(st_b)->rrb_cost; + ra_cost = get_content(st_b)->ra_cost; + rra_cost = get_content(st_b)->rra_cost; + if (method == RA_RB) + return (ft_max(rb_cost, ra_cost)); + else if (method == RRA_RB) + return (rb_cost - rra_cost); + else if (method == RA_RRB) + return (ra_cost - rrb_cost); + else if (method == RRA_RRB) + return (ft_max(-rrb_cost, -rra_cost)); + return (-1); +} + +static void _set_opt_method(t_stack *st_b, int *cost) +{ + if (ft_chmin(cost, _calc_cost(st_b, RA_RB))) + get_content(st_b)->opt_method = RA_RB; + if (ft_chmin(cost, _calc_cost(st_b, RRA_RB))) + get_content(st_b)->opt_method = RRA_RB; + if (ft_chmin(cost, _calc_cost(st_b, RA_RRB))) + get_content(st_b)->opt_method = RA_RRB; + if (ft_chmin(cost, _calc_cost(st_b, RRA_RRB))) + get_content(st_b)->opt_method = RRA_RRB; +} + +void set_min_cost_opt_method(t_stack **p_b) +{ + int i; + int size_b; + int cost; + t_stack *st_b; + + i = 0; + size_b = stack_size(*p_b); + st_b = *p_b; + while (i < size_b) + { + cost = INT_MAX; + _set_opt_method(st_b, &cost); + get_content(st_b)->min_cost = cost; + st_b = st_b->next; + i++; + } +} diff --git a/src/sort/large_sort/set_opt.c b/src/sort/large_sort/set_opt.c deleted file mode 100644 index 9bcd391..0000000 --- a/src/sort/large_sort/set_opt.c +++ /dev/null @@ -1,101 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* set_opt.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: reasuke +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/09 10:23:28 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:53:55 by reasuke ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "large_sort.h" -#include "push_swap.h" - -static int _calc_cost(t_stack *st_b, t_method method) -{ - int sf_cost; - int sr_cost; - int if_cost; - int ir_cost; - - sf_cost = get_content(st_b)->sf_cost; - sr_cost = get_content(st_b)->sr_cost; - if_cost = get_content(st_b)->if_cost; - ir_cost = get_content(st_b)->ir_cost; - if (method == FF) - return (ft_max(sf_cost, if_cost)); - else if (method == FR) - return (sf_cost - ir_cost); - else if (method == RF) - return (if_cost - sr_cost); - else if (method == RR) - return (ft_max(-sr_cost, -ir_cost)); - return (-1); -} - -static void _set_opt_method(t_stack *st_b, int *cost) -{ - if (ft_chmin(cost, _calc_cost(st_b, FF))) - get_content(st_b)->opt_method = FF; - if (ft_chmin(cost, _calc_cost(st_b, FR))) - get_content(st_b)->opt_method = FR; - if (ft_chmin(cost, _calc_cost(st_b, RF))) - get_content(st_b)->opt_method = RF; - if (ft_chmin(cost, _calc_cost(st_b, RR))) - get_content(st_b)->opt_method = RR; -} - -static void _set_min_cost_opt_method(t_stack **p_b) -{ - int i; - int size_b; - int cost; - t_stack *st_b; - - i = 0; - size_b = stack_size(*p_b); - st_b = *p_b; - while (i < size_b) - { - cost = INT_MAX; - _set_opt_method(st_b, &cost); - get_content(st_b)->min_cost = cost; - st_b = st_b->next; - i++; - } -} - -static void _set_is_opt(t_stack **p_b) -{ - int i; - t_stack *st_b; - int size_b; - int opt_cost; - int min_index; - - size_b = stack_size(*p_b); - i = 0; - st_b = *p_b; - opt_cost = INT_MAX; - min_index = -1; - while (i < size_b) - { - if (ft_chmin(&opt_cost, get_content(st_b)->min_cost)) - min_index = i; - st_b = st_b->next; - i++; - } - i = 0; - st_b = *p_b; - while (i++ < min_index) - st_b = st_b->next; - get_content(st_b)->is_opt = true; -} - -void set_opt(t_stack **p_b) -{ - _set_min_cost_opt_method(p_b); - _set_is_opt(p_b); -} diff --git a/test/test_greedy_operation.cpp b/test/test_greedy_operation.cpp index a97b2e0..e088c4f 100644 --- a/test/test_greedy_operation.cpp +++ b/test/test_greedy_operation.cpp @@ -11,15 +11,20 @@ extern "C" { } // stack_a | stack_b -// 0 | 1 <- FF: (0, 1), FR: (0, -4), RF: (0, 1), RR: (0, -4) +// 0 | 1 <- RA_RB: (0, 1), RRA_RB: (0, -4), RA_RRB: (0, 1), RRA_RRB: +// (0, -4) // 1 4 1 4: 1 -// 2 | 3 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3) +// 2 | 3 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) // 2 4 6 4: 2 -// 4 | 5 <- FF: (2, 3), FR: (2, -2), RF: (-3, 3), RR: (-3, -2) +// 4 | 5 <- RA_RB: (2, 3), RRA_RB: (2, -2), RA_RRB: (-3, 3), +// RRA_RRB: (-3, -2) // 3 4 6 3: 3 -// 6 | 7 <- FF: (3, 4), FR: (3, -1), RF: (-2, 4), RR: (-2, -1) +// 6 | 7 <- RA_RB: (3, 4), RRA_RB: (3, -1), RA_RRB: (-2, 4), +// RRA_RRB: (-2, -1) // 4 4 6 2: 2 -// 8 | 9 <- FF: (4, 0), FR: (4, 0), RF: (-1, 0), RR: (-1, 0) +// 8 | 9 <- RA_RB: (4, 0), RRA_RB: (4, 0), RA_RRB: (-1, 0), RRA_RRB: +// (-1, 0) // 4 4 1 1: 1 // ======================================================================== // stack_a | stack_b @@ -40,7 +45,7 @@ static void greedy_operation_case1() { stack_a = NULL; stack_b = NULL; for (int i = 0; i < 2 * N; ++i) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); if (i % 2 == 0) ft_lstadd_back(&stack_a, ft_lstnew(c)); else @@ -48,7 +53,8 @@ static void greedy_operation_case1() { } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); greedy_operation(&stack_a, &stack_b); std::vector after_a = {1, 2, 4, 6, 8, 0}; @@ -85,15 +91,20 @@ TEST(greedy_operation, case1) { } // stack_a | stack_b -// 6 | 8 <- FF: (0, 2), FR: (0, -3), RF: (0, 2), RR: (0, -3) +// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2), +// RRA_RRB: (0, -3) // 2 3 2 3: 2 -// 7 | 9 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3) +// 7 | 9 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) // 2 4 6 4: 2 -// 1 | 0 <- FF: (2, 2), FR: (2, -3), RF: (2, 2), RR: (2, -3) +// 1 | 0 <- RA_RB: (2, 2), RRA_RB: (2, -3), RA_RRB: (2, 2), +// RRA_RRB: (2, -3) // 2 5 2 5: 2 -// 3 | 2 <- FF: (3, 3), FR: (3, -2), RF: (-2, 3), RR: (-2, -2) +// 3 | 2 <- RA_RB: (3, 3), RRA_RB: (3, -2), RA_RRB: (-2, 3), +// RRA_RRB: (-2, -2) // 3 5 5 2: 2 -// 4 | 5 <- FF: (4, 0), FR: (-1, 0), RF: (4, 0), RR: (-1, 0) +// 4 | 5 <- RA_RB: (4, 0), RRA_RB: (-1, 0), RA_RRB: (4, 0), +// RRA_RRB: (-1, 0) // 4 1 4 1: 1 // ======================================================================== // stack_a | stack_b @@ -114,16 +125,17 @@ static void greedy_operation_case2() { stack_a = NULL; stack_b = NULL; for (int &i : v_a) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } for (int &i : v_b) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); greedy_operation(&stack_a, &stack_b); std::vector after_a = {5, 6, 7, 1, 3, 4}; diff --git a/test/test_is_sorted_stack.cpp b/test/test_is_sorted_stack.cpp index f8f583b..f77a9df 100644 --- a/test/test_is_sorted_stack.cpp +++ b/test/test_is_sorted_stack.cpp @@ -28,7 +28,7 @@ TEST(is_sorted_stack, sorted) { st = NULL; for (int i = 0; i < N; ++i) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&st, ft_lstnew(c)); } @@ -53,7 +53,7 @@ TEST(is_sorted_stack, not_sorted) { st = NULL; for (int &i : v) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&st, ft_lstnew(c)); } diff --git a/test/test_push_b_segmented.cpp b/test/test_push_b_segmented.cpp index b5321f4..3f9d6d9 100644 --- a/test/test_push_b_segmented.cpp +++ b/test/test_push_b_segmented.cpp @@ -25,7 +25,7 @@ static void segmented_test_main1() { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int &i : v) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } @@ -34,18 +34,23 @@ static void segmented_test_main1() { t_stack *st = stack_b; for (int i = 0; i < size_a; ++i) { if (i < 4) { + EXPECT_EQ(get_content(st)->segment, 4); EXPECT_GE(get_content(st)->index, 16); EXPECT_LE(get_content(st)->index, 19); } else if (4 <= i && i < 8) { + EXPECT_EQ(get_content(st)->segment, 2); EXPECT_GE(get_content(st)->index, 8); EXPECT_LE(get_content(st)->index, 11); } else if (8 <= i && i < 12) { + EXPECT_EQ(get_content(st)->segment, 0); EXPECT_GE(get_content(st)->index, 0); EXPECT_LE(get_content(st)->index, 3); } else if (12 <= i && i < 16) { + EXPECT_EQ(get_content(st)->segment, 1); EXPECT_GE(get_content(st)->index, 4); EXPECT_LE(get_content(st)->index, 7); } else { + EXPECT_EQ(get_content(st)->segment, 3); EXPECT_GE(get_content(st)->index, 12); EXPECT_LE(get_content(st)->index, 15); } @@ -84,7 +89,7 @@ static void segmented_test_main2() { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int &i : v) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } @@ -92,18 +97,23 @@ static void segmented_test_main2() { t_stack *st = stack_b; for (int i = 0; i < size_a - 3; ++i) { if (i < 5) { + EXPECT_EQ(get_content(st)->segment, 4); EXPECT_GE(get_content(st)->index, 12); EXPECT_LE(get_content(st)->index, 19); } else if (5 <= i && i < 8) { + EXPECT_EQ(get_content(st)->segment, 2); EXPECT_GE(get_content(st)->index, 6); EXPECT_LE(get_content(st)->index, 8); } else if (8 <= i && i < 11) { + EXPECT_EQ(get_content(st)->segment, 0); EXPECT_GE(get_content(st)->index, 0); EXPECT_LE(get_content(st)->index, 2); } else if (11 <= i && i < 14) { + EXPECT_EQ(get_content(st)->segment, 1); EXPECT_GE(get_content(st)->index, 3); EXPECT_LE(get_content(st)->index, 5); } else { + EXPECT_EQ(get_content(st)->segment, 3); EXPECT_GE(get_content(st)->index, 9); EXPECT_LE(get_content(st)->index, 11); } @@ -112,6 +122,7 @@ static void segmented_test_main2() { st = stack_a; while (st) { + EXPECT_EQ(get_content(st)->segment, -1); EXPECT_GE(get_content(st)->index, 12); EXPECT_LE(get_content(st)->index, 19); st = st->next; diff --git a/test/test_set_cost.cpp b/test/test_set_cost.cpp index dac4d0b..5b3758b 100644 --- a/test/test_set_cost.cpp +++ b/test/test_set_cost.cpp @@ -22,7 +22,7 @@ TEST(set_selection_cost, selection) { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int i = 0; i < N; ++i) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); ft_lstadd_back(&stack_b, ft_lstnew(c)); } @@ -31,10 +31,10 @@ TEST(set_selection_cost, selection) { // index: 2 t_stack *target = stack_b->next->next; - int sf_cost = get_content(target)->sf_cost; - int sr_cost = get_content(target)->sr_cost; - EXPECT_EQ(sf_cost, 2); - EXPECT_EQ(sr_cost, -3); + int rb_cost = get_content(target)->rb_cost; + int rrb_cost = get_content(target)->rrb_cost; + EXPECT_EQ(rb_cost, 2); + EXPECT_EQ(rrb_cost, -3); } // stack_a | stack_b @@ -49,7 +49,7 @@ TEST(set_insertion_cost, insertion1) { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int i = 0; i < 2 * N; ++i) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); if (i % 2 == 0) ft_lstadd_back(&stack_a, ft_lstnew(c)); else @@ -59,26 +59,26 @@ TEST(set_insertion_cost, insertion1) { set_cost(&stack_a, &stack_b); t_stack *target; - int if_cost; - int ir_cost; + int ra_cost; + int rra_cost; target = stack_b; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 1); - EXPECT_EQ(ir_cost, -4); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 1); + EXPECT_EQ(rra_cost, -4); target = stack_b->next->next; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 3); - EXPECT_EQ(ir_cost, -2); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 3); + EXPECT_EQ(rra_cost, -2); target = stack_b->next->next->next->next; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 0); - EXPECT_EQ(ir_cost, 0); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 0); + EXPECT_EQ(rra_cost, 0); } // stack_a | stack_b @@ -94,35 +94,35 @@ TEST(set_insertion_cost, insertion2) { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int &i : v_a) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } for (int &i : v_b) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); t_stack *target; - int if_cost; - int ir_cost; + int ra_cost; + int rra_cost; target = stack_b; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 2); - EXPECT_EQ(ir_cost, -3); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 2); + EXPECT_EQ(rra_cost, -3); target = stack_b->next->next; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 2); - EXPECT_EQ(ir_cost, -3); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 2); + EXPECT_EQ(rra_cost, -3); target = stack_b->next->next->next->next; - if_cost = get_content(target)->if_cost; - ir_cost = get_content(target)->ir_cost; - EXPECT_EQ(if_cost, 0); - EXPECT_EQ(ir_cost, 0); + ra_cost = get_content(target)->ra_cost; + rra_cost = get_content(target)->rra_cost; + EXPECT_EQ(ra_cost, 0); + EXPECT_EQ(rra_cost, 0); } diff --git a/test/test_set_is_target.cpp b/test/test_set_is_target.cpp new file mode 100644 index 0000000..b775864 --- /dev/null +++ b/test/test_set_is_target.cpp @@ -0,0 +1,227 @@ +// Copyright 2024, reasuke + +#include +#include + +#include "gtest/gtest.h" + +extern "C" { +#include "large_sort.h" +#include "push_swap.h" +} + +// stack_a | stack_b +// 0 | 1 <- RA_RB: (0, 1), RRA_RB: (0, -4), RA_RRB: (0, 1), RRA_RRB: +// (0, -4) +// 1 4 1 4: 1 +// 2 | 3 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) +// 2 4 6 4: 2 +// 4 | 5 <- RA_RB: (2, 3), RRA_RB: (2, -2), RA_RRB: (-3, 3), +// RRA_RRB: (-3, -2) +// 3 4 6 3: 3 +// 6 | 7 <- RA_RB: (3, 4), RRA_RB: (3, -1), RA_RRB: (-2, 4), +// RRA_RRB: (-2, -1) +// 4 4 6 2: 2 +// 8 | 9 <- RA_RB: (4, 0), RRA_RB: (4, 0), RA_RRB: (-1, 0), RRA_RRB: +// (-1, 0) +// 4 4 1 1: 1 + +TEST(set_is_target, case1_segment_not_set) { + int N; + t_stack *stack_a; + t_stack *stack_b; + t_content *c; + + N = 5; + stack_a = NULL; + stack_b = NULL; + for (int i = 0; i < 2 * N; ++i) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + if (i % 2 == 0) + ft_lstadd_back(&stack_a, ft_lstnew(c)); + else + ft_lstadd_back(&stack_b, ft_lstnew(c)); + } + set_cost(&stack_a, &stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); + + t_stack *st = stack_b; + + for (int i = 0; i < N; ++i) { + if (i == 0) + EXPECT_EQ(get_content(st)->is_target, true); + else + EXPECT_EQ(get_content(st)->is_target, false); + st = st->next; + } +} + +// stack_a | stack_b +// 0 | 1 <- RA_RB: (0, 1), RRA_RB: (0, -4), RA_RRB: (0, 1), RRA_RRB: +// (0, -4) +// 1 4 1 4: 1 +// ==== segment -1 ==== +// 2 | 3 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) +// 2 4 6 4: 2 +// ==== segment 1 ==== +// 4 | 5 <- RA_RB: (2, 3), RRA_RB: (2, -2), RA_RRB: (-3, 3), +// RRA_RRB: (-3, -2) +// 3 4 6 3: 3 +// ==== segment -1 ==== +// 6 | 7 <- RA_RB: (3, 4), RRA_RB: (3, -1), RA_RRB: (-2, 4), +// RRA_RRB: (-2, -1) +// 4 4 6 2: 2 +// ==== segment -1 ==== +// 8 | 9 <- RA_RB: (4, 0), RRA_RB: (4, 0), RA_RRB: (-1, 0), RRA_RRB: +// (-1, 0) +// 4 4 1 1: 1 +// ==== segment 0 ====> target + +TEST(set_is_target, case1_segment_set) { + int N; + t_stack *stack_a; + t_stack *stack_b; + t_content *c; + + N = 5; + stack_a = NULL; + stack_b = NULL; + for (int i = 0; i < 2 * N; ++i) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + if (i % 2 == 0) + ft_lstadd_back(&stack_a, ft_lstnew(c)); + else + ft_lstadd_back(&stack_b, ft_lstnew(c)); + } + // segment set + get_content(ft_lstlast(stack_b))->segment = 0; + set_cost(&stack_a, &stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); + + t_stack *st = stack_b; + + for (int i = 0; i < N; ++i) { + if (i == 4) + EXPECT_EQ(get_content(st)->is_target, true); + else + EXPECT_EQ(get_content(st)->is_target, false); + st = st->next; + } +} + +// stack_a | stack_b +// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2), +// RRA_RRB: (0, -3) +// 2 3 2 3: 2 +// 7 | 9 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) +// 2 4 6 4: 2 +// 1 | 0 <- RA_RB: (2, 2), RRA_RB: (2, -3), RA_RRB: (2, 2), +// RRA_RRB: (2, -3) +// 2 5 2 5: 2 +// 3 | 2 <- RA_RB: (3, 3), RRA_RB: (3, -2), RA_RRB: (-2, 3), +// RRA_RRB: (-2, -2) +// 3 5 5 2: 2 +// 4 | 5 <- RA_RB: (4, 0), RRA_RB: (-1, 0), RA_RRB: (4, 0), +// RRA_RRB: (-1, 0) +// 4 1 4 1: 1 + +TEST(set_is_target, case2_segment_not_set) { + int N = 5; + t_stack *stack_a; + t_stack *stack_b; + t_content *c; + + std::vector v_a = {6, 7, 1, 3, 4}; + std::vector v_b = {8, 9, 0, 2, 5}; + stack_a = NULL; + stack_b = NULL; + for (int &i : v_a) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + ft_lstadd_back(&stack_a, ft_lstnew(c)); + } + for (int &i : v_b) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + ft_lstadd_back(&stack_b, ft_lstnew(c)); + } + + set_cost(&stack_a, &stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); + + t_stack *st = stack_b; + + for (int i = 0; i < N; ++i) { + if (i == 4) + EXPECT_EQ(get_content(st)->is_target, true); + else + EXPECT_EQ(get_content(st)->is_target, false); + st = st->next; + } +} + +// stack_a | stack_b +// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2), +// RRA_RRB: (0, -3) +// 2 3 2 3: 2 +// ==== segment 0 ==== +// 7 | 9 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) +// 2 4 6 4: 2 +// ==== segment 1 ==== +// 1 | 0 <- RA_RB: (2, 2), RRA_RB: (2, -3), RA_RRB: (2, 2), +// RRA_RRB: (2, -3) +// 2 5 2 5: 2 +// ==== segment 2 ==== +// 3 | 2 <- RA_RB: (3, 3), RRA_RB: (3, -2), RA_RRB: (-2, 3), +// RRA_RRB: (-2, -2) +// 3 5 5 2: 2 +// ==== segment 3 ==== +// 4 | 5 <- RA_RB: (4, 0), RRA_RB: (-1, 0), RA_RRB: (4, 0), +// RRA_RRB: (-1, 0) +// 4 1 4 1: 1 +// ==== segment -1 ==== + +TEST(set_is_target, case2_segment_set) { + int N = 5; + t_stack *stack_a; + t_stack *stack_b; + t_content *c; + + std::vector v_a = {6, 7, 1, 3, 4}; + std::vector v_b = {8, 9, 0, 2, 5}; + stack_a = NULL; + stack_b = NULL; + for (int &i : v_a) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + ft_lstadd_back(&stack_a, ft_lstnew(c)); + } + for (int &i : v_b) { + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); + ft_lstadd_back(&stack_b, ft_lstnew(c)); + } + + // segment set + get_content(stack_b)->segment = 0; + get_content(stack_b->next)->segment = 1; + get_content(stack_b->next->next)->segment = 2; + get_content(stack_b->next->next->next)->segment = 3; + + set_cost(&stack_a, &stack_b); + set_min_cost_opt_method(&stack_b); + set_is_target(&stack_b); + + t_stack *st = stack_b; + + for (int i = 0; i < N; ++i) { + if (i == 3) + EXPECT_EQ(get_content(st)->is_target, true); + else + EXPECT_EQ(get_content(st)->is_target, false); + st = st->next; + } +} diff --git a/test/test_set_opt_method.cpp b/test/test_set_min_cost_opt_method.cpp similarity index 52% rename from test/test_set_opt_method.cpp rename to test/test_set_min_cost_opt_method.cpp index 0cbafce..d811baf 100644 --- a/test/test_set_opt_method.cpp +++ b/test/test_set_min_cost_opt_method.cpp @@ -11,18 +11,23 @@ extern "C" { } // stack_a | stack_b -// 0 | 1 <- FF: (0, 1), FR: (0, -4), RF: (0, 1), RR: (0, -4) +// 0 | 1 <- RA_RB: (0, 1), RRA_RB: (0, -4), RA_RRB: (0, 1), RRA_RRB: +// (0, -4) // 1 4 1 4: 1 -// 2 | 3 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3) +// 2 | 3 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) // 2 4 6 4: 2 -// 4 | 5 <- FF: (2, 3), FR: (2, -2), RF: (-3, 3), RR: (-3, -2) +// 4 | 5 <- RA_RB: (2, 3), RRA_RB: (2, -2), RA_RRB: (-3, 3), +// RRA_RRB: (-3, -2) // 3 4 6 3: 3 -// 6 | 7 <- FF: (3, 4), FR: (3, -1), RF: (-2, 4), RR: (-2, -1) +// 6 | 7 <- RA_RB: (3, 4), RRA_RB: (3, -1), RA_RRB: (-2, 4), +// RRA_RRB: (-2, -1) // 4 4 6 2: 2 -// 8 | 9 <- FF: (4, 0), FR: (4, 0), RF: (-1, 0), RR: (-1, 0) +// 8 | 9 <- RA_RB: (4, 0), RRA_RB: (4, 0), RA_RRB: (-1, 0), RRA_RRB: +// (-1, 0) // 4 4 1 1: 1 -TEST(set_opt, _set_opt_method1) { +TEST(set_min_cost_opt_method, _set_opt_method1) { int N; t_stack *stack_a; t_stack *stack_b; @@ -32,30 +37,30 @@ TEST(set_opt, _set_opt_method1) { stack_a = NULL; stack_b = NULL; for (int i = 0; i < 2 * N; ++i) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); if (i % 2 == 0) ft_lstadd_back(&stack_a, ft_lstnew(c)); else ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); t_stack *target; target = stack_b; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next->next; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next->next->next; - EXPECT_EQ(get_content(target)->opt_method, RR); + EXPECT_EQ(get_content(target)->opt_method, RRA_RRB); target = stack_b->next->next->next->next; - EXPECT_EQ(get_content(target)->opt_method, RF); + EXPECT_EQ(get_content(target)->opt_method, RA_RRB); } -TEST(set_opt, _set_min_cost1) { +TEST(set_min_cost_opt_method, _set_min_cost1) { int N; t_stack *stack_a; t_stack *stack_b; @@ -65,14 +70,14 @@ TEST(set_opt, _set_min_cost1) { stack_a = NULL; stack_b = NULL; for (int i = 0; i < 2 * N; ++i) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); if (i % 2 == 0) ft_lstadd_back(&stack_a, ft_lstnew(c)); else ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); t_stack *target; @@ -88,49 +93,24 @@ TEST(set_opt, _set_min_cost1) { EXPECT_EQ(get_content(target)->min_cost, 1); } -TEST(set_opt, _set_is_opt1) { - int N; - t_stack *stack_a; - t_stack *stack_b; - t_content *c; - - N = 5; - stack_a = NULL; - stack_b = NULL; - for (int i = 0; i < 2 * N; ++i) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); - if (i % 2 == 0) - ft_lstadd_back(&stack_a, ft_lstnew(c)); - else - ft_lstadd_back(&stack_b, ft_lstnew(c)); - } - set_cost(&stack_a, &stack_b); - set_opt(&stack_b); - - t_stack *st = stack_b; - - for (int i = 0; i < N; ++i) { - if (i == 0) - EXPECT_EQ(get_content(st)->is_opt, true); - else - EXPECT_EQ(get_content(st)->is_opt, false); - st = st->next; - } -} - // stack_a | stack_b -// 6 | 8 <- FF: (0, 2), FR: (0, -3), RF: (0, 2), RR: (0, -3) +// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2), +// RRA_RRB: (0, -3) // 2 3 2 3: 2 -// 7 | 9 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3) +// 7 | 9 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2), +// RRA_RRB: (-4, -3) // 2 4 6 4: 2 -// 1 | 0 <- FF: (2, 2), FR: (2, -3), RF: (2, 2), RR: (2, -3) +// 1 | 0 <- RA_RB: (2, 2), RRA_RB: (2, -3), RA_RRB: (2, 2), +// RRA_RRB: (2, -3) // 2 5 2 5: 2 -// 3 | 2 <- FF: (3, 3), FR: (3, -2), RF: (-2, 3), RR: (-2, -2) +// 3 | 2 <- RA_RB: (3, 3), RRA_RB: (3, -2), RA_RRB: (-2, 3), +// RRA_RRB: (-2, -2) // 3 5 5 2: 2 -// 4 | 5 <- FF: (4, 0), FR: (-1, 0), RF: (4, 0), RR: (-1, 0) +// 4 | 5 <- RA_RB: (4, 0), RRA_RB: (-1, 0), RA_RRB: (4, 0), +// RRA_RRB: (-1, 0) // 4 1 4 1: 1 -TEST(set_opt, _set_opt_method2) { +TEST(set_min_cost_opt_method, _set_opt_method2) { t_stack *stack_a; t_stack *stack_b; t_content *c; @@ -140,32 +120,32 @@ TEST(set_opt, _set_opt_method2) { stack_a = NULL; stack_b = NULL; for (int &i : v_a) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } for (int &i : v_b) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); t_stack *target; target = stack_b; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next->next; - EXPECT_EQ(get_content(target)->opt_method, FF); + EXPECT_EQ(get_content(target)->opt_method, RA_RB); target = stack_b->next->next->next; - EXPECT_EQ(get_content(target)->opt_method, RR); + EXPECT_EQ(get_content(target)->opt_method, RRA_RRB); target = stack_b->next->next->next->next; - EXPECT_EQ(get_content(target)->opt_method, RF); + EXPECT_EQ(get_content(target)->opt_method, RA_RRB); } -TEST(set_opt, _set_min_cost2) { +TEST(set_min_cost_opt_method, _set_min_cost2) { t_stack *stack_a; t_stack *stack_b; t_content *c; @@ -175,16 +155,16 @@ TEST(set_opt, _set_min_cost2) { stack_a = NULL; stack_b = NULL; for (int &i : v_a) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } for (int &i : v_b) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_b, ft_lstnew(c)); } set_cost(&stack_a, &stack_b); - set_opt(&stack_b); + set_min_cost_opt_method(&stack_b); t_stack *target; @@ -199,36 +179,3 @@ TEST(set_opt, _set_min_cost2) { target = stack_b->next->next->next->next; EXPECT_EQ(get_content(target)->min_cost, 1); } - -TEST(set_opt, _set_is_opt2) { - int N = 5; - t_stack *stack_a; - t_stack *stack_b; - t_content *c; - - std::vector v_a = {6, 7, 1, 3, 4}; - std::vector v_b = {8, 9, 0, 2, 5}; - stack_a = NULL; - stack_b = NULL; - for (int &i : v_a) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); - ft_lstadd_back(&stack_a, ft_lstnew(c)); - } - for (int &i : v_b) { - c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); - ft_lstadd_back(&stack_b, ft_lstnew(c)); - } - - set_cost(&stack_a, &stack_b); - set_opt(&stack_b); - - t_stack *st = stack_b; - - for (int i = 0; i < N; ++i) { - if (i == 4) - EXPECT_EQ(get_content(st)->is_opt, true); - else - EXPECT_EQ(get_content(st)->is_opt, false); - st = st->next; - } -} diff --git a/test/test_sort.cpp b/test/test_sort.cpp index f489cc8..31198be 100644 --- a/test/test_sort.cpp +++ b/test/test_sort.cpp @@ -32,7 +32,7 @@ static void sort_test_main(int N) { t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int &i : v) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } // sort @@ -73,7 +73,7 @@ static void random_sort_test_main(int N) { t_list *stack_a = NULL; t_list *stack_b = NULL; for (int &i : v) { - t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, -1, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&stack_a, ft_lstnew(c)); } // sort