#!/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="" 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" } # 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