LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Blogs > astrogeek
User Name
Password

Notices


Rate this Entry

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:

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
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:03 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration