#!/bin/bash # ---------------------------------------------------------------------- # COLORS # ---------------------------------------------------------------------- GRAY="\e[0;30m" RED="\e[0;31m" GREEN="\e[0;32m" YELLOW="\e[0;33m" BLUE="\e[0;34m" PURPLE="\e[0;35m" CYAN="\e[0;36m" WHITE="\e[0;37m" B_GRAY="\e[1;30m" B_RED="\e[1;31m" B_GREEN="\e[1;32m" B_YELLOW="\e[1;33m" B_BLUE="\e[1;34m" B_PURPLE="\e[1;35m" B_CYAN="\e[1;36m" B_WHITE="\e[1;37m" RESET="\e[0m" # ---------------------------------------------------------------------- # CONFIGURATION # ---------------------------------------------------------------------- NAME="computor" BINARY="./$NAME" if [[ ! -x "$BINARY" ]]; then echo -e >&2 $B_RED"Error:$RESET binary $B_PURPLE'$BINARY'$RESET not found or not executable." echo >&2 "Did you compile your C program?" exit 1 fi # flags SKIP_REDUCED=false SKIP_SOLUTION=false RUN_ONLY=false FLAGS="" # default empty # Parse command-line arguments while [[ "$#" -gt 0 ]]; do case $1 in --skip-reduced) SKIP_REDUCED=true ;; --skip-solution) SKIP_SOLUTION=true ;; --run-only) RUN_ONLY=true ;; -b) FLAGS="$FLAGS -b" ;; # append -b to FLAGS *) echo "Unknown parameter passed: $1"; exit 1 ;; esac shift done # ---------------------------------------------------------------------- # TEST LOGIC # ---------------------------------------------------------------------- run_test() { local test_name="$1" local input="$2" local expected="$3" local check_error="${4:-no_error}" # Default: false (compare output) # If RUN_ONLY is true, skip all test logic and just run the binary if [[ "$RUN_ONLY" == "true" ]]; then printf $B_PURPLE"Running: '%s'$RESET\n" "$test_name" printf "Input: '%s'\n" "$input" printf "Output:\n" printf "%s" "$input" | "$BINARY" $FLAGS 2>&1 printf "\n" return fi printf $B_PURPLE"Test : '%s'$RESET - " "$test_name" # Run the program and capture the output out=$(printf "%s" "$input" | "$BINARY" $FLAGS 2>&1) exit_code=$? # If SKIP_REDUCED is true, remove the "Reduced form" line from both output and expected if [[ "$SKIP_REDUCED" == "true" ]]; then out=$(echo "$out" | grep -v "Reduced form:") expected=$(echo "$expected" | grep -v "Reduced form:") fi # If SKIP_SOLUTION is true, remove all lines after the 2nd line following "Polynomial degree:" if [[ "$SKIP_SOLUTION" == "true" ]]; then # Get the line number of "Polynomial degree:" line_num=$(echo "$out" | grep -n "Polynomial degree:" | head -1 | cut -d: -f1) if [[ -n "$line_num" ]]; then # Keep lines up to 1 lines after "Polynomial degree:" out=$(echo "$out" | head -n $((line_num + 1))) expected=$(echo "$expected" | head -n $((line_num + 1))) fi fi # If check_error is true, verify if the output contains an error message or non-zero exit code if [[ "$check_error" == "error" ]]; then if [[ $exit_code -ne 0 || "$out" == *"Error"* ]]; then printf $B_GREEN"PASS$RESET (Input: '%s')\n" "$input" else printf $B_RED"FAIL$RESET\n" printf " "$B_WHITE"Input:$RESET '%s'\n" "$input" printf " "$B_WHITE"Got:$RESET\n" printf '%s\n' "$out" | sed 's/^/ | /' printf " "$B_WHITE"Expected:$RESET an error (non-zero exit code or 'Error' in output: $exit_code)\n" echo fi else # Compare with expected output if [[ "$out" == "$expected" ]]; then printf $B_GREEN"PASS$RESET (Input: '%s')\n" "$input" else printf $B_RED"FAIL$RESET\n" printf " "$B_WHITE"Input:$RESET '%s'\n" "$input" printf " "$B_WHITE"Got:$RESET\n" printf '%s\n' "$out" | sed 's/^/ | /' printf " "$B_WHITE"Expected:$RESET\n" printf '%s\n' "$expected" | sed 's/^/ | /' echo fi fi } # ---------------------------------------------------------------------- # TESTS CASES # ---------------------------------------------------------------------- run_test \ "1. degree 2" \ "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" "\ Reduced form: 2 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: -√(2/3) √(2/3)" run_test \ "2. degree 2" \ "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1" "\ Reduced form: 2 * x^0 - 4 * x^1 + 3.4 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: 4/6.8 + 3.34664i/6.8 4/6.8 - 3.34664i/6.8" run_test \ "3. degree 2" \ "3 * x^2 + 2 * x^2 = 5 * x^1" "\ Reduced form: 0 * x^0 - 5 * x^1 + 5 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly positive, the two solutions are: 13 -12" run_test \ "4. flag -e" \ '-e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4"' \ "" \ error run_test \ "5. degree 2" \ "3*x^2 + 2x = 0" "\ Reduced form: 0 * x^0 + 2 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly positive, the two solutions are: 2.66667 -3.33333" run_test \ "6. degree 2" \ "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" "\ Reduced form: 2 * x^0 - 4.123 * x^1 + 3.4 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: 4.123/6.8 + 3.19388i/6.8 4.123/6.8 - 3.19388i/6.8" run_test \ "7. float exponent" \ "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1.2" \ "" \ error run_test \ "8. degree 4" \ "3x^2 + 2x -7x^4 = x^4" "\ Reduced form: 0 * x^0 + 2 * x^1 + 3 * x^2 + 6 * x^4 = 0 Polynomial degree: 4 The polynomial degree is strictly greater than 2, I can't solve." run_test \ "9. degree 2" \ "3x² + 2x -7x¹ = x" "\ Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: -1/6 + 9.11043i/6 -1/6 - 9.11043i/6" run_test \ "10. degree 2" \ "3x² + 2x -7 = x" "\ Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: -1/6 + 9.11043i/6 -1/6 - 9.11043i/6" run_test \ "11. degree 2" \ "-3x² + 2x -7 = x" "\ Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: -1/6 + 9.11043i/6 -1/6 - 9.11043i/6" run_test \ "12. degree 2" \ "+3x² + 2x -7 = x" "\ Reduced form: 7 * x^0 + 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: -1/6 + 9.11043i/6 -1/6 - 9.11043i/6" run_test \ "13. degree 2" \ "3x² + 0x -7 = x" "\ Reduced form: 7 * x^0 - 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: 1/6 + 9.11043i/6 1/6 - 9.11043i/6" run_test \ "14. degree 2" \ "3x² + 0x -0 = x" "\ Reduced form: 0 * x^0 - 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly positive, the two solutions are: 1.66667 -1.33333" run_test \ "15. degree 2" \ "3x² + 2x -0 = x" "\ Reduced form: 0 * x^0 + 1 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly positive, the two solutions are: 1.33333 -1.66667" run_test \ "16. degree 1" \ "3x + 2x -0 = x" "\ Reduced form: 0 * x^0 + 4 * x^1 = 0 Polynomial degree: 1 The solution is: 0" run_test \ "17. degree 2" \ "3x² + x -0 = x" "\ Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is equal to zero, the solution is: 0" run_test \ "18. degree 2" \ "0x² + x -0 = x" "\ Reduced form: 0 * x^0 = 0 Any real number is a solution." run_test \ "19. degree 5" \ "2x⁵ + x -0 = -7x^5" "\ Reduced form: 0 * x^0 + 1 * x^1 + 0 * x^2 + 0 * x^3 + 0 * x^4 - 5 * x^5 = 0 Polynomial degree: 5 The polynomial degree is strictly greater than 2, I can't solve." run_test \ "20. degree 1" \ "2x + x -0 = -7x" "\ Reduced form: 0 * x^0 - 4 * x^1 = 0 Polynomial degree: 1 The solution is: 0" run_test \ "21. degree 1" \ "2x + x -3 = -7x" "\ Reduced form: 3 * x^0 - 4 * x^1 = 0 Polynomial degree: 1 The solution is: 0.75" run_test \ "23. degree 2 without [=]" \ "3x^2" \ "" \ error run_test \ "24. degree 2" \ "3x^2 = 0" "\ Reduced form: 0 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is equal to zero, the solution is: 0" run_test \ "25. degree 2" \ "3x^2 + 2 = 0" "\ Reduced form: 2 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Discriminant is strictly negative, the two complex solutions are: 4.89898i/6 -4.89898i/6" run_test \ "26. degree 2 pure" \ "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\ Reduced form: -2 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Radicant is strictly positive, the two solutions are: √(2/3) -√(2/3)" run_test \ "27. degree 2 pure" \ "9 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x" "\ Reduced form: -2 * x^0 + 0 * x^1 + 9 * x^2 = 0 Polynomial degree: 2 Radicant is strictly positive, the two solutions are: √(2)/3 -√(2)/3" run_test \ "28. degree 2 pure" \ "3 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\ Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Radicant is strictly positive, the two solutions are: 2/√(3) -2/√(3)" run_test \ "29. degree 2 pure" \ "16 * x^2 + 5 * x^1 - 4 * x^0 = 5 * x" "\ Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Radicant is strictly positive, the two solutions are: 1/2 -1/2" run_test \ "30. degree 2 pure" \ "4 * x^2 + 5 * x^1 - 16 * x^0 = 5 * x" "\ Reduced form: -4 * x^0 + 0 * x^1 + 3 * x^2 = 0 Polynomial degree: 2 Radicant is strictly positive, the two solutions are: 2 -2"