diff --git a/tests/climbing.c b/tests/climbing.c index b80bd93..d5a76cb 100644 --- a/tests/climbing.c +++ b/tests/climbing.c @@ -25,157 +25,8 @@ static void do_failure(const char *input) TestSuite(climbing); -Test(climbing, empty) -{ - do_failure(""); -} - -Test(climbing, trailing_operator) -{ - do_failure("1 +"); -} - -Test(climbing, trailing_expression) -{ - do_failure("1 1"); -} - -Test(climbing, double_operator) -{ - do_failure("1 * * 1"); -} - -Test(climbing, one) -{ - do_success("1", 1); -} - -Test(climbing, the_answer) -{ - do_success("42", 42); -} - -Test(climbing, int_max) -{ - do_success("2147483647", 2147483647); -} - -Test(climbing, whitespace) -{ - do_success(" 1 ", 1); -} - -Test(climbing, more_whitespace) -{ - do_success(" 1 + 2 ", 3); -} - -Test(climbing, one_plus_one) -{ - do_success("1+1", 2); -} - -Test(climbing, one_minus_one) -{ - do_success("1-1", 0); -} - -Test(climbing, additions) -{ - do_success("1+1+1+1+1", 5); -} - -Test(climbing, substractions) -{ - do_success("1-1-1-1-1", -3); -} - -Test(climbing, multiplication) -{ - do_success("2 * 3", 6); -} - -Test(climbing, multiplications) -{ - do_success("1 * 2 * 3 * 4", 24); -} - -Test(climbing, division) -{ - do_success("12 / 3", 4); -} - -Test(climbing, divisions) -{ - do_success("24 / 4 / 3 / 2", 1); -} - -Test(climbing, simple_priority) -{ - do_success("1 + 2 * 3", 7); -} - -Test(climbing, more_priority) -{ - do_success("1 + 6 / 3 + 4 * 6 + 14 / 7", 29); -} - -Test(climbing, fail_parenthesis) -{ - do_failure("(1 + 2))"); -} - -Test(climbing, simple_parenthesis) -{ - do_success("(1 + 2) * 3", 9); -} - -Test(climbing, more_parentheses) -{ - do_success("(1 + 2) * (3 - 4)", -3); -} - -Test(climbing, unary_minus) -{ - do_success("-1", -1); -} - -Test(climbing, unary_plus) -{ - do_success("+1", 1); -} - -Test(climbing, unary_torture) -{ - do_success("--+++--+-+-+-1", -1); -} - -Test(climbing, factorial) -{ - do_success("3!", 6); -} - -Test(climbing, fail_factorial) -{ - do_failure("3!!"); -} - -Test(climbing, power) -{ - do_success("4^3", 64); -} - -Test(climbing, powers) -{ - do_success("4^3^2", 262144); -} - -Test(climbing, fact_and_power) -{ - do_success("2^3!", 64); -} - -Test(climbing, altogether) -{ - do_success(" - 3 ^ 2 + - 4 * 8 / 2 + + 3! -- 2 + ((-1) + 1) * 2 ", -17); -} +#define SUCCESS(Name, Input, Expected) \ + Test(climbing, Name) { do_success(Input, Expected); } +#define FAILURE(Name, Input) \ + Test(climbing, Name) { do_failure(Input); } +#include "tests.inc" diff --git a/tests/recursive.c b/tests/recursive.c index dcc0a9d..da92103 100644 --- a/tests/recursive.c +++ b/tests/recursive.c @@ -25,157 +25,8 @@ static void do_failure(const char *input) TestSuite(recursive); -Test(recursive, empty) -{ - do_failure(""); -} - -Test(recursive, trailing_operator) -{ - do_failure("1 +"); -} - -Test(recursive, trailing_expression) -{ - do_failure("1 1"); -} - -Test(recursive, double_operator) -{ - do_failure("1 * * 1"); -} - -Test(recursive, one) -{ - do_success("1", 1); -} - -Test(recursive, the_answer) -{ - do_success("42", 42); -} - -Test(recursive, int_max) -{ - do_success("2147483647", 2147483647); -} - -Test(recursive, whitespace) -{ - do_success(" 1 ", 1); -} - -Test(recursive, more_whitespace) -{ - do_success(" 1 + 2 ", 3); -} - -Test(recursive, one_plus_one) -{ - do_success("1+1", 2); -} - -Test(recursive, one_minus_one) -{ - do_success("1-1", 0); -} - -Test(recursive, additions) -{ - do_success("1+1+1+1+1", 5); -} - -Test(recursive, substractions) -{ - do_success("1-1-1-1-1", -3); -} - -Test(recursive, multiplication) -{ - do_success("2 * 3", 6); -} - -Test(recursive, multiplications) -{ - do_success("1 * 2 * 3 * 4", 24); -} - -Test(recursive, division) -{ - do_success("12 / 3", 4); -} - -Test(recursive, divisions) -{ - do_success("24 / 4 / 3 / 2", 1); -} - -Test(recursive, simple_priority) -{ - do_success("1 + 2 * 3", 7); -} - -Test(recursive, more_priority) -{ - do_success("1 + 6 / 3 + 4 * 6 + 14 / 7", 29); -} - -Test(recursive, fail_parenthesis) -{ - do_failure("(1 + 2))"); -} - -Test(recursive, simple_parenthesis) -{ - do_success("(1 + 2) * 3", 9); -} - -Test(recursive, more_parentheses) -{ - do_success("(1 + 2) * (3 - 4)", -3); -} - -Test(recursive, unary_minus) -{ - do_success("-1", -1); -} - -Test(recursive, unary_plus) -{ - do_success("+1", 1); -} - -Test(recursive, unary_torture) -{ - do_success("--+++--+-+-+-1", -1); -} - -Test(recursive, factorial) -{ - do_success("3!", 6); -} - -Test(recursive, fail_factorial) -{ - do_failure("3!!"); -} - -Test(recursive, power) -{ - do_success("4^3", 64); -} - -Test(recursive, powers) -{ - do_success("4^3^2", 262144); -} - -Test(recursive, fact_and_power) -{ - do_success("2^3!", 64); -} - -Test(recursive, altogether) -{ - do_success(" - 3 ^ 2 + - 4 * 8 / 2 + + 3! -- 2 + ((-1) + 1) * 2 ", -17); -} +#define SUCCESS(Name, Input, Expected) \ + Test(recursive, Name) { do_success(Input, Expected); } +#define FAILURE(Name, Input) \ + Test(recursive, Name) { do_failure(Input); } +#include "tests.inc" diff --git a/tests/tests.inc b/tests/tests.inc new file mode 100644 index 0000000..85b2699 --- /dev/null +++ b/tests/tests.inc @@ -0,0 +1,32 @@ +FAILURE(empty, "") +FAILURE(trailing_operator, "1 +") +FAILURE(trailing_expression, "1 1") +FAILURE(double_operator, "1 * * 1") + +SUCCESS(one, "1", 1) +SUCCESS(the_answer, "42", 42) +SUCCESS(int_max, "2147483647", 2147483647) +SUCCESS(whitespace, " 1 ", 1) +SUCCESS(more_whitespace, " 1 + 2 ", 3) +SUCCESS(one_plus_one, "1+1", 2) +SUCCESS(one_minus_one, "1-1", 0) +SUCCESS(additions, "1+1+1+1+1", 5) +SUCCESS(substractions, "1-1-1-1-1", -3) +SUCCESS(multiplication, "2 * 3", 6) +SUCCESS(multiplications, "1 * 2 * 3 * 4", 24) +SUCCESS(division, "12 / 3", 4) +SUCCESS(divisions, "24 / 4 / 3 / 2", 1) +SUCCESS(simple_priority, "1 + 2 * 3", 7) +SUCCESS(more_priority, "1 + 6 / 3 + 4 * 6 + 14 / 7", 29) +FAILURE(fail_parenthesis, "(1 + 2))") +SUCCESS(simple_parenthesis, "(1 + 2) * 3", 9) +SUCCESS(more_parentheses, "(1 + 2) * (3 - 4)", -3) +SUCCESS(unary_minus, "-1", -1) +SUCCESS(unary_plus, "+1", 1) +SUCCESS(unary_torture, "--+++--+-+-+-1", -1) +SUCCESS(factorial, "3!", 6) +FAILURE(fail_factorial, "3!!") +SUCCESS(power, "4^3", 64) +SUCCESS(powers, "4^3^2", 262144) +SUCCESS(fact_and_power, "2^3!", 64) +SUCCESS(altogether, " - 3 ^ 2 + - 4 * 8 / 2 + + 3! -- 2 + ((-1) + 1) * 2 ", -17)