Skip to content

Instantly share code, notes, and snippets.

@mcclane
Created October 16, 2018 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcclane/71bf19b57defbcbb596297666b1cff55 to your computer and use it in GitHub Desktop.
Save mcclane/71bf19b57defbcbb596297666b1cff55 to your computer and use it in GitHub Desktop.
# Start of unittest - add to completely test functions in exp_eval
import unittest
from exp_eval import *
class test_expressions(unittest.TestCase):
def test_postfix_eval_addition(self):
"""Test subtraction with postfix_eval"""
self.assertAlmostEqual(postfix_eval("3 5 +"), 8)
def test_postfix_eval_subtract(self):
"""Test subtraction with postfix_eval"""
self.assertAlmostEqual(postfix_eval("7 4 -"), 3)
def test_postfix_eval_multiplication(self):
"""Test multiplication with postfix_eval"""
self.assertAlmostEqual(postfix_eval("4 11 *"), 44)
def test_postfix_eval_division(self):
"""Test division with postfix_eval"""
self.assertAlmostEqual(postfix_eval("4 2 /"), 2)
def test_postfix_eval_exponentiation(self):
"""Test exponentiation with postfix_eval"""
self.assertAlmostEqual(postfix_eval("2 5 **"), 32)
def test_postfix_eval_bitshift(self):
"""Test bitshift operations with postfix_eval"""
self.assertAlmostEqual(postfix_eval("2 10 <<"), 2048)
self.assertAlmostEqual(postfix_eval("10 2 >>"), 2)
self.assertAlmostEqual(postfix_eval("1 2 >>"), 0)
def test_postfix_eval_bitshift_illegal_operand(self):
"""Make sure a float bitshift raises an exception"""
try:
postfix_eval("1.02 2.02 >>")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Illegal bit shift operand")
try:
postfix_eval("1.02 2.02 <<")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Illegal bit shift operand")
try:
postfix_eval("1 2 1 / >>")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Illegal bit shift operand")
def test_postfix_eval_02(self):
"""Test postfix_eval with a string, make sure it raises an invalid token exception"""
try:
postfix_eval("blah")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Invalid token")
def test_postfix_eval_03(self):
"""Try postfix_eval with insufficient operands"""
try:
postfix_eval("4 +")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Insufficient operands")
def test_postfix_eval_04(self):
"""Try postfix eval with too many operands"""
try:
postfix_eval("1 2 3 +")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Too many operands")
def test_postfix_eval_05_zero_divisor(self):
"""Try postfix_eval with a zero divisor"""
with self.assertRaises(ValueError):
postfix_eval("1 0 /")
def test_postfix_eval_empty_string(self):
"""Test the postfix eval with an empty string"""
try:
postfix_eval("")
self.fail()
except PostfixFormatException as e:
self.assertEqual(str(e), "Insufficient operands")
def test_postfix_eval_teacher_problems(self):
"""Test postfix_eval with problems from class"""
self.assertEqual(postfix_eval("6 4 3 + 2 - * 6 /"), 5)
self.assertEqual(postfix_eval("5 2 4 * + 7 2 - 4 6 2 / 2 - * + 4 - +"), 18)
def test_postfix_eval_one_operand(self):
"""Test postfix_eval with one operand"""
self.assertAlmostEqual(postfix_eval("1"), 1)
def test_postfix_eval_infix_vs_postfix(self):
"""Test postfix_eval and infix_to_postfix by evaluating an expression with python"""
infix = "1 / 2 + 3 - 4 * 5 + 6 ** 2"
postfix = infix_to_postfix(infix)
self.assertEqual(postfix_eval(postfix), eval(infix))
infix = "1 << ( 2 - 3 + 4 * 5 + ( 6 ** 2 ) )"
postfix = infix_to_postfix(infix)
self.assertEqual(postfix_eval(postfix), eval(infix))
infix = "2 ** 3 + -4 - 3 ** 3 / 5"
postfix = infix_to_postfix(infix)
self.assertEqual(postfix_eval(postfix), eval(infix))
infix = "2 ** 3 + -4 - 3 ** 3 / 5"
postfix = infix_to_postfix(infix)
self.assertEqual(postfix_eval(postfix), eval(infix))
def test_infix_to_postfix_01(self):
"""Basic test for infix_to_postfix with one and two operands"""
self.assertEqual(infix_to_postfix("6 - 3"), "6 3 -")
self.assertEqual(infix_to_postfix("6"), "6")
def test_infix_to_postfix_02(self):
"""Test infix_to_postfix with a known example from class"""
self.assertEqual(infix_to_postfix("x ** y / ( 5 * z ) + 10"), "x y ** 5 z * / 10 +")
def test_infix_to_postfix_teacher_problems(self):
"""Test infix_to_postfix with problems from class materials"""
self.assertEqual(infix_to_postfix("5 * ( 6 + 3 - 7 * 3 + 2 ) / 6"), "5 6 3 + 7 3 * - 2 + * 6 /")
infix = "8 + 3 * 4 + ( 6 - 2 + 2 * ( 6 / 3 - 1 ) - 3 )"
postfix = infix_to_postfix(infix)
answer = "8 3 4 * + 6 2 - 2 6 3 / 1 - * + 3 - +"
self.assertEqual(postfix, answer)
def test_prefix_to_postfix(self):
"""Test prefix_to_postfix"""
self.assertEqual(prefix_to_postfix("* - 3 / 2 1 - / 4 5 6"), "3 2 1 / - 4 5 / 6 - *")
# self.assertEqual(prefix_to_postfix(
def test_failed_tests(self):
self.assertEqual("38 1.2 3.6 ** ** 2.8 / 6 3.7 ** 2 / 5 * - 3 - 23 + 1.1 / 2.2 + -2.4 5 1 ** ** - 1.6 3 / 9 2.8 -3 ** ** / 6.2 4 12.8 ** / 2 * 1.1 / 4.4 3.2 1.1 ** / 5.2 / 9.9 * - - + -2 +", infix_to_postfix("( ( 38 ** 1.2 ** 3.6 / 2.8 - 6 ** 3.7 / 2 * 5 - 3 + 23 ) / 1.1 + 2.2 ) - -2.4 ** 5 ** 1 + ( 1.6 / 3 / 9 ** 2.8 ** -3 - ( 6.2 / 4 ** 12.8 * 2 / 1.1 - 4.4 / 3.2 ** 1.1 / 5.2 * 9.9 ) ) + -2"))
self.assertEqual("3 2 >> 8 >> 3 / 17 12 4.2 / 1.2 8 << / 6 * << * 6.9 17 23 >> * 6 2.2 << / 3.2 << 56 21 / 1.4 * << 2.3 >> 4.1 * *", infix_to_postfix("( 3 >> 2 ) >> 8 / 3 * 17 << ( 12 / 4.2 / 1.2 << 8 * 6 ) * ( ( 6.9 * 17 >> 23 / 6 << 2.2 ) << 3.2 << ( 56 / 21 * 1.4 ) >> 2.3 * 4.1 )"))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment