Advertisement
colinm86

Untitled

Oct 13th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. from prefix_exp import *
  2. import unittest
  3.  
  4. class TestStringMethods(unittest.TestCase):
  5.     """Unit tests for prefix_exp file
  6.    Arguments:
  7.        unittest {unittest}
  8.    """
  9.     def test_tokenize(self):
  10.         self.assertEqual(tokenize('1*(22+3)/5'), ['1', '*', '(', '22', '+', '3', ')', '/', '5'])
  11.         self.assertEqual(tokenize('1  + 1'), ['1', '+', '1'])
  12.         self.assertEqual(tokenize('244+( 77 / 3) * 4'), ['244', '+', '(','77','/','3',')','*','4'])  
  13.         self.assertEqual(tokenize(''), [])
  14.    
  15.     def test_checkbalance(self):
  16.         self.assertEqual(checkbalance(['(','(',')',')','(','(',]),False)
  17.         self.assertEqual(checkbalance(['(','(',')',')','(',')',]),True)
  18.         self.assertEqual(checkbalance([]),True)
  19.    
  20.     def test_infixToPostfix(self):
  21.         with self.assertRaises(Exception):
  22.             infixToPostfix('1*(22+3))/5')
  23.         self.assertEqual(infixToPostfix('1 * 5'),'1 5 *')
  24.         self.assertEqual(infixToPostfix('(1 - 3) * (6*4)'),'1 3 - 6 4 * *')
  25.         self.assertEqual(infixToPostfix('1*(22+3)/5'),'1 22 3 + * 5 /')
  26.    
  27.     def test_postfixEval(self):
  28.         with self.assertRaises(Exception):
  29.             postfixEval([1,2,3])
  30.         self.assertEqual(postfixEval('1 5 *'),5)
  31.         self.assertEqual(postfixEval('1 3 - 6 4 * *'),-48)
  32.         self.assertEqual(postfixEval('1 22 3 + * 5 /'),5.0)
  33.    
  34.     def test_doMath(self):
  35.         with self.assertRaises(Exception):
  36.             doMath(3,3,3)
  37.         with self.assertRaises(Exception):
  38.             doMath('*',3,'4')
  39.         with self.assertRaises(Exception):
  40.             doMath('*','3',4)
  41.         with self.assertRaises(ArithmeticError):
  42.             doMath('?',3,4)
  43.         self.assertEqual(doMath('*',3,4),12)
  44.         self.assertEqual(doMath('*',-3,4),-12)
  45.         self.assertEqual(doMath('/',20,4),5)
  46.         self.assertEqual(doMath('+',20,4),24)
  47.         self.assertEqual(doMath('-',20,4),16)
  48.  
  49. if __name__ == '__main__':
  50.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement