Latest LQ Deal: Latest LQ Deals
 LinuxQuestions.org Test Cases: Associativity and Precedence of Math Expressions

Rate this Entry

# Test Cases: Associativity and Precedence of Math Expressions

Posted 10-11-2021 at 12:53 AM 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:

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```
And the results using Rev.3 lexer/parser as posted to this blog.

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```

All times are GMT -5. The time now is 10:33 AM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -