From d770d7fc879679a2add944806275e885eb574fff Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 14 May 2026 02:13:58 +0200 Subject: [PATCH] add tester --- Makefile | 50 +--------- README.md | 4 +- tester.sh | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+), 48 deletions(-) create mode 100644 tester.sh diff --git a/Makefile b/Makefile index ec9a0e4..bef8498 100644 --- a/Makefile +++ b/Makefile @@ -107,52 +107,10 @@ $(NAME): $(OBJS) run: $(NAME) @echo $(B_PURPLE)"\n---------------------------------------------\n1. degree 2 \n"$(RESET) -./$(NAME) $(RUN_FLAGS) "3 * x^2 + 5 * x^1 - 2 * x^0 = 5 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n2. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n3. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3 * x^2 + 2 * x^2 = 5 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n4. flag -e SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) -e "3 * x^2 + 2 * x - 7 * x^4 = 1 * x^4" - @echo $(B_PURPLE)"\n---------------------------------------------\n5. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3*x^2 + 2x = 0" - @echo $(B_PURPLE)"\n---------------------------------------------\n6. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5.123 * x^1" - @echo $(B_PURPLE)"\n---------------------------------------------\n7. float exponent SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3.4 * x^2 + 1 * x^1 - 2.0 * x^0 = 5 * x^1.2" - @echo $(B_PURPLE)"\n---------------------------------------------\n8. degree 4 SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x^2 + 2x -7x^4 = x^4" - @echo $(B_PURPLE)"\n---------------------------------------------\n9. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7x¹ = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n10. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n11. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "-3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n12. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "+3x² + 2x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n13. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + 0x -7 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n14. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + 0x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n15. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + 2x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n16. degree 1 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x + 2x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n17. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x² + x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n18. degree 2 SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "0x² + x -0 = x" - @echo $(B_PURPLE)"\n---------------------------------------------\n19. degree 5 SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "2x⁵ + x -0 = -7x^5" - @echo $(B_PURPLE)"\n---------------------------------------------\n20. degree 1 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "2x + x -0 = -7x" - @echo $(B_PURPLE)"\n---------------------------------------------\n21. degree 1 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "2x + x -3 = -7x" - @echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 SHOULD FAIL \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x^2" - @echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x^2 = 0" - @echo $(B_PURPLE)"\n---------------------------------------------\n23. degree 2 \n"$(RESET) - -./$(NAME) $(RUN_FLAGS) "3x^2 + 2 = 0" + +test: $(NAME) + bash tester.sh + clean: $(RM_OBJS) diff --git a/README.md b/README.md index 0e5c36e..3d3050c 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ ## todo -- fix error if b is 0 (b takes the value of c instead) +- create tester +- if no "=" sign return error - doing gcd for int values, and not for double values - double is nearly_equal_0 -- if no "=" sign return error ## ressources diff --git a/tester.sh b/tester.sh new file mode 100644 index 0000000..80f076d --- /dev/null +++ b/tester.sh @@ -0,0 +1,291 @@ +#!/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" +#SKIP_REDUCED=true + +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 + +# ---------------------------------------------------------------------- +# TEST LOGIC +# ---------------------------------------------------------------------- + +run_test() { + local test_name="$1" + local input="$2" + local expected="$3" + local check_error="${4:-no_error}" # Default: false (compare output) + + printf $B_PURPLE"Test : '%s'$RESET - " "$test_name" + + # Run the program and capture the output + out=$(printf "%s" "$input" | "$BINARY" 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 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\n" + 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\n" + 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: +4.89898i/6 +-4.89898i/6" + +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" \ +"3x^2" "\ +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 \ +"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"