diff --git a/src/checker.c b/src/checker.c index 8a05fbf..6d47d16 100644 --- a/src/checker.c +++ b/src/checker.c @@ -13,6 +13,7 @@ #include "push_swap.h" #include #include +#include void execute_move(const char *move, t_state *state) { @@ -43,8 +44,8 @@ int main(int argc, char *argv[]) t_state *state; char *move; - if (!initialise_state(&state)) - return (1); + if (!initialise_state(&state) || argc == 1) + return (ft_putstr_fd("Error\n", STDERR_FILENO), 1); while (argc-- > 1) { if (!new_element(&(state->a), argv[argc])) diff --git a/src/push_swap.c b/src/push_swap.c index daf32b2..4d2ef78 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -12,21 +12,23 @@ #include "libft.h" #include "push_swap.h" +#include + int main(int argc, char *argv[]) { t_state *state; state = NULL; - if (!initialise_state(&state)) - return (1); + if (!initialise_state(&state) || argc == 1) + return (ft_putstr_fd("Error\n", STDERR_FILENO), 1); while (argc-- > 1) { if (!new_element(&(state->a), argv[argc])) return (error(state)); - if (has_duplicates(state->a)) - return (error(state)); } + if (has_duplicates(state->a)) + return (error(state)); if (ft_lstsize(state->a) <= 1 || is_sorted(state->a)) return (0); sortmore(state); diff --git a/tester.sh b/tester.sh index a548d62..a63fc47 100755 --- a/tester.sh +++ b/tester.sh @@ -1,65 +1,266 @@ #!/bin/bash # This script is used to test the program +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' +BOLD='\033[1m' + run_test_case() { - # echo "Test case $1" - MAX=0 - MIN=10000000 - AVG=0 - ITER=$2 - N=$1 - MAX_ARGS="" - for i in $(seq 1 $ITER) - do - ARG=$(seq -1000 1000 | shuf -n $N) - OPS=$(./push_swap $ARG | wc -l) - OK=$(./push_swap $ARG | ./checker $ARG) - if [ $OPS -gt $MAX ] - then - MAX_ARGS="Max: $OPS | $(echo $ARG | tr -d '\n')" - MAX=$OPS - fi - if [ $OPS -lt $MIN ] - then - MIN=$OPS - fi - AVG=$((AVG + OPS)) - if [ $OK != "OK" ] - then - echo "================================" - echo "Error" - echo "Args: $ARG" - echo "Ops: $OPS" - echo "Checker: $OK" - echo "================================" - return - fi - done - AVG=$((AVG / ITER)) - # echo -e "$1,\t$AVG,\t$MAX,\t$MIN" - echo "List size: $1 Iterations: $ITER avg: $AVG max: $MAX min: $MIN" - # echo "Max: $MAX" - # echo "Min: $MIN" - # echo "Average: $AVG" - # echo "--------------------------------" - # echo "Max Args: $MAX_ARGS" - # echo "--------------------------------" - # echo "" + # echo "Test case $1" + MAX=0 + MIN=10000000 + AVG=0 + ITER=$2 + N=$1 + MAX_ARGS="" + ABOVE_5500_COUNT=0 + # Create a temporary directory to store outputs + TMP_DIR=$(mktemp -d) + + # Function to run a single test iteration + run_iteration() { + local iter=$1 + local ARG=$(seq -1000 1000 | shuf -n $N) + local OPS=$(./push_swap $ARG | wc -l | tr -d '[:space:]') + local OK=$(./push_swap $ARG | ./checker $ARG | tr -d '[:space:]') + + echo "$OPS" > "$TMP_DIR/ops_$iter" + echo "$ARG" > "$TMP_DIR/arg_$iter" + echo "$OK" > "$TMP_DIR/ok_$iter" + } + + export -f run_iteration + export TMP_DIR MAX MIN MAX_ARGS AVG N + + # Run the iterations in parallel + seq 1 $ITER | xargs -n 1 -P 8 bash -c 'run_iteration "$@"' _ + + # Process results + for file in $TMP_DIR/ops_*; do + if [ -f "$file" ]; then + local iter=$(basename $file | cut -d'_' -f2) + local OPS=$(cat "$file") + local ARG=$(cat "$TMP_DIR/arg_$iter") + local OK=$(cat "$TMP_DIR/ok_$iter") + AVG=$((AVG + OPS)) + + if [ "$OPS" -gt "$MAX" ]; then + MAX=$OPS + MAX_ARGS="Max: $OPS | $(echo $ARG | tr -d '\n')" + fi + + if [ "$OPS" -lt "$MIN" ]; then + MIN=$OPS + fi + + if [ "$OPS" -gt 5500 ]; then + ABOVE_5500_COUNT=$((ABOVE_5500_COUNT + 1)) + fi + + if [ "$OK" != "OK" ]; then + echo "================================" + echo "Error" + echo "Args: $ARG" + echo "Ops: $OPS" + echo "Checker: $OK" + echo "================================" + fi + fi + done + + AVG=$((AVG / ITER)) + echo -e "Testing $BOLD$N elements $ITER times:$NC max:$GREEN $MAX$NC min:$RED $MIN$NC avg:$AVG" + # echo "Iterations: $ITER" + # echo "Max: $MAX" + # echo "Min: $MIN" + # echo "Average: $AVG" + # echo "--------------------------------" + # echo "Max Args: $MAX_ARGS" + # echo "--------------------------------" + # if [ "$N" -eq 500 ]; then + # echo "Number of tests with OPS > 5500: $ABOVE_5500_COUNT" + # echo "--------------------------------" + # fi + # echo "" + + # Clean up + rm -rf "$TMP_DIR" } -run_test_case 1 10 -run_test_case 2 10 -run_test_case 3 10 -run_test_case 4 10 -run_test_case 5 10 -run_test_case 10 10 -run_test_case 20 10 -run_test_case 50 10 -run_test_case 100 10 -run_test_case 200 10 -run_test_case 500 10 - + +# Test push_swap with non-numeric arguments +NON_NUMERIC_ARGS="a b c" +NON_NUMERIC_OUTPUT=$(./push_swap $NON_NUMERIC_ARGS 2>&1 >/dev/null) +if [ "$NON_NUMERIC_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Non-numeric argument test passed${NC}" +else + echo -e "${RED}Non-numeric argument test failed${NC}" +fi + +# Test checker with non-numeric arguments +NON_NUMERIC_OUTPUT=$(./checker $NON_NUMERIC_ARGS 2>&1 >/dev/null) +if [ "$NON_NUMERIC_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Non-numeric argument test passed${NC}" +else + echo -e "${RED}Non-numeric argument test failed${NC}" +fi + +# Test push_swap with duplicate arguments +DUPLICATE_ARGS="1 1 2 3" +DUPLICATE_OUTPUT=$(./push_swap $DUPLICATE_ARGS 2>&1 >/dev/null) +if [ "$DUPLICATE_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Duplicate argument test passed${NC}" +else + echo -e "${RED}Duplicate argument test failed${NC}" +fi + +# Test checker with duplicate arguments +DUPLICATE_OUTPUT=$(./checker $DUPLICATE_ARGS 2>&1 >/dev/null) +if [ "$DUPLICATE_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Duplicate argument test passed${NC}" +else + echo -e "${RED}Duplicate argument test failed${NC}" +fi + +# Test push_swap with empty arguments +EMPTY_OUTPUT=$(./push_swap 2>&1 >/dev/null) +if [ "$EMPTY_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Empty argument test passed${NC}" +else + echo -e "${RED}Empty argument test failed${NC}" +fi + +# Test checker with empty arguments +EMPTY_OUTPUT=$(./checker 2>&1 >/dev/null) +if [ "$EMPTY_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Empty argument test passed${NC}" +else + echo -e "${RED}Empty argument test failed${NC}" +fi + +# Test push_swap with invalid arguments +INVALID_ARGS="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 a" +INVALID_OUTPUT=$(./push_swap $INVALID_ARGS 2>&1 >/dev/null) +if [ "$INVALID_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Invalid argument test passed${NC}" +else + echo -e "${RED}Invalid argument test failed${NC}" +fi + +# Test checker with invalid arguments +INVALID_OUTPUT=$(./checker $INVALID_ARGS 2>&1 >/dev/null) +if [ "$INVALID_OUTPUT" == "Error" ]; then + echo -e "${GREEN}Invalid argument test passed${NC}" +else + echo -e "${RED}Invalid argument test failed${NC}" +fi + +# Test push_swap with valid arguments +VALID_ARGS="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" +VALID_OUTPUT=$(./push_swap $VALID_ARGS 2>&1 >/dev/null) +if [ "$VALID_OUTPUT" != "Error" ]; then + echo -e "${GREEN}Valid argument test passed${NC}" +else + echo -e "${RED}Valid argument test failed${NC}" +fi + +# Test checker with valid arguments +VALID_OUTPUT=$(./push_swap $VALID_ARGS | ./checker $VALID_ARGS) +if [ "$VALID_OUTPUT" == "OK" ]; then + echo -e "${GREEN}Valid argument test passed${NC}" +else + echo -e "${RED}Valid argument test failed${NC}" +fi + +#Run push_swap with non numeric parameters. The program must display "Error". +OUTPUT=$(./push_swap a b c) +if [ "$OUTPUT" == "Error" ]; then + echo -e "${GREEN}Non-numeric argument test passed${NC}" +else + echo -e "${RED}Non-numeric argument test failed${NC}" +fi + +#Run push_swap with a duplicate numeric parameter. The program must display "Error". +OUTPUT=$(./push_swap 1 1 2 3) +if [ "$OUTPUT" == "Error" ]; then + echo -e "${GREEN}Duplicate argument test passed${NC}" +else + echo -e "${RED}Duplicate argument test failed${NC}" +fi + +#Run push_swap with only numeric parameters including one greater than MAXINT. The program must display "Error". +OUTPUT=$(./push_swap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 999999999999999) +if [ "$OUTPUT" == "Error" ]; then + echo -e "${GREEN}Invalid argument test passed${NC}" +else + echo -e "${RED}Invalid argument test failed${NC}" +fi + +#Run push_swap without any parameters. The program must not display anything and give the prompt back. +OUTPUT=$(./push_swap) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Empty argument test passed${NC}" +else + echo -e "${RED}Empty argument test failed${NC}" +fi + +#Run the following command "$>./push_swap 42". The program should display nothing (0 instruction). +OUTPUT=$(./push_swap 42) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Single argument test passed${NC}" +else + echo -e "${RED}Single argument test failed${NC}" +fi + +#Run the following command "$>./push_swap 2 3". The program should display nothing (0 instruction). +OUTPUT=$(./push_swap 2 3) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Two arguments test passed${NC}" +else + echo -e "${RED}Two arguments test failed${NC}" +fi + +#Run the following command "$>./push_swap 0 1 2 3". The program should display nothing (0 instruction). +OUTPUT=$(./push_swap 0 1 2 3) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Four arguments test passed${NC}" +else + echo -e "${RED}Four arguments test failed${NC}" +fi + +#Run the following command "$>./push_swap 0 1 2 3 4 5 6 7 8 9". The program should display nothing (0 instruction). +OUTPUT=$(./push_swap 0 1 2 3 4 5 6 7 8 9) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Ten arguments test passed${NC}" +else + echo -e "${RED}Ten arguments test failed${NC}" +fi + +#Run the following command "$>./push_swap 'Between 0 and 9 randomly sorted values chosen>'. The program should display nothing (0 instruction). +OUTPUT=$(./push_swap 11 22 33 44 55 66 77 88 99) +if [ "$OUTPUT" == "" ]; then + echo -e "${GREEN}Ten arguments test passed${NC}" +else + echo -e "${RED}Ten arguments test failed${NC}" +fi + +#Run "$>ARG="2 1 0"; ./push_swap $ARG | ./checker_OS $ARG". Check that the checker program displays "OK" and that the size of the list of instructions from push_swap is 2 OR 3. Otherwise the test fails. +OUTPUT=$(ARG="2 1 0"; ./push_swap $ARG | ./checker $ARG) +if [ "$OUTPUT" == "OK" ]; then + echo -e "${GREEN}Three arguments test passed${NC}" +else + echo -e "${RED}Three arguments test failed${NC}" +fi + +#Run "$>ARG="1 5 2 4 3"; ./push_swap $ARG | ./checker_OS $ARG". Check that the checker program displays "OK" and that the size of the list of instructions from push_swap is 12. Otherwise the test fails. +OUTPUT=$(ARG="1 5 2 4 3"; ./push_swap $ARG | ./checker $ARG) +if [ "$OUTPUT" == "OK" ]; then + echo -e "${GREEN}Five arguments test passed${NC}" +else + echo -e "${RED}Five arguments test failed${NC}" +fi