Test Cases: Associativity and Precedence of Math Expressions
Posted 10-10-2021 at 11:53 PM by astrogeek
This post is part of the Alphanumeric Calculator program group.
Test cases to validate associativity and precedence handling in math expressions.
Test cases input file:
And the results using Rev.3 lexer/parser as posted to this blog.
Test cases to validate associativity and precedence handling in math expressions.
Test cases input file:
Code:
cat tests_precedence /* Test of associativity and precedence in math expressions Input here is symbolic for clarity, alpha input will test same, not shown Remember: This is integer math as performed by basic C oeprators on long long values */ numeric ######## Basic associativity 3 + 4 - 5, should be (3 + 4) - 5 = 2 3 + 4 - 5 ######## Basic associativity 3 - 4 - 5, should be (3 - 4) - 5 = -6 3 - 4 - 5 ######## Parenthetic associativity 3 - (4 - 5), should be 3 - (-1) = 4 3 - (4 - 5) ######## Basic precedence 3 + 4 * 5, should be 3 + (4 * 5) = 23 3 + 4 * 5 ######## Basic precedence 3 + 8 / 4, should be 3 + (8 / 4) = 5 3 + 8 / 4 ######## Precedence and associativity 7 * ( 4 + 7 - 16 / 2) / 3 * 12 / 2 = 42 7 * ( 4 + 7 - 16 / 2) / 3 * 12 / 2
Code:
./alphacalc tests_precedence -c ######## Basic associativity 3 + 4 - 5, should be (3 + 4) - 5 = 2 2 ######## Basic associativity 3 - 4 - 5, should be 3 - 4 -5 = -6 -6 ######## Parenthetic associativity 3 - (4 - 5), should be 3 - (-1) = 4 4 ######## Basic precedence 3 + 4 * 5, should be 3 + (4 * 5) = 23 23 ######## Basic precedence 3 + 8 / 4, should be 3 + (8 / 4) = 5 5 ######## Precedence and associativity 7 * ( 4 + 7 - 16 / 2) / 3 * 12 / 2 = 42 42
Total Comments 0