From d02658b67dd1bd695b362c24cc17ba0286d984fb Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 1 Jan 2020 03:35:13 +0300 Subject: test: add more parser tests --- test/unit_tests/parser.cpp | 51 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/unit_tests/parser.cpp b/test/unit_tests/parser.cpp index 594e162..231be3d 100644 --- a/test/unit_tests/parser.cpp +++ b/test/unit_tests/parser.cpp @@ -29,35 +29,58 @@ const std::vector input{ "-1", "--1", "+--+-2", + // Basic exponentiation: + "0^0", + "4^.5", + " 4 ^ ---0.5 ", + // Exponentiation is right-associative: + "2 ^ 3 ^ 3", + "(2 ^ 3) ^ 3", + // Exponentiation has higher precedence than the unary minus: + "(-2) ^ 2", + "-2 ^ 2", + "(.5 ^ -1) ^ 4", + ".5 ^ -1 ^ 4", // Basic binary operators: " 1 + 2 ", " 2 * 1 + 3 ", " 2 * (1 + 3) ", " 2 * (1 + 3 * (1 - -3)) ", + " -2 * (1 + --3 * (1 - -3)) ", // Looks weird, but also works in e.g. Python: " -2 * -+--- (3 + -100e-1) ", - // Power operator is right-associative: - "2 ^ 3 ^ 3", - "(2 ^ 3) ^ 3", - // Power operator has higher precedence than the unary minus: - "(.5 ^ -1) ^ 4", - ".5 ^ -1 ^ 4", + " -2^2 --+- -3^0 / (4 + +12) ^.5^2 ", + " -2^2 --+- -3^0 / ((4 + +12) ^.5)^2 ", }; const std::vector expected{ + // Constants and unary operators: 1, -1, 1, -2, + // Basic exponentiation: + 1, + 2, + 0.5, + // Exponentiation is right-associative: + 134217728, + 512, + // Exponentiation has higher precedence than the unary minus: + 4, + -4, + 16, + 2, + // Basic binary operators: 3, 5, 8, 26, + -26, + // Looks weird, but also works in e.g. Python: 14, - 134217728, - 512, - 16, - 2, + -3.5, + -3.9375, }; } @@ -66,8 +89,11 @@ namespace exec::invalid { const std::vector input{ "", + "-", + "-+-", // Missing operand: " 1 + ", + "-2 + 3 ^ -", // Unmatched parentheses: " 2 * (1 + 3 ", " 2 * (1 + 3) )", @@ -76,6 +102,11 @@ const std::vector input{ const std::vector error_msg{ "server error: parser error: expected '-', '+', '(' or a number", "server error: parser error: expected '-', '+', '(' or a number", + "server error: parser error: expected '-', '+', '(' or a number", + // Missing operand: + "server error: parser error: expected '-', '+', '(' or a number", + "server error: parser error: expected '-', '+', '(' or a number", + // Unmatched parentheses: "server error: parser error: missing closing ')'", "server error: parser error: expected a binary operator", }; -- cgit v1.2.3