mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
Compare commits
93 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2652cacc7 | ||
|
|
ae1a91c7c9 | ||
| 798886976c | |||
| e2cbea4df9 | |||
| 88b7525bac | |||
|
|
3887e3b4a5 | ||
|
|
a3a462a515 | ||
|
|
587ea1750c | ||
|
|
afa8785379 | ||
| 0e7d1dce50 | |||
|
|
469320374d | ||
| ddd5b11a7a | |||
|
|
854d699780 | ||
|
|
52d868904f | ||
|
|
81ed92dc88 | ||
|
|
c6f04ab48f | ||
|
|
e266b6d6e9 | ||
|
|
59a80e6c9b | ||
| e9c0894f92 | |||
|
|
381bd66c2b | ||
| c2e7da01f4 | |||
|
|
75c331e7ea | ||
| 78ec8a3fa4 | |||
|
|
c323449e3a | ||
|
|
e0712aa8c4 | ||
| 2c805c9506 | |||
|
|
6b5347c997 | ||
|
|
89b7709d2d | ||
|
|
00af75f98f | ||
|
|
29c60fef6a | ||
| 60011b1b11 | |||
| f92eba4f03 | |||
| 1ebb32f58d | |||
| 52220b5b49 | |||
|
|
e8b2a90dc1 | ||
|
|
68dbe33733 | ||
| 82111f59ad | |||
|
|
fe69eeb821 | ||
|
|
02993cded2 | ||
| f4824f2984 | |||
|
|
b2f9912963 | ||
|
|
7d2fed6e76 | ||
|
|
696f1ca2d5 | ||
|
|
cfdf913c4b | ||
|
|
f8f1b43539 | ||
|
|
9c3f91dcb9 | ||
|
|
97ce3bf40b | ||
| 5ae963bb09 | |||
|
|
ccccc3b640 | ||
|
|
69dc80e498 | ||
| 15ca9cfe3f | |||
|
|
376f457f7d | ||
|
|
ad855e5915 | ||
| 7db1844447 | |||
| 929d7d4da5 | |||
| 319ed2521e | |||
|
|
be9d265fb5 | ||
|
|
439ac6948d | ||
| dae8fc84ea | |||
|
|
c138a43f0b | ||
|
|
90d45b07ae | ||
| b6a36a1696 | |||
|
|
0e5ef61de7 | ||
|
|
c131843bc0 | ||
|
|
32a0668b62 | ||
|
|
0df082065e | ||
|
|
00e01ed1f9 | ||
|
|
a405d7dad5 | ||
|
|
ee4994dcc6 | ||
| 9a20072f36 | |||
| 38eae5ab16 | |||
|
|
f32a316c5f | ||
|
|
173c11c2e3 | ||
|
|
42ed0f7579 | ||
|
|
4af87fa98a | ||
| 567a9c61ab | |||
| bb39e7d0ec | |||
| 2ba9029955 | |||
| 765ec373e5 | |||
| 335827d6b6 | |||
| 0dcbd8aaab | |||
| 4cb2fd3244 | |||
|
|
9bc53094a9 | ||
|
|
8dbf9f6c6e | ||
| b47b307663 | |||
| c799dd65ee | |||
|
|
2f4498d2c5 | ||
| 2c6d8d4095 | |||
|
|
6942548108 | ||
|
|
17f9a5affd | ||
|
|
e1351b4d2f | ||
|
|
93e8c10b27 | ||
|
|
0d9b46c748 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -58,7 +58,12 @@ dkms.conf
|
|||||||
*.swp
|
*.swp
|
||||||
|
|
||||||
# Executable
|
# Executable
|
||||||
pushswap
|
push_swap
|
||||||
|
checker
|
||||||
|
checker_linux
|
||||||
|
|
||||||
# File obj
|
# File obj
|
||||||
obj/
|
obj/
|
||||||
|
|
||||||
|
#playtest
|
||||||
|
playtest.sh
|
||||||
|
|||||||
86
Makefile
86
Makefile
@@ -14,21 +14,35 @@ PARS_DIR = parsing
|
|||||||
|
|
||||||
MEDIUM_DIR = medium
|
MEDIUM_DIR = medium
|
||||||
|
|
||||||
INCLUDES = headers
|
COMPLEX_DIR = radix
|
||||||
|
|
||||||
|
FLAGS_DIR = flags
|
||||||
|
|
||||||
|
CHECKER_DIR = check_error
|
||||||
|
|
||||||
|
INCLUDES = includes
|
||||||
|
|
||||||
#============================
|
#============================
|
||||||
# ALL FILES WITHOUT PATH
|
# ALL FILES WITHOUT PATH
|
||||||
#============================
|
#============================
|
||||||
|
|
||||||
SRC = main.c test_one.c
|
SRC = main.c ft_putnbr.c secure_write.c
|
||||||
|
|
||||||
INSERTION = insertion.c
|
INSERTION = insertion.c
|
||||||
|
|
||||||
PARSING = ft_atoi.c parsing.c parsing_2.c
|
FLAGS_FILES = algorithms_sort.c flag.c bench.c
|
||||||
|
|
||||||
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.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 indexer.c
|
||||||
|
|
||||||
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.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 \
|
||||||
|
move_patha.c move_pathb.c
|
||||||
|
|
||||||
|
COMPLEX_ALGO = radix.c
|
||||||
|
|
||||||
ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c
|
ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c
|
||||||
|
|
||||||
@@ -38,7 +52,8 @@ ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c
|
|||||||
|
|
||||||
ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \
|
ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \
|
||||||
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \
|
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \
|
||||||
$(INSERT_DIR)/$(INSERTION)
|
$(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) \
|
||||||
|
$(FLAGS_DIR)/$(FLAGS_FILES) $(CHECKER_DIR)/$(CHECKER_FILES)
|
||||||
|
|
||||||
OBJ_DIR = obj
|
OBJ_DIR = obj
|
||||||
|
|
||||||
@@ -51,7 +66,33 @@ NAME = push_swap
|
|||||||
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
|
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
|
||||||
DEP = $(OBJ:.o=.d)
|
DEP = $(OBJ:.o=.d)
|
||||||
|
|
||||||
.PHONY: all clean fclean re
|
#============================
|
||||||
|
# BONUS CONFIG
|
||||||
|
#============================
|
||||||
|
|
||||||
|
BONUS_DIR = bonus
|
||||||
|
|
||||||
|
GNL_D = GNL
|
||||||
|
|
||||||
|
GNL_DIR = bonus/GNL
|
||||||
|
|
||||||
|
BONUS_FILES = checker_bonus.c
|
||||||
|
|
||||||
|
GNL_FILES = get_next_line.c get_next_line_utils.c
|
||||||
|
|
||||||
|
SRC_FOR_BONUS = ft_putnbr.c secure_write.c
|
||||||
|
|
||||||
|
ALL_BONUS_FILES = $(SRC_FOR_BONUS) $(BONUS_DIR)/$(BONUS_FILES) $(BONUS_DIR)/$(GNL_D)/$(GNL_FILES) \
|
||||||
|
$(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \
|
||||||
|
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \
|
||||||
|
$(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) \
|
||||||
|
$(FLAGS_DIR)/$(FLAGS_FILES) $(CHECKER_DIR)/$(CHECKER_FILES)
|
||||||
|
|
||||||
|
BONUS_OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_BONUS_FILES:.c=.o)))
|
||||||
|
|
||||||
|
NAME_BONUS = checker
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re bonus
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
@@ -61,8 +102,11 @@ $(NAME): $(OBJ)
|
|||||||
@echo "======= PUSH SWAP COMPILED ========="
|
@echo "======= PUSH SWAP COMPILED ========="
|
||||||
@echo "===================================="
|
@echo "===================================="
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
|
$(NAME_BONUS): $(BONUS_OBJ)
|
||||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
@$(CC) $(CFLAGS) $(BONUS_OBJ) -o $(NAME_BONUS)
|
||||||
|
@echo "===================================="
|
||||||
|
@echo "======= CHECKER COMPILED ==========="
|
||||||
|
@echo "===================================="
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)
|
$(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR)
|
||||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
@@ -79,9 +123,30 @@ $(OBJ_DIR)/%.o: $(ALGO_DIR)/%.c | $(OBJ_DIR)
|
|||||||
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_DIR)/%.c | $(OBJ_DIR)
|
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_DIR)/%.c | $(OBJ_DIR)
|
||||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(COMPLEX_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
$(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR)
|
$(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR)
|
||||||
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(FLAGS_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(CHECKER_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(BONUS_DIR)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: $(BONUS_DIR)/$(GNL_D)/%.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJ_DIR_BONUS)/%.o: %.c | $(OBJ_DIR_BONUS)
|
||||||
|
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
|
||||||
|
|
||||||
$(OBJ_DIR):
|
$(OBJ_DIR):
|
||||||
@mkdir -p $(OBJ_DIR)
|
@mkdir -p $(OBJ_DIR)
|
||||||
|
|
||||||
@@ -93,9 +158,12 @@ clean:
|
|||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
@rm -f $(NAME)
|
@rm -f $(NAME)
|
||||||
|
@rm -f $(NAME_BONUS)
|
||||||
@echo "========== EXEC DELETED ============"
|
@echo "========== EXEC DELETED ============"
|
||||||
@echo "===================================="
|
@echo "===================================="
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
|
|
||||||
|
bonus: $(NAME_BONUS)
|
||||||
|
|
||||||
-include $(DEP)
|
-include $(DEP)
|
||||||
83
README.md
83
README.md
@@ -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
|
## 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.
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ static int to_insert(t_stacks *stacks, int sorted)
|
|||||||
a = stacks->a;
|
a = stacks->a;
|
||||||
while (i < sorted)
|
while (i < sorted)
|
||||||
{
|
{
|
||||||
if (stacks->b->value > a->previous->value && stacks->b->value <= a->value)
|
if (stacks->b->value > a->previous->value
|
||||||
|
&& stacks->b->value <= a->value)
|
||||||
return (i);
|
return (i);
|
||||||
a = a->previous;
|
a = a->previous;
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
@@ -14,67 +14,6 @@
|
|||||||
#include "medium_headers.h"
|
#include "medium_headers.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static int path_to_end(t_stacks *piles, int max_range, int range, char c)
|
|
||||||
{
|
|
||||||
t_stack *tmp;
|
|
||||||
int i;
|
|
||||||
int first_pass;
|
|
||||||
|
|
||||||
if (c == 'a')
|
|
||||||
tmp = piles->a;
|
|
||||||
else
|
|
||||||
tmp = piles->b;
|
|
||||||
tmp = tmp->previous;
|
|
||||||
i = 0;
|
|
||||||
first_pass = 1;
|
|
||||||
while (first_pass || tmp != piles->b->previous)
|
|
||||||
{
|
|
||||||
first_pass = 0;
|
|
||||||
if (in_range(tmp->value, max_range, range))
|
|
||||||
tmp = piles->b;
|
|
||||||
tmp = tmp->previous;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (i);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int path_to_start(t_stacks *piles, int max_range, int range, char c)
|
|
||||||
{
|
|
||||||
t_stack *tmp;
|
|
||||||
int i;
|
|
||||||
int first_pass;
|
|
||||||
|
|
||||||
if (c == 'a')
|
|
||||||
tmp = piles->a;
|
|
||||||
else
|
|
||||||
tmp = piles->b;
|
|
||||||
i = 0;
|
|
||||||
first_pass = 1;
|
|
||||||
while (first_pass || tmp != piles->b)
|
|
||||||
{
|
|
||||||
first_pass = 0;
|
|
||||||
if (in_range(tmp->value, max_range, range))
|
|
||||||
{
|
|
||||||
tmp = piles->b->previous;
|
|
||||||
}
|
|
||||||
tmp = tmp->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (i);
|
|
||||||
}
|
|
||||||
|
|
||||||
int wich_path(t_stacks *piles, int max_range, int range, char c)
|
|
||||||
{
|
|
||||||
int path_start;
|
|
||||||
int path_end;
|
|
||||||
|
|
||||||
path_start = path_to_start(piles, max_range, range, c);
|
|
||||||
path_end = path_to_end(piles, max_range, range, c);
|
|
||||||
if (path_start < path_end)
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int stack_len(t_stack *stack)
|
int stack_len(t_stack *stack)
|
||||||
{
|
{
|
||||||
t_stack *first;
|
t_stack *first;
|
||||||
@@ -90,16 +29,16 @@ int stack_len(t_stack *stack)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
|
||||||
{
|
{
|
||||||
t_tab *tmp;
|
t_tab *tmp;
|
||||||
|
|
||||||
tmp = preset;
|
tmp = preset;
|
||||||
while (piles->a)
|
while (stacks->a)
|
||||||
pb(piles);
|
pb(stacks);
|
||||||
while (preset)
|
while (preset)
|
||||||
{
|
{
|
||||||
push_range_to_b(piles, preset, range);
|
push_range_to_b(stacks, preset, range);
|
||||||
if (preset->next)
|
if (preset->next)
|
||||||
tmp = preset->next;
|
tmp = preset->next;
|
||||||
else
|
else
|
||||||
@@ -108,7 +47,5 @@ void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
|||||||
free(preset);
|
free(preset);
|
||||||
preset = tmp;
|
preset = tmp;
|
||||||
}
|
}
|
||||||
while (piles->b)
|
|
||||||
pa(piles);
|
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|||||||
94
algorithms/medium/move_patha.c
Normal file
94
algorithms/medium/move_patha.c
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
115
algorithms/medium/move_pathb.c
Normal file
115
algorithms/medium/move_pathb.c
Normal 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);
|
||||||
|
}
|
||||||
@@ -13,83 +13,79 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include "medium_headers.h"
|
#include "medium_headers.h"
|
||||||
|
|
||||||
static int number_move_reverse(t_stacks *piles, int value, char start_end)
|
void move_rr_rrr(t_stacks *stacks, int move, char c)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
t_stack *tmp;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
tmp = piles->a;
|
while (i < move && c == 's')
|
||||||
if (start_end == 's')
|
|
||||||
{
|
{
|
||||||
while (tmp->value < value)
|
rr(stacks);
|
||||||
{
|
i++;
|
||||||
tmp = tmp->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
while (i < move && c == 'e')
|
||||||
{
|
{
|
||||||
tmp = tmp->previous;
|
i++;
|
||||||
while (tmp->value > value)
|
rrr(stacks);
|
||||||
{
|
|
||||||
tmp = tmp->previous;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return (i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sort_path(t_stacks *piles, int value)
|
void put_in_order_ra_rra(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
int start_path;
|
int i;
|
||||||
int end_path;
|
|
||||||
|
|
||||||
start_path = number_move_reverse(piles, value, 's');
|
i = 0;
|
||||||
if (start_path == 0)
|
while (!check_order(stacks->a) && i < 50)
|
||||||
return (1);
|
{
|
||||||
end_path = number_move_reverse(piles, value, 'e');
|
ra(stacks);
|
||||||
if (start_path < end_path)
|
i++;
|
||||||
return (1);
|
}
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sort_little_pile(t_stacks *piles)
|
void path_rr_rrr(int value, t_stacks *stacks, t_tab *one_preset, int range)
|
||||||
{
|
{
|
||||||
if (!piles->a)
|
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'))
|
||||||
{
|
{
|
||||||
pa(piles);
|
move_a = move_left_to_right(value, stacks, 0);
|
||||||
return ;
|
if (move_a < final_move)
|
||||||
|
final_move = move_a;
|
||||||
|
move_rr_rrr(stacks, final_move, 's');
|
||||||
}
|
}
|
||||||
if (piles->a->previous->value < piles->b->value)
|
else if (wich_path_a(value, stacks) == 0
|
||||||
|
&& !wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||||
{
|
{
|
||||||
pa(piles);
|
move_a = move_right_to_left(value, stacks, 0);
|
||||||
ra(piles);
|
if (move_a < final_move)
|
||||||
return ;
|
final_move = move_a;
|
||||||
|
move_rr_rrr(stacks, final_move, 'e');
|
||||||
}
|
}
|
||||||
if (sort_path(piles, piles->b->value))
|
|
||||||
sort_from_left(piles);
|
|
||||||
else
|
|
||||||
sort_from_right(piles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
|
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)
|
while (one_preset->nb_in > 0)
|
||||||
{
|
{
|
||||||
if (wich_path(piles, one_preset->max_range, range, 'b'))
|
if (stacks->a)
|
||||||
{
|
{
|
||||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
value = get_value_finded(stacks, one_preset, range);
|
||||||
rb(piles);
|
path_rr_rrr(value, stacks, one_preset, range);
|
||||||
}
|
}
|
||||||
else
|
normal_move_path(stacks, one_preset, range);
|
||||||
{
|
move_ra_rra(stacks->b->index, stacks);
|
||||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
pa(stacks);
|
||||||
{
|
put_in_order_ra_rra(stacks);
|
||||||
rrb(piles);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort_little_pile(piles);
|
|
||||||
one_preset->nb_in--;
|
one_preset->nb_in--;
|
||||||
|
i++;
|
||||||
|
if (!check_order(stacks->a))
|
||||||
|
return ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,39 +11,33 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
|
||||||
void sort_from_left(t_stacks *piles)
|
int get_value_finded(t_stacks *stacks, t_tab *one_preset, int range)
|
||||||
{
|
{
|
||||||
int i;
|
int value;
|
||||||
|
t_stack *tmp;
|
||||||
|
|
||||||
i = 0;
|
tmp = assign_stack(stacks, 'b');
|
||||||
while (piles->b->value > piles->a->value)
|
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||||
{
|
{
|
||||||
ra(piles);
|
while (!in_range(tmp->index, one_preset->max_range, range))
|
||||||
i++;
|
tmp = tmp->next;
|
||||||
}
|
|
||||||
pa(piles);
|
|
||||||
while (i > 0)
|
|
||||||
{
|
|
||||||
rra(piles);
|
|
||||||
i--;
|
|
||||||
}
|
}
|
||||||
|
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 *piles)
|
t_stack *assign_stack(t_stacks *stacks, char c)
|
||||||
{
|
{
|
||||||
int i;
|
t_stack *tmp;
|
||||||
|
|
||||||
i = 0;
|
if (c == 'a')
|
||||||
while (piles->b->value < piles->a->previous->value)
|
tmp = stacks->a;
|
||||||
{
|
else
|
||||||
rra(piles);
|
tmp = stacks->b;
|
||||||
i++;
|
return (tmp);
|
||||||
}
|
|
||||||
pa(piles);
|
|
||||||
while (i >= 0)
|
|
||||||
{
|
|
||||||
ra(piles);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,16 @@ int get_first_lower(t_stack *first)
|
|||||||
{
|
{
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
int lower;
|
int lower;
|
||||||
|
int pass;
|
||||||
|
|
||||||
tmp = first;
|
tmp = first;
|
||||||
lower = tmp->value;
|
lower = tmp->index;
|
||||||
while (tmp->next != first)
|
pass = 1;
|
||||||
|
while (tmp != first || pass == 1)
|
||||||
{
|
{
|
||||||
if (lower > tmp->value)
|
pass = 0;
|
||||||
lower = tmp->value;
|
if (lower > tmp->index)
|
||||||
|
lower = tmp->index;
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
return (lower);
|
return (lower);
|
||||||
@@ -32,14 +35,19 @@ int get_next_lower(t_stack *first, int old_lower)
|
|||||||
{
|
{
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
int next_lower;
|
int next_lower;
|
||||||
|
int skip_first;
|
||||||
|
|
||||||
tmp = first;
|
tmp = first;
|
||||||
|
skip_first = 1;
|
||||||
next_lower = 2147483647;
|
next_lower = 2147483647;
|
||||||
while (tmp->next != first)
|
while (tmp != first || skip_first)
|
||||||
{
|
{
|
||||||
if (old_lower < tmp->value && tmp->value <= next_lower)
|
skip_first = 0;
|
||||||
|
if (old_lower < tmp->index && tmp->index <= next_lower)
|
||||||
{
|
{
|
||||||
next_lower = tmp->value;
|
next_lower = tmp->index;
|
||||||
|
if (next_lower == -2147483648)
|
||||||
|
return (next_lower);
|
||||||
}
|
}
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
@@ -51,6 +59,8 @@ int calcul_range(int value, int range)
|
|||||||
int max_range;
|
int max_range;
|
||||||
|
|
||||||
max_range = 0;
|
max_range = 0;
|
||||||
|
if (value == 2147483647)
|
||||||
|
return (value);
|
||||||
if (value < 0)
|
if (value < 0)
|
||||||
while (max_range > value)
|
while (max_range > value)
|
||||||
max_range -= range;
|
max_range -= range;
|
||||||
@@ -77,17 +87,18 @@ int get_number_in_range(int max_range, t_stack *a, int range)
|
|||||||
int nb_in;
|
int nb_in;
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
t_stack *first;
|
t_stack *first;
|
||||||
|
int pass;
|
||||||
|
|
||||||
nb_in = 0;
|
nb_in = 0;
|
||||||
tmp = a;
|
tmp = a;
|
||||||
|
pass = 1;
|
||||||
first = tmp;
|
first = tmp;
|
||||||
while (tmp->next != first)
|
while (tmp != first || pass == 1)
|
||||||
{
|
{
|
||||||
if (in_range(tmp->value, max_range, range))
|
pass = 0;
|
||||||
|
if (in_range(tmp->index, max_range, range))
|
||||||
nb_in++;
|
nb_in++;
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
if (in_range(tmp->value, max_range, range))
|
|
||||||
nb_in++;
|
|
||||||
return (nb_in);
|
return (nb_in);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,14 +16,17 @@
|
|||||||
static int get_max_number(t_stack *first)
|
static int get_max_number(t_stack *first)
|
||||||
{
|
{
|
||||||
int max;
|
int max;
|
||||||
|
int pass;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
|
|
||||||
a = first;
|
a = first;
|
||||||
max = a->value;
|
max = a->index;
|
||||||
while (a->next != first)
|
pass = 1;
|
||||||
|
while (a != first || pass == 1)
|
||||||
{
|
{
|
||||||
if (max < a->value)
|
pass = 0;
|
||||||
max = a->value;
|
if (max < a->index)
|
||||||
|
max = a->index;
|
||||||
a = a->next;
|
a = a->next;
|
||||||
}
|
}
|
||||||
return (max);
|
return (max);
|
||||||
@@ -33,13 +36,16 @@ static int get_min_number(t_stack *first)
|
|||||||
{
|
{
|
||||||
int min;
|
int min;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
|
int pass;
|
||||||
|
|
||||||
a = first;
|
a = first;
|
||||||
min = a->value;
|
min = a->index;
|
||||||
while (a->next != first)
|
pass = 1;
|
||||||
|
while (a != first || pass == 1)
|
||||||
{
|
{
|
||||||
if (min > a->value)
|
pass = 0;
|
||||||
min = a->value;
|
if (min > a->index)
|
||||||
|
min = a->index;
|
||||||
a = a->next;
|
a = a->next;
|
||||||
}
|
}
|
||||||
return (min);
|
return (min);
|
||||||
@@ -59,16 +65,16 @@ static int my_sqrt(int nb)
|
|||||||
|
|
||||||
int range_bucket(t_stack *first)
|
int range_bucket(t_stack *first)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
int diff;
|
long diff;
|
||||||
int sqrt;
|
int sqrt;
|
||||||
|
|
||||||
len = stack_len(first);
|
len = stack_len(first);
|
||||||
diff = (get_max_number(first) - get_min_number(first)) ;
|
diff = (long)get_max_number(first) - (long)get_min_number(first);
|
||||||
sqrt = my_sqrt(len);
|
sqrt = my_sqrt(len);
|
||||||
if (diff / sqrt < 2)
|
if (diff / sqrt < 2)
|
||||||
{
|
{
|
||||||
return (get_max_number(first));
|
return (get_max_number(first));
|
||||||
}
|
}
|
||||||
return ((get_max_number(first) - get_min_number(first)) / my_sqrt(len));
|
return (diff / my_sqrt(len));
|
||||||
}
|
}
|
||||||
|
|||||||
69
algorithms/radix/radix.c
Normal file
69
algorithms/radix/radix.c
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* radix.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/06 12:47:06 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 11:06:11 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static int still_unit_index(t_stacks *stacks, int unit)
|
||||||
|
{
|
||||||
|
t_stack *temp;
|
||||||
|
|
||||||
|
temp = stacks->a;
|
||||||
|
if (!temp || temp->index >= unit)
|
||||||
|
return (1);
|
||||||
|
temp = temp->next;
|
||||||
|
while (temp != stacks->a)
|
||||||
|
{
|
||||||
|
if (temp->index >= unit)
|
||||||
|
return (1);
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void push_by_number_to_b(t_stacks *stacks, int unit, int nb)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int s_len;
|
||||||
|
|
||||||
|
s_len = stack_a_len(stacks);
|
||||||
|
i = 0;
|
||||||
|
while (i < s_len && still_unit_index(stacks, unit))
|
||||||
|
{
|
||||||
|
if (stacks->a && (stacks->a->index % (unit * 10)) / unit == nb)
|
||||||
|
pb(stacks);
|
||||||
|
else
|
||||||
|
ra(stacks);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rec_sort(t_stacks *stacks, int unit)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!still_unit_index(stacks, unit))
|
||||||
|
return ;
|
||||||
|
while (i <= 9)
|
||||||
|
{
|
||||||
|
push_by_number_to_b(stacks, unit, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (stacks->b)
|
||||||
|
pa(stacks);
|
||||||
|
rec_sort(stacks, unit * 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
void radix(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
rec_sort(stacks, 1);
|
||||||
|
}
|
||||||
@@ -35,6 +35,11 @@ int r_to_lowest(t_stack *stack, int len)
|
|||||||
return (lowest_i);
|
return (lowest_i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void sort_two(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
sa(stacks);
|
||||||
|
}
|
||||||
|
|
||||||
void sort_three_a(t_stacks *stacks)
|
void sort_three_a(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (check_order(stacks->a))
|
if (check_order(stacks->a))
|
||||||
|
|||||||
112
bonus/GNL/get_next_line.c
Normal file
112
bonus/GNL/get_next_line.c
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/11/28 13:47:15 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static int del_before_nl(char buf[BUFFER_SIZE])
|
||||||
|
{
|
||||||
|
int nl_i;
|
||||||
|
int i;
|
||||||
|
char *temp;
|
||||||
|
|
||||||
|
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||||
|
if (nl_i >= 0)
|
||||||
|
{
|
||||||
|
temp = ft_substr_gnl(buf, nl_i + 1, BUFFER_SIZE - nl_i);
|
||||||
|
if (!temp)
|
||||||
|
return (-1);
|
||||||
|
ft_bzero(buf, BUFFER_SIZE);
|
||||||
|
i = 0;
|
||||||
|
while (temp[i])
|
||||||
|
{
|
||||||
|
buf[i] = temp[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *ft_read_one(char buf[BUFFER_SIZE], int fd, char *str)
|
||||||
|
{
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
str = ft_strjoin_new(str, buf, BUFFER_SIZE);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
ft_bzero(buf, BUFFER_SIZE);
|
||||||
|
temp = read(fd, buf, BUFFER_SIZE);
|
||||||
|
if (temp < 0)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
if ((!str || ft_strlen_1(str) == 0) && temp == 0)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
if (temp == 0)
|
||||||
|
return (str);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *extract_all_nl(char buf[BUFFER_SIZE], int fd, char *str, int nl_i)
|
||||||
|
{
|
||||||
|
int temp;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (nl_i < 0)
|
||||||
|
{
|
||||||
|
str = ft_read_one(buf, fd, str);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||||
|
if (nl_i < 0 && !ft_strlen_1(buf))
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
str = ft_strjoin_new(str, buf, nl_i);
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
temp = del_before_nl(buf);
|
||||||
|
if (temp < 0)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
static char buf[BUFFER_SIZE + 1];
|
||||||
|
int nl_i;
|
||||||
|
int temp;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||||
|
return (NULL);
|
||||||
|
nl_i = index_of_nl(buf, BUFFER_SIZE);
|
||||||
|
if (nl_i >= 0)
|
||||||
|
{
|
||||||
|
str = ft_substr_gnl(buf, 0, nl_i + 1);
|
||||||
|
temp = del_before_nl(buf);
|
||||||
|
if (temp < 0)
|
||||||
|
return (free(str), NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
str = extract_all_nl(buf, fd, ft_substr_gnl("", 0, 1), nl_i);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
102
bonus/GNL/get_next_line_utils.c
Normal file
102
bonus/GNL/get_next_line_utils.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/11/27 18:06:51 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int index_of_nl(char *str, int limit)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (-1);
|
||||||
|
i = 0;
|
||||||
|
while (i < limit && str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '\n')
|
||||||
|
return (i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlen_1(const char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
str = malloc(sizeof(char) * (ft_strlen_1(s1) + ft_strlen_1(s2) + 1));
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while (s1[i])
|
||||||
|
{
|
||||||
|
str[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (j <= limit && s2[j])
|
||||||
|
{
|
||||||
|
str[i + j] = s2[j];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
str[i + j] = '\0';
|
||||||
|
free((void *) s1);
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_substr_gnl(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char *sub_str;
|
||||||
|
size_t s_len;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (NULL);
|
||||||
|
s_len = ft_strlen_1(s);
|
||||||
|
if (start > s_len)
|
||||||
|
len = 0;
|
||||||
|
else if (s_len < (start + len))
|
||||||
|
len = s_len - start;
|
||||||
|
sub_str = malloc(sizeof(char) * (len + 1));
|
||||||
|
if (!sub_str)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
sub_str[i] = s[start + i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
sub_str[i] = '\0';
|
||||||
|
return (sub_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_bzero(void *s, size_t n)
|
||||||
|
{
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
*((unsigned char *) s) = '\0';
|
||||||
|
s++;
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
}
|
||||||
108
bonus/checker_bonus.c
Normal file
108
bonus/checker_bonus.c
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* checker_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 11:46:36 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 14:51:32 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "get_next_line.h"
|
||||||
|
#include "check_error.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static int apply_operation(t_stacks *stacks, char buf[1024])
|
||||||
|
{
|
||||||
|
if (ft_strncmp("sa\n", buf, ft_strlen(buf)))
|
||||||
|
return (sa(stacks), 1);
|
||||||
|
if (ft_strncmp("sb\n", buf, ft_strlen(buf)))
|
||||||
|
return (sb(stacks), 1);
|
||||||
|
if (ft_strncmp("ss\n", buf, ft_strlen(buf)))
|
||||||
|
return (ss(stacks), 1);
|
||||||
|
if (ft_strncmp("pa\n", buf, ft_strlen(buf)))
|
||||||
|
return (pa(stacks), 1);
|
||||||
|
if (ft_strncmp("pb\n", buf, ft_strlen(buf)))
|
||||||
|
return (pb(stacks), 1);
|
||||||
|
if (ft_strncmp("ra\n", buf, ft_strlen(buf)))
|
||||||
|
return (ra(stacks), 1);
|
||||||
|
if (ft_strncmp("rb\n", buf, ft_strlen(buf)))
|
||||||
|
return (rb(stacks), 1);
|
||||||
|
if (ft_strncmp("rr\n", buf, ft_strlen(buf)))
|
||||||
|
return (rr(stacks), 1);
|
||||||
|
if (ft_strncmp("rra\n", buf, ft_strlen(buf)))
|
||||||
|
return (rra(stacks), 1);
|
||||||
|
if (ft_strncmp("rrb\n", buf, ft_strlen(buf)))
|
||||||
|
return (rrb(stacks), 1);
|
||||||
|
if (ft_strncmp("rrr\n", buf, ft_strlen(buf)))
|
||||||
|
return (rrr(stacks), 1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_stacks_b_empty(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
if (stacks->b != NULL)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tester(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
char *buf;
|
||||||
|
|
||||||
|
buf = get_next_line(0);
|
||||||
|
while (buf)
|
||||||
|
{
|
||||||
|
if (!apply_operation(stacks, buf))
|
||||||
|
{
|
||||||
|
secure_write(2, "Error\n", 6);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
buf = get_next_line(0);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bonus(char **tab)
|
||||||
|
{
|
||||||
|
t_stacks *stacks;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
stacks = NULL;
|
||||||
|
len = len_split(tab);
|
||||||
|
stacks = init_stacks(len, tab, 0);
|
||||||
|
stacks->print = 0;
|
||||||
|
if (!stacks)
|
||||||
|
return (0);
|
||||||
|
tester(stacks);
|
||||||
|
free_all(stacks);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char **tab;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
return (write(2, "Error\n", 7));
|
||||||
|
tab = split_all(join_all(argc, argv));
|
||||||
|
if (!tab)
|
||||||
|
return (0);
|
||||||
|
if (!check_error_bonus(tab))
|
||||||
|
secure_write(2, "Error\n", 7);
|
||||||
|
else
|
||||||
|
bonus(tab);
|
||||||
|
free_tab(tab);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
50
check_error/check_error.c
Normal file
50
check_error/check_error.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_error.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 09:31:50 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 09:31:51 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "check_error.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
int verif_is_number(char **tab)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = len_split(tab);
|
||||||
|
if (tab[len - 1][0] == '-' && !ft_isdigit(tab[len - 1][1]))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_error_bonus(char **tab)
|
||||||
|
{
|
||||||
|
if (!verif_is_digit(tab, 0))
|
||||||
|
return (0);
|
||||||
|
if (!verif_overflow(tab, 0))
|
||||||
|
return (0);
|
||||||
|
if (!verif_double(tab, 0))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_error(char **tab, int mod)
|
||||||
|
{
|
||||||
|
if (!verif_flag(tab, mod))
|
||||||
|
return (0);
|
||||||
|
if (!verif_is_digit(tab, mod))
|
||||||
|
return (0);
|
||||||
|
if (!verif_overflow(tab, mod))
|
||||||
|
return (0);
|
||||||
|
if (!verif_double(tab, mod))
|
||||||
|
return (0);
|
||||||
|
if (!verif_is_number(tab))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
47
check_error/verif_double.c
Normal file
47
check_error/verif_double.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* verif_double.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 10:41:42 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 10:41:43 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "check_error.h"
|
||||||
|
|
||||||
|
static int is_double_in_tab(char **tab, int nb, int pos)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = pos;
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
if (nb == ft_atoi(tab[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int verif_double(char **tab, int mod)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int tmp;
|
||||||
|
|
||||||
|
i = wich_mod(mod);
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
if (tab[i + 1])
|
||||||
|
{
|
||||||
|
tmp = ft_atoi(tab[i]);
|
||||||
|
if (!is_double_in_tab(tab, tmp, i + 1))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
73
check_error/verif_flag.c
Normal file
73
check_error/verif_flag.c
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* verif_flag.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 09:51:11 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 09:51:12 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
static int is_exist_flag(char **tab, int pos)
|
||||||
|
{
|
||||||
|
int verif;
|
||||||
|
|
||||||
|
verif = 0;
|
||||||
|
if (ft_strncmp(tab[pos], "--bench", 30)
|
||||||
|
|| ft_strncmp(tab[pos], "--simple", 30)
|
||||||
|
|| ft_strncmp(tab[pos], "--medium", 30)
|
||||||
|
|| ft_strncmp(tab[pos], "--adaptive", 30)
|
||||||
|
|| ft_strncmp(tab[pos], "--complex", 30))
|
||||||
|
verif = 1;
|
||||||
|
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;
|
||||||
|
|
||||||
|
verif = 0;
|
||||||
|
if (mod == 1)
|
||||||
|
verif = (is_exist_flag(tab, 1));
|
||||||
|
else if (mod == 2)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int verif_double_flag(char **tab, int mod)
|
||||||
|
{
|
||||||
|
int verif;
|
||||||
|
|
||||||
|
verif = 1;
|
||||||
|
if (mod == 2 && ft_strncmp(tab[1], tab[2], 9))
|
||||||
|
verif = 0;
|
||||||
|
return (verif);
|
||||||
|
}
|
||||||
|
|
||||||
|
int verif_flag(char **tab, int mod)
|
||||||
|
{
|
||||||
|
if (mod == 0)
|
||||||
|
return (1);
|
||||||
|
if (verif_double_flag(tab, mod) && verif_exist_flag(tab, mod))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
44
check_error/verif_is_digit.c
Normal file
44
check_error/verif_is_digit.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* verif_is_digit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 10:25:23 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 10:25:24 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
int scan_str_is_digit(char *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
if ((tab[i] == '+' || tab[i] == '-') && ft_isdigit(tab[i + 1])
|
||||||
|
&& tab[i + 1])
|
||||||
|
i++;
|
||||||
|
if (!ft_isdigit(tab[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int verif_is_digit(char **tab, int mod)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = wich_mod(mod);
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
if (!scan_str_is_digit(tab[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
53
check_error/verif_overflow.c
Normal file
53
check_error/verif_overflow.c
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* verif_overflow.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 10:30:39 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 10:30:40 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
static int verif_atoi(const char *nptr)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int tmp;
|
||||||
|
int before;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp = 0;
|
||||||
|
before = 0;
|
||||||
|
if (ft_strncmp(nptr, "-2147483648", 15))
|
||||||
|
return (1);
|
||||||
|
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ')
|
||||||
|
i++;
|
||||||
|
if (nptr[i] == '-' || nptr[i] == '+')
|
||||||
|
i++;
|
||||||
|
while (nptr[i] >= '0' && nptr[i] <= '9')
|
||||||
|
{
|
||||||
|
before = tmp;
|
||||||
|
tmp = tmp * 10 + nptr[i] - '0';
|
||||||
|
if (before > tmp)
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int verif_overflow(char **tab, int mod)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = wich_mod(mod);
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
if (!verif_atoi(tab[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
65
flags/algorithms_sort.c
Normal file
65
flags/algorithms_sort.c
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* algorithms_sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 16:07:00 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "medium_headers.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
void simple(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = stack_a_len(stacks);
|
||||||
|
insertion(stacks, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void medium(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
t_tab *buckets;
|
||||||
|
int range;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = stack_a_len(stacks);
|
||||||
|
if (len == 2)
|
||||||
|
sort_two(stacks);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
range = range_bucket(stacks->a);
|
||||||
|
buckets = get_tabs(stacks->a, range);
|
||||||
|
if (!buckets)
|
||||||
|
return ;
|
||||||
|
bucket_algo(stacks, buckets, range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void complex(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
radix(stacks);
|
||||||
|
}
|
||||||
|
|
||||||
|
void adaptive(t_stacks *stacks, char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
float disorder;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (!ft_isdigit(tab[i][0]) && tab[i])
|
||||||
|
i++;
|
||||||
|
disorder = stacks->disorder;
|
||||||
|
if (disorder < 0.2)
|
||||||
|
simple(stacks);
|
||||||
|
else if (disorder < 0.5)
|
||||||
|
medium(stacks);
|
||||||
|
else
|
||||||
|
complex(stacks);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
117
flags/bench.c
Normal file
117
flags/bench.c
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* bench.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 13:59:52 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 12:13:36 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static void print_disorder(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
int nbr;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
stacks->disorder *= 10000;
|
||||||
|
nbr = (int) stacks->disorder;
|
||||||
|
str = ft_itoa(nbr);
|
||||||
|
if (!str)
|
||||||
|
exit (EXIT_FAILURE);
|
||||||
|
secure_write(2, "[bench] disorder: ", 18);
|
||||||
|
if (ft_strlen(str) <= 2)
|
||||||
|
secure_write(2, "0", 1);
|
||||||
|
else
|
||||||
|
secure_write(2, str, ft_strlen(str) - 2);
|
||||||
|
secure_write(2, ".", 1);
|
||||||
|
if (ft_strlen(str) == 1)
|
||||||
|
secure_write(2, "0", 1);
|
||||||
|
secure_write(2, &str[ft_strlen(str) - 2], ft_strlen(str) - 2);
|
||||||
|
secure_write(2, "%\n", 2);
|
||||||
|
stacks->disorder /= 10000;
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_algo(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
secure_write(2, "[bench] algo: ", 14);
|
||||||
|
if (stacks->algo == 0)
|
||||||
|
{
|
||||||
|
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(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(n√n)\n", 18);
|
||||||
|
else if (stacks->algo == 3)
|
||||||
|
secure_write(2, "Complex / O(nlogn)\n", 19);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_total_ops(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
unsigned int total_ops;
|
||||||
|
|
||||||
|
total_ops = 0;
|
||||||
|
total_ops += stacks->sa;
|
||||||
|
total_ops += stacks->sb;
|
||||||
|
total_ops += stacks->ss;
|
||||||
|
total_ops += stacks->pa;
|
||||||
|
total_ops += stacks->pb;
|
||||||
|
total_ops += stacks->ra;
|
||||||
|
total_ops += stacks->rb;
|
||||||
|
total_ops += stacks->rr;
|
||||||
|
total_ops += stacks->rra;
|
||||||
|
total_ops += stacks->rrb;
|
||||||
|
total_ops += stacks->rrr;
|
||||||
|
secure_write(2, "[bench] total_ops: ", 19);
|
||||||
|
ft_putnbr_fd((int) total_ops, 2);
|
||||||
|
secure_write(2, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_ops(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
secure_write(2, "[bench] sa: ", 12);
|
||||||
|
ft_putnbr_fd((int) stacks->sa, 2);
|
||||||
|
secure_write(2, " sb: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->sb, 2);
|
||||||
|
secure_write(2, " ss: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->ss, 2);
|
||||||
|
secure_write(2, " pa: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->pa, 2);
|
||||||
|
secure_write(2, " pb: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->pb, 2);
|
||||||
|
secure_write(2, "\n", 1);
|
||||||
|
secure_write(2, "[bench] ra: ", 12);
|
||||||
|
ft_putnbr_fd((int) stacks->ra, 2);
|
||||||
|
secure_write(2, " rb: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->rb, 2);
|
||||||
|
secure_write(2, " rr: ", 5);
|
||||||
|
ft_putnbr_fd((int) stacks->rr, 2);
|
||||||
|
secure_write(2, " rra: ", 6);
|
||||||
|
ft_putnbr_fd((int) stacks->rra, 2);
|
||||||
|
secure_write(2, " rrb: ", 6);
|
||||||
|
ft_putnbr_fd((int) stacks->rrb, 2);
|
||||||
|
secure_write(2, " rrr: ", 6);
|
||||||
|
ft_putnbr_fd((int) stacks->rrr, 2);
|
||||||
|
secure_write(2, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_bench(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
print_disorder(stacks);
|
||||||
|
print_algo(stacks);
|
||||||
|
print_total_ops(stacks);
|
||||||
|
print_ops(stacks);
|
||||||
|
}
|
||||||
86
flags/flag.c
Normal file
86
flags/flag.c
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* flag.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 16:07:29 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "flags.h"
|
||||||
|
|
||||||
|
int calcul_mod(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int mod;
|
||||||
|
|
||||||
|
mod = 0;
|
||||||
|
while (argv[mod] && !ft_isdigit(argv[mod][0])
|
||||||
|
&& !ft_isdigit(argv[mod][1]) && argv[mod][1] && mod < argc)
|
||||||
|
mod++;
|
||||||
|
mod--;
|
||||||
|
return (mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos_flag(char **argv, int mod)
|
||||||
|
{
|
||||||
|
if (mod == 1 || mod == 4)
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(argv[1], "--bench", 20))
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (mod == 2 || mod == 5)
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(argv[1], "--bench", 20))
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos_bench(char **argv, int mod)
|
||||||
|
{
|
||||||
|
if (mod == 1 || mod == 4)
|
||||||
|
{
|
||||||
|
if (ft_strncmp(argv[1], "--bench", 20))
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (mod == 2 || mod == 5)
|
||||||
|
{
|
||||||
|
if (ft_strncmp(argv[1], "--bench", 20))
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
adaptive(stacks, argv);
|
||||||
|
if (pos_b > 0)
|
||||||
|
stacks->bench = 1;
|
||||||
|
}
|
||||||
31
ft_putnbr.c
Normal file
31
ft_putnbr.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putnbr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 15:17:28 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 12:09:55 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
static void rec_ft_putnbr_fd(int nbr, int fd)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
if (!nbr)
|
||||||
|
return ;
|
||||||
|
rec_ft_putnbr_fd(nbr / 10, fd);
|
||||||
|
c = (nbr % 10) + '0';
|
||||||
|
secure_write(fd, &c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr_fd(int nbr, int fd)
|
||||||
|
{
|
||||||
|
if (!nbr)
|
||||||
|
secure_write(fd, "0", 1);
|
||||||
|
rec_ft_putnbr_fd(nbr, fd);
|
||||||
|
}
|
||||||
24
includes/check_error.h
Normal file
24
includes/check_error.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_error.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/09 10:16:35 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 10:16:37 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CHECK_ERROR_H
|
||||||
|
# define CHECK_ERROR_H
|
||||||
|
|
||||||
|
int verif_flag(char **tab, int mod);
|
||||||
|
int check_error(char **tab, int mod);
|
||||||
|
int verif_is_digit(char **tab, int mod);
|
||||||
|
int verif_overflow(char **tab, int mod);
|
||||||
|
int verif_double(char **tab, int mod);
|
||||||
|
int scan_str_is_digit(char *tab);
|
||||||
|
int check_error_bonus(char **tab);
|
||||||
|
|
||||||
|
#endif
|
||||||
26
includes/flags.h
Normal file
26
includes/flags.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* flags.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 15:32:54 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FLAGS_H
|
||||||
|
# define FLAGS_H
|
||||||
|
|
||||||
|
void simple(t_stacks *stacks);
|
||||||
|
void medium(t_stacks *stacks);
|
||||||
|
void complex(t_stacks *stacks);
|
||||||
|
void adaptive(t_stacks *stacks, char **tab);
|
||||||
|
void flags(int pos, int pos_b, char **argv, t_stacks *stacks);
|
||||||
|
int pos_bench(char **argv, int mod);
|
||||||
|
int pos_flag(char **argv, int mod);
|
||||||
|
int calcul_mod(int argc, char **argv);
|
||||||
|
void print_bench(t_stacks *stacks);
|
||||||
|
|
||||||
|
#endif
|
||||||
30
includes/get_next_line.h
Normal file
30
includes/get_next_line.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2025/11/27 18:05:13 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GET_NEXT_LINE_H
|
||||||
|
# define GET_NEXT_LINE_H
|
||||||
|
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
# define BUFFER_SIZE 42
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
|
||||||
|
size_t ft_strlen_1(const char *s);
|
||||||
|
char *ft_strjoin_new(char const *s1, char const *s2, size_t limit);
|
||||||
|
void ft_bzero(void *s, size_t n);
|
||||||
|
int index_of_nl(char *str, int limit);
|
||||||
|
char *ft_substr_gnl(char const *s, unsigned int start, size_t len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -13,6 +13,8 @@
|
|||||||
#ifndef MEDIUM_HEADERS_H
|
#ifndef MEDIUM_HEADERS_H
|
||||||
# define MEDIUM_HEADERS_H
|
# define MEDIUM_HEADERS_H
|
||||||
|
|
||||||
|
# include "push_swap.h"
|
||||||
|
|
||||||
typedef struct s_tab
|
typedef struct s_tab
|
||||||
{
|
{
|
||||||
int max_range;
|
int max_range;
|
||||||
@@ -21,13 +23,14 @@ typedef struct s_tab
|
|||||||
} t_tab;
|
} t_tab;
|
||||||
|
|
||||||
/* MEDIUM ALGO FILE */
|
/* MEDIUM ALGO FILE */
|
||||||
int wich_path(t_stacks *piles, int max_range, int range, char c);
|
int wich_path(t_stacks *stacks, int max_range, int range, char c);
|
||||||
int stack_len(t_stack *stack);
|
int stack_len(t_stack *stack);
|
||||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range);
|
void bucket_algo(t_stacks *stacks, t_tab *preset, int range);
|
||||||
/* SORT UTILS FILES */
|
/* SORT UTILS FILES */
|
||||||
void sort_from_left(t_stacks *piles);
|
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range);
|
||||||
void sort_from_right(t_stacks *piles);
|
void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range);
|
||||||
void push_range_to_b(t_stacks *piles, 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 */
|
/* MEDIUM UTILS FILES */
|
||||||
int range_bucket(t_stack *first);
|
int range_bucket(t_stack *first);
|
||||||
int get_first_lower(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);
|
int get_number_in_range(int max_range, t_stack *a, int range);
|
||||||
/* UTILS STRUCT TAB FILE */
|
/* UTILS STRUCT TAB FILE */
|
||||||
t_tab *get_tabs(t_stack *first, int range);
|
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
|
#endif
|
||||||
39
includes/parsing.h
Normal file
39
includes/parsing.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 11:27:39 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PARSING_H
|
||||||
|
# define PARSING_H
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include "push_swap.h"
|
||||||
|
|
||||||
|
int ft_atoi(const char *nptr);
|
||||||
|
int wich_mod(int mod);
|
||||||
|
t_stacks *init_stacks(int argc, char **argv, int mod);
|
||||||
|
int ft_strncmp(const char *s1, const char *s2, int n);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
|
void free_tab(char **tab);
|
||||||
|
int checker(int argc, char **argv);
|
||||||
|
int len_split(char **tab);
|
||||||
|
char *ft_itoa(int n);
|
||||||
|
int ft_isdigit(int c);
|
||||||
|
char *ft_strjoin(char const *s1, char const *s2);
|
||||||
|
int ft_strlcpy(char *dst, const char *src, int size);
|
||||||
|
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
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
/* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/15 14:34:45 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 15:32:04 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -16,18 +16,32 @@
|
|||||||
typedef struct s_stack
|
typedef struct s_stack
|
||||||
{
|
{
|
||||||
int value;
|
int value;
|
||||||
|
long long index;
|
||||||
struct s_stack *next;
|
struct s_stack *next;
|
||||||
struct s_stack *previous;
|
struct s_stack *previous;
|
||||||
} t_stack;
|
} t_stack;
|
||||||
|
|
||||||
typedef struct s_stacks
|
typedef struct s_stacks
|
||||||
{
|
{
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
t_stack *b;
|
t_stack *b;
|
||||||
|
int algo;
|
||||||
|
int bench;
|
||||||
|
int print;
|
||||||
|
float disorder;
|
||||||
|
unsigned int ra;
|
||||||
|
unsigned int rb;
|
||||||
|
unsigned int rr;
|
||||||
|
unsigned int rra;
|
||||||
|
unsigned int rrb;
|
||||||
|
unsigned int rrr;
|
||||||
|
unsigned int pa;
|
||||||
|
unsigned int pb;
|
||||||
|
unsigned int sa;
|
||||||
|
unsigned int sb;
|
||||||
|
unsigned int ss;
|
||||||
} t_stacks;
|
} t_stacks;
|
||||||
|
|
||||||
/* PRINT STACK FUNCTION*/
|
|
||||||
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
|
|
||||||
/*STACK_FUNCTIONS*/
|
/*STACK_FUNCTIONS*/
|
||||||
void pa(t_stacks *stacks);
|
void pa(t_stacks *stacks);
|
||||||
void pb(t_stacks *stacks);
|
void pb(t_stacks *stacks);
|
||||||
@@ -45,6 +59,7 @@ t_stack *new_stack(int value);
|
|||||||
void stack_add_back(t_stack **stack, t_stack *new);
|
void stack_add_back(t_stack **stack, t_stack *new);
|
||||||
void stack_add_front(t_stack **stack, t_stack *new);
|
void stack_add_front(t_stack **stack, t_stack *new);
|
||||||
void stack_clear_all(t_stack *stack, t_stack *first);
|
void stack_clear_all(t_stack *stack, t_stack *first);
|
||||||
|
void free_all(t_stacks *stacks);
|
||||||
/* STACKS LEN FILES */
|
/* STACKS LEN FILES */
|
||||||
int stack_a_len(t_stacks *stacks);
|
int stack_a_len(t_stacks *stacks);
|
||||||
int stack_b_len(t_stacks *stacks);
|
int stack_b_len(t_stacks *stacks);
|
||||||
@@ -52,6 +67,7 @@ int highest_stack_len(t_stacks *stacks);
|
|||||||
/* PRE SORT */
|
/* PRE SORT */
|
||||||
int r_to_lowest(t_stack *stack, int len);
|
int r_to_lowest(t_stack *stack, int len);
|
||||||
void sort_three_a(t_stacks *stacks);
|
void sort_three_a(t_stacks *stacks);
|
||||||
|
void sort_two(t_stacks *stacks);
|
||||||
/* ITERATE FILE */
|
/* ITERATE FILE */
|
||||||
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
|
void optimal_rotate(t_stacks *stacks, int i, int len, char stack);
|
||||||
/* COMPARE FILE */
|
/* COMPARE FILE */
|
||||||
@@ -63,6 +79,12 @@ int check_order(t_stack *stack);
|
|||||||
void insertion(t_stacks *stacks, int len);
|
void insertion(t_stacks *stacks, int len);
|
||||||
int test2(char **argv);
|
int test2(char **argv);
|
||||||
/* TEST FILE */
|
/* TEST FILE */
|
||||||
int test1(int argc, char **argv);
|
int test1(char **tab, int len, int mod);
|
||||||
|
|
||||||
|
/* RADIX */
|
||||||
|
void radix(t_stacks *stacks);
|
||||||
|
|
||||||
|
void secure_write(int fd, char *str, int len);
|
||||||
|
void ft_putnbr_fd(int nbr, int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
51
main.c
51
main.c
@@ -6,15 +6,60 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */
|
/* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */
|
||||||
/* Updated: 2025/12/14 16:55:43 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 15:34:59 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
#include "check_error.h"
|
||||||
|
#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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (argc > 1)
|
char **tab;
|
||||||
test1(argc, argv);
|
int mod;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
return (0);
|
||||||
|
tab = split_all(join_all(argc, argv));
|
||||||
|
if (!tab)
|
||||||
|
return (0);
|
||||||
|
len = len_split(tab);
|
||||||
|
mod = calcul_mod(len, tab);
|
||||||
|
if (check_error(tab, mod))
|
||||||
|
push_swap(tab, len, mod);
|
||||||
|
else
|
||||||
|
write(2, "Error\n", 6);
|
||||||
|
free_tab(tab);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
64
parsing/checker.c
Normal file
64
parsing/checker.c
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* checker.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 12:37:12 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 13:17:36 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "push_swap.h"
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
static int check_digits(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (!ft_isdigit(str[i]) && str[i] != ' ')
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int check_flags(char *str)
|
||||||
|
{
|
||||||
|
if (!ft_strncmp("--simple", str, ft_strlen(str)))
|
||||||
|
return (1);
|
||||||
|
else if (!ft_strncmp("--medium", str, ft_strlen(str)))
|
||||||
|
return (1);
|
||||||
|
else if (!ft_strncmp("--complex", str, ft_strlen(str)))
|
||||||
|
return (1);
|
||||||
|
else if (!ft_strncmp("--adaptative", str, ft_strlen(str)))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int checker(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int methods_flag;
|
||||||
|
int bench;
|
||||||
|
|
||||||
|
bench = 0;
|
||||||
|
methods_flag = 0;
|
||||||
|
i = 0;
|
||||||
|
while (++i < argc)
|
||||||
|
{
|
||||||
|
if (check_flags(argv[1]) && !methods_flag)
|
||||||
|
methods_flag = 1;
|
||||||
|
else if (!ft_strncmp("--bench", argv[1], ft_strlen(argv[1])) && !bench)
|
||||||
|
bench = 1;
|
||||||
|
else if (check_digits(argv[1]))
|
||||||
|
continue ;
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
@@ -1,42 +1,49 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* print_stacks.c :+: :+: :+: */
|
/* disorder.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/09 13:10:00 by dgaillet #+# #+# */
|
/* Created: 2026/01/09 11:05:37 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/09 13:36:03 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/09 14:53:45 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include <stdio.h>
|
#include "parsing.h"
|
||||||
|
|
||||||
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b)
|
static int strs_len(char **strs)
|
||||||
{
|
{
|
||||||
int a_len;
|
int i;
|
||||||
int b_len;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
a_len = stack_a_len(stacks);
|
while (strs[i])
|
||||||
b_len = stack_b_len(stacks);
|
i++;
|
||||||
while (i < len)
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
float compute_disorder(char **strs, int pos)
|
||||||
|
{
|
||||||
|
float mistakes;
|
||||||
|
float total_pairs;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
mistakes = 0;
|
||||||
|
total_pairs = 0;
|
||||||
|
i = pos;
|
||||||
|
while (i < strs_len(strs))
|
||||||
{
|
{
|
||||||
if (a_len >= len - i)
|
j = i + 1;
|
||||||
|
while (j < strs_len(strs))
|
||||||
{
|
{
|
||||||
printf("%d", a->value);
|
total_pairs += 1;
|
||||||
a = a->next;
|
if (ft_atoi(strs[i]) > ft_atoi(strs[j]))
|
||||||
|
mistakes += 1;
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
printf("\t");
|
|
||||||
if (b_len >= len - i)
|
|
||||||
{
|
|
||||||
printf("%d", b->value);
|
|
||||||
b = b->next;
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
printf("_\t_\nA\tB\n\n");
|
return (mistakes / total_pairs);
|
||||||
}
|
}
|
||||||
18
parsing/ft_isdigit.c
Normal file
18
parsing/ft_isdigit.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isdigit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 14:13:54 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 14:13:59 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isdigit(int c)
|
||||||
|
{
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
62
parsing/ft_itoa.c
Normal file
62
parsing/ft_itoa.c
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 14:13:00 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 14:13:27 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static size_t count_digits(int n)
|
||||||
|
{
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
size = 0;
|
||||||
|
if (n <= 0)
|
||||||
|
size++;
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
n /= 10;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
return (size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void insert_char(char *str, unsigned int nbr, size_t index)
|
||||||
|
{
|
||||||
|
while (nbr)
|
||||||
|
{
|
||||||
|
str[index--] = nbr % 10 + '0';
|
||||||
|
nbr /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_itoa(int n)
|
||||||
|
{
|
||||||
|
unsigned int nbr;
|
||||||
|
char *str;
|
||||||
|
size_t size;
|
||||||
|
|
||||||
|
nbr = n;
|
||||||
|
if (n < 0)
|
||||||
|
nbr = n * -1;
|
||||||
|
size = count_digits(n);
|
||||||
|
str = malloc(sizeof(char) * (size + 1));
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
str[size] = '\0';
|
||||||
|
if (nbr == 0)
|
||||||
|
str[0] = '0';
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
str[0] = '-';
|
||||||
|
insert_char(str, nbr, size - 1);
|
||||||
|
}
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
93
parsing/ft_split.c
Normal file
93
parsing/ft_split.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_split.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/11 15:34:04 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 14:55:21 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
static size_t count_words(char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t len;
|
||||||
|
size_t tester;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
len = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
tester = 0;
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
tester++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (tester > 0)
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t len_word(size_t i, char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
{
|
||||||
|
len++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_tab(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
free(tab[i++]);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char c)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
char **big_tab;
|
||||||
|
|
||||||
|
big_tab = malloc((count_words(s, c) + 1) * sizeof(char *));
|
||||||
|
if (!big_tab)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
j = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
while (s[i] == c && s[i])
|
||||||
|
i++;
|
||||||
|
if (s[i])
|
||||||
|
{
|
||||||
|
big_tab[j] = ft_substr(s, i, len_word(i, s, c));
|
||||||
|
if (!big_tab[j])
|
||||||
|
return (free_tab(big_tab), NULL);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
while (s[i] != c && s[i])
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
big_tab[j] = 0;
|
||||||
|
return (big_tab);
|
||||||
|
}
|
||||||
27
parsing/ft_strjoin.c
Normal file
27
parsing/ft_strjoin.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strjoin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/11 14:01:54 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/12 08:34:59 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
char *ft_strjoin(char const *s1, char const *s2)
|
||||||
|
{
|
||||||
|
int total_len;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
total_len = ft_strlen(s1) + ft_strlen(s2);
|
||||||
|
tmp = malloc((total_len + 1) * sizeof(char));
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
ft_strlcpy(tmp, s1, total_len + 1);
|
||||||
|
ft_strlcat(tmp, s2, total_len + 1);
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
38
parsing/ft_strlcat.c
Normal file
38
parsing/ft_strlcat.c
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlcat.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/10 09:22:05 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 11:03:31 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
int ft_strlcat(char *dst, const char *src, int size)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int len_src;
|
||||||
|
int len_dst;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
len_src = ft_strlen(src);
|
||||||
|
if (size == 0)
|
||||||
|
return (len_src);
|
||||||
|
j = ft_strlen(dst);
|
||||||
|
len_dst = ft_strlen(dst);
|
||||||
|
while (j < size - 1 && src[i])
|
||||||
|
{
|
||||||
|
dst[j] = src[i];
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dst[j] = '\0';
|
||||||
|
if (len_dst > size)
|
||||||
|
return (len_src + size);
|
||||||
|
return (len_dst + len_src);
|
||||||
|
}
|
||||||
31
parsing/ft_strlcpy.c
Normal file
31
parsing/ft_strlcpy.c
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/06 16:33:57 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 10:58:32 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
int ft_strlcpy(char *dst, const char *src, int size)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = ft_strlen(src);
|
||||||
|
i = 0;
|
||||||
|
if (size == 0)
|
||||||
|
return (len);
|
||||||
|
while (i < size - 1 && src[i])
|
||||||
|
{
|
||||||
|
dst[i] = src[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dst[i] = '\0';
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
@@ -1,20 +1,33 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* parsing.h :+: :+: :+: */
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */
|
/* Created: 2025/11/06 13:25:17 by mteriier #+# #+# */
|
||||||
/* Updated: 2026/01/07 08:03:10 by mteriier ### ########lyon.fr */
|
/* Updated: 2025/11/13 11:24:39 by mteriier ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef PARSING_H
|
#include <string.h>
|
||||||
# define PARSING_H
|
|
||||||
|
|
||||||
int ft_atoi(const char *nptr);
|
size_t ft_strlen(const char *s)
|
||||||
t_stacks *init_big_stacks2(int *tab, int len);
|
{
|
||||||
t_stacks *init_big_stacks(int argc, char **argv);
|
size_t i;
|
||||||
|
|
||||||
#endif
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int len_split(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
30
parsing/ft_strncmp.c
Normal file
30
parsing/ft_strncmp.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strncmp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/07 12:36:38 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2026/01/07 12:36:40 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_strncmp(const char *s1, const char *s2, int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (n == 0)
|
||||||
|
return (0);
|
||||||
|
while (i < n - 1 && s1[i] && s1[i] == s2[i])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
res = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||||
|
if (res == 0)
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
40
parsing/ft_substr.c
Normal file
40
parsing/ft_substr.c
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_substr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/11/11 13:46:41 by mteriier #+# #+# */
|
||||||
|
/* Updated: 2025/11/13 13:42:37 by mteriier ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "parsing.h"
|
||||||
|
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
size_t len_s;
|
||||||
|
size_t i;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (0);
|
||||||
|
len_s = ft_strlen(s);
|
||||||
|
if (len_s < start)
|
||||||
|
len = 0;
|
||||||
|
if (len > len_s - start)
|
||||||
|
len = len_s - start;
|
||||||
|
tmp = malloc((len + 1) * sizeof(char));
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
tmp[i] = s[start];
|
||||||
|
i++;
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
tmp[i] = '\0';
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
54
parsing/indexer.c
Normal file
54
parsing/indexer.c
Normal 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,22 +6,26 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */
|
/* Created: 2025/12/08 16:21:05 by mteriier #+# #+# */
|
||||||
/* Updated: 2025/12/09 10:19:17 by mteriier ### ########lyon.fr */
|
/* Updated: 2026/01/08 14:01:23 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static t_stack *parsing(int argc, char **argv)
|
int wich_mod(int mod)
|
||||||
|
{
|
||||||
|
return (mod % 3 + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_stack *parsing(int argc, char **argv, int mod)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int stock;
|
int stock;
|
||||||
t_stack *first;
|
t_stack *first;
|
||||||
t_stack *new;
|
t_stack *new;
|
||||||
|
|
||||||
i = 1;
|
i = wich_mod(mod);
|
||||||
first = NULL;
|
first = NULL;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
@@ -40,17 +44,42 @@ static t_stack *parsing(int argc, char **argv)
|
|||||||
return (first);
|
return (first);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_stacks *init_big_stacks(int argc, char **argv)
|
static void set_t_stacks(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
stacks->a = NULL;
|
||||||
|
stacks->b = NULL;
|
||||||
|
stacks->algo = 0;
|
||||||
|
stacks->bench = 0;
|
||||||
|
stacks->disorder = 0;
|
||||||
|
stacks->print = 1;
|
||||||
|
stacks->sa = 0;
|
||||||
|
stacks->sb = 0;
|
||||||
|
stacks->ss = 0;
|
||||||
|
stacks->pa = 0;
|
||||||
|
stacks->pb = 0;
|
||||||
|
stacks->ra = 0;
|
||||||
|
stacks->rb = 0;
|
||||||
|
stacks->rr = 0;
|
||||||
|
stacks->rra = 0;
|
||||||
|
stacks->rrb = 0;
|
||||||
|
stacks->rrr = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stacks *init_stacks(int argc, char **argv, int mod)
|
||||||
{
|
{
|
||||||
t_stacks *stacks;
|
t_stacks *stacks;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
|
|
||||||
stacks = malloc(sizeof(t_stacks));
|
stacks = malloc(sizeof(t_stacks));
|
||||||
stacks->a = NULL;
|
|
||||||
stacks->b = NULL;
|
|
||||||
if (!stacks)
|
if (!stacks)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
a = parsing(argc, argv);
|
set_t_stacks(stacks);
|
||||||
|
a = parsing(argc, argv, mod);
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
free(stacks);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
stacks->a = a;
|
stacks->a = a;
|
||||||
return (stacks);
|
return (stacks);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,53 +3,62 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* parsing_2.c :+: :+: :+: */
|
/* parsing_2.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/22 13:10:58 by mteriier #+# #+# */
|
/* Created: 2026/01/09 08:22:21 by mteriier #+# #+# */
|
||||||
/* Updated: 2025/12/22 13:11:21 by mteriier ### ########.fr */
|
/* Updated: 2026/01/09 08:22:22 by mteriier ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "push_swap.h"
|
#include "parsing.h"
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static t_stack *parsing2(int *tab, int len)
|
static char *free_join(char *tab)
|
||||||
{
|
{
|
||||||
|
free(tab);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *join_utils(char *start_tab, char *end_str)
|
||||||
|
{
|
||||||
|
char *tab;
|
||||||
|
char *tmp2;
|
||||||
|
|
||||||
|
tmp2 = ft_strjoin(end_str, " ");
|
||||||
|
if (!tmp2)
|
||||||
|
return (free_join(start_tab));
|
||||||
|
tab = ft_strjoin(start_tab, tmp2);
|
||||||
|
free(start_tab);
|
||||||
|
free(tmp2);
|
||||||
|
return (tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *join_all(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char *tab;
|
||||||
int i;
|
int i;
|
||||||
int stock;
|
|
||||||
t_stack *first;
|
|
||||||
t_stack *new;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
first = NULL;
|
tab = ft_strjoin(argv[i], " ");
|
||||||
while (i < len)
|
i++;
|
||||||
|
if (!tab)
|
||||||
|
return (NULL);
|
||||||
|
while (i < argc)
|
||||||
{
|
{
|
||||||
stock = tab[i];
|
tab = join_utils(tab, argv[i]);
|
||||||
new = new_stack(stock);
|
|
||||||
if (!new && !first)
|
|
||||||
return (NULL);
|
|
||||||
else if (!new)
|
|
||||||
{
|
|
||||||
stack_clear_all(first, first);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
stack_add_back(&first, new);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (first);
|
return (tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_stacks *init_big_stacks2(int *tab, int len)
|
char **split_all(char *tab)
|
||||||
{
|
{
|
||||||
t_stacks *stacks;
|
char **split;
|
||||||
t_stack *a;
|
|
||||||
|
|
||||||
stacks = malloc(sizeof(t_stacks));
|
if (!tab)
|
||||||
stacks->a = NULL;
|
|
||||||
stacks->b = NULL;
|
|
||||||
if (!stacks)
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
a = parsing2(tab, len);
|
split = ft_split(tab, ' ');
|
||||||
stacks->a = a;
|
free (tab);
|
||||||
return (stacks);
|
if (!split)
|
||||||
|
return (NULL);
|
||||||
|
return (split);
|
||||||
}
|
}
|
||||||
|
|||||||
22
secure_write.c
Normal file
22
secure_write.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* secure_write.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void secure_write(int fd, char *str, int len)
|
||||||
|
{
|
||||||
|
if (len < 0)
|
||||||
|
return ;
|
||||||
|
if (!str || write(fd, str, len) < 0)
|
||||||
|
exit (EXIT_FAILURE);
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */
|
/* Created: 2025/12/08 15:09:40 by mteriier #+# #+# */
|
||||||
/* Updated: 2025/12/12 11:39:33 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 13:57:56 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -30,7 +30,9 @@ void pa(t_stacks *stacks)
|
|||||||
stacks->b = stacks->b->next;
|
stacks->b = stacks->b->next;
|
||||||
}
|
}
|
||||||
stack_add_front(&(stacks->a), b_push);
|
stack_add_front(&(stacks->a), b_push);
|
||||||
write(1, "pa\n", 3);
|
stacks->pa++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "pa\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pb(t_stacks *stacks)
|
void pb(t_stacks *stacks)
|
||||||
@@ -49,5 +51,7 @@ void pb(t_stacks *stacks)
|
|||||||
stacks->a = stacks->a->next;
|
stacks->a = stacks->a->next;
|
||||||
}
|
}
|
||||||
stack_add_front(&(stacks->b), a_push);
|
stack_add_front(&(stacks->b), a_push);
|
||||||
write(1, "pb\n", 3);
|
stacks->pb++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "pb\n", 3);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */
|
/* Created: 2025/12/08 14:43:45 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/12 11:39:25 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 13:57:27 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,14 +17,18 @@ void rra(t_stacks *stacks)
|
|||||||
{
|
{
|
||||||
if (stacks && stacks->a && stacks->a->previous)
|
if (stacks && stacks->a && stacks->a->previous)
|
||||||
stacks->a = stacks->a->previous;
|
stacks->a = stacks->a->previous;
|
||||||
write(1, "rra\n", 4);
|
stacks->rra++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "rra\n", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rrb(t_stacks *stacks)
|
void rrb(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (stacks && stacks->b && stacks->b->previous)
|
if (stacks && stacks->b && stacks->b->previous)
|
||||||
stacks->b = stacks->b->previous;
|
stacks->b = stacks->b->previous;
|
||||||
write(1, "rrb\n", 4);
|
stacks->rrb++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "rrb\n", 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rrr(t_stacks *stacks)
|
void rrr(t_stacks *stacks)
|
||||||
@@ -33,5 +37,7 @@ void rrr(t_stacks *stacks)
|
|||||||
stacks->b = stacks->b->previous;
|
stacks->b = stacks->b->previous;
|
||||||
if (stacks && stacks->a && stacks->a->previous)
|
if (stacks && stacks->a && stacks->a->previous)
|
||||||
stacks->a = stacks->a->previous;
|
stacks->a = stacks->a->previous;
|
||||||
write(1, "rrr\n", 4);
|
stacks->rrr++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "rrr\n", 4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */
|
/* Created: 2025/12/08 14:32:10 by dgaillet #+# #+# */
|
||||||
/* Updated: 2025/12/12 11:39:17 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 13:57:47 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,14 +17,18 @@ void ra(t_stacks *stacks)
|
|||||||
{
|
{
|
||||||
if (stacks && stacks->a && stacks->a->next)
|
if (stacks && stacks->a && stacks->a->next)
|
||||||
stacks->a = stacks->a->next;
|
stacks->a = stacks->a->next;
|
||||||
write(1, "ra\n", 3);
|
stacks->ra++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "ra\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rb(t_stacks *stacks)
|
void rb(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (stacks && stacks->b && stacks->b->next)
|
if (stacks && stacks->b && stacks->b->next)
|
||||||
stacks->b = stacks->b->next;
|
stacks->b = stacks->b->next;
|
||||||
write(1, "rb\n", 3);
|
stacks->rb++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "rb\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void rr(t_stacks *stacks)
|
void rr(t_stacks *stacks)
|
||||||
@@ -33,5 +37,7 @@ void rr(t_stacks *stacks)
|
|||||||
stacks->a = stacks->a->next;
|
stacks->a = stacks->a->next;
|
||||||
if (stacks && stacks->b && stacks->b->next)
|
if (stacks && stacks->b && stacks->b->next)
|
||||||
stacks->b = stacks->b->next;
|
stacks->b = stacks->b->next;
|
||||||
write(1, "rr\n", 3);
|
stacks->rr++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "rr\n", 3);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ t_stack *new_stack(int value)
|
|||||||
if (!new)
|
if (!new)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
new->value = value;
|
new->value = value;
|
||||||
|
new->index = -1;
|
||||||
new->next = NULL;
|
new->next = NULL;
|
||||||
new->previous = NULL;
|
new->previous = NULL;
|
||||||
return (new);
|
return (new);
|
||||||
|
|||||||
@@ -21,3 +21,12 @@ void stack_clear_all(t_stack *stack, t_stack *first)
|
|||||||
stack_clear_all(stack->next, first);
|
stack_clear_all(stack->next, first);
|
||||||
free(stack);
|
free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_all(t_stacks *stacks)
|
||||||
|
{
|
||||||
|
if (stacks->a)
|
||||||
|
stack_clear_all(stacks->a, stacks->a);
|
||||||
|
if (stacks->b)
|
||||||
|
stack_clear_all(stacks->b, stacks->b);
|
||||||
|
free(stacks);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */
|
/* Created: 2025/12/08 14:48:44 by mteriier #+# #+# */
|
||||||
/* Updated: 2025/12/12 11:38:52 by dgaillet ### ########lyon.fr */
|
/* Updated: 2026/01/08 13:58:13 by dgaillet ### ########lyon.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -24,7 +24,9 @@ void sa(t_stacks *stacks)
|
|||||||
stock = a->value;
|
stock = a->value;
|
||||||
a->value = a->next->value;
|
a->value = a->next->value;
|
||||||
a->next->value = stock;
|
a->next->value = stock;
|
||||||
write(1, "sa\n", 3);
|
stacks->sa++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "sa\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sb(t_stacks *stacks)
|
void sb(t_stacks *stacks)
|
||||||
@@ -38,7 +40,9 @@ void sb(t_stacks *stacks)
|
|||||||
stock = b->value;
|
stock = b->value;
|
||||||
b->value = b->next->value;
|
b->value = b->next->value;
|
||||||
b->next->value = stock;
|
b->next->value = stock;
|
||||||
write(1, "sb\n", 3);
|
stacks->sb++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "sb\n", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ss(t_stacks *stacks)
|
void ss(t_stacks *stacks)
|
||||||
@@ -63,5 +67,7 @@ void ss(t_stacks *stacks)
|
|||||||
a->value = a->next->value;
|
a->value = a->next->value;
|
||||||
a->next->value = stock;
|
a->next->value = stock;
|
||||||
}
|
}
|
||||||
write(1, "ss\n", 3);
|
stacks->ss++;
|
||||||
|
if (stacks->print)
|
||||||
|
secure_write(1, "ss\n", 3);
|
||||||
}
|
}
|
||||||
|
|||||||
37
test_one.c
37
test_one.c
@@ -1,37 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_one.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
|
|
||||||
/* Updated: 2025/12/22 12:34:35 by mteriier ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "push_swap.h"
|
|
||||||
#include "parsing.h"
|
|
||||||
#include "medium_headers.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int test1(int argc, char **argv)
|
|
||||||
{
|
|
||||||
t_stacks *piles;
|
|
||||||
t_tab *preset;
|
|
||||||
|
|
||||||
piles = NULL;
|
|
||||||
if (argc > 1)
|
|
||||||
{
|
|
||||||
piles = init_big_stacks(argc, argv);
|
|
||||||
preset = get_tabs(piles->a, range_bucket(piles->a));
|
|
||||||
bucket_algo(piles, preset, range_bucket(piles->a));
|
|
||||||
}
|
|
||||||
if (piles->a)
|
|
||||||
stack_clear_all(piles->a, piles->a);
|
|
||||||
if (piles->b)
|
|
||||||
stack_clear_all(piles->b, piles->b);
|
|
||||||
free(piles);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
765
test_push_swap.sh
Executable file
765
test_push_swap.sh
Executable 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
|
||||||
Reference in New Issue
Block a user