summaryrefslogtreecommitdiff
path: root/rushs/tinyprintf/evalexpr/tests/unit_tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/tinyprintf/evalexpr/tests/unit_tests.c')
-rw-r--r--rushs/tinyprintf/evalexpr/tests/unit_tests.c208
1 files changed, 208 insertions, 0 deletions
diff --git a/rushs/tinyprintf/evalexpr/tests/unit_tests.c b/rushs/tinyprintf/evalexpr/tests/unit_tests.c
new file mode 100644
index 0000000..ed445a0
--- /dev/null
+++ b/rushs/tinyprintf/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);
+}