38 Commits

Author SHA1 Message Date
Maoake Teriierooiterai
b2652cacc7 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into fix_bench 2026-01-17 16:06:40 +01:00
Maoake Teriierooiterai
ae1a91c7c9 fixed bench 2026-01-17 16:06:12 +01:00
798886976c FIX; add error for invalid flag in bonus 2026-01-17 16:05:50 +01:00
e2cbea4df9 rm playtest.sh 2026-01-16 12:09:38 +01:00
88b7525bac Change simple algo for adaptive requierement to 02 max + norminette 2026-01-16 12:07:14 +01:00
Maoake Teriierooiterai
3887e3b4a5 finish to switch 2026-01-16 11:56:00 +01:00
Maoake Teriierooiterai
a3a462a515 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into playtest 2026-01-16 10:20:36 +01:00
Maoake Teriierooiterai
587ea1750c finish my shit on medium 2026-01-16 10:03:33 +01:00
Maoake Teriierooiterai
afa8785379 need to fix 2026-01-15 18:42:15 +01:00
0e7d1dce50 Fix indexer position in main, check if stacks is well mallocked before index stack 2026-01-15 12:44:51 +01:00
Maoake Teriierooiterai
469320374d trying some opti start to opti 2026-01-15 12:09:38 +01:00
ddd5b11a7a Add ai part to README 2026-01-15 12:09:27 +01:00
Maoake Teriierooiterai
854d699780 modifying some folders 2026-01-15 11:39:32 +01:00
Maoake Teriierooiterai
52d868904f fix some errors 2026-01-15 08:20:38 +01:00
Maoake Teriierooiterai
81ed92dc88 change the makefile 2026-01-15 07:14:10 +01:00
Maoake Teriierooiterai
c6f04ab48f adding verif flag functions 2026-01-14 16:22:05 +01:00
Maoake Teriierooiterai
e266b6d6e9 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into playtest 2026-01-14 16:08:29 +01:00
Maoake Teriierooiterai
59a80e6c9b modify makefile 2026-01-14 16:07:41 +01:00
e9c0894f92 Maybe final clean 2026-01-14 15:47:11 +01:00
Maoake Teriierooiterai
381bd66c2b Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into playtest 2026-01-14 14:00:25 +01:00
c2e7da01f4 remove test_one 2026-01-14 13:59:05 +01:00
Maoake Teriierooiterai
75c331e7ea correct some errors 2026-01-14 13:58:56 +01:00
78ec8a3fa4 Fix radix 2026-01-14 13:54:56 +01:00
Maoake Teriierooiterai
c323449e3a Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into readme 2026-01-14 13:38:39 +01:00
Maoake Teriierooiterai
e0712aa8c4 finish the readme 2026-01-14 13:38:07 +01:00
2c805c9506 Fix bench write size 2026-01-14 12:07:51 +01:00
Maoake Teriierooiterai
6b5347c997 modify git ignore 2026-01-14 12:03:31 +01:00
Maoake Teriierooiterai
89b7709d2d Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap into error_valgrind 2026-01-14 12:00:12 +01:00
Maoake Teriierooiterai
00af75f98f fix algo 2026-01-14 11:59:30 +01:00
Maoake Teriierooiterai
29c60fef6a finish my bonus and not doing the valgrind 2026-01-14 11:52:32 +01:00
60011b1b11 Fix bench valgrind error (Write size) 2026-01-14 11:42:10 +01:00
f92eba4f03 Merge branch 'indexer' 2026-01-14 11:19:07 +01:00
1ebb32f58d Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-14 11:16:26 +01:00
52220b5b49 Add index in t_stack + indexer function to add it 2026-01-14 11:15:31 +01:00
82111f59ad Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-14 10:08:14 +01:00
f4824f2984 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-13 12:23:35 +01:00
5ae963bb09 Merge branch 'master' of github.com:DavidGailleton/42-Push_Swap 2026-01-12 13:37:31 +01:00
15ca9cfe3f FIX: exit if str is null 2026-01-09 14:57:42 +01:00
30 changed files with 1324 additions and 264 deletions

4
.gitignore vendored
View File

@@ -60,6 +60,10 @@ dkms.conf
# Executable
push_swap
checker
checker_linux
# File obj
obj/
#playtest
playtest.sh

View File

@@ -26,20 +26,21 @@ INCLUDES = includes
# ALL FILES WITHOUT PATH
#============================
SRC = main.c test_one.c ft_putnbr.c secure_write.c
SRC = main.c ft_putnbr.c secure_write.c
INSERTION = insertion.c
FLAGS_FILES = algorithms_sort.c flag.c bench.c
PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c ft_itoa.c ft_isdigit.c \
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c
ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c indexer.c
CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c \
move_patha.c move_pathb.c
COMPLEX_ALGO = radix.c
@@ -58,7 +59,7 @@ OBJ_DIR = obj
CC = cc
CFLAGS = -Wall -Werror -Wextra -g3 -I$(INCLUDES)
CFLAGS = -Wall -Werror -Wextra -I$(INCLUDES)
NAME = push_swap
@@ -104,7 +105,7 @@ $(NAME): $(OBJ)
$(NAME_BONUS): $(BONUS_OBJ)
@$(CC) $(CFLAGS) $(BONUS_OBJ) -o $(NAME_BONUS)
@echo "===================================="
@echo "======= PUSH SWAP COMPILED ========="
@echo "======= CHECKER COMPILED ==========="
@echo "===================================="
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)

View File

@@ -1,5 +1,84 @@
This project has been created as part of the 42 curriculum by mteriier[, dgaillet].
*This project has been created as part of the 42 curriculum by dgaillet[, mteriier].*
## Description
The *Push swap* project goal is to sort values with push swap operations beetween 2 stack (*a* and *b*)
The **Push swap** project goal is to sort values with push swap operations beetween 2 stack (*a* and *b*)
with different algorithms. Depends on the difficulties to solve the stacks. We will select a certain algorithms.
The difficulty is calculate with a disorder function *(compute_disorder)*. Disorder result will be between 0 and 1.
Selected algo depends on the disorder:
**Disorder < 0.2 ( O(n2) ):**
- Insertion sort adaptation ( len = 500 > complexity = 30 000)
**0.2 >= Disorder < 0.5 ( O(n√n) ):**
- Bucket sort adaptations with √n buckets ( len = 500 > complexity = 11900)
**Disorder >= 0.5 ( O(n log n) ):**
- Radix sort adaptation (LSD or MSD) ( len = 500 > complexity = 8000)
## Instructions
### 1. Command to compile push_swap :
```
make
```
### 2. Command to compile bonus :
```
make bonus
```
### 3. Command to delete all objects and exec :
```
make fclean
```
### 3. Command exemples how to use push_swap :
```
./push_swap 7 5 8 12 30 -20
```
```
ARG="7 5 8 12 30 -20"; ./push_swap $ARG
```
### 4. Command exemples how to use checker :
```
./checker 7 5 8 12 30 -20
(put the command like pa,ra,rr,rrr ...)
```
### 5. Command exemples how to use checker and push_swap together :
```
ARG="7 5 8 12 30 -20"; ./push_swap $ARG | ./checker $ARG
```
## Resources
- **For the algorithms**
https://en.wikipedia.org/wiki/Sorting_algorithm
- **For BUCKET SORT**
https://www.meilisearch.com/blog/bucket-sort-guide
- **For RADIX**
https://www.geeksforgeeks.org/dsa/radix-sort/
### AI
AI was utilized to provide a reminder on how to include obj files within a subfolder for the Makefile.
## Additional sections may be required depending on the project
### Justification to why we chose this algorithms
- **Insertion sort**
We select **Insertion** cause that was the more simple to implement with the restrictions.
- **Bucket sort**
We select **Bucket** cause i like how the algorithms sort.
Define the range and the number of buckets.
- **Radix sort**
We select **Radix** cause that was the more simple to implement with the restrictions.

View File

@@ -14,67 +14,6 @@
#include "medium_headers.h"
#include <stdlib.h>
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = stacks->a;
else
tmp = stacks->b;
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != stacks->b->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = stacks->b;
tmp = tmp->previous;
i++;
}
return (i);
}
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = stacks->a;
else
tmp = stacks->b;
i = 0;
first_pass = 1;
while (first_pass || tmp != stacks->b)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
{
tmp = stacks->b->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *stacks, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(stacks, max_range, range, c);
path_end = path_to_end(stacks, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
int stack_len(t_stack *stack)
{
t_stack *first;
@@ -108,7 +47,5 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
free(preset);
preset = tmp;
}
while (stacks->b)
pa(stacks);
return ;
}

View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_patha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/16 06:20:47 by mteriier #+# #+# */
/* Updated: 2026/01/16 06:20:55 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "medium_headers.h"
#include "push_swap.h"
static int is_border_a(int value, t_stacks *stacks)
{
t_stack *tmp;
if (!stacks->a)
return (1);
tmp = assign_stack(stacks, 'a');
if ((value < tmp->index && check_order(tmp))
|| (value > tmp->index && value > tmp->previous->index
&& check_order(stacks->a)))
return (1);
return (0);
}
int move_left_to_right(int value, t_stacks *stacks, int is_move)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'a');
while (value > tmp->index && !is_move)
{
tmp = tmp->next;
i++;
}
while (value > stacks->a->index && is_move)
ra(stacks);
return (i);
}
int move_right_to_left(int value, t_stacks *stacks, int is_move)
{
int i;
t_stack *tmp;
tmp = assign_stack(stacks, 'a');
i = 0;
while (value < tmp->previous->index && !is_move)
{
tmp = tmp->previous;
i++;
}
while (value < stacks->a->previous->index && is_move)
rra(stacks);
return (i);
}
int wich_path_a(int value, t_stacks *stacks)
{
t_stack *tmp;
int move_from_left;
int move_from_right;
tmp = assign_stack(stacks, 'a');
if (is_border_a(value, stacks))
return (-1);
move_from_left = move_left_to_right(value, stacks, 0);
move_from_right = move_right_to_left(value, stacks, 0);
if ((check_order(stacks->a) && move_from_left < move_from_right)
|| (move_from_left > move_from_right && !check_order(stacks->a)))
return (1);
else
return (0);
}
void move_ra_rra(int value, t_stacks *stacks)
{
if (is_border_a(value, stacks))
return ;
if (wich_path_a(value, stacks) == 1)
{
move_left_to_right(value, stacks, 1);
}
else if (wich_path_a(value, stacks) == 0)
{
move_right_to_left(value, stacks, 1);
}
}

View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_pathb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/16 06:20:38 by mteriier #+# #+# */
/* Updated: 2026/01/16 06:20:42 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "medium_headers.h"
#include "push_swap.h"
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != start->previous)
{
first_pass = 0;
if (in_range(tmp->index, max_range, range))
tmp = start;
tmp = tmp->previous;
i++;
}
return (i);
}
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
{
t_stack *tmp;
t_stack *start;
int i;
int first_pass;
tmp = assign_stack(stacks, c);
start = assign_stack(stacks, c);
i = 0;
first_pass = 1;
while (first_pass || tmp != start)
{
first_pass = 0;
if (in_range(tmp->index, max_range, range))
{
tmp = start->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *stacks, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(stacks, max_range, range, c);
path_end = path_to_end(stacks, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range)
{
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(stacks->b->index, one_preset->max_range, range))
rb(stacks);
}
else
{
while (!in_range(stacks->b->index, one_preset->max_range, range))
{
rrb(stacks);
}
}
}
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int i;
t_stack *tmp;
i = 0;
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
while (!in_range(tmp->index, one_preset->max_range, range))
{
i++;
tmp = tmp->next;
}
}
else
{
while (!in_range(tmp->index, one_preset->max_range, range))
{
i++;
tmp = tmp->previous;
}
}
return (i);
}

View File

@@ -13,83 +13,79 @@
#include "push_swap.h"
#include "medium_headers.h"
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
void move_rr_rrr(t_stacks *stacks, int move, char c)
{
int i;
t_stack *tmp;
int i;
i = 0;
tmp = stacks->a;
if (start_end == 's')
while (i < move && c == 's')
{
while (tmp->value < value)
{
tmp = tmp->next;
i++;
}
rr(stacks);
i++;
}
else
while (i < move && c == 'e')
{
tmp = tmp->previous;
while (tmp->value > value)
{
tmp = tmp->previous;
i++;
}
i++;
rrr(stacks);
}
return (i);
}
static int sort_path(t_stacks *stacks, int value)
void put_in_order_ra_rra(t_stacks *stacks)
{
int start_path;
int end_path;
int i;
start_path = number_move_reverse(stacks, value, 's');
if (start_path == 0)
return (1);
end_path = number_move_reverse(stacks, value, 'e');
if (start_path < end_path)
return (1);
return (0);
}
static void sort_little_pile(t_stacks *stacks)
{
if (!stacks->a)
i = 0;
while (!check_order(stacks->a) && i < 50)
{
pa(stacks);
return ;
}
if (stacks->a->previous->value < stacks->b->value)
{
pa(stacks);
ra(stacks);
return ;
i++;
}
}
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range)
{
int move_a;
int final_move;
final_move = get_pre_move_b(stacks, one_preset, range);
if (wich_path_a(value, stacks) == 1
&& wich_path(stacks, one_preset->max_range, range, 'b'))
{
move_a = move_left_to_right(value, stacks, 0);
if (move_a < final_move)
final_move = move_a;
move_rr_rrr(stacks, final_move, 's');
}
else if (wich_path_a(value, stacks) == 0
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
{
move_a = move_right_to_left(value, stacks, 0);
if (move_a < final_move)
final_move = move_a;
move_rr_rrr(stacks, final_move, 'e');
}
if (sort_path(stacks, stacks->b->value))
sort_from_left(stacks);
else
sort_from_right(stacks);
}
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
{
int value;
int i;
i = 0;
while (one_preset->nb_in > 0)
{
if (wich_path(stacks, one_preset->max_range, range, 'b'))
if (stacks->a)
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
rb(stacks);
value = get_value_finded(stacks, one_preset, range);
path_rr_rrr(value, stacks, one_preset, range);
}
else
{
while (!in_range(stacks->b->value, one_preset->max_range, range))
{
rrb(stacks);
}
}
sort_little_pile(stacks);
normal_move_path(stacks, one_preset, range);
move_ra_rra(stacks->b->index, stacks);
pa(stacks);
put_in_order_ra_rra(stacks);
one_preset->nb_in--;
i++;
if (!check_order(stacks->a))
return ;
}
}

View File

@@ -11,39 +11,33 @@
/* ************************************************************************** */
#include "push_swap.h"
#include "medium_headers.h"
void sort_from_left(t_stacks *stacks)
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
{
int i;
int value;
t_stack *tmp;
i = 0;
while (stacks->b->value > stacks->a->value)
tmp = assign_stack(stacks, 'b');
if (wich_path(stacks, one_preset->max_range, range, 'b'))
{
ra(stacks);
i++;
}
pa(stacks);
while (i > 0)
{
rra(stacks);
i--;
while (!in_range(tmp->index, one_preset->max_range, range))
tmp = tmp->next;
}
else
while (!in_range(tmp->index, one_preset->max_range, range))
tmp = tmp->previous;
value = tmp->index;
return (value);
}
void sort_from_right(t_stacks *stacks)
t_stack *assign_stack(t_stacks *stacks, char c)
{
int i;
t_stack *tmp;
i = 0;
while (stacks->b->value < stacks->a->previous->value)
{
rra(stacks);
i++;
}
pa(stacks);
while (i >= 0)
{
ra(stacks);
i--;
}
if (c == 'a')
tmp = stacks->a;
else
tmp = stacks->b;
return (tmp);
}

View File

@@ -19,13 +19,13 @@ int get_first_lower(t_stack *first)
int pass;
tmp = first;
lower = tmp->value;
lower = tmp->index;
pass = 1;
while (tmp != first || pass == 1)
{
pass = 0;
if (lower > tmp->value)
lower = tmp->value;
if (lower > tmp->index)
lower = tmp->index;
tmp = tmp->next;
}
return (lower);
@@ -43,9 +43,9 @@ int get_next_lower(t_stack *first, int old_lower)
while (tmp != first || skip_first)
{
skip_first = 0;
if (old_lower < tmp->value && tmp->value <= next_lower)
if (old_lower < tmp->index && tmp->index <= next_lower)
{
next_lower = tmp->value;
next_lower = tmp->index;
if (next_lower == -2147483648)
return (next_lower);
}
@@ -96,7 +96,7 @@ int get_number_in_range(int max_range, t_stack *a, int range)
while (tmp != first || pass == 1)
{
pass = 0;
if (in_range(tmp->value, max_range, range))
if (in_range(tmp->index, max_range, range))
nb_in++;
tmp = tmp->next;
}

View File

@@ -20,13 +20,13 @@ static int get_max_number(t_stack *first)
t_stack *a;
a = first;
max = a->value;
max = a->index;
pass = 1;
while (a != first || pass == 1)
{
pass = 0;
if (max < a->value)
max = a->value;
if (max < a->index)
max = a->index;
a = a->next;
}
return (max);
@@ -39,13 +39,13 @@ static int get_min_number(t_stack *first)
int pass;
a = first;
min = a->value;
min = a->index;
pass = 1;
while (a != first || pass == 1)
{
pass = 0;
if (min > a->value)
min = a->value;
if (min > a->index)
min = a->index;
a = a->next;
}
return (min);

View File

@@ -12,17 +12,17 @@
#include "push_swap.h"
static int still_unit_value(t_stacks *stacks, int unit)
static int still_unit_index(t_stacks *stacks, int unit)
{
t_stack *temp;
temp = stacks->a;
if (!temp || temp->value >= unit)
if (!temp || temp->index >= unit)
return (1);
temp = temp->next;
while (temp != stacks->a)
{
if (temp->value >= unit)
if (temp->index >= unit)
return (1);
temp = temp->next;
}
@@ -33,19 +33,12 @@ static void push_by_number_to_b(t_stacks *stacks, int unit, int nb)
{
int i;
int s_len;
t_stack *temp;
temp = stacks->a;
s_len = 1;
while (temp && temp->next != stacks->a)
{
s_len++;
temp = temp->next;
}
s_len = stack_a_len(stacks);
i = 0;
while (i < s_len && still_unit_value(stacks, unit))
while (i < s_len && still_unit_index(stacks, unit))
{
if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb)
if (stacks->a && (stacks->a->index % (unit * 10)) / unit == nb)
pb(stacks);
else
ra(stacks);
@@ -58,7 +51,7 @@ static void rec_sort(t_stacks *stacks, int unit)
int i;
i = 0;
if (!still_unit_value(stacks, unit))
if (!still_unit_index(stacks, unit))
return ;
while (i <= 9)
{

View File

@@ -18,27 +18,27 @@
static int apply_operation(t_stacks *stacks, char buf[1024])
{
if (!ft_strncmp("sa", buf, ft_strlen(buf)))
if (ft_strncmp("sa\n", buf, ft_strlen(buf)))
return (sa(stacks), 1);
if (!ft_strncmp("sb", buf, ft_strlen(buf)))
if (ft_strncmp("sb\n", buf, ft_strlen(buf)))
return (sb(stacks), 1);
if (!ft_strncmp("ss", buf, ft_strlen(buf)))
if (ft_strncmp("ss\n", buf, ft_strlen(buf)))
return (ss(stacks), 1);
if (!ft_strncmp("pa", buf, ft_strlen(buf)))
if (ft_strncmp("pa\n", buf, ft_strlen(buf)))
return (pa(stacks), 1);
if (!ft_strncmp("pb", buf, ft_strlen(buf)))
if (ft_strncmp("pb\n", buf, ft_strlen(buf)))
return (pb(stacks), 1);
if (!ft_strncmp("ra", buf, ft_strlen(buf)))
if (ft_strncmp("ra\n", buf, ft_strlen(buf)))
return (ra(stacks), 1);
if (!ft_strncmp("rb", buf, ft_strlen(buf)))
if (ft_strncmp("rb\n", buf, ft_strlen(buf)))
return (rb(stacks), 1);
if (!ft_strncmp("rr", buf, ft_strlen(buf)))
if (ft_strncmp("rr\n", buf, ft_strlen(buf)))
return (rr(stacks), 1);
if (!ft_strncmp("rra", buf, ft_strlen(buf)))
if (ft_strncmp("rra\n", buf, ft_strlen(buf)))
return (rra(stacks), 1);
if (!ft_strncmp("rrb", buf, ft_strlen(buf)))
if (ft_strncmp("rrb\n", buf, ft_strlen(buf)))
return (rrb(stacks), 1);
if (!ft_strncmp("rrr", buf, ft_strlen(buf)))
if (ft_strncmp("rrr\n", buf, ft_strlen(buf)))
return (rrr(stacks), 1);
return (0);
}
@@ -58,15 +58,19 @@ static int tester(t_stacks *stacks)
while (buf)
{
if (!apply_operation(stacks, buf))
break ;
{
secure_write(2, "Error\n", 6);
return (0);
}
free(buf);
buf = get_next_line(0);
}
if (!is_stacks_b_empty(stacks))
return (write(1, "KO\n", 3));
if (!check_order(stacks->a))
return (write(1, "KO\n", 3));
write(1, "OK\n", 3);
if (!is_stacks_b_empty(stacks) || !check_order(stacks->a))
{
secure_write(1, "KO\n", 3);
return (0);
}
secure_write(1, "OK\n", 3);
return (0);
}
@@ -96,7 +100,7 @@ int main(int argc, char **argv)
if (!tab)
return (0);
if (!check_error_bonus(tab))
write(2, "Error\n", 7);
secure_write(2, "Error\n", 7);
else
bonus(tab);
free_tab(tab);

View File

@@ -26,6 +26,16 @@ static int is_exist_flag(char **tab, int pos)
return (verif);
}
static int is_algo_flag(char *str)
{
if (ft_strncmp(str, "--simple", 30)
|| ft_strncmp(str, "--medium", 30)
|| ft_strncmp(str, "--adaptive", 30)
|| ft_strncmp(str, "--complex", 30))
return (1);
return (0);
}
static int verif_exist_flag(char **tab, int mod)
{
int verif;
@@ -37,6 +47,8 @@ static int verif_exist_flag(char **tab, int mod)
{
if (is_exist_flag(tab, 1) && is_exist_flag(tab, 2))
verif = 1;
if (is_algo_flag(tab[1]) && is_algo_flag(tab[2]))
verif = 0;
}
return (verif);
}

View File

@@ -19,7 +19,6 @@ void simple(t_stacks *stacks)
int len;
len = stack_a_len(stacks);
stacks->algo = 1;
insertion(stacks, len);
}
@@ -29,7 +28,6 @@ void medium(t_stacks *stacks)
int range;
int len;
stacks->algo = 2;
len = stack_a_len(stacks);
if (len == 2)
sort_two(stacks);
@@ -45,7 +43,6 @@ void medium(t_stacks *stacks)
void complex(t_stacks *stacks)
{
stacks->algo = 3;
radix(stacks);
}
@@ -58,7 +55,7 @@ void adaptive(t_stacks *stacks, char **tab)
while (!ft_isdigit(tab[i][0]) && tab[i])
i++;
disorder = stacks->disorder;
if (disorder < 0.3)
if (disorder < 0.2)
simple(stacks);
else if (disorder < 0.5)
medium(stacks);

View File

@@ -19,7 +19,8 @@ static void print_disorder(t_stacks *stacks)
int nbr;
char *str;
nbr = (int) stacks->disorder * 10000;
stacks->disorder *= 10000;
nbr = (int) stacks->disorder;
str = ft_itoa(nbr);
if (!str)
exit (EXIT_FAILURE);
@@ -31,8 +32,9 @@ static void print_disorder(t_stacks *stacks)
secure_write(2, ".", 1);
if (ft_strlen(str) == 1)
secure_write(2, "0", 1);
secure_write(2, &str[ft_strlen(str) - 2], 2);
secure_write(2, &str[ft_strlen(str) - 2], ft_strlen(str) - 2);
secure_write(2, "%\n", 2);
stacks->disorder /= 10000;
free(str);
}
@@ -41,20 +43,20 @@ static void print_algo(t_stacks *stacks)
secure_write(2, "[bench] algo: ", 14);
if (stacks->algo == 0)
{
secure_write(2, "Adaptative", 10);
secure_write(2, "Adaptive", 8);
if (stacks->disorder < 0.2)
secure_write(2, " / O(n2n)\n", 10);
else if (stacks->disorder >= 0.5)
secure_write(2, " / O(n√n)\n", 10);
else
secure_write(2, " / O(nlogn)\n", 12);
else
secure_write(2, " / O(n√n)\n", 12);
}
else if (stacks->algo == 1)
secure_write(2, "Simple / O(n2n)\n", 16);
else if (stacks->algo == 2)
secure_write(2, "Medium / O(nlogn)\n", 18);
secure_write(2, "Medium / O(nn)\n", 18);
else if (stacks->algo == 3)
secure_write(2, "Complex / O(nn)\n", 17);
secure_write(2, "Complex / O(nlogn)\n", 19);
}
static void print_total_ops(t_stacks *stacks)

View File

@@ -19,7 +19,8 @@ int calcul_mod(int argc, char **argv)
int mod;
mod = 0;
while (argv[mod] && !ft_isdigit(argv[mod][0]) && mod < argc)
while (argv[mod] && !ft_isdigit(argv[mod][0])
&& !ft_isdigit(argv[mod][1]) && argv[mod][1] && mod < argc)
mod++;
mod--;
return (mod);
@@ -62,11 +63,20 @@ int pos_bench(char **argv, int mod)
void flags(int pos, int pos_b, char **argv, t_stacks *stacks)
{
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
{
stacks->algo = 1;
simple(stacks);
}
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
{
stacks->algo = 2;
medium(stacks);
}
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
{
stacks->algo = 3;
complex(stacks);
}
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
adaptive(stacks, argv);
else

View File

@@ -18,7 +18,7 @@ static void rec_ft_putnbr_fd(int nbr, int fd)
if (!nbr)
return ;
ft_putnbr_fd(nbr / 10, fd);
rec_ft_putnbr_fd(nbr / 10, fd);
c = (nbr % 10) + '0';
secure_write(fd, &c, 1);
}

View File

@@ -13,6 +13,8 @@
#ifndef MEDIUM_HEADERS_H
# define MEDIUM_HEADERS_H
# include "push_swap.h"
typedef struct s_tab
{
int max_range;
@@ -25,9 +27,10 @@ int wich_path(t_stacks *stacks, int max_range, int range, char c);
int stack_len(t_stack *stack);
void bucket_algo(t_stacks *stacks, t_tab *preset, int range);
/* SORT UTILS FILES */
void sort_from_left(t_stacks *stacks);
void sort_from_right(t_stacks *stacks);
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range);
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range);
t_stack *assign_stack(t_stacks *stacks, char c);
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range);
/* MEDIUM UTILS FILES */
int range_bucket(t_stack *first);
int get_first_lower(t_stack *first);
@@ -37,5 +40,13 @@ int in_range(int value, int max_range, int range);
int get_number_in_range(int max_range, t_stack *a, int range);
/* UTILS STRUCT TAB FILE */
t_tab *get_tabs(t_stack *first, int range);
/* MOVE PATH FILES*/
int get_pre_move_b(t_stacks *stacks, t_tab *one_preset, int range);
int move_left_to_right(int value, t_stacks *stacks, int is_move);
int move_right_to_left(int value, t_stacks *stacks, int is_move);
int wich_path_a(int value, t_stacks *stacks);
void move_ra_rra(int value, t_stacks *stacks);
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range);
void put_in_order_ra_rra(t_stacks *stacks);
#endif

View File

@@ -34,5 +34,6 @@ int ft_strlcat(char *dst, const char *src, int size);
char *join_all(int argc, char **argv);
char **split_all(char *tab);
float compute_disorder(char **strs, int pos);
void indexer(t_stacks *stacks);
#endif

View File

@@ -16,6 +16,7 @@
typedef struct s_stack
{
int value;
long long index;
struct s_stack *next;
struct s_stack *previous;
} t_stack;

31
main.c
View File

@@ -16,6 +16,33 @@
#include "flags.h"
#include <unistd.h>
static int push_swap(char **tab, int len, int mod)
{
t_stacks *stacks;
stacks = NULL;
if (mod == -1)
return (0);
stacks = init_stacks(len, tab, mod);
if (!stacks)
return (0);
indexer(stacks);
if (check_order(stacks->a))
{
flags(0, pos_bench(tab, mod), tab, stacks);
if (stacks->bench == 1)
print_bench(stacks);
free_all(stacks);
return (0);
}
stacks->disorder = compute_disorder(tab, wich_mod(mod));
flags(pos_flag(tab, mod), pos_bench(tab, mod), tab, stacks);
if (stacks->bench == 1)
print_bench(stacks);
free_all(stacks);
return (0);
}
int main(int argc, char **argv)
{
char **tab;
@@ -30,9 +57,9 @@ int main(int argc, char **argv)
len = len_split(tab);
mod = calcul_mod(len, tab);
if (check_error(tab, mod))
test1(tab, len, mod);
push_swap(tab, len, mod);
else
write(2, "Error\n", 7);
write(2, "Error\n", 6);
free_tab(tab);
return (0);
}

54
parsing/indexer.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* indexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/14 10:14:06 by dgaillet #+# #+# */
/* Updated: 2026/01/14 10:14:08 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_stack *first_without_index(t_stack *stack)
{
t_stack *first;
if (stack->index < 0)
return (stack);
first = stack;
stack = stack->next;
while (stack != first)
{
if (stack->index < 0)
return (stack);
stack = stack->next;
}
return ((void *) 0);
}
void indexer(t_stacks *stacks)
{
int i;
int len;
t_stack *lower;
t_stack *temp;
i = 0;
len = stack_a_len(stacks);
while (i < len)
{
lower = first_without_index(stacks->a);
temp = lower->next;
while (temp != stacks->a)
{
if (lower->value > temp->value && temp->index < 0)
lower = temp;
temp = temp->next;
}
lower->index = i;
i++;
}
}

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
/* Updated: 2026/01/09 12:05:56 by dgaillet ### ########lyon.fr */
/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
@@ -17,6 +17,6 @@ void secure_write(int fd, char *str, int len)
{
if (len < 0)
return ;
if (write(fd, str, len) < 0)
if (!str || write(fd, str, len) < 0)
exit (EXIT_FAILURE);
}

View File

@@ -32,7 +32,7 @@ void pa(t_stacks *stacks)
stack_add_front(&(stacks->a), b_push);
stacks->pa++;
if (stacks->print)
write(1, "pa\n", 3);
secure_write(1, "pa\n", 3);
}
void pb(t_stacks *stacks)
@@ -53,5 +53,5 @@ void pb(t_stacks *stacks)
stack_add_front(&(stacks->b), a_push);
stacks->pb++;
if (stacks->print)
write(1, "pb\n", 3);
secure_write(1, "pb\n", 3);
}

View File

@@ -18,7 +18,8 @@ void rra(t_stacks *stacks)
if (stacks && stacks->a && stacks->a->previous)
stacks->a = stacks->a->previous;
stacks->rra++;
write(1, "rra\n", 4);
if (stacks->print)
secure_write(1, "rra\n", 4);
}
void rrb(t_stacks *stacks)
@@ -27,7 +28,7 @@ void rrb(t_stacks *stacks)
stacks->b = stacks->b->previous;
stacks->rrb++;
if (stacks->print)
write(1, "rrb\n", 4);
secure_write(1, "rrb\n", 4);
}
void rrr(t_stacks *stacks)
@@ -38,5 +39,5 @@ void rrr(t_stacks *stacks)
stacks->a = stacks->a->previous;
stacks->rrr++;
if (stacks->print)
write(1, "rrr\n", 4);
secure_write(1, "rrr\n", 4);
}

View File

@@ -19,7 +19,7 @@ void ra(t_stacks *stacks)
stacks->a = stacks->a->next;
stacks->ra++;
if (stacks->print)
write(1, "ra\n", 3);
secure_write(1, "ra\n", 3);
}
void rb(t_stacks *stacks)
@@ -28,7 +28,7 @@ void rb(t_stacks *stacks)
stacks->b = stacks->b->next;
stacks->rb++;
if (stacks->print)
write(1, "rb\n", 3);
secure_write(1, "rb\n", 3);
}
void rr(t_stacks *stacks)
@@ -39,5 +39,5 @@ void rr(t_stacks *stacks)
stacks->b = stacks->b->next;
stacks->rr++;
if (stacks->print)
write(1, "rr\n", 3);
secure_write(1, "rr\n", 3);
}

View File

@@ -21,6 +21,7 @@ t_stack *new_stack(int value)
if (!new)
return (NULL);
new->value = value;
new->index = -1;
new->next = NULL;
new->previous = NULL;
return (new);

View File

@@ -26,7 +26,7 @@ void sa(t_stacks *stacks)
a->next->value = stock;
stacks->sa++;
if (stacks->print)
write(1, "sa\n", 3);
secure_write(1, "sa\n", 3);
}
void sb(t_stacks *stacks)
@@ -42,7 +42,7 @@ void sb(t_stacks *stacks)
b->next->value = stock;
stacks->sb++;
if (stacks->print)
write(1, "sb\n", 3);
secure_write(1, "sb\n", 3);
}
void ss(t_stacks *stacks)
@@ -69,5 +69,5 @@ void ss(t_stacks *stacks)
}
stacks->ss++;
if (stacks->print)
write(1, "ss\n", 3);
secure_write(1, "ss\n", 3);
}

View File

@@ -1,39 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
/* Updated: 2026/01/08 15:35:43 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "flags.h"
#include "parsing.h"
#include "medium_headers.h"
int test1(char **tab, int len, int mod)
{
t_stacks *stacks;
stacks = NULL;
if (mod == -1)
return (0);
stacks = init_stacks(len, tab, mod);
if (!stacks)
return (0);
if (check_order(stacks->a))
{
free_all(stacks);
return (0);
}
stacks->disorder = compute_disorder(tab, wich_mod(mod));
flags(pos_flag(tab, mod), pos_bench(tab, mod), tab, stacks);
if (stacks->bench == 1)
print_bench(stacks);
free_all(stacks);
return (0);
}

765
test_push_swap.sh Executable file
View File

@@ -0,0 +1,765 @@
#!/usr/bin/env bash
# Script complet push_swap 42
# Avec tests pour tous les flags, piles déjà triées, leaks, stress et benchmark
PS=./push_swap
CK=./checker_linux
VALGRIND="valgrind --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=42"
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[1;33m"
NC="\033[0m"
fail=0
# =========================
# UTILS
# =========================
expect_exact() {
NAME="$1"
CMD="$2"
EXPECT="$3"
OUT=$(eval "$CMD" 2>&1)
RET=$?
if diff -u <(printf "%s\n" "$EXPECT") <(printf "%s\n" "$OUT") >/dev/null && [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} $NAME"
else
echo -e "${RED}[KO]${NC} $NAME"
echo "EXPECTED: $EXPECT (return 0)"
echo "GOT : $OUT (return $RET)"
fail=1
fi
}
check_sort() {
local ARG="$1"
local FLAG="$2"
# Exécuter push_swap
OUT=$(eval "$PS $FLAG $ARG" 2>/dev/null)
RET=$?
# Compter les opérations
if [ -z "$OUT" ]; then
OPS=0
else
OPS=$(echo "$OUT" | wc -l)
fi
# Si 0 opération, vérifier si la pile est déjà triée
if [ $OPS -eq 0 ] && [ $RET -eq 0 ]; then
# Tester si c'est bien trié avec un checker "à vide"
echo "" | eval "$CK $ARG" 2>/dev/null | grep -q "OK"
if [ $? -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} $FLAG $ARG (ops=0, already sorted)"
return
fi
fi
# Vérifier avec checker (si ops > 0)
if [ $OPS -gt 0 ]; then
RES=$(echo "$OUT" | eval "$CK $ARG" 2>/dev/null)
else
RES=""
fi
# Si déjà trié (1 2 3), accepter 0 ou 1 opération
if echo "$ARG" | grep -qE '1.*2.*3' && [ $(echo "$ARG" | grep -o '[0-9]' | wc -l) -eq 3 ]; then
if [ "$RES" = "OK" ] || [ $OPS -eq 0 ]; then
if [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} $FLAG $ARG (ops=$OPS)"
return
fi
fi
fi
# Sinon, comportement normal
if [ "$RES" = "OK" ] && [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} $FLAG $ARG (ops=$OPS)"
else
echo -e "${RED}[KO]${NC} $FLAG $ARG (ops=$OPS, checker=$RES, return=$RET)"
fail=1
fi
}
check_leak() {
ARG="$1"
FLAG="$2"
$VALGRIND $PS $FLAG $ARG >/dev/null 2>&1
if [ $? -eq 42 ]; then
echo -e "${RED}[LEAK ❌]${NC} $FLAG $ARG"
fail=1
else
echo -e "${GREEN}[NO LEAK ✅]${NC} $FLAG $ARG"
fi
}
random_arg() {
N=$1
seq -$((N*10)) $((N*10)) | shuf | head -n $N | tr '\n' ' '
}
# =========================
# CAS DEJA TRIE (ops <= 1 accepté)
# =========================
echo -e "\n${YELLOW}=== CAS DEJA TRIE ===${NC}"
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
ARG="1 2 3"
OUT=$($PS $FLAG $ARG 2>/dev/null)
RET=$?
if [ $RET -eq 0 ]; then
if [ -z "$OUT" ]; then
echo -e "${GREEN}[OK]${NC} Pile déjà triée $FLAG (0 ops)"
else
OPS=$(echo "$OUT" | wc -l)
RES=$(echo "$OUT" | $CK $ARG 2>/dev/null)
if [ "$RES" = "OK" ] && [ $OPS -le 1 ]; then
echo -e "${GREEN}[OK]${NC} Pile déjà triée $FLAG (ops=$OPS)"
else
echo -e "${RED}[KO]${NC} Pile déjà triée $FLAG (ops=$OPS, checker=$RES)"
fail=1
fi
fi
else
echo -e "${RED}[KO]${NC} Pile déjà triée $FLAG (return=$RET)"
fail=1
fi
done
# =========================
# ERREURS STRICTES
# =========================
echo -e "${YELLOW}=== ERREURS STRICTES ===${NC}"
expect_exact "doublons" "$PS 1 2 1" "Error"
expect_exact "lettre" "$PS 1 a 3" "Error"
expect_exact "overflow +" "$PS 2147483648" "Error"
expect_exact "overflow -" "$PS -2147483649" "Error"
expect_exact "flag" "$PS --help" "Error"
# =========================
# CAS PAS D'ARGUMENT
# =========================
echo -e "\n${YELLOW}=== CAS PAS D'ARGUMENT ===${NC}"
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
OUT=$($PS $FLAG)
RET=$?
OPS=$(echo "$OUT" | wc -l)
if [ -z "$OUT" ] && [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} Pas d'argument $FLAG (aucune sortie, return 0)"
else
echo -e "${RED}[KO]${NC} Pas d'argument $FLAG (sortie inattendue ou return $RET)"
fail=1
fi
done
# =========================
# PARSING / FORMAT
# =========================
echo -e "\n${YELLOW}=== PARSING / FORMAT ===${NC}"
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
check_sort "\"1 2 3\"" "$FLAG"
check_sort "\" 1 2 3 \"" "$FLAG"
check_sort $'1\t2\t3' "$FLAG"
check_sort "1 2 3" "$FLAG"
done
# =========================
# TRI SIMPLES
# =========================
echo -e "\n${YELLOW}=== TRI SIMPLES ===${NC}"
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
check_sort "2 1" "$FLAG"
check_sort "3 2 1" "$FLAG"
check_sort "5 1 4 2 3" "$FLAG"
done
# =========================
# RANDOM TESTS
# =========================
echo -e "\n${YELLOW}=== RANDOM TESTS 10 NUMS ===${NC}"
for i in {1..5}; do
ARG=$(random_arg 10)
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
check_sort "$ARG" "$FLAG"
done
done
# =========================
# STRESS TESTS
# =========================
echo -e "\n${YELLOW}=== STRESS TESTS ===${NC}"
for N in 100 500; do
ARG=$(random_arg $N)
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
OPS=$($PS $FLAG $ARG | wc -l)
RES=$(echo "$($PS $FLAG $ARG)" | $CK $ARG 2>/dev/null)
RET=$?
echo "$FLAG $N numbers → ops=$OPS result=$RES return=$RET"
done
done
sorted_arg() {
seq 1 $1 | tr '\n' ' '
}
reverse_arg() {
seq $1 -1 1 | tr '\n' ' '
}
almost_sorted_arg() {
seq 1 $1 | shuf -n $1 --random-source=<(yes 42) | tr '\n' ' '
}
verdict() {
local avg=$1
local limit=$2
if (( avg <= limit )); then
echo -e "${GREEN}OK${NC}"
else
echo -e "${RED}KO${NC}"
fi
}
# =========================
# BENCHMARK 100 TESTS
# =========================
echo -e "\n${YELLOW}=== TEST STRUCTURÉ 100 ELEMENTS ===${NC}"
LIMIT_100=2000
for TYPE in sorted reverse almost; do
sum=0; min=999999; max=0
for i in {1..50}; do
case $TYPE in
sorted) ARG=$(sorted_arg 100) ;;
reverse) ARG=$(reverse_arg 100) ;;
almost) ARG=$(almost_sorted_arg 100) ;;
esac
C=$($PS $ARG | wc -l)
((sum+=C))
((C<min)) && min=$C
((C>max)) && max=$C
done
avg=$((sum/50))
result=$(verdict $avg $LIMIT_100)
echo "$TYPE → min=$min max=$max avg=$avg$result"
done
# =========================
# BENCHMARK 500 TESTS
# =========================
echo -e "\n${YELLOW}=== TEST STRUCTURÉ 500 ELEMENTS ===${NC}"
LIMIT_500=12000
for TYPE in sorted reverse almost; do
sum=0; min=999999; max=0
for i in {1..20}; do
case $TYPE in
sorted) ARG=$(sorted_arg 500) ;;
reverse) ARG=$(reverse_arg 500) ;;
almost) ARG=$(almost_sorted_arg 500) ;;
esac
C=$($PS $ARG | wc -l)
((sum+=C))
((C<min)) && min=$C
((C>max)) && max=$C
done
avg=$((sum/20))
result=$(verdict $avg $LIMIT_500)
echo "$TYPE → min=$min max=$max avg=$avg$result"
done
# =========================
# LEAKS
# =========================
echo -e "\n${YELLOW}=== LEAKS ===${NC}"
for ARG in "2 1" "1 2 3" "1 2 2" "$(random_arg 100)"; do
for FLAG in "" "--simple" "--medium" "--complex" "--adaptive"; do
check_leak "$ARG" "$FLAG"
done
done
# =========================
# RESULT FINAL
# =========================
if [ $fail -eq 0 ]; then
echo -e "\n${GREEN}ALL TESTS PASSED 🎉${NC}"
else
echo -e "\n${RED}SOME TESTS FAILED ❌${NC}"
fi
# Tests des checkers (checker_linux et bonus checker)
PS=./push_swap
CK_LINUX=./checker_linux
CK_BONUS=./checker
VALGRIND="valgrind --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=42"
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m"
fail=0
random_arg() {
N=$1
seq -$((N*10)) $((N*10)) | shuf | head -n $N | tr '\n' ' '
}
check_leak_checker() {
ARG="$1"
OPS="$2"
CHECKER="$3"
echo "$OPS" | $VALGRIND $CHECKER $ARG >/dev/null 2>&1
if [ $? -eq 42 ]; then
echo -e "${RED}[LEAK ❌]${NC} $(basename $CHECKER) '$ARG'"
fail=1
else
echo -e "${GREEN}[NO LEAK ✅]${NC} $(basename $CHECKER) '$ARG'"
fi
}
# =========================
# TESTS CHECKER_LINUX
# =========================
echo -e "${BLUE}╔════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ TESTS CHECKER_LINUX ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════╝${NC}"
echo -e "\n${YELLOW}=== CHECKER_LINUX: Erreurs ===${NC}"
# Doublons
RES=$(echo "" | $CK_LINUX 1 2 1 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Doublons → Error"
else
echo -e "${RED}[KO]${NC} Doublons (got: '$RES')"
fail=1
fi
# Lettre
RES=$(echo "" | $CK_LINUX 1 a 3 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Lettre → Error"
else
echo -e "${RED}[KO]${NC} Lettre (got: '$RES')"
fail=1
fi
# Overflow
RES=$(echo "" | $CK_LINUX 2147483648 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Overflow → Error"
else
echo -e "${RED}[KO]${NC} Overflow (got: '$RES')"
fail=1
fi
# Opération invalide
RES=$(echo "invalid_op" | $CK_LINUX 1 2 3 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Op invalide → Error"
else
echo -e "${RED}[KO]${NC} Op invalide (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== CHECKER_LINUX: Pas d'argument ===${NC}"
OUT=$(echo "" | $CK_LINUX 2>&1)
RET=$?
if [ -z "$OUT" ] && [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} Pas d'argument"
else
echo -e "${RED}[KO]${NC} Pas d'argument (out='$OUT', ret=$RET)"
fail=1
fi
echo -e "\n${YELLOW}=== CHECKER_LINUX: Opérations valides ===${NC}"
# sa sur "2 1"
RES=$(echo "sa" | $CK_LINUX 2 1)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}[OK]${NC} sa sur '2 1' → OK"
else
echo -e "${RED}[KO]${NC} sa sur '2 1' (got: '$RES')"
fail=1
fi
# Séquence complète
ARG="3 2 1"
OPS=$($PS $ARG)
RES=$(echo "$OPS" | $CK_LINUX $ARG)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}[OK]${NC} Séquence '$ARG' → OK"
else
echo -e "${RED}[KO]${NC} Séquence '$ARG' (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== CHECKER_LINUX: Pile non triée ===${NC}"
RES=$(echo "sa" | $CK_LINUX 3 2 1)
if [ "$RES" = "KO" ]; then
echo -e "${GREEN}[OK]${NC} Détecte pile non triée → KO"
else
echo -e "${RED}[KO]${NC} Pile non triée (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== CHECKER_LINUX: Stress test ===${NC}"
for N in 10 100 500; do
ARG=$(random_arg $N)
OPS=$($PS $ARG 2>/dev/null)
RES=$(echo "$OPS" | $CK_LINUX $ARG 2>/dev/null)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}${NC} $N nombres → OK"
else
echo -e "${RED}${NC} $N nombres → $RES"
fail=1
fi
done
echo -e "\n${YELLOW}=== CHECKER_LINUX: Leaks ===${NC}"
check_leak_checker "2 1" "sa" "$CK_LINUX"
check_leak_checker "1 2 3" "" "$CK_LINUX"
check_leak_checker "1 2 2" "" "$CK_LINUX"
ARG=$(random_arg 50)
OPS=$($PS $ARG)
check_leak_checker "$ARG" "$OPS" "$CK_LINUX"
# =========================
# TESTS BONUS CHECKER
# =========================
if [ -f "$CK_BONUS" ]; then
echo -e "\n${BLUE}╔════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ TESTS BONUS CHECKER ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════╝${NC}"
echo -e "\n${YELLOW}=== BONUS: Erreurs ===${NC}"
# Doublons
RES=$(echo "" | $CK_BONUS 1 2 1 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Doublons → Error"
else
echo -e "${RED}[KO]${NC} Doublons (got: '$RES')"
fail=1
fi
# Lettre
RES=$(echo "" | $CK_BONUS 1 a 3 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Lettre → Error"
else
echo -e "${RED}[KO]${NC} Lettre (got: '$RES')"
fail=1
fi
# Overflow
RES=$(echo "" | $CK_BONUS 2147483648 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Overflow → Error"
else
echo -e "${RED}[KO]${NC} Overflow (got: '$RES')"
fail=1
fi
# Opération invalide
RES=$(echo "xx" | $CK_BONUS 3 2 1 2>&1)
if [ "$RES" = "Error" ]; then
echo -e "${GREEN}[OK]${NC} Op invalide → Error"
else
echo -e "${RED}[KO]${NC} Op invalide (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Pas d'argument ===${NC}"
OUT=$(echo "" | $CK_BONUS 2>&1)
RET=$?
if [ -z "$OUT" ] && [ $RET -eq 0 ]; then
echo -e "${GREEN}[OK]${NC} Pas d'argument"
else
echo -e "${RED}[KO]${NC} Pas d'argument (out='$OUT', ret=$RET)"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Pile déjà triée ===${NC}"
RES=$(echo "" | $CK_BONUS 1 2 3)
if [ "$RES" = "OK" ] || [ -z "$RES" ]; then
echo -e "${GREEN}[OK]${NC} Pile triée (0 ops) → OK"
else
echo -e "${RED}[KO]${NC} Pile triée (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Opérations valides ===${NC}"
# sa sur "2 1"
RES=$(echo "sa" | $CK_BONUS 2 1)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}[OK]${NC} sa sur '2 1' → OK"
else
echo -e "${RED}[KO]${NC} sa sur '2 1' (got: '$RES')"
fail=1
fi
# pb sur pile vide b
RES=$(echo "pb" | $CK_BONUS 2 1 2>/dev/null)
if [ "$RES" = "KO" ]; then
echo -e "${GREEN}[OK]${NC} pb (pile b reste vide) → KO"
else
echo -e "${YELLOW}[INFO]${NC} pb sur pile vide (got: '$RES')"
fi
# Séquence complète
ARG="3 2 1"
OPS=$($PS $ARG)
RES=$(echo "$OPS" | $CK_BONUS $ARG)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}[OK]${NC} Séquence '$ARG' → OK"
else
echo -e "${RED}[KO]${NC} Séquence '$ARG' (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Pile non triée ===${NC}"
RES=$(echo "sa" | $CK_BONUS 3 2 1)
if [ "$RES" = "KO" ]; then
echo -e "${GREEN}[OK]${NC} Détecte pile non triée → KO"
else
echo -e "${RED}[KO]${NC} Pile non triée (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Comparaison avec checker_linux ===${NC}"
passed=0
total=20
for i in $(seq 1 $total); do
ARG=$(random_arg 10)
OPS=$($PS $ARG 2>/dev/null)
RES_LINUX=$(echo "$OPS" | $CK_LINUX $ARG 2>/dev/null)
RES_BONUS=$(echo "$OPS" | $CK_BONUS $ARG 2>/dev/null)
if [ "$RES_LINUX" = "$RES_BONUS" ]; then
((passed++))
else
echo -e "${RED}[DIFF]${NC} Args: $(echo $ARG | cut -d' ' -f1-5)..."
echo " Linux: $RES_LINUX"
echo " Bonus: $RES_BONUS"
fail=1
fi
done
echo "$passed/$total tests concordent"
echo -e "\n${YELLOW}=== BONUS: Parsing ===${NC}"
# Avec guillemets
RES=$(echo "" | eval "$CK_BONUS \"1 2 3\"")
if [ "$RES" = "OK" ] || [ -z "$RES" ]; then
echo -e "${GREEN}[OK]${NC} Parsing guillemets"
else
echo -e "${RED}[KO]${NC} Parsing guillemets (got: '$RES')"
fail=1
fi
# Espaces multiples
if [ "$RES" = "OK" ] || [ -z "$RES" ]; then
echo -e "${GREEN}[OK]${NC} Espaces multiples"
else
echo -e "${RED}[KO]${NC} Espaces multiples (got: '$RES')"
fail=1
fi
echo -e "\n${YELLOW}=== BONUS: Stress test ===${NC}"
for N in 10 100 500; do
ARG=$(random_arg $N)
OPS=$($PS $ARG 2>/dev/null)
RES=$(echo "$OPS" | $CK_BONUS $ARG 2>/dev/null)
if [ "$RES" = "OK" ]; then
echo -e "${GREEN}${NC} $N nombres → OK"
else
echo -e "${RED}${NC} $N nombres → $RES"
fail=1
fi
done
echo -e "\n${YELLOW}=== BONUS: Toutes les opérations ===${NC}"
# Test sa
RES=$(echo "sa" | $CK_BONUS 3 1 2)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} sa" || echo -e "${RED}[KO]${NC} sa (got: '$RES')"
# Test sb (avec pb avant)
RES=$(echo -e "pb\nsb" | $CK_BONUS 1 3 2)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} sb" || echo -e "${RED}[KO]${NC} sb (got: '$RES')"
# Test ss
RES=$(echo -e "pb\nss" | $CK_BONUS 2 1 4 3)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} ss" || echo -e "${RED}[KO]${NC} ss (got: '$RES')"
# Test pa
RES=$(echo -e "pb\npa" | $CK_BONUS 1 2 3)
[ "$RES" = "OK" ] && echo -e "${GREEN}[OK]${NC} pa" || echo -e "${RED}[KO]${NC} pa (got: '$RES')"
# Test pb
RES=$(echo "pb" | $CK_BONUS 1 2 3)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} pb" || echo -e "${RED}[KO]${NC} pb (got: '$RES')"
# Test ra
RES=$(echo "ra" | $CK_BONUS 2 3 1)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} ra" || echo -e "${RED}[KO]${NC} ra (got: '$RES')"
# Test rb
RES=$(echo -e "pb\nrb" | $CK_BONUS 1 2 3)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} rb" || echo -e "${RED}[KO]${NC} rb (got: '$RES')"
# Test rr
RES=$(echo -e "pb\nrr" | $CK_BONUS 2 3 1)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} rr" || echo -e "${RED}[KO]${NC} rr (got: '$RES')"
# Test rra
RES=$(echo "rra" | $CK_BONUS 3 1 2)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} rra" || echo -e "${RED}[KO]${NC} rra (got: '$RES')"
# Test rrb
RES=$(echo -e "pb\nrrb" | $CK_BONUS 1 2 3)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} rrb" || echo -e "${RED}[KO]${NC} rrb (got: '$RES')"
# Test rrr
RES=$(echo -e "pb\nrrr" | $CK_BONUS 3 1 2)
[ "$RES" = "KO" ] && echo -e "${GREEN}[OK]${NC} rrr" || echo -e "${RED}[KO]${NC} rrr (got: '$RES')"
echo -e "\n${YELLOW}=== BONUS: Leaks ===${NC}"
check_leak_checker "2 1" "sa" "$CK_BONUS"
check_leak_checker "1 2 3" " $CK_BONUS"
check_leak_checker "1 2 2" "" "$CK_BONUS"
ARG=$(random_arg 50)
OPS=$($PS $ARG)
check_leak_checker "$ARG" "$OPS" "$CK_BONUS"
else
echo -e "\n${YELLOW}⚠ Checker bonus non trouvé (./checker)${NC}"
fi
# =========================
# RESULT FINAL
# =========================
echo -e "\n${BLUE}═══════════════════════════════════════${NC}"
if [ $fail -eq 0 ]; then
echo -e "${GREEN}✓ ALL CHECKER TESTS PASSED 🎉${NC}"
else
echo -e "${RED}✗ SOME CHECKER TESTS FAILED ❌${NC}"
fi
echo -e "${BLUE}═══════════════════════════════════════${NC}"
# =========================
# TEST PUSH_SWAP AVEC CHECKER_LINUX (VERSION ÉTENDUE)
# =========================
echo -e "\n${YELLOW}=== TEST PUSH_SWAP AVEC CHECKER_LINUX (ÉTENDU) ===${NC}"
for N in 3 5 10 50 100 500; do
ARG=$(random_arg $N)
echo -e "\nPile aléatoire ($N éléments): $ARG"
# Générer les opérations avec push_swap
OPS=$($PS $ARG 2>/dev/null)
N_OPS=$(echo "$OPS" | wc -l)
# Vérifier avec checker_linux
if [ -z "$OPS" ]; then
# Pas d'opérations → pile déjà triée
echo "" | $CK $ARG >/dev/null 2>&1
CK_RET=$?
else
printf "%s\n" "$OPS" | $CK $ARG >/dev/null 2>&1
CK_RET=$?
fi
echo "Nombre d'opérations: $N_OPS"
echo "Checker Linux return code: $CK_RET"
if [ $CK_RET -eq 0 ]; then
echo -e "${GREEN}[OK] Pile triée correctement${NC}"
elif [ $CK_RET -eq 1 ]; then
echo -e "${RED}[KO] Pile non triée${NC}"
fail=1
else
echo -e "${RED}[ERROR] Checker a retourné une erreur${NC}"
fail=1
fi
done
# =========================
# TESTS DE PILES INVERSÉES
# =========================
echo -e "\n${YELLOW}=== TESTS PILES INVERSÉES ===${NC}"
for ARG in "3 2 1" "5 4 3 2 1" "10 9 8 7 6 5 4 3 2 1"; do
OPS=$($PS $ARG 2>/dev/null)
N_OPS=$(echo "$OPS" | wc -l)
printf "%s\n" "$OPS" | $CK $ARG >/dev/null 2>&1
CK_RET=$?
if [ $CK_RET -eq 0 ]; then
echo -e "${GREEN}[OK] Pile inversée triée correctement ($ARG, ops=$N_OPS)${NC}"
else
echo -e "${RED}[KO] Pile inversée non triée ($ARG)${NC}"
fail=1
fi
done
# =========================
# TESTS DE VALEURS LIMITES
# =========================
echo -e "\n${YELLOW}=== TESTS VALEURS LIMITES ===${NC}"
for ARG in "2147483647 -2147483648 0" ; do
OPS=$($PS $ARG 2>/dev/null)
N_OPS=$(echo "$OPS" | wc -l)
printf "%s\n" "$OPS" | $CK $ARG >/dev/null 2>&1
CK_RET=$?
if [ $CK_RET -eq 0 ]; then
echo -e "${GREEN}[OK] Valeurs limites triées correctement ($ARG, ops=$N_OPS)${NC}"
else
echo -e "${RED}[KO] Valeurs limites non triées ($ARG)${NC}"
fail=1
fi
done
# =========================
# TESTS D'ENTRÉES INVALIDES
# =========================
echo -e "\n${YELLOW}=== TESTS ENTRÉES INVALIDES ===${NC}"
for ARG in "1 2 2" "1 a 3" "2147483648 -2147483649"; do
RES=$($PS $ARG 2>&1)
if echo "$RES" | grep -q "Error"; then
echo -e "${GREEN}[OK] Entrée invalide détectée ($ARG)${NC}"
else
echo -e "${RED}[KO] Entrée invalide non détectée ($ARG)${NC}"
fail=1
fi
done