From e7080acc798de9a13eed85bfe7706c616882844c Mon Sep 17 00:00:00 2001 From: rask24 <70057885+rask24@users.noreply.github.com> Date: Tue, 13 Feb 2024 15:50:54 +0900 Subject: [PATCH] Fix content index (#26) * refactor generate_stack * update Makefile test * rename main * add header to initialization * fix module * fix 0 indexed --- Makefile | 8 +-- include/push_swap.h | 7 ++- src/initialization/check_args.c | 3 +- src/initialization/exit_with_error.c | 3 +- src/initialization/generate_stack.c | 79 +++++++++++++------------- src/main.c | 24 ++++---- src/sort/is_sorted_stack.c | 4 +- src/sort/large_sort/large_sort.c | 2 +- src/sort/large_sort/push_b_segmented.c | 24 ++------ src/sort/micro_sort.c | 16 +++--- test/test_is_sorted_stack.cpp | 4 +- test/test_push_b_segmented.cpp | 68 +++++++++++----------- test/test_sort.cpp | 8 +-- 13 files changed, 122 insertions(+), 128 deletions(-) diff --git a/Makefile b/Makefile index 93e4df8..5aa7bb5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME = push_swap CFLAGS = -Werror -Wextra -Wall CXXFLAGS = -std=c++17 -Wall -Wextra -Werror PROD_FLAGS = -O3 -DEV_FLAGS = -g -fsanitize=address -O0 -D DEV +DEV_FLAGS = -g -fsanitize=address,integer,undefined -O0 -D DEV LEAK_FLAGS = -O0 -D DEV -D LEAK DEPFLAGS = -MMD -MP INCLUDE = -I $(INC_DIR) @@ -109,9 +109,7 @@ leak: $(NAME) releak: fclean leak -test: test_clean test_main - -test_main: all $(GTEST_OBJ) $(TEST_OBJ) +test: all $(GTEST_OBJ) $(TEST_OBJ) @echo "$(BLUE)\ntest linking$(RESET)" @$(CXX) -L $(LIBFT_DIR) -lft -lpthread $(OBJ_FILTER_MAIN) $(TEST_OBJ) $(GTEST_OBJ) -o $(TEST_NAME) ./$(TEST_NAME) @@ -121,6 +119,8 @@ test_clean: @echo "$(BLUE)test cleaning$(RESET)" @$(RM) -r $(TEST_BUILD_DIR) +retest: test_clean test + $(TEST_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp @mkdir -p $(@D) @$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $< -o $@ diff --git a/include/push_swap.h b/include/push_swap.h index 6301243..c55ccee 100644 --- a/include/push_swap.h +++ b/include/push_swap.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:58:12 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 12:45:48 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,4 +21,9 @@ # include # include +int check_args(int argc, char **argv); +t_stack *generate_stack(int argc, char **argv); + +void sort(t_stack **p_a, t_stack **p_b); + #endif diff --git a/src/initialization/check_args.c b/src/initialization/check_args.c index 2b3b051..544e5a3 100644 --- a/src/initialization/check_args.c +++ b/src/initialization/check_args.c @@ -6,11 +6,12 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:44:55 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:35:17 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 11:45:54 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "initialization.h" +#include "push_swap.h" static bool _has_not_digit(int argc, char **argv) { diff --git a/src/initialization/exit_with_error.c b/src/initialization/exit_with_error.c index 52c84d8..c074c44 100644 --- a/src/initialization/exit_with_error.c +++ b/src/initialization/exit_with_error.c @@ -6,11 +6,12 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:38:17 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:35:14 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 11:46:04 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "initialization.h" +#include "push_swap.h" void exit_with_error(void) { diff --git a/src/initialization/generate_stack.c b/src/initialization/generate_stack.c index 0ce473c..3e4ae48 100644 --- a/src/initialization/generate_stack.c +++ b/src/initialization/generate_stack.c @@ -6,107 +6,106 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:35:32 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 14:01:23 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "initialization.h" +#include "push_swap.h" -int *_generate_int_array(int array_size) +int *_allocate_int_array(int size_a) { int *array; - array = malloc(sizeof(int) * array_size); + array = malloc(sizeof(int) * size_a); if (!array) exit_with_error(); return (array); } -int *_generate_numberd_array(int array_size, char **argv) +int *_generate_numberd_array(int size_a, char **argv) { int i; - int *numberd_array; + int *numberd; - numberd_array = _generate_int_array(array_size); + numberd = _allocate_int_array(size_a); i = 0; - while (i < array_size) + while (i < size_a) { - numberd_array[i] = ft_atoi(argv[i + 1]); + numberd[i] = ft_atoi(argv[i + 1]); i++; } - return (numberd_array); + return (numberd); } -int *_generate_sorted_array(int argc, char **argv) +int *_generate_sorted_array(int size_a, char **argv) { - int array_size; - int *sorted_array; + int *sorted; int i; int j; - array_size = argc - 1; - sorted_array = _generate_numberd_array(array_size, argv); - i = array_size - 1; + sorted = _generate_numberd_array(size_a, argv); + i = size_a - 1; while (i > 0) { j = 0; while (j < i) { - if (sorted_array[j] > sorted_array[j + 1]) - ft_swap(sorted_array + j, sorted_array + j + 1); + if (sorted[j] > sorted[j + 1]) + ft_swap(sorted + j, sorted + j + 1); j++; } i--; } - return (sorted_array); + return (sorted); } -int *_coordinate_compression(int argc, char **argv) +int *_coordinate_compression(int size_a, char **argv) { - int *compressred_array; - int *sorted_array; - int array_size; + int *comp; + int *sorted; int i; int j; - array_size = argc - 1; - compressred_array = _generate_int_array(array_size); - sorted_array = _generate_sorted_array(argc, argv); + comp = _allocate_int_array(size_a); + sorted = _generate_sorted_array(size_a, argv); i = 0; - while (i < array_size) + while (i < size_a) { j = 0; - while (j < array_size) + while (j < size_a) { - if (ft_atoi(argv[i + 1]) == sorted_array[j]) - compressred_array[i] = j + 1; + if (ft_atoi(argv[i + 1]) == sorted[j]) + comp[i] = j; j++; } i++; } - free(sorted_array); - return (compressred_array); + free(sorted); + return (comp); } t_stack *generate_stack(int argc, char **argv) { - t_stack *stack; - int *compressed_array; + t_stack *st; + int *comp; int i; t_content *ptr; + int size_a; - compressed_array = _coordinate_compression(argc, argv); + size_a = argc - 1; + comp = _coordinate_compression(size_a, argv); i = 0; - stack = NULL; - while (i < argc - 1) + st = NULL; + while (i < size_a) { ptr = ft_calloc(1, sizeof(t_content)); if (!ptr) exit_with_error(); - ptr->index = compressed_array[i]; - ft_lstadd_back(&stack, ft_lstnew(ptr)); + ptr->index = comp[i]; + ft_lstadd_back(&st, ft_lstnew(ptr)); i++; } - free(compressed_array); - return (stack); + free(comp); + return (st); } diff --git a/src/main.c b/src/main.c index 1ebb70e..78a4385 100644 --- a/src/main.c +++ b/src/main.c @@ -6,18 +6,16 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:54:10 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:00:02 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ -#include "initialization.h" #include "push_swap.h" -#include "sort.h" #ifdef LEAK # ifdef __APPLE__ -void leak_chek(void) __attribute__((destructor)); +void leak_chek(void) __attribute__((destructor)); void leak_chek(void) { @@ -35,15 +33,21 @@ void put_void(void *content) #endif +static void _put_void(void *p_content) +{ + ft_printf("%d\n", ((t_content *)p_content)->index); +} + int main(int argc, char **argv) { - t_stack *a; - t_stack *b; + t_stack *st_a; + t_stack *st_b; check_args(argc, argv); - a = generate_stack(argc, argv); - b = NULL; - sort(&a, &b); - clear_stack(&a, free); + st_a = generate_stack(argc, argv); + st_b = NULL; + ft_lstiter(st_a, _put_void); + sort(&st_a, &st_b); + clear_stack(&st_a, free); return (0); } diff --git a/src/sort/is_sorted_stack.c b/src/sort/is_sorted_stack.c index 1fe7b89..d130a36 100644 --- a/src/sort/is_sorted_stack.c +++ b/src/sort/is_sorted_stack.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:54:04 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 14:01:58 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ bool is_sorted_stack(t_stack *st) { int current; - current = 1; + current = 0; while (st) { if (get_content(st)->index != current) diff --git a/src/sort/large_sort/large_sort.c b/src/sort/large_sort/large_sort.c index 607a6bc..7683f8d 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/12 23:57:46 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 13:28:59 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/sort/large_sort/push_b_segmented.c b/src/sort/large_sort/push_b_segmented.c index 808af83..c383255 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/12 23:28:29 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:01:23 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,20 +14,6 @@ #include "push_swap.h" #include "stack_operations.h" -// 1 ~ 100 -// segment1: 1 ~ 20 -// segment1: 21 ~ 40 -// segment1: 41 ~ 60 -// segment1: 61 ~ 80 -// segment1: 81 ~ 100 - -// 123: 24 -// [0, 24) -> [0, seg_size) -// [24, 48) -> [seg_size, seg_size * 2) -// [48, 72) -> [seg_size * 2, seg_size * 3) -// [72, 96) -> [seg_size * 3, seg_size * 4) -// [96, 123) -> [seg_size * 4, N) - static int _calc_segment_id(int target, int n, int segs) { int seg_size; @@ -48,8 +34,6 @@ static int _calc_segment_id(int target, int n, int segs) return (-1); } -// ft_printf("ns: %d, index: %d, pushed: %d, n: %d\n", num_segment, index, -// pushed, n); static bool _should_push_b(int index, int pushed, int n, int segs) { int seg_size; @@ -70,8 +54,8 @@ static bool _should_push_b(int index, int pushed, int n, int segs) sup = seg_size * (cur_id + 1); } if (cur_id == segs - 1) - return (inf < index && index <= n); - return (inf < index && index <= sup); + return (inf <= index); + return (inf <= index && index < sup); } void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) @@ -86,7 +70,7 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) if (_should_push_b(index, pushed, n, segs)) { operate_pb(p_a, p_b); - if (_calc_segment_id(index - 1, n, segs) % 2 == 1) + if (_calc_segment_id(index, n, segs) % 2 == 1) operate_rb(p_b); pushed++; } diff --git a/src/sort/micro_sort.c b/src/sort/micro_sort.c index 8f70f63..60d0283 100644 --- a/src/sort/micro_sort.c +++ b/src/sort/micro_sort.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:54:02 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:38:56 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,21 +18,21 @@ static void _handle_4(t_stack **p_a, t_stack **p_b) { operate_pb(p_a, p_b); nano_sort(p_a); - if (get_first_index(p_b) == 1) + if (get_first_index(p_b) == 0) operate_pa(p_b, p_a); - else if (get_first_index(p_b) == 2) + else if (get_first_index(p_b) == 1) { operate_pa(p_b, p_a); operate_sa(p_a); } - else if (get_first_index(p_b) == 3) + else if (get_first_index(p_b) == 2) { operate_rra(p_a); operate_pa(p_b, p_a); operate_ra(p_a); operate_ra(p_a); } - else if (get_first_index(p_b) == 4) + else if (get_first_index(p_b) == 3) { operate_pa(p_b, p_a); operate_ra(p_a); @@ -49,7 +49,7 @@ static void _edge_flow_5(t_stack **p_a, t_stack **p_b) static void _normal_flow_5(t_stack **p_a, t_stack **p_b) { - if (get_first_index(p_b) == 5) + if (get_first_index(p_b) == 4) { operate_pa(p_b, p_a); operate_rra(p_a); @@ -63,7 +63,7 @@ static void _normal_flow_5(t_stack **p_a, t_stack **p_b) while (get_first_index(p_a) != get_first_index(p_b) + 1) operate_rra(p_a); operate_pa(p_b, p_a); - while (get_first_index(p_a) != 1) + while (get_first_index(p_a) != 0) operate_rra(p_a); } @@ -74,7 +74,7 @@ static void _handle_5(t_stack **p_a, t_stack **p_b) nano_sort(p_a); if (get_first_index(p_b) < get_second_index(p_b)) operate_sb(p_b); - if (get_first_index(p_b) == 5 && get_second_index(p_b) == 4) + if (get_first_index(p_b) == 4 && get_second_index(p_b) == 3) _edge_flow_5(p_a, p_b); else _normal_flow_5(p_a, p_b); diff --git a/test/test_is_sorted_stack.cpp b/test/test_is_sorted_stack.cpp index 340caeb..f8f583b 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 + 1, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, 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 + 1, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, 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 48c6407..b5321f4 100644 --- a/test/test_push_b_segmented.cpp +++ b/test/test_push_b_segmented.cpp @@ -18,8 +18,8 @@ __attribute__((unused)) static void put_void(void *p_content) { } static void segmented_test_main1() { - std::vector v = {9, 14, 18, 13, 12, 19, 2, 8, 16, 6, - 10, 17, 7, 1, 20, 15, 3, 11, 4, 5}; + std::vector v = {2, 7, 4, 9, 10, 0, 17, 19, 13, 8, + 1, 15, 11, 3, 5, 18, 16, 12, 14, 6}; int size_a = v.size(); t_stack *stack_a = NULL; @@ -34,20 +34,20 @@ static void segmented_test_main1() { t_stack *st = stack_b; for (int i = 0; i < size_a; ++i) { if (i < 4) { - EXPECT_GE(get_content(st)->index, 17); - EXPECT_LE(get_content(st)->index, 20); + EXPECT_GE(get_content(st)->index, 16); + EXPECT_LE(get_content(st)->index, 19); } else if (4 <= i && i < 8) { - EXPECT_GE(get_content(st)->index, 9); - EXPECT_LE(get_content(st)->index, 12); + EXPECT_GE(get_content(st)->index, 8); + EXPECT_LE(get_content(st)->index, 11); } else if (8 <= i && i < 12) { - EXPECT_GE(get_content(st)->index, 1); - EXPECT_LE(get_content(st)->index, 4); + EXPECT_GE(get_content(st)->index, 0); + EXPECT_LE(get_content(st)->index, 3); } else if (12 <= i && i < 16) { - EXPECT_GE(get_content(st)->index, 5); - EXPECT_LE(get_content(st)->index, 8); + EXPECT_GE(get_content(st)->index, 4); + EXPECT_LE(get_content(st)->index, 7); } else { - EXPECT_GE(get_content(st)->index, 13); - EXPECT_LE(get_content(st)->index, 16); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 15); } st = st->next; } @@ -73,7 +73,7 @@ static void segmented_test_main2() { int N = 20; std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // shuffle vector std::random_device seed_gen; @@ -92,44 +92,44 @@ static void segmented_test_main2() { t_stack *st = stack_b; for (int i = 0; i < size_a - 3; ++i) { if (i < 5) { - EXPECT_GE(get_content(st)->index, 13); - EXPECT_LE(get_content(st)->index, 17); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 19); } else if (5 <= i && i < 8) { - EXPECT_GE(get_content(st)->index, 7); - EXPECT_LE(get_content(st)->index, 9); + EXPECT_GE(get_content(st)->index, 6); + EXPECT_LE(get_content(st)->index, 8); } else if (8 <= i && i < 11) { - EXPECT_GE(get_content(st)->index, 1); - EXPECT_LE(get_content(st)->index, 3); + EXPECT_GE(get_content(st)->index, 0); + EXPECT_LE(get_content(st)->index, 2); } else if (11 <= i && i < 14) { - EXPECT_GE(get_content(st)->index, 4); - EXPECT_LE(get_content(st)->index, 6); + EXPECT_GE(get_content(st)->index, 3); + EXPECT_LE(get_content(st)->index, 5); } else { - EXPECT_GE(get_content(st)->index, 10); - EXPECT_LE(get_content(st)->index, 12); + EXPECT_GE(get_content(st)->index, 9); + EXPECT_LE(get_content(st)->index, 11); } st = st->next; } st = stack_a; while (st) { - EXPECT_GE(get_content(st)->index, 18); - EXPECT_LE(get_content(st)->index, 20); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 19); st = st->next; } } TEST(push_b_segmented, case2) { - // // save stdout - // int stdout_copy = dup(STDOUT_FILENO); - // // redirect stdout to /dev/null - // int dev_null = open("/dev/null", O_WRONLY); - // dup2(dev_null, 1); - // close(dev_null); + // save stdout + int stdout_copy = dup(STDOUT_FILENO); + // redirect stdout to /dev/null + int dev_null = open("/dev/null", O_WRONLY); + dup2(dev_null, 1); + close(dev_null); // execute test segmented_test_main2(); - // // revert stdout - // dup2(stdout_copy, STDOUT_FILENO); - // close(stdout_copy); + // revert stdout + dup2(stdout_copy, STDOUT_FILENO); + close(stdout_copy); } diff --git a/test/test_sort.cpp b/test/test_sort.cpp index aad5f26..f489cc8 100644 --- a/test/test_sort.cpp +++ b/test/test_sort.cpp @@ -16,7 +16,7 @@ static void sort_test_main(int N) { // e.g. N = 3 -> v = {1, 2, 3} std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // generate permutation of v (N! patterns) @@ -38,7 +38,7 @@ static void sort_test_main(int N) { // sort sort(&stack_a, &stack_b); // check if the order is appropreate - for (int i = 1; stack_a; ++i, stack_a = stack_a->next) { + for (int i = 0; stack_a; ++i, stack_a = stack_a->next) { EXPECT_EQ(get_first_index(&stack_a), i); } } while (std::next_permutation(v.begin(), v.end())); @@ -63,7 +63,7 @@ static void sort_test(int N) { static void random_sort_test_main(int N) { std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // shuffle vector std::random_device seed_gen; @@ -79,7 +79,7 @@ static void random_sort_test_main(int N) { // sort sort(&stack_a, &stack_b); // check if the order is appropreate - for (int i = 1; stack_a; ++i, stack_a = stack_a->next) { + for (int i = 0; stack_a; ++i, stack_a = stack_a->next) { EXPECT_EQ(*(int *)stack_a->content, i); } }