diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/piscine/evalexpr/tests | |
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/evalexpr/tests')
| -rwxr-xr-x | graphs/piscine/evalexpr/tests/tests.sh | 82 | ||||
| -rw-r--r-- | graphs/piscine/evalexpr/tests/unit_tests.c | 208 |
2 files changed, 290 insertions, 0 deletions
diff --git a/graphs/piscine/evalexpr/tests/tests.sh b/graphs/piscine/evalexpr/tests/tests.sh new file mode 100755 index 0000000..920f09b --- /dev/null +++ b/graphs/piscine/evalexpr/tests/tests.sh @@ -0,0 +1,82 @@ +#!/bin/sh + +REF_OUT="ref.out" +TEST_OUT="test.out" + +testrpn() +{ + echo "$2" > "$REF_OUT" + echo "Evaluating '$1' in RPN notation..." + echo "$1" | ./evalexpr -rpn > "$TEST_OUT" + diff "$REF_OUT" "$TEST_OUT" && echo "Success" +} + +testeval() +{ + echo "$1" | bc 2> /dev/null > "$REF_OUT" + echo "Evaluating '$1' in standard notation..." + echo "$1" | ./evalexpr > "$TEST_OUT" + diff "$REF_OUT" "$TEST_OUT" && echo "Success" +} + +testerror() +{ + echo "Testing error code '$2'..." + echo "$1" | ./evalexpr + error="$(echo $?)" + [ "$2" -eq "$error" ] && echo "Succesful failure" || echo "Wrong error $error" +} + +clean() +{ + rm "$REF_OUT" "$TEST_OUT" +} + +# RPN + +echo "Tests for RPN:" +echo "======" + +testrpn "1 1 +" 2 +testrpn "5 2 2 ^ 3 + *" 35 +testrpn "10 6 9 3 + 0 11 - * / * 17 + 5 +" 22 +testrpn "3 4 5 * 3 + -" "-20" +testrpn "3 2 % 9 3 1 2 + * / -" 0 + +echo +echo "=============================================" +echo + +# Standard + +echo "Tests for standard notation:" +echo "======" + +testeval "1 + 1" +testeval " 1 + 1 +1 " +testeval "2 * 2" +testeval "5 * (2 + 4)" +testeval "5 * (2 % 4)" +testeval " 5 *(2 ^4) " +testeval " 5 *(2 ^4 " + +echo +echo "=============================================" +echo + +# Errors + +echo "Error tests:" +echo "======" + +testerror "" 0 +testerror "a+1" 1 +testerror "1%0" 3 + +echo "Testing error code '4'..." +./evalexpr --toto 2> /dev/null +echo $? + +# Cleanup + +clean diff --git a/graphs/piscine/evalexpr/tests/unit_tests.c b/graphs/piscine/evalexpr/tests/unit_tests.c new file mode 100644 index 0000000..ed445a0 --- /dev/null +++ b/graphs/piscine/evalexpr/tests/unit_tests.c @@ -0,0 +1,208 @@ +#include <criterion/criterion.h> +#include <criterion/assert.h> +#include <stddef.h> +#include <string.h> + +#include "../src/evalexpr.h" + +TestSuite(parse_number); + +Test(parse_number, parse_42) +{ + size_t o; + int actual = parse_number("42", &o); + cr_expect(actual == 42, "Attendu : %d, renvoyé : %d", 42, actual); + cr_expect(o == 1, "Décalage attendu : %d, renvoyé : %zu", 1, o); +} + +Test(parse_number, parse_4) +{ + size_t o; + int actual = parse_number("4", &o); + cr_expect(actual == 4, "Attendu : %d, renvoyé : %d", 4, actual); + cr_expect(o == 0, "Décalage attendu : %d, renvoyé : %zu", 0, o); +} + +TestSuite(my_pow); + +Test(my_pow, my_pow00) +{ + int actual = my_pow(0, 0); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} + +Test(my_pow, my_pown0) +{ + int actual = my_pow(50, 0); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} +Test(my_pow, my_pow0n) +{ + int actual = my_pow(0, 42); + cr_expect(actual == 0, "Attendu : %d, renvoyé : %d", 0, actual); +} +Test(my_pow, my_powab) +{ + int actual = my_pow(3,3); + cr_expect(actual == 27, "Attendu : %d, renvoyé : %d", 27, actual); +} +Test(my_pow, my_powab_2) +{ + int actual = my_pow(4, 2); + cr_expect(actual == 16, "Attendu : %d, renvoyé : %d", 16, actual); +} +Test(my_pow, my_powab_3) +{ + int actual = my_pow(10, 3); + cr_expect(actual == 1000, "Attendu : %d, renvoyé : %d", 1000, actual); +} +Test(my_pow, my_pow1n) +{ + int actual = my_pow(1, 50); + cr_expect(actual == 1, "Attendu : %d, renvoyé : %d", 1, actual); +} + +TestSuite(RPN); + +Test(RPN, evalrpn_easiest) +{ + const char test[] = "1 1 +"; + int expected = 0; + int retval; + int res = 2; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_35) +{ + const char test[] = "5 2 2 ^ 3 + *"; + int expected = 0; + int retval; + int res = 35; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_22) +{ + const char test[] = "10 6 9 3 + 0 11 - * / * 17 + 5 +"; + int expected = 0; + int retval; + int res = 22; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_minus20) +{ + const char test[] = "3 4 5 * 3 + -"; + int expected = 0; + int retval; + int res = -20; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} +Test(RPN, evalrpn_zero) +{ + const char test[] = "3 2 % 9 3 1 2 + * / -"; + int expected = 0; + int retval; + int res = 0; + int actual = evalrpn(test, &retval); + cr_expect(actual == expected, "%s => Retour attendu : %d, renvoyé : %d", test, expected, actual); + cr_expect(retval == res, "%s => Résultat attendu : %d, reçu : %d", test, res, retval); +} + +TestSuite(Precedence); + +Test(Precedence, parenthesis_above_all) +{ + struct token pleft = { PAR_LEFT, '(' }; + struct token pright = { PAR_RIGHT, ')' }; + struct token unplus = { UN_PLUS, '+' }; + struct token exp = { EXP, '^' }; + struct token mul = { MUL, '*' }; + struct token minus = { SUB, '-' }; + int eq = opcmp(&pleft, &pright); + int sup = opcmp(&pleft, &unplus); + int inf = opcmp(&unplus, &pright); + int parftw = opcmp(&pleft, &exp); + int par4ever = opcmp(&pright, &mul); + int paragain = opcmp(&pright, &minus); + cr_expect(eq == 0, "Wrong order (equal)"); + cr_expect(sup > 0, "Wrong order (>)"); + cr_expect(inf < 0, "Wrong order (<)"); + cr_expect(parftw > 0, "Wrong order (>)"); + cr_expect(par4ever > 0, "Wrong order (>)"); + cr_expect(paragain > 0, "Wrong order (>)"); +} + +Test(Precedence, other_precedence_tests) +{ + struct token exp = { EXP, '^' }; + struct token mul = { MUL, '*' }; + struct token unplus = { UN_PLUS, '+' }; + struct token minus = { SUB, '-' }; + struct token plus = { ADD, '+' }; + int eq = opcmp(&minus, &plus); + int sup = opcmp(&exp, &mul); + int inf = opcmp(&plus, &unplus); + + cr_expect(eq == 0, "Wrong order (equal)"); + cr_expect(sup > 0, "Wrong order (>)"); + cr_expect(inf < 0, "Wrong order (<)"); +} + +TestSuite(ShuntingTests); + +Test(ShuntingTests, shunt_simplest) +{ + char *rpn; + const char *expr = "1 + 1"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "1 1 +") == 0, "Expected '1 1 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_nico) +{ + char *rpn; + const char *expr = "1 + 1 + 1"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "1 1 + 1 +") == 0, "Expected '1 1 + 1 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_harderdaddy) +{ + char *rpn; + const char *expr = "5*(2^2+3)"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "5 2 2 ^ 3 + *") == 0, "Expected '5 2 2 ^ 3 + *', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_numbers) +{ + char *rpn; + const char *expr = "42 + 50"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "42 50 +") == 0, "Expected '42 50 +', got %s", rpn); + free(rpn); +} + +Test(ShuntingTests, shunt_mod) +{ + char *rpn; + const char *expr = "42 % 50"; + int actual = shunting_yard(expr, &rpn); + cr_expect(actual == 0, "Expected shunting_yard return value %d, got %d", 0, actual); + cr_expect(strcmp(rpn, "42 50 %") == 0, "Expected '42 50 +', got %s", rpn); + free(rpn); +} |
